fix harmony patches in plugins
All checks were successful
Build / Compute Version (push) Successful in 5s
Build / Build Nuget package (CringeBootstrap.Abstractions) (push) Successful in 1m53s
Build / Build Nuget package (NuGet) (push) Successful in 2m2s
Build / Build Nuget package (CringePlugins) (push) Successful in 2m49s
Build / Build Launcher (push) Successful in 3m28s
Build / Build Nuget package (SharedCringe) (push) Successful in 4m55s

This commit is contained in:
zznty
2024-11-04 01:05:22 +07:00
parent 010e477ea4
commit a9a203e5a8
2 changed files with 20 additions and 4 deletions

View File

@@ -65,8 +65,9 @@ public static class IntrospectionPatches
{
if (AssemblyLoadContext.GetLoadContext(assembly) is ICoreLoadContext)
return true;
__result = IntrospectionContext.Global.CollectAttributedTypes<HarmonyAttribute>(assembly.GetMainModule())
// static classes are abstract
__result = IntrospectionContext.Global.CollectAttributedTypes<HarmonyAttribute>(assembly.GetMainModule(), true)
.ToArray();
return false;
}

View File

@@ -1,5 +1,6 @@
using System.Reflection;
using dnlib.DotNet;
using VRage.FileSystem;
namespace CringePlugins.Utils;
@@ -7,14 +8,28 @@ public class IntrospectionContext
{
public static IntrospectionContext Global { get; } = new();
internal readonly ModuleContext Context = ModuleDef.CreateModuleContext();
internal readonly ModuleContext Context;
public IntrospectionContext()
{
var assemblyResolver = new AssemblyResolver();
assemblyResolver.PreSearchPaths.Add(AppContext.BaseDirectory);
assemblyResolver.PreSearchPaths.Add(MyFileSystem.ExePath);
Context = new(assemblyResolver);
}
public IEnumerable<Type> CollectAttributedTypes<TAttribute>(Module module, bool allowAbstract = false) where TAttribute : Attribute
{
var moduleDef = ModuleDefMD.Load(module, Context);
var token = moduleDef.ImportAsTypeSig(typeof(TAttribute));
return moduleDef.GetTypes()
.Where(b => b.CustomAttributes.IsDefined(typeof(TAttribute).FullName) && (allowAbstract || !b.IsAbstract))
.Where(b => b.CustomAttributes.Any(a =>
a.AttributeType.FullName == token.FullName || MatchBaseType(a.AttributeType, token)) &&
(allowAbstract || !b.IsAbstract))
.Select(b => module.GetType(b.FullName.Replace('/', '+'), true, false)!);
}