only rewrite assemblies if needed (#166)

This commit is contained in:
Jesse Plamondon-Willard 2016-11-27 12:08:00 -05:00
parent 2e40ad7ad3
commit 8917fb6697
2 changed files with 14 additions and 9 deletions

View File

@ -65,24 +65,29 @@ namespace StardewModdingAPI.Framework.AssemblyRewriting
foreach (ModuleDefinition module in assembly.Modules)
{
// remove old assembly references
bool shouldRewrite = false;
for (int i = 0; i < module.AssemblyReferences.Count; i++)
{
bool shouldRemove = this.RemoveAssemblyNames.Any(name => module.AssemblyReferences[i].Name == name) || this.TargetAssemblies.Any(a => module.AssemblyReferences[i].Name == a.GetName().Name);
bool shouldRemove = this.RemoveAssemblyNames.Any(name => module.AssemblyReferences[i].Name == name);
if (shouldRemove)
{
shouldRewrite = true;
module.AssemblyReferences.RemoveAt(i);
i--;
}
}
// add target assembly references
foreach (AssemblyNameReference target in this.AssemblyNameReferences.Values)
module.AssemblyReferences.Add(target);
// replace references
if (shouldRewrite)
{
// add target assembly references
foreach (AssemblyNameReference target in this.AssemblyNameReferences.Values)
module.AssemblyReferences.Add(target);
// rewrite type scopes to use target assemblies
TypeReference[] refs = (TypeReference[])module.GetTypeReferences();
foreach (TypeReference type in refs)
this.ChangeTypeScope(type);
// rewrite type scopes to use target assemblies
foreach (TypeReference type in module.GetTypeReferences())
this.ChangeTypeScope(type);
}
}
}

View File

@ -94,7 +94,7 @@ namespace StardewModdingAPI.Framework
CachePaths cachePaths = this.GetCacheInfo(assemblyPath);
if (!File.Exists(cachePaths.Assembly))
throw new InvalidOperationException($"The assembly {assemblyPath} doesn't exist in the preprocessed cache.");
return Assembly.UnsafeLoadFrom(cachePaths.Assembly);
return Assembly.UnsafeLoadFrom(cachePaths.Assembly); // unsafe load allows DLLs downloaded from the Internet without the user needing to 'unblock' them
}