diff options
Diffstat (limited to 'src/StardewModdingAPI/Framework')
| -rw-r--r-- | src/StardewModdingAPI/Framework/AssemblyLoader.cs | 18 | ||||
| -rw-r--r-- | src/StardewModdingAPI/Framework/IncompatibleInstructionException.cs | 27 |
2 files changed, 45 insertions, 0 deletions
diff --git a/src/StardewModdingAPI/Framework/AssemblyLoader.cs b/src/StardewModdingAPI/Framework/AssemblyLoader.cs index 8af67772..dfe0d03f 100644 --- a/src/StardewModdingAPI/Framework/AssemblyLoader.cs +++ b/src/StardewModdingAPI/Framework/AssemblyLoader.cs @@ -55,6 +55,7 @@ namespace StardewModdingAPI.Framework /// <summary>Preprocess and load an assembly.</summary> /// <param name="assemblyPath">The assembly file path.</param> /// <returns>Returns the rewrite metadata for the preprocessed assembly.</returns> + /// <exception cref="IncompatibleInstructionException">An incompatible CIL instruction was found while rewriting the assembly.</exception> public Assembly Load(string assemblyPath) { // get referenced local assemblies @@ -159,6 +160,7 @@ namespace StardewModdingAPI.Framework /// <summary>Rewrite the types referenced by an assembly.</summary> /// <param name="assembly">The assembly to rewrite.</param> /// <returns>Returns whether the assembly was modified.</returns> + /// <exception cref="IncompatibleInstructionException">An incompatible CIL instruction was found while rewriting the assembly.</exception> private bool RewriteAssembly(AssemblyDefinition assembly) { ModuleDefinition module = assembly.MainModule; @@ -189,6 +191,22 @@ namespace StardewModdingAPI.Framework this.ChangeTypeScope(type); } + // throw exception if assembly contains incompatible instructions can't be rewritten + { + IInstructionFinder[] finders = Constants.GetIncompatibilityFinders().ToArray(); + foreach (MethodDefinition method in this.GetMethods(module)) + { + foreach (Instruction instruction in method.Body.Instructions) + { + foreach (IInstructionFinder finder in finders) + { + if (finder.IsMatch(instruction, platformChanged)) + throw new IncompatibleInstructionException(finder.NounPhrase, $"Found an incompatible CIL instruction ({finder.NounPhrase}) while loading assembly {assembly.Name.Name}."); + } + } + } + } + // rewrite incompatible instructions bool anyRewritten = false; IInstructionRewriter[] rewriters = Constants.GetRewriters().ToArray(); diff --git a/src/StardewModdingAPI/Framework/IncompatibleInstructionException.cs b/src/StardewModdingAPI/Framework/IncompatibleInstructionException.cs new file mode 100644 index 00000000..affe2cb3 --- /dev/null +++ b/src/StardewModdingAPI/Framework/IncompatibleInstructionException.cs @@ -0,0 +1,27 @@ +using System; + +namespace StardewModdingAPI.Framework +{ + /// <summary>An exception raised when an incompatible instruction is found while loading a mod assembly.</summary> + internal class IncompatibleInstructionException : Exception + { + /********* + ** Accessors + *********/ + /// <summary>A brief noun phrase which describes the incompatible instruction that was found.</summary> + public string NounPhrase { get; } + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="nounPhrase">A brief noun phrase which describes the incompatible instruction that was found.</param> + /// <param name="message">A message which describes the error.</param> + public IncompatibleInstructionException(string nounPhrase, string message) + : base(message) + { + this.NounPhrase = nounPhrase; + } + } +} |
