From 929dccb75a1405737975d76648e015a3e7c00177 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 7 Oct 2017 23:07:10 -0400 Subject: reorganise repo structure --- .../ModLoading/AssemblyDefinitionResolver.cs | 61 ---- .../Framework/ModLoading/AssemblyLoadStatus.cs | 15 - .../Framework/ModLoading/AssemblyLoader.cs | 333 ------------------- .../Framework/ModLoading/AssemblyParseResult.cs | 36 -- .../Framework/ModLoading/Finders/EventFinder.cs | 82 ----- .../Framework/ModLoading/Finders/FieldFinder.cs | 82 ----- .../Framework/ModLoading/Finders/MethodFinder.cs | 82 ----- .../Framework/ModLoading/Finders/PropertyFinder.cs | 82 ----- .../Framework/ModLoading/Finders/TypeFinder.cs | 133 -------- .../Framework/ModLoading/IInstructionHandler.cs | 34 -- .../ModLoading/IncompatibleInstructionException.cs | 35 -- .../ModLoading/InstructionHandleResult.cs | 24 -- .../ModLoading/InvalidModStateException.cs | 14 - .../Framework/ModLoading/ModDependencyStatus.cs | 18 - .../Framework/ModLoading/ModMetadata.cs | 68 ---- .../Framework/ModLoading/ModMetadataStatus.cs | 12 - .../Framework/ModLoading/ModResolver.cs | 366 --------------------- .../Framework/ModLoading/Platform.cs | 12 - .../Framework/ModLoading/PlatformAssemblyMap.cs | 55 ---- .../Framework/ModLoading/RewriteHelper.cs | 94 ------ .../ModLoading/Rewriters/FieldReplaceRewriter.cs | 50 --- .../Rewriters/FieldToPropertyRewriter.cs | 51 --- .../ModLoading/Rewriters/MethodParentRewriter.cs | 88 ----- .../ModLoading/Rewriters/TypeReferenceRewriter.cs | 154 --------- .../Rewriters/VirtualEntryCallRemover.cs | 90 ----- 25 files changed, 2071 deletions(-) delete mode 100644 src/StardewModdingAPI/Framework/ModLoading/AssemblyDefinitionResolver.cs delete mode 100644 src/StardewModdingAPI/Framework/ModLoading/AssemblyLoadStatus.cs delete mode 100644 src/StardewModdingAPI/Framework/ModLoading/AssemblyLoader.cs delete mode 100644 src/StardewModdingAPI/Framework/ModLoading/AssemblyParseResult.cs delete mode 100644 src/StardewModdingAPI/Framework/ModLoading/Finders/EventFinder.cs delete mode 100644 src/StardewModdingAPI/Framework/ModLoading/Finders/FieldFinder.cs delete mode 100644 src/StardewModdingAPI/Framework/ModLoading/Finders/MethodFinder.cs delete mode 100644 src/StardewModdingAPI/Framework/ModLoading/Finders/PropertyFinder.cs delete mode 100644 src/StardewModdingAPI/Framework/ModLoading/Finders/TypeFinder.cs delete mode 100644 src/StardewModdingAPI/Framework/ModLoading/IInstructionHandler.cs delete mode 100644 src/StardewModdingAPI/Framework/ModLoading/IncompatibleInstructionException.cs delete mode 100644 src/StardewModdingAPI/Framework/ModLoading/InstructionHandleResult.cs delete mode 100644 src/StardewModdingAPI/Framework/ModLoading/InvalidModStateException.cs delete mode 100644 src/StardewModdingAPI/Framework/ModLoading/ModDependencyStatus.cs delete mode 100644 src/StardewModdingAPI/Framework/ModLoading/ModMetadata.cs delete mode 100644 src/StardewModdingAPI/Framework/ModLoading/ModMetadataStatus.cs delete mode 100644 src/StardewModdingAPI/Framework/ModLoading/ModResolver.cs delete mode 100644 src/StardewModdingAPI/Framework/ModLoading/Platform.cs delete mode 100644 src/StardewModdingAPI/Framework/ModLoading/PlatformAssemblyMap.cs delete mode 100644 src/StardewModdingAPI/Framework/ModLoading/RewriteHelper.cs delete mode 100644 src/StardewModdingAPI/Framework/ModLoading/Rewriters/FieldReplaceRewriter.cs delete mode 100644 src/StardewModdingAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs delete mode 100644 src/StardewModdingAPI/Framework/ModLoading/Rewriters/MethodParentRewriter.cs delete mode 100644 src/StardewModdingAPI/Framework/ModLoading/Rewriters/TypeReferenceRewriter.cs delete mode 100644 src/StardewModdingAPI/Framework/ModLoading/Rewriters/VirtualEntryCallRemover.cs (limited to 'src/StardewModdingAPI/Framework/ModLoading') diff --git a/src/StardewModdingAPI/Framework/ModLoading/AssemblyDefinitionResolver.cs b/src/StardewModdingAPI/Framework/ModLoading/AssemblyDefinitionResolver.cs deleted file mode 100644 index 4378798c..00000000 --- a/src/StardewModdingAPI/Framework/ModLoading/AssemblyDefinitionResolver.cs +++ /dev/null @@ -1,61 +0,0 @@ -using System.Collections.Generic; -using Mono.Cecil; - -namespace StardewModdingAPI.Framework.ModLoading -{ - /// A minimal assembly definition resolver which resolves references to known assemblies. - internal class AssemblyDefinitionResolver : DefaultAssemblyResolver - { - /********* - ** Properties - *********/ - /// The known assemblies. - private readonly IDictionary Loaded = new Dictionary(); - - - /********* - ** Public methods - *********/ - /// Add known assemblies to the resolver. - /// The known assemblies. - public void Add(params AssemblyDefinition[] assemblies) - { - foreach (AssemblyDefinition assembly in assemblies) - { - this.Loaded[assembly.Name.Name] = assembly; - this.Loaded[assembly.Name.FullName] = assembly; - } - } - - /// Resolve an assembly reference. - /// The assembly name. - public override AssemblyDefinition Resolve(AssemblyNameReference name) => this.ResolveName(name.Name) ?? base.Resolve(name); - - /// Resolve an assembly reference. - /// The assembly name. - /// The assembly reader parameters. - public override AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters parameters) => this.ResolveName(name.Name) ?? base.Resolve(name, parameters); - - /// Resolve an assembly reference. - /// The assembly full name (including version, etc). - public override AssemblyDefinition Resolve(string fullName) => this.ResolveName(fullName) ?? base.Resolve(fullName); - - /// Resolve an assembly reference. - /// The assembly full name (including version, etc). - /// The assembly reader parameters. - public override AssemblyDefinition Resolve(string fullName, ReaderParameters parameters) => this.ResolveName(fullName) ?? base.Resolve(fullName, parameters); - - - /********* - ** Private methods - *********/ - /// Resolve a known assembly definition based on its short or full name. - /// The assembly's short or full name. - private AssemblyDefinition ResolveName(string name) - { - return this.Loaded.ContainsKey(name) - ? this.Loaded[name] - : null; - } - } -} diff --git a/src/StardewModdingAPI/Framework/ModLoading/AssemblyLoadStatus.cs b/src/StardewModdingAPI/Framework/ModLoading/AssemblyLoadStatus.cs deleted file mode 100644 index 11be19fc..00000000 --- a/src/StardewModdingAPI/Framework/ModLoading/AssemblyLoadStatus.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace StardewModdingAPI.Framework.ModLoading -{ - /// Indicates the result of an assembly load. - internal enum AssemblyLoadStatus - { - /// The assembly was loaded successfully. - Okay = 1, - - /// The assembly could not be loaded. - Failed = 2, - - /// The assembly is already loaded. - AlreadyLoaded = 3 - } -} diff --git a/src/StardewModdingAPI/Framework/ModLoading/AssemblyLoader.cs b/src/StardewModdingAPI/Framework/ModLoading/AssemblyLoader.cs deleted file mode 100644 index 1e3c4a05..00000000 --- a/src/StardewModdingAPI/Framework/ModLoading/AssemblyLoader.cs +++ /dev/null @@ -1,333 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Reflection; -using Mono.Cecil; -using Mono.Cecil.Cil; -using StardewModdingAPI.Framework.Exceptions; -using StardewModdingAPI.Metadata; - -namespace StardewModdingAPI.Framework.ModLoading -{ - /// Preprocesses and loads mod assemblies. - internal class AssemblyLoader - { - /********* - ** Properties - *********/ - /// Metadata for mapping assemblies to the current platform. - private readonly PlatformAssemblyMap AssemblyMap; - - /// A type => assembly lookup for types which should be rewritten. - private readonly IDictionary TypeAssemblies; - - /// Encapsulates monitoring and logging. - private readonly IMonitor Monitor; - - /// Whether to enable developer mode logging. - private readonly bool IsDeveloperMode; - - - /********* - ** Public methods - *********/ - /// Construct an instance. - /// The current game platform. - /// Encapsulates monitoring and logging. - /// Whether to enable developer mode logging. - public AssemblyLoader(Platform targetPlatform, IMonitor monitor, bool isDeveloperMode) - { - this.Monitor = monitor; - this.IsDeveloperMode = isDeveloperMode; - this.AssemblyMap = Constants.GetAssemblyMap(targetPlatform); - - // generate type => assembly lookup for types which should be rewritten - this.TypeAssemblies = new Dictionary(); - foreach (Assembly assembly in this.AssemblyMap.Targets) - { - ModuleDefinition module = this.AssemblyMap.TargetModules[assembly]; - foreach (TypeDefinition type in module.GetTypes()) - { - if (!type.IsPublic) - continue; // no need to rewrite - if (type.Namespace.Contains("<")) - continue; // ignore assembly metadata - this.TypeAssemblies[type.FullName] = assembly; - } - } - } - - /// Preprocess and load an assembly. - /// The mod for which the assembly is being loaded. - /// The assembly file path. - /// Assume the mod is compatible, even if incompatible code is detected. - /// Returns the rewrite metadata for the preprocessed assembly. - /// An incompatible CIL instruction was found while rewriting the assembly. - public Assembly Load(IModMetadata mod, string assemblyPath, bool assumeCompatible) - { - // get referenced local assemblies - AssemblyParseResult[] assemblies; - { - AssemblyDefinitionResolver resolver = new AssemblyDefinitionResolver(); - HashSet visitedAssemblyNames = new HashSet(AppDomain.CurrentDomain.GetAssemblies().Select(p => p.GetName().Name)); // don't try loading assemblies that are already loaded - assemblies = this.GetReferencedLocalAssemblies(new FileInfo(assemblyPath), visitedAssemblyNames, resolver).ToArray(); - } - - // validate load - if (!assemblies.Any() || assemblies[0].Status == AssemblyLoadStatus.Failed) - { - throw new SAssemblyLoadFailedException(!File.Exists(assemblyPath) - ? $"Could not load '{assemblyPath}' because it doesn't exist." - : $"Could not load '{assemblyPath}'." - ); - } - if (assemblies.Last().Status == AssemblyLoadStatus.AlreadyLoaded) // mod assembly is last in dependency order - throw new SAssemblyLoadFailedException($"Could not load '{assemblyPath}' because it was already loaded. Do you have two copies of this mod?"); - - // rewrite & load assemblies in leaf-to-root order - bool oneAssembly = assemblies.Length == 1; - Assembly lastAssembly = null; - HashSet loggedMessages = new HashSet(); - foreach (AssemblyParseResult assembly in assemblies) - { - if (assembly.Status == AssemblyLoadStatus.AlreadyLoaded) - continue; - - bool changed = this.RewriteAssembly(mod, assembly.Definition, assumeCompatible, loggedMessages, logPrefix: " "); - if (changed) - { - if (!oneAssembly) - this.Monitor.Log($" Loading {assembly.File.Name} (rewritten in memory)...", LogLevel.Trace); - using (MemoryStream outStream = new MemoryStream()) - { - assembly.Definition.Write(outStream); - byte[] bytes = outStream.ToArray(); - lastAssembly = Assembly.Load(bytes); - } - } - else - { - if (!oneAssembly) - this.Monitor.Log($" Loading {assembly.File.Name}...", LogLevel.Trace); - lastAssembly = Assembly.UnsafeLoadFrom(assembly.File.FullName); - } - } - - // last assembly loaded is the root - return lastAssembly; - } - - /// Resolve an assembly by its name. - /// The assembly name. - /// - /// This implementation returns the first loaded assembly which matches the short form of - /// the assembly name, to resolve assembly resolution issues when rewriting - /// assemblies (especially with Mono). Since this is meant to be called on , - /// the implicit assumption is that loading the exact assembly failed. - /// - public Assembly ResolveAssembly(string name) - { - string shortName = name.Split(new[] { ',' }, 2).First(); // get simple name (without version and culture) - return AppDomain.CurrentDomain - .GetAssemblies() - .FirstOrDefault(p => p.GetName().Name == shortName); - } - - - /********* - ** Private methods - *********/ - /**** - ** Assembly parsing - ****/ - /// Get a list of referenced local assemblies starting from the mod assembly, ordered from leaf to root. - /// The assembly file to load. - /// The assembly names that should be skipped. - /// A resolver which resolves references to known assemblies. - /// Returns the rewrite metadata for the preprocessed assembly. - private IEnumerable GetReferencedLocalAssemblies(FileInfo file, HashSet visitedAssemblyNames, IAssemblyResolver assemblyResolver) - { - // validate - if (file.Directory == null) - throw new InvalidOperationException($"Could not get directory from file path '{file.FullName}'."); - if (!file.Exists) - yield break; // not a local assembly - - // read assembly - byte[] assemblyBytes = File.ReadAllBytes(file.FullName); - AssemblyDefinition assembly; - using (Stream readStream = new MemoryStream(assemblyBytes)) - assembly = AssemblyDefinition.ReadAssembly(readStream, new ReaderParameters(ReadingMode.Deferred) { AssemblyResolver = assemblyResolver }); - - // skip if already visited - if (visitedAssemblyNames.Contains(assembly.Name.Name)) - yield return new AssemblyParseResult(file, null, AssemblyLoadStatus.AlreadyLoaded); - visitedAssemblyNames.Add(assembly.Name.Name); - - // yield referenced assemblies - foreach (AssemblyNameReference dependency in assembly.MainModule.AssemblyReferences) - { - FileInfo dependencyFile = new FileInfo(Path.Combine(file.Directory.FullName, $"{dependency.Name}.dll")); - foreach (AssemblyParseResult result in this.GetReferencedLocalAssemblies(dependencyFile, visitedAssemblyNames, assemblyResolver)) - yield return result; - } - - // yield assembly - yield return new AssemblyParseResult(file, assembly, AssemblyLoadStatus.Okay); - } - - /**** - ** Assembly rewriting - ****/ - /// Rewrite the types referenced by an assembly. - /// The mod for which the assembly is being loaded. - /// The assembly to rewrite. - /// Assume the mod is compatible, even if incompatible code is detected. - /// The messages that have already been logged for this mod. - /// A string to prefix to log messages. - /// Returns whether the assembly was modified. - /// An incompatible CIL instruction was found while rewriting the assembly. - private bool RewriteAssembly(IModMetadata mod, AssemblyDefinition assembly, bool assumeCompatible, HashSet loggedMessages, string logPrefix) - { - ModuleDefinition module = assembly.MainModule; - string filename = $"{assembly.Name.Name}.dll"; - - // swap assembly references if needed (e.g. XNA => MonoGame) - bool platformChanged = false; - for (int i = 0; i < module.AssemblyReferences.Count; i++) - { - // remove old assembly reference - if (this.AssemblyMap.RemoveNames.Any(name => module.AssemblyReferences[i].Name == name)) - { - this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Rewriting {filename} for OS..."); - platformChanged = true; - module.AssemblyReferences.RemoveAt(i); - i--; - } - } - if (platformChanged) - { - // add target assembly references - foreach (AssemblyNameReference target in this.AssemblyMap.TargetReferences.Values) - module.AssemblyReferences.Add(target); - - // rewrite type scopes to use target assemblies - IEnumerable typeReferences = module.GetTypeReferences().OrderBy(p => p.FullName); - foreach (TypeReference type in typeReferences) - this.ChangeTypeScope(type); - } - - // find (and optionally rewrite) incompatible instructions - bool anyRewritten = false; - IInstructionHandler[] handlers = new InstructionMetadata().GetHandlers().ToArray(); - foreach (MethodDefinition method in this.GetMethods(module)) - { - // check method definition - foreach (IInstructionHandler handler in handlers) - { - InstructionHandleResult result = handler.Handle(module, method, this.AssemblyMap, platformChanged); - this.ProcessInstructionHandleResult(mod, handler, result, loggedMessages, logPrefix, assumeCompatible, filename); - if (result == InstructionHandleResult.Rewritten) - anyRewritten = true; - } - - // check CIL instructions - ILProcessor cil = method.Body.GetILProcessor(); - foreach (Instruction instruction in cil.Body.Instructions.ToArray()) - { - foreach (IInstructionHandler handler in handlers) - { - InstructionHandleResult result = handler.Handle(module, cil, instruction, this.AssemblyMap, platformChanged); - this.ProcessInstructionHandleResult(mod, handler, result, loggedMessages, logPrefix, assumeCompatible, filename); - if (result == InstructionHandleResult.Rewritten) - anyRewritten = true; - } - } - } - - return platformChanged || anyRewritten; - } - - /// Process the result from an instruction handler. - /// The mod being analysed. - /// The instruction handler. - /// The result returned by the handler. - /// The messages already logged for the current mod. - /// Assume the mod is compatible, even if incompatible code is detected. - /// A string to prefix to log messages. - /// The assembly filename for log messages. - private void ProcessInstructionHandleResult(IModMetadata mod, IInstructionHandler handler, InstructionHandleResult result, HashSet loggedMessages, string logPrefix, bool assumeCompatible, string filename) - { - switch (result) - { - case InstructionHandleResult.Rewritten: - this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Rewrote {filename} to fix {handler.NounPhrase}..."); - break; - - case InstructionHandleResult.NotCompatible: - if (!assumeCompatible) - throw new IncompatibleInstructionException(handler.NounPhrase, $"Found an incompatible CIL instruction ({handler.NounPhrase}) while loading assembly {filename}."); - this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Found an incompatible CIL instruction ({handler.NounPhrase}) while loading assembly {filename}, but SMAPI is configured to allow it anyway. The mod may crash or behave unexpectedly.", LogLevel.Warn); - break; - - case InstructionHandleResult.DetectedGamePatch: - this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Detected game patcher ({handler.NounPhrase}) in assembly {filename}."); - this.Monitor.LogOnce(loggedMessages, $"{mod.DisplayName} patches the game, which may impact game stability. If you encounter problems, try removing this mod first.", LogLevel.Warn); - break; - - case InstructionHandleResult.DetectedSaveSerialiser: - this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Detected possible save serialiser change ({handler.NounPhrase}) in assembly {filename}."); - this.Monitor.LogOnce(loggedMessages, $"{mod.DisplayName} seems to change the save serialiser. It may change your saves in such a way that they won't work without this mod in the future.", LogLevel.Warn); - break; - - case InstructionHandleResult.DetectedDynamic: - this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Detected 'dynamic' keyword ({handler.NounPhrase}) in assembly {filename}."); - this.Monitor.LogOnce(loggedMessages, $"{mod.DisplayName} uses the 'dynamic' keyword, which isn't compatible with Stardew Valley on Linux or Mac.", -#if SMAPI_FOR_WINDOWS - this.IsDeveloperMode ? LogLevel.Warn : LogLevel.Debug -#else - LogLevel.Warn -#endif - ); - break; - - case InstructionHandleResult.None: - break; - - default: - throw new NotSupportedException($"Unrecognised instruction handler result '{result}'."); - } - } - - /// Get the correct reference to use for compatibility with the current platform. - /// The type reference to rewrite. - private void ChangeTypeScope(TypeReference type) - { - // check skip conditions - if (type == null || type.FullName.StartsWith("System.")) - return; - - // get assembly - if (!this.TypeAssemblies.TryGetValue(type.FullName, out Assembly assembly)) - return; - - // replace scope - AssemblyNameReference assemblyRef = this.AssemblyMap.TargetReferences[assembly]; - type.Scope = assemblyRef; - } - - /// Get all methods in a module. - /// The module to search. - private IEnumerable GetMethods(ModuleDefinition module) - { - return ( - from type in module.GetTypes() - where type.HasMethods - from method in type.Methods - where method.HasBody - select method - ); - } - } -} diff --git a/src/StardewModdingAPI/Framework/ModLoading/AssemblyParseResult.cs b/src/StardewModdingAPI/Framework/ModLoading/AssemblyParseResult.cs deleted file mode 100644 index b56a776c..00000000 --- a/src/StardewModdingAPI/Framework/ModLoading/AssemblyParseResult.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.IO; -using Mono.Cecil; - -namespace StardewModdingAPI.Framework.ModLoading -{ - /// Metadata about a parsed assembly definition. - internal class AssemblyParseResult - { - /********* - ** Accessors - *********/ - /// The original assembly file. - public readonly FileInfo File; - - /// The assembly definition. - public readonly AssemblyDefinition Definition; - - /// The result of the assembly load. - public AssemblyLoadStatus Status; - - - /********* - ** Public methods - *********/ - /// Construct an instance. - /// The original assembly file. - /// The assembly definition. - /// The result of the assembly load. - public AssemblyParseResult(FileInfo file, AssemblyDefinition assembly, AssemblyLoadStatus status) - { - this.File = file; - this.Definition = assembly; - this.Status = status; - } - } -} diff --git a/src/StardewModdingAPI/Framework/ModLoading/Finders/EventFinder.cs b/src/StardewModdingAPI/Framework/ModLoading/Finders/EventFinder.cs deleted file mode 100644 index e4beb7a9..00000000 --- a/src/StardewModdingAPI/Framework/ModLoading/Finders/EventFinder.cs +++ /dev/null @@ -1,82 +0,0 @@ -using Mono.Cecil; -using Mono.Cecil.Cil; - -namespace StardewModdingAPI.Framework.ModLoading.Finders -{ - /// Finds incompatible CIL instructions that reference a given event. - internal class EventFinder : IInstructionHandler - { - /********* - ** 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; - - /// The result to return for matching instructions. - private readonly InstructionHandleResult Result; - - - /********* - ** 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. - /// The result to return for matching instructions. - public EventFinder(string fullTypeName, string eventName, InstructionHandleResult result) - { - this.FullTypeName = fullTypeName; - this.EventName = eventName; - this.Result = result; - this.NounPhrase = $"{fullTypeName}.{eventName} event"; - } - - /// Perform the predefined logic for a method if applicable. - /// The assembly module containing the instruction. - /// The method definition containing the instruction. - /// Metadata for mapping assemblies to the current platform. - /// Whether the mod was compiled on a different platform. - public virtual InstructionHandleResult Handle(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) - { - return InstructionHandleResult.None; - } - - /// Perform the predefined logic for an instruction if applicable. - /// The assembly module containing the instruction. - /// The CIL processor. - /// The instruction to handle. - /// Metadata for mapping assemblies to the current platform. - /// Whether the mod was compiled on a different platform. - public virtual InstructionHandleResult Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) - { - return this.IsMatch(instruction) - ? this.Result - : InstructionHandleResult.None; - } - - - /********* - ** 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 deleted file mode 100644 index 00805815..00000000 --- a/src/StardewModdingAPI/Framework/ModLoading/Finders/FieldFinder.cs +++ /dev/null @@ -1,82 +0,0 @@ -using Mono.Cecil; -using Mono.Cecil.Cil; - -namespace StardewModdingAPI.Framework.ModLoading.Finders -{ - /// Finds incompatible CIL instructions that reference a given field. - internal class FieldFinder : IInstructionHandler - { - /********* - ** 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; - - /// The result to return for matching instructions. - private readonly InstructionHandleResult Result; - - - /********* - ** 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. - /// The result to return for matching instructions. - public FieldFinder(string fullTypeName, string fieldName, InstructionHandleResult result) - { - this.FullTypeName = fullTypeName; - this.FieldName = fieldName; - this.Result = result; - this.NounPhrase = $"{fullTypeName}.{fieldName} field"; - } - - /// Perform the predefined logic for a method if applicable. - /// The assembly module containing the instruction. - /// The method definition containing the instruction. - /// Metadata for mapping assemblies to the current platform. - /// Whether the mod was compiled on a different platform. - public virtual InstructionHandleResult Handle(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) - { - return InstructionHandleResult.None; - } - - /// Perform the predefined logic for an instruction if applicable. - /// The assembly module containing the instruction. - /// The CIL processor. - /// The instruction to handle. - /// Metadata for mapping assemblies to the current platform. - /// Whether the mod was compiled on a different platform. - public virtual InstructionHandleResult Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) - { - return this.IsMatch(instruction) - ? this.Result - : InstructionHandleResult.None; - } - - - /********* - ** 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 deleted file mode 100644 index 5358f181..00000000 --- a/src/StardewModdingAPI/Framework/ModLoading/Finders/MethodFinder.cs +++ /dev/null @@ -1,82 +0,0 @@ -using Mono.Cecil; -using Mono.Cecil.Cil; - -namespace StardewModdingAPI.Framework.ModLoading.Finders -{ - /// Finds incompatible CIL instructions that reference a given method. - internal class MethodFinder : IInstructionHandler - { - /********* - ** 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; - - /// The result to return for matching instructions. - private readonly InstructionHandleResult Result; - - - /********* - ** 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. - /// The result to return for matching instructions. - public MethodFinder(string fullTypeName, string methodName, InstructionHandleResult result) - { - this.FullTypeName = fullTypeName; - this.MethodName = methodName; - this.Result = result; - this.NounPhrase = $"{fullTypeName}.{methodName} method"; - } - - /// Perform the predefined logic for a method if applicable. - /// The assembly module containing the instruction. - /// The method definition containing the instruction. - /// Metadata for mapping assemblies to the current platform. - /// Whether the mod was compiled on a different platform. - public virtual InstructionHandleResult Handle(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) - { - return InstructionHandleResult.None; - } - - /// Perform the predefined logic for an instruction if applicable. - /// The assembly module containing the instruction. - /// The CIL processor. - /// The instruction to handle. - /// Metadata for mapping assemblies to the current platform. - /// Whether the mod was compiled on a different platform. - public InstructionHandleResult Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) - { - return this.IsMatch(instruction) - ? this.Result - : InstructionHandleResult.None; - } - - - /********* - ** 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 deleted file mode 100644 index e54c86cf..00000000 --- a/src/StardewModdingAPI/Framework/ModLoading/Finders/PropertyFinder.cs +++ /dev/null @@ -1,82 +0,0 @@ -using Mono.Cecil; -using Mono.Cecil.Cil; - -namespace StardewModdingAPI.Framework.ModLoading.Finders -{ - /// Finds incompatible CIL instructions that reference a given property. - internal class PropertyFinder : IInstructionHandler - { - /********* - ** 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; - - /// The result to return for matching instructions. - private readonly InstructionHandleResult Result; - - - /********* - ** 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. - /// The result to return for matching instructions. - public PropertyFinder(string fullTypeName, string propertyName, InstructionHandleResult result) - { - this.FullTypeName = fullTypeName; - this.PropertyName = propertyName; - this.Result = result; - this.NounPhrase = $"{fullTypeName}.{propertyName} property"; - } - - /// Perform the predefined logic for a method if applicable. - /// The assembly module containing the instruction. - /// The method definition containing the instruction. - /// Metadata for mapping assemblies to the current platform. - /// Whether the mod was compiled on a different platform. - public virtual InstructionHandleResult Handle(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) - { - return InstructionHandleResult.None; - } - - /// Perform the predefined logic for an instruction if applicable. - /// The assembly module containing the instruction. - /// The CIL processor. - /// The instruction to handle. - /// Metadata for mapping assemblies to the current platform. - /// Whether the mod was compiled on a different platform. - public virtual InstructionHandleResult Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) - { - return this.IsMatch(instruction) - ? this.Result - : InstructionHandleResult.None; - } - - - /********* - ** 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 deleted file mode 100644 index 45349def..00000000 --- a/src/StardewModdingAPI/Framework/ModLoading/Finders/TypeFinder.cs +++ /dev/null @@ -1,133 +0,0 @@ -using System.Linq; -using Mono.Cecil; -using Mono.Cecil.Cil; - -namespace StardewModdingAPI.Framework.ModLoading.Finders -{ - /// Finds incompatible CIL instructions that reference a given type. - internal class TypeFinder : IInstructionHandler - { - /********* - ** Accessors - *********/ - /// The full type name for which to find references. - private readonly string FullTypeName; - - /// The result to return for matching instructions. - private readonly InstructionHandleResult Result; - - - /********* - ** Accessors - *********/ - /// 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. - /// The result to return for matching instructions. - public TypeFinder(string fullTypeName, InstructionHandleResult result) - { - this.FullTypeName = fullTypeName; - this.Result = result; - this.NounPhrase = $"{fullTypeName} type"; - } - - /// Perform the predefined logic for a method if applicable. - /// The assembly module containing the instruction. - /// The method definition containing the instruction. - /// Metadata for mapping assemblies to the current platform. - /// Whether the mod was compiled on a different platform. - public virtual InstructionHandleResult Handle(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) - { - return this.IsMatch(method) - ? this.Result - : InstructionHandleResult.None; - } - - /// Perform the predefined logic for an instruction if applicable. - /// The assembly module containing the instruction. - /// The CIL processor. - /// The instruction to handle. - /// Metadata for mapping assemblies to the current platform. - /// Whether the mod was compiled on a different platform. - public virtual InstructionHandleResult Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) - { - return this.IsMatch(instruction) - ? this.Result - : InstructionHandleResult.None; - } - - - /********* - ** 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/IInstructionHandler.cs b/src/StardewModdingAPI/Framework/ModLoading/IInstructionHandler.cs deleted file mode 100644 index 8830cc74..00000000 --- a/src/StardewModdingAPI/Framework/ModLoading/IInstructionHandler.cs +++ /dev/null @@ -1,34 +0,0 @@ -using Mono.Cecil; -using Mono.Cecil.Cil; - -namespace StardewModdingAPI.Framework.ModLoading -{ - /// Performs predefined logic for detected CIL instructions. - internal interface IInstructionHandler - { - /********* - ** Accessors - *********/ - /// A brief noun phrase indicating what the handler matches. - string NounPhrase { get; } - - - /********* - ** Methods - *********/ - /// Perform the predefined logic for a method if applicable. - /// The assembly module containing the instruction. - /// The method definition containing the instruction. - /// Metadata for mapping assemblies to the current platform. - /// Whether the mod was compiled on a different platform. - InstructionHandleResult Handle(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged); - - /// Perform the predefined logic for an instruction if applicable. - /// The assembly module containing the instruction. - /// The CIL processor. - /// The instruction to handle. - /// Metadata for mapping assemblies to the current platform. - /// Whether the mod was compiled on a different platform. - InstructionHandleResult Handle(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 deleted file mode 100644 index 17ec24b1..00000000 --- a/src/StardewModdingAPI/Framework/ModLoading/IncompatibleInstructionException.cs +++ /dev/null @@ -1,35 +0,0 @@ -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/InstructionHandleResult.cs b/src/StardewModdingAPI/Framework/ModLoading/InstructionHandleResult.cs deleted file mode 100644 index 0ae598fc..00000000 --- a/src/StardewModdingAPI/Framework/ModLoading/InstructionHandleResult.cs +++ /dev/null @@ -1,24 +0,0 @@ -namespace StardewModdingAPI.Framework.ModLoading -{ - /// Indicates how an instruction was handled. - internal enum InstructionHandleResult - { - /// No special handling is needed. - None, - - /// The instruction was successfully rewritten for compatibility. - Rewritten, - - /// The instruction is not compatible and can't be rewritten for compatibility. - NotCompatible, - - /// The instruction is compatible, but patches the game in a way that may impact stability. - DetectedGamePatch, - - /// The instruction is compatible, but affects the save serializer in a way that may make saves unloadable without the mod. - DetectedSaveSerialiser, - - /// The instruction is compatible, but uses the dynamic keyword which won't work on Linux/Mac. - DetectedDynamic - } -} diff --git a/src/StardewModdingAPI/Framework/ModLoading/InvalidModStateException.cs b/src/StardewModdingAPI/Framework/ModLoading/InvalidModStateException.cs deleted file mode 100644 index 075e237a..00000000 --- a/src/StardewModdingAPI/Framework/ModLoading/InvalidModStateException.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; - -namespace StardewModdingAPI.Framework.ModLoading -{ - /// An exception which indicates that something went seriously wrong while loading mods, and SMAPI should abort outright. - internal class InvalidModStateException : Exception - { - /// Construct an instance. - /// The error message. - /// The underlying exception, if any. - public InvalidModStateException(string message, Exception ex = null) - : base(message, ex) { } - } -} diff --git a/src/StardewModdingAPI/Framework/ModLoading/ModDependencyStatus.cs b/src/StardewModdingAPI/Framework/ModLoading/ModDependencyStatus.cs deleted file mode 100644 index 0774b487..00000000 --- a/src/StardewModdingAPI/Framework/ModLoading/ModDependencyStatus.cs +++ /dev/null @@ -1,18 +0,0 @@ -namespace StardewModdingAPI.Framework.ModLoading -{ - /// The status of a given mod in the dependency-sorting algorithm. - internal enum ModDependencyStatus - { - /// The mod hasn't been visited yet. - Queued, - - /// The mod is currently being analysed as part of a dependency chain. - Checking, - - /// The mod has already been sorted. - Sorted, - - /// The mod couldn't be sorted due to a metadata issue (e.g. missing dependencies). - Failed - } -} diff --git a/src/StardewModdingAPI/Framework/ModLoading/ModMetadata.cs b/src/StardewModdingAPI/Framework/ModLoading/ModMetadata.cs deleted file mode 100644 index 5055da75..00000000 --- a/src/StardewModdingAPI/Framework/ModLoading/ModMetadata.cs +++ /dev/null @@ -1,68 +0,0 @@ -using StardewModdingAPI.Framework.Models; - -namespace StardewModdingAPI.Framework.ModLoading -{ - /// Metadata for a mod. - internal class ModMetadata : IModMetadata - { - /********* - ** Accessors - *********/ - /// The mod's display name. - public string DisplayName { get; } - - /// The mod's full directory path. - public string DirectoryPath { get; } - - /// The mod manifest. - public IManifest Manifest { get; } - - /// Metadata about the mod from SMAPI's internal data (if any). - public ModDataRecord DataRecord { get; } - - /// The metadata resolution status. - public ModMetadataStatus Status { get; private set; } - - /// The reason the metadata is invalid, if any. - public string Error { get; private set; } - - /// The mod instance (if it was loaded). - public IMod Mod { get; private set; } - - - /********* - ** Public methods - *********/ - /// Construct an instance. - /// The mod's display name. - /// The mod's full directory path. - /// The mod manifest. - /// Metadata about the mod from SMAPI's internal data (if any). - public ModMetadata(string displayName, string directoryPath, IManifest manifest, ModDataRecord dataRecord) - { - this.DisplayName = displayName; - this.DirectoryPath = directoryPath; - this.Manifest = manifest; - this.DataRecord = dataRecord; - } - - /// Set the mod status. - /// The metadata resolution status. - /// The reason the metadata is invalid, if any. - /// Return the instance for chaining. - public IModMetadata SetStatus(ModMetadataStatus status, string error = null) - { - this.Status = status; - this.Error = error; - return this; - } - - /// Set the mod instance. - /// The mod instance to set. - public IModMetadata SetMod(IMod mod) - { - this.Mod = mod; - return this; - } - } -} diff --git a/src/StardewModdingAPI/Framework/ModLoading/ModMetadataStatus.cs b/src/StardewModdingAPI/Framework/ModLoading/ModMetadataStatus.cs deleted file mode 100644 index ab65f7b4..00000000 --- a/src/StardewModdingAPI/Framework/ModLoading/ModMetadataStatus.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace StardewModdingAPI.Framework.ModLoading -{ - /// Indicates the status of a mod's metadata resolution. - internal enum ModMetadataStatus - { - /// The mod has been found, but hasn't been processed yet. - Found, - - /// The mod cannot be loaded. - Failed - } -} diff --git a/src/StardewModdingAPI/Framework/ModLoading/ModResolver.cs b/src/StardewModdingAPI/Framework/ModLoading/ModResolver.cs deleted file mode 100644 index d0ef1b08..00000000 --- a/src/StardewModdingAPI/Framework/ModLoading/ModResolver.cs +++ /dev/null @@ -1,366 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using StardewModdingAPI.Framework.Exceptions; -using StardewModdingAPI.Framework.Models; -using StardewModdingAPI.Framework.Serialisation; - -namespace StardewModdingAPI.Framework.ModLoading -{ - /// Finds and processes mod metadata. - internal class ModResolver - { - /********* - ** Public methods - *********/ - /// Get manifest metadata for each folder in the given root path. - /// The root path to search for mods. - /// The JSON helper with which to read manifests. - /// Metadata about mods from SMAPI's internal data. - /// Returns the manifests by relative folder. - public IEnumerable ReadManifests(string rootPath, JsonHelper jsonHelper, IEnumerable dataRecords) - { - dataRecords = dataRecords.ToArray(); - - foreach (DirectoryInfo modDir in this.GetModFolders(rootPath)) - { - // read file - Manifest manifest = null; - string path = Path.Combine(modDir.FullName, "manifest.json"); - string error = null; - try - { - // read manifest - manifest = jsonHelper.ReadJsonFile(path); - - // validate - if (manifest == null) - { - error = File.Exists(path) - ? "its manifest is invalid." - : "it doesn't have a manifest."; - } - else if (string.IsNullOrWhiteSpace(manifest.EntryDll)) - error = "its manifest doesn't set an entry DLL."; - } - catch (SParseException ex) - { - error = $"parsing its manifest failed: {ex.Message}"; - } - catch (Exception ex) - { - error = $"parsing its manifest failed:\n{ex.GetLogSummary()}"; - } - - // get internal data record (if any) - ModDataRecord dataRecord = null; - if (manifest != null) - { - string key = !string.IsNullOrWhiteSpace(manifest.UniqueID) ? manifest.UniqueID : manifest.EntryDll; - dataRecord = dataRecords.FirstOrDefault(record => record.ID.Matches(key, manifest)); - } - - // add default update keys - if (manifest != null && manifest.UpdateKeys == null && dataRecord?.UpdateKeys != null) - manifest.UpdateKeys = dataRecord.UpdateKeys; - - // build metadata - string displayName = !string.IsNullOrWhiteSpace(manifest?.Name) - ? manifest.Name - : modDir.FullName.Replace(rootPath, "").Trim('/', '\\'); - ModMetadataStatus status = error == null - ? ModMetadataStatus.Found - : ModMetadataStatus.Failed; - - yield return new ModMetadata(displayName, modDir.FullName, manifest, dataRecord).SetStatus(status, error); - } - } - - /// Validate manifest metadata. - /// The mod manifests to validate. - /// The current SMAPI version. - /// Maps vendor keys (like Nexus) to their mod URL template (where {0} is the mod ID). - public void ValidateManifests(IEnumerable mods, ISemanticVersion apiVersion, IDictionary vendorModUrls) - { - mods = mods.ToArray(); - - // validate each manifest - foreach (IModMetadata mod in mods) - { - // skip if already failed - if (mod.Status == ModMetadataStatus.Failed) - continue; - - // validate compatibility - ModCompatibility compatibility = mod.DataRecord?.GetCompatibility(mod.Manifest.Version); - switch (compatibility?.Status) - { - case ModStatus.Obsolete: - mod.SetStatus(ModMetadataStatus.Failed, $"it's obsolete: {compatibility.ReasonPhrase}"); -