From ebf22c1b06cd12ce02df16278062f7d37bc66a4c Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Tue, 19 Sep 2017 23:42:54 -0400 Subject: generalise IInstructionRewriter into IInstructionHandler (#347) --- .../Framework/ModLoading/AssemblyLoader.cs | 60 +++++++++++++--------- .../Framework/ModLoading/Finders/EventFinder.cs | 49 +++++++++--------- .../Framework/ModLoading/Finders/FieldFinder.cs | 49 +++++++++--------- .../Framework/ModLoading/Finders/MethodFinder.cs | 49 +++++++++--------- .../Framework/ModLoading/Finders/PropertyFinder.cs | 49 +++++++++--------- .../Framework/ModLoading/Finders/TypeFinder.cs | 54 ++++++++++--------- .../Framework/ModLoading/IInstructionRewriter.cs | 32 +++++------- .../ModLoading/InstructionHandleResult.cs | 15 ++++++ .../ModLoading/Rewriters/FieldReplaceRewriter.cs | 23 ++++----- .../Rewriters/FieldToPropertyRewriter.cs | 23 ++++----- .../ModLoading/Rewriters/MethodParentRewriter.cs | 39 ++++++-------- .../ModLoading/Rewriters/TypeReferenceRewriter.cs | 41 +++++++-------- 12 files changed, 243 insertions(+), 240 deletions(-) create mode 100644 src/StardewModdingAPI/Framework/ModLoading/InstructionHandleResult.cs (limited to 'src/StardewModdingAPI/Framework/ModLoading') 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 { - /// Finds incompatible CIL instructions that reference a given event and throws an . - internal class EventFinder : IInstructionRewriter + /// Finds incompatible CIL instructions that reference a given event. + internal class EventFinder : IInstructionHandler { /********* ** Properties @@ -15,6 +15,9 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders /// The event name for which to find references. private readonly string EventName; + /// The result to return for matching instructions. + private readonly InstructionHandleResult Result; + /********* ** Accessors @@ -29,42 +32,38 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders /// Construct an instance. /// The full type name for which to find references. /// The event name for which to find references. - /// A brief noun phrase indicating what the instruction finder matches (or null to generate one). - public EventFinder(string fullTypeName, string eventName, string nounPhrase = null) + /// The result to return for matching instructions. + 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"; } - /// Rewrite a method definition for compatibility. - /// The mod to which the module belongs. - /// The module being rewritten. - /// The method definition to rewrite. + /// Perform the predefined logic for a method if applicable. + /// The mod containing the instruction. + /// The assembly module containing the instruction. + /// The method definition containing the instruction. /// Metadata for mapping assemblies to the current platform. /// Whether the mod was compiled on a different platform. - /// Returns whether the instruction was rewritten. - /// The CIL instruction is not compatible, and can't be rewritten. - 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; } - /// Rewrite a CIL instruction for compatibility. - /// The mod to which the module belongs. - /// The module being rewritten. - /// The CIL rewriter. - /// The instruction to rewrite. + /// Perform the predefined logic for an instruction if applicable. + /// The mod containing the instruction. + /// The assembly module containing the instruction. + /// The CIL processor. + /// The instruction to handle. /// Metadata for mapping assemblies to the current platform. /// Whether the mod was compiled on a different platform. - /// Returns whether the instruction was rewritten. - /// The CIL instruction is not compatible, and can't be rewritten. - 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 { - /// Finds incompatible CIL instructions that reference a given field and throws an . - internal class FieldFinder : IInstructionRewriter + /// Finds incompatible CIL instructions that reference a given field. + internal class FieldFinder : IInstructionHandler { /********* ** Properties @@ -15,6 +15,9 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders /// The field name for which to find references. private readonly string FieldName; + /// The result to return for matching instructions. + private readonly InstructionHandleResult Result; + /********* ** Accessors @@ -29,42 +32,38 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders /// Construct an instance. /// The full type name for which to find references. /// The field name for which to find references. - /// A brief noun phrase indicating what the instruction finder matches (or null to generate one). - public FieldFinder(string fullTypeName, string fieldName, string nounPhrase = null) + /// The result to return for matching instructions. + 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"; } - /// Rewrite a method definition for compatibility. - /// The mod to which the module belongs. - /// The module being rewritten. - /// The method definition to rewrite. + /// Perform the predefined logic for a method if applicable. + /// The mod containing the instruction. + /// The assembly module containing the instruction. + /// The method definition containing the instruction. /// Metadata for mapping assemblies to the current platform. /// Whether the mod was compiled on a different platform. - /// Returns whether the instruction was rewritten. - /// The CIL instruction is not compatible, and can't be rewritten. - 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; } - /// Rewrite a CIL instruction for compatibility. - /// The mod to which the module belongs. - /// The module being rewritten. - /// The CIL rewriter. - /// The instruction to rewrite. + /// Perform the predefined logic for an instruction if applicable. + /// The mod containing the instruction. + /// The assembly module containing the instruction. + /// The CIL processor. + /// The instruction to handle. /// Metadata for mapping assemblies to the current platform. /// Whether the mod was compiled on a different platform. - /// Returns whether the instruction was rewritten. - /// The CIL instruction is not compatible, and can't be rewritten. - 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 { - /// Finds incompatible CIL instructions that reference a given method and throws an . - internal class MethodFinder : IInstructionRewriter + /// Finds incompatible CIL instructions that reference a given method. + internal class MethodFinder : IInstructionHandler { /********* ** Properties @@ -15,6 +15,9 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders /// The method name for which to find references. private readonly string MethodName; + /// The result to return for matching instructions. + private readonly InstructionHandleResult Result; + /********* ** Accessors @@ -29,42 +32,38 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders /// Construct an instance. /// The full type name for which to find references. /// The method name for which to find references. - /// A brief noun phrase indicating what the instruction finder matches (or null to generate one). - public MethodFinder(string fullTypeName, string methodName, string nounPhrase = null) + /// The result to return for matching instructions. + 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"; } - /// Rewrite a method definition for compatibility. - /// The mod to which the module belongs. - /// The module being rewritten. - /// The method definition to rewrite. + /// Perform the predefined logic for a method if applicable. + /// The mod containing the instruction. + /// The assembly module containing the instruction. + /// The method definition containing the instruction. /// Metadata for mapping assemblies to the current platform. /// Whether the mod was compiled on a different platform. - /// Returns whether the instruction was rewritten. - /// The CIL instruction is not compatible, and can't be rewritten. - 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; } - /// Rewrite a CIL instruction for compatibility. - /// The mod to which the module belongs. - /// The module being rewritten. - /// The CIL rewriter. - /// The instruction to rewrite. + /// Perform the predefined logic for an instruction if applicable. + /// The mod containing the instruction. + /// The assembly module containing the instruction. + /// The CIL processor. + /// The instruction to handle. /// Metadata for mapping assemblies to the current platform. /// Whether the mod was compiled on a different platform. - /// Returns whether the instruction was rewritten. - /// The CIL instruction is not compatible, and can't be rewritten. - 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 { - /// Finds incompatible CIL instructions that reference a given property and throws an . - internal class PropertyFinder : IInstructionRewriter + /// Finds incompatible CIL instructions that reference a given property. + internal class PropertyFinder : IInstructionHandler { /********* ** Properties @@ -15,6 +15,9 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders /// The property name for which to find references. private readonly string PropertyName; + /// The result to return for matching instructions. + private readonly InstructionHandleResult Result; + /********* ** Accessors @@ -29,42 +32,38 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders /// Construct an instance. /// The full type name for which to find references. /// The property name for which to find references. - /// A brief noun phrase indicating what the instruction finder matches (or null to generate one). - public PropertyFinder(string fullTypeName, string propertyName, string nounPhrase = null) + /// The result to return for matching instructions. + 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"; } - /// Rewrite a method definition for compatibility. - /// The mod to which the module belongs. - /// The module being rewritten. - /// The method definition to rewrite. + /// Perform the predefined logic for a method if applicable. + /// The mod containing the instruction. + /// The assembly module containing the instruction. + /// The method definition containing the instruction. /// Metadata for mapping assemblies to the current platform. /// Whether the mod was compiled on a different platform. - /// Returns whether the instruction was rewritten. - /// The CIL instruction is not compatible, and can't be rewritten. - 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; } - /// Rewrite a CIL instruction for compatibility. - /// The mod to which the module belongs. - /// The module being rewritten. - /// The CIL rewriter. - /// The instruction to rewrite. + /// Perform the predefined logic for an instruction if applicable. + /// The mod containing the instruction. + /// The assembly module containing the instruction. + /// The CIL processor. + /// The instruction to handle. /// Metadata for mapping assemblies to the current platform. /// Whether the mod was compiled on a different platform. - /// Returns whether the instruction was rewritten. - /// The CIL instruction is not compatible, and can't be rewritten. - 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 { - /// Finds incompatible CIL instructions that reference a given type and throws an . - internal class TypeFinder : IInstructionRewriter + /// Finds incompatible CIL instructions that reference a given type. + internal class TypeFinder : IInstructionHandler { /********* ** Accessors @@ -13,6 +13,9 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders /// The full type name for which to find references. private readonly string FullTypeName; + /// The result to return for matching instructions. + private readonly InstructionHandleResult Result; + /********* ** Accessors @@ -26,44 +29,39 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders *********/ /// Construct an instance. /// The full type name to match. - /// A brief noun phrase indicating what the instruction finder matches (or null to generate one). - public TypeFinder(string fullTypeName, string nounPhrase = null) + /// The result to return for matching instructions. + public TypeFinder(string fullTypeName, InstructionHandleResult result) { this.FullTypeName = fullTypeName; - this.NounPhrase = nounPhrase ?? $"{fullTypeName} type"; + this.Result = result; + this.NounPhrase = $"{fullTypeName} type"; } - /// Rewrite a method definition for compatibility. - /// The mod to which the module belongs. - /// The module being rewritten. - /// The method definition to rewrite. + /// Perform the predefined logic for a method if applicable. + /// The mod containing the instruction. + /// The assembly module containing the instruction. + /// The method definition containing the instruction. /// Metadata for mapping assemblies to the current platform. /// Whether the mod was compiled on a different platform. - /// Returns whether the instruction was rewritten. - /// The CIL instruction is not compatible, and can't be rewritten. - 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; } - /// Rewrite a CIL instruction for compatibility. - /// The mod to which the module belongs. - /// The module being rewritten. - /// The CIL rewriter. - /// The instruction to rewrite. + /// Perform the predefined logic for an instruction if applicable. + /// The mod containing the instruction. + /// The assembly module containing the instruction. + /// The CIL processor. + /// The instruction to handle. /// Metadata for mapping assemblies to the current platform. /// Whether the mod was compiled on a different platform. - /// Returns whether the instruction was rewritten. - /// The CIL instruction is not compatible, and can't be rewritten. - 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 { - /// Rewrites CIL instructions for compatibility. - internal interface IInstructionRewriter + /// Performs predefined logic for detected CIL instructions. + internal interface IInstructionHandler { /********* ** Accessors *********/ - /// A brief noun phrase indicating what the rewriter matches. + /// A brief noun phrase indicating what the handler matches. string NounPhrase { get; } /********* ** Methods *********/ - /// Rewrite a method definition for compatibility. - /// The mod to which the module belongs. - /// The module being rewritten. - /// The method definition to rewrite. + /// Perform the predefined logic for a method if applicable. + /// The mod containing the instruction. + /// The assembly module containing the instruction. + /// The method definition containing the instruction. /// Metadata for mapping assemblies to the current platform. /// Whether the mod was compiled on a different platform. - /// Returns whether the instruction was rewritten. - /// The CIL instruction is not compatible, and can't be rewritten. - bool Rewrite(IModMetadata mod, ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged); + InstructionHandleResult Handle(IModMetadata mod, ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged); - /// Rewrite a CIL instruction for compatibility. - /// The mod to which the module belongs. - /// The module being rewritten. - /// The CIL rewriter. - /// The instruction to rewrite. + /// Perform the predefined logic for an instruction if applicable. + /// The mod containing the instruction. + /// The assembly module containing the instruction. + /// The CIL processor. + /// The instruction to handle. /// Metadata for mapping assemblies to the current platform. /// Whether the mod was compiled on a different platform. - /// Returns whether the instruction was rewritten. - /// The CIL instruction is not compatible, and can't be rewritten. - 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 +{ + /// Indicates how an instruction was handled. + internal enum InstructionHandleResult + { + /// No special handling is needed. + None, + + /// The instruction was successfully rewritten for compatibility. + Rewritten, + + /// The instruction is not compatible and can't be rewritten for compatibility. + 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 /// The type whose field to which references should be rewritten. /// The field name to rewrite. /// The new field name to reference. - /// A brief noun phrase indicating what the instruction finder matches (or null to generate one). - 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."); } - /// Rewrite a CIL instruction for compatibility. - /// The mod to which the module belongs. - /// The module being rewritten. - /// The CIL rewriter. - /// The instruction to rewrite. + /// Perform the predefined logic for an instruction if applicable. + /// The mod containing the instruction. + /// The assembly module containing the instruction. + /// The CIL processor. + /// The instruction to handle. /// Metadata for mapping assemblies to the current platform. /// Whether the mod was compiled on a different platform. - /// Returns whether the instruction was rewritten. - /// The CIL instruction is not compatible, and can't be rewritten. - 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 /// Construct an instance. /// The type whose field to which references should be rewritten. /// The field name to rewrite. - /// A brief noun phrase indicating what the instruction finder matches (or null to generate one). - 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; } - /// Rewrite a CIL instruction for compatibility. - /// The mod to which the module belongs. - /// The module being rewritten. - /// The CIL rewriter. - /// The instruction to rewrite. + /// Perform the predefined logic for an instruction if applicable. + /// The mod containing the instruction. + /// The assembly module containing the instruction. + /// The CIL processor. + /// The instruction to handle. /// Metadata for mapping assemblies to the current platform. /// Whether the mod was compiled on a different platform. - /// Returns whether the instruction was rewritten. - /// The CIL instruction is not compatible, and can't be rewritten. - 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 { /// Rewrites method references from one parent type to another if the signatures match. - internal class MethodParentRewriter : IInstructionRewriter + internal class MethodParentRewriter : IInstructionHandler { /********* ** Properties @@ -34,45 +34,40 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters /// The type whose methods to remap. /// The type with methods to map to. /// Whether to only rewrite references if loading the assembly on a different platform than it was compiled on. - /// A brief noun phrase indicating what the instruction finder matches (or null to generate one). - 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; } - /// Rewrite a method definition for compatibility. - /// The mod to which the module belongs. - /// The module being rewritten. - /// The method definition to rewrite. + /// Perform the predefined logic for a method if applicable. + /// The mod containing the instruction. + /// The assembly module containing the instruction. + /// The method definition containing the instruction. /// Metadata for mapping assemblies to the current platform. /// Whether the mod was compiled on a different platform. - /// Returns whether the instruction was rewritten. - /// The CIL instruction is not compatible, and can't be rewritten. - 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; } - /// Rewrite a CIL instruction for compatibility. - /// The mod to which the module belongs. - /// The module being rewritten. - /// The CIL rewriter. - /// The instruction to rewrite. + /// Perform the predefined logic for an instruction if applicable. + /// The mod containing the instruction. + /// The assembly module containing the instruction. + /// The CIL processor. + /// The instruction to handle. /// Metadata for mapping assemblies to the current platform. /// Whether the mod was compiled on a different platform. - /// Returns whether the instruction was rewritten. - /// The CIL instruction is not compatible, and can't be rewritten. - 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 /// Construct an instance. /// The full type name to which to find references. /// The new type to reference. - /// A brief noun phrase indicating what the instruction finder matches (or null to generate one). - 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; } - /// Rewrite a method definition for compatibility. - /// The mod to which the module belongs. - /// The module being rewritten. - /// The method definition to rewrite. + /// Perform the predefined logic for a method if applicable. + /// The mod containing the instruction. + /// The assembly module containing the instruction. + /// The method definition containing the instruction. /// Metadata for mapping assemblies to the current platform. /// Whether the mod was compiled on a different platform. - /// Returns whether the instruction was rewritten. - /// The CIL instruction is not compatible, and can't be rewritten. - 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; } - /// Rewrite a CIL instruction for compatibility. - /// The mod to which the module belongs. - /// The module being rewritten. - /// The CIL rewriter. - /// The instruction to rewrite. + /// Perform the predefined logic for an instruction if applicable. + /// The mod containing the instruction. + /// The assembly module containing the instruction. + /// The CIL processor. + /// The instruction to handle. /// Metadata for mapping assemblies to the current platform. /// Whether the mod was compiled on a different platform. - /// Returns whether the instruction was rewritten. - /// The CIL instruction is not compatible, and can't be rewritten. - 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 *********/ /// Get the adjusted type reference if it matches, else the same value. - /// The module being rewritten. + /// The assembly module containing the instruction. /// The type to replace if it matches. private TypeReference RewriteIfNeeded(ModuleDefinition module, TypeReference type) { -- cgit