diff options
Diffstat (limited to 'src/StardewModdingAPI.AssemblyRewriters/Rewriters')
3 files changed, 25 insertions, 67 deletions
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 { /// <summary>Rewrites references to one field with another.</summary> - public class FieldReplaceRewriter : BaseFieldRewriter + public class FieldReplaceRewriter : FieldFinder, IInstructionRewriter { /********* ** Properties @@ -15,21 +15,11 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters /// <summary>The type whose field to which references should be rewritten.</summary> private readonly Type Type; - /// <summary>The field name to rewrite.</summary> - private readonly string FromFieldName; - /// <summary>The new field name to reference.</summary> private readonly string ToFieldName; /********* - ** Accessors - *********/ - /// <summary>A brief noun phrase indicating what the instruction finder matches.</summary> - public override string NounPhrase { get; } - - - /********* ** Public methods *********/ /// <summary>Construct an instance.</summary> @@ -38,38 +28,25 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters /// <param name="toFieldName">The new field name to reference.</param> /// <param name="nounPhrase">A brief noun phrase indicating what the instruction finder matches (or <c>null</c> to generate one).</param> 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 *********/ - /// <summary>Get whether a field reference should be rewritten.</summary> - /// <param name="instruction">The IL instruction.</param> - /// <param name="fieldRef">The field reference.</param> - /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param> - protected override bool IsMatch(Instruction instruction, FieldReference fieldRef, bool platformChanged) - { - return - fieldRef.DeclaringType.FullName == this.Type.FullName - && fieldRef.Name == this.FromFieldName; - } - - /// <summary>Rewrite a method for compatibility.</summary> + /// <summary>Rewrite a CIL instruction for compatibility.</summary> /// <param name="module">The module being rewritten.</param> /// <param name="cil">The CIL rewriter.</param> - /// <param name="instruction">The instruction which references the field.</param> - /// <param name="fieldRef">The field reference invoked by the <paramref name="instruction"/>.</param> + /// <param name="instruction">The instruction to rewrite.</param> /// <param name="assemblyMap">Metadata for mapping assemblies to the current platform.</param> - 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 { /// <summary>Rewrites field references into property references.</summary> - public class FieldToPropertyRewriter : BaseFieldRewriter + public class FieldToPropertyRewriter : FieldFinder, IInstructionRewriter { /********* ** Properties @@ -19,13 +19,6 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters /********* - ** Accessors - *********/ - /// <summary>A brief noun phrase indicating what the instruction finder matches.</summary> - public override string NounPhrase { get; } - - - /********* ** Public methods *********/ /// <summary>Construct an instance.</summary> @@ -33,34 +26,22 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters /// <param name="fieldName">The field name to rewrite.</param> /// <param name="nounPhrase">A brief noun phrase indicating what the instruction finder matches (or <c>null</c> to generate one).</param> 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 *********/ - /// <summary>Get whether a field reference should be rewritten.</summary> - /// <param name="instruction">The IL instruction.</param> - /// <param name="fieldRef">The field reference.</param> - /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param> - protected override bool IsMatch(Instruction instruction, FieldReference fieldRef, bool platformChanged) - { - return - fieldRef.DeclaringType.FullName == this.Type.FullName - && fieldRef.Name == this.FieldName; - } - - /// <summary>Rewrite a method for compatibility.</summary> + /// <summary>Rewrite a CIL instruction for compatibility.</summary> /// <param name="module">The module being rewritten.</param> /// <param name="cil">The CIL rewriter.</param> - /// <param name="instruction">The instruction which references the field.</param> - /// <param name="fieldRef">The field reference invoked by the <paramref name="instruction"/>.</param> + /// <param name="instruction">The instruction to rewrite.</param> /// <param name="assemblyMap">Metadata for mapping assemblies to the current platform.</param> - 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 { /// <summary>Rewrites method references from one parent type to another if the signatures match.</summary> - public class MethodParentRewriter : BaseMethodRewriter + public class MethodParentRewriter : IInstructionRewriter { /********* ** Properties @@ -25,7 +24,7 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters ** Accessors *********/ /// <summary>A brief noun phrase indicating what the instruction finder matches.</summary> - public override string NounPhrase { get; } + public string NounPhrase { get; } /********* @@ -48,26 +47,27 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters /********* ** Protected methods *********/ - /// <summary>Get whether a method reference should be rewritten.</summary> + /// <summary>Get whether a CIL instruction matches.</summary> /// <param name="instruction">The IL instruction.</param> - /// <param name="methodRef">The method reference.</param> /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param> - 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); } - /// <summary>Rewrite a method for compatibility.</summary> + /// <summary>Rewrite a CIL instruction for compatibility.</summary> /// <param name="module">The module being rewritten.</param> /// <param name="cil">The CIL rewriter.</param> - /// <param name="instruction">The instruction which calls the method.</param> - /// <param name="methodRef">The method reference invoked by the <paramref name="instruction"/>.</param> + /// <param name="instruction">The instruction to rewrite.</param> /// <param name="assemblyMap">Metadata for mapping assemblies to the current platform.</param> - 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); } } |