Skip to content

Commit

Permalink
[PTRun] Support plugin with library dependencies (microsoft#18435)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob committed May 23, 2022
1 parent 251ea6d commit b5a2319
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
42 changes: 42 additions & 0 deletions src/modules/launcher/Wox.Plugin/PluginLoadContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Reflection;
using System.Runtime.Loader;

namespace Wox.Plugin
{
internal class PluginLoadContext : AssemblyLoadContext
{
private AssemblyDependencyResolver _resolver;

public PluginLoadContext(string pluginPath)
{
_resolver = new AssemblyDependencyResolver(pluginPath);
}

protected override Assembly Load(AssemblyName assemblyName)
{
string assemblyPath = _resolver.ResolveAssemblyToPath(assemblyName);
if (assemblyPath != null)
{
return LoadFromAssemblyPath(assemblyPath);
}

return null;
}

protected override IntPtr LoadUnmanagedDll(string unmanagedDllName)
{
string libraryPath = _resolver.ResolveUnmanagedDllToPath(unmanagedDllName);
if (libraryPath != null)
{
return LoadUnmanagedDllFromPath(libraryPath);
}

return IntPtr.Zero;
}
}
}
4 changes: 3 additions & 1 deletion src/modules/launcher/Wox.Plugin/PluginPair.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
Expand Down Expand Up @@ -134,7 +135,8 @@ private bool CreatePluginInstance()

try
{
_assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(Metadata.ExecuteFilePath);
var loadContext = new PluginLoadContext(Metadata.ExecuteFilePath);
_assembly = loadContext.LoadFromAssemblyName(new AssemblyName(Path.GetFileNameWithoutExtension(Metadata.ExecuteFilePath)));
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception e)
Expand Down

0 comments on commit b5a2319

Please sign in to comment.