diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2021-09-03 18:36:39 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2021-09-03 18:36:39 -0400 |
commit | c5b8cd626489dad6210fe629658314dfc85f4d08 (patch) | |
tree | 3e30c3172e6c0bb3e422036581684593156fad22 /src/SMAPI/Framework/ModLoading/Rewriters | |
parent | 4ee96a20bb6c74bc7ff6176a03e7f15d47cddfa8 (diff) | |
parent | 6d4ea7f0bd584602632e6e308d52bb369b30006f (diff) | |
download | SMAPI-c5b8cd626489dad6210fe629658314dfc85f4d08.tar.gz SMAPI-c5b8cd626489dad6210fe629658314dfc85f4d08.tar.bz2 SMAPI-c5b8cd626489dad6210fe629658314dfc85f4d08.zip |
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI/Framework/ModLoading/Rewriters')
-rw-r--r-- | src/SMAPI/Framework/ModLoading/Rewriters/ArchitectureAssemblyRewriter.cs | 31 | ||||
-rw-r--r-- | src/SMAPI/Framework/ModLoading/Rewriters/HarmonyRewriter.cs (renamed from src/SMAPI/Framework/ModLoading/Rewriters/Harmony1AssemblyRewriter.cs) | 50 | ||||
-rw-r--r-- | src/SMAPI/Framework/ModLoading/Rewriters/HeuristicFieldRewriter.cs | 6 | ||||
-rw-r--r-- | src/SMAPI/Framework/ModLoading/Rewriters/HeuristicMethodRewriter.cs | 6 |
4 files changed, 69 insertions, 24 deletions
diff --git a/src/SMAPI/Framework/ModLoading/Rewriters/ArchitectureAssemblyRewriter.cs b/src/SMAPI/Framework/ModLoading/Rewriters/ArchitectureAssemblyRewriter.cs new file mode 100644 index 00000000..cc830216 --- /dev/null +++ b/src/SMAPI/Framework/ModLoading/Rewriters/ArchitectureAssemblyRewriter.cs @@ -0,0 +1,31 @@ +using Mono.Cecil; +using StardewModdingAPI.Framework.ModLoading.Framework; + +namespace StardewModdingAPI.Framework.ModLoading.Rewriters +{ + /// <summary>Removes the 32-bit-only from loaded assemblies.</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 &= ~ModuleAttributes.Required32Bit; + this.MarkRewritten(); + return true; + } + + return false; + } + + } +} diff --git a/src/SMAPI/Framework/ModLoading/Rewriters/Harmony1AssemblyRewriter.cs b/src/SMAPI/Framework/ModLoading/Rewriters/HarmonyRewriter.cs index 7a3b428d..922d4bc4 100644 --- a/src/SMAPI/Framework/ModLoading/Rewriters/Harmony1AssemblyRewriter.cs +++ b/src/SMAPI/Framework/ModLoading/Rewriters/HarmonyRewriter.cs @@ -7,8 +7,8 @@ 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 Harmony1AssemblyRewriter : BaseInstructionHandler + /// <summary>Detects Harmony references, and rewrites Harmony 1.x assembly references to work with Harmony 2.x.</summary> + internal class HarmonyRewriter : BaseInstructionHandler { /********* ** Fields @@ -16,19 +16,29 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters /// <summary>Whether any Harmony 1.x types were replaced.</summary> private bool ReplacedTypes; + /// <summary>Whether to rewrite Harmony 1.x code.</summary> + private readonly bool ShouldRewrite; + /********* ** Public methods *********/ /// <summary>Construct an instance.</summary> - public Harmony1AssemblyRewriter() - : base(defaultPhrase: "Harmony 1.x") { } + public HarmonyRewriter(bool shouldRewrite = true) + : base(defaultPhrase: "Harmony 1.x") + { + this.ShouldRewrite = shouldRewrite; + } /// <inheritdoc /> public override bool Handle(ModuleDefinition module, TypeReference type, Action<TypeReference> replaceWith) { + // detect Harmony + if (!(type.Scope is AssemblyNameReference scope) || scope.Name != "0Harmony") + return false; + // rewrite Harmony 1.x type to Harmony 2.0 type - if (type.Scope is AssemblyNameReference { Name: "0Harmony" } scope && scope.Version.Major == 1) + if (this.ShouldRewrite && scope.Version.Major == 1) { Type targetType = this.GetMappedType(type); replaceWith(module.ImportReference(targetType)); @@ -37,28 +47,32 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters return true; } + this.MarkFlag(InstructionHandleResult.DetectedGamePatch); return false; } /// <inheritdoc /> public override bool Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction) { - // rewrite Harmony 1.x methods to Harmony 2.0 - MethodReference methodRef = RewriteHelper.AsMethodReference(instruction); - if (this.TryRewriteMethodsToFacade(module, methodRef)) - { - this.OnChanged(); - return true; - } - - // rewrite renamed fields - FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction); - if (fieldRef != null) + if (this.ShouldRewrite) { - if (fieldRef.DeclaringType.FullName == "HarmonyLib.HarmonyMethod" && fieldRef.Name == "prioritiy") + // rewrite Harmony 1.x methods to Harmony 2.0 + MethodReference methodRef = RewriteHelper.AsMethodReference(instruction); + if (this.TryRewriteMethodsToFacade(module, methodRef)) { - fieldRef.Name = nameof(HarmonyMethod.priority); this.OnChanged(); + return true; + } + + // rewrite renamed fields + FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction); + if (fieldRef != null) + { + if (fieldRef.DeclaringType.FullName == "HarmonyLib.HarmonyMethod" && fieldRef.Name == "prioritiy") + { + fieldRef.Name = nameof(HarmonyMethod.priority); + this.OnChanged(); + } } } diff --git a/src/SMAPI/Framework/ModLoading/Rewriters/HeuristicFieldRewriter.cs b/src/SMAPI/Framework/ModLoading/Rewriters/HeuristicFieldRewriter.cs index f59a6ab1..57f1dd17 100644 --- a/src/SMAPI/Framework/ModLoading/Rewriters/HeuristicFieldRewriter.cs +++ b/src/SMAPI/Framework/ModLoading/Rewriters/HeuristicFieldRewriter.cs @@ -13,7 +13,7 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters ** Fields *********/ /// <summary>The assembly names to which to rewrite broken references.</summary> - private readonly HashSet<string> RewriteReferencesToAssemblies; + private readonly ISet<string> RewriteReferencesToAssemblies; /********* @@ -21,10 +21,10 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters *********/ /// <summary>Construct an instance.</summary> /// <param name="rewriteReferencesToAssemblies">The assembly names to which to rewrite broken references.</param> - public HeuristicFieldRewriter(string[] rewriteReferencesToAssemblies) + public HeuristicFieldRewriter(ISet<string> rewriteReferencesToAssemblies) : base(defaultPhrase: "field changed to property") // ignored since we specify phrases { - this.RewriteReferencesToAssemblies = new HashSet<string>(rewriteReferencesToAssemblies); + this.RewriteReferencesToAssemblies = rewriteReferencesToAssemblies; } /// <inheritdoc /> diff --git a/src/SMAPI/Framework/ModLoading/Rewriters/HeuristicMethodRewriter.cs b/src/SMAPI/Framework/ModLoading/Rewriters/HeuristicMethodRewriter.cs index e133b6fa..89de437e 100644 --- a/src/SMAPI/Framework/ModLoading/Rewriters/HeuristicMethodRewriter.cs +++ b/src/SMAPI/Framework/ModLoading/Rewriters/HeuristicMethodRewriter.cs @@ -13,7 +13,7 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters ** Fields *********/ /// <summary>The assembly names to which to rewrite broken references.</summary> - private readonly HashSet<string> RewriteReferencesToAssemblies; + private readonly ISet<string> RewriteReferencesToAssemblies; /********* @@ -21,10 +21,10 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters *********/ /// <summary>Construct an instance.</summary> /// <param name="rewriteReferencesToAssemblies">The assembly names to which to rewrite broken references.</param> - public HeuristicMethodRewriter(string[] rewriteReferencesToAssemblies) + public HeuristicMethodRewriter(ISet<string> rewriteReferencesToAssemblies) : base(defaultPhrase: "methods with missing parameters") // ignored since we specify phrases { - this.RewriteReferencesToAssemblies = new HashSet<string>(rewriteReferencesToAssemblies); + this.RewriteReferencesToAssemblies = rewriteReferencesToAssemblies; } /// <inheritdoc /> |