From fd5b082feceef5d9ee6878b13a9db1865afad61f Mon Sep 17 00:00:00 2001 From: Anairkoen Schno Date: Mon, 29 Mar 2021 05:24:17 -0500 Subject: [PATCH] LibLoader now correctly expands environment variables in PATH --- IPA.Loader/Loader/LibLoader.cs | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/IPA.Loader/Loader/LibLoader.cs b/IPA.Loader/Loader/LibLoader.cs index 573c5775..1e1ecdd8 100644 --- a/IPA.Loader/Loader/LibLoader.cs +++ b/IPA.Loader/Loader/LibLoader.cs @@ -65,11 +65,12 @@ namespace IPA.Loader { FilenameLocations = new Dictionary(); - foreach (var fn in TraverseTree(LibraryPath, s => s != NativeLibraryPath)) + foreach (var fn in TraverseTree(LibraryPath, s => s != NativeLibraryPath)) + { if (FilenameLocations.ContainsKey(fn.Name)) Log(Logger.Level.Critical, $"Multiple instances of {fn.Name} exist in Libs! Ignoring {fn.FullName}"); - else FilenameLocations.Add(fn.Name, fn.FullName); - + else FilenameLocations.Add(fn.Name, fn.FullName); + } if (!SetDefaultDllDirectories(LoadLibraryFlags.LOAD_LIBRARY_SEARCH_USER_DIRS | LoadLibraryFlags.LOAD_LIBRARY_SEARCH_SYSTEM32 | LoadLibraryFlags.LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LoadLibraryFlags.LOAD_LIBRARY_SEARCH_APPLICATION_DIR)) @@ -94,7 +95,7 @@ namespace IPA.Loader if (Directory.Exists(NativeLibraryPath)) { AddDir(NativeLibraryPath); - TraverseTree(NativeLibraryPath, dir => + _ = TraverseTree(NativeLibraryPath, dir => { // this is a terrible hack for iterating directories AddDir(dir); return true; }).All(f => true); // force it to iterate all @@ -103,8 +104,12 @@ namespace IPA.Loader //var unityData = Directory.EnumerateDirectories(Environment.CurrentDirectory, "*_Data").First(); //AddDir(Path.Combine(unityData, "Plugins")); - foreach (var dir in Environment.GetEnvironmentVariable("path").Split(Path.PathSeparator)) - AddDir(dir); + foreach (var dir in Environment.GetEnvironmentVariable("path") + .Split(Path.PathSeparator) + .Select(Environment.ExpandEnvironmentVariables)) + { + AddDir(dir); + } } } @@ -171,10 +176,10 @@ namespace IPA.Loader { if (dirValidator == null) dirValidator = s => true; - Stack dirs = new Stack(32); + var dirs = new Stack(32); if (!Directory.Exists(root)) - throw new ArgumentException(); + throw new ArgumentException("Directory does not exist", nameof(root)); dirs.Push(root); while (dirs.Count > 0) @@ -218,12 +223,13 @@ namespace IPA.Loader } } - [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] + [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] +#if NET461 + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] +#endif private static extern IntPtr AddDllDirectory(string lpPathName); [Flags] - [SuppressMessage("ReSharper", "InconsistentNaming")] - [SuppressMessage("ReSharper", "UnusedMember.Local")] private enum LoadLibraryFlags : uint { None = 0, @@ -233,7 +239,10 @@ namespace IPA.Loader LOAD_LIBRARY_SEARCH_USER_DIRS = 0x00000400, } - [DllImport("kernel32.dll", SetLastError = true)] + [DllImport("kernel32.dll", SetLastError = true)] +#if NET461 + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] +#endif private static extern bool SetDefaultDllDirectories(LoadLibraryFlags dwFlags); } }