Skip to content

Commit

Permalink
# 36 Changed AssemblyResolver to Load assemblies without locking files
Browse files Browse the repository at this point in the history
As suggested from @EQAditu
  • Loading branch information
easly1989 committed Nov 6, 2019
1 parent c78ee64 commit 478da8e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 13 deletions.
67 changes: 54 additions & 13 deletions DFAssist.Loader/AssemblyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ public class AssemblyResolver
private bool _attached;
private bool _initialized;
private string _librariesPath;
private string _pdbPath;
private ActPluginData _pluginData;
private IActPluginV1 _plugin;

public bool Attach(IActPluginV1 plugin)
{
if (_attached)
return true;

_attached = true;
_plugin = plugin;

Expand All @@ -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;
}
}
Expand All @@ -66,7 +87,7 @@ public void Detach()

_attached = false;
_initialized = false;

AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;
_librariesPath = null;
_pluginData = null;
Expand All @@ -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)
{
Expand All @@ -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;
}

Expand Down
5 changes: 5 additions & 0 deletions DFAssist/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="libs" />
</assemblyBinding>
</runtime>
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true"/>
Expand Down

0 comments on commit 478da8e

Please sign in to comment.