diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-02-09 23:24:54 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-02-09 23:24:54 -0500 |
commit | 25a3d9773c7e91903ced41e4bafd164ae42a9644 (patch) | |
tree | 13f802c6e48d784e4ad43f8731198097c7fb57fb /src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_GameMode_FieldRewriter.cs | |
parent | 2b336faa1bcdf7c2abc1cffb72c1def70fa6e8a7 (diff) | |
download | SMAPI-25a3d9773c7e91903ced41e4bafd164ae42a9644.tar.gz SMAPI-25a3d9773c7e91903ced41e4bafd164ae42a9644.tar.bz2 SMAPI-25a3d9773c7e91903ced41e4bafd164ae42a9644.zip |
add field rewriter for the `Game1.gameMode` change in SDV 1.2 (#231)
Diffstat (limited to 'src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_GameMode_FieldRewriter.cs')
-rw-r--r-- | src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_GameMode_FieldRewriter.cs | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_GameMode_FieldRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_GameMode_FieldRewriter.cs new file mode 100644 index 00000000..e8cf3d13 --- /dev/null +++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_GameMode_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.gameMode"/>.</summary> + /// <remarks>Stardew Valley changed the <see cref="Game1.gameMode"/> 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_GameMode_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.gameMode); + } + + /// <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.gameMode)}")); + cil.Replace(instruction, cil.Create(OpCodes.Call, propertyRef)); + } + } +} |