summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_Player_FieldRewriter.cs42
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj1
-rw-r--r--src/StardewModdingAPI/Constants.cs3
3 files changed, 45 insertions, 1 deletions
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_Player_FieldRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_Player_FieldRewriter.cs
new file mode 100644
index 00000000..fb87c19a
--- /dev/null
+++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_Player_FieldRewriter.cs
@@ -0,0 +1,42 @@
+using System.Diagnostics.CodeAnalysis;
+using Mono.Cecil;
+using Mono.Cecil.Cil;
+using StardewModdingAPI.AssemblyRewriters.Framework;
+using StardewValley;
+
+namespace StardewModdingAPI.AssemblyRewriters.Rewriters.SDV1_2
+{
+ /// <summary>Rewrites field references to <see cref="Game1.player"/>.</summary>
+ /// <remarks>Stardew Valley changed the <see cref="Game1.player"/> field to a property, which broke many mods that reference it.</remarks>
+ [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "This class is not meant to be used directly, and is deliberately named to make it easier to know what it changes at a glance.")]
+ public class Game1_Player_FieldRewriter : BaseFieldRewriter
+ {
+ /*********
+ ** Protected methods
+ *********/
+ /// <summary>Get whether a field reference should be rewritten.</summary>
+ /// <param name="instruction">The IL instruction.</param>
+ /// <param name="fieldRef">The field reference.</param>
+ /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param>
+ protected override bool ShouldRewrite(Instruction instruction, FieldReference fieldRef, bool platformChanged)
+ {
+ return
+ (instruction.OpCode == OpCodes.Ldsfld || instruction.OpCode == OpCodes.Stsfld) // static field
+ && fieldRef.DeclaringType.FullName == typeof(Game1).FullName
+ && fieldRef.Name == nameof(Game1.player);
+ }
+
+ /// <summary>Rewrite a method for compatibility.</summary>
+ /// <param name="module">The module being rewritten.</param>
+ /// <param name="cil">The CIL rewriter.</param>
+ /// <param name="instruction">The instruction which references the field.</param>
+ /// <param name="fieldRef">The field reference invoked by the <paramref name="instruction"/>.</param>
+ /// <param name="assemblyMap">Metadata for mapping assemblies to the current platform.</param>
+ protected override void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, FieldReference fieldRef, PlatformAssemblyMap assemblyMap)
+ {
+ string methodPrefix = instruction.OpCode == OpCodes.Ldsfld ? "get" : "set";
+ MethodReference propertyRef = module.Import(typeof(Game1).GetMethod($"{methodPrefix}_{nameof(Game1.player)}"));
+ cil.Replace(instruction, cil.Create(OpCodes.Call, propertyRef));
+ }
+ }
+}
diff --git a/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj b/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj
index a58dc176..2383fb0b 100644
--- a/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj
+++ b/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj
@@ -77,6 +77,7 @@
<Compile Include="Framework\BaseFieldRewriter.cs" />
<Compile Include="Framework\BaseMethodRewriter.cs" />
<Compile Include="Framework\RewriteHelper.cs" />
+ <Compile Include="Rewriters\SDV1_2\Game1_Player_FieldRewriter.cs" />
<Compile Include="Rewriters\SDV1_2\Game1_ActiveClickableMenu_FieldRewriter.cs" />
<Compile Include="Rewriters\Crossplatform\SpriteBatch_MethodRewriter.cs" />
</ItemGroup>
diff --git a/src/StardewModdingAPI/Constants.cs b/src/StardewModdingAPI/Constants.cs
index cee33560..74387ee3 100644
--- a/src/StardewModdingAPI/Constants.cs
+++ b/src/StardewModdingAPI/Constants.cs
@@ -130,7 +130,8 @@ namespace StardewModdingAPI
new SpriteBatch_MethodRewriter(),
// Stardew Valley 1.2
- new Game1_ActiveClickableMenu_FieldRewriter()
+ new Game1_ActiveClickableMenu_FieldRewriter(),
+ new Game1_Player_FieldRewriter()
};
}