diff options
Diffstat (limited to 'src/SMAPI.Installer/Program.cs')
-rw-r--r-- | src/SMAPI.Installer/Program.cs | 47 |
1 files changed, 46 insertions, 1 deletions
diff --git a/src/SMAPI.Installer/Program.cs b/src/SMAPI.Installer/Program.cs index 8f328ecf..4d259fd3 100644 --- a/src/SMAPI.Installer/Program.cs +++ b/src/SMAPI.Installer/Program.cs @@ -1,17 +1,62 @@ -namespace StardewModdingApi.Installer +using System; +using System.Diagnostics.CodeAnalysis; +using System.IO; +using System.Reflection; +using StardewModdingAPI.Internal; + +namespace StardewModdingApi.Installer { /// <summary>The entry point for SMAPI's install and uninstall console app.</summary> internal class Program { /********* + ** Properties + *********/ + /// <summary>The absolute path to search for referenced assemblies.</summary> + [SuppressMessage("ReSharper", "AssignNullToNotNullAttribute", Justification = "The assembly location is never null in this context.")] + private static string DllSearchPath; + + + /********* ** Public methods *********/ /// <summary>Run the install or uninstall script.</summary> /// <param name="args">The command line arguments.</param> public static void Main(string[] args) { + // set up assembly resolution + string installerPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); + Program.DllSearchPath = Path.Combine(installerPath, "internal", EnvironmentUtility.DetectPlatform() == Platform.Windows ? "Windows" : "Mono", "smapi-internal"); + AppDomain.CurrentDomain.AssemblyResolve += Program.CurrentDomain_AssemblyResolve; + + // launch installer var installer = new InteractiveInstaller(); installer.Run(args); } + + /********* + ** Private methods + *********/ + /// <summary>Method called when assembly resolution fails, which may return a manually resolved assembly.</summary> + /// <param name="sender">The event sender.</param> + /// <param name="e">The event arguments.</param> + private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs e) + { + AssemblyName name = new AssemblyName(e.Name); + foreach (FileInfo dll in new DirectoryInfo(Program.DllSearchPath).EnumerateFiles("*.dll")) + { + try + { + if (name.Name.Equals(AssemblyName.GetAssemblyName(dll.FullName).Name, StringComparison.InvariantCultureIgnoreCase)) + return Assembly.LoadFrom(dll.FullName); + } + catch (Exception ex) + { + throw new InvalidOperationException($"Could not load dependency 'smapi-lib/{dll.Name}'. Consider deleting and redownloading the SMAPI installer.", ex); + } + } + + return null; + } } } |