summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI.AssemblyRewriters/Rewriters
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
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')
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldReplaceRewriter.cs28
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldToPropertyRewriter.cs11
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/Rewriters/MethodParentRewriter.cs35
3 files changed, 47 insertions, 27 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;
}
}
}
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldToPropertyRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldToPropertyRewriter.cs
index f2f99cc1..a25f3fef 100644
--- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldToPropertyRewriter.cs
+++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldToPropertyRewriter.cs
@@ -6,7 +6,7 @@ using StardewModdingAPI.AssemblyRewriters.Finders;
namespace StardewModdingAPI.AssemblyRewriters.Rewriters
{
/// <summary>Rewrites field references into property references.</summary>
- public class FieldToPropertyRewriter : FieldFinder, IInstructionRewriter
+ public class FieldToPropertyRewriter : FieldFinder
{
/*********
** Properties
@@ -37,11 +37,18 @@ 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)
{
+ if (!this.IsMatch(instruction, platformChanged))
+ return false;
+
string methodPrefix = instruction.OpCode == OpCodes.Ldsfld || instruction.OpCode == OpCodes.Ldfld ? "get" : "set";
MethodReference propertyRef = module.Import(this.Type.GetMethod($"{methodPrefix}_{this.FieldName}"));
cil.Replace(instruction, cil.Create(OpCodes.Call, propertyRef));
+ return true;
}
}
}
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/MethodParentRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/MethodParentRewriter.cs
index 24d4dff9..3ec8c704 100644
--- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/MethodParentRewriter.cs
+++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/MethodParentRewriter.cs
@@ -43,10 +43,32 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters
this.OnlyIfPlatformChanged = onlyIfPlatformChanged;
}
+ /// <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 to rewrite.</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>
+ /// <returns>Returns whether the instruction was rewritten.</returns>
+ /// <exception cref="IncompatibleInstructionException">The CIL instruction is not compatible, and can't be rewritten.</exception>
+ public bool Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
+ {
+ if (!this.IsMatch(instruction, platformChanged))
+ return false;
+
+ MethodReference methodRef = (MethodReference)instruction.Operand;
+ methodRef.DeclaringType = module.Import(this.ToType);
+ return true;
+ }
+
+
+ /*********
+ ** Protected methods
+ *********/
/// <summary>Get whether a CIL instruction matches.</summary>
/// <param name="instruction">The IL instruction.</param>
/// <param name="platformChanged">Whether the mod was compiled on a different platform.</param>
- public bool IsMatch(Instruction instruction, bool platformChanged)
+ protected bool IsMatch(Instruction instruction, bool platformChanged)
{
MethodReference methodRef = RewriteHelper.AsMethodReference(instruction);
return
@@ -55,16 +77,5 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters
&& methodRef.DeclaringType.FullName == this.FromType.FullName
&& RewriteHelper.HasMatchingSignature(this.ToType, methodRef);
}
-
- /// <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 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)
- {
- MethodReference methodRef = (MethodReference)instruction.Operand;
- methodRef.DeclaringType = module.Import(this.ToType);
- }
}
}