using System; using System.Collections.Generic; using System.Collections.Immutable; using System.ComponentModel.DataAnnotations; using System.Diagnostics.CodeAnalysis; using System.Reflection; using System.Runtime.CompilerServices; using Barotrauma.LuaCs; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; namespace Barotrauma.LuaCs; public interface IAssemblyLoaderService : IService { public interface IFactory : IService { IAssemblyLoaderService CreateInstance(LoaderInitData initData); } /// /// Constructor record for instancing. /// /// /// /// /// Assemblies and Types in this context are for only. /// Execution of assembly data is forbidden. /// /// /// /// public record LoaderInitData( [Required] Guid InstanceId, [Required][NotNull] string Name, [Required] bool IsReferenceMode, ContentPackage OwnerPackage, Action OnUnload, Func OnResolvingManaged, Func OnResolvingUnmanagedDll); /// /// ID for this instance. /// Guid Id { get; } /// /// Indicates that the assemblies in this load context are metadata references only and not /// intended for execution. /// bool IsReferenceOnlyMode { get; } /// /// Runtime value of constant for extensibility use. /// public static readonly string InternalsAccessAssemblyName = InternalsAwareAssemblyName; /// /// Name for all runtime-compiled assemblies requiring access to internal assembly components. /// public const string InternalsAwareAssemblyName = "InternalsAwareAssembly"; /// /// Add additional locations for dependency resolution to use. /// /// /// public FluentResults.Result AddDependencyPaths(ImmutableArray paths); /// /// Compiles the supplied syntaxtrees and options into an in-memory assembly image. /// Builds metadata from loaded assemblies, only supply your own if you have in-memory images not managed by the /// AssemblyManager class. /// /// [NotNull]Name reference of the assembly. /// [IMPORTANT] This is used to reference this assembly as the true name will be forced if /// publicized assemblies are not used (InternalsVisibleTo Attrib). /// Must be supplied for in-memory assemblies. /// Must be unique to all other assemblies explicitly loaded using this context. /// Forces the assembly name to and grants access to internal. /// [IMPORTANT]Cannot be null or empty if is false. /// [NotNull]Syntax trees to compile into the assembly. /// All MetadataReferences to be used for compilation. /// [IMPORTANT] This method builds metadata from loaded assemblies, only supply your own if you have in-memory /// images not managed by the AssemblyManager class. /// [NotNull]CSharp compilation options. This method automatically adds the 'IgnoreAccessChecks' property for compilation. /// Success state of the operation. public FluentResults.Result CompileScriptAssembly( [NotNull] string assemblyName, bool compileWithInternalAccess, ImmutableArray syntaxTrees, ImmutableArray metadataReferences, CSharpCompilationOptions compilationOptions = null); /// /// Loads the assembly from the provided location and registers all new paths provided with dependency resolution. /// /// Absolute path to the managed assembly. /// Additional paths for dependency resolution. /// Success and reference to the assembly if successful. public FluentResults.Result LoadAssemblyFromFile(string assemblyFilePath, ImmutableArray additionalDependencyPaths); /// /// Returns the already loaded assembly with the same name. /// /// Name of the assembly. /// Operation success on assembly found and assembly. public FluentResults.Result GetAssemblyByName(string assemblyName); /// /// Gets the list of Types from loaded assemblies. /// /// public FluentResults.Result> GetTypesInAssemblies(); /// /// Gets the list of Types from loaded assemblies. Does not create a defensive copy and blocks loading/unloading. /// /// public IEnumerable UnsafeGetTypesInAssemblies(); /// /// Returns the first found type given it's fully qualified name. /// /// /// public FluentResults.Result GetTypeInAssemblies(string typeName); /// /// List of loaded assemblies. /// public IEnumerable Assemblies { get; } public IEnumerable AssemblyReferences { get; } }