Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ObjectCreator assembly resolving on Windows. #13190

Merged
merged 2 commits into from
Apr 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions OpenRA.Game/ModData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ public void Dispose()
MapCache.Dispose();
if (VoxelLoader != null)
VoxelLoader.Dispose();

if (ObjectCreator != null)
ObjectCreator.Dispose();
}
}

Expand Down
30 changes: 21 additions & 9 deletions OpenRA.Game/ObjectCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

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
Expand All @@ -29,13 +29,6 @@ public class ObjectCreator
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);
Expand Down Expand Up @@ -76,7 +69,6 @@ public ObjectCreator(Manifest manifest, FileSystem.FileSystem modFiles)

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)
Expand All @@ -85,6 +77,9 @@ Assembly ResolveAssembly(object sender, ResolveEventArgs e)
if (a.FullName == e.Name)
return a;

if (assemblies == null)
return null;

return assemblies.Select(a => a.First).FirstOrDefault(a => a.FullName == e.Name);
}

Expand Down Expand Up @@ -159,6 +154,23 @@ public IEnumerable<Type> GetTypes()
.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 { }
}
Expand Down