Allow plugins to look up harmony types in other plugins/themselves
All checks were successful
Build / Compute Version (push) Successful in 5s
Build / Build Nuget package (CringeBootstrap.Abstractions) (push) Successful in 1m37s
Build / Build Nuget package (NuGet) (push) Successful in 2m17s
Build / Build Nuget package (CringePlugins) (push) Successful in 2m53s
Build / Build Nuget package (SharedCringe) (push) Successful in 1m59s
Build / Build Launcher (push) Successful in 3m10s

This commit is contained in:
2024-11-03 16:45:09 -05:00
parent ed4fd11de0
commit b3ef089a70
3 changed files with 14 additions and 4 deletions

View File

@@ -21,6 +21,7 @@ using VRage.ModAPI;
using VRage.ObjectBuilders;
using VRage.ObjectBuilders.Private;
using VRage.Plugins;
using CringePlugins.Loader;
namespace CringeLauncher.Patches;
@@ -80,7 +81,8 @@ public static class IntrospectionPatches
[HarmonyPrefix, HarmonyPatch(typeof(AccessTools), nameof(AccessTools.AllAssemblies))]
private static bool AllAssembliesHarmonyPrefix(ref IEnumerable<Assembly> __result)
{
__result = AssemblyLoadContext.GetLoadContext(typeof(IntrospectionPatches).Assembly)?.Assemblies ?? [];
__result = AssemblyLoadContext.GetLoadContext(typeof(IntrospectionPatches).Assembly)?.Assemblies
.Concat(PluginsLifetime.Contexts.SelectMany(x => x.Assemblies)) ?? [];
return false;
}

View File

@@ -1,6 +1,8 @@
using System.Runtime.Loader;
using System.Collections.Immutable;
using System.Runtime.Loader;
using CringeBootstrap.Abstractions;
using CringePlugins.Utils;
using SharedCringe.Loader;
using VRage.Plugins;
namespace CringePlugins.Loader;
@@ -26,12 +28,13 @@ internal sealed class PluginInstance
{
}
public void Instantiate()
public void Instantiate(ImmutableArray<DerivedAssemblyLoadContext>.Builder contextBuilder)
{
if (AssemblyLoadContext.GetLoadContext(typeof(PluginInstance).Assembly) is not ICoreLoadContext parentContext)
throw new NotSupportedException("Plugin instantiation is not supported in this context");
_context = new PluginAssemblyLoadContext(parentContext, _entrypointPath);
contextBuilder.Add(_context);
var entrypoint = _context.LoadEntrypoint();

View File

@@ -11,11 +11,14 @@ using NuGet;
using NuGet.Deps;
using NuGet.Frameworks;
using NuGet.Versioning;
using SharedCringe.Loader;
namespace CringePlugins.Loader;
public class PluginsLifetime : ILoadingStage
{
public static ImmutableArray<DerivedAssemblyLoadContext> Contexts { get; private set; } = [];
private static readonly Logger Log = LogManager.GetCurrentClassLogger();
public string Name => "Loading Plugins";
@@ -71,11 +74,12 @@ public class PluginsLifetime : ILoadingStage
private void RegisterLifetime()
{
var contextBuilder = Contexts.ToBuilder();
foreach (var instance in _plugins)
{
try
{
instance.Instantiate();
instance.Instantiate(contextBuilder);
instance.RegisterLifetime();
}
catch (Exception e)
@@ -83,6 +87,7 @@ public class PluginsLifetime : ILoadingStage
Log.Error(e, "Failed to instantiate plugin {Plugin}", instance.Metadata);
}
}
Contexts = contextBuilder.ToImmutable();
}
private async Task LoadPlugins(IReadOnlySet<CachedPackage> packages, PackageSourceMapping sourceMapping,