Skip to content

Commit

Permalink
- Added the option to not use publicized assemblies.
Browse files Browse the repository at this point in the history
  • Loading branch information
TBN-MapleWheels committed Oct 25, 2023
1 parent d4755f9 commit 7308ff9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ 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 @@ -157,11 +159,11 @@ public AssemblyLoadingSuccessState CompileAndLoadScriptAssembly(
if (externMetadataReferences is not null)
metadataReferences.AddRange(externMetadataReferences);

// build metadata refs from global where not an in-memory compiled assembly and not the same assembly as supplied.
metadataReferences.AddRange(AppDomain.CurrentDomain.GetAssemblies()
// build metadata refs from default where not an in-memory compiled assembly and not the same assembly as supplied.
metadataReferences.AddRange(AssemblyLoadContext.Default.Assemblies
.Where(a =>
{
if (a.IsDynamic || string.IsNullOrEmpty(a.Location) || a.Location.Contains("xunit"))
if (a.IsDynamic || string.IsNullOrWhiteSpace(a.Location) || a.Location.Contains("xunit"))
return false;
if (a.FullName is null)
return true;
Expand All @@ -172,7 +174,28 @@ 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 @@ -252,7 +275,8 @@ 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) continue;
if (loadedAcL.Acl is null || loadedAcL.Acl.IsTemplateMode || loadedAcL.Acl.IsDisposed)
continue;

try
{
Expand Down Expand Up @@ -285,5 +309,6 @@ private void OnUnload(AssemblyLoadContext alc)
CompiledAssemblyImage = null;
_dependencyResolvers.Clear();
base.Unloading -= OnUnload;
this.IsDisposed = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public sealed class RunConfig
/// <summary>
/// Compiles the mod using non-publicized assemblies.
/// </summary>
[XmlElement(ElementName = "UseNonPublicizedAssemblies")]
[XmlElement(ElementName = "UseNonPubAssemblies")]
[DefaultValue(false)]
public bool UseNonPublicizedAssemblies { get; set; }

Expand Down

0 comments on commit 7308ff9

Please sign in to comment.