From d4755f9a37575601cd53c5e836be32f8ae0f036d Mon Sep 17 00:00:00 2001 From: MapleWheels Date: Tue, 24 Oct 2023 17:51:48 -0400 Subject: [PATCH] Scriptloading overhaul (#163) * - Added option to RunConfig.cs for mods to compile with non-publicized assemblies. - Added default values attrib to RunConfig.cs - Added thread sleeping to CsPackageManager.Dispose() to give time before generating a strong-ref to check acl unload status. * - Added PrintWarning to ModUtils.cs - Made previously unloaded ACLs warning only. - Made plugins unload after lua script disposal. - Small code cleanups. - Cleared acl record references on unloading. - Made ACL unload run via event hook. * Made non-pub asm compilation name the same long-a** name in XML, as per Evil's request. --- .../MemoryFileAssemblyContextLoader.cs | 35 +++---------------- 1 file changed, 5 insertions(+), 30 deletions(-) diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Plugins/MemoryFileAssemblyContextLoader.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Plugins/MemoryFileAssemblyContextLoader.cs index 368b86e655..a6222bcb9c 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Plugins/MemoryFileAssemblyContextLoader.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Plugins/MemoryFileAssemblyContextLoader.cs @@ -30,12 +30,10 @@ public class MemoryFileAssemblyContextLoader : AssemblyLoadContext protected bool IsResolving; //this is to avoid circular dependency lookup. private AssemblyManager _assemblyManager; public bool IsTemplateMode { get; set; } - public bool IsDisposed { get; private set; } public MemoryFileAssemblyContextLoader(AssemblyManager assemblyManager) : base(isCollectible: true) { this._assemblyManager = assemblyManager; - this.IsDisposed = false; base.Unloading += OnUnload; } @@ -159,11 +157,11 @@ public AssemblyLoadingSuccessState CompileAndLoadScriptAssembly( if (externMetadataReferences is not null) metadataReferences.AddRange(externMetadataReferences); - // build metadata refs from default where not an in-memory compiled assembly and not the same assembly as supplied. - metadataReferences.AddRange(AssemblyLoadContext.Default.Assemblies + // build metadata refs from global where not an in-memory compiled assembly and not the same assembly as supplied. + metadataReferences.AddRange(AppDomain.CurrentDomain.GetAssemblies() .Where(a => { - if (a.IsDynamic || string.IsNullOrWhiteSpace(a.Location) || a.Location.Contains("xunit")) + if (a.IsDynamic || string.IsNullOrEmpty(a.Location) || a.Location.Contains("xunit")) return false; if (a.FullName is null) return true; @@ -174,28 +172,7 @@ public AssemblyLoadingSuccessState CompileAndLoadScriptAssembly( .Where(a => !(a.IsDynamic || string.IsNullOrEmpty(a.Location) || a.Location.Contains("xunit"))) .Select(a => MetadataReference.CreateFromFile(a.Location) as MetadataReference) ).ToList()); - - // build metadata refs from ACL assemblies from files/disk. - foreach (AssemblyManager.LoadedACL loadedAcl in _assemblyManager.GetAllLoadedACLs()) - { - if(loadedAcl.Acl.IsTemplateMode || loadedAcl.Acl.IsDisposed) - continue; - metadataReferences.AddRange(loadedAcl.Acl.Assemblies - .Where(a => - { - if (a.IsDynamic || string.IsNullOrWhiteSpace(a.Location) || a.Location.Contains("xunit")) - return false; - if (a.FullName is null) - return true; - return !externAssemblyNames.Contains(a.FullName); // exclude duplicates - }) - .Select(a => MetadataReference.CreateFromFile(a.Location) as MetadataReference) - .Union(externAssemblyRefs // add custom supplied assemblies - .Where(a => !(a.IsDynamic || string.IsNullOrEmpty(a.Location) || a.Location.Contains("xunit"))) - .Select(a => MetadataReference.CreateFromFile(a.Location) as MetadataReference) - ).ToList()); - } - + // build metadata refs from in-memory images foreach (var loadedAcl in _assemblyManager.GetAllLoadedACLs()) { @@ -275,8 +252,7 @@ protected override Assembly Load(AssemblyName assemblyName) //try resolve against other loaded alcs foreach (var loadedAcL in _assemblyManager.GetAllLoadedACLs()) { - if (loadedAcL.Acl is null || loadedAcL.Acl.IsTemplateMode || loadedAcL.Acl.IsDisposed) - continue; + if (loadedAcL.Acl is null || loadedAcL.Acl.IsTemplateMode) continue; try { @@ -309,6 +285,5 @@ private void OnUnload(AssemblyLoadContext alc) CompiledAssemblyImage = null; _dependencyResolvers.Clear(); base.Unloading -= OnUnload; - this.IsDisposed = true; } }