#nullable disable 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.AddWithExplicitNames(assembly, assembly.Name.Name, assembly.Name.FullName); } /// Add a known assembly to the resolver with the given names. This overrides the assembly names that would normally be assigned. /// The assembly to add. /// The assembly names for which it should be returned. public void AddWithExplicitNames(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) { return 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) { return 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; } } }