diff options
author | Chase Warrington <spacechase0.and.cat@gmail.com> | 2021-08-29 15:48:28 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2021-09-01 19:35:18 -0400 |
commit | ec5fbb06113b29342e6d4b213144f4dc3e358b03 (patch) | |
tree | 2a0d1677b4b94380a67a194e3e2041fd892b80c9 /src/SMAPI/Framework | |
parent | 9316fe303827cb31f38f4a5bb068f8fa0190ac5e (diff) | |
download | SMAPI-ec5fbb06113b29342e6d4b213144f4dc3e358b03.tar.gz SMAPI-ec5fbb06113b29342e6d4b213144f4dc3e358b03.tar.bz2 SMAPI-ec5fbb06113b29342e6d4b213144f4dc3e358b03.zip |
Rewrite 32-bit assemblies for 64-bit
Diffstat (limited to 'src/SMAPI/Framework')
5 files changed, 64 insertions, 1 deletions
diff --git a/src/SMAPI/Framework/ModLoading/AssemblyLoader.cs b/src/SMAPI/Framework/ModLoading/AssemblyLoader.cs index 3e35c9dd..ae42909c 100644 --- a/src/SMAPI/Framework/ModLoading/AssemblyLoader.cs +++ b/src/SMAPI/Framework/ModLoading/AssemblyLoader.cs @@ -336,6 +336,13 @@ namespace StardewModdingAPI.Framework.ModLoading IInstructionHandler[] handlers = new InstructionMetadata().GetHandlers(this.ParanoidMode, platformChanged, this.RewriteMods).ToArray(); RecursiveRewriter rewriter = new RecursiveRewriter( module: module, + rewriteModule: (module) => + { + bool rewritten = false; + foreach (IInstructionHandler handler in handlers) + rewritten |= handler.Handle(module); + return rewritten; + }, rewriteType: (type, replaceWith) => { bool rewritten = false; diff --git a/src/SMAPI/Framework/ModLoading/Framework/BaseInstructionHandler.cs b/src/SMAPI/Framework/ModLoading/Framework/BaseInstructionHandler.cs index 624113b3..d5d1b38e 100644 --- a/src/SMAPI/Framework/ModLoading/Framework/BaseInstructionHandler.cs +++ b/src/SMAPI/Framework/ModLoading/Framework/BaseInstructionHandler.cs @@ -25,6 +25,12 @@ namespace StardewModdingAPI.Framework.ModLoading.Framework ** Public methods *********/ /// <inheritdoc /> + public virtual bool Handle(ModuleDefinition module) + { + return false; + } + + /// <inheritdoc /> public virtual bool Handle(ModuleDefinition module, TypeReference type, Action<TypeReference> replaceWith) { return false; diff --git a/src/SMAPI/Framework/ModLoading/Framework/RecursiveRewriter.cs b/src/SMAPI/Framework/ModLoading/Framework/RecursiveRewriter.cs index 10f68f0d..52f4003c 100644 --- a/src/SMAPI/Framework/ModLoading/Framework/RecursiveRewriter.cs +++ b/src/SMAPI/Framework/ModLoading/Framework/RecursiveRewriter.cs @@ -13,6 +13,11 @@ namespace StardewModdingAPI.Framework.ModLoading.Framework /********* ** Delegates *********/ + /// <summary>Rewrite a module definition in the assembly code.</summary> + /// <param name="module">The current module definition.</param> + /// <returns>Returns whether the module was changed.</returns> + public delegate bool RewriteModuleDelegate(ModuleDefinition module); + /// <summary>Rewrite a type reference in the assembly code.</summary> /// <param name="type">The current type reference.</param> /// <param name="replaceWith">Replaces the type reference with the given type.</param> @@ -33,6 +38,9 @@ namespace StardewModdingAPI.Framework.ModLoading.Framework public ModuleDefinition Module { get; } /// <summary>Handle or rewrite a type reference if needed.</summary> + public RewriteModuleDelegate RewriteModuleImpl { get; } + + /// <summary>Handle or rewrite a type reference if needed.</summary> public RewriteTypeDelegate RewriteTypeImpl { get; } /// <summary>Handle or rewrite a CIL instruction if needed.</summary> @@ -46,9 +54,10 @@ namespace StardewModdingAPI.Framework.ModLoading.Framework /// <param name="module">The module to rewrite.</param> /// <param name="rewriteType">Handle or rewrite a type reference if needed.</param> /// <param name="rewriteInstruction">Handle or rewrite a CIL instruction if needed.</param> - public RecursiveRewriter(ModuleDefinition module, RewriteTypeDelegate rewriteType, RewriteInstructionDelegate rewriteInstruction) + public RecursiveRewriter(ModuleDefinition module, RewriteModuleDelegate rewriteModule, RewriteTypeDelegate rewriteType, RewriteInstructionDelegate rewriteInstruction) { this.Module = module; + this.RewriteModuleImpl = rewriteModule; this.RewriteTypeImpl = rewriteType; this.RewriteInstructionImpl = rewriteInstruction; } @@ -63,6 +72,8 @@ namespace StardewModdingAPI.Framework.ModLoading.Framework try { + changed |= this.RewriteModuleImpl( this.Module ); + foreach (var type in types) changed |= this.RewriteTypeDefinition(type); } diff --git a/src/SMAPI/Framework/ModLoading/IInstructionHandler.cs b/src/SMAPI/Framework/ModLoading/IInstructionHandler.cs index 17c9ba68..d41732f8 100644 --- a/src/SMAPI/Framework/ModLoading/IInstructionHandler.cs +++ b/src/SMAPI/Framework/ModLoading/IInstructionHandler.cs @@ -24,6 +24,11 @@ namespace StardewModdingAPI.Framework.ModLoading /********* ** Methods *********/ + /// <summary>Rewrite a module definition if needed.</summary> + /// <param name="module">The assembly module.</param> + /// <returns>Returns whether the module was changed.</returns> + bool Handle(ModuleDefinition module); + /// <summary>Rewrite a type reference if needed.</summary> /// <param name="module">The assembly module containing the instruction.</param> /// <param name="type">The type definition to handle.</param> diff --git a/src/SMAPI/Framework/ModLoading/Rewriters/ArchitectureAssemblyRewriter.cs b/src/SMAPI/Framework/ModLoading/Rewriters/ArchitectureAssemblyRewriter.cs new file mode 100644 index 00000000..216c042a --- /dev/null +++ b/src/SMAPI/Framework/ModLoading/Rewriters/ArchitectureAssemblyRewriter.cs @@ -0,0 +1,34 @@ +using System; +using HarmonyLib; +using Mono.Cecil; +using Mono.Cecil.Cil; +using StardewModdingAPI.Framework.ModLoading.Framework; +using StardewModdingAPI.Framework.ModLoading.RewriteFacades; + +namespace StardewModdingAPI.Framework.ModLoading.Rewriters +{ + /// <summary>Rewrites Harmony 1.x assembly references to work with Harmony 2.x.</summary> + internal class ArchitectureAssemblyRewriter : BaseInstructionHandler + { + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + public ArchitectureAssemblyRewriter() + : base(defaultPhrase: "32-bit architecture") { } + + + /// <inheritdoc /> + public override bool Handle( ModuleDefinition module ) + { + if ( module.Attributes.HasFlag( ModuleAttributes.Required32Bit ) ) + { + module.Attributes = module.Attributes & ~ModuleAttributes.Required32Bit; + this.MarkRewritten(); + return true; + } + return false; + } + + } +} |