reject mods which reference obsolete Game1.borderFont and Game1.smoothFont fields (#247)

This commit is contained in:
Jesse Plamondon-Willard 2017-03-12 03:23:20 -04:00
parent 9fab0bf58f
commit fae362723f
9 changed files with 87 additions and 4 deletions

View File

@ -0,0 +1,35 @@
using System.Diagnostics.CodeAnalysis;
using Mono.Cecil;
using Mono.Cecil.Cil;
using StardewModdingAPI.AssemblyRewriters.Framework;
using StardewValley;
namespace StardewModdingAPI.AssemblyRewriters.Finders
{
/// <summary>Finds CIL instructions that reference the former <c>Game1.borderFont</c> field, which was removed in Stardew Valley 1.2.31.2.6.</summary>
[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_borderFont_FieldFinder : BaseFieldFinder
{
/*********
** Accessors
*********/
/// <summary>A brief noun phrase indicating what the instruction finder matches.</summary>
public override string NounPhrase { get; } = $"obsolete {nameof(Game1)}.borderFont field";
/*********
** 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 IsMatch(Instruction instruction, FieldReference fieldRef, bool platformChanged)
{
return
this.IsStaticField(instruction)
&& fieldRef.DeclaringType.FullName == typeof(Game1).FullName
&& fieldRef.Name == "borderFont";
}
}
}

View File

@ -0,0 +1,35 @@
using System.Diagnostics.CodeAnalysis;
using Mono.Cecil;
using Mono.Cecil.Cil;
using StardewModdingAPI.AssemblyRewriters.Framework;
using StardewValley;
namespace StardewModdingAPI.AssemblyRewriters.Finders
{
/// <summary>Finds CIL instructions that reference the former <c>Game1.smoothFont</c> field, which was removed in Stardew Valley 1.2.31.2.6.</summary>
[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_smoothFont_FieldFinder : BaseFieldFinder
{
/*********
** Accessors
*********/
/// <summary>A brief noun phrase indicating what the instruction finder matches.</summary>
public override string NounPhrase { get; } = $"obsolete {nameof(Game1)}.smoothFont field";
/*********
** 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 IsMatch(Instruction instruction, FieldReference fieldRef, bool platformChanged)
{
return
this.IsStaticField(instruction)
&& fieldRef.DeclaringType.FullName == typeof(Game1).FullName
&& fieldRef.Name == "smoothFont";
}
}
}

View File

@ -5,7 +5,7 @@ using StardewModdingAPI.AssemblyRewriters.Framework;
namespace StardewModdingAPI.AssemblyRewriters.Finders namespace StardewModdingAPI.AssemblyRewriters.Finders
{ {
/// <summary>Matches CIL instructions that reference the former <c>StardewModdingAPI.Extensions</c> class, which was removed in SMAPI 1.9.</summary> /// <summary>Finds CIL instructions that reference the former <c>StardewModdingAPI.Extensions</c> class, which was removed in SMAPI 1.9.</summary>
[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.")] [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 SMAPI_Extensions_MethodFinder : BaseMethodFinder public class SMAPI_Extensions_MethodFinder : BaseMethodFinder
{ {

View File

@ -35,5 +35,12 @@ namespace StardewModdingAPI.AssemblyRewriters.Framework
/// <param name="fieldRef">The field reference.</param> /// <param name="fieldRef">The field reference.</param>
/// <param name="platformChanged">Whether the mod was compiled on a different platform.</param> /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param>
protected abstract bool IsMatch(Instruction instruction, FieldReference fieldRef, bool platformChanged); protected abstract bool IsMatch(Instruction instruction, FieldReference fieldRef, bool platformChanged);
/// <summary>Whether an instruction is a static field reference.</summary>
/// <param name="instruction">The IL instruction.</param>
protected bool IsStaticField(Instruction instruction)
{
return instruction.OpCode == OpCodes.Ldsfld || instruction.OpCode == OpCodes.Stsfld;
}
} }
} }

View File

@ -28,7 +28,7 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters.SDV1_2
protected override bool IsMatch(Instruction instruction, FieldReference fieldRef, bool platformChanged) protected override bool IsMatch(Instruction instruction, FieldReference fieldRef, bool platformChanged)
{ {
return return
(instruction.OpCode == OpCodes.Ldsfld || instruction.OpCode == OpCodes.Stsfld) // static field this.IsStaticField(instruction)
&& fieldRef.DeclaringType.FullName == typeof(Game1).FullName && fieldRef.DeclaringType.FullName == typeof(Game1).FullName
&& fieldRef.Name == nameof(Game1.activeClickableMenu); && fieldRef.Name == nameof(Game1.activeClickableMenu);
} }

View File

@ -28,7 +28,7 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters.SDV1_2
protected override bool IsMatch(Instruction instruction, FieldReference fieldRef, bool platformChanged) protected override bool IsMatch(Instruction instruction, FieldReference fieldRef, bool platformChanged)
{ {
return return
(instruction.OpCode == OpCodes.Ldsfld || instruction.OpCode == OpCodes.Stsfld) // static field this.IsStaticField(instruction)
&& fieldRef.DeclaringType.FullName == typeof(Game1).FullName && fieldRef.DeclaringType.FullName == typeof(Game1).FullName
&& fieldRef.Name == nameof(Game1.gameMode); && fieldRef.Name == nameof(Game1.gameMode);
} }

View File

@ -28,7 +28,7 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters.SDV1_2
protected override bool IsMatch(Instruction instruction, FieldReference fieldRef, bool platformChanged) protected override bool IsMatch(Instruction instruction, FieldReference fieldRef, bool platformChanged)
{ {
return return
(instruction.OpCode == OpCodes.Ldsfld || instruction.OpCode == OpCodes.Stsfld) // static field this.IsStaticField(instruction)
&& fieldRef.DeclaringType.FullName == typeof(Game1).FullName && fieldRef.DeclaringType.FullName == typeof(Game1).FullName
&& fieldRef.Name == nameof(Game1.player); && fieldRef.Name == nameof(Game1.player);
} }

View File

@ -66,6 +66,8 @@
<Compile Include="..\GlobalAssemblyInfo.cs"> <Compile Include="..\GlobalAssemblyInfo.cs">
<Link>Properties\GlobalAssemblyInfo.cs</Link> <Link>Properties\GlobalAssemblyInfo.cs</Link>
</Compile> </Compile>
<Compile Include="Finders\Game1_smoothFont_FieldFinder.cs" />
<Compile Include="Finders\Game1_borderFont_FieldFinder.cs" />
<Compile Include="Finders\SMAPI_Extensions_MethodFinder.cs" /> <Compile Include="Finders\SMAPI_Extensions_MethodFinder.cs" />
<Compile Include="Framework\BaseFieldFinder.cs" /> <Compile Include="Framework\BaseFieldFinder.cs" />
<Compile Include="Framework\BaseMethodFinder.cs" /> <Compile Include="Framework\BaseMethodFinder.cs" />

View File

@ -141,6 +141,10 @@ namespace StardewModdingAPI
{ {
return new IInstructionFinder[] return new IInstructionFinder[]
{ {
// changes in Stardew Valley 1.2
new Game1_borderFont_FieldFinder(),
new Game1_smoothFont_FieldFinder(),
// APIs removed in SMAPI 1.9 // APIs removed in SMAPI 1.9
new SMAPI_Extensions_MethodFinder() new SMAPI_Extensions_MethodFinder()
}; };