From 5fb59408fa4ec388cd3a1ca93505a2846628b457 Mon Sep 17 00:00:00 2001 From: xChipseq Date: Sun, 31 Aug 2025 17:51:02 +0200 Subject: [PATCH] wip --- MiraAPI.Example/ExampleLocalSetings.cs | 13 ++---- MiraAPI.Example/ExamplePlugin.cs | 6 ++- .../Attributes/LocalSettingAttribute.cs | 15 ++++++- .../Attributes/LocalSettingToggleAttribute.cs | 17 ++++++- MiraAPI/LocalSettings/LocalSettingsTab.cs | 20 +++------ .../SettingTypes/LocalSettingBase.cs | 8 ++-- ...lSetingToggle.cs => LocalSettingToggle.cs} | 36 ++++++--------- MiraAPI/PluginLoading/MiraPluginManager.cs | 45 +++++++++++++++++++ 8 files changed, 105 insertions(+), 55 deletions(-) rename MiraAPI/LocalSettings/SettingTypes/{LocalSetingToggle.cs => LocalSettingToggle.cs} (83%) diff --git a/MiraAPI.Example/ExampleLocalSetings.cs b/MiraAPI.Example/ExampleLocalSetings.cs index 0feb0f0..c682617 100644 --- a/MiraAPI.Example/ExampleLocalSetings.cs +++ b/MiraAPI.Example/ExampleLocalSetings.cs @@ -7,13 +7,8 @@ namespace MiraAPI.Example; public static class ExampleLocalSetings { - public static LocalSettingsTab ModTab { get; } = new LocalSettingsTab("Example Tab"); + public static LocalSettingsTab ModTab { get; } = new("Example Tab"); - [LocalSettingToggle] - public static ConfigEntry ExampleToggleSetting { get; set; } - - internal static void BindSettings(ConfigFile config) - { - ExampleToggleSetting = config.Bind("General", "Example Toggle", false); - } -} \ No newline at end of file + [LocalSetting] + public static ConfigEntry ExampleToggleSetting { get; private set; } +} diff --git a/MiraAPI.Example/ExamplePlugin.cs b/MiraAPI.Example/ExamplePlugin.cs index 19d23e7..3819347 100644 --- a/MiraAPI.Example/ExamplePlugin.cs +++ b/MiraAPI.Example/ExamplePlugin.cs @@ -1,4 +1,5 @@ -using BepInEx; +using System.Linq; +using BepInEx; using BepInEx.Configuration; using BepInEx.Unity.IL2CPP; using HarmonyLib; @@ -21,8 +22,9 @@ public partial class ExamplePlugin : BasePlugin, IMiraPlugin public ConfigFile GetConfigFile() => Config; public override void Load() { - ExampleLocalSetings.BindSettings(Config); ExampleEventHandlers.Initialize(); Harmony.PatchAll(); + + Config.Keys. } } diff --git a/MiraAPI/LocalSettings/Attributes/LocalSettingAttribute.cs b/MiraAPI/LocalSettings/Attributes/LocalSettingAttribute.cs index 3d72d3c..6735d4e 100644 --- a/MiraAPI/LocalSettings/Attributes/LocalSettingAttribute.cs +++ b/MiraAPI/LocalSettings/Attributes/LocalSettingAttribute.cs @@ -1,11 +1,22 @@ using System; +using BepInEx.Configuration; +using UnityEngine; namespace MiraAPI.LocalSettings.Attributes; /// /// Base class for all local settings attributes. /// +/// The local setting type the attribute handles. [AttributeUsage(AttributeTargets.Property)] -public abstract class LocalSettingAttribute : Attribute +public abstract class LocalSettingAttribute( + LocalSettingsTab tab, + string? name = null, + string? description = null) : Attribute { -} + /// + /// Returns the created setting object. + /// + /// The created class. + protected abstract GameObject CreateSetting(); +} \ No newline at end of file diff --git a/MiraAPI/LocalSettings/Attributes/LocalSettingToggleAttribute.cs b/MiraAPI/LocalSettings/Attributes/LocalSettingToggleAttribute.cs index 428acf1..7a8cd34 100644 --- a/MiraAPI/LocalSettings/Attributes/LocalSettingToggleAttribute.cs +++ b/MiraAPI/LocalSettings/Attributes/LocalSettingToggleAttribute.cs @@ -1,4 +1,7 @@ using System; +using BepInEx.Configuration; +using MiraAPI.LocalSettings.SettingTypes; +using UnityEngine; namespace MiraAPI.LocalSettings.Attributes; @@ -6,6 +9,18 @@ namespace MiraAPI.LocalSettings.Attributes; /// Creates a toggle and binds it with the config entry. /// [AttributeUsage(AttributeTargets.Property)] -public class LocalSettingToggleAttribute : LocalSettingAttribute +public class LocalSettingToggleAttribute( + LocalSettingsTab tab, + string? name = null, + string? description = null, + Color? toggleColor = null, + Color? toggleHoverColor = null, + Color? toggleActiveColor = null + ) : LocalSettingAttribute(tab, name, description) { + /// + protected override LocalSettingToggle CreateSetting(ConfigEntryBase configEntry) + { + return new LocalSettingToggle(tab, configEntry, name, description, toggleColor, toggleHoverColor, toggleActiveColor); + } } diff --git a/MiraAPI/LocalSettings/LocalSettingsTab.cs b/MiraAPI/LocalSettings/LocalSettingsTab.cs index 8a6b2dc..e956589 100644 --- a/MiraAPI/LocalSettings/LocalSettingsTab.cs +++ b/MiraAPI/LocalSettings/LocalSettingsTab.cs @@ -25,16 +25,6 @@ public class LocalSettingsTab public List Settings { get; } - /// - public LocalSettingsTab(string tabName) : this( - tabName, - Color.white, - Palette.AcceptedGreen, - Palette.AcceptedGreen, - createButton: true) - { - } - /// /// Initializes a new instance of the class. /// @@ -44,18 +34,18 @@ public class LocalSettingsTab /// The active color of the tab. /// The tab icon. Defaults to no icon. /// Should the tab button be created. Without it the tab needs to be opened manually. - public LocalSettingsTab(string tabName, Color tabColor, Color tabHoverColor, Color tabActiveColor, LoadableAsset? tabIcon = null, bool createButton = false) + public LocalSettingsTab(string tabName, Color? tabColor = null, Color? tabHoverColor = null, Color? tabActiveColor = null, LoadableAsset? tabIcon = null, bool createButton = true) { TabName = tabName; - TabColor = tabColor; - TabHoverColor = tabHoverColor; - TabActiveColor = tabActiveColor; + TabColor = tabColor ?? Color.white; + TabHoverColor = tabHoverColor ?? Palette.AcceptedGreen; + TabActiveColor = tabActiveColor ?? Palette.AcceptedGreen; TabIcon = tabIcon; ShouldCreateButton = createButton; Settings = new(); } - public GameObject? CreateTab(OptionsMenuBehaviour instance, ref int tabIdx, ref float offset) + internal GameObject? CreateTab(OptionsMenuBehaviour instance, ref int tabIdx, ref float offset) { var tab = Object.Instantiate(instance.transform.FindChild("GeneralTab").gameObject, instance.transform); tab.name = $"{TabName}Tab"; diff --git a/MiraAPI/LocalSettings/SettingTypes/LocalSettingBase.cs b/MiraAPI/LocalSettings/SettingTypes/LocalSettingBase.cs index 5634ecf..e93131a 100644 --- a/MiraAPI/LocalSettings/SettingTypes/LocalSettingBase.cs +++ b/MiraAPI/LocalSettings/SettingTypes/LocalSettingBase.cs @@ -15,14 +15,15 @@ public abstract class LocalSettingBase : ILocalSetting /// /// Initializes a new instance of the class. /// + /// The tab to create the setting in. /// The config entry. /// The name of the setting. /// The description of the setting. - protected LocalSettingBase(LocalSettingsTab tab, ConfigEntryBase configEntry, string name = null, string description = null) + protected LocalSettingBase(LocalSettingsTab tab, ConfigEntryBase configEntry, string? name = null, string? description = null) { ConfigEntry = configEntry; Name = name ?? ConfigEntry.Definition.Key; - Description = description; + Description = description ?? ConfigEntry.Description.Description; tab.Settings.Add(this); } @@ -30,12 +31,11 @@ public abstract class LocalSettingBase : ILocalSetting /// /// Creates an instance of the setting. /// - /// The transform parent of the setting. /// The created setting. public abstract GameObject? CreateOption(ToggleButtonBehaviour toggle, SlideBar slider, Transform parent, ref float offset, ref int order, bool last); /// - /// Gets the value of the config entry, casted to T. + /// Gets the value of the config entry, casted to the setting type. /// /// The value. public T GetValue() diff --git a/MiraAPI/LocalSettings/SettingTypes/LocalSetingToggle.cs b/MiraAPI/LocalSettings/SettingTypes/LocalSettingToggle.cs similarity index 83% rename from MiraAPI/LocalSettings/SettingTypes/LocalSetingToggle.cs rename to MiraAPI/LocalSettings/SettingTypes/LocalSettingToggle.cs index a2443dd..00babc1 100644 --- a/MiraAPI/LocalSettings/SettingTypes/LocalSetingToggle.cs +++ b/MiraAPI/LocalSettings/SettingTypes/LocalSettingToggle.cs @@ -6,7 +6,10 @@ using UnityEngine.Events; namespace MiraAPI.LocalSettings.SettingTypes; -public class LocalSetingToggle : LocalSettingBase +/// +/// Local setting class for toggles. +/// +public class LocalSettingToggle : LocalSettingBase { /// /// Gets the color of the toggle. @@ -23,40 +26,29 @@ public class LocalSetingToggle : LocalSettingBase /// public Color ToggleActiveColor { get; } - /// - public LocalSetingToggle(LocalSettingsTab tab, ConfigEntryBase configEntry, string name = null, string description = null) : this( - tab, - configEntry, - name, - description, - Color.white, - Palette.AcceptedGreen, - Palette.AcceptedGreen) - { - } - /// /// Initializes a new instance of the class. /// + /// /// Tab that the toggle belongs to. /// The binded config entry. /// The name of the setting. Defaults to ConfigEntry key. /// The desription of the setting. No description by default. /// The color of the toggle. /// The hover color of the toggle. /// The active color of the toggle. - public LocalSetingToggle( + public LocalSettingToggle( LocalSettingsTab tab, ConfigEntryBase configEntry, - string name, - string description, - Color toggleColor, - Color toggleHoverColor, - Color toggleActiveColor) + string? name = null, + string? description = null, + Color? toggleColor = null, + Color? toggleHoverColor = null, + Color? toggleActiveColor = null) : base(tab, configEntry, name, description) { - ToggleColor = toggleColor; - ToggleHoverColor = toggleHoverColor; - ToggleActiveColor = toggleActiveColor; + ToggleColor = toggleColor ?? Color.white; + ToggleHoverColor = toggleHoverColor ?? Palette.AcceptedGreen; + ToggleActiveColor = toggleActiveColor ?? Palette.AcceptedGreen; } public override GameObject? CreateOption(ToggleButtonBehaviour toggle, SlideBar slider, Transform parent, ref float offset, ref int order, bool last) diff --git a/MiraAPI/PluginLoading/MiraPluginManager.cs b/MiraAPI/PluginLoading/MiraPluginManager.cs index 759a0fd..aa6dfcd 100644 --- a/MiraAPI/PluginLoading/MiraPluginManager.cs +++ b/MiraAPI/PluginLoading/MiraPluginManager.cs @@ -4,6 +4,7 @@ using System.Collections.ObjectModel; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Reflection; +using BepInEx.Configuration; using BepInEx.Unity.IL2CPP; using MiraAPI.Colors; using MiraAPI.Events; @@ -12,6 +13,7 @@ using MiraAPI.GameOptions; using MiraAPI.GameOptions.Attributes; using MiraAPI.Hud; using MiraAPI.Keybinds; +using MiraAPI.LocalSettings.Attributes; using MiraAPI.Modifiers; using MiraAPI.Presets; using MiraAPI.Roles; @@ -109,6 +111,7 @@ public sealed class MiraPluginManager continue; } + RegisterLocalSettings(type); RegisterColorClasses(type); } @@ -317,4 +320,46 @@ public sealed class MiraPluginManager return false; } + + private static void RegisterLocalSettings(Type type) + { + try + { + if (!type.IsStatic()) + { + Logger.Error($"The type {type.Name} has to be static for local settings."); + return; + } + + foreach (var property in type.GetProperties()) + { + if (property.PropertyType.IsAssignableTo(typeof(ConfigEntryBase))) + { + continue; + } + + if (property.GetValue(null) is not ConfigEntryBase configEntry) + { + continue; + } + + var attribute = property.GetCustomAttribute(); + if (attribute == null) + { + continue; + } + + attribute.Create(configEntry); + } + + foreach (var field in type.GetFields().Where(f => f.FieldType.IsAssignableTo(typeof(ConfigEntryBase)) && f.GetCustomAttribute() != null)) + { + Logger.Error($"{field.Name} is a field, not a property. Use properties for local settings."); + } + } + catch (Exception e) + { + Logger.Error($"Failed to register local settings from {type.Name}: {e}"); + } + } }