Merge pull request #13190 from pchote/object-weirdness

Fix ObjectCreator assembly resolving on Windows.
This commit is contained in:
reaperrr
2017-04-26 21:29:33 +02:00
committed by GitHub
2 changed files with 24 additions and 9 deletions

View File

@@ -217,6 +217,9 @@ namespace OpenRA
MapCache.Dispose();
if (VoxelLoader != null)
VoxelLoader.Dispose();
if (ObjectCreator != null)
ObjectCreator.Dispose();
}
}

View File

@@ -18,7 +18,7 @@ using OpenRA.Primitives;
namespace OpenRA
{
public class ObjectCreator
public sealed class ObjectCreator : IDisposable
{
// .NET does not support unloading assemblies, so mod libraries will leak across mod changes.
// This tracks the assemblies that have been loaded since game start so that we don't load multiple copies
@@ -29,13 +29,6 @@ namespace OpenRA
readonly Pair<Assembly, string>[] assemblies;
readonly bool isMonoRuntime = Type.GetType("Mono.Runtime") != null;
public ObjectCreator(Assembly a)
{
typeCache = new Cache<string, Type>(FindType);
ctorCache = new Cache<Type, ConstructorInfo>(GetCtor);
assemblies = a.GetNamespaces().Select(ns => Pair.New(a, ns)).ToArray();
}
public ObjectCreator(Manifest manifest, FileSystem.FileSystem modFiles)
{
typeCache = new Cache<string, Type>(FindType);
@@ -76,7 +69,6 @@ namespace OpenRA
AppDomain.CurrentDomain.AssemblyResolve += ResolveAssembly;
assemblies = assemblyList.SelectMany(asm => asm.GetNamespaces().Select(ns => Pair.New(asm, ns))).ToArray();
AppDomain.CurrentDomain.AssemblyResolve -= ResolveAssembly;
}
Assembly ResolveAssembly(object sender, ResolveEventArgs e)
@@ -85,6 +77,9 @@ namespace OpenRA
if (a.FullName == e.Name)
return a;
if (assemblies == null)
return null;
return assemblies.Select(a => a.First).FirstOrDefault(a => a.FullName == e.Name);
}
@@ -159,6 +154,23 @@ namespace OpenRA
.SelectMany(ma => ma.GetTypes());
}
~ObjectCreator()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
void Dispose(bool disposing)
{
if (disposing)
AppDomain.CurrentDomain.AssemblyResolve -= ResolveAssembly;
}
[AttributeUsage(AttributeTargets.Constructor)]
public sealed class UseCtorAttribute : Attribute { }
}