From a9a203e5a879d9983ee79259cad2f038a0620ffc Mon Sep 17 00:00:00 2001 From: zznty <94796179+zznty@users.noreply.github.com> Date: Mon, 4 Nov 2024 01:05:22 +0700 Subject: [PATCH] fix harmony patches in plugins --- .../Patches/IntrospectionPatches.cs | 5 +++-- CringePlugins/Utils/IntrospectionContext.cs | 19 +++++++++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/CringeLauncher/Patches/IntrospectionPatches.cs b/CringeLauncher/Patches/IntrospectionPatches.cs index 3e64826..0422dd5 100644 --- a/CringeLauncher/Patches/IntrospectionPatches.cs +++ b/CringeLauncher/Patches/IntrospectionPatches.cs @@ -65,8 +65,9 @@ public static class IntrospectionPatches { if (AssemblyLoadContext.GetLoadContext(assembly) is ICoreLoadContext) return true; - - __result = IntrospectionContext.Global.CollectAttributedTypes(assembly.GetMainModule()) + + // static classes are abstract + __result = IntrospectionContext.Global.CollectAttributedTypes(assembly.GetMainModule(), true) .ToArray(); return false; } diff --git a/CringePlugins/Utils/IntrospectionContext.cs b/CringePlugins/Utils/IntrospectionContext.cs index 4ebdf2f..2a35cae 100644 --- a/CringePlugins/Utils/IntrospectionContext.cs +++ b/CringePlugins/Utils/IntrospectionContext.cs @@ -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 CollectAttributedTypes(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)!); }