Skip to content

Commit

Permalink
Scriptloading overhaul (evilfactory#163)
Browse files Browse the repository at this point in the history
* - 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.
  • Loading branch information
MapleWheels authored and TBN-MapleWheels committed Oct 25, 2023
1 parent c69c448 commit d4755f9
Showing 1 changed file with 5 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand All @@ -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())
{
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -309,6 +285,5 @@ private void OnUnload(AssemblyLoadContext alc)
CompiledAssemblyImage = null;
_dependencyResolvers.Clear();
base.Unloading -= OnUnload;
this.IsDisposed = true;
}
}

0 comments on commit d4755f9

Please sign in to comment.