Skip to content

Commit

Permalink
- Fixed ACL name not being shown in warnings.
Browse files Browse the repository at this point in the history
- Fixed thread lock recursion causing fileloadexceptions.
  • Loading branch information
TBN-MapleWheels committed Oct 26, 2023
1 parent 2a93114 commit e2695db
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -352,13 +352,25 @@ public IEnumerable<LoadedACL> GetAllLoadedACLs()
{
OpsLockLoaded.ExitReadLock();
}

}

#endregion

#region InternalAPI

/// <summary>
/// [Unsafe] Warning: only for use in nested threading functions. Requires care to manage access.
/// Does not make any guarantees about the state of the ACL after the list has been returned.
/// </summary>
/// <returns></returns>
[MethodImpl(MethodImplOptions.Synchronized | MethodImplOptions.NoInlining)]
internal ImmutableList<LoadedACL> UnsafeGetAllLoadedACLs()
{
if (LoadedACLs.IsEmpty)
return ImmutableList<LoadedACL>.Empty;
return LoadedACLs.Select(kvp => kvp.Value).ToImmutableList();
}

/// <summary>
/// Used by content package and plugin management to stop unloading of a given ACL until all plugins have gracefully closed.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,15 +295,16 @@ public AssemblyLoadingSuccessState LoadAssemblyPackages()
_assemblyManager.OnAssemblyUnloading += AssemblyManagerOnAssemblyUnloading;

// log error if some ACLs are still unloading (some assembly is still in use)
_assemblyManager.FinalizeDispose(); //Update lists
if (_assemblyManager.IsCurrentlyUnloading)
{
ModUtils.Logging.PrintWarning($"WARNING: Some mods from a previous session (lobby) are still loaded! This may result in undefined behaviour!\nIf you notice any odd behaviour that only occurs after multiple lobbies, please restart your game.");
ModUtils.Logging.PrintWarning($"The below ACLs are still unloading:");
foreach (var wkref in _assemblyManager.StillUnloadingACLs)
{
ModUtils.Logging.PrintWarning($"The below ACL is still unloading:");
if (wkref.TryGetTarget(out var tgt))
{
ModUtils.Logging.PrintWarning($"ACL Name: {tgt.Name}");
ModUtils.Logging.PrintWarning($"ACL Name: {tgt.FriendlyName}");
foreach (Assembly assembly in tgt.Assemblies)
{
ModUtils.Logging.PrintWarning($"-- Assembly: {assembly.GetName()}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,20 +280,33 @@ protected override Assembly Load(AssemblyName assemblyName)
}

//try resolve against other loaded alcs
foreach (var loadedAcL in _assemblyManager.GetAllLoadedACLs())
ImmutableList<AssemblyManager.LoadedACL> list;
try
{
if (loadedAcL.Acl is null || loadedAcL.Acl.IsTemplateMode || loadedAcL.Acl.IsDisposed)
continue;
try
{
ass = loadedAcL.Acl.LoadFromAssemblyName(assemblyName);
if (ass is not null)
return ass;
}
catch
list = _assemblyManager.UnsafeGetAllLoadedACLs();
}
catch
{
list = ImmutableList<AssemblyManager.LoadedACL>.Empty;
}

if (!list.IsEmpty)
{
foreach (var loadedAcL in list)
{
// LoadFromAssemblyName throws, no need to propagate
if (loadedAcL.Acl is null || loadedAcL.Acl.IsTemplateMode || loadedAcL.Acl.IsDisposed)
continue;

try
{
ass = loadedAcL.Acl.LoadFromAssemblyName(assemblyName);
if (ass is not null)
return ass;
}
catch
{
// LoadFromAssemblyName throws, no need to propagate
}
}
}

Expand All @@ -315,6 +328,7 @@ private void OnUnload(AssemblyLoadContext alc)
CompiledAssembly = null;
CompiledAssemblyImage = null;
_dependencyResolvers.Clear();
_assemblyManager = null;
base.Unloading -= OnUnload;
this.IsDisposed = true;
}
Expand Down

0 comments on commit e2695db

Please sign in to comment.