From 0cd558b98c077b4f07062377613a5b04e295eed1 Mon Sep 17 00:00:00 2001 From: AeonLucid Date: Thu, 5 Nov 2020 02:50:56 +0100 Subject: [PATCH] Workaround for broken config / plugins path --- src/Impostor.Server/Plugins/PluginLoader.cs | 5 ++-- src/Impostor.Server/Program.cs | 2 ++ src/Impostor.Server/Utils/DirUtils.cs | 29 +++++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 src/Impostor.Server/Utils/DirUtils.cs diff --git a/src/Impostor.Server/Plugins/PluginLoader.cs b/src/Impostor.Server/Plugins/PluginLoader.cs index ef6dae6..1ea2072 100644 --- a/src/Impostor.Server/Plugins/PluginLoader.cs +++ b/src/Impostor.Server/Plugins/PluginLoader.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Reflection; using System.Runtime.Loader; using Impostor.Api.Plugins; +using Impostor.Server.Utils; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.FileSystemGlobbing; using Microsoft.Extensions.Hosting; @@ -25,7 +26,7 @@ namespace Impostor.Server.Plugins var pluginPaths = new List(config.Paths); var libraryPaths = new List(config.LibraryPaths); - var rootFolder = AppContext.BaseDirectory; + var rootFolder = DirUtils.GetExecutableDirectory(); pluginPaths.Add(Path.Combine(rootFolder, "plugins")); libraryPaths.Add(Path.Combine(rootFolder, "libraries")); @@ -143,4 +144,4 @@ namespace Impostor.Server.Plugins } } } -} \ No newline at end of file +} diff --git a/src/Impostor.Server/Program.cs b/src/Impostor.Server/Program.cs index a58dfa0..361a671 100644 --- a/src/Impostor.Server/Program.cs +++ b/src/Impostor.Server/Program.cs @@ -14,6 +14,7 @@ using Impostor.Server.Net.Messages; using Impostor.Server.Net.Redirector; using Impostor.Server.Plugins; using Impostor.Server.Recorder; +using Impostor.Server.Utils; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -75,6 +76,7 @@ namespace Impostor.Server .Get() ?? new PluginConfig(); return Host.CreateDefaultBuilder(args) + .UseContentRoot(DirUtils.GetExecutableDirectory()) #if DEBUG .UseEnvironment(Environment.GetEnvironmentVariable("IMPOSTOR_ENV") ?? "Development") #else diff --git a/src/Impostor.Server/Utils/DirUtils.cs b/src/Impostor.Server/Utils/DirUtils.cs new file mode 100644 index 0000000..0d1c7d9 --- /dev/null +++ b/src/Impostor.Server/Utils/DirUtils.cs @@ -0,0 +1,29 @@ +using System; +using System.Diagnostics; +using System.IO; + +namespace Impostor.Server.Utils +{ + internal static class DirUtils + { + // Workaround for: + // - https://github.com/dotnet/runtime/issues/40828 + // - https://github.com/dotnet/runtime/issues/38405#issuecomment-652643506 + public static string GetExecutableDirectory() + { + var programDirectory = Path.GetDirectoryName(typeof(Program).Assembly.Location)!; + var currentDirectory = Directory.GetCurrentDirectory(); + var baseDirectory = AppDomain.CurrentDomain.BaseDirectory; + var executablePath = Process.GetCurrentProcess().MainModule!.FileName; + var executableDirectory = Path.GetDirectoryName(executablePath)!; + var executable = Path.GetFileNameWithoutExtension(executablePath); + + if ("dotnet".Equals(executable, StringComparison.InvariantCultureIgnoreCase)) + { + return programDirectory; + } + + return executableDirectory; + } + } +} -- 2.39.5