From 7b641d816466fe7d9229374c175f59ee32b8dc5c Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 25 Mar 2017 15:17:26 -0400 Subject: simplify CIL rewriter hierarchy --- .../Rewriters/FieldReplaceRewriter.cs | 37 ++++------------------ .../Rewriters/FieldToPropertyRewriter.cs | 31 ++++-------------- .../Rewriters/MethodParentRewriter.cs | 24 +++++++------- 3 files changed, 25 insertions(+), 67 deletions(-) (limited to 'src/StardewModdingAPI.AssemblyRewriters/Rewriters') diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldReplaceRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldReplaceRewriter.cs index 31f9a40f..a715e07b 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldReplaceRewriter.cs +++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldReplaceRewriter.cs @@ -2,12 +2,12 @@ using System; using System.Reflection; using Mono.Cecil; using Mono.Cecil.Cil; -using StardewModdingAPI.AssemblyRewriters.Framework; +using StardewModdingAPI.AssemblyRewriters.Finders; namespace StardewModdingAPI.AssemblyRewriters.Rewriters { /// Rewrites references to one field with another. - public class FieldReplaceRewriter : BaseFieldRewriter + public class FieldReplaceRewriter : FieldFinder, IInstructionRewriter { /********* ** Properties @@ -15,20 +15,10 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters /// The type whose field to which references should be rewritten. private readonly Type Type; - /// The field name to rewrite. - private readonly string FromFieldName; - /// The new field name to reference. private readonly string ToFieldName; - /********* - ** Accessors - *********/ - /// A brief noun phrase indicating what the instruction finder matches. - public override string NounPhrase { get; } - - /********* ** Public methods *********/ @@ -38,38 +28,25 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters /// The new field name to reference. /// A brief noun phrase indicating what the instruction finder matches (or null to generate one). public FieldReplaceRewriter(Type type, string fromFieldName, string toFieldName, string nounPhrase = null) + : base(type.FullName, fromFieldName, nounPhrase) { this.Type = type; - this.FromFieldName = fromFieldName; this.ToFieldName = toFieldName; - this.NounPhrase = nounPhrase ?? $"{type.Name}.{fromFieldName} 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.FromFieldName; - } - - /// Rewrite a method for compatibility. + /// Rewrite a CIL instruction for compatibility. /// The module being rewritten. /// The CIL rewriter. - /// The instruction which references the field. - /// The field reference invoked by the . + /// The instruction to rewrite. /// Metadata for mapping assemblies to the current platform. - protected override void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, FieldReference fieldRef, PlatformAssemblyMap assemblyMap) + public void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap) { FieldInfo field = this.Type.GetField(this.ToFieldName); - if(field == null) + if (field == null) throw new InvalidOperationException($"The {this.Type.FullName} class doesn't have a {this.ToFieldName} field."); FieldReference newRef = module.Import(field); cil.Replace(instruction, cil.Create(instruction.OpCode, newRef)); diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldToPropertyRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldToPropertyRewriter.cs index caf0a16c..62e24559 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldToPropertyRewriter.cs +++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldToPropertyRewriter.cs @@ -1,12 +1,12 @@ using System; using Mono.Cecil; using Mono.Cecil.Cil; -using StardewModdingAPI.AssemblyRewriters.Framework; +using StardewModdingAPI.AssemblyRewriters.Finders; namespace StardewModdingAPI.AssemblyRewriters.Rewriters { /// Rewrites field references into property references. - public class FieldToPropertyRewriter : BaseFieldRewriter + public class FieldToPropertyRewriter : FieldFinder, IInstructionRewriter { /********* ** Properties @@ -18,13 +18,6 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters private readonly string FieldName; - /********* - ** Accessors - *********/ - /// A brief noun phrase indicating what the instruction finder matches. - public override string NounPhrase { get; } - - /********* ** Public methods *********/ @@ -33,34 +26,22 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters /// 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) + : base(type.FullName, fieldName, nounPhrase) { 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. + /// Rewrite a CIL instruction for compatibility. /// The module being rewritten. /// The CIL rewriter. - /// The instruction which references the field. - /// The field reference invoked by the . + /// The instruction to rewrite. /// Metadata for mapping assemblies to the current platform. - protected override void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, FieldReference fieldRef, PlatformAssemblyMap assemblyMap) + public void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap) { string methodPrefix = instruction.OpCode == OpCodes.Ldsfld || instruction.OpCode == OpCodes.Ldfld ? "get" : "set"; MethodReference propertyRef = module.Import(this.Type.GetMethod($"{methodPrefix}_{this.FieldName}")); diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/MethodParentRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/MethodParentRewriter.cs index 9c19f473..9b895056 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/MethodParentRewriter.cs +++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/MethodParentRewriter.cs @@ -1,12 +1,11 @@ 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 + public class MethodParentRewriter : IInstructionRewriter { /********* ** Properties @@ -25,7 +24,7 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters ** Accessors *********/ /// A brief noun phrase indicating what the instruction finder matches. - public override string NounPhrase { get; } + public string NounPhrase { get; } /********* @@ -48,26 +47,27 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters /********* ** Protected methods *********/ - /// Get whether a method reference should be rewritten. + /// Get whether a CIL instruction matches. /// 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) + public bool IsMatch(Instruction instruction, bool platformChanged) { + MethodReference methodRef = RewriteHelper.AsMethodReference(instruction); return - (!this.OnlyIfPlatformChanged || platformChanged) + methodRef != null + && (platformChanged || !this.OnlyIfPlatformChanged) && methodRef.DeclaringType.FullName == this.FromType.FullName - && this.HasMatchingSignature(this.ToType, methodRef); + && RewriteHelper.HasMatchingSignature(this.ToType, methodRef); } - /// Rewrite a method for compatibility. + /// Rewrite a CIL instruction for compatibility. /// The module being rewritten. /// The CIL rewriter. - /// The instruction which calls the method. - /// The method reference invoked by the . + /// The instruction to rewrite. /// Metadata for mapping assemblies to the current platform. - protected override void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, MethodReference methodRef, PlatformAssemblyMap assemblyMap) + public void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap) { + MethodReference methodRef = (MethodReference)instruction.Operand; methodRef.DeclaringType = module.Import(this.ToType); } } -- cgit