From 8c5bd12f4793a9c866b6046c05482592a5c799bc Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Tue, 19 Sep 2017 22:45:28 -0400 Subject: merge assembly rewriters into main SMAPI project (#347) --- .../Finders/EventFinder.cs | 83 ----------- .../Finders/FieldFinder.cs | 83 ----------- .../Finders/MethodFinder.cs | 83 ----------- .../Finders/PropertyFinder.cs | 83 ----------- .../Finders/TypeFinder.cs | 135 ------------------ .../IInstructionRewriter.cs | 38 ----- .../IncompatibleInstructionException.cs | 35 ----- .../Platform.cs | 12 -- .../PlatformAssemblyMap.cs | 55 -------- .../Properties/AssemblyInfo.cs | 7 - .../RewriteHelper.cs | 94 ------------ .../Rewriters/FieldReplaceRewriter.cs | 53 ------- .../Rewriters/FieldToPropertyRewriter.cs | 54 ------- .../Rewriters/MethodParentRewriter.cs | 93 ------------ .../Rewriters/TypeReferenceRewriter.cs | 157 --------------------- .../Rewriters/Wrappers/SpriteBatchWrapper.cs | 59 -------- .../StardewModdingAPI.AssemblyRewriters.csproj | 73 ---------- .../packages.config | 4 - .../InteractiveInstaller.cs | 6 +- src/StardewModdingAPI.sln | 14 +- src/StardewModdingAPI/Constants.cs | 8 +- .../Framework/ModLoading/AssemblyLoader.cs | 4 +- .../Framework/ModLoading/Finders/EventFinder.cs | 83 +++++++++++ .../Framework/ModLoading/Finders/FieldFinder.cs | 83 +++++++++++ .../Framework/ModLoading/Finders/MethodFinder.cs | 83 +++++++++++ .../Framework/ModLoading/Finders/PropertyFinder.cs | 83 +++++++++++ .../Framework/ModLoading/Finders/TypeFinder.cs | 135 ++++++++++++++++++ .../Framework/ModLoading/IInstructionRewriter.cs | 38 +++++ .../ModLoading/IncompatibleInstructionException.cs | 35 +++++ .../ModLoading/InvalidModStateException.cs | 4 +- .../Framework/ModLoading/ModMetadataStatus.cs | 4 +- .../Framework/ModLoading/Platform.cs | 12 ++ .../Framework/ModLoading/PlatformAssemblyMap.cs | 55 ++++++++ .../Framework/ModLoading/RewriteHelper.cs | 94 ++++++++++++ .../ModLoading/Rewriters/FieldReplaceRewriter.cs | 53 +++++++ .../Rewriters/FieldToPropertyRewriter.cs | 54 +++++++ .../ModLoading/Rewriters/MethodParentRewriter.cs | 93 ++++++++++++ .../ModLoading/Rewriters/TypeReferenceRewriter.cs | 157 +++++++++++++++++++++ .../Rewriters/Wrappers/SpriteBatchWrapper.cs | 59 ++++++++ src/StardewModdingAPI/Framework/SContentManager.cs | 2 +- src/StardewModdingAPI/Program.cs | 1 - src/StardewModdingAPI/StardewModdingAPI.csproj | 22 ++- src/common.targets | 1 - src/prepare-install-package.targets | 2 - 44 files changed, 1147 insertions(+), 1239 deletions(-) delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/Finders/EventFinder.cs delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/Finders/FieldFinder.cs delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/Finders/MethodFinder.cs delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/Finders/PropertyFinder.cs delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/Finders/TypeFinder.cs delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/IInstructionRewriter.cs delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/IncompatibleInstructionException.cs delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/Platform.cs delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/PlatformAssemblyMap.cs delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/Properties/AssemblyInfo.cs delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/RewriteHelper.cs delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldReplaceRewriter.cs delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldToPropertyRewriter.cs delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/Rewriters/MethodParentRewriter.cs delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/Rewriters/TypeReferenceRewriter.cs delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/Rewriters/Wrappers/SpriteBatchWrapper.cs delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/packages.config create mode 100644 src/StardewModdingAPI/Framework/ModLoading/Finders/EventFinder.cs create mode 100644 src/StardewModdingAPI/Framework/ModLoading/Finders/FieldFinder.cs create mode 100644 src/StardewModdingAPI/Framework/ModLoading/Finders/MethodFinder.cs create mode 100644 src/StardewModdingAPI/Framework/ModLoading/Finders/PropertyFinder.cs create mode 100644 src/StardewModdingAPI/Framework/ModLoading/Finders/TypeFinder.cs create mode 100644 src/StardewModdingAPI/Framework/ModLoading/IInstructionRewriter.cs create mode 100644 src/StardewModdingAPI/Framework/ModLoading/IncompatibleInstructionException.cs create mode 100644 src/StardewModdingAPI/Framework/ModLoading/Platform.cs create mode 100644 src/StardewModdingAPI/Framework/ModLoading/PlatformAssemblyMap.cs create mode 100644 src/StardewModdingAPI/Framework/ModLoading/RewriteHelper.cs create mode 100644 src/StardewModdingAPI/Framework/ModLoading/Rewriters/FieldReplaceRewriter.cs create mode 100644 src/StardewModdingAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs create mode 100644 src/StardewModdingAPI/Framework/ModLoading/Rewriters/MethodParentRewriter.cs create mode 100644 src/StardewModdingAPI/Framework/ModLoading/Rewriters/TypeReferenceRewriter.cs create mode 100644 src/StardewModdingAPI/Framework/ModLoading/Rewriters/Wrappers/SpriteBatchWrapper.cs (limited to 'src') diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/EventFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/EventFinder.cs deleted file mode 100644 index c0051469..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/Finders/EventFinder.cs +++ /dev/null @@ -1,83 +0,0 @@ -using Mono.Cecil; -using Mono.Cecil.Cil; - -namespace StardewModdingAPI.AssemblyRewriters.Finders -{ - /// Finds incompatible CIL instructions that reference a given event and throws an . - public class EventFinder : IInstructionRewriter - { - /********* - ** Properties - *********/ - /// The full type name for which to find references. - private readonly string FullTypeName; - - /// The event name for which to find references. - private readonly string EventName; - - - /********* - ** Accessors - *********/ - /// A brief noun phrase indicating what the instruction finder matches. - public string NounPhrase { get; } - - - /********* - ** Public methods - *********/ - /// 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) - { - this.FullTypeName = fullTypeName; - this.EventName = eventName; - this.NounPhrase = nounPhrase ?? $"{fullTypeName}.{eventName} event"; - } - - /// Rewrite a method definition for compatibility. - /// The module being rewritten. - /// The method definition to rewrite. - /// 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(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) - { - return false; - } - - /// Rewrite a CIL instruction for compatibility. - /// The module being rewritten. - /// The CIL rewriter. - /// The instruction to rewrite. - /// 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(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) - { - if (!this.IsMatch(instruction)) - return false; - - throw new IncompatibleInstructionException(this.NounPhrase); - } - - - /********* - ** Protected methods - *********/ - /// Get whether a CIL instruction matches. - /// The IL instruction. - protected bool IsMatch(Instruction instruction) - { - MethodReference methodRef = RewriteHelper.AsMethodReference(instruction); - return - methodRef != null - && methodRef.DeclaringType.FullName == this.FullTypeName - && (methodRef.Name == "add_" + this.EventName || methodRef.Name == "remove_" + this.EventName); - } - } -} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/FieldFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/FieldFinder.cs deleted file mode 100644 index b44883e9..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/Finders/FieldFinder.cs +++ /dev/null @@ -1,83 +0,0 @@ -using Mono.Cecil; -using Mono.Cecil.Cil; - -namespace StardewModdingAPI.AssemblyRewriters.Finders -{ - /// Finds incompatible CIL instructions that reference a given field and throws an . - public class FieldFinder : IInstructionRewriter - { - /********* - ** Properties - *********/ - /// The full type name for which to find references. - private readonly string FullTypeName; - - /// The field name for which to find references. - private readonly string FieldName; - - - /********* - ** Accessors - *********/ - /// A brief noun phrase indicating what the instruction finder matches. - public string NounPhrase { get; } - - - /********* - ** Public methods - *********/ - /// 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) - { - this.FullTypeName = fullTypeName; - this.FieldName = fieldName; - this.NounPhrase = nounPhrase ?? $"{fullTypeName}.{fieldName} field"; - } - - /// Rewrite a method definition for compatibility. - /// The module being rewritten. - /// The method definition to rewrite. - /// 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(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) - { - return false; - } - - /// Rewrite a CIL instruction for compatibility. - /// The module being rewritten. - /// The CIL rewriter. - /// The instruction to rewrite. - /// 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(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) - { - if (!this.IsMatch(instruction)) - return false; - - throw new IncompatibleInstructionException(this.NounPhrase); - } - - - /********* - ** Protected methods - *********/ - /// Get whether a CIL instruction matches. - /// The IL instruction. - protected bool IsMatch(Instruction instruction) - { - FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction); - return - fieldRef != null - && fieldRef.DeclaringType.FullName == this.FullTypeName - && fieldRef.Name == this.FieldName; - } - } -} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/MethodFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/MethodFinder.cs deleted file mode 100644 index 19dda58a..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/Finders/MethodFinder.cs +++ /dev/null @@ -1,83 +0,0 @@ -using Mono.Cecil; -using Mono.Cecil.Cil; - -namespace StardewModdingAPI.AssemblyRewriters.Finders -{ - /// Finds incompatible CIL instructions that reference a given method and throws an . - public class MethodFinder : IInstructionRewriter - { - /********* - ** Properties - *********/ - /// The full type name for which to find references. - private readonly string FullTypeName; - - /// The method name for which to find references. - private readonly string MethodName; - - - /********* - ** Accessors - *********/ - /// A brief noun phrase indicating what the instruction finder matches. - public string NounPhrase { get; } - - - /********* - ** Public methods - *********/ - /// 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) - { - this.FullTypeName = fullTypeName; - this.MethodName = methodName; - this.NounPhrase = nounPhrase ?? $"{fullTypeName}.{methodName} method"; - } - - /// Rewrite a method definition for compatibility. - /// The module being rewritten. - /// The method definition to rewrite. - /// 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(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) - { - return false; - } - - /// Rewrite a CIL instruction for compatibility. - /// The module being rewritten. - /// The CIL rewriter. - /// The instruction to rewrite. - /// 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(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) - { - if (!this.IsMatch(instruction)) - return false; - - throw new IncompatibleInstructionException(this.NounPhrase); - } - - - /********* - ** Protected methods - *********/ - /// Get whether a CIL instruction matches. - /// The IL instruction. - protected bool IsMatch(Instruction instruction) - { - MethodReference methodRef = RewriteHelper.AsMethodReference(instruction); - return - methodRef != null - && methodRef.DeclaringType.FullName == this.FullTypeName - && methodRef.Name == this.MethodName; - } - } -} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/PropertyFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/PropertyFinder.cs deleted file mode 100644 index 441f15f2..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/Finders/PropertyFinder.cs +++ /dev/null @@ -1,83 +0,0 @@ -using Mono.Cecil; -using Mono.Cecil.Cil; - -namespace StardewModdingAPI.AssemblyRewriters.Finders -{ - /// Finds incompatible CIL instructions that reference a given property and throws an . - public class PropertyFinder : IInstructionRewriter - { - /********* - ** Properties - *********/ - /// The full type name for which to find references. - private readonly string FullTypeName; - - /// The property name for which to find references. - private readonly string PropertyName; - - - /********* - ** Accessors - *********/ - /// A brief noun phrase indicating what the instruction finder matches. - public string NounPhrase { get; } - - - /********* - ** Public methods - *********/ - /// 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) - { - this.FullTypeName = fullTypeName; - this.PropertyName = propertyName; - this.NounPhrase = nounPhrase ?? $"{fullTypeName}.{propertyName} property"; - } - - /// Rewrite a method definition for compatibility. - /// The module being rewritten. - /// The method definition to rewrite. - /// 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(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) - { - return false; - } - - /// Rewrite a CIL instruction for compatibility. - /// The module being rewritten. - /// The CIL rewriter. - /// The instruction to rewrite. - /// 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(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) - { - if (!this.IsMatch(instruction)) - return false; - - throw new IncompatibleInstructionException(this.NounPhrase); - } - - - /********* - ** Protected methods - *********/ - /// Get whether a CIL instruction matches. - /// The IL instruction. - protected bool IsMatch(Instruction instruction) - { - MethodReference methodRef = RewriteHelper.AsMethodReference(instruction); - return - methodRef != null - && methodRef.DeclaringType.FullName == this.FullTypeName - && (methodRef.Name == "get_" + this.PropertyName || methodRef.Name == "set_" + this.PropertyName); - } - } -} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/TypeFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/TypeFinder.cs deleted file mode 100644 index 0560e38e..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/Finders/TypeFinder.cs +++ /dev/null @@ -1,135 +0,0 @@ -using System.Linq; -using Mono.Cecil; -using Mono.Cecil.Cil; - -namespace StardewModdingAPI.AssemblyRewriters.Finders -{ - /// Finds incompatible CIL instructions that reference a given type and throws an . - public class TypeFinder : IInstructionRewriter - { - /********* - ** Accessors - *********/ - /// The full type name for which to find references. - private readonly string FullTypeName; - - - /********* - ** Accessors - *********/ - /// A brief noun phrase indicating what the instruction finder matches. - public string NounPhrase { get; } - - - /********* - ** Public methods - *********/ - /// 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) - { - this.FullTypeName = fullTypeName; - this.NounPhrase = nounPhrase ?? $"{fullTypeName} type"; - } - - /// Rewrite a method definition for compatibility. - /// The module being rewritten. - /// The method definition to rewrite. - /// 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(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) - { - if (!this.IsMatch(method)) - return false; - - throw new IncompatibleInstructionException(this.NounPhrase); - } - - /// Rewrite a CIL instruction for compatibility. - /// The module being rewritten. - /// The CIL rewriter. - /// The instruction to rewrite. - /// 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(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) - { - if (!this.IsMatch(instruction)) - return false; - - throw new IncompatibleInstructionException(this.NounPhrase); - } - - - /********* - ** Protected methods - *********/ - /// Get whether a CIL instruction matches. - /// The method deifnition. - protected bool IsMatch(MethodDefinition method) - { - if (this.IsMatch(method.ReturnType)) - return true; - - foreach (VariableDefinition variable in method.Body.Variables) - { - if (this.IsMatch(variable.VariableType)) - return true; - } - - return false; - } - - /// Get whether a CIL instruction matches. - /// The IL instruction. - protected bool IsMatch(Instruction instruction) - { - // field reference - FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction); - if (fieldRef != null) - { - return - this.IsMatch(fieldRef.DeclaringType) // field on target class - || this.IsMatch(fieldRef.FieldType); // field value is target class - } - - // method reference - MethodReference methodRef = RewriteHelper.AsMethodReference(instruction); - if (methodRef != null) - { - return - this.IsMatch(methodRef.DeclaringType) // method on target class - || this.IsMatch(methodRef.ReturnType) // method returns target class - || methodRef.Parameters.Any(p => this.IsMatch(p.ParameterType)); // method parameters - } - - return false; - } - - /// Get whether a type reference matches the expected type. - /// The type to check. - protected bool IsMatch(TypeReference type) - { - // root type - if (type.FullName == this.FullTypeName) - return true; - - // generic arguments - if (type is GenericInstanceType genericType) - { - if (genericType.GenericArguments.Any(this.IsMatch)) - return true; - } - - // generic parameters (e.g. constraints) - if (type.GenericParameters.Any(this.IsMatch)) - return true; - - return false; - } - } -} diff --git a/src/StardewModdingAPI.AssemblyRewriters/IInstructionRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/IInstructionRewriter.cs deleted file mode 100644 index 2f16b23d..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/IInstructionRewriter.cs +++ /dev/null @@ -1,38 +0,0 @@ -using Mono.Cecil; -using Mono.Cecil.Cil; - -namespace StardewModdingAPI.AssemblyRewriters -{ - /// Rewrites CIL instructions for compatibility. - public interface IInstructionRewriter - { - /********* - ** Accessors - *********/ - /// A brief noun phrase indicating what the rewriter matches. - string NounPhrase { get; } - - - /********* - ** Methods - *********/ - /// Rewrite a method definition for compatibility. - /// The module being rewritten. - /// The method definition to rewrite. - /// 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(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged); - - /// Rewrite a CIL instruction for compatibility. - /// The module being rewritten. - /// The CIL rewriter. - /// The instruction to rewrite. - /// 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(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged); - } -} diff --git a/src/StardewModdingAPI.AssemblyRewriters/IncompatibleInstructionException.cs b/src/StardewModdingAPI.AssemblyRewriters/IncompatibleInstructionException.cs deleted file mode 100644 index f7e6bd8f..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/IncompatibleInstructionException.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System; - -namespace StardewModdingAPI.AssemblyRewriters -{ - /// An exception raised when an incompatible instruction is found while loading a mod assembly. - public class IncompatibleInstructionException : Exception - { - /********* - ** Accessors - *********/ - /// A brief noun phrase which describes the incompatible instruction that was found. - public string NounPhrase { get; } - - - /********* - ** Public methods - *********/ - /// Construct an instance. - /// A brief noun phrase which describes the incompatible instruction that was found. - public IncompatibleInstructionException(string nounPhrase) - : base($"Found an incompatible CIL instruction ({nounPhrase}).") - { - this.NounPhrase = nounPhrase; - } - - /// Construct an instance. - /// A brief noun phrase which describes the incompatible instruction that was found. - /// A message which describes the error. - public IncompatibleInstructionException(string nounPhrase, string message) - : base(message) - { - this.NounPhrase = nounPhrase; - } - } -} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Platform.cs b/src/StardewModdingAPI.AssemblyRewriters/Platform.cs deleted file mode 100644 index 8888a9a8..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/Platform.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace StardewModdingAPI.AssemblyRewriters -{ - /// The game's platform version. - public enum Platform - { - /// The Linux/Mac version of the game. - Mono, - - /// The Windows version of the game. - Windows - } -} \ No newline at end of file diff --git a/src/StardewModdingAPI.AssemblyRewriters/PlatformAssemblyMap.cs b/src/StardewModdingAPI.AssemblyRewriters/PlatformAssemblyMap.cs deleted file mode 100644 index fce2b187..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/PlatformAssemblyMap.cs +++ /dev/null @@ -1,55 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using Mono.Cecil; - -namespace StardewModdingAPI.AssemblyRewriters -{ - /// Metadata for mapping assemblies to the current . - public class PlatformAssemblyMap - { - /********* - ** Accessors - *********/ - /**** - ** Data - ****/ - /// The target game platform. - public readonly Platform TargetPlatform; - - /// The short assembly names to remove as assembly reference, and replace with the . These should be short names (like "Stardew Valley"). - public readonly string[] RemoveNames; - - /**** - ** Metadata - ****/ - /// The assemblies to target. Equivalent types should be rewritten to use these assemblies. - public readonly Assembly[] Targets; - - /// An assembly => reference cache. - public readonly IDictionary TargetReferences; - - /// An assembly => module cache. - public readonly IDictionary TargetModules; - - - /********* - ** Public methods - *********/ - /// Construct an instance. - /// The target game platform. - /// The assembly short names to remove (like Stardew Valley). - /// The assemblies to target. - public PlatformAssemblyMap(Platform targetPlatform, string[] removeAssemblyNames, Assembly[] targetAssemblies) - { - // save data - this.TargetPlatform = targetPlatform; - this.RemoveNames = removeAssemblyNames; - - // cache assembly metadata - this.Targets = targetAssemblies; - this.TargetReferences = this.Targets.ToDictionary(assembly => assembly, assembly => AssemblyNameReference.Parse(assembly.FullName)); - this.TargetModules = this.Targets.ToDictionary(assembly => assembly, assembly => ModuleDefinition.ReadModule(assembly.Modules.Single().FullyQualifiedName)); - } - } -} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Properties/AssemblyInfo.cs b/src/StardewModdingAPI.AssemblyRewriters/Properties/AssemblyInfo.cs deleted file mode 100644 index 25fbfef9..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,7 +0,0 @@ -using System.Reflection; -using System.Runtime.InteropServices; - -[assembly: AssemblyTitle("StardewModdingAPI.AssemblyRewriters")] -[assembly: AssemblyDescription("Contains internal SMAPI code for converting mods between Linux/Mac and Windows. This assembly is used by SMAPI internally and shouldn't be referenced directly by mods.")] -[assembly: AssemblyProduct("StardewModdingAPI.CrossplatformRewriters")] -[assembly: Guid("10db0676-9fc1-4771-a2c8-e2519f091e49")] diff --git a/src/StardewModdingAPI.AssemblyRewriters/RewriteHelper.cs b/src/StardewModdingAPI.AssemblyRewriters/RewriteHelper.cs deleted file mode 100644 index cfb330dd..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/RewriteHelper.cs +++ /dev/null @@ -1,94 +0,0 @@ -using System; -using System.Linq; -using System.Reflection; -using Mono.Cecil; -using Mono.Cecil.Cil; - -namespace StardewModdingAPI.AssemblyRewriters -{ - /// Provides helper methods for field rewriters. - internal static class RewriteHelper - { - /********* - ** Public methods - *********/ - /// Get the field reference from an instruction if it matches. - /// The IL instruction. - public static FieldReference AsFieldReference(Instruction instruction) - { - return instruction.OpCode == OpCodes.Ldfld || instruction.OpCode == OpCodes.Ldsfld || instruction.OpCode == OpCodes.Stfld || instruction.OpCode == OpCodes.Stsfld - ? (FieldReference)instruction.Operand - : null; - } - - /// Get the method reference from an instruction if it matches. - /// The IL instruction. - public static MethodReference AsMethodReference(Instruction instruction) - { - return instruction.OpCode == OpCodes.Call || instruction.OpCode == OpCodes.Callvirt - ? (MethodReference)instruction.Operand - : null; - } - - /// Get whether a type matches a type reference. - /// The defined type. - /// The type reference. - public static bool IsSameType(Type type, TypeReference reference) - { - // same namespace & name - if (type.Namespace != reference.Namespace || type.Name != reference.Name) - return false; - - // same generic parameters - if (type.IsGenericType) - { - if (!reference.IsGenericInstance) - return false; - - Type[] defGenerics = type.GetGenericArguments(); - TypeReference[] refGenerics = ((GenericInstanceType)reference).GenericArguments.ToArray(); - if (defGenerics.Length != refGenerics.Length) - return false; - for (int i = 0; i < defGenerics.Length; i++) - { - if (!RewriteHelper.IsSameType(defGenerics[i], refGenerics[i])) - return false; - } - } - - return true; - } - - /// Get whether a method definition matches the signature expected by a method reference. - /// The method definition. - /// The method reference. - public static bool HasMatchingSignature(MethodInfo definition, MethodReference reference) - { - // same name - if (definition.Name != reference.Name) - return false; - - // same arguments - ParameterInfo[] definitionParameters = definition.GetParameters(); - ParameterDefinition[] referenceParameters = reference.Parameters.ToArray(); - if (referenceParameters.Length != definitionParameters.Length) - return false; - for (int i = 0; i < referenceParameters.Length; i++) - { - if (!RewriteHelper.IsSameType(definitionParameters[i].ParameterType, referenceParameters[i].ParameterType)) - return false; - } - return true; - } - - /// Get whether a type has a method whose signature matches the one expected by a method reference. - /// The type to check. - /// The method reference. - public static bool HasMatchingSignature(Type type, MethodReference reference) - { - return type - .GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public) - .Any(method => RewriteHelper.HasMatchingSignature(method, reference)); - } - } -} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldReplaceRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldReplaceRewriter.cs deleted file mode 100644 index 73844073..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldReplaceRewriter.cs +++ /dev/null @@ -1,53 +0,0 @@ -using System; -using System.Reflection; -using Mono.Cecil; -using Mono.Cecil.Cil; -using StardewModdingAPI.AssemblyRewriters.Finders; - -namespace StardewModdingAPI.AssemblyRewriters.Rewriters -{ - /// Rewrites references to one field with another. - public class FieldReplaceRewriter : FieldFinder - { - /********* - ** Properties - *********/ - /// The new field to reference. - private readonly FieldInfo ToField; - - - /********* - ** Public methods - *********/ - /// Construct an instance. - /// 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) - { - 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 module being rewritten. - /// The CIL rewriter. - /// The instruction to rewrite. - /// 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(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) - { - if (!this.IsMatch(instruction)) - return false; - - FieldReference newRef = module.Import(this.ToField); - cil.Replace(instruction, cil.Create(instruction.OpCode, newRef)); - return true; - } - } -} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldToPropertyRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldToPropertyRewriter.cs deleted file mode 100644 index 3f57042d..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldToPropertyRewriter.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using Mono.Cecil; -using Mono.Cecil.Cil; -using StardewModdingAPI.AssemblyRewriters.Finders; - -namespace StardewModdingAPI.AssemblyRewriters.Rewriters -{ - /// Rewrites field references into property references. - public class FieldToPropertyRewriter : FieldFinder - { - /********* - ** Properties - *********/ - /// The type whose field to which references should be rewritten. - private readonly Type Type; - - /// The field name to rewrite. - private readonly string FieldName; - - - /********* - ** Public methods - *********/ - /// 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) - { - this.Type = type; - this.FieldName = fieldName; - } - - /// Rewrite a CIL instruction for compatibility. - /// The module being rewritten. - /// The CIL rewriter. - /// The instruction to rewrite. - /// 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(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) - { - if (!this.IsMatch(instruction)) - return false; - - 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; - } - } -} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/MethodParentRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/MethodParentRewriter.cs deleted file mode 100644 index 035ef211..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/MethodParentRewriter.cs +++ /dev/null @@ -1,93 +0,0 @@ -using System; -using Mono.Cecil; -using Mono.Cecil.Cil; - -namespace StardewModdingAPI.AssemblyRewriters.Rewriters -{ - /// Rewrites method references from one parent type to another if the signatures match. - public class MethodParentRewriter : IInstructionRewriter - { - /********* - ** Properties - *********/ - /// The type whose methods to remap. - private readonly Type FromType; - - /// The type with methods to map to. - private readonly Type ToType; - - /// Whether to only rewrite references if loading the assembly on a different platform than it was compiled on. - private readonly bool OnlyIfPlatformChanged; - - - /********* - ** Accessors - *********/ - /// A brief noun phrase indicating what the instruction finder matches. - public string NounPhrase { get; } - - - /********* - ** Public methods - *********/ - /// Construct an instance. - /// 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) - { - this.FromType = fromType; - this.ToType = toType; - this.NounPhrase = nounPhrase ?? $"{fromType.Name} methods"; - this.OnlyIfPlatformChanged = onlyIfPlatformChanged; - } - - /// Rewrite a method definition for compatibility. - /// The module being rewritten. - /// The method definition to rewrite. - /// 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(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) - { - return false; - } - - /// Rewrite a CIL instruction for compatibility. - /// The module being rewritten. - /// The CIL rewriter. - /// The instruction to rewrite. - /// 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(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) - { - if (!this.IsMatch(instruction, platformChanged)) - return false; - - MethodReference methodRef = (MethodReference)instruction.Operand; - methodRef.DeclaringType = module.Import(this.ToType); - return true; - } - - - /********* - ** Protected methods - *********/ - /// Get whether a CIL instruction matches. - /// The IL instruction. - /// Whether the mod was compiled on a different platform. - protected bool IsMatch(Instruction instruction, bool platformChanged) - { - MethodReference methodRef = RewriteHelper.AsMethodReference(instruction); - return - methodRef != null - && (platformChanged || !this.OnlyIfPlatformChanged) - && methodRef.DeclaringType.FullName == this.FromType.FullName - && RewriteHelper.HasMatchingSignature(this.ToType, methodRef); - } - } -} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/TypeReferenceRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/TypeReferenceRewriter.cs deleted file mode 100644 index da6d9bc9..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/TypeReferenceRewriter.cs +++ /dev/null @@ -1,157 +0,0 @@ -using System; -using Mono.Cecil; -using Mono.Cecil.Cil; -using StardewModdingAPI.AssemblyRewriters.Finders; - -namespace StardewModdingAPI.AssemblyRewriters.Rewriters -{ - /// Rewrites all references to a type. - public class TypeReferenceRewriter : TypeFinder - { - /********* - ** Properties - *********/ - /// The full type name to which to find references. - private readonly string FromTypeName; - - /// The new type to reference. - private readonly Type ToType; - - - /********* - ** Public methods - *********/ - /// 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) - { - this.FromTypeName = fromTypeFullName; - this.ToType = toType; - } - - /// Rewrite a method definition for compatibility. - /// The module being rewritten. - /// The method definition to rewrite. - /// 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(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) - { - bool rewritten = false; - - // return type - if (this.IsMatch(method.ReturnType)) - { - method.ReturnType = this.RewriteIfNeeded(module, method.ReturnType); - rewritten = true; - } - - // parameters - foreach (ParameterDefinition parameter in method.Parameters) - { - if (this.IsMatch(parameter.ParameterType)) - { - parameter.ParameterType = this.RewriteIfNeeded(module, parameter.ParameterType); - rewritten = true; - } - } - - // generic parameters - for (int i = 0; i < method.GenericParameters.Count; i++) - { - var parameter = method.GenericParameters[i]; - if (this.IsMatch(parameter)) - { - TypeReference newType = this.RewriteIfNeeded(module, parameter); - if (newType != parameter) - method.GenericParameters[i] = new GenericParameter(parameter.Name, newType); - rewritten = true; - } - } - - // local variables - foreach (VariableDefinition variable in method.Body.Variables) - { - if (this.IsMatch(variable.VariableType)) - { - variable.VariableType = this.RewriteIfNeeded(module, variable.VariableType); - rewritten = true; - } - } - - return rewritten; - } - - /// Rewrite a CIL instruction for compatibility. - /// The module being rewritten. - /// The CIL rewriter. - /// The instruction to rewrite. - /// 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(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) - { - if (!this.IsMatch(instruction) && !instruction.ToString().Contains(this.FromTypeName)) - return false; - - // field reference - FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction); - if (fieldRef != null) - { - fieldRef.DeclaringType = this.RewriteIfNeeded(module, fieldRef.DeclaringType); - fieldRef.FieldType = this.RewriteIfNeeded(module, fieldRef.FieldType); - } - - // method reference - MethodReference methodRef = RewriteHelper.AsMethodReference(instruction); - if (methodRef != null) - { - methodRef.DeclaringType = this.RewriteIfNeeded(module, methodRef.DeclaringType); - methodRef.ReturnType = this.RewriteIfNeeded(module, methodRef.ReturnType); - foreach (var parameter in methodRef.Parameters) - parameter.ParameterType = this.RewriteIfNeeded(module, parameter.ParameterType); - } - - // type reference - if (instruction.Operand is TypeReference typeRef) - { - TypeReference newRef = this.RewriteIfNeeded(module, typeRef); - if (typeRef != newRef) - cil.Replace(instruction, cil.Create(instruction.OpCode, newRef)); - } - - return true; - } - - /********* - ** Private methods - *********/ - /// Get the adjusted type reference if it matches, else the same value. - /// The module being rewritten. - /// The type to replace if it matches. - private TypeReference RewriteIfNeeded(ModuleDefinition module, TypeReference type) - { - // root type - if (type.FullName == this.FromTypeName) - return module.Import(this.ToType); - - // generic arguments - if (type is GenericInstanceType genericType) - { - for (int i = 0; i < genericType.GenericArguments.Count; i++) - genericType.GenericArguments[i] = this.RewriteIfNeeded(module, genericType.GenericArguments[i]); - } - - // generic parameters (e.g. constraints) - for (int i = 0; i < type.GenericParameters.Count; i++) - type.GenericParameters[i] = new GenericParameter(this.RewriteIfNeeded(module, type.GenericParameters[i])); - - return type; - } - } -} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/Wrappers/SpriteBatchWrapper.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/Wrappers/SpriteBatchWrapper.cs deleted file mode 100644 index ee68f1d5..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/Wrappers/SpriteBatchWrapper.cs +++ /dev/null @@ -1,59 +0,0 @@ -using System.Diagnostics.CodeAnalysis; -using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Graphics; - -namespace StardewModdingAPI.AssemblyRewriters.Rewriters.Wrappers -{ - /// Wraps methods that are incompatible when converting compiled code between MonoGame and XNA. - public class SpriteBatchWrapper : SpriteBatch - { - /********* - ** Public methods - *********/ - /// Construct an instance. - public SpriteBatchWrapper(GraphicsDevice graphicsDevice) : base(graphicsDevice) { } - - - /**** - ** MonoGame signatures - ****/ - [SuppressMessage("ReSharper", "CS0109", Justification = "The 'new' modifier applies when compiled on Linux/Mac.")] - public new void Begin(SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, RasterizerState rasterizerState, Effect effect, Matrix? matrix) - { - base.Begin(sortMode, blendState, samplerState, depthStencilState, rasterizerState, effect, matrix ?? Matrix.Identity); - } - - /**** - ** XNA signatures - ****/ - [SuppressMessage("ReSharper", "CS0109", Justification = "The 'new' modifier applies when compiled on Windows.")] - public new void Begin() - { - base.Begin(); - } - - [SuppressMessage("ReSharper", "CS0109", Justification = "The 'new' modifier applies when compiled on Windows.")] - public new void Begin(SpriteSortMode sortMode, BlendState blendState) - { - base.Begin(sortMode, blendState); - } - - [SuppressMessage("ReSharper", "CS0109", Justification = "The 'new' modifier applies when compiled on Windows.")] - public new void Begin(SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, RasterizerState rasterizerState) - { - base.Begin(sortMode, blendState, samplerState, depthStencilState, rasterizerState); - } - - [SuppressMessage("ReSharper", "CS0109", Justification = "The 'new' modifier applies when compiled on Windows.")] - public new void Begin(SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, RasterizerState rasterizerState, Effect effect) - { - base.Begin(sortMode, blendState, samplerState, depthStencilState, rasterizerState, effect); - } - - [SuppressMessage("ReSharper", "CS0109", Justification = "The 'new' modifier applies when compiled on Windows.")] - public new void Begin(SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, RasterizerState rasterizerState, Effect effect, Matrix transformMatrix) - { - base.Begin(sortMode, blendState, samplerState, depthStencilState, rasterizerState, effect, transformMatrix); - } - } -} \ No newline at end of file diff --git a/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj b/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj deleted file mode 100644 index 8416bd51..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj +++ /dev/null @@ -1,73 +0,0 @@ - - - - - Debug - x86 - {10DB0676-9FC1-4771-A2C8-E2519F091E49} - Library - Properties - StardewModdingAPI.AssemblyRewriters - StardewModdingAPI.AssemblyRewriters - v4.5 - 512 - - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - ..\packages\Mono.Cecil.0.9.6.4\lib\net45\Mono.Cecil.dll - True - - - ..\packages\Mono.Cecil.0.9.6.4\lib\net45\Mono.Cecil.Mdb.dll - True - - - ..\packages\Mono.Cecil.0.9.6.4\lib\net45\Mono.Cecil.Pdb.dll - True - - - - - - Properties\GlobalAssemblyInfo.cs - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/StardewModdingAPI.AssemblyRewriters/packages.config b/src/StardewModdingAPI.AssemblyRewriters/packages.config deleted file mode 100644 index 88fbc79d..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/packages.config +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/src/StardewModdingAPI.Installer/InteractiveInstaller.cs b/src/StardewModdingAPI.Installer/InteractiveInstaller.cs index 01288f33..b52529a6 100644 --- a/src/StardewModdingAPI.Installer/InteractiveInstaller.cs +++ b/src/StardewModdingAPI.Installer/InteractiveInstaller.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Globalization; using System.IO; @@ -81,7 +81,6 @@ namespace StardewModdingApi.Installer yield return GetInstallPath("StardewModdingAPI.exe"); yield return GetInstallPath("StardewModdingAPI.config.json"); yield return GetInstallPath("StardewModdingAPI.data.json"); - yield return GetInstallPath("StardewModdingAPI.AssemblyRewriters.dll"); yield return GetInstallPath("System.ValueTuple.dll"); yield return GetInstallPath("steam_appid.txt"); @@ -98,7 +97,8 @@ namespace StardewModdingApi.Installer // obsolete yield return GetInstallPath("Mods/.cache"); // 1.3-1.4 yield return GetInstallPath("Mono.Cecil.Rocks.dll"); // 1.3–1.8 - yield return GetInstallPath("StardewModdingAPI-settings.json"); // 1.0-1.4 + yield return GetInstallPath("StardewModdingAPI-settings.json"); // 1.0–1.4 + yield return GetInstallPath("StardewModdingAPI.AssemblyRewriters.dll"); // 1.3–1.15.4 if (modsDir.Exists) { foreach (DirectoryInfo modDir in modsDir.EnumerateDirectories()) diff --git a/src/StardewModdingAPI.sln b/src/StardewModdingAPI.sln index 9c3f18f8..a2e0ec44 100644 --- a/src/StardewModdingAPI.sln +++ b/src/StardewModdingAPI.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26430.16 +VisualStudioVersion = 15.0.26730.15 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TrainerMod", "TrainerMod\TrainerMod.csproj", "{28480467-1A48-46A7-99F8-236D95225359}" EndProject @@ -27,8 +27,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StardewModdingAPI.Installer {F1A573B0-F436-472C-AE29-0B91EA6B9F8F} = {F1A573B0-F436-472C-AE29-0B91EA6B9F8F} EndProjectSection EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StardewModdingAPI.AssemblyRewriters", "StardewModdingAPI.AssemblyRewriters\StardewModdingAPI.AssemblyRewriters.csproj", "{10DB0676-9FC1-4771-A2C8-E2519F091E49}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StardewModdingAPI.Tests", "StardewModdingAPI.Tests\StardewModdingAPI.Tests.csproj", "{36CCB19E-92EB-48C7-9615-98EEFD45109B}" EndProject Global @@ -71,16 +69,6 @@ Global {443DDF81-6AAF-420A-A610-3459F37E5575}.Release|Mixed Platforms.Build.0 = Release|x86 {443DDF81-6AAF-420A-A610-3459F37E5575}.Release|x86.ActiveCfg = Release|x86 {443DDF81-6AAF-420A-A610-3459F37E5575}.Release|x86.Build.0 = Release|x86 - {10DB0676-9FC1-4771-A2C8-E2519F091E49}.Debug|Any CPU.ActiveCfg = Debug|x86 - {10DB0676-9FC1-4771-A2C8-E2519F091E49}.Debug|Mixed Platforms.ActiveCfg = Debug|x86 - {10DB0676-9FC1-4771-A2C8-E2519F091E49}.Debug|Mixed Platforms.Build.0 = Debug|x86 - {10DB0676-9FC1-4771-A2C8-E2519F091E49}.Debug|x86.ActiveCfg = Debug|x86 - {10DB0676-9FC1-4771-A2C8-E2519F091E49}.Debug|x86.Build.0 = Debug|x86 - {10DB0676-9FC1-4771-A2C8-E2519F091E49}.Release|Any CPU.ActiveCfg = Release|x86 - {10DB0676-9FC1-4771-A2C8-E2519F091E49}.Release|Mixed Platforms.ActiveCfg = Release|x86 - {10DB0676-9FC1-4771-A2C8-E2519F091E49}.Release|Mixed Platforms.Build.0 = Release|x86 - {10DB0676-9FC1-4771-A2C8-E2519F091E49}.Release|x86.ActiveCfg = Release|x86 - {10DB0676-9FC1-4771-A2C8-E2519F091E49}.Release|x86.Build.0 = Release|x86 {36CCB19E-92EB-48C7-9615-98EEFD45109B}.Debug|Any CPU.ActiveCfg = Debug|x86 {36CCB19E-92EB-48C7-9615-98EEFD45109B}.Debug|Mixed Platforms.ActiveCfg = Debug|x86 {36CCB19E-92EB-48C7-9615-98EEFD45109B}.Debug|Mixed Platforms.Build.0 = Debug|x86 diff --git a/src/StardewModdingAPI/Constants.cs b/src/StardewModdingAPI/Constants.cs index fea9377a..4bbd7328 100644 --- a/src/StardewModdingAPI/Constants.cs +++ b/src/StardewModdingAPI/Constants.cs @@ -4,12 +4,12 @@ using System.IO; using System.Linq; using System.Reflection; using Microsoft.Xna.Framework.Graphics; -using StardewModdingAPI.AssemblyRewriters; -using StardewModdingAPI.AssemblyRewriters.Finders; -using StardewModdingAPI.AssemblyRewriters.Rewriters; -using StardewModdingAPI.AssemblyRewriters.Rewriters.Wrappers; using StardewModdingAPI.Events; using StardewModdingAPI.Framework; +using StardewModdingAPI.Framework.ModLoading; +using StardewModdingAPI.Framework.ModLoading.Finders; +using StardewModdingAPI.Framework.ModLoading.Rewriters; +using StardewModdingAPI.Framework.ModLoading.Rewriters.Wrappers; using StardewValley; namespace StardewModdingAPI diff --git a/src/StardewModdingAPI/Framework/ModLoading/AssemblyLoader.cs b/src/StardewModdingAPI/Framework/ModLoading/AssemblyLoader.cs index 9c642bef..27a81038 100644 --- a/src/StardewModdingAPI/Framework/ModLoading/AssemblyLoader.cs +++ b/src/StardewModdingAPI/Framework/ModLoading/AssemblyLoader.cs @@ -5,7 +5,6 @@ using System.Linq; using System.Reflection; using Mono.Cecil; using Mono.Cecil.Cil; -using StardewModdingAPI.AssemblyRewriters; using StardewModdingAPI.Framework.Exceptions; namespace StardewModdingAPI.Framework.ModLoading @@ -270,8 +269,7 @@ namespace StardewModdingAPI.Framework.ModLoading return; // get assembly - Assembly assembly; - if (!this.TypeAssemblies.TryGetValue(type.FullName, out assembly)) + if (!this.TypeAssemblies.TryGetValue(type.FullName, out Assembly assembly)) return; // replace scope diff --git a/src/StardewModdingAPI/Framework/ModLoading/Finders/EventFinder.cs b/src/StardewModdingAPI/Framework/ModLoading/Finders/EventFinder.cs new file mode 100644 index 00000000..ce234e39 --- /dev/null +++ b/src/StardewModdingAPI/Framework/ModLoading/Finders/EventFinder.cs @@ -0,0 +1,83 @@ +using Mono.Cecil; +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 + { + /********* + ** Properties + *********/ + /// The full type name for which to find references. + private readonly string FullTypeName; + + /// The event name for which to find references. + private readonly string EventName; + + + /********* + ** Accessors + *********/ + /// A brief noun phrase indicating what the instruction finder matches. + public string NounPhrase { get; } + + + /********* + ** Public methods + *********/ + /// 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) + { + this.FullTypeName = fullTypeName; + this.EventName = eventName; + this.NounPhrase = nounPhrase ?? $"{fullTypeName}.{eventName} event"; + } + + /// Rewrite a method definition for compatibility. + /// The module being rewritten. + /// The method definition to rewrite. + /// 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(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) + { + return false; + } + + /// Rewrite a CIL instruction for compatibility. + /// The module being rewritten. + /// The CIL rewriter. + /// The instruction to rewrite. + /// 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(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) + { + if (!this.IsMatch(instruction)) + return false; + + throw new IncompatibleInstructionException(this.NounPhrase); + } + + + /********* + ** Protected methods + *********/ + /// Get whether a CIL instruction matches. + /// The IL instruction. + protected bool IsMatch(Instruction instruction) + { + MethodReference methodRef = RewriteHelper.AsMethodReference(instruction); + return + methodRef != null + && methodRef.DeclaringType.FullName == this.FullTypeName + && (methodRef.Name == "add_" + this.EventName || methodRef.Name == "remove_" + this.EventName); + } + } +} diff --git a/src/StardewModdingAPI/Framework/ModLoading/Finders/FieldFinder.cs b/src/StardewModdingAPI/Framework/ModLoading/Finders/FieldFinder.cs new file mode 100644 index 00000000..2feaf2e6 --- /dev/null +++ b/src/StardewModdingAPI/Framework/ModLoading/Finders/FieldFinder.cs @@ -0,0 +1,83 @@ +using Mono.Cecil; +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 + { + /********* + ** Properties + *********/ + /// The full type name for which to find references. + private readonly string FullTypeName; + + /// The field name for which to find references. + private readonly string FieldName; + + + /********* + ** Accessors + *********/ + /// A brief noun phrase indicating what the instruction finder matches. + public string NounPhrase { get; } + + + /********* + ** Public methods + *********/ + /// 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) + { + this.FullTypeName = fullTypeName; + this.FieldName = fieldName; + this.NounPhrase = nounPhrase ?? $"{fullTypeName}.{fieldName} field"; + } + + /// Rewrite a method definition for compatibility. + /// The module being rewritten. + /// The method definition to rewrite. + /// 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(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) + { + return false; + } + + /// Rewrite a CIL instruction for compatibility. + /// The module being rewritten. + /// The CIL rewriter. + /// The instruction to rewrite. + /// 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(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) + { + if (!this.IsMatch(instruction)) + return false; + + throw new IncompatibleInstructionException(this.NounPhrase); + } + + + /********* + ** Protected methods + *********/ + /// Get whether a CIL instruction matches. + /// The IL instruction. + protected bool IsMatch(Instruction instruction) + { + FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction); + return + fieldRef != null + && fieldRef.DeclaringType.FullName == this.FullTypeName + && fieldRef.Name == this.FieldName; + } + } +} diff --git a/src/StardewModdingAPI/Framework/ModLoading/Finders/MethodFinder.cs b/src/StardewModdingAPI/Framework/ModLoading/Finders/MethodFinder.cs new file mode 100644 index 00000000..c3bb36e3 --- /dev/null +++ b/src/StardewModdingAPI/Framework/ModLoading/Finders/MethodFinder.cs @@ -0,0 +1,83 @@ +using Mono.Cecil; +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 + { + /********* + ** Properties + *********/ + /// The full type name for which to find references. + private readonly string FullTypeName; + + /// The method name for which to find references. + private readonly string MethodName; + + + /********* + ** Accessors + *********/ + /// A brief noun phrase indicating what the instruction finder matches. + public string NounPhrase { get; } + + + /********* + ** Public methods + *********/ + /// 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) + { + this.FullTypeName = fullTypeName; + this.MethodName = methodName; + this.NounPhrase = nounPhrase ?? $"{fullTypeName}.{methodName} method"; + } + + /// Rewrite a method definition for compatibility. + /// The module being rewritten. + /// The method definition to rewrite. + /// 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(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) + { + return false; + } + + /// Rewrite a CIL instruction for compatibility. + /// The module being rewritten. + /// The CIL rewriter. + /// The instruction to rewrite. + /// 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(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) + { + if (!this.IsMatch(instruction)) + return false; + + throw new IncompatibleInstructionException(this.NounPhrase); + } + + + /********* + ** Protected methods + *********/ + /// Get whether a CIL instruction matches. + /// The IL instruction. + protected bool IsMatch(Instruction instruction) + { + MethodReference methodRef = RewriteHelper.AsMethodReference(instruction); + return + methodRef != null + && methodRef.DeclaringType.FullName == this.FullTypeName + && methodRef.Name == this.MethodName; + } + } +} diff --git a/src/StardewModdingAPI/Framework/ModLoading/Finders/PropertyFinder.cs b/src/StardewModdingAPI/Framework/ModLoading/Finders/PropertyFinder.cs new file mode 100644 index 00000000..d1fed84b --- /dev/null +++ b/src/StardewModdingAPI/Framework/ModLoading/Finders/PropertyFinder.cs @@ -0,0 +1,83 @@ +using Mono.Cecil; +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 + { + /********* + ** Properties + *********/ + /// The full type name for which to find references. + private readonly string FullTypeName; + + /// The property name for which to find references. + private readonly string PropertyName; + + + /********* + ** Accessors + *********/ + /// A brief noun phrase indicating what the instruction finder matches. + public string NounPhrase { get; } + + + /********* + ** Public methods + *********/ + /// 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) + { + this.FullTypeName = fullTypeName; + this.PropertyName = propertyName; + this.NounPhrase = nounPhrase ?? $"{fullTypeName}.{propertyName} property"; + } + + /// Rewrite a method definition for compatibility. + /// The module being rewritten. + /// The method definition to rewrite. + /// 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(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) + { + return false; + } + + /// Rewrite a CIL instruction for compatibility. + /// The module being rewritten. + /// The CIL rewriter. + /// The instruction to rewrite. + /// 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(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) + { + if (!this.IsMatch(instruction)) + return false; + + throw new IncompatibleInstructionException(this.NounPhrase); + } + + + /********* + ** Protected methods + *********/ + /// Get whether a CIL instruction matches. + /// The IL instruction. + protected bool IsMatch(Instruction instruction) + { + MethodReference methodRef = RewriteHelper.AsMethodReference(instruction); + return + methodRef != null + && methodRef.DeclaringType.FullName == this.FullTypeName + && (methodRef.Name == "get_" + this.PropertyName || methodRef.Name == "set_" + this.PropertyName); + } + } +} diff --git a/src/StardewModdingAPI/Framework/ModLoading/Finders/TypeFinder.cs b/src/StardewModdingAPI/Framework/ModLoading/Finders/TypeFinder.cs new file mode 100644 index 00000000..e67e6766 --- /dev/null +++ b/src/StardewModdingAPI/Framework/ModLoading/Finders/TypeFinder.cs @@ -0,0 +1,135 @@ +using System.Linq; +using Mono.Cecil; +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 + { + /********* + ** Accessors + *********/ + /// The full type name for which to find references. + private readonly string FullTypeName; + + + /********* + ** Accessors + *********/ + /// A brief noun phrase indicating what the instruction finder matches. + public string NounPhrase { get; } + + + /********* + ** Public methods + *********/ + /// 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) + { + this.FullTypeName = fullTypeName; + this.NounPhrase = nounPhrase ?? $"{fullTypeName} type"; + } + + /// Rewrite a method definition for compatibility. + /// The module being rewritten. + /// The method definition to rewrite. + /// 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(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) + { + if (!this.IsMatch(method)) + return false; + + throw new IncompatibleInstructionException(this.NounPhrase); + } + + /// Rewrite a CIL instruction for compatibility. + /// The module being rewritten. + /// The CIL rewriter. + /// The instruction to rewrite. + /// 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(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) + { + if (!this.IsMatch(instruction)) + return false; + + throw new IncompatibleInstructionException(this.NounPhrase); + } + + + /********* + ** Protected methods + *********/ + /// Get whether a CIL instruction matches. + /// The method deifnition. + protected bool IsMatch(MethodDefinition method) + { + if (this.IsMatch(method.ReturnType)) + return true; + + foreach (VariableDefinition variable in method.Body.Variables) + { + if (this.IsMatch(variable.VariableType)) + return true; + } + + return false; + } + + /// Get whether a CIL instruction matches. + /// The IL instruction. + protected bool IsMatch(Instruction instruction) + { + // field reference + FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction); + if (fieldRef != null) + { + return + this.IsMatch(fieldRef.DeclaringType) // field on target class + || this.IsMatch(fieldRef.FieldType); // field value is target class + } + + // method reference + MethodReference methodRef = RewriteHelper.AsMethodReference(instruction); + if (methodRef != null) + { + return + this.IsMatch(methodRef.DeclaringType) // method on target class + || this.IsMatch(methodRef.ReturnType) // method returns target class + || methodRef.Parameters.Any(p => this.IsMatch(p.ParameterType)); // method parameters + } + + return false; + } + + /// Get whether a type reference matches the expected type. + /// The type to check. + protected bool IsMatch(TypeReference type) + { + // root type + if (type.FullName == this.FullTypeName) + return true; + + // generic arguments + if (type is GenericInstanceType genericType) + { + if (genericType.GenericArguments.Any(this.IsMatch)) + return true; + } + + // generic parameters (e.g. constraints) + if (type.GenericParameters.Any(this.IsMatch)) + return true; + + return false; + } + } +} diff --git a/src/StardewModdingAPI/Framework/ModLoading/IInstructionRewriter.cs b/src/StardewModdingAPI/Framework/ModLoading/IInstructionRewriter.cs new file mode 100644 index 00000000..9b35cdae --- /dev/null +++ b/src/StardewModdingAPI/Framework/ModLoading/IInstructionRewriter.cs @@ -0,0 +1,38 @@ +using Mono.Cecil; +using Mono.Cecil.Cil; + +namespace StardewModdingAPI.Framework.ModLoading +{ + /// Rewrites CIL instructions for compatibility. + internal interface IInstructionRewriter + { + /********* + ** Accessors + *********/ + /// A brief noun phrase indicating what the rewriter matches. + string NounPhrase { get; } + + + /********* + ** Methods + *********/ + /// Rewrite a method definition for compatibility. + /// The module being rewritten. + /// The method definition to rewrite. + /// 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(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged); + + /// Rewrite a CIL instruction for compatibility. + /// The module being rewritten. + /// The CIL rewriter. + /// The instruction to rewrite. + /// 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(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged); + } +} diff --git a/src/StardewModdingAPI/Framework/ModLoading/IncompatibleInstructionException.cs b/src/StardewModdingAPI/Framework/ModLoading/IncompatibleInstructionException.cs new file mode 100644 index 00000000..17ec24b1 --- /dev/null +++ b/src/StardewModdingAPI/Framework/ModLoading/IncompatibleInstructionException.cs @@ -0,0 +1,35 @@ +using System; + +namespace StardewModdingAPI.Framework.ModLoading +{ + /// An exception raised when an incompatible instruction is found while loading a mod assembly. + internal class IncompatibleInstructionException : Exception + { + /********* + ** Accessors + *********/ + /// A brief noun phrase which describes the incompatible instruction that was found. + public string NounPhrase { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// A brief noun phrase which describes the incompatible instruction that was found. + public IncompatibleInstructionException(string nounPhrase) + : base($"Found an incompatible CIL instruction ({nounPhrase}).") + { + this.NounPhrase = nounPhrase; + } + + /// Construct an instance. + /// A brief noun phrase which describes the incompatible instruction that was found. + /// A message which describes the error. + public IncompatibleInstructionException(string nounPhrase, string message) + : base(message) + { + this.NounPhrase = nounPhrase; + } + } +} diff --git a/src/StardewModdingAPI/Framework/ModLoading/InvalidModStateException.cs b/src/StardewModdingAPI/Framework/ModLoading/InvalidModStateException.cs index ab11272a..075e237a 100644 --- a/src/StardewModdingAPI/Framework/ModLoading/InvalidModStateException.cs +++ b/src/StardewModdingAPI/Framework/ModLoading/InvalidModStateException.cs @@ -1,9 +1,9 @@ -using System; +using System; namespace StardewModdingAPI.Framework.ModLoading { /// An exception which indicates that something went seriously wrong while loading mods, and SMAPI should abort outright. - public class InvalidModStateException : Exception + internal class InvalidModStateException : Exception { /// Construct an instance. /// The error message. diff --git a/src/StardewModdingAPI/Framework/ModLoading/ModMetadataStatus.cs b/src/StardewModdingAPI/Framework/ModLoading/ModMetadataStatus.cs index 1b2b0b55..ab65f7b4 100644 --- a/src/StardewModdingAPI/Framework/ModLoading/ModMetadataStatus.cs +++ b/src/StardewModdingAPI/Framework/ModLoading/ModMetadataStatus.cs @@ -1,4 +1,4 @@ -namespace StardewModdingAPI.Framework.ModLoading +namespace StardewModdingAPI.Framework.ModLoading { /// Indicates the status of a mod's metadata resolution. internal enum ModMetadataStatus @@ -9,4 +9,4 @@ /// The mod cannot be loaded. Failed } -} \ No newline at end of file +} diff --git a/src/StardewModdingAPI/Framework/ModLoading/Platform.cs b/src/StardewModdingAPI/Framework/ModLoading/Platform.cs new file mode 100644 index 00000000..45e881c4 --- /dev/null +++ b/src/StardewModdingAPI/Framework/ModLoading/Platform.cs @@ -0,0 +1,12 @@ +namespace StardewModdingAPI.Framework.ModLoading +{ + /// The game's platform version. + internal enum Platform + { + /// The Linux/Mac version of the game. + Mono, + + /// The Windows version of the game. + Windows + } +} diff --git a/src/StardewModdingAPI/Framework/ModLoading/PlatformAssemblyMap.cs b/src/StardewModdingAPI/Framework/ModLoading/PlatformAssemblyMap.cs new file mode 100644 index 00000000..463f45e8 --- /dev/null +++ b/src/StardewModdingAPI/Framework/ModLoading/PlatformAssemblyMap.cs @@ -0,0 +1,55 @@ +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using Mono.Cecil; + +namespace StardewModdingAPI.Framework.ModLoading +{ + /// Metadata for mapping assemblies to the current . + internal class PlatformAssemblyMap + { + /********* + ** Accessors + *********/ + /**** + ** Data + ****/ + /// The target game platform. + public readonly Platform TargetPlatform; + + /// The short assembly names to remove as assembly reference, and replace with the . These should be short names (like "Stardew Valley"). + public readonly string[] RemoveNames; + + /**** + ** Metadata + ****/ + /// The assemblies to target. Equivalent types should be rewritten to use these assemblies. + public readonly Assembly[] Targets; + + /// An assembly => reference cache. + public readonly IDictionary TargetReferences; + + /// An assembly => module cache. + public readonly IDictionary TargetModules; + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The target game platform. + /// The assembly short names to remove (like Stardew Valley). + /// The assemblies to target. + public PlatformAssemblyMap(Platform targetPlatform, string[] removeAssemblyNames, Assembly[] targetAssemblies) + { + // save data + this.TargetPlatform = targetPlatform; + this.RemoveNames = removeAssemblyNames; + + // cache assembly metadata + this.Targets = targetAssemblies; + this.TargetReferences = this.Targets.ToDictionary(assembly => assembly, assembly => AssemblyNameReference.Parse(assembly.FullName)); + this.TargetModules = this.Targets.ToDictionary(assembly => assembly, assembly => ModuleDefinition.ReadModule(assembly.Modules.Single().FullyQualifiedName)); + } + } +} diff --git a/src/StardewModdingAPI/Framework/ModLoading/RewriteHelper.cs b/src/StardewModdingAPI/Framework/ModLoading/RewriteHelper.cs new file mode 100644 index 00000000..56a60a72 --- /dev/null +++ b/src/StardewModdingAPI/Framework/ModLoading/RewriteHelper.cs @@ -0,0 +1,94 @@ +using System; +using System.Linq; +using System.Reflection; +using Mono.Cecil; +using Mono.Cecil.Cil; + +namespace StardewModdingAPI.Framework.ModLoading +{ + /// Provides helper methods for field rewriters. + internal static class RewriteHelper + { + /********* + ** Public methods + *********/ + /// Get the field reference from an instruction if it matches. + /// The IL instruction. + public static FieldReference AsFieldReference(Instruction instruction) + { + return instruction.OpCode == OpCodes.Ldfld || instruction.OpCode == OpCodes.Ldsfld || instruction.OpCode == OpCodes.Stfld || instruction.OpCode == OpCodes.Stsfld + ? (FieldReference)instruction.Operand + : null; + } + + /// Get the method reference from an instruction if it matches. + /// The IL instruction. + public static MethodReference AsMethodReference(Instruction instruction) + { + return instruction.OpCode == OpCodes.Call || instruction.OpCode == OpCodes.Callvirt + ? (MethodReference)instruction.Operand + : null; + } + + /// Get whether a type matches a type reference. + /// The defined type. + /// The type reference. + public static bool IsSameType(Type type, TypeReference reference) + { + // same namespace & name + if (type.Namespace != reference.Namespace || type.Name != reference.Name) + return false; + + // same generic parameters + if (type.IsGenericType) + { + if (!reference.IsGenericInstance) + return false; + + Type[] defGenerics = type.GetGenericArguments(); + TypeReference[] refGenerics = ((GenericInstanceType)reference).GenericArguments.ToArray(); + if (defGenerics.Length != refGenerics.Length) + return false; + for (int i = 0; i < defGenerics.Length; i++) + { + if (!RewriteHelper.IsSameType(defGenerics[i], refGenerics[i])) + return false; + } + } + + return true; + } + + /// Get whether a method definition matches the signature expected by a method reference. + /// The method definition. + /// The method reference. + public static bool HasMatchingSignature(MethodInfo definition, MethodReference reference) + { + // same name + if (definition.Name != reference.Name) + return false; + + // same arguments + ParameterInfo[] definitionParameters = definition.GetParameters(); + ParameterDefinition[] referenceParameters = reference.Parameters.ToArray(); + if (referenceParameters.Length != definitionParameters.Length) + return false; + for (int i = 0; i < referenceParameters.Length; i++) + { + if (!RewriteHelper.IsSameType(definitionParameters[i].ParameterType, referenceParameters[i].ParameterType)) + return false; + } + return true; + } + + /// Get whether a type has a method whose signature matches the one expected by a method reference. + /// The type to check. + /// The method reference. + public static bool HasMatchingSignature(Type type, MethodReference reference) + { + return type + .GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public) + .Any(method => RewriteHelper.HasMatchingSignature(method, reference)); + } + } +} diff --git a/src/StardewModdingAPI/Framework/ModLoading/Rewriters/FieldReplaceRewriter.cs b/src/StardewModdingAPI/Framework/ModLoading/Rewriters/FieldReplaceRewriter.cs new file mode 100644 index 00000000..7b838f13 --- /dev/null +++ b/src/StardewModdingAPI/Framework/ModLoading/Rewriters/FieldReplaceRewriter.cs @@ -0,0 +1,53 @@ +using System; +using System.Reflection; +using Mono.Cecil; +using Mono.Cecil.Cil; +using StardewModdingAPI.Framework.ModLoading.Finders; + +namespace StardewModdingAPI.Framework.ModLoading.Rewriters +{ + /// Rewrites references to one field with another. + internal class FieldReplaceRewriter : FieldFinder + { + /********* + ** Properties + *********/ + /// The new field to reference. + private readonly FieldInfo ToField; + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// 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) + { + 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 module being rewritten. + /// The CIL rewriter. + /// The instruction to rewrite. + /// 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(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) + { + if (!this.IsMatch(instruction)) + return false; + + FieldReference newRef = module.Import(this.ToField); + cil.Replace(instruction, cil.Create(instruction.OpCode, newRef)); + return true; + } + } +} diff --git a/src/StardewModdingAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs b/src/StardewModdingAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs new file mode 100644 index 00000000..8ef14103 --- /dev/null +++ b/src/StardewModdingAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs @@ -0,0 +1,54 @@ +using System; +using Mono.Cecil; +using Mono.Cecil.Cil; +using StardewModdingAPI.Framework.ModLoading.Finders; + +namespace StardewModdingAPI.Framework.ModLoading.Rewriters +{ + /// Rewrites field references into property references. + internal class FieldToPropertyRewriter : FieldFinder + { + /********* + ** Properties + *********/ + /// The type whose field to which references should be rewritten. + private readonly Type Type; + + /// The field name to rewrite. + private readonly string FieldName; + + + /********* + ** Public methods + *********/ + /// 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) + { + this.Type = type; + this.FieldName = fieldName; + } + + /// Rewrite a CIL instruction for compatibility. + /// The module being rewritten. + /// The CIL rewriter. + /// The instruction to rewrite. + /// 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(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) + { + if (!this.IsMatch(instruction)) + return false; + + 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; + } + } +} diff --git a/src/StardewModdingAPI/Framework/ModLoading/Rewriters/MethodParentRewriter.cs b/src/StardewModdingAPI/Framework/ModLoading/Rewriters/MethodParentRewriter.cs new file mode 100644 index 00000000..1b95b83b --- /dev/null +++ b/src/StardewModdingAPI/Framework/ModLoading/Rewriters/MethodParentRewriter.cs @@ -0,0 +1,93 @@ +using System; +using Mono.Cecil; +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 + { + /********* + ** Properties + *********/ + /// The type whose methods to remap. + private readonly Type FromType; + + /// The type with methods to map to. + private readonly Type ToType; + + /// Whether to only rewrite references if loading the assembly on a different platform than it was compiled on. + private readonly bool OnlyIfPlatformChanged; + + + /********* + ** Accessors + *********/ + /// A brief noun phrase indicating what the instruction finder matches. + public string NounPhrase { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// 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) + { + this.FromType = fromType; + this.ToType = toType; + this.NounPhrase = nounPhrase ?? $"{fromType.Name} methods"; + this.OnlyIfPlatformChanged = onlyIfPlatformChanged; + } + + /// Rewrite a method definition for compatibility. + /// The module being rewritten. + /// The method definition to rewrite. + /// 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(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) + { + return false; + } + + /// Rewrite a CIL instruction for compatibility. + /// The module being rewritten. + /// The CIL rewriter. + /// The instruction to rewrite. + /// 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(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) + { + if (!this.IsMatch(instruction, platformChanged)) + return false; + + MethodReference methodRef = (MethodReference)instruction.Operand; + methodRef.DeclaringType = module.Import(this.ToType); + return true; + } + + + /********* + ** Protected methods + *********/ + /// Get whether a CIL instruction matches. + /// The IL instruction. + /// Whether the mod was compiled on a different platform. + protected bool IsMatch(Instruction instruction, bool platformChanged) + { + MethodReference methodRef = RewriteHelper.AsMethodReference(instruction); + return + methodRef != null + && (platformChanged || !this.OnlyIfPlatformChanged) + && methodRef.DeclaringType.FullName == this.FromType.FullName + && RewriteHelper.HasMatchingSignature(this.ToType, methodRef); + } + } +} diff --git a/src/StardewModdingAPI/Framework/ModLoading/Rewriters/TypeReferenceRewriter.cs b/src/StardewModdingAPI/Framework/ModLoading/Rewriters/TypeReferenceRewriter.cs new file mode 100644 index 00000000..2c444b64 --- /dev/null +++ b/src/StardewModdingAPI/Framework/ModLoading/Rewriters/TypeReferenceRewriter.cs @@ -0,0 +1,157 @@ +using System; +using Mono.Cecil; +using Mono.Cecil.Cil; +using StardewModdingAPI.Framework.ModLoading.Finders; + +namespace StardewModdingAPI.Framework.ModLoading.Rewriters +{ + /// Rewrites all references to a type. + internal class TypeReferenceRewriter : TypeFinder + { + /********* + ** Properties + *********/ + /// The full type name to which to find references. + private readonly string FromTypeName; + + /// The new type to reference. + private readonly Type ToType; + + + /********* + ** Public methods + *********/ + /// 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) + { + this.FromTypeName = fromTypeFullName; + this.ToType = toType; + } + + /// Rewrite a method definition for compatibility. + /// The module being rewritten. + /// The method definition to rewrite. + /// 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(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) + { + bool rewritten = false; + + // return type + if (this.IsMatch(method.ReturnType)) + { + method.ReturnType = this.RewriteIfNeeded(module, method.ReturnType); + rewritten = true; + } + + // parameters + foreach (ParameterDefinition parameter in method.Parameters) + { + if (this.IsMatch(parameter.ParameterType)) + { + parameter.ParameterType = this.RewriteIfNeeded(module, parameter.ParameterType); + rewritten = true; + } + } + + // generic parameters + for (int i = 0; i < method.GenericParameters.Count; i++) + { + var parameter = method.GenericParameters[i]; + if (this.IsMatch(parameter)) + { + TypeReference newType = this.RewriteIfNeeded(module, parameter); + if (newType != parameter) + method.GenericParameters[i] = new GenericParameter(parameter.Name, newType); + rewritten = true; + } + } + + // local variables + foreach (VariableDefinition variable in method.Body.Variables) + { + if (this.IsMatch(variable.VariableType)) + { + variable.VariableType = this.RewriteIfNeeded(module, variable.VariableType); + rewritten = true; + } + } + + return rewritten; + } + + /// Rewrite a CIL instruction for compatibility. + /// The module being rewritten. + /// The CIL rewriter. + /// The instruction to rewrite. + /// 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(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) + { + if (!this.IsMatch(instruction) && !instruction.ToString().Contains(this.FromTypeName)) + return false; + + // field reference + FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction); + if (fieldRef != null) + { + fieldRef.DeclaringType = this.RewriteIfNeeded(module, fieldRef.DeclaringType); + fieldRef.FieldType = this.RewriteIfNeeded(module, fieldRef.FieldType); + } + + // method reference + MethodReference methodRef = RewriteHelper.AsMethodReference(instruction); + if (methodRef != null) + { + methodRef.DeclaringType = this.RewriteIfNeeded(module, methodRef.DeclaringType); + methodRef.ReturnType = this.RewriteIfNeeded(module, methodRef.ReturnType); + foreach (var parameter in methodRef.Parameters) + parameter.ParameterType = this.RewriteIfNeeded(module, parameter.ParameterType); + } + + // type reference + if (instruction.Operand is TypeReference typeRef) + { + TypeReference newRef = this.RewriteIfNeeded(module, typeRef); + if (typeRef != newRef) + cil.Replace(instruction, cil.Create(instruction.OpCode, newRef)); + } + + return true; + } + + /********* + ** Private methods + *********/ + /// Get the adjusted type reference if it matches, else the same value. + /// The module being rewritten. + /// The type to replace if it matches. + private TypeReference RewriteIfNeeded(ModuleDefinition module, TypeReference type) + { + // root type + if (type.FullName == this.FromTypeName) + return module.Import(this.ToType); + + // generic arguments + if (type is GenericInstanceType genericType) + { + for (int i = 0; i < genericType.GenericArguments.Count; i++) + genericType.GenericArguments[i] = this.RewriteIfNeeded(module, genericType.GenericArguments[i]); + } + + // generic parameters (e.g. constraints) + for (int i = 0; i < type.GenericParameters.Count; i++) + type.GenericParameters[i] = new GenericParameter(this.RewriteIfNeeded(module, type.GenericParameters[i])); + + return type; + } + } +} diff --git a/src/StardewModdingAPI/Framework/ModLoading/Rewriters/Wrappers/SpriteBatchWrapper.cs b/src/StardewModdingAPI/Framework/ModLoading/Rewriters/Wrappers/SpriteBatchWrapper.cs new file mode 100644 index 00000000..7a631f69 --- /dev/null +++ b/src/StardewModdingAPI/Framework/ModLoading/Rewriters/Wrappers/SpriteBatchWrapper.cs @@ -0,0 +1,59 @@ +using System.Diagnostics.CodeAnalysis; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; + +namespace StardewModdingAPI.Framework.ModLoading.Rewriters.Wrappers +{ + /// Wraps methods that are incompatible when converting compiled code between MonoGame and XNA. + internal class SpriteBatchWrapper : SpriteBatch + { + /********* + ** Public methods + *********/ + /// Construct an instance. + public SpriteBatchWrapper(GraphicsDevice graphicsDevice) : base(graphicsDevice) { } + + + /**** + ** MonoGame signatures + ****/ + [SuppressMessage("ReSharper", "CS0109", Justification = "The 'new' modifier applies when compiled on Linux/Mac.")] + public new void Begin(SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, RasterizerState rasterizerState, Effect effect, Matrix? matrix) + { + base.Begin(sortMode, blendState, samplerState, depthStencilState, rasterizerState, effect, matrix ?? Matrix.Identity); + } + + /**** + ** XNA signatures + ****/ + [SuppressMessage("ReSharper", "CS0109", Justification = "The 'new' modifier applies when compiled on Windows.")] + public new void Begin() + { + base.Begin(); + } + + [SuppressMessage("ReSharper", "CS0109", Justification = "The 'new' modifier applies when compiled on Windows.")] + public new void Begin(SpriteSortMode sortMode, BlendState blendState) + { + base.Begin(sortMode, blendState); + } + + [SuppressMessage("ReSharper", "CS0109", Justification = "The 'new' modifier applies when compiled on Windows.")] + public new void Begin(SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, RasterizerState rasterizerState) + { + base.Begin(sortMode, blendState, samplerState, depthStencilState, rasterizerState); + } + + [SuppressMessage("ReSharper", "CS0109", Justification = "The 'new' modifier applies when compiled on Windows.")] + public new void Begin(SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, RasterizerState rasterizerState, Effect effect) + { + base.Begin(sortMode, blendState, samplerState, depthStencilState, rasterizerState, effect); + } + + [SuppressMessage("ReSharper", "CS0109", Justification = "The 'new' modifier applies when compiled on Windows.")] + public new void Begin(SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, RasterizerState rasterizerState, Effect effect, Matrix transformMatrix) + { + base.Begin(sortMode, blendState, samplerState, depthStencilState, rasterizerState, effect, transformMatrix); + } + } +} diff --git a/src/StardewModdingAPI/Framework/SContentManager.cs b/src/StardewModdingAPI/Framework/SContentManager.cs index 9553e79f..33403841 100644 --- a/src/StardewModdingAPI/Framework/SContentManager.cs +++ b/src/StardewModdingAPI/Framework/SContentManager.cs @@ -6,8 +6,8 @@ using System.Linq; using System.Reflection; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; -using StardewModdingAPI.AssemblyRewriters; using StardewModdingAPI.Framework.Content; +using StardewModdingAPI.Framework.ModLoading; using StardewModdingAPI.Framework.Reflection; using StardewModdingAPI.Framework.Utilities; using StardewModdingAPI.Metadata; diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs index ad873598..c1165216 100644 --- a/src/StardewModdingAPI/Program.cs +++ b/src/StardewModdingAPI/Program.cs @@ -12,7 +12,6 @@ using System.Management; using System.Windows.Forms; #endif using Newtonsoft.Json; -using StardewModdingAPI.AssemblyRewriters; using StardewModdingAPI.Events; using StardewModdingAPI.Framework; using StardewModdingAPI.Framework.Exceptions; diff --git a/src/StardewModdingAPI/StardewModdingAPI.csproj b/src/StardewModdingAPI/StardewModdingAPI.csproj index 8daf21b7..5f59d125 100644 --- a/src/StardewModdingAPI/StardewModdingAPI.csproj +++ b/src/StardewModdingAPI/StardewModdingAPI.csproj @@ -91,9 +91,25 @@ Properties\GlobalAssemblyInfo.cs + + + + + + + + + + + + + + + + @@ -257,12 +273,6 @@ false - - - {10db0676-9fc1-4771-a2c8-e2519f091e49} - StardewModdingAPI.AssemblyRewriters - - \ No newline at end of file diff --git a/src/common.targets b/src/common.targets index ee138524..c450b3a5 100644 --- a/src/common.targets +++ b/src/common.targets @@ -83,7 +83,6 @@ - diff --git a/src/prepare-install-package.targets b/src/prepare-install-package.targets index f2a2b23c..44997fef 100644 --- a/src/prepare-install-package.targets +++ b/src/prepare-install-package.targets @@ -27,7 +27,6 @@ - @@ -41,7 +40,6 @@ - -- cgit