summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI.AssemblyRewriters/Finders
diff options
context:
space:
mode:
Diffstat (limited to 'src/StardewModdingAPI.AssemblyRewriters/Finders')
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/Finders/EventFinder.cs83
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/Finders/FieldFinder.cs83
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/Finders/MethodFinder.cs83
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/Finders/PropertyFinder.cs83
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/Finders/TypeFinder.cs135
5 files changed, 0 insertions, 467 deletions
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/EventFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/EventFinder.cs
deleted file mode 100644
index c0051469..00000000
--- a/src/StardewModdingAPI.AssemblyRewriters/Finders/EventFinder.cs
+++ /dev/null
@@ -1,83 +0,0 @@
-using Mono.Cecil;
-using Mono.Cecil.Cil;
-
-namespace StardewModdingAPI.AssemblyRewriters.Finders
-{
- /// <summary>Finds incompatible CIL instructions that reference a given event and throws an <see cref="IncompatibleInstructionException"/>.</summary>
- public class EventFinder : IInstructionRewriter
- {
- /*********
- ** Properties
- *********/
- /// <summary>The full type name for which to find references.</summary>
- private readonly string FullTypeName;
-
- /// <summary>The event name for which to find references.</summary>
- private readonly string EventName;
-
-
- /*********
- ** Accessors
- *********/
- /// <summary>A brief noun phrase indicating what the instruction finder matches.</summary>
- public string NounPhrase { get; }
-
-
- /*********
- ** Public methods
- *********/
- /// <summary>Construct an instance.</summary>
- /// <param name="fullTypeName">The full type name for which to find references.</param>
- /// <param name="eventName">The event name for which to find references.</param>
- /// <param name="nounPhrase">A brief noun phrase indicating what the instruction finder matches (or <c>null</c> to generate one).</param>
- public EventFinder(string fullTypeName, string eventName, string nounPhrase = null)
- {
- this.FullTypeName = fullTypeName;
- this.EventName = eventName;
- this.NounPhrase = nounPhrase ?? $"{fullTypeName}.{eventName} event";
- }
-
- /// <summary>Rewrite a method definition for compatibility.</summary>
- /// <param name="module">The module being rewritten.</param>
- /// <param name="method">The method definition to rewrite.</param>
- /// <param name="assemblyMap">Metadata for mapping assemblies to the current platform.</param>
- /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param>
- /// <returns>Returns whether the instruction was rewritten.</returns>
- /// <exception cref="IncompatibleInstructionException">The CIL instruction is not compatible, and can't be rewritten.</exception>
- public virtual bool Rewrite(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged)
- {
- return false;
- }
-
- /// <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>
- /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param>
- /// <returns>Returns whether the instruction was rewritten.</returns>
- /// <exception cref="IncompatibleInstructionException">The CIL instruction is not compatible, and can't be rewritten.</exception>
- public virtual bool Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
- {
- if (!this.IsMatch(instruction))
- return false;
-
- throw new IncompatibleInstructionException(this.NounPhrase);
- }
-
-
- /*********
- ** Protected methods
- *********/
- /// <summary>Get whether a CIL instruction matches.</summary>
- /// <param name="instruction">The IL instruction.</param>
- protected bool IsMatch(Instruction instruction)
- {
- MethodReference methodRef = RewriteHelper.AsMethodReference(instruction);
- return
- methodRef != null
- && methodRef.DeclaringType.FullName == this.FullTypeName
- && (methodRef.Name == "add_" + this.EventName || methodRef.Name == "remove_" + this.EventName);
- }
- }
-}
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/FieldFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/FieldFinder.cs
deleted file mode 100644
index b44883e9..00000000
--- a/src/StardewModdingAPI.AssemblyRewriters/Finders/FieldFinder.cs
+++ /dev/null
@@ -1,83 +0,0 @@
-using Mono.Cecil;
-using Mono.Cecil.Cil;
-
-namespace StardewModdingAPI.AssemblyRewriters.Finders
-{
- /// <summary>Finds incompatible CIL instructions that reference a given field and throws an <see cref="IncompatibleInstructionException"/>.</summary>
- public class FieldFinder : IInstructionRewriter
- {
- /*********
- ** Properties
- *********/
- /// <summary>The full type name for which to find references.</summary>
- private readonly string FullTypeName;
-
- /// <summary>The field name for which to find references.</summary>
- private readonly string FieldName;
-
-
- /*********
- ** Accessors
- *********/
- /// <summary>A brief noun phrase indicating what the instruction finder matches.</summary>
- public string NounPhrase { get; }
-
-
- /*********
- ** Public methods
- *********/
- /// <summary>Construct an instance.</summary>
- /// <param name="fullTypeName">The full type name for which to find references.</param>
- /// <param name="fieldName">The field name for which to find references.</param>
- /// <param name="nounPhrase">A brief noun phrase indicating what the instruction finder matches (or <c>null</c> to generate one).</param>
- public FieldFinder(string fullTypeName, string fieldName, string nounPhrase = null)
- {
- this.FullTypeName = fullTypeName;
- this.FieldName = fieldName;
- this.NounPhrase = nounPhrase ?? $"{fullTypeName}.{fieldName} field";
- }
-
- /// <summary>Rewrite a method definition for compatibility.</summary>
- /// <param name="module">The module being rewritten.</param>
- /// <param name="method">The method definition to rewrite.</param>
- /// <param name="assemblyMap">Metadata for mapping assemblies to the current platform.</param>
- /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param>
- /// <returns>Returns whether the instruction was rewritten.</returns>
- /// <exception cref="IncompatibleInstructionException">The CIL instruction is not compatible, and can't be rewritten.</exception>
- public virtual bool Rewrite(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged)
- {
- return false;
- }
-
- /// <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>
- /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param>
- /// <returns>Returns whether the instruction was rewritten.</returns>
- /// <exception cref="IncompatibleInstructionException">The CIL instruction is not compatible, and can't be rewritten.</exception>
- public virtual bool Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
- {
- if (!this.IsMatch(instruction))
- return false;
-
- throw new IncompatibleInstructionException(this.NounPhrase);
- }
-
-
- /*********
- ** Protected methods
- *********/
- /// <summary>Get whether a CIL instruction matches.</summary>
- /// <param name="instruction">The IL instruction.</param>
- protected bool IsMatch(Instruction instruction)
- {
- FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction);
- return
- fieldRef != null
- && fieldRef.DeclaringType.FullName == this.FullTypeName
- && fieldRef.Name == this.FieldName;
- }
- }
-}
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/MethodFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/MethodFinder.cs
deleted file mode 100644
index 19dda58a..00000000
--- a/src/StardewModdingAPI.AssemblyRewriters/Finders/MethodFinder.cs
+++ /dev/null
@@ -1,83 +0,0 @@
-using Mono.Cecil;
-using Mono.Cecil.Cil;
-
-namespace StardewModdingAPI.AssemblyRewriters.Finders
-{
- /// <summary>Finds incompatible CIL instructions that reference a given method and throws an <see cref="IncompatibleInstructionException"/>.</summary>
- public class MethodFinder : IInstructionRewriter
- {
- /*********
- ** Properties
- *********/
- /// <summary>The full type name for which to find references.</summary>
- private readonly string FullTypeName;
-
- /// <summary>The method name for which to find references.</summary>
- private readonly string MethodName;
-
-
- /*********
- ** Accessors
- *********/
- /// <summary>A brief noun phrase indicating what the instruction finder matches.</summary>
- public string NounPhrase { get; }
-
-
- /*********
- ** Public methods
- *********/
- /// <summary>Construct an instance.</summary>
- /// <param name="fullTypeName">The full type name for which to find references.</param>
- /// <param name="methodName">The method name for which to find references.</param>
- /// <param name="nounPhrase">A brief noun phrase indicating what the instruction finder matches (or <c>null</c> to generate one).</param>
- public MethodFinder(string fullTypeName, string methodName, string nounPhrase = null)
- {
- this.FullTypeName = fullTypeName;
- this.MethodName = methodName;
- this.NounPhrase = nounPhrase ?? $"{fullTypeName}.{methodName} method";
- }
-
- /// <summary>Rewrite a method definition for compatibility.</summary>
- /// <param name="module">The module being rewritten.</param>
- /// <param name="method">The method definition to rewrite.</param>
- /// <param name="assemblyMap">Metadata for mapping assemblies to the current platform.</param>
- /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param>
- /// <returns>Returns whether the instruction was rewritten.</returns>
- /// <exception cref="IncompatibleInstructionException">The CIL instruction is not compatible, and can't be rewritten.</exception>
- public virtual bool Rewrite(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged)
- {
- return false;
- }
-
- /// <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>
- /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param>
- /// <returns>Returns whether the instruction was rewritten.</returns>
- /// <exception cref="IncompatibleInstructionException">The CIL instruction is not compatible, and can't be rewritten.</exception>
- public bool Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
- {
- if (!this.IsMatch(instruction))
- return false;
-
- throw new IncompatibleInstructionException(this.NounPhrase);
- }
-
-
- /*********
- ** Protected methods
- *********/
- /// <summary>Get whether a CIL instruction matches.</summary>
- /// <param name="instruction">The IL instruction.</param>
- protected bool IsMatch(Instruction instruction)
- {
- MethodReference methodRef = RewriteHelper.AsMethodReference(instruction);
- return
- methodRef != null
- && methodRef.DeclaringType.FullName == this.FullTypeName
- && methodRef.Name == this.MethodName;
- }
- }
-}
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/PropertyFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/PropertyFinder.cs
deleted file mode 100644
index 441f15f2..00000000
--- a/src/StardewModdingAPI.AssemblyRewriters/Finders/PropertyFinder.cs
+++ /dev/null
@@ -1,83 +0,0 @@
-using Mono.Cecil;
-using Mono.Cecil.Cil;
-
-namespace StardewModdingAPI.AssemblyRewriters.Finders
-{
- /// <summary>Finds incompatible CIL instructions that reference a given property and throws an <see cref="IncompatibleInstructionException"/>.</summary>
- public class PropertyFinder : IInstructionRewriter
- {
- /*********
- ** Properties
- *********/
- /// <summary>The full type name for which to find references.</summary>
- private readonly string FullTypeName;
-
- /// <summary>The property name for which to find references.</summary>
- private readonly string PropertyName;
-
-
- /*********
- ** Accessors
- *********/
- /// <summary>A brief noun phrase indicating what the instruction finder matches.</summary>
- public string NounPhrase { get; }
-
-
- /*********
- ** Public methods
- *********/
- /// <summary>Construct an instance.</summary>
- /// <param name="fullTypeName">The full type name for which to find references.</param>
- /// <param name="propertyName">The property name for which to find references.</param>
- /// <param name="nounPhrase">A brief noun phrase indicating what the instruction finder matches (or <c>null</c> to generate one).</param>
- public PropertyFinder(string fullTypeName, string propertyName, string nounPhrase = null)
- {
- this.FullTypeName = fullTypeName;
- this.PropertyName = propertyName;
- this.NounPhrase = nounPhrase ?? $"{fullTypeName}.{propertyName} property";
- }
-
- /// <summary>Rewrite a method definition for compatibility.</summary>
- /// <param name="module">The module being rewritten.</param>
- /// <param name="method">The method definition to rewrite.</param>
- /// <param name="assemblyMap">Metadata for mapping assemblies to the current platform.</param>
- /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param>
- /// <returns>Returns whether the instruction was rewritten.</returns>
- /// <exception cref="IncompatibleInstructionException">The CIL instruction is not compatible, and can't be rewritten.</exception>
- public virtual bool Rewrite(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged)
- {
- return false;
- }
-
- /// <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>
- /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param>
- /// <returns>Returns whether the instruction was rewritten.</returns>
- /// <exception cref="IncompatibleInstructionException">The CIL instruction is not compatible, and can't be rewritten.</exception>
- public virtual bool Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
- {
- if (!this.IsMatch(instruction))
- return false;
-
- throw new IncompatibleInstructionException(this.NounPhrase);
- }
-
-
- /*********
- ** Protected methods
- *********/
- /// <summary>Get whether a CIL instruction matches.</summary>
- /// <param name="instruction">The IL instruction.</param>
- protected bool IsMatch(Instruction instruction)
- {
- MethodReference methodRef = RewriteHelper.AsMethodReference(instruction);
- return
- methodRef != null
- && methodRef.DeclaringType.FullName == this.FullTypeName
- && (methodRef.Name == "get_" + this.PropertyName || methodRef.Name == "set_" + this.PropertyName);
- }
- }
-}
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/TypeFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/TypeFinder.cs
deleted file mode 100644
index 0560e38e..00000000
--- a/src/StardewModdingAPI.AssemblyRewriters/Finders/TypeFinder.cs
+++ /dev/null
@@ -1,135 +0,0 @@
-using System.Linq;
-using Mono.Cecil;
-using Mono.Cecil.Cil;
-
-namespace StardewModdingAPI.AssemblyRewriters.Finders
-{
- /// <summary>Finds incompatible CIL instructions that reference a given type and throws an <see cref="IncompatibleInstructionException"/>.</summary>
- public class TypeFinder : IInstructionRewriter
- {
- /*********
- ** Accessors
- *********/
- /// <summary>The full type name for which to find references.</summary>
- private readonly string FullTypeName;
-
-
- /*********
- ** Accessors
- *********/
- /// <summary>A brief noun phrase indicating what the instruction finder matches.</summary>
- public string NounPhrase { 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 (or <c>null</c> to generate one).</param>
- public TypeFinder(string fullTypeName, string nounPhrase = null)
- {
- this.FullTypeName = fullTypeName;
- this.NounPhrase = nounPhrase ?? $"{fullTypeName} type";
- }
-
- /// <summary>Rewrite a method definition for compatibility.</summary>
- /// <param name="module">The module being rewritten.</param>
- /// <param name="method">The method definition to rewrite.</param>
- /// <param name="assemblyMap">Metadata for mapping assemblies to the current platform.</param>
- /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param>
- /// <returns>Returns whether the instruction was rewritten.</returns>
- /// <exception cref="IncompatibleInstructionException">The CIL instruction is not compatible, and can't be rewritten.</exception>
- public virtual bool Rewrite(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged)
- {
- if (!this.IsMatch(method))
- return false;
-
- throw new IncompatibleInstructionException(this.NounPhrase);
- }
-
- /// <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>
- /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param>
- /// <returns>Returns whether the instruction was rewritten.</returns>
- /// <exception cref="IncompatibleInstructionException">The CIL instruction is not compatible, and can't be rewritten.</exception>
- public virtual bool Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
- {
- if (!this.IsMatch(instruction))
- return false;
-
- throw new IncompatibleInstructionException(this.NounPhrase);
- }
-
-
- /*********
- ** Protected methods
- *********/
- /// <summary>Get whether a CIL instruction matches.</summary>
- /// <param name="method">The method deifnition.</param>
- protected bool IsMatch(MethodDefinition method)
- {
- if (this.IsMatch(method.ReturnType))
- return true;
-
- foreach (VariableDefinition variable in method.Body.Variables)
- {
- if (this.IsMatch(variable.VariableType))
- return true;
- }
-
- return false;
- }
-
- /// <summary>Get whether a CIL instruction matches.</summary>
- /// <param name="instruction">The IL instruction.</param>
- protected bool IsMatch(Instruction instruction)
- {
- // field reference
- FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction);
- if (fieldRef != null)
- {
- return
- this.IsMatch(fieldRef.DeclaringType) // field on target class
- || this.IsMatch(fieldRef.FieldType); // field value is target class
- }
-
- // method reference
- MethodReference methodRef = RewriteHelper.AsMethodReference(instruction);
- if (methodRef != null)
- {
- return
- this.IsMatch(methodRef.DeclaringType) // method on target class
- || this.IsMatch(methodRef.ReturnType) // method returns target class
- || methodRef.Parameters.Any(p => this.IsMatch(p.ParameterType)); // method parameters
- }
-
- return false;
- }
-
- /// <summary>Get whether a type reference matches the expected type.</summary>
- /// <param name="type">The type to check.</param>
- protected bool IsMatch(TypeReference type)
- {
- // root type
- if (type.FullName == this.FullTypeName)
- return true;
-
- // generic arguments
- if (type is GenericInstanceType genericType)
- {
- if (genericType.GenericArguments.Any(this.IsMatch))
- return true;
- }
-
- // generic parameters (e.g. constraints)
- if (type.GenericParameters.Any(this.IsMatch))
- return true;
-
- return false;
- }
- }
-}