summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldReplaceRewriter.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-03-26 19:01:35 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-03-26 19:01:35 -0400
commit85ed48809032fdbb8461ce4c34acfbe06f68652b (patch)
tree957c768ddcb8d3a7dc4928803aae775e9b632970 /src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldReplaceRewriter.cs
parent23443721cd2cc5391510a6e65b4e6559037e5b5e (diff)
downloadSMAPI-85ed48809032fdbb8461ce4c34acfbe06f68652b.tar.gz
SMAPI-85ed48809032fdbb8461ce4c34acfbe06f68652b.tar.bz2
SMAPI-85ed48809032fdbb8461ce4c34acfbe06f68652b.zip
merge CIL finders & rewriters into one interface (#254)
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;
}
}
}