diff options
Diffstat (limited to 'src/StardewModdingAPI/Framework/ModLoading/Finders')
5 files changed, 122 insertions, 128 deletions
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; } |