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.Add(assembly, assembly.Name.Name, assembly.Name.FullName); } /// Add known assemblies to the resolver. /// The assembly to add. /// The assembly names for which it should be returned. public void Add(AssemblyDefinition assembly, params string[] names) { this.RegisterAssembly(assembly); foreach (string name in names) this.Lookup[name] = 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; } } }