diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2020-05-19 20:57:50 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2020-05-19 20:57:50 -0400 |
commit | 1838842bbc2db2d1049c193b8650bd101ba4858f (patch) | |
tree | 650c97b1d00091c53869323307705e78b3766275 /src/SMAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs | |
parent | f96dde00f98a913557617f716673f1af355cc6b5 (diff) | |
download | SMAPI-1838842bbc2db2d1049c193b8650bd101ba4858f.tar.gz SMAPI-1838842bbc2db2d1049c193b8650bd101ba4858f.tar.bz2 SMAPI-1838842bbc2db2d1049c193b8650bd101ba4858f.zip |
rewrite assembly rewriting, merge Harmony rewriters (#711)
This reduces duplication, decouples it from the assembly loader, and makes it more flexible to handle Harmony rewriting.
Diffstat (limited to 'src/SMAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs')
-rw-r--r-- | src/SMAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs | 42 |
1 files changed, 24 insertions, 18 deletions
diff --git a/src/SMAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs b/src/SMAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs index a43c5e9a..c3b5854e 100644 --- a/src/SMAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs +++ b/src/SMAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs @@ -1,21 +1,24 @@ using System; using Mono.Cecil; using Mono.Cecil.Cil; -using StardewModdingAPI.Framework.ModLoading.Finders; +using StardewModdingAPI.Framework.ModLoading.Framework; namespace StardewModdingAPI.Framework.ModLoading.Rewriters { /// <summary>Rewrites field references into property references.</summary> - internal class FieldToPropertyRewriter : FieldFinder + internal class FieldToPropertyRewriter : BaseInstructionHandler { /********* ** Fields *********/ - /// <summary>The type whose field to which references should be rewritten.</summary> + /// <summary>The type containing the field to which references should be rewritten.</summary> private readonly Type Type; - /// <summary>The property name.</summary> - private readonly string PropertyName; + /// <summary>The field name to which references should be rewritten.</summary> + private readonly string FromFieldName; + + /// <summary>The new property name.</summary> + private readonly string ToPropertyName; /********* @@ -26,10 +29,11 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters /// <param name="fieldName">The field name to rewrite.</param> /// <param name="propertyName">The property name (if different).</param> public FieldToPropertyRewriter(Type type, string fieldName, string propertyName) - : base(type.FullName, fieldName, InstructionHandleResult.None) + : base(defaultPhrase: $"{type.FullName}.{fieldName} field") { this.Type = type; - this.PropertyName = propertyName; + this.FromFieldName = fieldName; + this.ToPropertyName = propertyName; } /// <summary>Construct an instance.</summary> @@ -38,22 +42,24 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters public FieldToPropertyRewriter(Type type, string fieldName) : this(type, fieldName, fieldName) { } - /// <summary>Perform the predefined logic for an instruction if applicable.</summary> + /// <summary>Rewrite a CIL instruction reference if needed.</summary> /// <param name="module">The assembly module containing the instruction.</param> /// <param name="cil">The CIL processor.</param> - /// <param name="instruction">The instruction to handle.</param> - /// <param name="assemblyMap">Metadata for mapping assemblies to the current platform.</param> - /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param> - public override InstructionHandleResult Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) + /// <param name="instruction">The CIL instruction to handle.</param> + /// <param name="replaceWith">Replaces the CIL instruction with a new one.</param> + /// <returns>Returns whether the instruction was changed.</returns> + public override bool Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, Action<Instruction> replaceWith) { - if (!this.IsMatch(instruction)) - return InstructionHandleResult.None; + // get field ref + FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction); + if (!RewriteHelper.IsFieldReferenceTo(fieldRef, this.Type.FullName, this.FromFieldName)) + return false; + // replace with property string methodPrefix = instruction.OpCode == OpCodes.Ldsfld || instruction.OpCode == OpCodes.Ldfld ? "get" : "set"; - MethodReference propertyRef = module.ImportReference(this.Type.GetMethod($"{methodPrefix}_{this.PropertyName}")); - cil.Replace(instruction, cil.Create(OpCodes.Call, propertyRef)); - - return InstructionHandleResult.Rewritten; + MethodReference propertyRef = module.ImportReference(this.Type.GetMethod($"{methodPrefix}_{this.ToPropertyName}")); + replaceWith(cil.Create(OpCodes.Call, propertyRef)); + return this.MarkRewritten(); } } } |