From 0fa01eed42703047a118b3fbb4090630f65c24d9 Mon Sep 17 00:00:00 2001 From: D1GQ Date: Mon, 29 Jun 2026 22:18:17 -0500 Subject: [PATCH] More cleanup --- src/Attributes/AutoRegisterAttribute.cs | 30 ++++++++++++++++--------- src/Attributes/RegisterInIl2Cpp.cs | 17 ++++++-------- src/BAUPlugin.cs | 4 ++-- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/Attributes/AutoRegisterAttribute.cs b/src/Attributes/AutoRegisterAttribute.cs index ab59400..9482258 100644 --- a/src/Attributes/AutoRegisterAttribute.cs +++ b/src/Attributes/AutoRegisterAttribute.cs @@ -13,21 +13,27 @@ internal abstract class AutoRegisterAttribute : Attribute /// /// Scans the entire assembly and registers all instances of classes marked with subclasses. /// - 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(); } } /// - /// When implemented in a derived class, registers instances of a specific type discovered through reflection. + /// When implemented in a derived class, registers individual instances. /// - protected abstract void RegisterInstances(); + protected abstract void Register(); } /// @@ -55,16 +61,20 @@ internal abstract class AutoRegisterAttribute : AutoRegisterAttribute where T internal static J? GetInstance() where J : T => (J?)_instances.FirstOrDefault(instance => instance.GetType() == typeof(J)); /// - 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) diff --git a/src/Attributes/RegisterInIl2Cpp.cs b/src/Attributes/RegisterInIl2Cpp.cs index 799da76..a4e43ce 100644 --- a/src/Attributes/RegisterInIl2Cpp.cs +++ b/src/Attributes/RegisterInIl2Cpp.cs @@ -19,21 +19,18 @@ internal class RegisterInIl2Cpp(params Type[] interfaces) : Attribute /// /// Registers all classes marked with from the executing assembly. /// - internal static void RegisterAll() + internal static void Initialize() { - var assembly = ModInfo.Assembly; - - var types = assembly.GetTypes() - .Where(t => t.GetCustomAttribute() != null) - .ToList(); - + var types = ModInfo.Assembly.GetTypes(); foreach (var type in types) { + var attr = type.GetCustomAttribute(); + if (attr == null) + continue; + try { - var attr = type.GetCustomAttribute(); - - if (attr.Interfaces.Any()) + if (attr.Interfaces.Length > 0) ClassInjector.RegisterTypeInIl2Cpp(type, new RegisterTypeOptions { Interfaces = attr.Interfaces }); else ClassInjector.RegisterTypeInIl2Cpp(type); diff --git a/src/BAUPlugin.cs b/src/BAUPlugin.cs index ab4076a..b2ee6e1 100644 --- a/src/BAUPlugin.cs +++ b/src/BAUPlugin.cs @@ -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()]); - AutoRegisterAttribute.RegisterAll(); + AutoRegisterAttribute.Initialize(); IMonoExtension.RegisterAll(); OutfitData.Initialize(); SceneManager.add_sceneLoaded((Action)OnSceneLoaded);