summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldReplaceRewriter.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldReplaceRewriter.cs')
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldReplaceRewriter.cs28
1 files changed, 15 insertions, 13 deletions
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldReplaceRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldReplaceRewriter.cs
index ffd22e7c..95663c49 100644
--- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldReplaceRewriter.cs
+++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldReplaceRewriter.cs
@@ -7,16 +7,13 @@ using StardewModdingAPI.AssemblyRewriters.Finders;
namespace StardewModdingAPI.AssemblyRewriters.Rewriters
{
/// <summary>Rewrites references to one field with another.</summary>
- public class FieldReplaceRewriter : FieldFinder, IInstructionRewriter
+ public class FieldReplaceRewriter : FieldFinder
{
/*********
** Properties
*********/
- /// <summary>The type whose field to which references should be rewritten.</summary>
- private readonly Type Type;
-
- /// <summary>The new field name to reference.</summary>
- private readonly string ToFieldName;
+ /// <summary>The new field to reference.</summary>
+ private readonly FieldInfo ToField;
/*********
@@ -30,8 +27,9 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters
public FieldReplaceRewriter(Type type, string fromFieldName, string toFieldName, string nounPhrase = null)
: base(type.FullName, fromFieldName, nounPhrase)
{
- this.Type = type;
- this.ToFieldName = toFieldName;
+ this.ToField = type.GetField(toFieldName);
+ if (this.ToField == null)
+ throw new InvalidOperationException($"The {type.FullName} class doesn't have a {toFieldName} field.");
}
/// <summary>Rewrite a CIL instruction for compatibility.</summary>
@@ -39,13 +37,17 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters
/// <param name="cil">The CIL rewriter.</param>
/// <param name="instruction">The instruction to rewrite.</param>
/// <param name="assemblyMap">Metadata for mapping assemblies to the current platform.</param>
- public void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap)
+ /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param>
+ /// <returns>Returns whether the instruction was rewritten.</returns>
+ /// <exception cref="IncompatibleInstructionException">The CIL instruction is not compatible, and can't be rewritten.</exception>
+ public override bool Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
{
- FieldInfo field = this.Type.GetField(this.ToFieldName);
- if (field == null)
- throw new InvalidOperationException($"The {this.Type.FullName} class doesn't have a {this.ToFieldName} field.");
- FieldReference newRef = module.Import(field);
+ if (!this.IsMatch(instruction, platformChanged))
+ return false;
+
+ FieldReference newRef = module.Import(this.ToField);
cil.Replace(instruction, cil.Create(instruction.OpCode, newRef));
+ return true;
}
}
}