summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/ModLoading
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2020-05-19 20:57:50 -0400
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2020-05-19 20:57:50 -0400
commit1838842bbc2db2d1049c193b8650bd101ba4858f (patch)
tree650c97b1d00091c53869323307705e78b3766275 /src/SMAPI/Framework/ModLoading
parentf96dde00f98a913557617f716673f1af355cc6b5 (diff)
downloadSMAPI-1838842bbc2db2d1049c193b8650bd101ba4858f.tar.gz
SMAPI-1838842bbc2db2d1049c193b8650bd101ba4858f.tar.bz2
SMAPI-1838842bbc2db2d1049c193b8650bd101ba4858f.zip
rewrite assembly rewriting, merge Harmony rewriters (#711)
This reduces duplication, decouples it from the assembly loader, and makes it more flexible to handle Harmony rewriting.
Diffstat (limited to 'src/SMAPI/Framework/ModLoading')
-rw-r--r--src/SMAPI/Framework/ModLoading/AssemblyLoader.cs100
-rw-r--r--src/SMAPI/Framework/ModLoading/Finders/EventFinder.cs18
-rw-r--r--src/SMAPI/Framework/ModLoading/Finders/FieldFinder.cs31
-rw-r--r--src/SMAPI/Framework/ModLoading/Finders/MethodFinder.cs18
-rw-r--r--src/SMAPI/Framework/ModLoading/Finders/PropertyFinder.cs18
-rw-r--r--src/SMAPI/Framework/ModLoading/Finders/ReferenceToMemberWithUnexpectedTypeFinder.cs27
-rw-r--r--src/SMAPI/Framework/ModLoading/Finders/ReferenceToMissingMemberFinder.cs28
-rw-r--r--src/SMAPI/Framework/ModLoading/Finders/TypeAssemblyFinder.cs42
-rw-r--r--src/SMAPI/Framework/ModLoading/Finders/TypeFinder.cs42
-rw-r--r--src/SMAPI/Framework/ModLoading/Framework/BaseInstructionHandler.cs66
-rw-r--r--src/SMAPI/Framework/ModLoading/Framework/BaseTypeFinder.cs172
-rw-r--r--src/SMAPI/Framework/ModLoading/Framework/BaseTypeReferenceRewriter.cs209
-rw-r--r--src/SMAPI/Framework/ModLoading/Framework/RecursiveRewriter.cs260
-rw-r--r--src/SMAPI/Framework/ModLoading/Framework/RewriteHelper.cs (renamed from src/SMAPI/Framework/ModLoading/RewriteHelper.cs)24
-rw-r--r--src/SMAPI/Framework/ModLoading/IInstructionHandler.cs35
-rw-r--r--src/SMAPI/Framework/ModLoading/Rewriters/FieldReplaceRewriter.cs37
-rw-r--r--src/SMAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs42
-rw-r--r--src/SMAPI/Framework/ModLoading/Rewriters/Harmony1AssemblyRewriter.cs107
-rw-r--r--src/SMAPI/Framework/ModLoading/Rewriters/MethodParentRewriter.cs40
-rw-r--r--src/SMAPI/Framework/ModLoading/Rewriters/StaticFieldToConstantRewriter.cs35
-rw-r--r--src/SMAPI/Framework/ModLoading/Rewriters/TypeReferenceRewriter.cs50
21 files changed, 699 insertions, 702 deletions
diff --git a/src/SMAPI/Framework/ModLoading/AssemblyLoader.cs b/src/SMAPI/Framework/ModLoading/AssemblyLoader.cs
index b95a45b5..d9b4af1b 100644
--- a/src/SMAPI/Framework/ModLoading/AssemblyLoader.cs
+++ b/src/SMAPI/Framework/ModLoading/AssemblyLoader.cs
@@ -4,8 +4,8 @@ using System.IO;
using System.Linq;
using System.Reflection;
using Mono.Cecil;
-using Mono.Cecil.Cil;
using StardewModdingAPI.Framework.Exceptions;
+using StardewModdingAPI.Framework.ModLoading.Framework;
using StardewModdingAPI.Metadata;
using StardewModdingAPI.Toolkit.Framework.ModData;
using StardewModdingAPI.Toolkit.Utilities;
@@ -283,54 +283,32 @@ namespace StardewModdingAPI.Framework.ModLoading
this.ChangeTypeScope(type);
}
- // find (and optionally rewrite) incompatible instructions
- bool anyRewritten = false;
- IInstructionHandler[] handlers = new InstructionMetadata().GetHandlers(this.ParanoidMode).ToArray();
- foreach (TypeDefinition type in module.GetTypes())
- {
- // check type definition
- foreach (IInstructionHandler handler in handlers)
+ // find or rewrite code
+ IInstructionHandler[] handlers = new InstructionMetadata().GetHandlers(this.ParanoidMode, platformChanged).ToArray();
+ RecursiveRewriter rewriter = new RecursiveRewriter(
+ module: module,
+ rewriteType: (type, replaceWith) =>
{
- InstructionHandleResult result = handler.Handle(module, type, this.AssemblyMap, platformChanged);
- this.ProcessInstructionHandleResult(mod, handler, result, loggedMessages, logPrefix, filename);
- if (result == InstructionHandleResult.Rewritten)
- anyRewritten = true;
- }
-
- // check methods
- foreach (MethodDefinition method in type.Methods.Where(p => p.HasBody))
+ bool rewritten = false;
+ foreach (IInstructionHandler handler in handlers)
+ rewritten |= handler.Handle(module, type, replaceWith);
+ return rewritten;
+ },
+ rewriteInstruction: (instruction, cil, replaceWith) =>
{
- // check method definition
+ bool rewritten = false;
foreach (IInstructionHandler handler in handlers)
- {
- InstructionHandleResult result = handler.Handle(module, method, this.AssemblyMap, platformChanged);
- this.ProcessInstructionHandleResult(mod, handler, result, loggedMessages, logPrefix, filename);
- if (result == InstructionHandleResult.Rewritten)
- anyRewritten = true;
- }
-
- // check CIL instructions
- ILProcessor cil = method.Body.GetILProcessor();
- var instructions = cil.Body.Instructions;
- // ReSharper disable once ForCanBeConvertedToForeach -- deliberate access by index so each handler sees replacements from previous handlers
- for (int offset = 0; offset < instructions.Count; offset++)
- {
- Instruction instruction = instructions[offset];
- if (instruction.OpCode.Code == Code.Nop)
- continue;
-
- foreach (IInstructionHandler handler in handlers)
- {
- InstructionHandleResult result = handler.Handle(module, cil, instruction, this.AssemblyMap, platformChanged);
- this.ProcessInstructionHandleResult(mod, handler, result, loggedMessages, logPrefix, filename);
- if (result == InstructionHandleResult.Rewritten)
- {
- instruction = instructions[offset];
- anyRewritten = true;
- }
- }
- }
+ rewritten |= handler.Handle(module, cil, instruction, replaceWith);
+ return rewritten;
}
+ );
+ bool anyRewritten = rewriter.RewriteModule();
+
+ // handle rewrite flags
+ foreach (IInstructionHandler handler in handlers)
+ {
+ foreach (var flag in handler.Flags)
+ this.ProcessInstructionHandleResult(mod, handler, flag, loggedMessages, logPrefix, filename);
}
return platformChanged || anyRewritten;
@@ -345,49 +323,52 @@ namespace StardewModdingAPI.Framework.ModLoading
/// <param name="filename">The assembly filename for log messages.</param>
private void ProcessInstructionHandleResult(IModMetadata mod, IInstructionHandler handler, InstructionHandleResult result, HashSet<string> loggedMessages, string logPrefix, string filename)
{
+ // get message template
+ // ($phrase is replaced with the noun phrase or messages)
+ string template = null;
switch (result)
{
case InstructionHandleResult.Rewritten:
- this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Rewrote {filename} to fix {handler.NounPhrase}...");
+ template = $"{logPrefix}Rewrote {filename} to fix $phrase...";
break;
case InstructionHandleResult.NotCompatible:
- this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Broken code in {filename}: {handler.NounPhrase}.");
+ template = $"{logPrefix}Broken code in {filename}: $phrase.";
mod.SetWarning(ModWarning.BrokenCodeLoaded);
break;
case InstructionHandleResult.DetectedGamePatch:
- this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Detected game patcher ({handler.NounPhrase}) in assembly {filename}.");
+ template = $"{logPrefix}Detected game patcher ($phrase) in assembly {filename}.";
mod.SetWarning(ModWarning.PatchesGame);
break;
case InstructionHandleResult.DetectedSaveSerializer:
- this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Detected possible save serializer change ({handler.NounPhrase}) in assembly {filename}.");
+ template = $"{logPrefix}Detected possible save serializer change ($phrase) in assembly {filename}.";
mod.SetWarning(ModWarning.ChangesSaveSerializer);
break;
case InstructionHandleResult.DetectedUnvalidatedUpdateTick:
- this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Detected reference to {handler.NounPhrase} in assembly {filename}.");
+ template = $"{logPrefix}Detected reference to $phrase in assembly {filename}.";
mod.SetWarning(ModWarning.UsesUnvalidatedUpdateTick);
break;
case InstructionHandleResult.DetectedDynamic:
- this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Detected 'dynamic' keyword ({handler.NounPhrase}) in assembly {filename}.");
+ template = $"{logPrefix}Detected 'dynamic' keyword ($phrase) in assembly {filename}.";
mod.SetWarning(ModWarning.UsesDynamic);
break;
case InstructionHandleResult.DetectedConsoleAccess:
- this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Detected direct console access ({handler.NounPhrase}) in assembly {filename}.");
+ template = $"{logPrefix}Detected direct console access ($phrase) in assembly {filename}.";
mod.SetWarning(ModWarning.AccessesConsole);
break;
case InstructionHandleResult.DetectedFilesystemAccess:
- this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Detected filesystem access ({handler.NounPhrase}) in assembly {filename}.");
+ template = $"{logPrefix}Detected filesystem access ($phrase) in assembly {filename}.";
mod.SetWarning(ModWarning.AccessesFilesystem);
break;
case InstructionHandleResult.DetectedShellAccess:
- this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Detected shell or process access ({handler.NounPhrase}) in assembly {filename}.");
+ template = $"{logPrefix}Detected shell or process access ($phrase) in assembly {filename}.";
mod.SetWarning(ModWarning.AccessesShell);
break;
@@ -397,6 +378,17 @@ namespace StardewModdingAPI.Framework.ModLoading
default:
throw new NotSupportedException($"Unrecognized instruction handler result '{result}'.");
}
+ if (template == null)
+ return;
+
+ // format messages
+ if (handler.Phrases.Any())
+ {
+ foreach (string message in handler.Phrases)
+ this.Monitor.LogOnce(template.Replace("$phrase", message));
+ }
+ else
+ this.Monitor.LogOnce(template.Replace("$phrase", handler.DefaultPhrase ?? handler.GetType().Name));
}
/// <summary>Get the correct reference to use for compatibility with the current platform.</summary>
diff --git a/src/SMAPI/Framework/ModLoading/Finders/EventFinder.cs b/src/SMAPI/Framework/ModLoading/Finders/EventFinder.cs
index 1a7ae636..e1476b73 100644
--- a/src/SMAPI/Framework/ModLoading/Finders/EventFinder.cs
+++ b/src/SMAPI/Framework/ModLoading/Finders/EventFinder.cs
@@ -1,3 +1,4 @@
+using System;
using Mono.Cecil;
using Mono.Cecil.Cil;
using StardewModdingAPI.Framework.ModLoading.Framework;
@@ -28,24 +29,25 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders
/// <param name="eventName">The event name for which to find references.</param>
/// <param name="result">The result to return for matching instructions.</param>
public EventFinder(string fullTypeName, string eventName, InstructionHandleResult result)
- : base(nounPhrase: $"{fullTypeName}.{eventName} event")
+ : base(defaultPhrase: $"{fullTypeName}.{eventName} event")
{
this.FullTypeName = fullTypeName;
this.EventName = eventName;
this.Result = result;
}
- /// <summary>Perform the predefined logic for an instruction if applicable.</summary>
+ /// <summary>Rewrite a CIL instruction reference if needed.</summary>
/// <param name="module">The assembly module containing the instruction.</param>
/// <param name="cil">The CIL processor.</param>
/// <param name="instruction">The CIL 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>
- public override InstructionHandleResult Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
+ /// <param name="replaceWith">Replaces the CIL instruction with a new one.</param>
+ /// <returns>Returns whether the instruction was changed.</returns>
+ public override bool Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, Action<Instruction> replaceWith)
{
- return this.IsMatch(instruction)
- ? this.Result
- : InstructionHandleResult.None;
+ if (!this.Flags.Contains(this.Result) && this.IsMatch(instruction))
+ this.MarkFlag(this.Result);
+
+ return false;
}
diff --git a/src/SMAPI/Framework/ModLoading/Finders/FieldFinder.cs b/src/SMAPI/Framework/ModLoading/Finders/FieldFinder.cs
index 9ae07916..c157ed9b 100644
--- a/src/SMAPI/Framework/ModLoading/Finders/FieldFinder.cs
+++ b/src/SMAPI/Framework/ModLoading/Finders/FieldFinder.cs
@@ -1,3 +1,4 @@
+using System;
using Mono.Cecil;
using Mono.Cecil.Cil;
using StardewModdingAPI.Framework.ModLoading.Framework;
@@ -28,39 +29,25 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders
/// <param name="fieldName">The field name for which to find references.</param>
/// <param name="result">The result to return for matching instructions.</param>
public FieldFinder(string fullTypeName, string fieldName, InstructionHandleResult result)
- : base(nounPhrase: $"{fullTypeName}.{fieldName} field")
+ : base(defaultPhrase: $"{fullTypeName}.{fieldName} field")
{
this.FullTypeName = fullTypeName;
this.FieldName = fieldName;
this.Result = result;
}
- /// <summary>Perform the predefined logic for an instruction if applicable.</summary>
+ /// <summary>Rewrite a CIL instruction reference if needed.</summary>
/// <param name="module">The assembly module containing the instruction.</param>
/// <param name="cil">The CIL processor.</param>
/// <param name="instruction">The CIL 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>
- public override InstructionHandleResult Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
+ /// <param name="replaceWith">Replaces the CIL instruction with a new one.</param>
+ /// <returns>Returns whether the instruction was changed.</returns>
+ public override bool Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, Action<Instruction> replaceWith)
{
- return this.IsMatch(instruction)
- ? this.Result
- : InstructionHandleResult.None;
- }
-
+ if (!this.Flags.Contains(this.Result) && RewriteHelper.IsFieldReferenceTo(instruction, this.FullTypeName, this.FieldName))
+ this.MarkFlag(this.Result);
- /*********
- ** Protected methods
- *********/
- /// <summary>Get whether a CIL instruction matches.</summary>
- /// <param name="instruction">The IL instruction.</param>
- protected bool IsMatch(Instruction instruction)
- {
- FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction);
- return
- fieldRef != null
- && fieldRef.DeclaringType.FullName == this.FullTypeName
- && fieldRef.Name == this.FieldName;
+ return false;
}
}
}
diff --git a/src/SMAPI/Framework/ModLoading/Finders/MethodFinder.cs b/src/SMAPI/Framework/ModLoading/Finders/MethodFinder.cs
index 75584f1f..82c93a7c 100644
--- a/src/SMAPI/Framework/ModLoading/Finders/MethodFinder.cs
+++ b/src/SMAPI/Framework/ModLoading/Finders/MethodFinder.cs
@@ -1,3 +1,4 @@
+using System;
using Mono.Cecil;
using Mono.Cecil.Cil;
using StardewModdingAPI.Framework.ModLoading.Framework;
@@ -28,24 +29,25 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders
/// <param name="methodName">The method name for which to find references.</param>
/// <param name="result">The result to return for matching instructions.</param>
public MethodFinder(string fullTypeName, string methodName, InstructionHandleResult result)
- : base(nounPhrase: $"{fullTypeName}.{methodName} method")
+ : base(defaultPhrase: $"{fullTypeName}.{methodName} method")
{
this.FullTypeName = fullTypeName;
this.MethodName = methodName;
this.Result = result;
}
- /// <summary>Perform the predefined logic for an instruction if applicable.</summary>
+ /// <summary>Rewrite a CIL instruction reference if needed.</summary>
/// <param name="module">The assembly module containing the instruction.</param>
/// <param name="cil">The CIL processor.</param>
/// <param name="instruction">The CIL 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>
- public override InstructionHandleResult Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
+ /// <param name="replaceWith">Replaces the CIL instruction with a new one.</param>
+ /// <returns>Returns whether the instruction was changed.</returns>
+ public override bool Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, Action<Instruction> replaceWith)
{
- return this.IsMatch(instruction)
- ? this.Result
- : InstructionHandleResult.None;
+ if (!this.Flags.Contains(this.Result) && this.IsMatch(instruction))
+ this.MarkFlag(this.Result);
+
+ return false;
}
diff --git a/src/SMAPI/Framework/ModLoading/Finders/PropertyFinder.cs b/src/SMAPI/Framework/ModLoading/Finders/PropertyFinder.cs
index 811420c5..c96d61a2 100644
--- a/src/SMAPI/Framework/ModLoading/Finders/PropertyFinder.cs
+++ b/src/SMAPI/Framework/ModLoading/Finders/PropertyFinder.cs
@@ -1,3 +1,4 @@
+using System;
using Mono.Cecil;
using Mono.Cecil.Cil;
using StardewModdingAPI.Framework.ModLoading.Framework;
@@ -28,24 +29,25 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders
/// <param name="propertyName">The property name for which to find references.</param>
/// <param name="result">The result to return for matching instructions.</param>
public PropertyFinder(string fullTypeName, string propertyName, InstructionHandleResult result)
- : base(nounPhrase: $"{fullTypeName}.{propertyName} property")
+ : base(defaultPhrase: $"{fullTypeName}.{propertyName} property")
{
this.FullTypeName = fullTypeName;
this.PropertyName = propertyName;
this.Result = result;
}
- /// <summary>Perform the predefined logic for an instruction if applicable.</summary>
+ /// <summary>Rewrite a CIL instruction reference if needed.</summary>
/// <param name="module">The assembly module containing the instruction.</param>
/// <param name="cil">The CIL processor.</param>
/// <param name="instruction">The CIL 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>
- public override InstructionHandleResult Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
+ /// <param name="replaceWith">Replaces the CIL instruction with a new one.</param>
+ /// <returns>Returns whether the instruction was changed.</returns>
+ public override bool Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, Action<Instruction> replaceWith)
{
- return this.IsMatch(instruction)
- ? this.Result
- : InstructionHandleResult.None;
+ if (!this.Flags.Contains(this.Result) && this.IsMatch(instruction))
+ this.MarkFlag(this.Result);
+
+ return false;
}
diff --git a/src/SMAPI/Framework/ModLoading/Finders/ReferenceToMemberWithUnexpectedTypeFinder.cs b/src/SMAPI/Framework/ModLoading/Finders/ReferenceToMemberWithUnexpectedTypeFinder.cs
index 1029d350..a67cfa4f 100644
--- a/src/SMAPI/Framework/ModLoading/Finders/ReferenceToMemberWithUnexpectedTypeFinder.cs
+++ b/src/SMAPI/Framework/ModLoading/Finders/ReferenceToMemberWithUnexpectedTypeFinder.cs
@@ -1,3 +1,4 @@
+using System;
using System.Collections.Generic;
using System.Linq;
using Mono.Cecil;
@@ -23,18 +24,18 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders
/// <summary>Construct an instance.</summary>
/// <param name="validateReferencesToAssemblies">The assembly names to which to heuristically detect broken references.</param>
public ReferenceToMemberWithUnexpectedTypeFinder(string[] validateReferencesToAssemblies)
- : base(nounPhrase: "")
+ : base(defaultPhrase: "")
{
this.ValidateReferencesToAssemblies = new HashSet<string>(validateReferencesToAssemblies);
}
- /// <summary>Perform the predefined logic for an instruction if applicable.</summary>
+ /// <summary>Rewrite a CIL instruction reference if needed.</summary>
/// <param name="module">The assembly module containing the instruction.</param>
/// <param name="cil">The CIL processor.</param>
/// <param name="instruction">The CIL 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>
- public override InstructionHandleResult Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
+ /// <param name="replaceWith">Replaces the CIL instruction with a new one.</param>
+ /// <returns>Returns whether the instruction was changed.</returns>
+ public override bool Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, Action<Instruction> replaceWith)
{
// field reference
FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction);
@@ -43,13 +44,13 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders
// get target field
FieldDefinition targetField = fieldRef.DeclaringType.Resolve()?.Fields.FirstOrDefault(p => p.Name == fieldRef.Name);
if (targetField == null)
- return InstructionHandleResult.None;
+ return false;
// validate return type
if (!RewriteHelper.LooksLikeSameType(fieldRef.FieldType, targetField.FieldType))
{
- this.NounPhrase = $"reference to {fieldRef.DeclaringType.FullName}.{fieldRef.Name} (field returns {this.GetFriendlyTypeName(targetField.FieldType)}, not {this.GetFriendlyTypeName(fieldRef.FieldType)})";
- return InstructionHandleResult.NotCompatible;
+ this.MarkFlag(InstructionHandleResult.NotCompatible, $"reference to {fieldRef.DeclaringType.FullName}.{fieldRef.Name} (field returns {this.GetFriendlyTypeName(targetField.FieldType)}, not {this.GetFriendlyTypeName(fieldRef.FieldType)})");
+ return false;
}
}
@@ -60,21 +61,21 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders
// get potential targets
MethodDefinition[] candidateMethods = methodReference.DeclaringType.Resolve()?.Methods.Where(found => found.Name == methodReference.Name).ToArray();
if (candidateMethods == null || !candidateMethods.Any())
- return InstructionHandleResult.None;
+ return false;
// compare return types
MethodDefinition methodDef = methodReference.Resolve();
if (methodDef == null)
- return InstructionHandleResult.None; // validated by ReferenceToMissingMemberFinder
+ return false; // validated by ReferenceToMissingMemberFinder
if (candidateMethods.All(method => !RewriteHelper.LooksLikeSameType(method.ReturnType, methodDef.ReturnType)))
{
- this.NounPhrase = $"reference to {methodDef.DeclaringType.FullName}.{methodDef.Name} (no such method returns {this.GetFriendlyTypeName(methodDef.ReturnType)})";
- return InstructionHandleResult.NotCompatible;
+ this.MarkFlag(InstructionHandleResult.NotCompatible, $"reference to {methodDef.DeclaringType.FullName}.{methodDef.Name} (no such method returns {this.GetFriendlyTypeName(methodDef.ReturnType)})");
+ return false;
}
}
- return InstructionHandleResult.None;
+ return false;
}
diff --git a/src/SMAPI/Framework/ModLoading/Finders/ReferenceToMissingMemberFinder.cs b/src/SMAPI/Framework/ModLoading/Finders/ReferenceToMissingMemberFinder.cs
index fefa88f4..ebb62948 100644
--- a/src/SMAPI/Framework/ModLoading/Finders/ReferenceToMissingMemberFinder.cs
+++ b/src/SMAPI/Framework/ModLoading/Finders/ReferenceToMissingMemberFinder.cs
@@ -1,3 +1,4 @@
+using System;
using System.Collections.Generic;
using System.Linq;
using Mono.Cecil;
@@ -23,18 +24,18 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders
/// <summary>Construct an instance.</summary>
/// <param name="validateReferencesToAssemblies">The assembly names to which to heuristically detect broken references.</param>
public ReferenceToMissingMemberFinder(string[] validateReferencesToAssemblies)
- : base(nounPhrase: "")
+ : base(defaultPhrase: "")
{
this.ValidateReferencesToAssemblies = new HashSet<string>(validateReferencesToAssemblies);
}
- /// <summary>Perform the predefined logic for an instruction if applicable.</summary>
+ /// <summary>Rewrite a CIL instruction reference if needed.</summary>
/// <param name="module">The assembly module containing the instruction.</param>
/// <param name="cil">The CIL processor.</param>
/// <param name="instruction">The CIL 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>
- public override InstructionHandleResult Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
+ /// <param name="replaceWith">Replaces the CIL instruction with a new one.</param>
+ /// <returns>Returns whether the instruction was changed.</returns>
+ public override bool Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, Action<Instruction> replaceWith)
{
// field reference
FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction);
@@ -43,8 +44,8 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders
FieldDefinition target = fieldRef.DeclaringType.Resolve()?.Fields.FirstOrDefault(p => p.Name == fieldRef.Name);
if (target == null)
{
- this.NounPhrase = $"reference to {fieldRef.DeclaringType.FullName}.{fieldRef.Name} (no such field)";
- return InstructionHandleResult.NotCompatible;
+ this.MarkFlag(InstructionHandleResult.NotCompatible, $"reference to {fieldRef.DeclaringType.FullName}.{fieldRef.Name} (no such field)");
+ return false;
}
}
@@ -55,17 +56,20 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders
MethodDefinition target = methodRef.Resolve();
if (target == null)
{
+ string phrase = null;
if (this.IsProperty(methodRef))
- this.NounPhrase = $"reference to {methodRef.DeclaringType.FullName}.{methodRef.Name.Substring(4)} (no such property)";
+ phrase = $"reference to {methodRef.DeclaringType.FullName}.{methodRef.Name.Substring(4)} (no such property)";
else if (methodRef.Name == ".ctor")
- this.NounPhrase = $"reference to {methodRef.DeclaringType.FullName}.{methodRef.Name} (no matching constructor)";
+ phrase = $"reference to {methodRef.DeclaringType.FullName}.{methodRef.Name} (no matching constructor)";
else
- this.NounPhrase = $"reference to {methodRef.DeclaringType.FullName}.{methodRef.Name} (no such method)";
- return InstructionHandleResult.NotCompatible;
+ phrase = $"reference to {methodRef.DeclaringType.FullName}.{methodRef.Name} (no such method)";
+
+ this.MarkFlag(InstructionHandleResult.NotCompatible, phrase);
+ return false;
}
}
- return InstructionHandleResult.None;
+ return false;
}
diff --git a/src/SMAPI/Framework/ModLoading/Finders/TypeAssemblyFinder.cs b/src/SMAPI/Framework/ModLoading/Finders/TypeAssemblyFinder.cs
index 5301186b..a1ade536 100644
--- a/src/SMAPI/Framework/ModLoading/Finders/TypeAssemblyFinder.cs
+++ b/src/SMAPI/Framework/ModLoading/Finders/TypeAssemblyFinder.cs
@@ -5,21 +5,47 @@ using StardewModdingAPI.Framework.ModLoading.Framework;
namespace StardewModdingAPI.Framework.ModLoading.Finders
{
/// <summary>Finds incompatible CIL instructions that reference types in a given assembly.</summary>
- internal class TypeAssemblyFinder : BaseTypeFinder
+ internal class TypeAssemblyFinder : BaseInstructionHandler
{
/*********
+ ** Fields
+ *********/
+ /// <summary>The full assembly name to which to find references.</summary>
+ private readonly string AssemblyName;
+
+ /// <summary>The result to return for matching instructions.</summary>
+ private readonly InstructionHandleResult Result;
+
+ /// <summary>Get whether a matched type should be ignored.</summary>
+ private readonly Func<TypeReference, bool> ShouldIgnore;
+
+
+ /*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="assemblyName">The full assembly name to which to find references.</param>
/// <param name="result">The result to return for matching instructions.</param>
- /// <param name="shouldIgnore">A lambda which overrides a matched type.</param>
+ /// <param name="shouldIgnore">Get whether a matched type should be ignored.</param>
public TypeAssemblyFinder(string assemblyName, InstructionHandleResult result, Func<TypeReference, bool> shouldIgnore = null)
- : base(
- isMatch: type => type.Scope.Name == assemblyName && (shouldIgnore == null || !shouldIgnore(type)),
- result: result,
- nounPhrase: $"{assemblyName} assembly"
- )
- { }
+ : base(defaultPhrase: $"{assemblyName} assembly")
+ {
+ this.AssemblyName = assemblyName;
+ this.Result = result;
+ this.ShouldIgnore = shouldIgnore;
+ }
+
+ /// <summary>Rewrite a type reference if needed.</summary>
+ /// <param name="module">The assembly module containing the instruction.</param>
+ /// <param name="type">The type definition to handle.</param>
+ /// <param name="replaceWith">Replaces the type reference with a new one.</param>
+ /// <returns>Returns whether the type was changed.</returns>
+ public override bool Handle(ModuleDefinition module, TypeReference type, Action<TypeReference> replaceWith)
+ {
+ if (type.Scope.Name == this.AssemblyName && this.ShouldIgnore?.Invoke(type) != true)
+ this.MarkFlag(this.Result);
+
+ return false;
+ }
}
}
diff --git a/src/SMAPI/Fr