From 6f1da2de9f03cc3f3896d1c97b9693b6599f5dcf Mon Sep 17 00:00:00 2001 From: XtraCube <72575280+XtraCube@users.noreply.github.com> Date: Fri, 2 May 2025 19:25:03 -0400 Subject: [PATCH] add roles to presets --- .../GameOptions/OptionTypes/ModdedOption.cs | 10 +---- MiraAPI/Presets/OptionPreset.cs | 9 ++++- MiraAPI/Presets/PresetManager.cs | 11 ++++++ MiraAPI/Roles/ICustomRole.cs | 39 +++++++++++++++++++ 4 files changed, 60 insertions(+), 9 deletions(-) diff --git a/MiraAPI/GameOptions/OptionTypes/ModdedOption.cs b/MiraAPI/GameOptions/OptionTypes/ModdedOption.cs index cf5e900..94f6f3d 100644 --- a/MiraAPI/GameOptions/OptionTypes/ModdedOption.cs +++ b/MiraAPI/GameOptions/OptionTypes/ModdedOption.cs @@ -126,14 +126,8 @@ public abstract class ModdedOption : IModdedOption Logger.Error($"Attempted to save {Title} to preset, but ConfigDefinition is null."); return; } - var value = saveDefault ? DefaultValue : Value; - - if (!presetConfig.ContainsKey(ConfigDefinition)) - { - presetConfig.Bind(ConfigDefinition, DefaultValue); - } - - presetConfig[ConfigDefinition].BoxedValue = value; + Bind(presetConfig); + presetConfig[ConfigDefinition].BoxedValue = saveDefault ? DefaultValue : Value; } /// diff --git a/MiraAPI/Presets/OptionPreset.cs b/MiraAPI/Presets/OptionPreset.cs index 3a551a4..7e07df5 100644 --- a/MiraAPI/Presets/OptionPreset.cs +++ b/MiraAPI/Presets/OptionPreset.cs @@ -1,5 +1,7 @@ -using BepInEx.Configuration; +using System.Linq; +using BepInEx.Configuration; using MiraAPI.PluginLoading; +using MiraAPI.Roles; using UnityEngine; namespace MiraAPI.Presets; @@ -58,5 +60,10 @@ public class OptionPreset { option.LoadFromPreset(PresetConfig); } + + foreach (var role in Plugin.InternalRoles.Values.OfType()) + { + role.LoadFromPreset(PresetConfig); + } } } diff --git a/MiraAPI/Presets/PresetManager.cs b/MiraAPI/Presets/PresetManager.cs index 732b602..e6c6460 100644 --- a/MiraAPI/Presets/PresetManager.cs +++ b/MiraAPI/Presets/PresetManager.cs @@ -2,6 +2,7 @@ using System.Linq; using BepInEx.Configuration; using MiraAPI.PluginLoading; +using MiraAPI.Roles; using Reactor.Utilities; using UnityEngine; @@ -32,6 +33,11 @@ public static class PresetManager option.SaveToPreset(presetConfig, true); } + foreach (var role in plugin.InternalRoles.Values.OfType()) + { + role.SaveToPreset(presetConfig); + } + presetConfig.Save(); } @@ -70,6 +76,11 @@ public static class PresetManager { option.Bind(presetConfig); } + + foreach (var role in plugin.InternalRoles.Values.OfType()) + { + role.BindConfig(presetConfig); + } plugin.InternalPresets.Add(new OptionPreset(presetName, plugin, presetConfig)); } plugin.Presets = [.. plugin.InternalPresets]; diff --git a/MiraAPI/Roles/ICustomRole.cs b/MiraAPI/Roles/ICustomRole.cs index abc0656..8f69e3d 100644 --- a/MiraAPI/Roles/ICustomRole.cs +++ b/MiraAPI/Roles/ICustomRole.cs @@ -84,6 +84,45 @@ public interface ICustomRole : IOptionable internal ConfigDefinition NumConfigDefinition => new("Roles", $"Num {GetType().FullName}"); internal ConfigDefinition ChanceConfigDefinition => new("Roles", $"Chance {GetType().FullName}"); + /// + /// Binds the configuration options for this role to the provided ConfigFile. + /// + /// The ConfigFile to bind the options to. + public virtual void BindConfig(ConfigFile config) + { + config.Bind(NumConfigDefinition, Configuration.DefaultRoleCount); + config.Bind(ChanceConfigDefinition, Configuration.DefaultChance); + } + + /// + /// Saves the role's configuration to a preset ConfigFile. + /// + /// The ConfigFile to save the preset configuration to. + /// Whether to use the default values for the configuration. + public virtual void SaveToPreset(ConfigFile presetConfig, bool useDefault=false) + { + BindConfig(presetConfig); + presetConfig[NumConfigDefinition].BoxedValue = useDefault ? Configuration.DefaultRoleCount : GetCount(); + presetConfig[ChanceConfigDefinition].BoxedValue = useDefault ? Configuration.DefaultChance : GetChance(); + } + + /// + /// Loads the role's configuration from a preset ConfigFile. + /// + /// The ConfigFile containing the preset configuration. + public virtual void LoadFromPreset(ConfigFile presetConfig) + { + if (presetConfig.TryGetEntry(NumConfigDefinition, out ConfigEntry numEntry)) + { + SetCount(numEntry.Value); + } + + if (presetConfig.TryGetEntry(ChanceConfigDefinition, out ConfigEntry chanceEntry)) + { + SetChance(chanceEntry.Value); + } + } + /// /// Gets the role chance option. ///