add bootstrap and plugins prototype

This commit is contained in:
zznty
2024-10-10 00:38:19 +07:00
parent c7757a2279
commit 0933915c64
32 changed files with 1487 additions and 76 deletions

View File

@@ -0,0 +1,49 @@
using System.Reflection;
using dnlib.DotNet;
namespace CringePlugins.Utils;
public class IntrospectionContext
{
public static IntrospectionContext Global { get; } = new();
private readonly ModuleContext _context = ModuleDef.CreateModuleContext();
public IEnumerable<Type> CollectAttributedTypes<TAttribute>(Module module, bool allowAbstract = false) where TAttribute : Attribute
{
var moduleDef = ModuleDefMD.Load(module, _context);
return moduleDef.GetTypes()
.Where(b => b.CustomAttributes.IsDefined(typeof(TAttribute).FullName) && (allowAbstract || !b.IsAbstract))
.Select(b => module.GetType(b.FullName, true, false)!);
}
public IEnumerable<Type> CollectDerivedTypes<T>(Module module, bool allowAbstract = false)
{
var moduleDef = ModuleDefMD.Load(module, _context);
var token = moduleDef.ImportAsTypeSig(typeof(T));
return moduleDef.GetTypes()
.Where(b => (typeof(T).IsInterface
? b.Interfaces.Any(i => i.Interface.FullName == token.FullName)
: MatchBaseType(b, token)) && (allowAbstract || !b.IsAbstract))
.Select(b => module.GetType(b.FullName, true, false)!);
}
private static bool MatchBaseType(ITypeDefOrRef? defOrRef, TypeSig token)
{
while ((defOrRef = defOrRef.GetBaseType()) != null)
{
if (defOrRef.FullName == token.FullName)
return true;
}
return false;
}
}
public static class AssemblyExtensions
{
public static Module GetMainModule(this Assembly assembly) => assembly.GetModule(assembly.GetName().Name! + ".dll") ?? assembly.GetModules()[0];
}