reject mods which reference obsolete SGame class (#247)

This commit is contained in:
Jesse Plamondon-Willard 2017-03-12 17:48:53 -04:00
parent 003a9586b2
commit a12bcf3b78
4 changed files with 86 additions and 1 deletions

View File

@ -0,0 +1,30 @@
using StardewModdingAPI.AssemblyRewriters.Framework;
namespace StardewModdingAPI.AssemblyRewriters.Finders
{
/// <summary>Base class for a type reference finder.</summary>
public class GenericTypeFinder : BaseTypeFinder
{
/*********
** Accessors
*********/
/// <summary>A brief noun phrase indicating what the instruction finder matches.</summary>
public override string NounPhrase { get; }
/// <summary>The full type name to match.</summary>
public override string FullTypeName { get; }
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="fullTypeName">The full type name to match.</param>
/// <param name="nounPhrase">A brief noun phrase indicating what the instruction finder matches.</param>
public GenericTypeFinder(string fullTypeName, string nounPhrase)
{
this.FullTypeName = fullTypeName;
this.NounPhrase = nounPhrase;
}
}
}

View File

@ -0,0 +1,52 @@
using System.Linq;
using Mono.Cecil;
using Mono.Cecil.Cil;
namespace StardewModdingAPI.AssemblyRewriters.Framework
{
/// <summary>Base class for a type reference finder.</summary>
public abstract class BaseTypeFinder : IInstructionFinder
{
/*********
** Accessors
*********/
/// <summary>A brief noun phrase indicating what the instruction finder matches.</summary>
public abstract string NounPhrase { get; }
/// <summary>The full type name to match.</summary>
public abstract string FullTypeName { get; }
/*********
** Public methods
*********/
/// <summary>Get whether a CIL instruction matches.</summary>
/// <param name="instruction">The IL instruction.</param>
/// <param name="platformChanged">Whether the mod was compiled on a different platform.</param>
public virtual bool IsMatch(Instruction instruction, bool platformChanged)
{
string fullName = this.FullTypeName;
// field reference
if (instruction.OpCode == OpCodes.Ldfld || instruction.OpCode == OpCodes.Ldsfld || instruction.OpCode == OpCodes.Stfld || instruction.OpCode == OpCodes.Stsfld)
{
FieldReference field = (FieldReference)instruction.Operand;
return
field.DeclaringType.FullName == fullName // field on target class
|| field.FieldType.FullName == fullName; // field value is target class
}
// method reference
if (instruction.OpCode == OpCodes.Call || instruction.OpCode == OpCodes.Callvirt)
{
MethodReference method = (MethodReference)instruction.Operand;
return
method.DeclaringType.FullName == fullName // method on target class
|| method.ReturnType.FullName == fullName // method returns target class
|| method.Parameters.Any(p => p.ParameterType.FullName == fullName); // method parameters
}
return false;
}
}
}

View File

@ -69,8 +69,10 @@
<Compile Include="Finders\Game1_smoothFont_FieldFinder.cs" />
<Compile Include="Finders\Game1_borderFont_FieldFinder.cs" />
<Compile Include="Finders\SMAPI_Extensions_MethodFinder.cs" />
<Compile Include="Framework\BaseTypeFinder.cs" />
<Compile Include="Framework\BaseFieldFinder.cs" />
<Compile Include="Framework\BaseMethodFinder.cs" />
<Compile Include="Finders\GenericTypeFinder.cs" />
<Compile Include="IInstructionRewriter.cs" />
<Compile Include="IInstructionFinder.cs" />
<Compile Include="Platform.cs" />

View File

@ -146,7 +146,8 @@ namespace StardewModdingAPI
new Game1_smoothFont_FieldFinder(),
// APIs removed in SMAPI 1.9
new SMAPI_Extensions_MethodFinder()
new SMAPI_Extensions_MethodFinder(),
new GenericTypeFinder("StardewModdingAPI.Inheritance.SGame", "obsolete StardewModdingAPI.SGame class")
};
}