summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/StardewModdingAPI/Framework/ModLoading/AssemblyLoader.cs60
-rw-r--r--src/StardewModdingAPI/Framework/ModLoading/Finders/EventFinder.cs49
-rw-r--r--src/StardewModdingAPI/Framework/ModLoading/Finders/FieldFinder.cs49
-rw-r--r--src/StardewModdingAPI/Framework/ModLoading/Finders/MethodFinder.cs49
-rw-r--r--src/StardewModdingAPI/Framework/ModLoading/Finders/PropertyFinder.cs49
-rw-r--r--src/StardewModdingAPI/Framework/ModLoading/Finders/TypeFinder.cs54
-rw-r--r--src/StardewModdingAPI/Framework/ModLoading/IInstructionRewriter.cs32
-rw-r--r--src/StardewModdingAPI/Framework/ModLoading/InstructionHandleResult.cs15
-rw-r--r--src/StardewModdingAPI/Framework/ModLoading/Rewriters/FieldReplaceRewriter.cs23
-rw-r--r--src/StardewModdingAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs23
-rw-r--r--src/StardewModdingAPI/Framework/ModLoading/Rewriters/MethodParentRewriter.cs39
-rw-r--r--src/StardewModdingAPI/Framework/ModLoading/Rewriters/TypeReferenceRewriter.cs41
-rw-r--r--src/StardewModdingAPI/Metadata/InstructionMetadata.cs84
-rw-r--r--src/StardewModdingAPI/StardewModdingAPI.csproj1
14 files changed, 286 insertions, 282 deletions
diff --git a/src/StardewModdingAPI/Framework/ModLoading/AssemblyLoader.cs b/src/StardewModdingAPI/Framework/ModLoading/AssemblyLoader.cs
index 871a081f..3a8e4fa9 100644
--- a/src/StardewModdingAPI/Framework/ModLoading/AssemblyLoader.cs
+++ b/src/StardewModdingAPI/Framework/ModLoading/AssemblyLoader.cs
@@ -214,25 +214,31 @@ namespace StardewModdingAPI.Framework.ModLoading
// find (and optionally rewrite) incompatible instructions
bool anyRewritten = false;
- IInstructionRewriter[] rewriters = new InstructionMetadata().GetRewriters().ToArray();
+ IInstructionHandler[] handlers = new InstructionMetadata().GetHandlers().ToArray();
foreach (MethodDefinition method in this.GetMethods(module))
{
// check method definition
- foreach (IInstructionRewriter rewriter in rewriters)
+ foreach (IInstructionHandler handler in handlers)
{
- try
+ InstructionHandleResult result = handler.Handle(mod, module, method, this.AssemblyMap, platformChanged);
+ switch (result)
{
- if (rewriter.Rewrite(mod, module, method, this.AssemblyMap, platformChanged))
- {
- this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Rewrote {filename} to fix {rewriter.NounPhrase}...");
+ case InstructionHandleResult.Rewritten:
+ this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Rewrote {filename} to fix {handler.NounPhrase}...");
anyRewritten = true;
- }
- }
- catch (IncompatibleInstructionException)
- {
- if (!assumeCompatible)
- throw new IncompatibleInstructionException(rewriter.NounPhrase, $"Found an incompatible CIL instruction ({rewriter.NounPhrase}) while loading assembly {filename}.");
- this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Found an incompatible CIL instruction ({rewriter.NounPhrase}) while loading assembly {filename}, but SMAPI is configured to allow it anyway. The mod may crash or behave unexpectedly.", LogLevel.Warn);
+ break;
+
+ case InstructionHandleResult.NotCompatible:
+ if (!assumeCompatible)
+ throw new IncompatibleInstructionException(handler.NounPhrase, $"Found an incompatible CIL instruction ({handler.NounPhrase}) while loading assembly {filename}.");
+ this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Found an incompatible CIL instruction ({handler.NounPhrase}) while loading assembly {filename}, but SMAPI is configured to allow it anyway. The mod may crash or behave unexpectedly.", LogLevel.Warn);
+ break;
+
+ case InstructionHandleResult.None:
+ break;
+
+ default:
+ throw new NotSupportedException($"Unrecognised instruction handler result '{result}'.");
}
}
@@ -240,21 +246,27 @@ namespace StardewModdingAPI.Framework.ModLoading
ILProcessor cil = method.Body.GetILProcessor();
foreach (Instruction instruction in cil.Body.Instructions.ToArray())
{
- foreach (IInstructionRewriter rewriter in rewriters)
+ foreach (IInstructionHandler rewriter in handlers)
{
- try
+ InstructionHandleResult result = rewriter.Handle(mod, module, cil, instruction, this.AssemblyMap, platformChanged);
+ switch (result)
{
- if (rewriter.Rewrite(mod, module, cil, instruction, this.AssemblyMap, platformChanged))
- {
+ case InstructionHandleResult.Rewritten:
this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Rewrote {filename} to fix {rewriter.NounPhrase}...");
anyRewritten = true;
- }
- }
- catch (IncompatibleInstructionException)
- {
- if (!assumeCompatible)
- throw new IncompatibleInstructionException(rewriter.NounPhrase, $"Found an incompatible CIL instruction ({rewriter.NounPhrase}) while loading assembly {filename}.");
- this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Found an incompatible CIL instruction ({rewriter.NounPhrase}) while loading assembly {filename}, but SMAPI is configured to allow it anyway. The mod may crash or behave unexpectedly.", LogLevel.Warn);
+ break;
+
+ case InstructionHandleResult.NotCompatible:
+ if (!assumeCompatible)
+ throw new IncompatibleInstructionException(rewriter.NounPhrase, $"Found an incompatible CIL instruction ({rewriter.NounPhrase}) while loading assembly {filename}.");
+ this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Found an incompatible CIL instruction ({rewriter.NounPhrase}) while loading assembly {filename}, but SMAPI is configured to allow it anyway. The mod may crash or behave unexpectedly.", LogLevel.Warn);
+ break;
+
+ case InstructionHandleResult.None:
+ break;
+
+ default:
+ throw new NotSupportedException($"Unrecognised instruction handler result '{result}'.");
}
}
}
diff --git a/src/StardewModdingAPI/Framework/ModLoading/Finders/EventFinder.cs b/src/StardewModdingAPI/Framework/ModLoading/Finders/EventFinder.cs
index ac5034c4..cd65b2dd 100644
--- a/src/StardewModdingAPI/Framework/ModLoading/Finders/EventFinder.cs
+++ b/src/StardewModdingAPI/Framework/ModLoading/Finders/EventFinder.cs
@@ -3,8 +3,8 @@ using Mono.Cecil.Cil;
namespace StardewModdingAPI.Framework.ModLoading.Finders
{
- /// <summary>Finds incompatible CIL instructions that reference a given event and throws an <see cref="IncompatibleInstructionException"/>.</summary>
- internal class EventFinder : IInstructionRewriter
+ /// <summary>Finds incompatible CIL instructions that reference a given event.</summary>
+ internal class EventFinder : IInstructionHandler
{
/*********
** Properties
@@ -15,6 +15,9 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders
/// <summary>The event name for which to find references.</summary>
private readonly string EventName;
+ /// <summary>The result to return for matching instructions.</summary>
+ private readonly InstructionHandleResult Result;
+
/*********
** Accessors
@@ -29,42 +32,38 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders
/// <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)
+ /// <param name="result">The result to return for matching instructions.</param>
+ public EventFinder(string fullTypeName, string eventName, InstructionHandleResult result)
{
this.FullTypeName = fullTypeName;
this.EventName = eventName;
- this.NounPhrase = nounPhrase ?? $"{fullTypeName}.{eventName} event";
+ this.Result = result;
+ this.NounPhrase = $"{fullTypeName}.{eventName} event";
}
- /// <summary>Rewrite a method definition for compatibility.</summary>
- /// <param name="mod">The mod to which the module belongs.</param>
- /// <param name="module">The module being rewritten.</param>
- /// <param name="method">The method definition to rewrite.</param>
+ /// <summary>Perform the predefined logic for a method if applicable.</summary>
+ /// <param name="mod">The mod containing the instruction.</param>
+ /// <param name="module">The assembly module containing the instruction.</param>
+ /// <param name="method">The method definition containing the instruction.</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(IModMetadata mod, ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged)
+ public virtual InstructionHandleResult Handle(IModMetadata mod, ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged)
{
- return false;
+ return InstructionHandleResult.None;
}
- /// <summary>Rewrite a CIL instruction for compatibility.</summary>
- /// <param name="mod">The mod to which the module belongs.</param>
- /// <param name="module">The module being rewritten.</param>
- /// <param name="cil">The CIL rewriter.</param>
- /// <param name="instruction">The instruction to rewrite.</param>
+ /// <summary>Perform the predefined logic for an instruction if applicable.</summary>
+ /// <param name="mod">The mod containing the instruction.</param>
+ /// <param name="module">The assembly module containing the instruction.</param>
+ /// <param name="cil">The CIL processor.</param>
+ /// <param name="instruction">The instruction to handle.</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(IModMetadata mod, ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
+ public virtual InstructionHandleResult Handle(IModMetadata mod, ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
{
- if (!this.IsMatch(instruction))
- return false;
-
- throw new IncompatibleInstructionException(this.NounPhrase);
+ return this.IsMatch(instruction)
+ ? this.Result
+ : InstructionHandleResult.None;
}
diff --git a/src/StardewModdingAPI/Framework/ModLoading/Finders/FieldFinder.cs b/src/StardewModdingAPI/Framework/ModLoading/Finders/FieldFinder.cs
index 008399d5..1ec4483e 100644
--- a/src/StardewModdingAPI/Framework/ModLoading/Finders/FieldFinder.cs
+++ b/src/StardewModdingAPI/Framework/ModLoading/Finders/FieldFinder.cs
@@ -3,8 +3,8 @@ using Mono.Cecil.Cil;
namespace StardewModdingAPI.Framework.ModLoading.Finders
{
- /// <summary>Finds incompatible CIL instructions that reference a given field and throws an <see cref="IncompatibleInstructionException"/>.</summary>
- internal class FieldFinder : IInstructionRewriter
+ /// <summary>Finds incompatible CIL instructions that reference a given field.</summary>
+ internal class FieldFinder : IInstructionHandler
{
/*********
** Properties
@@ -15,6 +15,9 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders
/// <summary>The field name for which to find references.</summary>
private readonly string FieldName;
+ /// <summary>The result to return for matching instructions.</summary>
+ private readonly InstructionHandleResult Result;
+
/*********
** Accessors
@@ -29,42 +32,38 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders
/// <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)
+ /// <param name="result">The result to return for matching instructions.</param>
+ public FieldFinder(string fullTypeName, string fieldName, InstructionHandleResult result)
{
this.FullTypeName = fullTypeName;
this.FieldName = fieldName;
- this.NounPhrase = nounPhrase ?? $"{fullTypeName}.{fieldName} field";
+ this.Result = result;
+ this.NounPhrase = $"{fullTypeName}.{fieldName} field";
}
- /// <summary>Rewrite a method definition for compatibility.</summary>
- /// <param name="mod">The mod to which the module belongs.</param>
- /// <param name="module">The module being rewritten.</param>
- /// <param name="method">The method definition to rewrite.</param>
+ /// <summary>Perform the predefined logic for a method if applicable.</summary>
+ /// <param name="mod">The mod containing the instruction.</param>
+ /// <param name="module">The assembly module containing the instruction.</param>
+ /// <param name="method">The method definition containing the instruction.</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(IModMetadata mod, ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged)
+ public virtual InstructionHandleResult Handle(IModMetadata mod, ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged)
{
- return false;
+ return InstructionHandleResult.None;
}
- /// <summary>Rewrite a CIL instruction for compatibility.</summary>
- /// <param name="mod">The mod to which the module belongs.</param>
- /// <param name="module">The module being rewritten.</param>
- /// <param name="cil">The CIL rewriter.</param>
- /// <param name="instruction">The instruction to rewrite.</param>
+ /// <summary>Perform the predefined logic for an instruction if applicable.</summary>
+ /// <param name="mod">The mod containing the instruction.</param>
+ /// <param name="module">The assembly module containing the instruction.</param>
+ /// <param name="cil">The CIL processor.</param>
+ /// <param name="instruction">The instruction to handle.</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(IModMetadata mod, ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
+ public virtual InstructionHandleResult Handle(IModMetadata mod, ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
{
- if (!this.IsMatch(instruction))
- return false;
-
- throw new IncompatibleInstructionException(this.NounPhrase);
+ return this.IsMatch(instruction)
+ ? this.Result
+ : InstructionHandleResult.None;
}
diff --git a/src/StardewModdingAPI/Framework/ModLoading/Finders/MethodFinder.cs b/src/StardewModdingAPI/Framework/ModLoading/Finders/MethodFinder.cs
index 2a6dc99e..4924c6e2 100644
--- a/src/StardewModdingAPI/Framework/ModLoading/Finders/MethodFinder.cs
+++ b/src/StardewModdingAPI/Framework/ModLoading/Finders/MethodFinder.cs
@@ -3,8 +3,8 @@ using Mono.Cecil.Cil;
namespace StardewModdingAPI.Framework.ModLoading.Finders
{
- /// <summary>Finds incompatible CIL instructions that reference a given method and throws an <see cref="IncompatibleInstructionException"/>.</summary>
- internal class MethodFinder : IInstructionRewriter
+ /// <summary>Finds incompatible CIL instructions that reference a given method.</summary>
+ internal class MethodFinder : IInstructionHandler
{
/*********
** Properties
@@ -15,6 +15,9 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders
/// <summary>The method name for which to find references.</summary>
private readonly string MethodName;
+ /// <summary>The result to return for matching instructions.</summary>
+ private readonly InstructionHandleResult Result;
+
/*********
** Accessors
@@ -29,42 +32,38 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders
/// <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)
+ /// <param name="result">The result to return for matching instructions.</param>
+ public MethodFinder(string fullTypeName, string methodName, InstructionHandleResult result)
{
this.FullTypeName = fullTypeName;
this.MethodName = methodName;
- this.NounPhrase = nounPhrase ?? $"{fullTypeName}.{methodName} method";
+ this.Result = result;
+ this.NounPhrase = $"{fullTypeName}.{methodName} method";
}
- /// <summary>Rewrite a method definition for compatibility.</summary>
- /// <param name="mod">The mod to which the module belongs.</param>
- /// <param name="module">The module being rewritten.</param>
- /// <param name="method">The method definition to rewrite.</param>
+ /// <summary>Perform the predefined logic for a method if applicable.</summary>
+ /// <param name="mod">The mod containing the instruction.</param>
+ /// <param name="module">The assembly module containing the instruction.</param>
+ /// <param name="method">The method definition containing the instruction.</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(IModMetadata mod, ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged)
+ public virtual InstructionHandleResult Handle(IModMetadata mod, ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged)
{
- return false;
+ return InstructionHandleResult.None;
}
- /// <summary>Rewrite a CIL instruction for compatibility.</summary>
- /// <param name="mod">The mod to which the module belongs.</param>
- /// <param name="module">The module being rewritten.</param>
- /// <param name="cil">The CIL rewriter.</param>
- /// <param name="instruction">The instruction to rewrite.</param>
+ /// <summary>Perform the predefined logic for an instruction if applicable.</summary>
+ /// <param name="mod">The mod containing the instruction.</param>
+ /// <param name="module">The assembly module containing the instruction.</param>
+ /// <param name="cil">The CIL processor.</param>
+ /// <param name="instruction">The instruction to handle.</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(IModMetadata mod, ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
+ public InstructionHandleResult Handle(IModMetadata mod, ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
{
- if (!this.IsMatch(instruction))
- return false;
-
- throw new IncompatibleInstructionException(this.NounPhrase);
+ return this.IsMatch(instruction)
+ ? this.Result
+ : InstructionHandleResult.None;
}
diff --git a/src/StardewModdingAPI/Framework/ModLoading/Finders/PropertyFinder.cs b/src/StardewModdingAPI/Framework/ModLoading/Finders/PropertyFinder.cs
index a0ce1cbf..37392f77 100644
--- a/src/StardewModdingAPI/Framework/ModLoading/Finders/PropertyFinder.cs
+++ b/src/StardewModdingAPI/Framework/ModLoading/Finders/PropertyFinder.cs
@@ -3,8 +3,8 @@ using Mono.Cecil.Cil;
namespace StardewModdingAPI.Framework.ModLoading.Finders
{
- /// <summary>Finds incompatible CIL instructions that reference a given property and throws an <see cref="IncompatibleInstructionException"/>.</summary>
- internal class PropertyFinder : IInstructionRewriter
+ /// <summary>Finds incompatible CIL instructions that reference a given property.</summary>
+ internal class PropertyFinder : IInstructionHandler
{
/*********
** Properties
@@ -15,6 +15,9 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders
/// <summary>The property name for which to find references.</summary>
private readonly string PropertyName;
+ /// <summary>The result to return for matching instructions.</summary>
+ private readonly InstructionHandleResult Result;
+
/*********
** Accessors
@@ -29,42 +32,38 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders
/// <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)
+ /// <param name="result">The result to return for matching instructions.</param>
+ public PropertyFinder(string fullTypeName, string propertyName, InstructionHandleResult result)
{
this.FullTypeName = fullTypeName;
this.PropertyName = propertyName;
- this.NounPhrase = nounPhrase ?? $"{fullTypeName}.{propertyName} property";
+ this.Result = result;
+ this.NounPhrase = $"{fullTypeName}.{propertyName} property";
}
- /// <summary>Rewrite a method definition for compatibility.</summary>
- /// <param name="mod">The mod to which the module belongs.</param>
- /// <param name="module">The module being rewritten.</param>
- /// <param name="method">The method definition to rewrite.</param>
+ /// <summary>Perform the predefined logic for a method if applicable.</summary>
+ /// <param name="mod">The mod containing the instruction.</param>
+ /// <param name="module">The assembly module containing the instruction.</param>
+ /// <param name="method">The method definition containing the instruction.</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(IModMetadata mod, ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged)
+ public virtual InstructionHandleResult Handle(IModMetadata mod, ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged)
{
- return false;
+ return InstructionHandleResult.None;
}
- /// <summary>Rewrite a CIL instruction for compatibility.</summary>
- /// <param name="mod">The mod to which the module belongs.</param>
- /// <param name="module">The module being rewritten.</param>
- /// <param name="cil">The CIL rewriter.</param>
- /// <param name="instruction">The instruction to rewrite.</param>
+ /// <summary>Perform the predefined logic for an instruction if applicable.</summary>
+ /// <param name="mod">The mod containing the instruction.</param>
+ /// <param name="module">The assembly module containing the instruction.</param>
+ /// <param name="cil">The CIL processor.</param>
+ /// <param name="instruction">The instruction to handle.</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(IModMetadata mod, ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
+ public virtual InstructionHandleResult Handle(IModMetadata mod, ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
{
- if (!this.IsMatch(instruction))
- return false;
-
- throw new IncompatibleInstructionException(this.NounPhrase);
+ return this.IsMatch(instruction)
+ ? this.Result
+ : InstructionHandleResult.None;
}
diff --git a/src/StardewModdingAPI/Framework/ModLoading/Finders/TypeFinder.cs b/src/StardewModdingAPI/Framework/ModLoading/Finders/TypeFinder.cs
index a3005c85..a0703669 100644
--- a/src/StardewModdingAPI/Framework/ModLoading/Finders/TypeFinder.cs
+++ b/src/StardewModdingAPI/Framework/ModLoading/Finders/TypeFinder.cs
@@ -4,8 +4,8 @@ using Mono.Cecil.Cil;
namespace StardewModdingAPI.Framework.ModLoading.Finders
{
- /// <summary>Finds incompatible CIL instructions that reference a given type and throws an <see cref="IncompatibleInstructionException"/>.</summary>
- internal class TypeFinder : IInstructionRewriter
+ /// <summary>Finds incompatible CIL instructions that reference a given type.</summary>
+ internal class TypeFinder : IInstructionHandler
{
/*********
** Accessors
@@ -13,6 +13,9 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders
/// <summary>The full type name for which to find references.</summary>
private readonly string FullTypeName;
+ /// <summary>The result to return for matching instructions.</summary>
+ private readonly InstructionHandleResult Result;
+
/*********
** Accessors
@@ -26,44 +29,39 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders
*********/
/// <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)
+ /// <param name="result">The result to return for matching instructions.</param>
+ public TypeFinder(string fullTypeName, InstructionHandleResult result)
{
this.FullTypeName = fullTypeName;
- this.NounPhrase = nounPhrase ?? $"{fullTypeName} type";
+ this.Result = result;
+ this.NounPhrase = $"{fullTypeName} type";
}
- /// <summary>Rewrite a method definition for compatibility.</summary>
- /// <param name="mod">The mod to which the module belongs.</param>
- /// <param name="module">The module being rewritten.</param>
- /// <param name="method">The method definition to rewrite.</param>
+ /// <summary>Perform the predefined logic for a method if applicable.</summary>
+ /// <param name="mod">The mod containing the instruction.</param>
+ /// <param name="module">The assembly module containing the instruction.</param>
+ /// <param name="method">The method definition containing the instruction.</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(IModMetadata mod, ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged)
+ public virtual InstructionHandleResult Handle(IModMetadata mod, ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged)
{
- if (!this.IsMatch(method))
- return false;
-
- throw new IncompatibleInstructionException(this.NounPhrase);
+ return this.IsMatch(method)
+ ? this.Result
+ : InstructionHandleResult.None;
}
- /// <summary>Rewrite a CIL instruction for compatibility.</summary>
- /// <param name="mod">The mod to which the module belongs.</param>
- /// <param name="module">The module being rewritten.</param>
- /// <param name="cil">The CIL rewriter.</param>
- /// <param name="instruction">The instruction to rewrite.</param>
+ /// <summary>Perform the predefined logic for an instruction if applicable.</summary>
+ /// <param name="mod">The mod containing the instruction.</param>
+ /// <param name="module">The assembly module containing the instruction.</param>
+ /// <param name="cil">The CIL processor.</param>
+ /// <param name="instruction">The instruction to handle.</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(IModMetadata mod, ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
+ public virtual InstructionHandleResult Handle(IModMetadata mod, ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
{
- if (!this.IsMatch(instruction))
- return false;
-
- throw new IncompatibleInstructionException(this.NounPhrase);
+ return this.IsMatch(instruction)
+ ? this.Result
+ : InstructionHandleResult.None;
}
diff --git a/src/StardewModdingAPI/Framework/ModLoading/IInstructionRewriter.cs b/src/StardewModdingAPI/Framework/ModLoading/IInstructionRewriter.cs
index ce12c717..be2e10ee 100644
--- a/src/StardewModdingAPI/Framework/ModLoading/IInstructionRewriter.cs
+++ b/src/StardewModdingAPI/Framework/ModLoading/IInstructionRewriter.cs
@@ -3,38 +3,34 @@ using Mono.Cecil.Cil;
namespace StardewModdingAPI.Framework.ModLoading
{
- /// <summary>Rewrites CIL instructions for compatibility.</summary>
- internal interface IInstructionRewriter
+ /// <summary>Performs predefined logic for detected CIL instructions.</summary>
+ internal interface IInstructionHandler
{
/*********
** Accessors
*********/
- /// <summary>A brief noun phrase indicating what the rewriter matches.</summary>
+ /// <summary>A brief noun phrase indicating what the handler matches.</summary>
string NounPhrase { get; }
/*********
** Methods
*********/
- /// <summary>Rewrite a method definition for compatibility.</summary>
- /// <param name="mod">The mod to which the module belongs.</param>
- /// <param name="module">The module being rewritten.</param>
- /// <param name="method">The method definition to rewrite.</param>
+ /// <summary>Perform the predefined logic for a method if applicable.</summary>
+ /// <param name="mod">The mod containing the instruction.</param>
+ /// <param name="module">The assembly module containing the instruction.</param>
+ /// <param name="method">The method definition containing the instruction.</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(IModMetadata mod, ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged);
+ InstructionHandleResult Handle(IModMetadata mod, ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged);
- /// <summary>Rewrite a CIL instruction for compatibility.</summary>
- /// <param name="mod">The mod to which the module belongs.</param>
- /// <param name="module">The module being rewritten.</param>
- /// <param name="cil">The CIL rewriter.</param>
- /// <param name="instruction">The instruction to rewrite.</param>
+ /// <summary>Perform the predefined logic for an instruction if applicable.</summary>
+ /// <param name="mod">The mod containing the instruction.</param>
+ /// <param name="module">The assembly module containing the instruction.</param>
+ /// <param name="cil">The CIL processor.</param>
+ /// <param name="instruction">The instruction to handle.</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(IModMetadata mod, ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged);
+ InstructionHandleResult Handle(IModMetadata mod, ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged);
}
}
diff --git a/src/StardewModdingAPI/Framework/ModLoading/InstructionHandleResult.cs b/src/StardewModdingAPI/Framework/ModLoading/InstructionHandleResult.cs
new file mode 100644
index 00000000..3921e9c4
--- /dev/null
+++ b/src/StardewModdingAPI/Framework/ModLoading/InstructionHandleResult.cs
@@ -0,0 +1,15 @@
+namespace StardewModdingAPI.Framework.ModLoading
+{
+ /// <summary>Indicates how an instruction was handled.</summary>
+ internal enum InstructionHandleResult
+ {
+ /// <summary>No special handling is needed.</summary>
+ None,
+
+ /// <summary>The instruction was successfully rewritten for compatibility.</summary>
+ Rewritten,
+
+ /// <summary>The instruction is not compatible and can't be rewritten for compatibility.</summary>
+ NotCompatible
+ }
+}
diff --git a/src/StardewModdingAPI/Framework/ModLoading/Rewriters/FieldReplaceRewriter.cs b/src/StardewModdingAPI/Framework/ModLoading/Rewriters/FieldReplaceRewriter.cs
index fb2a9a96..324d1d1e 100644
--- a/src/StardewModdingAPI/Framework/ModLoading/Rewriters/FieldReplaceRewriter.cs
+++ b/src/StardewModdingAPI/Framework/ModLoading/Rewriters/FieldReplaceRewriter.cs
@@ -23,32 +23,29 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters
/// <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)
+ public FieldReplaceRewriter(Type type, string fromFieldName, string toFieldName)
+ : base(type.FullName, fromFieldName, InstructionHandleResult.None)
{
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="mod">The mod to which the module belongs.</param>
- /// <param name="module">The module being rewritten.</param>
- /// <param name="cil">The CIL rewriter.</param>
- /// <param name="instruction">The instruction to rewrite.</param>
+ /// <summary>Perform the predefined logic for an instruction if applicable.</summary>
+ /// <param name="mod">The mod containing the instruction.</param>
+ /// <param name="module">The assembly module containing the instruction.</param>
+ /// <param name="cil">The CIL processor.</param>
+ /// <param name="instruction">The instruction to handle.</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(IModMetadata mod, ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
+ public override InstructionHandleResult Handle(IModMetadata mod, ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
{
if (!this.IsMatch(instruction))
- return false;
+ return InstructionHandleResult.None;
FieldReference newRef = module.Import(this.ToField);
cil.Replace(instruction, cil.Create(instruction.OpCode, newRef));
- return true;
+ return InstructionHandleResult.Rewritten;
}
}
}
diff --git a/src/StardewModdingAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs b/src/StardewModdingAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs
index 03d1f707..1b005869 100644
--- a/src/StardewModdingAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs
+++ b/src/StardewModdingAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs
@@ -24,32 +24,29 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters
/// <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)
+ public FieldToPropertyRewriter(Type type, string fieldName)
+ : base(type.FullName, fieldName, InstructionHandleResult.None)
{
this.Type = type;
this.FieldName = fieldName;
}
- /// <summary>Rewrite a CIL instruction for compatibility.</summary>
- /// <param name="mod">The mod to which the module belongs.</param>
- /// <param name="module">The module being rewritten.</param>
- /// <param name="cil">The CIL rewriter.</param>
- /// <param name="instruction">The instruction to rewrite.</param>
+ /// <summary>Perform the predefined logic for an instruction if applicable.</summary>
+ /// <param name="mod">The mod containing the instruction.</param>
+ /// <param name="module">The assembly module containing the instruction.</param>
+ /// <param name="cil">The CIL processor.</param>
+ /// <param name="instruction">The instruction to handle.</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(IModMetadata mod, ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
+ public override InstructionHandleResult Handle(IModMetadata mod, ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
{
if (!this.IsMatch(instruction))
- return false;
+ return InstructionHandleResult.None;
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;
+ return InstructionHandleResult.Rewritten;
}
}
}
diff --git a/src/StardewModdingAPI/Framework/ModLoading/Rewriters/MethodParentRewriter.cs b/src/StardewModdingAPI/Framework/ModLoading/Rewriters/MethodParentRewriter.cs
index 1e116e1f..50338071 100644
--- a/src/StardewModdingAPI/Framework/ModLoading/Rewriters/MethodParentRewriter.cs
+++ b/src/StardewModdingAPI/Framework/ModLoading/Rewriters/MethodParentRewriter.cs
@@ -5,7 +5,7 @@ using Mono.Cecil.Cil;
namespace StardewModdingAPI.Framework.ModLoading.Rewriters
{
/// <summary>Rewrites method references from one parent type to another if the signatures match.</summary>
- internal class MethodParentRewriter : IInstructionRewriter
+ internal class MethodParentRewriter : IInstructionHandler
{
/*********
** Properties
@@ -34,45 +34,40 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters
/// <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)
+ public MethodParentRewriter(Type fromType, Type toType, bool onlyIfPlatformChanged = false)
{
this.FromType = fromType;
this.ToType = toType;
- this.NounPhrase = nounPhrase ?? $"{fromType.Name} methods";
+ this.NounPhrase = $"{fromType.Name} methods";
this.OnlyIfPlatformChanged = onlyIfPlatformChanged;
}
- /// <summary>Rewrite a method definition for compatibility.</summary>
- /// <param name="mod">The mod to which the module belongs.</param>
- /// <param name="module">The module being rewritten.</param>
- /// <param name="method">The method definition to rewrite.</param>
+ /// <summary>Perform the predefined logic for a method if applicable.</summary>
+ /// <param name="mod">The mod containing the instruction.</param>
+ /// <param name="module">The assembly module containing the instruction.</param>
+ /// <param name="method">The method definition containing the instruction.</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(IModMetadata mod, ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged)
+ public InstructionHandleResult Handle(IModMetadata mod, ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged)
{
- return false;
+ return InstructionHandleResult.None;
}
- /// <summary>Rewrite a CIL instruction for compatibility.</summary>
- /// <param name="mod">The mod to which the module belongs.</param>
- /// <param name="module">The module being rewritten.</param>
- /// <param name="cil">The CIL rewriter.</param>
- /// <param name="instruction">The instruction to rewrite.</param>
+ /// <summary>Perform the predefined logic for an instruction if applicable.</summary>
+ /// <param name="mod">The mod containing the instruction.</param>
+ /// <param name="module">The assembly module containing the instruction.</param>
+ /// <param name="cil">The CIL processor.</param>
+ /// <param name="instruction">The instruction to handle.</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(IModMetadata mod, ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
+ public InstructionHandleResult Handle(IModMetadata mod, ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
{
if (!this.IsMatch(instruction, platformChanged))
- return false;
+ return InstructionHandleResult.None;
MethodReference methodRef = (MethodReference)instruction.Operand;
methodRef.DeclaringType = module.Import(this.ToType);
- return true;
+ return InstructionHandleResult.Rewritten;
}
diff --git a/src/StardewModdingAPI/Framework/ModLoading/Rewriters/TypeReferenceRewriter.cs b/src/StardewModdingAPI/Framework/ModLoading/Rewriters/TypeReferenceRewriter.cs
index 8db39cfe..df6e5e4b 100644
--- a/src/StardewModdingAPI/Framework/ModLoading/Rewriters/TypeReferenceRewriter.cs
+++ b/src/StardewModdingAPI/Framework/ModLoading/Rewriters/TypeReferenceRewriter.cs
@@ -24,23 +24,20 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters
/// <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)
+ public TypeReferenceRewriter(string fromTypeFullName, Type toType)
+ : base(fromTypeFullName, InstructionHandleResult.None)
{
this.FromTypeName = fromTypeFullName;
this.ToType = toType;
}
- /// <summary>Rewrite a method definition for compatibility.</summary>
- /// <param name="mod">The mod to which the module belongs.</param>
- /// <param name="module">The module being rewritten.</param>
- /// <param name="method">The method definition to rewrite.</param>
+ /// <summary>Perform the predefined logic for a method if applicable.</summary>
+ /// <param name="mod">The mod containing the instruction.</param>
+ /// <param name="module">The assembly module containing the instruction.</param>
+ /// <param name="method">The method definition containing the instruction.</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(IModMetadata mod, ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged)
+ public override InstructionHandleResult Handle(IModMetadata mod, ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged)
{
bool rewritten = false;
@@ -84,22 +81,22 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters
}
}
- return rewritten;
+ return rewritten
+ ? InstructionHandleResult.Rewritten
+ : InstructionHandleResult.None;
}
- /// <summary>Rewrite a CIL instruction for compatibility.</summary>
- /// <param name="mod">The mod to which the module belongs.</param>
- /// <param name="module">The module being rewritten.</param>
- /// <param name="cil">The CIL rewriter.</param>
- /// <param name="instruction">The instruction to rewrite.</param>
+ /// <summary>Perform the predefined logic for an instruction if applicable.</summary>
+ /// <param name="mod">The mod containing the instruction.</param>
+ /// <param name="module">The assembly module containing the instruction.</param>
+ /// <param name="cil">The CIL processor.</param>
+ /// <param name="instruction">The instruction to handle.</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(IModMetadata mod, ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
+ public override InstructionHandleResult Handle(IModMetadata mod, ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
{
if (!this.IsMatch(instruction) && !instruction.ToString().Contains(this.FromTypeName))
- return false;
+ return InstructionHandleResult.None;
// field reference
FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction);
@@ -127,14 +124,14 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters
cil.Replace(instruction, cil.Create(instruction.OpCode, newRef));
}
- return true;
+ return InstructionHandleResult.Rewritten;
}
/*********
** 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="module">The assembly module containing the instruction.</param>
/// <param name="type">The type to replace if it matches.</param>
private TypeReference RewriteIfNeeded(ModuleDefinition module, TypeReference type)
{
diff --git a/src/StardewModdingAPI/Metadata/InstructionMetadata.cs b/src/StardewModdingAPI/Metadata/InstructionMetadata.cs
index 67fdc041..fc4f112c 100644
--- a/src/StardewModdingAPI/Metadata/InstructionMetadata.cs
+++ b/src/StardewModdingAPI/Metadata/InstructionMetadata.cs
@@ -16,61 +16,61 @@ namespace StardewModdingAPI.Metadata
** Public methods
*********/
/// <summary>Get rewriters which detect or fix incompatible CIL instructions in mod assemblies.</summary>
- public IEnumerable<IInstructionRewriter> GetRewriters()
+ public IEnumerable<IInstructionHandler> GetHandlers()
{
- return new IInstructionRewriter[]
+ return new IInstructionHandler[]
{
/****
- ** Finders throw an exception when incompatible code is found.
+ ** throw exception for incompatible code
****/
// changes in Stardew Valley 1.2 (with no rewriters)
- new FieldFinder("StardewValley.Item", "set_Name"),
+ new FieldFinder("StardewValley.Item", "set_Name", InstructionHandleResult.NotCompatible),
// APIs removed in SMAPI 1.9
- new TypeFinder("StardewModdingAPI.Advanced.ConfigFile"),
- new TypeFinder("StardewModdingAPI.Advanced.IConfigFile"),
- new TypeFinder("StardewModdingAPI.Entities.SPlayer"),
- new TypeFinder("StardewModdingAPI.Extensions"),
- new TypeFinder("StardewModdingAPI.Inheritance.SGame"),
- new TypeFinder("StardewModdingAPI.Inheritance.SObject"),
- new TypeFinder("StardewModdingAPI.LogWriter"),
- new TypeFinder("StardewModdingAPI.Manifest"),
- new TypeFinder("StardewModdingAPI.Version"),
- new EventFinder("StardewModdingAPI.Events.GraphicsEvents", "DrawDebug"),
- new EventFinder("StardewModdingAPI.Events.GraphicsEvents", "DrawTick"),
- new EventFinder("StardewModdingAPI.Events.GraphicsEvents", "OnPostRenderHudEventNoCheck"),
- new EventFinder("StardewModdingAPI.Events.GraphicsEvents", "OnPostRenderGuiEventNoCheck"),
- new EventFinder("StardewModdingAPI.Events.GraphicsEvents", "OnPreRenderHudEventNoCheck"),
- new EventFinder("StardewModdingAPI.Events.GraphicsEvents", "OnPreRenderGuiEventNoCheck"),
+ new TypeFinder("StardewModdingAPI.Advanced.ConfigFile", InstructionHandleResult.NotCompatible),
+ new TypeFinder("StardewModdingAPI.Advanced.IConfigFile", InstructionHandleResult.NotCompatible),
+ new TypeFinder("StardewModdingAPI.Entities.SPlayer", InstructionHandleResult.NotCompatible),
+ new TypeFinder("StardewModdingAPI.Extensions", InstructionHandleResult.NotCompatible),
+ new TypeFinder("StardewModdingAPI.Inheritance.SGame", InstructionHandleResult.NotCompatible),
+ new TypeFinder("StardewModdingAPI.Inheritance.SObject", InstructionHandleResult.NotCompatible),
+ new TypeFinder("StardewModdingAPI.LogWriter", InstructionHandleResult.NotCompatible),
+ new TypeFinder("StardewModdingAPI.Manifest", InstructionHandleResult.NotCompatible),
+ new TypeFinder("StardewModdingAPI.Version", InstructionHandleResult.NotCompatible),
+ new EventFinder("StardewModdingAPI.Events.GraphicsEvents", "DrawDebug", InstructionHandleResult.NotCompatible),
+ new EventFinder("StardewModdingAPI.Events.GraphicsEvents", "DrawTick", InstructionHandleResult.NotCompatible),
+ new EventFinder("StardewModdingAPI.Events.GraphicsEvents", "OnPostRenderHudEventNoCheck", InstructionHandleResult.NotCompatible),
+ new EventFinder("StardewModdingAPI.Events.GraphicsEvents", "OnPostRenderGuiEventNoCheck", InstructionHandleResult.NotCompatible),
+ new EventFinder("StardewModdingAPI.Events.GraphicsEvents", "OnPreRenderHudEventNoCheck", InstructionHandleResult.NotCompatible),
+ new EventFinder("StardewModdingAPI.Events.GraphicsEvents", "OnPreRenderGuiEventNoCheck", InstructionHandleResult.NotCompatible),
// APIs removed in SMAPI 2.0
#if !SMAPI_1_x
- new TypeFinder("StardewModdingAPI.Command"),
- new TypeFinder("StardewModdingAPI.Config"),
- new TypeFinder("StardewModdingAPI.Log"),
- new EventFinder("StardewModdingAPI.Events.GameEvents", "Initialize"),
- new EventFinder("StardewModdingAPI.Events.GameEvents", "LoadContent"),
- new EventFinder("StardewModdingAPI.Events.GameEvents", "GameLoaded"),
- new EventFinder("StardewModdingAPI.Events.GameEvents", "FirstUpdateTick"),
- new EventFinder("StardewModdingAPI.Events.PlayerEvents", "LoadedGame"),
- new EventFinder("StardewModdingAPI.Events.PlayerEvents", "FarmerChanged"),
- new EventFinder("StardewModdingAPI.Events.TimeEvents", "DayOfMonthChanged"),
- new EventFinder("StardewModdingAPI.Events.TimeEvents", "YearOfGameChanged"),
- new EventFinder("StardewModdingAPI.Events.TimeEvents", "SeasonOfYearChanged"),
- new EventFinder("StardewModdingAPI.Events.TimeEvents", "OnNewDay"),
- new TypeFinder("StardewModdingAPI.Events.EventArgsCommand"),
- new TypeFinder("StardewModdingAPI.Events.EventArgsFarmerChanged"),
- new TypeFinder("StardewModdingAPI.Events.EventArgsLoadedGameChanged"),
- new TypeFinder("StardewModdingAPI.Events.EventArgsNewDay"),
- new TypeFinder("StardewModdingAPI.Events.EventArgsStringChanged"),
- new PropertyFinder("StardewModdingAPI.Mod", "PathOnDisk"),
- new PropertyFinder("StardewModdingAPI.Mod", "BaseConfigPath"),
- new PropertyFinder("StardewModdingAPI.Mod", "PerSaveConfigFolder"),
- new PropertyFinder("StardewModdingAPI.Mod", "PerSaveConfigPath"),
+ new TypeFinder("StardewModdingAPI.Command", InstructionHandleResult.NotCompatible),
+ new TypeFinder("StardewModdingAPI.Config", InstructionHandleResult.NotCompatible),
+ new TypeFinder("StardewModdingAPI.Log", InstructionHandleResult.NotCompatible),
+ new EventFinder("StardewModdingAPI.Events.GameEvents", "Initialize", InstructionHandleResult.NotCompatible),
+ new EventFinder("StardewModdingAPI.Events.GameEvents", "LoadContent", InstructionHandleResult.NotCompatible),
+ new EventFinder("StardewModdingAPI.Events.GameEvents", "GameLoaded", InstructionHandleResult.NotCompatible),
+ new EventFinder("StardewModdingAPI.Events.GameEvents", "FirstUpdateTick", InstructionHandleResult.NotCompatible),
+ new EventFinder("StardewModdingAPI.Events.PlayerEvents", "LoadedGame", InstructionHandleResult.NotCompatible),
+ new EventFinder("StardewModdingAPI.Events.PlayerEvents", "FarmerChanged", InstructionHandleResult.NotCompatible),
+ new EventFinder("StardewModdingAPI.Events.TimeEvents", "DayOfMonthChanged", InstructionHandleResult.NotCompatible),
+ new EventFinder("StardewModdingAPI.Events.TimeEvents", "YearOfGameChanged", InstructionHandleResult.NotCompatible),
+ new EventFinder("StardewModdingAPI.Events.TimeEvents", "SeasonOfYearChanged", InstructionHandleResult.NotCompatible),
+ new EventFinder("StardewModdingAPI.Events.TimeEvents", "OnNewDay", InstructionHandleResult.NotCompatible),
+ new TypeFinder("StardewModdingAPI.Events.EventArgsCommand", InstructionHandleResult.NotCompatible),
+ new TypeFinder("StardewModdingAPI.Events.EventArgsFarmerChanged", InstructionHandleResult.NotCompatible),
+ new TypeFinder("StardewModdingAPI.Events.EventArgsLoadedGameChanged", InstructionHandleResult.NotCompatible),
+ new TypeFinder("StardewModdingAPI.Events.EventArgsNewDay", InstructionHandleResult.NotCompatible),
+ new TypeFinder("StardewModdingAPI.Events.EventArgsStringChanged", InstructionHandleResult.NotCompatible),
+ new PropertyFinder("StardewModdingAPI.Mod", "PathOnDisk", InstructionHandleResult.NotCompatible),
+ new PropertyFinder("StardewModdingAPI.Mod", "BaseConfigPath", InstructionHandleResult.NotCompatible),
+ new PropertyFinder("StardewModdingAPI.Mod", "PerSaveConfigFolder", InstructionHandleResult.NotCompatible),
+ new PropertyFinder("StardewModdingAPI.Mod", "PerSaveConfigPath", InstructionHandleResult.NotCompatible),
#endif
/****
- ** Rewriters change CIL as needed to fix incompatible code
+ ** rewrite CIL to fix incompatible code
****/
// crossplatform
new MethodParentRewriter(typeof(SpriteBatch), typeof(SpriteBatchWrapper), onlyIfPlatformChanged: true),
diff --git a/src/StardewModdingAPI/StardewModdingAPI.csproj b/src/StardewModdingAPI/StardewModdingAPI.csproj
index d4b1cb69..8b3b6936 100644
--- a/src/StardewModdingAPI/StardewModdingAPI.csproj
+++ b/src/StardewModdingAPI/StardewModdingAPI.csproj
@@ -98,6 +98,7 @@
<Compile Include="Framework\ModLoading\Finders\TypeFinder.cs" />
<Compile Include="Framework\ModLoading\IInstructionRewriter.cs" />
<Compile Include="Framework\ModLoading\IncompatibleInstructionException.cs" />
+ <Compile Include="Framework\ModLoading\InstructionHandleResult.cs" />
<Compile Include="Framework\ModLoading\Platform.cs" />
<Compile Include="Framework\ModLoading\PlatformAssemblyMap.cs" />
<Compile Include="Framework\ModLoading\RewriteHelper.cs" />