using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Mono.Cecil; using StardewModdingAPI.Internal; namespace StardewModdingAPI.Framework.ModLoading { /// Metadata for mapping assemblies to the current . internal class PlatformAssemblyMap : IDisposable { /********* ** 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, new ReaderParameters { InMemory = true })); } /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. public void Dispose() { foreach (ModuleDefinition module in this.TargetModules.Values) module.Dispose(); } } }