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 { /********* ** Fields *********/ /// The known assemblies. private readonly IDictionary Lookup = 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.RegisterAssembly(assembly); this.Lookup[assembly.Name.Name] = assembly; this.Lookup[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); /********* ** 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.Lookup.TryGetValue(name, out AssemblyDefinition match) ? match : null; } } }