diff --git a/DFAssist.Loader/AssemblyResolver.cs b/DFAssist.Loader/AssemblyResolver.cs index b6e6786..4a727fb 100644 --- a/DFAssist.Loader/AssemblyResolver.cs +++ b/DFAssist.Loader/AssemblyResolver.cs @@ -15,6 +15,7 @@ public class AssemblyResolver private bool _attached; private bool _initialized; private string _librariesPath; + private string _pdbPath; private ActPluginData _pluginData; private IActPluginV1 _plugin; @@ -22,7 +23,7 @@ public bool Attach(IActPluginV1 plugin) { if (_attached) return true; - + _attached = true; _plugin = plugin; @@ -33,28 +34,48 @@ public bool Attach(IActPluginV1 plugin) private void Initialize(IActPluginV1 plugin) { - if(_initialized) + if (_initialized) return; try { _pluginData = ActGlobals.oFormActMain.PluginGetSelfData(plugin); - if(_pluginData == null) + if (_pluginData == null) + { + ActGlobals.oFormActMain.ParseRawLogLine(false, DateTime.Now, "[DFAssist] Unable to find DFAssist data from ActGlobals.oFormActMain!"); return; + } var enviroment = Path.GetDirectoryName(_pluginData.pluginFile.ToString()); if (string.IsNullOrWhiteSpace(enviroment)) + { + ActGlobals.oFormActMain.ParseRawLogLine(false, DateTime.Now, "[DFAssist] Unable to find the plugin base directory!"); return; + } _librariesPath = Path.Combine(enviroment, "libs"); if (!Directory.Exists(_librariesPath)) + { + ActGlobals.oFormActMain.ParseRawLogLine(false, DateTime.Now, "[DFAssist] Unable to find the 'libs' directory!"); return; + } + +#if DEBUG + // we also add the pdb folder to load, + // this is completely optional and will be done only if we are in debug + _pdbPath = Path.Combine(enviroment, "pdb"); + if(!Directory.Exists(_pdbPath)) + { + ActGlobals.oFormActMain.ParseRawLogLine(false, DateTime.Now, "[DFAssist] Unable to find the 'pdb' directory!"); + return; + } +#endif _initialized = true; } catch (Exception) { - Debug.WriteLine("There was an error when attaching to AssemblyResolve!"); + ActGlobals.oFormActMain.ParseRawLogLine(false, DateTime.Now, "[DFAssist] There was an error when attaching to AssemblyResolve!"); throw; } } @@ -66,7 +87,7 @@ public void Detach() _attached = false; _initialized = false; - + AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve; _librariesPath = null; _pluginData = null; @@ -80,22 +101,42 @@ private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs a if (!_initialized || args.Name.Contains(".resources") - || args.RequestingAssembly == null - || GetAssemblyName(args.RequestingAssembly.FullName) != nameof(DFAssist)) + || args.RequestingAssembly == null) + return null; + + // check if any of the assemblies of this plugin is requesting an AssemblyResolve + var requestingAssemblyName = GetAssemblyName(args.RequestingAssembly.FullName); + if(requestingAssemblyName != "DFAssist" + && requestingAssemblyName != "DFAssist.Plugin" + && requestingAssemblyName != "DFAssist.Core" + && requestingAssemblyName != "DFAssist.Contracts" + && requestingAssemblyName != "DFAssist.WinToast") return null; var assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.FullName == args.Name); if (assembly != null) return assembly; - var filename = GetAssemblyName(args.Name) + ".dll".ToLower(); - var asmFile = Path.Combine(_librariesPath, filename); - - if (File.Exists(asmFile)) + var filename = GetAssemblyName(args.Name); + var dllFile = filename + ".dll".ToLower(); + var dllFileFullPath = Path.Combine(_librariesPath, dllFile); +#if DEBUG + var pdbFile = filename + ".pdb".ToLower(); + var pdbFileFullPath = Path.Combine(_pdbPath, pdbFile); +#endif + if (File.Exists(dllFileFullPath)) { try { - return Assembly.LoadFrom(asmFile); + var dllBytes = File.ReadAllBytes(dllFileFullPath); +#if DEBUG + if(File.Exists(pdbFileFullPath)) + { + var pdbBytes = File.ReadAllBytes(pdbFileFullPath); + return Assembly.Load(dllBytes, pdbBytes); + } +#endif + return Assembly.Load(dllBytes); } catch (Exception) { @@ -104,7 +145,7 @@ private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs a } } - _pluginData.lblPluginStatus.Text = $"Unable to find {asmFile}, the plugin cannot be starterd."; + _pluginData.lblPluginStatus.Text = $"Unable to find {dllFile}, the plugin cannot be starterd."; return null; } diff --git a/DFAssist/App.config b/DFAssist/App.config index b277f73..58ae9fc 100644 --- a/DFAssist/App.config +++ b/DFAssist/App.config @@ -3,6 +3,11 @@ + + + + +