From 8c5bd12f4793a9c866b6046c05482592a5c799bc Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Tue, 19 Sep 2017 22:45:28 -0400 Subject: merge assembly rewriters into main SMAPI project (#347) --- .../Rewriters/FieldReplaceRewriter.cs | 53 ---------------------- 1 file changed, 53 deletions(-) delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldReplaceRewriter.cs (limited to 'src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldReplaceRewriter.cs') diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldReplaceRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldReplaceRewriter.cs deleted file mode 100644 index 73844073..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldReplaceRewriter.cs +++ /dev/null @@ -1,53 +0,0 @@ -using System; -using System.Reflection; -using Mono.Cecil; -using Mono.Cecil.Cil; -using StardewModdingAPI.AssemblyRewriters.Finders; - -namespace StardewModdingAPI.AssemblyRewriters.Rewriters -{ - /// Rewrites references to one field with another. - public class FieldReplaceRewriter : FieldFinder - { - /********* - ** Properties - *********/ - /// The new field to reference. - private readonly FieldInfo ToField; - - - /********* - ** Public methods - *********/ - /// Construct an instance. - /// The type whose field to which references should be rewritten. - /// The field name to rewrite. - /// 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.ToField = type.GetField(toFieldName); - if (this.ToField == null) - throw new InvalidOperationException($"The {type.FullName} class doesn't have a {toFieldName} field."); - } - - /// Rewrite a CIL instruction for compatibility. - /// The module being rewritten. - /// The CIL rewriter. - /// The instruction to rewrite. - /// Metadata for mapping assemblies to the current platform. - /// Whether the mod was compiled on a different platform. - /// Returns whether the instruction was rewritten. - /// The CIL instruction is not compatible, and can't be rewritten. - public override bool Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) - { - if (!this.IsMatch(instruction)) - return false; - - FieldReference newRef = module.Import(this.ToField); - cil.Replace(instruction, cil.Create(instruction.OpCode, newRef)); - return true; - } - } -} -- cgit