summaryrefslogtreecommitdiff
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
parent41715cefcde3c838bb079cb37aac5a3b2dcb1004 (diff)
downloadSMAPI-b8f17e6afb00b78ac31df98f7dc3bbe071a99e47.tar.gz
SMAPI-b8f17e6afb00b78ac31df98f7dc3bbe071a99e47.tar.bz2
SMAPI-b8f17e6afb00b78ac31df98f7dc3bbe071a99e47.zip
update rewriters for Stardew Valley 1.3 (#453)
-rw-r--r--src/SMAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs18
-rw-r--r--src/SMAPI/Framework/ModLoading/Rewriters/StaticFieldToConstantRewriter.cs63
-rw-r--r--src/SMAPI/Metadata/InstructionMetadata.cs37
-rw-r--r--src/SMAPI/StardewModdingAPI.csproj1
4 files changed, 105 insertions, 14 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.");
+ }
+ }
+}
diff --git a/src/SMAPI/Metadata/InstructionMetadata.cs b/src/SMAPI/Metadata/InstructionMetadata.cs
index 4ed1e38e..603a43cb 100644
--- a/src/SMAPI/Metadata/InstructionMetadata.cs
+++ b/src/SMAPI/Metadata/InstructionMetadata.cs
@@ -6,6 +6,9 @@ using StardewModdingAPI.Framework.ModLoading;
using StardewModdingAPI.Framework.ModLoading.Finders;
using StardewModdingAPI.Framework.ModLoading.Rewriters;
using StardewValley;
+#if STARDEW_VALLEY_1_3
+using SObject = StardewValley.Object;
+#endif
namespace StardewModdingAPI.Metadata
{
@@ -31,10 +34,11 @@ namespace StardewModdingAPI.Metadata
/****
** rewrite CIL to fix incompatible code
****/
- // crossplatform
+ // rewrite for crossplatform compatibility
new MethodParentRewriter(typeof(SpriteBatch), typeof(SpriteBatchMethods), onlyIfPlatformChanged: true),
- // Stardew Valley 1.2
+#if !STARDEW_VALLEY_1_3
+ // rewrite for Stardew Valley 1.2
new FieldToPropertyRewriter(typeof(Game1), nameof(Game1.activeClickableMenu)),
new FieldToPropertyRewriter(typeof(Game1), nameof(Game1.currentMinigame)),
new FieldToPropertyRewriter(typeof(Game1), nameof(Game1.gameMode)),
@@ -42,19 +46,33 @@ namespace StardewModdingAPI.Metadata
new FieldReplaceRewriter(typeof(Game1), "borderFont", nameof(Game1.smallFont)),
new FieldReplaceRewriter(typeof(Game1), "smoothFont", nameof(Game1.smallFont)),
- // SMAPI 1.9
+ // rewrite for SMAPI 1.9
new TypeReferenceRewriter("StardewModdingAPI.Inheritance.ItemStackChange", typeof(ItemStackChange)),
+#endif
- // SMAPI 2.0
- new VirtualEntryCallRemover(), // Mod.Entry changed from virtual to abstract in SMAPI 2.0, which breaks the few mods which called base.Entry()
+ // rewrite for SMAPI 2.0
+ new VirtualEntryCallRemover(),
+
+ // rewrite for Stardew Valley 1.3
+#if STARDEW_VALLEY_1_3
+ new StaticFieldToConstantRewriter<int>(typeof(Game1), "tileSize", Game1.tileSize),
+ new FieldToPropertyRewriter(typeof(Character), nameof(Character.name), nameof(Character.Name)),
+ new FieldToPropertyRewriter(typeof(GameLocation), nameof(GameLocation.isFarm), nameof(GameLocation.IsFarm)),
+ new FieldToPropertyRewriter(typeof(GameLocation), nameof(GameLocation.isOutdoors), nameof(GameLocation.isOutdoors)),
+ new FieldToPropertyRewriter(typeof(GameLocation), nameof(GameLocation.name), nameof(GameLocation.Name)),
+ new FieldToPropertyRewriter(typeof(Item), nameof(Item.category), nameof(Item.Category)),
+ new FieldToPropertyRewriter(typeof(SObject), nameof(SObject.quality), nameof(SObject.Quality)),
+ new FieldToPropertyRewriter(typeof(SObject), nameof(SObject.stack), nameof(SObject.Stack)),
+#endif
/****
** detect incompatible code
****/
- // changes in Stardew Valley 1.2 (with no rewriters)
+ #if !STARDEW_VALLEY_1_3
+ // detect changes in Stardew Valley 1.2
new FieldFinder("StardewValley.Item", "set_Name", InstructionHandleResult.NotCompatible),
- // APIs removed in SMAPI 1.9
+ // detect APIs removed in SMAPI 1.9
new TypeFinder("StardewModdingAPI.Advanced.ConfigFile", InstructionHandleResult.NotCompatible),
new TypeFinder("StardewModdingAPI.Advanced.IConfigFile", InstructionHandleResult.NotCompatible),
new TypeFinder("StardewModdingAPI.Entities.SPlayer", InstructionHandleResult.NotCompatible),
@@ -71,7 +89,7 @@ namespace StardewModdingAPI.Metadata
new EventFinder("StardewModdingAPI.Events.GraphicsEvents", "OnPreRenderHudEventNoCheck", InstructionHandleResult.NotCompatible),
new EventFinder("StardewModdingAPI.Events.GraphicsEvents", "OnPreRenderGuiEventNoCheck", InstructionHandleResult.NotCompatible),
- // APIs removed in SMAPI 2.0
+ // detect APIs removed in SMAPI 2.0
new TypeFinder("StardewModdingAPI.Command", InstructionHandleResult.NotCompatible),
new TypeFinder("StardewModdingAPI.Config", InstructionHandleResult.NotCompatible),
new TypeFinder("StardewModdingAPI.Log", InstructionHandleResult.NotCompatible),
@@ -93,8 +111,9 @@ namespace StardewModdingAPI.Metadata
new PropertyFinder("StardewModdingAPI.Mod", "BaseConfigPath", InstructionHandleResult.NotCompatible),
new PropertyFinder("StardewModdingAPI.Mod", "PerSaveConfigFolder", InstructionHandleResult.NotCompatible),
new PropertyFinder("StardewModdingAPI.Mod", "PerSaveConfigPath", InstructionHandleResult.NotCompatible),
+ #endif
- // broken code
+ // detect broken code
new ReferenceToMissingMemberFinder(this.ValidateReferencesToAssemblies),
new ReferenceToMemberWithUnexpectedTypeFinder(this.ValidateReferencesToAssemblies),
diff --git a/src/SMAPI/StardewModdingAPI.csproj b/src/SMAPI/StardewModdingAPI.csproj
index 42bcb9a8..1dc7740e 100644
--- a/src/SMAPI/StardewModdingAPI.csproj
+++ b/src/SMAPI/StardewModdingAPI.csproj
@@ -112,6 +112,7 @@
<Compile Include="Framework\ModLoading\PlatformAssemblyMap.cs" />
<Compile Include="Framework\ModLoading\RewriteHelper.cs" />
<Compile Include="Framework\ModLoading\Rewriters\FieldReplaceRewriter.cs" />
+ <Compile Include="Framework\ModLoading\Rewriters\StaticFieldToConstantRewriter.cs" />
<Compile Include="Framework\ModLoading\Rewriters\FieldToPropertyRewriter.cs" />
<Compile Include="Framework\ModLoading\Finders\ReferenceToMemberWithUnexpectedTypeFinder.cs" />
<Compile Include="Framework\ModLoading\Rewriters\VirtualEntryCallRemover.cs" />