From f4192663d78c7a45418f07f0bf4acb67b11291fe Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Tue, 5 May 2020 20:53:02 -0400 Subject: add Harmony 2.0 rewriters (#711) --- .../Rewriters/Harmony1AssemblyRewriter.cs | 77 ++++++++++++++++++++++ .../ModLoading/Rewriters/MethodParentRewriter.cs | 18 +++-- .../ModLoading/Rewriters/TypeReferenceRewriter.cs | 68 +++++++++++++++++++ 3 files changed, 158 insertions(+), 5 deletions(-) create mode 100644 src/SMAPI/Framework/ModLoading/Rewriters/Harmony1AssemblyRewriter.cs create mode 100644 src/SMAPI/Framework/ModLoading/Rewriters/TypeReferenceRewriter.cs (limited to 'src/SMAPI/Framework/ModLoading/Rewriters') diff --git a/src/SMAPI/Framework/ModLoading/Rewriters/Harmony1AssemblyRewriter.cs b/src/SMAPI/Framework/ModLoading/Rewriters/Harmony1AssemblyRewriter.cs new file mode 100644 index 00000000..29e44bfe --- /dev/null +++ b/src/SMAPI/Framework/ModLoading/Rewriters/Harmony1AssemblyRewriter.cs @@ -0,0 +1,77 @@ +using System; +using Mono.Cecil; +using StardewModdingAPI.Framework.ModLoading.Finders; +using StardewModdingAPI.Framework.ModLoading.Framework; + +namespace StardewModdingAPI.Framework.ModLoading.Rewriters +{ + /// Rewrites Harmony 1.x assembly references to work with Harmony 2.x. + internal class Harmony1AssemblyRewriter : BaseTypeReferenceRewriter + { + /********* + ** Fields + *********/ + /// The full assembly name to which to find references. + private const string FromAssemblyName = "0Harmony"; + + /// The main Harmony type. + private readonly Type HarmonyType = typeof(HarmonyLib.Harmony); + + + /********* + ** Public methods + *********/ + /// Construct an instance. + public Harmony1AssemblyRewriter() + : base(new TypeAssemblyFinder(Harmony1AssemblyRewriter.FromAssemblyName, InstructionHandleResult.None), "Harmony 1.x types") + { } + + + /********* + ** Private methods + *********/ + /// Change a type reference if needed. + /// The assembly module containing the instruction. + /// The type to replace if it matches. + /// Assign the new type reference. + protected override bool RewriteIfNeeded(ModuleDefinition module, TypeReference type, Action set) + { + bool rewritten = false; + + // current type + if (type.Scope.Name == Harmony1AssemblyRewriter.FromAssemblyName && type.Scope is AssemblyNameReference assemblyScope && assemblyScope.Version.Major == 1) + { + Type targetType = this.GetMappedType(type); + set(module.ImportReference(targetType)); + return true; + } + + // recurse into generic arguments + if (type is GenericInstanceType genericType) + { + for (int i = 0; i < genericType.GenericArguments.Count; i++) + rewritten |= this.RewriteIfNeeded(module, genericType.GenericArguments[i], typeRef => genericType.GenericArguments[i] = typeRef); + } + + // recurse into generic parameters (e.g. constraints) + for (int i = 0; i < type.GenericParameters.Count; i++) + rewritten |= this.RewriteIfNeeded(module, type.GenericParameters[i], typeRef => type.GenericParameters[i] = new GenericParameter(typeRef)); + + return rewritten; + } + + /// Get an equivalent Harmony 2.x type. + /// The Harmony 1.x method. + private Type GetMappedType(TypeReference type) + { + // main Harmony object + if (type.FullName == "Harmony.HarmonyInstance") + return this.HarmonyType; + + // other objects + string fullName = type.FullName.Replace("Harmony.", "HarmonyLib."); + string targetName = this.HarmonyType.AssemblyQualifiedName.Replace(this.HarmonyType.FullName, fullName); + return Type.GetType(targetName, throwOnError: true); + } + } +} diff --git a/src/SMAPI/Framework/ModLoading/Rewriters/MethodParentRewriter.cs b/src/SMAPI/Framework/ModLoading/Rewriters/MethodParentRewriter.cs index 6b8c2de1..0984dc44 100644 --- a/src/SMAPI/Framework/ModLoading/Rewriters/MethodParentRewriter.cs +++ b/src/SMAPI/Framework/ModLoading/Rewriters/MethodParentRewriter.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using Mono.Cecil; using Mono.Cecil.Cil; @@ -10,8 +11,8 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters /********* ** Fields *********/ - /// The type whose methods to remap. - private readonly Type FromType; + /// The full name of the type whose methods to remap. + private readonly string FromType; /// The type with methods to map to. private readonly Type ToType; @@ -34,14 +35,21 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters /// The type whose methods to remap. /// The type with methods to map to. /// Whether to only rewrite references if loading the assembly on a different platform than it was compiled on. - public MethodParentRewriter(Type fromType, Type toType, bool onlyIfPlatformChanged = false) + public MethodParentRewriter(string fromType, Type toType, bool onlyIfPlatformChanged = false) { this.FromType = fromType; this.ToType = toType; - this.NounPhrase = $"{fromType.Name} methods"; + this.NounPhrase = $"{fromType.Split('.').Last()} methods"; this.OnlyIfPlatformChanged = onlyIfPlatformChanged; } + /// Construct an instance. + /// The type whose methods to remap. + /// The type with methods to map to. + /// Whether to only rewrite references if loading the assembly on a different platform than it was compiled on. + public MethodParentRewriter(Type fromType, Type toType, bool onlyIfPlatformChanged = false) + : this(fromType.FullName, toType, onlyIfPlatformChanged) { } + /// Perform the predefined logic for a method if applicable. /// The assembly module containing the instruction. /// The method definition containing the instruction. @@ -81,7 +89,7 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters return methodRef != null && (platformChanged || !this.OnlyIfPlatformChanged) - && methodRef.DeclaringType.FullName == this.FromType.FullName + && methodRef.DeclaringType.FullName == this.FromType && RewriteHelper.HasMatchingSignature(this.ToType, methodRef); } } diff --git a/src/SMAPI/Framework/ModLoading/Rewriters/TypeReferenceRewriter.cs b/src/SMAPI/Framework/ModLoading/Rewriters/TypeReferenceRewriter.cs new file mode 100644 index 00000000..d95e5ac9 --- /dev/null +++ b/src/SMAPI/Framework/ModLoading/Rewriters/TypeReferenceRewriter.cs @@ -0,0 +1,68 @@ +using System; +using Mono.Cecil; +using StardewModdingAPI.Framework.ModLoading.Finders; +using StardewModdingAPI.Framework.ModLoading.Framework; + +namespace StardewModdingAPI.Framework.ModLoading.Rewriters +{ + /// Rewrites all references to a type. + internal class TypeReferenceRewriter : BaseTypeReferenceRewriter + { + /********* + ** Fields + *********/ + /// The full type name to which to find references. + private readonly string FromTypeName; + + /// The new type to reference. + private readonly Type ToType; + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The full type name to which to find references. + /// The new type to reference. + /// A lambda which overrides a matched type. + public TypeReferenceRewriter(string fromTypeFullName, Type toType, Func shouldIgnore = null) + : base(new TypeFinder(fromTypeFullName, InstructionHandleResult.None, shouldIgnore), $"{fromTypeFullName} type") + { + this.FromTypeName = fromTypeFullName; + this.ToType = toType; + } + + + /********* + ** Protected methods + *********/ + /// Change a type reference if needed. + /// The assembly module containing the instruction. + /// The type to replace if it matches. + /// Assign the new type reference. + protected override bool RewriteIfNeeded(ModuleDefinition module, TypeReference type, Action set) + { + bool rewritten = false; + + // current type + if (type.FullName == this.FromTypeName) + { + set(module.ImportReference(this.ToType)); + return true; + } + + // recurse into generic arguments + if (type is GenericInstanceType genericType) + { + for (int i = 0; i < genericType.GenericArguments.Count; i++) + rewritten |= this.RewriteIfNeeded(module, genericType.GenericArguments[i], typeRef => genericType.GenericArguments[i] = typeRef); + } + + // recurse into generic parameters (e.g. constraints) + for (int i = 0; i < type.GenericParameters.Count; i++) + rewritten |= this.RewriteIfNeeded(module, type.GenericParameters[i], typeRef => type.GenericParameters[i] = new GenericParameter(typeRef)); + + return rewritten; + } + } +} -- cgit