From 8af4c7110149dc7a6eab398e5d97e31830daf000 Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Thu, 18 Aug 2022 10:29:30 -0600 Subject: [PATCH 1/6] Change assembly loadig --- Source/LibationFileManager/InteropFactory.cs | 47 +++++++++++++------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/Source/LibationFileManager/InteropFactory.cs b/Source/LibationFileManager/InteropFactory.cs index 6e5cf96b..ce674ae4 100644 --- a/Source/LibationFileManager/InteropFactory.cs +++ b/Source/LibationFileManager/InteropFactory.cs @@ -21,7 +21,7 @@ namespace LibationFileManager private static IInteropFunctions _create(params object[] values) => InteropFunctionsType is null ? new NullInteropFunctions() -//: values is null || values.Length == 0 ? Activator.CreateInstance(InteropFunctionsType) as IInteropFunctions + //: values is null || values.Length == 0 ? Activator.CreateInstance(InteropFunctionsType) as IInteropFunctions : Activator.CreateInstance(InteropFunctionsType, values) as IInteropFunctions; #region load types @@ -32,21 +32,27 @@ namespace LibationFileManager : Configuration.IsMacOs ? a => Path.GetFileName(a).StartsWithInsensitive("mac") || Path.GetFileName(a).StartsWithInsensitive("osx") : _ => false; - private const string CONFIG_APP_ENDING = "ConfigApp.exe"; + private const string CONFIG_APP_ENDING = "ConfigApp.dll"; private static List ModuleList { get; } = new(); static InteropFactory() { // searches file names for potential matches; doesn't run anything var configApp = getOSConfigApp(); +#if DEBUG + // nothing to load if (configApp is null) + { + Serilog.Log.Logger.Error($"Unable to locate *{CONFIG_APP_ENDING}"); return; + } // runs the exe and gets the exe's loaded modules ModuleList = LoadModuleList(Path.GetFileNameWithoutExtension(configApp)) .OrderBy(x => x.ModuleName) .ToList(); +#endif AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; @@ -61,18 +67,13 @@ namespace LibationFileManager var here = Path.GetDirectoryName(Environment.ProcessPath); // find '*ConfigApp.exe' files - var exes = - Directory.EnumerateFiles(here, $"*{CONFIG_APP_ENDING}", SearchOption.TopDirectoryOnly) + var appName = + Directory.EnumerateFiles(here, $"*{CONFIG_APP_ENDING}*", SearchOption.TopDirectoryOnly) // sanity check. shouldn't ever be true .Except(new[] { Environment.ProcessPath }) - .Where(exe => - // has a corresponding dll - File.Exists(Path.ChangeExtension(exe, "dll")) - && MatchesOS(exe) - ) - .ToList(); - var exeName = exes.FirstOrDefault(); - return exeName; + .FirstOrDefault(exe => MatchesOS(exe)); + + return appName; } private static List LoadModuleList(string exeName) @@ -115,12 +116,28 @@ namespace LibationFileManager private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) { // e.g. "System.Windows.Forms, Version=6.0.2.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" - var asmName = args.Name.Split(',')[0]; + var asmName = args.Name.Split(',')[0] + ".dll"; + +#if DEBUG // `First` instead of `FirstOrDefault`. If it's not present we're going to fail anyway. May as well be here - var module = ModuleList.First(m => m.ModuleName.StartsWith(asmName)); + var modulePath = ModuleList.SingleOrDefault(m => m.ModuleName.EqualsInsensitive(asmName)).FileName; +#else + var here = Path.GetDirectoryName(Environment.ProcessPath); - return Assembly.LoadFrom(module.FileName); + // find '*ConfigApp.dll' files + var modulePath = + Directory.EnumerateFiles(here, asmName, SearchOption.TopDirectoryOnly) + .SingleOrDefault(); + +#endif + if (modulePath is null) + { + Serilog.Log.Logger.Error($"Unable to load module {args.Name}"); + return null; + } + + return Assembly.LoadFrom(modulePath); } #endregion From 09cc838bb488847ef35c7427b182de469ebc27f2 Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Thu, 18 Aug 2022 10:45:07 -0600 Subject: [PATCH 2/6] Checks --- Source/LibationFileManager/InteropFactory.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Source/LibationFileManager/InteropFactory.cs b/Source/LibationFileManager/InteropFactory.cs index ce674ae4..61fae68b 100644 --- a/Source/LibationFileManager/InteropFactory.cs +++ b/Source/LibationFileManager/InteropFactory.cs @@ -39,8 +39,6 @@ namespace LibationFileManager // searches file names for potential matches; doesn't run anything var configApp = getOSConfigApp(); -#if DEBUG - // nothing to load if (configApp is null) { @@ -48,6 +46,9 @@ namespace LibationFileManager return; } +#if DEBUG + + // runs the exe and gets the exe's loaded modules ModuleList = LoadModuleList(Path.GetFileNameWithoutExtension(configApp)) .OrderBy(x => x.ModuleName) @@ -56,7 +57,7 @@ namespace LibationFileManager AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; - var configAppAssembly = Assembly.LoadFrom(Path.ChangeExtension(configApp, "dll")); + var configAppAssembly = Assembly.LoadFrom(configApp); var type = typeof(IInteropFunctions); InteropFunctionsType = configAppAssembly .GetTypes() @@ -121,7 +122,7 @@ namespace LibationFileManager #if DEBUG // `First` instead of `FirstOrDefault`. If it's not present we're going to fail anyway. May as well be here - var modulePath = ModuleList.SingleOrDefault(m => m.ModuleName.EqualsInsensitive(asmName)).FileName; + var modulePath = ModuleList.SingleOrDefault(m => m.ModuleName.EqualsInsensitive(asmName))?.FileName; #else var here = Path.GetDirectoryName(Environment.ProcessPath); From 1927d1996100d2d4819d051a17e22daafddafa00 Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Thu, 18 Aug 2022 10:47:53 -0600 Subject: [PATCH 3/6] comments --- Source/LibationFileManager/InteropFactory.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/LibationFileManager/InteropFactory.cs b/Source/LibationFileManager/InteropFactory.cs index 61fae68b..81d2c15c 100644 --- a/Source/LibationFileManager/InteropFactory.cs +++ b/Source/LibationFileManager/InteropFactory.cs @@ -121,7 +121,6 @@ namespace LibationFileManager #if DEBUG - // `First` instead of `FirstOrDefault`. If it's not present we're going to fail anyway. May as well be here var modulePath = ModuleList.SingleOrDefault(m => m.ModuleName.EqualsInsensitive(asmName))?.FileName; #else var here = Path.GetDirectoryName(Environment.ProcessPath); @@ -134,6 +133,7 @@ namespace LibationFileManager #endif if (modulePath is null) { + //Let the runtime handle any dll not found exceptions. Serilog.Log.Logger.Error($"Unable to load module {args.Name}"); return null; } From 9dd5940c8ca41803a1cf3da494cf3c76b9472d22 Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Thu, 18 Aug 2022 10:59:00 -0600 Subject: [PATCH 4/6] Remove trailing wild --- Source/LibationFileManager/InteropFactory.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Source/LibationFileManager/InteropFactory.cs b/Source/LibationFileManager/InteropFactory.cs index 81d2c15c..397b5cf0 100644 --- a/Source/LibationFileManager/InteropFactory.cs +++ b/Source/LibationFileManager/InteropFactory.cs @@ -48,7 +48,6 @@ namespace LibationFileManager #if DEBUG - // runs the exe and gets the exe's loaded modules ModuleList = LoadModuleList(Path.GetFileNameWithoutExtension(configApp)) .OrderBy(x => x.ModuleName) @@ -69,7 +68,7 @@ namespace LibationFileManager // find '*ConfigApp.exe' files var appName = - Directory.EnumerateFiles(here, $"*{CONFIG_APP_ENDING}*", SearchOption.TopDirectoryOnly) + Directory.EnumerateFiles(here, $"*{CONFIG_APP_ENDING}", SearchOption.TopDirectoryOnly) // sanity check. shouldn't ever be true .Except(new[] { Environment.ProcessPath }) .FirstOrDefault(exe => MatchesOS(exe)); From 6d2624d52b63e086d3fdc0625a97bec0e3b8c3bc Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Thu, 18 Aug 2022 10:59:37 -0600 Subject: [PATCH 5/6] Fix comment --- Source/LibationFileManager/InteropFactory.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/LibationFileManager/InteropFactory.cs b/Source/LibationFileManager/InteropFactory.cs index 397b5cf0..84af0996 100644 --- a/Source/LibationFileManager/InteropFactory.cs +++ b/Source/LibationFileManager/InteropFactory.cs @@ -66,7 +66,7 @@ namespace LibationFileManager { var here = Path.GetDirectoryName(Environment.ProcessPath); - // find '*ConfigApp.exe' files + // find '*ConfigApp.dll' files var appName = Directory.EnumerateFiles(here, $"*{CONFIG_APP_ENDING}", SearchOption.TopDirectoryOnly) // sanity check. shouldn't ever be true From 17181811f05fcf95af0999379a4d7d0afae9560c Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Thu, 18 Aug 2022 11:21:40 -0600 Subject: [PATCH 6/6] Remove assembly hot loading --- Source/LibationFileManager/InteropFactory.cs | 29 ++++++++++++++------ 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/Source/LibationFileManager/InteropFactory.cs b/Source/LibationFileManager/InteropFactory.cs index 84af0996..d596c9ab 100644 --- a/Source/LibationFileManager/InteropFactory.cs +++ b/Source/LibationFileManager/InteropFactory.cs @@ -46,13 +46,17 @@ namespace LibationFileManager return; } + /* + * Commented code used to locate assemblies from the *ConfigApp.exe's module list. + * Use this method to locate dependencies when they are not in Libation's program files directory. #if DEBUG - // runs the exe and gets the exe's loaded modules - ModuleList = LoadModuleList(Path.GetFileNameWithoutExtension(configApp)) - .OrderBy(x => x.ModuleName) - .ToList(); + // runs the exe and gets the exe's loaded modules + ModuleList = LoadModuleList(Path.GetFileNameWithoutExtension(configApp)) + .OrderBy(x => x.ModuleName) + .ToList(); #endif + */ AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; @@ -76,6 +80,9 @@ namespace LibationFileManager return appName; } + /* + * Use this method to locate dependencies when they are not in Libation's program files directory. + * private static List LoadModuleList(string exeName) { var proc = new Process @@ -112,24 +119,30 @@ namespace LibationFileManager return modules; } + */ private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) { // e.g. "System.Windows.Forms, Version=6.0.2.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" var asmName = args.Name.Split(',')[0] + ".dll"; -#if DEBUG + + /* + * Commented code used to locate assemblies from the *ConfigApp.exe's module list. + * Use this method to locate dependencies when they are not in Libation's program files directory. + #if DEBUG var modulePath = ModuleList.SingleOrDefault(m => m.ModuleName.EqualsInsensitive(asmName))?.FileName; -#else + #else + */ var here = Path.GetDirectoryName(Environment.ProcessPath); - // find '*ConfigApp.dll' files + // find the requested assembly in the program files directory var modulePath = Directory.EnumerateFiles(here, asmName, SearchOption.TopDirectoryOnly) .SingleOrDefault(); -#endif + //#endif if (modulePath is null) { //Let the runtime handle any dll not found exceptions.