Skip to content

Commit

Permalink
resolving embedded dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
caunt committed Sep 26, 2024
1 parent 62df2ee commit 164bfe3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/API/Plugins/IPluginDependencyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ namespace Void.Proxy.API.Plugins;
public interface IPluginDependencyService
{
public string? ResolveAssemblyPath(AssemblyName assemblyName);
public Stream? ResolveEmbeddedAssemblyStream(AssemblyName assemblyName);
}
21 changes: 21 additions & 0 deletions src/Platform/Plugins/PluginDependencyService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Reflection;
using System.Runtime.Versioning;
using System.Threading;
using Nito.Disposables.Internals;
using NuGet.Common;
using NuGet.Configuration;
Expand Down Expand Up @@ -35,6 +36,26 @@ public class PluginDependencyService(ILogger<PluginDependencyService> logger) :
return assemblyPath;
}

public Stream? ResolveEmbeddedAssemblyStream(AssemblyName assemblyName)
{
logger.LogInformation("Resolving {AssemblyName} embedded dependency", assemblyName.Name);

if (string.IsNullOrWhiteSpace(assemblyName.Name))
return null;

var assembly = Assembly.GetExecutingAssembly();
var resource = assembly.GetManifestResourceNames().FirstOrDefault(name => name.StartsWith(assemblyName.Name));

if (string.IsNullOrWhiteSpace(resource))
return null;

if (assembly.GetManifestResourceStream(resource) is { } stream)
return stream;

logger.LogWarning("Embedded assembly {ResourceName} couldn't be loaded", resource);
return null;
}

private async ValueTask<string?> ResolveAssemblyFromNuGetAsync(AssemblyName assemblyName, CancellationToken cancellationToken = default)
{
var assemblyPath = await ResolveAssemblyFromOfflineNuGetAsync(assemblyName, cancellationToken);
Expand Down
2 changes: 1 addition & 1 deletion src/Platform/Plugins/PluginService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public async ValueTask LoadEmbeddedPluginsAsync(CancellationToken cancellationTo
{
if (assembly.GetManifestResourceStream(resourceName) is not { } stream)
{
logger.LogWarning("Embedded plugin {PluginName} couldn't be extracted", resourceName);
logger.LogWarning("Embedded plugin {PluginName} couldn't be loaded", resourceName);
continue;
}

Expand Down
10 changes: 10 additions & 0 deletions src/Platform/Reflection/PluginLoadContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,18 @@ protected override Assembly Load(AssemblyName assemblyName)
_logger.LogTrace("Loading {AssemblyName} assembly into {ContextName} context", assemblyName.Name, Name);

if (VersionedDependencies.Any(assemblyName.FullName.StartsWith))
{
assembly = Default.Assemblies.FirstOrDefault(loadedAssembly => loadedAssembly.FullName == assemblyName.FullName);

if (assembly is null)
{
using var assemblyStream = _dependencies.ResolveEmbeddedAssemblyStream(assemblyName);

if (assemblyStream is not null)
return LoadFromStream(assemblyStream);
}
}

if (SharedDependencies.Any(assemblyName.FullName.StartsWith) && assembly is null)
assembly = Default.Assemblies.FirstOrDefault(loadedAssembly => loadedAssembly.GetName().Name == assemblyName.Name);

Expand Down

0 comments on commit 164bfe3

Please sign in to comment.