diff options
Diffstat (limited to 'src')
4 files changed, 86 insertions, 1 deletions
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/GenericTypeFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/GenericTypeFinder.cs new file mode 100644 index 00000000..11ffa734 --- /dev/null +++ b/src/StardewModdingAPI.AssemblyRewriters/Finders/GenericTypeFinder.cs @@ -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; + } + } +} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseTypeFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseTypeFinder.cs new file mode 100644 index 00000000..5a768794 --- /dev/null +++ b/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseTypeFinder.cs @@ -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; + } + } +} diff --git a/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj b/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj index 3f437ca0..d489eb34 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj +++ b/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj @@ -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" /> diff --git a/src/StardewModdingAPI/Constants.cs b/src/StardewModdingAPI/Constants.cs index 563cdd52..1e1ca325 100644 --- a/src/StardewModdingAPI/Constants.cs +++ b/src/StardewModdingAPI/Constants.cs @@ -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") }; } |