summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/ModLoading
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2018-03-11 19:10:08 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2018-03-11 19:10:08 -0400
commitb8f17e6afb00b78ac31df98f7dc3bbe071a99e47 (patch)
tree72fab9f23d7e8e0ca44a30f1791dd1adff8fe20a /src/SMAPI/Framework/ModLoading
parent41715cefcde3c838bb079cb37aac5a3b2dcb1004 (diff)
downloadSMAPI-b8f17e6afb00b78ac31df98f7dc3bbe071a99e47.tar.gz
SMAPI-b8f17e6afb00b78ac31df98f7dc3bbe071a99e47.tar.bz2
SMAPI-b8f17e6afb00b78ac31df98f7dc3bbe071a99e47.zip
update rewriters for Stardew Valley 1.3 (#453)
Diffstat (limited to 'src/SMAPI/Framework/ModLoading')
-rw-r--r--src/SMAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs18
-rw-r--r--src/SMAPI/Framework/ModLoading/Rewriters/StaticFieldToConstantRewriter.cs63
2 files changed, 76 insertions, 5 deletions
diff --git a/src/SMAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs b/src/SMAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs
index a20b8bee..b1fa377a 100644
--- a/src/SMAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs
+++ b/src/SMAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs
@@ -14,8 +14,8 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters
/// <summary>The type whose field to which references should be rewritten.</summary>
private readonly Type Type;
- /// <summary>The field name to rewrite.</summary>
- private readonly string FieldName;
+ /// <summary>The property name.</summary>
+ private readonly string PropertyName;
/*********
@@ -24,13 +24,20 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters
/// <summary>Construct an instance.</summary>
/// <param name="type">The type whose field to which references should be rewritten.</param>
/// <param name="fieldName">The field name to rewrite.</param>
- public FieldToPropertyRewriter(Type type, string fieldName)
+ /// <param name="propertyName">The property name (if different).</param>
+ public FieldToPropertyRewriter(Type type, string fieldName, string propertyName)
: base(type.FullName, fieldName, InstructionHandleResult.None)
{
this.Type = type;
- this.FieldName = fieldName;
+ this.PropertyName = propertyName;
}
+ /// <summary>Construct an instance.</summary>
+ /// <param name="type">The type whose field to which references should be rewritten.</param>
+ /// <param name="fieldName">The field name to rewrite.</param>
+ public FieldToPropertyRewriter(Type type, string fieldName)
+ : this(type, fieldName, fieldName) { }
+
/// <summary>Perform the predefined logic for an instruction if applicable.</summary>
/// <param name="module">The assembly module containing the instruction.</param>
/// <param name="cil">The CIL processor.</param>
@@ -43,8 +50,9 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters
return InstructionHandleResult.None;
string methodPrefix = instruction.OpCode == OpCodes.Ldsfld || instruction.OpCode == OpCodes.Ldfld ? "get" : "set";
- MethodReference propertyRef = module.Import(this.Type.GetMethod($"{methodPrefix}_{this.FieldName}"));
+ MethodReference propertyRef = module.Import(this.Type.GetMethod($"{methodPrefix}_{this.PropertyName}"));
cil.Replace(instruction, cil.Create(OpCodes.Call, propertyRef));
+
return InstructionHandleResult.Rewritten;
}
}
diff --git a/src/SMAPI/Framework/ModLoading/Rewriters/StaticFieldToConstantRewriter.cs b/src/SMAPI/Framework/ModLoading/Rewriters/StaticFieldToConstantRewriter.cs
new file mode 100644
index 00000000..5e12b46a
--- /dev/null
+++ b/src/SMAPI/Framework/ModLoading/Rewriters/StaticFieldToConstantRewriter.cs
@@ -0,0 +1,63 @@
+using System;
+using Mono.Cecil;
+using Mono.Cecil.Cil;
+using StardewModdingAPI.Framework.ModLoading.Finders;
+
+namespace StardewModdingAPI.Framework.ModLoading.Rewriters
+{
+ /// <summary>Rewrites static field references into constant values.</summary>
+ /// <typeparam name="TValue">The constant value type.</typeparam>
+ internal class StaticFieldToConstantRewriter<TValue> : FieldFinder
+ {
+ /*********
+ ** Properties
+ *********/
+ /// <summary>The constant value to replace with.</summary>
+ private readonly TValue Value;
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="type">The type whose field to which references should be rewritten.</param>
+ /// <param name="fieldName">The field name to rewrite.</param>
+ /// <param name="value">The constant value to replace with.</param>
+ public StaticFieldToConstantRewriter(Type type, string fieldName, TValue value)
+ : base(type.FullName, fieldName, InstructionHandleResult.None)
+ {
+ this.Value = value;
+ }
+
+ /// <summary>Perform the predefined logic for an instruction if applicable.</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)
+ {
+ if (!this.IsMatch(instruction))
+ return InstructionHandleResult.None;
+
+ cil.Replace(instruction, this.CreateConstantInstruction(cil, this.Value));
+ return InstructionHandleResult.Rewritten;
+ }
+
+
+ /*********
+ ** Private methods
+ *********/
+ /// <summary>Create a CIL constant value instruction.</summary>
+ /// <param name="cil">The CIL processor.</param>
+ /// <param name="value">The constant value to set.</param>
+ private Instruction CreateConstantInstruction(ILProcessor cil, object value)
+ {
+ if (typeof(TValue) == typeof(int))
+ return cil.Create(OpCodes.Ldc_I4, (int)value);
+ if (typeof(TValue) == typeof(string))
+ return cil.Create(OpCodes.Ldstr, (string)value);
+ throw new NotSupportedException($"Rewriting to constant values of type {typeof(TValue)} isn't currently supported.");
+ }
+ }
+}