diff options
Diffstat (limited to 'src/StardewModdingAPI/Framework/AssemblyRewriting')
4 files changed, 104 insertions, 7 deletions
diff --git a/src/StardewModdingAPI/Framework/AssemblyRewriting/AssemblyTypeRewriter.cs b/src/StardewModdingAPI/Framework/AssemblyRewriting/AssemblyTypeRewriter.cs index 3459488e..9d4d6b11 100644 --- a/src/StardewModdingAPI/Framework/AssemblyRewriting/AssemblyTypeRewriter.cs +++ b/src/StardewModdingAPI/Framework/AssemblyRewriting/AssemblyTypeRewriter.cs @@ -54,7 +54,8 @@ namespace StardewModdingAPI.Framework.AssemblyRewriting /// <summary>Rewrite the types referenced by an assembly.</summary> /// <param name="assembly">The assembly to rewrite.</param> - public void RewriteAssembly(AssemblyDefinition assembly) + /// <returns>Returns whether the assembly was modified.</returns> + public 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 @@ -71,7 +72,7 @@ namespace StardewModdingAPI.Framework.AssemblyRewriting } } if (!shouldRewrite) - return; + return false; // add target assembly references foreach (AssemblyNameReference target in this.AssemblyMap.TargetReferences.Values) @@ -117,6 +118,7 @@ namespace StardewModdingAPI.Framework.AssemblyRewriting } method.Body.OptimizeMacros(); } + return true; } diff --git a/src/StardewModdingAPI/Framework/AssemblyRewriting/CacheEntry.cs b/src/StardewModdingAPI/Framework/AssemblyRewriting/CacheEntry.cs new file mode 100644 index 00000000..3dfbc78c --- /dev/null +++ b/src/StardewModdingAPI/Framework/AssemblyRewriting/CacheEntry.cs @@ -0,0 +1,46 @@ +using System.IO; + +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>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="useCachedAssembly">Whether to use the cached assembly instead of the original assembly.</param> + public CacheEntry(string hash, string apiVersion, bool useCachedAssembly) + { + this.Hash = hash; + this.ApiVersion = apiVersion; + 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> + public bool IsUpToDate(CachePaths paths, string hash, Version currentVersion) + { + return hash == this.Hash + && this.ApiVersion == currentVersion.ToString() + && (!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 index 17c4d188..18861873 100644 --- a/src/StardewModdingAPI/Framework/AssemblyRewriting/CachePaths.cs +++ b/src/StardewModdingAPI/Framework/AssemblyRewriting/CachePaths.cs @@ -12,8 +12,8 @@ namespace StardewModdingAPI.Framework.AssemblyRewriting /// <summary>The file path of the assembly file.</summary> public string Assembly { get; } - /// <summary>The file path containing the MD5 hash for the assembly.</summary> - public string Hash { get; } + /// <summary>The file path containing the assembly metadata.</summary> + public string Metadata { get; } /********* @@ -22,12 +22,12 @@ namespace StardewModdingAPI.Framework.AssemblyRewriting /// <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="hash">The file path containing the MD5 hash for the assembly.</param> - public CachePaths(string directory, string assembly, string hash) + /// <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.Hash = hash; + 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 new file mode 100644 index 00000000..8f34bb20 --- /dev/null +++ b/src/StardewModdingAPI/Framework/AssemblyRewriting/RewriteResult.cs @@ -0,0 +1,49 @@ +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; + } + } +} |