From 4d48bdfe7c3806ec1995cd499ca9382ace2d8a53 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 25 Mar 2017 13:50:01 -0400 Subject: drop 'generic' prefix for rewriters since they're all generic now --- .../Rewriters/FieldToPropertyRewriter.cs | 70 ++++++++++++++++++++ .../Rewriters/GenericFieldToPropertyRewriter.cs | 70 -------------------- .../Rewriters/GenericMethodMapper.cs | 74 ---------------------- .../Rewriters/MethodParentRewriter.cs | 74 ++++++++++++++++++++++ 4 files changed, 144 insertions(+), 144 deletions(-) create mode 100644 src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldToPropertyRewriter.cs delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/Rewriters/GenericFieldToPropertyRewriter.cs delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/Rewriters/GenericMethodMapper.cs create mode 100644 src/StardewModdingAPI.AssemblyRewriters/Rewriters/MethodParentRewriter.cs (limited to 'src/StardewModdingAPI.AssemblyRewriters/Rewriters') diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldToPropertyRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldToPropertyRewriter.cs new file mode 100644 index 00000000..caf0a16c --- /dev/null +++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldToPropertyRewriter.cs @@ -0,0 +1,70 @@ +using System; +using Mono.Cecil; +using Mono.Cecil.Cil; +using StardewModdingAPI.AssemblyRewriters.Framework; + +namespace StardewModdingAPI.AssemblyRewriters.Rewriters +{ + /// Rewrites field references into property references. + public class FieldToPropertyRewriter : BaseFieldRewriter + { + /********* + ** Properties + *********/ + /// The type whose field to which references should be rewritten. + private readonly Type Type; + + /// The field name to rewrite. + private readonly string FieldName; + + + /********* + ** Accessors + *********/ + /// A brief noun phrase indicating what the instruction finder matches. + public override string NounPhrase { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The type whose field to which references should be rewritten. + /// The field name to rewrite. + /// A brief noun phrase indicating what the instruction finder matches (or null to generate one). + public FieldToPropertyRewriter(Type type, string fieldName, string nounPhrase = null) + { + this.Type = type; + this.FieldName = fieldName; + this.NounPhrase = nounPhrase ?? $"{type.Name}.{fieldName} field"; + } + + + /********* + ** Protected methods + *********/ + /// Get whether a field reference should be rewritten. + /// The IL instruction. + /// The field reference. + /// Whether the mod was compiled on a different platform. + protected override bool IsMatch(Instruction instruction, FieldReference fieldRef, bool platformChanged) + { + return + fieldRef.DeclaringType.FullName == this.Type.FullName + && fieldRef.Name == this.FieldName; + } + + /// Rewrite a method for compatibility. + /// The module being rewritten. + /// The CIL rewriter. + /// The instruction which references the field. + /// The field reference invoked by the . + /// Metadata for mapping assemblies to the current platform. + protected override void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, FieldReference fieldRef, PlatformAssemblyMap assemblyMap) + { + string methodPrefix = instruction.OpCode == OpCodes.Ldsfld || instruction.OpCode == OpCodes.Ldfld ? "get" : "set"; + MethodReference propertyRef = module.Import(this.Type.GetMethod($"{methodPrefix}_{this.FieldName}")); + cil.Replace(instruction, cil.Create(OpCodes.Call, propertyRef)); + } + } +} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/GenericFieldToPropertyRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/GenericFieldToPropertyRewriter.cs deleted file mode 100644 index f58bcfbb..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/GenericFieldToPropertyRewriter.cs +++ /dev/null @@ -1,70 +0,0 @@ -using System; -using Mono.Cecil; -using Mono.Cecil.Cil; -using StardewModdingAPI.AssemblyRewriters.Framework; - -namespace StardewModdingAPI.AssemblyRewriters.Rewriters -{ - /// Rewrites field references into property references. - public class GenericFieldToPropertyRewriter : BaseFieldRewriter - { - /********* - ** Properties - *********/ - /// The type whose field to which references should be rewritten. - private readonly Type Type; - - /// The field name to rewrite. - private readonly string FieldName; - - - /********* - ** Accessors - *********/ - /// A brief noun phrase indicating what the instruction finder matches. - public override string NounPhrase { get; } - - - /********* - ** Public methods - *********/ - /// Construct an instance. - /// The type whose field to which references should be rewritten. - /// The field name to rewrite. - /// A brief noun phrase indicating what the instruction finder matches (or null to generate one). - public GenericFieldToPropertyRewriter(Type type, string fieldName, string nounPhrase = null) - { - this.Type = type; - this.FieldName = fieldName; - this.NounPhrase = nounPhrase ?? $"{type.Name}.{fieldName} field"; - } - - - /********* - ** Protected methods - *********/ - /// Get whether a field reference should be rewritten. - /// The IL instruction. - /// The field reference. - /// Whether the mod was compiled on a different platform. - protected override bool IsMatch(Instruction instruction, FieldReference fieldRef, bool platformChanged) - { - return - fieldRef.DeclaringType.FullName == this.Type.FullName - && fieldRef.Name == this.FieldName; - } - - /// Rewrite a method for compatibility. - /// The module being rewritten. - /// The CIL rewriter. - /// The instruction which references the field. - /// The field reference invoked by the . - /// Metadata for mapping assemblies to the current platform. - protected override void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, FieldReference fieldRef, PlatformAssemblyMap assemblyMap) - { - string methodPrefix = instruction.OpCode == OpCodes.Ldsfld || instruction.OpCode == OpCodes.Ldfld ? "get" : "set"; - MethodReference propertyRef = module.Import(this.Type.GetMethod($"{methodPrefix}_{this.FieldName}")); - cil.Replace(instruction, cil.Create(OpCodes.Call, propertyRef)); - } - } -} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/GenericMethodMapper.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/GenericMethodMapper.cs deleted file mode 100644 index 49e0aad7..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/GenericMethodMapper.cs +++ /dev/null @@ -1,74 +0,0 @@ -using System; -using Mono.Cecil; -using Mono.Cecil.Cil; -using StardewModdingAPI.AssemblyRewriters.Framework; - -namespace StardewModdingAPI.AssemblyRewriters.Rewriters -{ - /// Rewrites method references from one parent type to another if the signatures match. - public class GenericMethodMapper : BaseMethodRewriter - { - /********* - ** Properties - *********/ - /// The type whose methods to remap. - private readonly Type FromType; - - /// The type with methods to map to. - private readonly Type ToType; - - /// Whether to only rewrite references if loading the assembly on a different platform than it was compiled on. - private readonly bool OnlyIfPlatformChanged; - - - /********* - ** Accessors - *********/ - /// A brief noun phrase indicating what the instruction finder matches. - public override string NounPhrase { get; } - - - /********* - ** Public methods - *********/ - /// 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. - /// A brief noun phrase indicating what the instruction finder matches (or null to generate one). - public GenericMethodMapper(Type fromType, Type toType, bool onlyIfPlatformChanged = false, string nounPhrase = null) - { - this.FromType = fromType; - this.ToType = toType; - this.NounPhrase = nounPhrase ?? $"{fromType.Name} methods"; - this.OnlyIfPlatformChanged = onlyIfPlatformChanged; - } - - - /********* - ** Protected methods - *********/ - /// Get whether a method reference should be rewritten. - /// The IL instruction. - /// The method reference. - /// Whether the mod was compiled on a different platform. - protected override bool IsMatch(Instruction instruction, MethodReference methodRef, bool platformChanged) - { - return - (!this.OnlyIfPlatformChanged || platformChanged) - && methodRef.DeclaringType.FullName == this.FromType.FullName - && this.HasMatchingSignature(this.ToType, methodRef); - } - - /// Rewrite a method for compatibility. - /// The module being rewritten. - /// The CIL rewriter. - /// The instruction which calls the method. - /// The method reference invoked by the . - /// Metadata for mapping assemblies to the current platform. - protected override void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, MethodReference methodRef, PlatformAssemblyMap assemblyMap) - { - methodRef.DeclaringType = module.Import(this.ToType); - } - } -} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/MethodParentRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/MethodParentRewriter.cs new file mode 100644 index 00000000..9c19f473 --- /dev/null +++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/MethodParentRewriter.cs @@ -0,0 +1,74 @@ +using System; +using Mono.Cecil; +using Mono.Cecil.Cil; +using StardewModdingAPI.AssemblyRewriters.Framework; + +namespace StardewModdingAPI.AssemblyRewriters.Rewriters +{ + /// Rewrites method references from one parent type to another if the signatures match. + public class MethodParentRewriter : BaseMethodRewriter + { + /********* + ** Properties + *********/ + /// The type whose methods to remap. + private readonly Type FromType; + + /// The type with methods to map to. + private readonly Type ToType; + + /// Whether to only rewrite references if loading the assembly on a different platform than it was compiled on. + private readonly bool OnlyIfPlatformChanged; + + + /********* + ** Accessors + *********/ + /// A brief noun phrase indicating what the instruction finder matches. + public override string NounPhrase { get; } + + + /********* + ** Public methods + *********/ + /// 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. + /// A brief noun phrase indicating what the instruction finder matches (or null to generate one). + public MethodParentRewriter(Type fromType, Type toType, bool onlyIfPlatformChanged = false, string nounPhrase = null) + { + this.FromType = fromType; + this.ToType = toType; + this.NounPhrase = nounPhrase ?? $"{fromType.Name} methods"; + this.OnlyIfPlatformChanged = onlyIfPlatformChanged; + } + + + /********* + ** Protected methods + *********/ + /// Get whether a method reference should be rewritten. + /// The IL instruction. + /// The method reference. + /// Whether the mod was compiled on a different platform. + protected override bool IsMatch(Instruction instruction, MethodReference methodRef, bool platformChanged) + { + return + (!this.OnlyIfPlatformChanged || platformChanged) + && methodRef.DeclaringType.FullName == this.FromType.FullName + && this.HasMatchingSignature(this.ToType, methodRef); + } + + /// Rewrite a method for compatibility. + /// The module being rewritten. + /// The CIL rewriter. + /// The instruction which calls the method. + /// The method reference invoked by the . + /// Metadata for mapping assemblies to the current platform. + protected override void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, MethodReference methodRef, PlatformAssemblyMap assemblyMap) + { + methodRef.DeclaringType = module.Import(this.ToType); + } + } +} -- cgit