More cleanup

This commit is contained in:
D1GQ committed 2026-06-29 22:18:17 -05:00
1 parent 9e42b79c3c
commit 0fa01eed42
3 files changed
+29 -22

No files matched your search

+20 -10
View File
@@ -13,21 +13,27 @@ internal abstract class AutoRegisterAttribute : Attribute
/// <summary>
/// Scans the entire assembly and registers all instances of classes marked with <see cref="AutoRegisterAttribute"/> subclasses.
/// </summary>
internal static void RegisterAll()
internal static void Initialize()
{
var types = ModInfo.Assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(AutoRegisterAttribute)) && !t.IsAbstract && t.IsSealed).ToArray();
var types = ModInfo.Assembly.GetTypes();
foreach (var type in types)
{
if (type.IsAbstract || !type.IsSealed)
continue;
if (!typeof(AutoRegisterAttribute).IsAssignableFrom(type))
continue;
var tempAttribute = (AutoRegisterAttribute)FormatterServices.GetUninitializedObject(type);
tempAttribute.RegisterInstances();
tempAttribute.Register();
}
}
/// <summary>
/// When implemented in a derived class, registers instances of a specific type discovered through reflection.
/// When implemented in a derived class, registers individual instances.
/// </summary>
protected abstract void RegisterInstances();
protected abstract void Register();
}
/// <summary>
@@ -55,16 +61,20 @@ internal abstract class AutoRegisterAttribute<T> : AutoRegisterAttribute where T
internal static J? GetInstance<J>() where J : T => (J?)_instances.FirstOrDefault(instance => instance.GetType() == typeof(J));
/// <inheritdoc/>
protected override void RegisterInstances()
protected override void Register()
{
var attributedTypes = ModInfo.Assembly.GetTypes().Where(t => t.GetCustomAttributes(GetType(), false).Any()).Where(t => !t.IsAbstract && !t.IsInterface);
var types = ModInfo.Assembly.GetTypes();
foreach (var type in attributedTypes)
foreach (var type in types)
{
// Check if the type implements the interface or inherits from the base class
if (type.GetCustomAttribute(GetType()) == null)
continue;
if (type.IsAbstract || type.IsInterface)
continue;
if (typeof(T).IsAssignableFrom(type))
{
// Try to get any parameterless constructor (public, private, or internal)
var constructor = type.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, null, Type.EmptyTypes, null);
if (constructor != null)
+7 -10
View File
@@ -19,21 +19,18 @@ internal class RegisterInIl2Cpp(params Type[] interfaces) : Attribute
/// <summary>
/// Registers all classes marked with <see cref="RegisterInIl2Cpp"/> from the executing assembly.
/// </summary>
internal static void RegisterAll()
internal static void Initialize()
{
var assembly = ModInfo.Assembly;
var types = assembly.GetTypes()
.Where(t => t.GetCustomAttribute<RegisterInIl2Cpp>() != null)
.ToList();
var types = ModInfo.Assembly.GetTypes();
foreach (var type in types)
{
var attr = type.GetCustomAttribute<RegisterInIl2Cpp>();
if (attr == null)
continue;
try
{
var attr = type.GetCustomAttribute<RegisterInIl2Cpp>();
if (attr.Interfaces.Any())
if (attr.Interfaces.Length > 0)
ClassInjector.RegisterTypeInIl2Cpp(type, new RegisterTypeOptions { Interfaces = attr.Interfaces });
else
ClassInjector.RegisterTypeInIl2Cpp(type);
+2 -2
View File
@@ -135,7 +135,7 @@ internal class BAUPlugin : BasePlugin
SetupConsole();
}
RegisterInIl2Cpp.RegisterAll();
RegisterInIl2Cpp.Initialize();
IL2CPPChainloader.Instance.Finished += OnChainloaderFinished;
}
catch (Exception ex)
@@ -162,7 +162,7 @@ internal class BAUPlugin : BasePlugin
MonoExtensionPatch.Patch(Harmony);
GameSettingsPatch.SetupSettings(true);
BAUModdedSupportEvents.OnBAUOptionsLoadedEvent.InvokeAll([.. OptionItem.AllOptions.Cast<object>()]);
AutoRegisterAttribute.RegisterAll();
AutoRegisterAttribute.Initialize();
IMonoExtension.RegisterAll();
OutfitData.Initialize();
SceneManager.add_sceneLoaded((Action<Scene, LoadSceneMode>)OnSceneLoaded);