summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/Framework/BaseFieldRewriter.cs51
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/Framework/BaseMethodRewriter.cs11
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/Rewriters/ActiveClickableMenuRewriter.cs40
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/Rewriters/SpriteBatchRewriter.cs3
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj2
5 files changed, 99 insertions, 8 deletions
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseFieldRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseFieldRewriter.cs
new file mode 100644
index 00000000..7e01ca73
--- /dev/null
+++ b/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseFieldRewriter.cs
@@ -0,0 +1,51 @@
+using Mono.Cecil;
+using Mono.Cecil.Cil;
+
+namespace StardewModdingAPI.AssemblyRewriters.Framework
+{
+ /// <summary>Base class for a field rewriter.</summary>
+ public abstract class BaseFieldRewriter : IInstructionRewriter
+ {
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Get whether a CIL instruction should be rewritten.</summary>
+ /// <param name="instruction">The IL instruction.</param>
+ /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param>
+ public bool ShouldRewrite(Instruction instruction, bool platformChanged)
+ {
+ if (instruction.OpCode != OpCodes.Ldfld && instruction.OpCode != OpCodes.Ldsfld && instruction.OpCode != OpCodes.Stfld && instruction.OpCode != OpCodes.Stsfld)
+ return false; // not a field reference
+ return this.ShouldRewrite(instruction, (FieldReference)instruction.Operand, platformChanged);
+ }
+
+ /// <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)
+ {
+ FieldReference fieldRef = (FieldReference)instruction.Operand;
+ this.Rewrite(module, cil, instruction, fieldRef, assemblyMap);
+ }
+
+
+ /*********
+ ** 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 abstract bool ShouldRewrite(Instruction instruction, FieldReference fieldRef, bool platformChanged);
+
+ /// <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 abstract void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, FieldReference fieldRef, PlatformAssemblyMap assemblyMap);
+ }
+}
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseMethodRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseMethodRewriter.cs
index c9729ee8..e53e5c56 100644
--- a/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseMethodRewriter.cs
+++ b/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseMethodRewriter.cs
@@ -17,13 +17,9 @@ namespace StardewModdingAPI.AssemblyRewriters.Framework
/// <param name="platformChanged">Whether the mod was compiled on a different platform.</param>
public bool ShouldRewrite(Instruction instruction, bool platformChanged)
{
- // ignore non-method-call instructions
if (instruction.OpCode != OpCodes.Call && instruction.OpCode != OpCodes.Callvirt)
- return false;
-
- // check reference
- MethodReference methodRef = (MethodReference)instruction.Operand;
- return this.ShouldRewrite(methodRef, platformChanged);
+ return false; // not a method reference
+ return this.ShouldRewrite(instruction, (MethodReference)instruction.Operand, platformChanged);
}
/// <summary>Rewrite a CIL instruction for compatibility.</summary>
@@ -42,9 +38,10 @@ namespace StardewModdingAPI.AssemblyRewriters.Framework
** Protected methods
*********/
/// <summary>Get whether a method reference should be rewritten.</summary>
+ /// <param name="instruction">The IL instruction.</param>
/// <param name="methodRef">The method reference.</param>
/// <param name="platformChanged">Whether the mod was compiled on a different platform.</param>
- protected abstract bool ShouldRewrite(MethodReference methodRef, bool platformChanged);
+ protected abstract bool ShouldRewrite(Instruction instruction, MethodReference methodRef, bool platformChanged);
/// <summary>Rewrite a method for compatibility.</summary>
/// <param name="module">The module being rewritten.</param>
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/ActiveClickableMenuRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/ActiveClickableMenuRewriter.cs
new file mode 100644
index 00000000..b8aef019
--- /dev/null
+++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/ActiveClickableMenuRewriter.cs
@@ -0,0 +1,40 @@
+using Mono.Cecil;
+using Mono.Cecil.Cil;
+using StardewModdingAPI.AssemblyRewriters.Framework;
+using StardewValley;
+
+namespace StardewModdingAPI.AssemblyRewriters.Rewriters
+{
+ /// <summary>Rewrites field references to <see cref="Game1.activeClickableMenu"/>.</summary>
+ /// <remarks>Stardew Valley changed the <see cref="Game1.activeClickableMenu"/> field to a property, which broke many mods that reference it.</remarks>
+ public class ActiveClickableMenuRewriter : 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.activeClickableMenu);
+ }
+
+ /// <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.activeClickableMenu)}"));
+ cil.Replace(instruction, cil.Create(OpCodes.Call, propertyRef));
+ }
+ }
+}
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SpriteBatchRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SpriteBatchRewriter.cs
index 819578e0..109a6a6e 100644
--- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SpriteBatchRewriter.cs
+++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SpriteBatchRewriter.cs
@@ -15,9 +15,10 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters
** Protected methods
*********/
/// <summary>Get whether a method reference should be rewritten.</summary>
+ /// <param name="instruction">The IL instruction.</param>
/// <param name="methodRef">The method reference.</param>
/// <param name="platformChanged">Whether the mod was compiled on a different platform.</param>
- protected override bool ShouldRewrite(MethodReference methodRef, bool platformChanged)
+ protected override bool ShouldRewrite(Instruction instruction, MethodReference methodRef, bool platformChanged)
{
return platformChanged
&& methodRef.DeclaringType.FullName == typeof(SpriteBatch).FullName
diff --git a/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj b/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj
index 0b549a86..4433131b 100644
--- a/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj
+++ b/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj
@@ -74,8 +74,10 @@
<Compile Include="Platform.cs" />
<Compile Include="PlatformAssemblyMap.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="Framework\BaseFieldRewriter.cs" />
<Compile Include="Framework\BaseMethodRewriter.cs" />
<Compile Include="Framework\RewriteHelper.cs" />
+ <Compile Include="Rewriters\ActiveClickableMenuRewriter.cs" />
<Compile Include="Rewriters\SpriteBatchRewriter.cs" />
</ItemGroup>
<ItemGroup>