summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI/Framework
diff options
context:
space:
mode:
Diffstat (limited to 'src/StardewModdingAPI/Framework')
-rw-r--r--src/StardewModdingAPI/Framework/AssemblyDefinitionResolver.cs61
-rw-r--r--src/StardewModdingAPI/Framework/AssemblyLoader.cs (renamed from src/StardewModdingAPI/Framework/AssemblyRewriting/AssemblyTypeRewriter.cs)137
-rw-r--r--src/StardewModdingAPI/Framework/AssemblyParseResult.cs31
-rw-r--r--src/StardewModdingAPI/Framework/AssemblyRewriting/CacheEntry.cs61
-rw-r--r--src/StardewModdingAPI/Framework/AssemblyRewriting/CachePaths.cs33
-rw-r--r--src/StardewModdingAPI/Framework/AssemblyRewriting/RewriteResult.cs49
-rw-r--r--src/StardewModdingAPI/Framework/ModAssemblyLoader.cs143
7 files changed, 199 insertions, 316 deletions
diff --git a/src/StardewModdingAPI/Framework/AssemblyDefinitionResolver.cs b/src/StardewModdingAPI/Framework/AssemblyDefinitionResolver.cs
new file mode 100644
index 00000000..b4e69fcd
--- /dev/null
+++ b/src/StardewModdingAPI/Framework/AssemblyDefinitionResolver.cs
@@ -0,0 +1,61 @@
+using System.Collections.Generic;
+using Mono.Cecil;
+
+namespace StardewModdingAPI.Framework
+{
+ /// <summary>A minimal assembly definition resolver which resolves references to known assemblies.</summary>
+ internal class AssemblyDefinitionResolver : DefaultAssemblyResolver
+ {
+ /*********
+ ** Properties
+ *********/
+ /// <summary>The known assemblies.</summary>
+ private readonly IDictionary<string, AssemblyDefinition> Loaded = new Dictionary<string, AssemblyDefinition>();
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Add known assemblies to the resolver.</summary>
+ /// <param name="assemblies">The known assemblies.</param>
+ public void Add(params AssemblyDefinition[] assemblies)
+ {
+ foreach (AssemblyDefinition assembly in assemblies)
+ {
+ this.Loaded[assembly.Name.Name] = assembly;
+ this.Loaded[assembly.Name.FullName] = assembly;
+ }
+ }
+
+ /// <summary>Resolve an assembly reference.</summary>
+ /// <param name="name">The assembly name.</param>
+ public override AssemblyDefinition Resolve(AssemblyNameReference name) => this.ResolveName(name.Name) ?? base.Resolve(name);
+
+ /// <summary>Resolve an assembly reference.</summary>
+ /// <param name="name">The assembly name.</param>
+ /// <param name="parameters">The assembly reader parameters.</param>
+ public override AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters parameters) => this.ResolveName(name.Name) ?? base.Resolve(name, parameters);
+
+ /// <summary>Resolve an assembly reference.</summary>
+ /// <param name="fullName">The assembly full name (including version, etc).</param>
+ public override AssemblyDefinition Resolve(string fullName) => this.ResolveName(fullName) ?? base.Resolve(fullName);
+
+ /// <summary>Resolve an assembly reference.</summary>
+ /// <param name="fullName">The assembly full name (including version, etc).</param>
+ /// <param name="parameters">The assembly reader parameters.</param>
+ public override AssemblyDefinition Resolve(string fullName, ReaderParameters parameters) => this.ResolveName(fullName) ?? base.Resolve(fullName, parameters);
+
+
+ /*********
+ ** Private methods
+ *********/
+ /// <summary>Resolve a known assembly definition based on its short or full name.</summary>
+ /// <param name="name">The assembly's short or full name.</param>
+ private AssemblyDefinition ResolveName(string name)
+ {
+ return this.Loaded.ContainsKey(name)
+ ? this.Loaded[name]
+ : null;
+ }
+ }
+}
diff --git a/src/StardewModdingAPI/Framework/AssemblyRewriting/AssemblyTypeRewriter.cs b/src/StardewModdingAPI/Framework/AssemblyLoader.cs
index 9d4d6b11..37f2764a 100644
--- a/src/StardewModdingAPI/Framework/AssemblyRewriting/AssemblyTypeRewriter.cs
+++ b/src/StardewModdingAPI/Framework/AssemblyLoader.cs
@@ -1,4 +1,6 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
+using System.IO;
using System.Linq;
using System.Reflection;
using Mono.Cecil;
@@ -6,15 +8,15 @@ using Mono.Cecil.Cil;
using Mono.Cecil.Rocks;
using StardewModdingAPI.AssemblyRewriters;
-namespace StardewModdingAPI.Framework.AssemblyRewriting
+namespace StardewModdingAPI.Framework
{
- /// <summary>Rewrites type references.</summary>
- internal class AssemblyTypeRewriter
+ /// <summary>Preprocesses and loads mod assemblies.</summary>
+ internal class AssemblyLoader
{
/*********
** Properties
*********/
- /// <summary>Metadata for mapping assemblies to the current <see cref="Platform"/>.</summary>
+ /// <summary>Metadata for mapping assemblies to the current platform.</summary>
private readonly PlatformAssemblyMap AssemblyMap;
/// <summary>A type => assembly lookup for types which should be rewritten.</summary>
@@ -28,17 +30,16 @@ namespace StardewModdingAPI.Framework.AssemblyRewriting
** Public methods
*********/
/// <summary>Construct an instance.</summary>
- /// <param name="assemblyMap">Metadata for mapping assemblies to the current <see cref="Platform"/>.</param>
+ /// <param name="targetPlatform">The current game platform.</param>
/// <param name="monitor">Encapsulates monitoring and logging.</param>
- public AssemblyTypeRewriter(PlatformAssemblyMap assemblyMap, IMonitor monitor)
+ public AssemblyLoader(Platform targetPlatform, IMonitor monitor)
{
- // save config
- this.AssemblyMap = assemblyMap;
this.Monitor = monitor;
+ this.AssemblyMap = Constants.GetAssemblyMap(targetPlatform);
- // collect type => assembly lookup
+ // generate type => assembly lookup for types which should be rewritten
this.TypeAssemblies = new Dictionary<string, Assembly>();
- foreach (Assembly assembly in assemblyMap.Targets)
+ foreach (Assembly assembly in this.AssemblyMap.Targets)
{
ModuleDefinition module = this.AssemblyMap.TargetModules[assembly];
foreach (TypeDefinition type in module.GetTypes())
@@ -52,10 +53,102 @@ namespace StardewModdingAPI.Framework.AssemblyRewriting
}
}
+ /// <summary>Preprocess and load an assembly, writing modified assemblies to the cache folder if needed.</summary>
+ /// <param name="assemblyPath">The assembly file path.</param>
+ /// <returns>Returns the rewrite metadata for the preprocessed assembly.</returns>
+ public Assembly Load(string assemblyPath)
+ {
+ // get referenced local assemblies
+ AssemblyParseResult[] assemblies;
+ {
+ AssemblyDefinitionResolver resolver = new AssemblyDefinitionResolver();
+ assemblies = this.GetReferencedLocalAssemblies(new FileInfo(assemblyPath), new HashSet<string>(), resolver).ToArray();
+ if (!assemblies.Any())
+ throw new InvalidOperationException($"Could not load '{assemblyPath}' because it doesn't exist.");
+ resolver.Add(assemblies.Select(p => p.Definition).ToArray());
+ }
+
+ // rewrite & load assemblies in leaf-to-root order
+ Assembly lastAssembly = null;
+ foreach (AssemblyParseResult assembly in assemblies)
+ {
+ this.Monitor.Log($"Loading {assembly.File.FullName}...", LogLevel.Trace);
+ this.RewriteAssembly(assembly.Definition);
+ using (MemoryStream outStream = new MemoryStream())
+ {
+ assembly.Definition.Write(outStream);
+ byte[] bytes = outStream.ToArray();
+ lastAssembly = Assembly.Load(bytes);
+ }
+ }
+
+ // last assembly loaded is the root
+ return lastAssembly;
+ }
+
+ /// <summary>Resolve an assembly by its name.</summary>
+ /// <param name="name">The assembly name.</param>
+ /// <remarks>
+ /// This implementation returns the first loaded assembly which matches the short form of
+ /// the assembly name, to resolve assembly resolution issues when rewriting
+ /// assemblies (especially with Mono). Since this is meant to be called on <see cref="AppDomain.AssemblyResolve"/>,
+ /// the implicit assumption is that loading the exact assembly failed.
+ /// </remarks>
+ public Assembly ResolveAssembly(string name)
+ {
+ string shortName = name.Split(new[] { ',' }, 2).First(); // get simple name (without version and culture)
+ return AppDomain.CurrentDomain
+ .GetAssemblies()
+ .FirstOrDefault(p => p.GetName().Name == shortName);
+ }
+
+
+ /*********
+ ** Private methods
+ *********/
+ /****
+ ** Assembly parsing
+ ****/
+ /// <summary>Get a list of referenced local assemblies starting from the mod assembly, ordered from leaf to root.</summary>
+ /// <param name="file">The assembly file to load.</param>
+ /// <param name="visitedAssemblyPaths">The assembly paths that should be skipped.</param>
+ /// <returns>Returns the rewrite metadata for the preprocessed assembly.</returns>
+ private IEnumerable<AssemblyParseResult> GetReferencedLocalAssemblies(FileInfo file, HashSet<string> visitedAssemblyPaths, IAssemblyResolver assemblyResolver)
+ {
+ // validate
+ if (file.Directory == null)
+ throw new InvalidOperationException($"Could not get directory from file path '{file.FullName}'.");
+ if (visitedAssemblyPaths.Contains(file.FullName))
+ yield break; // already visited
+ if (!file.Exists)
+ yield break; // not a local assembly
+ visitedAssemblyPaths.Add(file.FullName);
+
+ // read assembly
+ byte[] assemblyBytes = File.ReadAllBytes(file.FullName);
+ AssemblyDefinition assembly;
+ using (Stream readStream = new MemoryStream(assemblyBytes))
+ assembly = AssemblyDefinition.ReadAssembly(readStream, new ReaderParameters(ReadingMode.Deferred) { AssemblyResolver = assemblyResolver });
+
+ // yield referenced assemblies
+ foreach (AssemblyNameReference dependency in assembly.MainModule.AssemblyReferences)
+ {
+ FileInfo dependencyFile = new FileInfo(Path.Combine(file.Directory.FullName, $"{dependency.Name}.dll"));
+ foreach (AssemblyParseResult result in this.GetReferencedLocalAssemblies(dependencyFile, visitedAssemblyPaths, assemblyResolver))
+ yield return result;
+ }
+
+ // yield assembly
+ yield return new AssemblyParseResult(file, assembly);
+ }
+
+ /****
+ ** Assembly rewriting
+ ****/
/// <summary>Rewrite the types referenced by an assembly.</summary>
/// <param name="assembly">The assembly to rewrite.</param>
/// <returns>Returns whether the assembly was modified.</returns>
- public bool RewriteAssembly(AssemblyDefinition assembly)
+ private bool RewriteAssembly(AssemblyDefinition assembly)
{
ModuleDefinition module = assembly.Modules.Single(); // technically an assembly can have multiple modules, but none of the build tools (including MSBuild) support it; simplify by assuming one module
@@ -65,7 +158,6 @@ namespace StardewModdingAPI.Framework.AssemblyRewriting
{
if (this.AssemblyMap.RemoveNames.Any(name => module.AssemblyReferences[i].Name == name))
{
- this.Monitor.Log($"removing reference to {module.AssemblyReferences[i]}", LogLevel.Trace);
shouldRewrite = true;
module.AssemblyReferences.RemoveAt(i);
i--;
@@ -76,19 +168,12 @@ namespace StardewModdingAPI.Framework.AssemblyRewriting
// add target assembly references
foreach (AssemblyNameReference target in this.AssemblyMap.TargetReferences.Values)
- {
- this.Monitor.Log($" adding reference to {target}", LogLevel.Trace);
module.AssemblyReferences.Add(target);
- }
// rewrite type scopes to use target assemblies
IEnumerable<TypeReference> typeReferences = module.GetTypeReferences().OrderBy(p => p.FullName);
- string lastTypeLogged = null;
foreach (TypeReference type in typeReferences)
- {
- this.ChangeTypeScope(type, shouldLog: type.FullName != lastTypeLogged);
- lastTypeLogged = type.FullName;
- }
+ this.ChangeTypeScope(type);
// rewrite incompatible methods
IMethodRewriter[] methodRewriters = Constants.GetMethodRewriters().ToArray();
@@ -111,7 +196,6 @@ namespace StardewModdingAPI.Framework.AssemblyRewriting
if (rewriter != null)
{
MethodReference methodRef = (MethodReference)op.Operand;
- this.Monitor.Log($"rewriting method reference {methodRef.DeclaringType.FullName}.{methodRef.Name}", LogLevel.Trace);
rewriter.Rewrite(module, cil, op, methodRef, this.AssemblyMap);
}
}
@@ -121,14 +205,9 @@ namespace StardewModdingAPI.Framework.AssemblyRewriting
return true;
}
-
- /*********
- ** Private methods
- *********/
/// <summary>Get the correct reference to use for compatibility with the current platform.</summary>
/// <param name="type">The type reference to rewrite.</param>
- /// <param name="shouldLog">Whether to log a message.</param>
- private void ChangeTypeScope(TypeReference type, bool shouldLog)
+ private void ChangeTypeScope(TypeReference type)
{
// check skip conditions
if (type == null || type.FullName.StartsWith("System."))
@@ -141,8 +220,6 @@ namespace StardewModdingAPI.Framework.AssemblyRewriting
// replace scope
AssemblyNameReference assemblyRef = this.AssemblyMap.TargetReferences[assembly];
- if (shouldLog)
- this.Monitor.Log($"redirecting {type.FullName} from {type.Scope.Name} to {assemblyRef.Name}", LogLevel.Trace);
type.Scope = assemblyRef;
}
diff --git a/src/StardewModdingAPI/Framework/AssemblyParseResult.cs b/src/StardewModdingAPI/Framework/AssemblyParseResult.cs
new file mode 100644
index 00000000..bff976aa
--- /dev/null
+++ b/src/StardewModdingAPI/Framework/AssemblyParseResult.cs
@@ -0,0 +1,31 @@
+using System.IO;
+using Mono.Cecil;
+
+namespace StardewModdingAPI.Framework
+{
+ /// <summary>Metadata about a parsed assembly definition.</summary>
+ internal class AssemblyParseResult
+ {
+ /*********
+ ** Accessors
+ *********/
+ /// <summary>The original assembly file.</summary>
+ public readonly FileInfo File;
+
+ /// <summary>The assembly definition.</summary>
+ public readonly AssemblyDefinition Definition;
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="file">The original assembly file.</param>
+ /// <param name="assembly">The assembly definition.</param>
+ public AssemblyParseResult(FileInfo file, AssemblyDefinition assembly)
+ {
+ this.File = file;
+ this.Definition = assembly;
+ }
+ }
+} \ No newline at end of file
diff --git a/src/StardewModdingAPI/Framework/AssemblyRewriting/CacheEntry.cs b/src/StardewModdingAPI/Framework/AssemblyRewriting/CacheEntry.cs
deleted file mode 100644
index 4c3b86fe..00000000
--- a/src/StardewModdingAPI/Framework/AssemblyRewriting/CacheEntry.cs
+++ /dev/null
@@ -1,61 +0,0 @@
-using System.IO;
-using StardewModdingAPI.AssemblyRewriters;
-
-namespace StardewModdingAPI.Framework.AssemblyRewriting
-{
- /// <summary>Represents cached metadata for a rewritten assembly.</summary>
- internal class CacheEntry
- {
- /*********
- ** Accessors
- *********/
- /// <summary>The MD5 hash for the original assembly.</summary>
- public readonly string Hash;
-
- /// <summary>The SMAPI version used to rewrite the assembly.</summary>
- public readonly string ApiVersion;
-
- /// <summary>The target platform.</summary>
- public readonly Platform Platform;
-
- /// <summary>The <see cref="System.Environment.MachineName"/> value for the machine used to rewrite the assembly.</summary>
- public readonly string MachineName;
-
- /// <summary>Whether to use the cached assembly instead of the original assembly.</summary>
- public readonly bool UseCachedAssembly;
-
-
- /*********
- ** Public methods
- *********/
- /// <summary>Construct an instance.</summary>
- /// <param name="hash">The MD5 hash for the original assembly.</param>
- /// <param name="apiVersion">The SMAPI version used to rewrite the assembly.</param>
- /// <param name="platform">The target platform.</param>
- /// <param name="machineName">The <see cref="System.Environment.MachineName"/> value for the machine used to rewrite the assembly.</param>
- /// <param name="useCachedAssembly">Whether to use the cached assembly instead of the original assembly.</param>
- public CacheEntry(string hash, string apiVersion, Platform platform, string machineName, bool useCachedAssembly)
- {
- this.Hash = hash;
- this.ApiVersion = apiVersion;
- this.Platform = platform;
- this.MachineName = machineName;
- this.UseCachedAssembly = useCachedAssembly;
- }
-
- /// <summary>Get whether the cache entry is up-to-date for the given assembly hash.</summary>
- /// <param name="paths">The paths for the cached assembly.</param>
- /// <param name="hash">The MD5 hash of the original assembly.</param>
- /// <param name="currentVersion">The current SMAPI version.</param>
- /// <param name="platform">The target platform.</param>
- /// <param name="machineName">The <see cref="System.Environment.MachineName"/> value for the machine reading the assembly.</param>
- public bool IsUpToDate(CachePaths paths, string hash, ISemanticVersion currentVersion, Platform platform, string machineName)
- {
- return hash == this.Hash
- && this.ApiVersion == currentVersion.ToString()
- && this.Platform == platform
- && this.MachineName == machineName
- && (!this.UseCachedAssembly || File.Exists(paths.Assembly));
- }
- }
-} \ No newline at end of file
diff --git a/src/StardewModdingAPI/Framework/AssemblyRewriting/CachePaths.cs b/src/StardewModdingAPI/Framework/AssemblyRewriting/CachePaths.cs
deleted file mode 100644
index 18861873..00000000
--- a/src/StardewModdingAPI/Framework/AssemblyRewriting/CachePaths.cs
+++ /dev/null
@@ -1,33 +0,0 @@
-namespace StardewModdingAPI.Framework.AssemblyRewriting
-{
- /// <summary>Contains the paths for an assembly's cached data.</summary>
- internal struct CachePaths
- {
- /*********
- ** Accessors
- *********/
- /// <summary>The directory path which contains the assembly.</summary>
- public string Directory { get; }
-
- /// <summary>The file path of the assembly file.</summary>
- public string Assembly { get; }
-
- /// <summary>The file path containing the assembly metadata.</summary>
- public string Metadata { get; }
-
-
- /*********
- ** Public methods
- *********/
- /// <summary>Construct an instance.</summary>
- /// <param name="directory">The directory path which contains the assembly.</param>
- /// <param name="assembly">The file path of the assembly file.</param>
- /// <param name="metadata">The file path containing the assembly metadata.</param>
- public CachePaths(string directory, string assembly, string metadata)
- {
- this.Directory = directory;
- this.Assembly = assembly;
- this.Metadata = metadata;
- }
- }
-} \ No newline at end of file
diff --git a/src/StardewModdingAPI/Framework/AssemblyRewriting/RewriteResult.cs b/src/StardewModdingAPI/Framework/AssemblyRewriting/RewriteResult.cs
deleted file mode 100644
index 8f34bb20..00000000
--- a/src/StardewModdingAPI/Framework/AssemblyRewriting/RewriteResult.cs
+++ /dev/null
@@ -1,49 +0,0 @@
-namespace StardewModdingAPI.Framework.AssemblyRewriting
-{
- /// <summary>Metadata about a preprocessed assembly.</summary>
- internal class RewriteResult
- {
- /*********
- ** Accessors
- *********/
- /// <summary>The original assembly path.</summary>
- public readonly string OriginalAssemblyPath;
-
- /// <summary>The cache paths.</summary>
- public readonly CachePaths CachePaths;
-
- /// <summary>The rewritten assembly bytes.</summary>
- public readonly byte[] AssemblyBytes;
-
- /// <summary>The MD5 hash for the original assembly.</summary>
- public readonly string Hash;
-
- /// <summary>Whether to use the cached assembly instead of the original assembly.</summary>
- public readonly bool UseCachedAssembly;
-
- /// <summary>Whether this data is newer than the cache.</summary>
- public readonly bool IsNewerThanCache;
-
-
-
- /*********
- ** Public methods
- *********/
- /// <summary>Construct an instance.</summary>
- /// <param name="originalAssemblyPath"></param>
- /// <param name="cachePaths">The cache paths.</param>
- /// <param name="assemblyBytes">The rewritten assembly bytes.</param>
- /// <param name="hash">The MD5 hash for the original assembly.</param>
- /// <param name="useCachedAssembly">Whether to use the cached assembly instead of the original assembly.</param>
- /// <param name="isNewerThanCache">Whether this data is newer than the cache.</param>
- public RewriteResult(string originalAssemblyPath, CachePaths cachePaths, byte[] assemblyBytes, string hash, bool useCachedAssembly, bool isNewerThanCache)
- {
- this.OriginalAssemblyPath = originalAssemblyPath;
- this.CachePaths = cachePaths;
- this.Hash = hash;
- this.AssemblyBytes = assemblyBytes;
- this.UseCachedAssembly = useCachedAssembly;
- this.IsNewerThanCache = isNewerThanCache;
- }
- }
-}
diff --git a/src/StardewModdingAPI/Framework/ModAssemblyLoader.cs b/src/StardewModdingAPI/Framework/ModAssemblyLoader.cs
deleted file mode 100644
index e4760398..00000000
--- a/src/StardewModdingAPI/Framework/ModAssemblyLoader.cs
+++ /dev/null
@@ -1,143 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.IO;
-using System.Linq;
-using System.Reflection;
-using System.Security.Cryptography;
-using Mono.Cecil;
-using Newtonsoft.Json;
-using StardewModdingAPI.AssemblyRewriters;
-using StardewModdingAPI.Framework.AssemblyRewriting;
-
-namespace StardewModdingAPI.Framework
-{
- /// <summary>Preprocesses and loads mod assemblies.</summary>
- internal class ModAssemblyLoader
- {
- /*********
- ** Properties
- *********/
- /// <summary>The name of the directory containing a mod's cached data.</summary>
- private readonly string CacheDirName;
-
- /// <summary>Metadata for mapping assemblies to the current <see cref="Platform"/>.</summary>
- private readonly PlatformAssemblyMap AssemblyMap;
-
- /// <summary>Rewrites assembly types to match the current platform.</summary>
- private readonly AssemblyTypeRewriter AssemblyTypeRewriter;
-
- /// <summary>Encapsulates monitoring and logging.</summary>
- private readonly IMonitor Monitor;
-
- /// <summary>The current game platform.</summary>
- private readonly Platform TargetPlatform;
-
- /*********
- ** Public methods
- *********/
- /// <summary>Construct an instance.</summary>
- /// <param name="cacheDirName">The name of the directory containing a mod's cached data.</param>
- /// <param name="targetPlatform">The current game platform.</param>
- /// <param name="monitor">Encapsulates monitoring and logging.</param>
- public ModAssemblyLoader(string cacheDirName, Platform targetPlatform, IMonitor monitor)
- {
- this.CacheDirName = cacheDirName;
- this.TargetPlatform = targetPlatform;
- this.Monitor = monitor;
- this.AssemblyMap = Constants.GetAssemblyMap(targetPlatform);
- this.AssemblyTypeRewriter = new AssemblyTypeRewriter(this.AssemblyMap, monitor);
- }
-
- /// <summary>Preprocess an assembly unless the cache is up to date.</summary>
- /// <param name="assemblyPath">The assembly file path.</param>
- /// <returns>Returns the rewrite metadata for the preprocessed assembly.</returns>
- public RewriteResult ProcessAssemblyUnlessCached(string assemblyPath)
- {
- // read assembly data
- byte[] assemblyBytes = File.ReadAllBytes(assemblyPath);
- string hash = string.Join("", MD5.Create().ComputeHash(assemblyBytes).Select(p => p.ToString("X2")));
-
- // get cached result if current
- CachePaths cachePaths = this.GetCachePaths(assemblyPath);
- {
- CacheEntry cacheEntry = File.Exists(cachePaths.Metadata) ? JsonConvert.DeserializeObject<CacheEntry>(File.ReadAllText(cachePaths.Metadata)) : null;
- if (cacheEntry != null && cacheEntry.IsUpToDate(cachePaths, hash, Constants.ApiVersion, this.TargetPlatform, Environment.MachineName))
- return new RewriteResult(assemblyPath, cachePaths, assemblyBytes, cacheEntry.Hash, cacheEntry.UseCachedAssembly, isNewerThanCache: false); // no rewrite needed
- }
- this.Monitor.Log($"Preprocessing {Path.GetFileName(assemblyPath)} for compatibility...", LogLevel.Trace);
-
- // rewrite assembly
- AssemblyDefinition assembly;
- using (Stream readStream = new MemoryStream(assemblyBytes))
- assembly = AssemblyDefinition.ReadAssembly(readStream);
- bool modified = this.AssemblyTypeRewriter.RewriteAssembly(assembly);
- using (MemoryStream outStream = new MemoryStream())
- {
- assembly.Write(outStream);
- byte[] outBytes = outStream.ToArray();
- return new RewriteResult(assemblyPath, cachePaths, outBytes, hash, useCachedAssembly: modified, isNewerThanCache: true);
- }
- }
-
- /// <summary>Write rewritten assembly metadata to the cache for a mod.</summary>
- /// <param name="results">The rewrite results.</param>
- /// <param name="forceCacheAssemblies">Whether to write all assemblies to the cache, even if they weren't modified.</param>
- /// <exception cref="InvalidOperationException">There are no results to write, or the results are not all for the same directory.</exception>
- public void WriteCache(IEnumerable<RewriteResult> results, bool forceCacheAssemblies)
- {
- results = results.ToArray();
-
- // get cache directory
- if (!results.Any())
- throw new InvalidOperationException("There are no assemblies to cache.");
- if (results.Select(p => p.CachePaths.Directory).Distinct().Count() > 1)
- throw new InvalidOperationException("The assemblies can't be cached together because they have different source directories.");
- string cacheDir = results.Select(p => p.CachePaths.Directory).First();
-
- // reset cache
- if (Directory.Exists(cacheDir))
- Directory.Delete(cacheDir, recursive: true);
- Directory.CreateDirectory(cacheDir);
-
- // cache all results
- foreach (RewriteResult result in results)
- {
- CacheEntry cacheEntry = new CacheEntry(result.Hash, Constants.ApiVersion.ToString(), this.TargetPlatform, Environment.MachineName, forceCacheAssemblies || result.UseCachedAssembly);
- File.WriteAllText(result.CachePaths.Metadata, JsonConvert.SerializeObject(cacheEntry));
- if (forceCacheAssemblies || result.UseCachedAssembly)
- File.WriteAllBytes(result.CachePaths.Assembly, result.AssemblyBytes);
- }
- }
-
- /// <summary>Resolve an assembly from its name.</summary>
- /// <param name="name">The assembly name.</param>
- /// <remarks>
- /// This implementation returns the first loaded assembly which matches the short form of
- /// the assembly name, to resolve assembly resolution issues when rewriting
- /// assemblies (especially with Mono). Since this is meant to be called on <see cref="AppDomain.AssemblyResolve"/>,
- /// the implicit assumption is that loading the exact assembly failed.
- /// </remarks>
- public Assembly ResolveAssembly(string name)
- {
- string shortName = name.Split(new[] { ',' }, 2).First(); // get simple name (without version and culture)
- return AppDomain.CurrentDomain
- .GetAssemblies()
- .FirstOrDefault(p => p.GetName().Name == shortName);
- }
-
-
- /*********
- ** Private methods
- *********/
- /// <summary>Get the cache details for an assembly.</summary>
- /// <param name="assemblyPath">The assembly file path.</param>
- private CachePaths GetCachePaths(string assemblyPath)
- {
- string fileName = Path.GetFileName(assemblyPath);
- string dirPath = Path.Combine(Path.GetDirectoryName(assemblyPath), this.CacheDirName);
- string cacheAssemblyPath = Path.Combine(dirPath, fileName);
- string metadataPath = Path.Combine(dirPath, $"{fileName}.json");
- return new CachePaths(dirPath, cacheAssemblyPath, metadataPath);
- }
- }
-}