summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI.AssemblyRewriters
diff options
context:
space:
mode:
Diffstat (limited to 'src/StardewModdingAPI.AssemblyRewriters')
-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/TypeFinder.cs135
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/IInstructionRewriter.cs38
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/IMethodRewriter.cs21
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/IncompatibleInstructionException.cs35
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/RewriteHelper.cs (renamed from src/StardewModdingAPI.AssemblyRewriters/Rewriters/BaseMethodRewriter.cs)98
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldReplaceRewriter.cs53
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldToPropertyRewriter.cs54
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/Rewriters/MethodParentRewriter.cs93
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/Rewriters/SpriteBatchRewriter.cs30
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/Rewriters/TypeReferenceRewriter.cs157
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/Rewriters/Wrappers/SpriteBatchWrapper.cs (renamed from src/StardewModdingAPI.AssemblyRewriters/Wrappers/CompatibleSpriteBatch.cs)17
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj20
15 files changed, 888 insertions, 112 deletions
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/EventFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/EventFinder.cs
new file mode 100644
index 00000000..c0051469
--- /dev/null
+++ b/src/StardewModdingAPI.AssemblyRewriters/Finders/EventFinder.cs
@@ -0,0 +1,83 @@
+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
new file mode 100644
index 00000000..b44883e9
--- /dev/null
+++ b/src/StardewModdingAPI.AssemblyRewriters/Finders/FieldFinder.cs
@@ -0,0 +1,83 @@
+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
new file mode 100644
index 00000000..19dda58a
--- /dev/null
+++ b/src/StardewModdingAPI.AssemblyRewriters/Finders/MethodFinder.cs
@@ -0,0 +1,83 @@
+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/TypeFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/TypeFinder.cs
new file mode 100644
index 00000000..0560e38e
--- /dev/null
+++ b/src/StardewModdingAPI.AssemblyRewriters/Finders/TypeFinder.cs
@@ -0,0 +1,135 @@
+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;
+ }
+ }
+}
diff --git a/src/StardewModdingAPI.AssemblyRewriters/IInstructionRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/IInstructionRewriter.cs
new file mode 100644
index 00000000..2f16b23d
--- /dev/null
+++ b/src/StardewModdingAPI.AssemblyRewriters/IInstructionRewriter.cs
@@ -0,0 +1,38 @@
+using Mono.Cecil;
+using Mono.Cecil.Cil;
+
+namespace StardewModdingAPI.AssemblyRewriters
+{
+ /// <summary>Rewrites CIL instructions for compatibility.</summary>
+ public interface IInstructionRewriter
+ {
+ /*********
+ ** Accessors
+ *********/
+ /// <summary>A brief noun phrase indicating what the rewriter matches.</summary>
+ string NounPhrase { get; }
+
+
+ /*********
+ ** Methods
+ *********/
+ /// <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>
+ bool Rewrite(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged);
+
+ /// <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>
+ bool Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged);
+ }
+}
diff --git a/src/StardewModdingAPI.AssemblyRewriters/IMethodRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/IMethodRewriter.cs
deleted file mode 100644
index 5cbb7e0d..00000000
--- a/src/StardewModdingAPI.AssemblyRewriters/IMethodRewriter.cs
+++ /dev/null
@@ -1,21 +0,0 @@
-using Mono.Cecil;
-using Mono.Cecil.Cil;
-
-namespace StardewModdingAPI.AssemblyRewriters
-{
- /// <summary>Rewrites a method for compatibility.</summary>
- public interface IMethodRewriter
- {
- /// <summary>Get whether the given method reference can be rewritten.</summary>
- /// <param name="methodRef">The method reference.</param>
- bool ShouldRewrite(MethodReference methodRef);
-
- /// <summary>Rewrite a method for compatibility.</summary>
- /// <param name="module">The module being rewritten.</param>
- /// <param name="cil">The CIL rewriter.</param>
- /// <param name="callOp">The instruction which calls the method.</param>
- /// <param name="methodRef">The method reference invoked by the <paramref name="callOp"/>.</param>
- /// <param name="assemblyMap">Metadata for mapping assemblies to the current platform.</param>
- void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction callOp, MethodReference methodRef, PlatformAssemblyMap assemblyMap);
- }
-}
diff --git a/src/StardewModdingAPI.AssemblyRewriters/IncompatibleInstructionException.cs b/src/StardewModdingAPI.AssemblyRewriters/IncompatibleInstructionException.cs
new file mode 100644
index 00000000..f7e6bd8f
--- /dev/null
+++ b/src/StardewModdingAPI.AssemblyRewriters/IncompatibleInstructionException.cs
@@ -0,0 +1,35 @@
+using System;
+
+namespace StardewModdingAPI.AssemblyRewriters
+{
+ /// <summary>An exception raised when an incompatible instruction is found while loading a mod assembly.</summary>
+ public class IncompatibleInstructionException : Exception
+ {
+ /*********
+ ** Accessors
+ *********/
+ /// <summary>A brief noun phrase which describes the incompatible instruction that was found.</summary>
+ public string NounPhrase { get; }
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="nounPhrase">A brief noun phrase which describes the incompatible instruction that was found.</param>
+ public IncompatibleInstructionException(string nounPhrase)
+ : base($"Found an incompatible CIL instruction ({nounPhrase}).")
+ {
+ this.NounPhrase = nounPhrase;
+ }
+
+ /// <summary>Construct an instance.</summary>
+ /// <param name="nounPhrase">A brief noun phrase which describes the incompatible instruction that was found.</param>
+ /// <param name="message">A message which describes the error.</param>
+ public IncompatibleInstructionException(string nounPhrase, string message)
+ : base(message)
+ {
+ this.NounPhrase = nounPhrase;
+ }
+ }
+}
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/BaseMethodRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/RewriteHelper.cs
index 1af6e6c4..cfb330dd 100644
--- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/BaseMethodRewriter.cs
+++ b/src/StardewModdingAPI.AssemblyRewriters/RewriteHelper.cs
@@ -4,66 +4,36 @@ using System.Reflection;
using Mono.Cecil;
using Mono.Cecil.Cil;
-namespace StardewModdingAPI.AssemblyRewriters.Rewriters
+namespace StardewModdingAPI.AssemblyRewriters
{
- /// <summary>Base class for a method rewriter.</summary>
- public abstract class BaseMethodRewriter : IMethodRewriter
+ /// <summary>Provides helper methods for field rewriters.</summary>
+ internal static class RewriteHelper
{
/*********
** Public methods
*********/
- /// <summary>Get whether the given method reference can be rewritten.</summary>
- /// <param name="methodRef">The method reference.</param>
- public abstract bool ShouldRewrite(MethodReference methodRef);
-
- /// <summary>Rewrite a method for compatibility.</summary>
- /// <param name="module">The module being rewritten.</param>
- /// <param name="cil">The CIL rewriter.</param>
- /// <param name="callOp">The instruction which calls the method.</param>
- /// <param name="methodRef">The method reference invoked by the <paramref name="callOp"/>.</param>
- /// <param name="assemblyMap">Metadata for mapping assemblies to the current platform.</param>
- public abstract void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction callOp, MethodReference methodRef, PlatformAssemblyMap assemblyMap);
-
-
- /*********
- ** Protected methods
- *********/
- /// <summary>Get whether a method definition matches the signature expected by a method reference.</summary>
- /// <param name="definition">The method definition.</param>
- /// <param name="reference">The method reference.</param>
- protected bool HasMatchingSignature(MethodInfo definition, MethodReference reference)
+ /// <summary>Get the field reference from an instruction if it matches.</summary>
+ /// <param name="instruction">The IL instruction.</param>
+ public static FieldReference AsFieldReference(Instruction instruction)
{
- // same name
- if (definition.Name != reference.Name)
- return false;
-
- // same arguments
- ParameterInfo[] definitionParameters = definition.GetParameters();
- ParameterDefinition[] referenceParameters = reference.Parameters.ToArray();
- if (referenceParameters.Length != definitionParameters.Length)
- return false;
- for (int i = 0; i < referenceParameters.Length; i++)
- {
- if (!this.IsMatchingType(definitionParameters[i].ParameterType, referenceParameters[i].ParameterType))
- return false;
- }
- return true;
+ return instruction.OpCode == OpCodes.Ldfld || instruction.OpCode == OpCodes.Ldsfld || instruction.OpCode == OpCodes.Stfld || instruction.OpCode == OpCodes.Stsfld
+ ? (FieldReference)instruction.Operand
+ : null;
}
- /// <summary>Get whether a type has a method whose signature matches the one expected by a method reference.</summary>
- /// <param name="type">The type to check.</param>
- /// <param name="reference">The method reference.</param>
- protected bool HasMatchingSignature(Type type, MethodReference reference)
+ /// <summary>Get the method reference from an instruction if it matches.</summary>
+ /// <param name="instruction">The IL instruction.</param>
+ public static MethodReference AsMethodReference(Instruction instruction)
{
- return type
- .GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public)
- .Any(method => this.HasMatchingSignature(method, reference));
+ return instruction.OpCode == OpCodes.Call || instruction.OpCode == OpCodes.Callvirt
+ ? (MethodReference)instruction.Operand
+ : null;
}
/// <summary>Get whether a type matches a type reference.</summary>
/// <param name="type">The defined type.</param>
/// <param name="reference">The type reference.</param>
- private bool IsMatchingType(Type type, TypeReference reference)
+ public static bool IsSameType(Type type, TypeReference reference)
{
// same namespace & name
if (type.Namespace != reference.Namespace || type.Name != reference.Name)
@@ -81,12 +51,44 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters
return false;
for (int i = 0; i < defGenerics.Length; i++)
{
- if (!this.IsMatchingType(defGenerics[i], refGenerics[i]))
+ if (!RewriteHelper.IsSameType(defGenerics[i], refGenerics[i]))
return false;
}
}
return true;
}
+
+ /// <summary>Get whether a method definition matches the signature expected by a method reference.</summary>
+ /// <param name="definition">The method definition.</param>
+ /// <param name="reference">The method reference.</param>
+ public static bool HasMatchingSignature(MethodInfo definition, MethodReference reference)
+ {
+ // same name
+ if (definition.Name != reference.Name)
+ return false;
+
+ // same arguments
+ ParameterInfo[] definitionParameters = definition.GetParameters();
+ ParameterDefinition[] referenceParameters = reference.Parameters.ToArray();
+ if (referenceParameters.Length != definitionParameters.Length)
+ return false;
+ for (int i = 0; i < referenceParameters.Length; i++)
+ {
+ if (!RewriteHelper.IsSameType(definitionParameters[i].ParameterType, referenceParameters[i].ParameterType))
+ return false;
+ }
+ return true;
+ }
+
+ /// <summary>Get whether a type has a method whose signature matches the one expected by a method reference.</summary>
+ /// <param name="type">The type to check.</param>
+ /// <param name="reference">The method reference.</param>
+ public static bool HasMatchingSignature(Type type, MethodReference reference)
+ {
+ return type
+ .GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public)
+ .Any(method => RewriteHelper.HasMatchingSignature(method, reference));
+ }
}
-} \ No newline at end of file
+}
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldReplaceRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldReplaceRewriter.cs
new file mode 100644
index 00000000..73844073
--- /dev/null
+++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldReplaceRewriter.cs
@@ -0,0 +1,53 @@
+using System;
+using System.Reflection;
+using Mono.Cecil;
+using Mono.Cecil.Cil;
+using StardewModdingAPI.AssemblyRewriters.Finders;
+
+namespace StardewModdingAPI.AssemblyRewriters.Rewriters
+{
+ /// <summary>Rewrites references to one field with another.</summary>
+ public class FieldReplaceRewriter : FieldFinder
+ {
+ /*********
+ ** Properties
+ *********/
+ /// <summary>The new field to reference.</summary>
+ private readonly FieldInfo ToField;
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="type">The type whose field to which references should be rewritten.</param>
+ /// <param name="fromFieldName">The field name to rewrite.</param>
+ /// <param name="toFieldName">The new field name to reference.</param>
+ /// <param name="nounPhrase">A brief noun phrase indicating what the instruction finder matches (or <c>null</c> to generate one).</param>
+ public FieldReplaceRewriter(Type type, string fromFieldName, string toFieldName, string nounPhrase = null)
+ : base(type.FullName, fromFieldName, nounPhrase)
+ {
+ this.ToField = type.GetField(toFieldName);
+ if (this.ToField == null)
+ throw new InvalidOperationException($"The {type.FullName} class doesn't have a {toFieldName} field.");
+ }
+
+ /// <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 override bool Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
+ {
+ if (!this.IsMatch(instruction))
+ return false;
+
+ FieldReference newRef = module.Import(this.ToField);
+ cil.Replace(instruction, cil.Create(instruction.OpCode, newRef));
+ return true;
+ }
+ }
+}
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldToPropertyRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldToPropertyRewriter.cs
new file mode 100644
index 00000000..3f57042d
--- /dev/null
+++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldToPropertyRewriter.cs
@@ -0,0 +1,54 @@
+using System;
+using Mono.Cecil;
+using Mono.Cecil.Cil;
+using StardewModdingAPI.AssemblyRewriters.Finders;
+
+namespace StardewModdingAPI.AssemblyRewriters.Rewriters
+{
+ /// <summary>Rewrites field references into property references.</summary>
+ public class FieldToPropertyRewriter : FieldFinder
+ {
+ /*********
+ ** Properties
+ *********/
+ /// <summary>The type whose field to which references should be rewritten.</summary>
+ private readonly Type Type;
+
+ /// <summary>The field name to rewrite.</summary>
+ private readonly string FieldName;
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="type">The type whose field to which references should be rewritten.</param>
+ /// <param name="fieldName">The field name to rewrite.</param>
+ /// <param name="nounPhrase">A brief noun phrase indicating what the instruction finder matches (or <c>null</c> to generate one).</param>
+ public FieldToPropertyRewriter(Type type, string fieldName, string nounPhrase = null)
+ : base(type.FullName, fieldName, nounPhrase)
+ {
+ this.Type = type;
+ this.FieldName = fieldName;
+ }
+
+ /// <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 override bool Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
+ {
+ if (!this.IsMatch(instruction))
+ return false;
+
+ string methodPrefix = instruction.OpCode == OpCodes.Ldsfld || instruction.OpCode == OpCodes.Ldfld ? "get" : "set";
+ MethodReference propertyRef = module.Import(this.Type.GetMethod($"{methodPrefix}_{this.FieldName}"));
+ cil.Replace(instruction, cil.Create(OpCodes.Call, propertyRef));
+ return true;
+ }
+ }
+}
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/MethodParentRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/MethodParentRewriter.cs
new file mode 100644
index 00000000..035ef211
--- /dev/null
+++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/MethodParentRewriter.cs
@@ -0,0 +1,93 @@
+using System;
+using Mono.Cecil;
+using Mono.Cecil.Cil;
+
+namespace StardewModdingAPI.AssemblyRewriters.Rewriters
+{
+ /// <summary>Rewrites method references from one parent type to another if the signatures match.</summary>
+ public class MethodParentRewriter : IInstructionRewriter
+ {
+ /*********
+ ** Properties
+ *********/
+ /// <summary>The type whose methods to remap.</summary>
+ private readonly Type FromType;
+
+ /// <summary>The type with methods to map to.</summary>
+ private readonly Type ToType;
+
+ /// <summary>Whether to only rewrite references if loading the assembly on a different platform than it was compiled on.</summary>
+ private readonly bool OnlyIfPlatformChanged;
+
+
+ /*********
+ ** 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="fromType">The type whose methods to remap.</param>
+ /// <param name="toType">The type with methods to map to.</param>
+ /// <param name="onlyIfPlatformChanged">Whether to only rewrite references if loading the assembly on a different platform than it was compiled on.</param>
+ /// <param name="nounPhrase">A brief noun phrase indicating what the instruction finder matches (or <c>null</c> to generate one).</param>
+ public MethodParentRewriter(Type fromType, Type toType, bool onlyIfPlatformChanged = false, string nounPhrase = null)
+ {
+ this.FromType = fromType;
+ this.ToType = toType;
+ this.NounPhrase = nounPhrase ?? $"{fromType.Name} methods";
+ this.OnlyIfPlatformChanged = onlyIfPlatformChanged;
+ }
+
+ /// <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 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, platformChanged))
+ return false;
+
+ MethodReference methodRef = (MethodReference)instruction.Operand;
+ methodRef.DeclaringType = module.Import(this.ToType);
+ return true;
+ }
+
+
+ /*********
+ ** Protected 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>
+ protected bool IsMatch(Instruction instruction, bool platformChanged)
+ {
+ MethodReference methodRef = RewriteHelper.AsMethodReference(instruction);
+ return
+ methodRef != null
+ && (platformChanged || !this.OnlyIfPlatformChanged)
+ && methodRef.DeclaringType.FullName == this.FromType.FullName
+ && RewriteHelper.HasMatchingSignature(this.ToType, methodRef);
+ }
+ }
+}
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SpriteBatchRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SpriteBatchRewriter.cs
deleted file mode 100644
index 1c0a5cf3..00000000
--- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SpriteBatchRewriter.cs
+++ /dev/null
@@ -1,30 +0,0 @@
-using Microsoft.Xna.Framework.Graphics;
-using Mono.Cecil;
-using Mono.Cecil.Cil;
-using StardewModdingAPI.AssemblyRewriters.Wrappers;
-
-namespace StardewModdingAPI.AssemblyRewriters.Rewriters
-{
- /// <summary>Rewrites references to <see cref="SpriteBatch"/> to fix inconsistent method signatures between MonoGame and XNA.</summary>
- /// <remarks>MonoGame has one <c>SpriteBatch.Begin</c> method with optional arguments, but XNA has multiple method overloads. Incompatible method references are rewritten to use <see cref="CompatibleSpriteBatch"/>, which redirects all method signatures to the proper compiled MonoGame/XNA method.</remarks>
- public class SpriteBatchRewriter : BaseMethodRewriter
- {
- /// <summary>Get whether the given method reference can be rewritten.</summary>
- /// <param name="methodRef">The method reference.</param>
- public override bool ShouldRewrite(MethodReference methodRef)
- {
- return methodRef.DeclaringType.FullName == typeof(SpriteBatch).FullName && this.HasMatchingSignature(typeof(CompatibleSpriteBatch), methodRef);
- }
-
- /// <summary>Rewrite a method for compatibility.</summary>
- /// <param name="module">The module being rewritten.</param>
- /// <param name="cil">The CIL rewriter.</param>
- /// <param name="callOp">The instruction which calls the method.</param>
- /// <param name="methodRef">The method reference invoked by the <paramref name="callOp"/>.</param>
- /// <param name="assemblyMap">Metadata for mapping assemblies to the current platform.</param>
- public override void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction callOp, MethodReference methodRef, PlatformAssemblyMap assemblyMap)
- {
- methodRef.DeclaringType = module.Import(typeof(CompatibleSpriteBatch));
- }
- }
-} \ No newline at end of file
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/TypeReferenceRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/TypeReferenceRewriter.cs
new file mode 100644
index 00000000..da6d9bc9
--- /dev/null
+++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/TypeReferenceRewriter.cs
@@ -0,0 +1,157 @@
+using System;
+using Mono.Cecil;
+using Mono.Cecil.Cil;
+using StardewModdingAPI.AssemblyRewriters.Finders;
+
+namespace StardewModdingAPI.AssemblyRewriters.Rewriters
+{
+ /// <summary>Rewrites all references to a type.</summary>
+ public class TypeReferenceRewriter : TypeFinder
+ {
+ /*********
+ ** Properties
+ *********/
+ /// <summary>The full type name to which to find references.</summary>
+ private readonly string FromTypeName;
+
+ /// <summary>The new type to reference.</summary>
+ private readonly Type ToType;
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="fromTypeFullName">The full type name to which to find references.</param>
+ /// <param name="toType">The new type to reference.</param>
+ /// <param name="nounPhrase">A brief noun phrase indicating what the instruction finder matches (or <c>null</c> to generate one).</param>
+ public TypeReferenceRewriter(string fromTypeFullName, Type toType, string nounPhrase = null)
+ : base(fromTypeFullName, nounPhrase)
+ {
+ this.FromTypeName = fromTypeFullName;
+ this.ToType = toType;
+ }
+
+ /// <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 override bool Rewrite(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged)
+ {
+ bool rewritten = false;
+
+ // return type
+ if (this.IsMatch(method.ReturnType))
+ {
+ method.ReturnType = this.RewriteIfNeeded(module, method.ReturnType);
+ rewritten = true;
+ }
+
+ // parameters
+ foreach (ParameterDefinition parameter in method.Parameters)
+ {
+ if (this.IsMatch(parameter.ParameterType))
+ {
+ parameter.ParameterType = this.RewriteIfNeeded(module, parameter.ParameterType);
+ rewritten = true;
+ }
+ }
+
+ // generic parameters
+ for (int i = 0; i < method.GenericParameters.Count; i++)
+ {
+ var parameter = method.GenericParameters[i];
+ if (this.IsMatch(parameter))
+ {
+ TypeReference newType = this.RewriteIfNeeded(module, parameter);
+ if (newType != parameter)
+ method.GenericParameters[i] = new GenericParameter(parameter.Name, newType);
+ rewritten = true;
+ }
+ }
+
+ // local variables
+ foreach (VariableDefinition variable in method.Body.Variables)
+ {
+ if (this.IsMatch(variable.VariableType))
+ {
+ variable.VariableType = this.RewriteIfNeeded(module, variable.VariableType);
+ rewritten = true;
+ }
+ }
+
+ return rewritten;
+ }
+
+ /// <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 override bool Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
+ {
+ if (!this.IsMatch(instruction) && !instruction.ToString().Contains(this.FromTypeName))
+ return false;
+
+ // field reference
+ FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction);
+ if (fieldRef != null)
+ {
+ fieldRef.DeclaringType = this.RewriteIfNeeded(module, fieldRef.DeclaringType);
+ fieldRef.FieldType = this.RewriteIfNeeded(module, fieldRef.FieldType);
+ }
+
+ // method reference
+ MethodReference methodRef = RewriteHelper.AsMethodReference(instruction);
+ if (methodRef != null)
+ {
+ methodRef.DeclaringType = this.RewriteIfNeeded(module, methodRef.DeclaringType);
+ methodRef.ReturnType = this.RewriteIfNeeded(module, methodRef.ReturnType);
+ foreach (var parameter in methodRef.Parameters)
+ parameter.ParameterType = this.RewriteIfNeeded(module, parameter.ParameterType);
+ }
+
+ // type reference
+ if (instruction.Operand is TypeReference typeRef)
+ {
+ TypeReference newRef = this.RewriteIfNeeded(module, typeRef);
+ if (typeRef != newRef)
+ cil.Replace(instruction, cil.Create(instruction.OpCode, newRef));
+ }
+
+ return true;
+ }
+
+ /*********
+ ** Private methods
+ *********/
+ /// <summary>Get the adjusted type reference if it matches, else the same value.</summary>
+ /// <param name="module">The module being rewritten.</param>
+ /// <param name="type">The type to replace if it matches.</param>
+ private TypeReference RewriteIfNeeded(ModuleDefinition module, TypeReference type)
+ {
+ // root type
+ if (type.FullName == this.FromTypeName)
+ return module.Import(this.ToType);
+
+ // generic arguments
+ if (type is GenericInstanceType genericType)
+ {
+ for (int i = 0; i < genericType.GenericArguments.Count; i++)
+ genericType.GenericArguments[i] = this.RewriteIfNeeded(module, genericType.GenericArguments[i]);
+ }
+
+ // generic parameters (e.g. constraints)
+ for (int i = 0; i < type.GenericParameters.Count; i++)
+ type.GenericParameters[i] = new GenericParameter(this.RewriteIfNeeded(module, type.GenericParameters[i]));
+
+ return type;
+ }
+ }
+}
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Wrappers/CompatibleSpriteBatch.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/Wrappers/SpriteBatchWrapper.cs
index e28d1a68..ee68f1d5 100644
--- a/src/StardewModdingAPI.AssemblyRewriters/Wrappers/CompatibleSpriteBatch.cs
+++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/Wrappers/SpriteBatchWrapper.cs
@@ -1,21 +1,23 @@
-using Microsoft.Xna.Framework;
+using System.Diagnostics.CodeAnalysis;
+using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
-#pragma warning disable CS0109 // Member does not hide an inherited member; new keyword is not required
-namespace StardewModdingAPI.AssemblyRewriters.Wrappers
+namespace StardewModdingAPI.AssemblyRewriters.Rewriters.Wrappers
{
/// <summary>Wraps <see cref="SpriteBatch"/> methods that are incompatible when converting compiled code between MonoGame and XNA.</summary>
- public class CompatibleSpriteBatch : SpriteBatch
+ public class SpriteBatchWrapper : SpriteBatch
{
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
- public CompatibleSpriteBatch(GraphicsDevice graphicsDevice) : base(graphicsDevice) { }
+ public SpriteBatchWrapper(GraphicsDevice graphicsDevice) : base(graphicsDevice) { }
+
/****
** MonoGame signatures
****/
+ [SuppressMessage("ReSharper", "CS0109", Justification = "The 'new' modifier applies when compiled on Linux/Mac.")]
public new void Begin(SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, RasterizerState rasterizerState, Effect effect, Matrix? matrix)
{
base.Begin(sortMode, blendState, samplerState, depthStencilState, rasterizerState, effect, matrix ?? Matrix.Identity);
@@ -24,26 +26,31 @@ namespace StardewModdingAPI.AssemblyRewriters.Wrappers
/****
** XNA signatures
****/
+ [SuppressMessage("ReSharper", "CS0109", Justification = "The 'new' modifier applies when compiled on Windows.")]
public new void Begin()
{
base.Begin();
}
+ [SuppressMessage("ReSharper", "CS0109", Justification = "The 'new' modifier applies when compiled on Windows.")]
public new void Begin(SpriteSortMode sortMode, BlendState blendState)
{
base.Begin(sortMode, blendState);
}
+ [SuppressMessage("ReSharper", "CS0109", Justification = "The 'new' modifier applies when compiled on Windows.")]
public new void Begin(SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, RasterizerState rasterizerState)
{
base.Begin(sortMode, blendState, samplerState, depthStencilState, rasterizerState);
}
+ [SuppressMessage("ReSharper", "CS0109", Justification = "The 'new' modifier applies when compiled on Windows.")]
public new void Begin(SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, RasterizerState rasterizerState, Effect effect)
{
base.Begin(sortMode, blendState, samplerState, depthStencilState, rasterizerState, effect);
}
+ [SuppressMessage("ReSharper", "CS0109", Justification = "The 'new' modifier applies when compiled on Windows.")]
public new void Begin(SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, RasterizerState rasterizerState, Effect effect, Matrix transformMatrix)
{
base.Begin(sortMode, blendState, samplerState, depthStencilState, rasterizerState, effect, transformMatrix);
diff --git a/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj b/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj
index 1e6caacc..775de9f2 100644
--- a/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj
+++ b/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj
@@ -60,23 +60,27 @@
<HintPath>..\packages\Mono.Cecil.0.9.6.4\lib\net45\Mono.Cecil.Pdb.dll</HintPath>
<Private>True</Private>
</Reference>
- <Reference Include="Mono.Cecil.Rocks, Version=0.9.6.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756, processorArchitecture=MSIL">
- <HintPath>..\packages\Mono.Cecil.0.9.6.4\lib\net45\Mono.Cecil.Rocks.dll</HintPath>
- <Private>True</Private>
- </Reference>
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\GlobalAssemblyInfo.cs">
<Link>Properties\GlobalAssemblyInfo.cs</Link>
</Compile>
- <Compile Include="IMethodRewriter.cs" />
+ <Compile Include="Finders\EventFinder.cs" />
+ <Compile Include="Finders\FieldFinder.cs" />
+ <Compile Include="Finders\MethodFinder.cs" />
+ <Compile Include="Finders\TypeFinder.cs" />
+ <Compile Include="IncompatibleInstructionException.cs" />
+ <Compile Include="RewriteHelper.cs" />
+ <Compile Include="IInstructionRewriter.cs" />
<Compile Include="Platform.cs" />
<Compile Include="PlatformAssemblyMap.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
- <Compile Include="Rewriters\BaseMethodRewriter.cs" />
- <Compile Include="Rewriters\SpriteBatchRewriter.cs" />
- <Compile Include="Wrappers\CompatibleSpriteBatch.cs" />
+ <Compile Include="Rewriters\TypeReferenceRewriter.cs" />
+ <Compile Include="Rewriters\FieldReplaceRewriter.cs" />
+ <Compile Include="Rewriters\FieldToPropertyRewriter.cs" />
+ <Compile Include="Rewriters\MethodParentRewriter.cs" />
+ <Compile Include="Rewriters\Wrappers\SpriteBatchWrapper.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />