From 612597566b773676500d8acbbd09c9bb52eae2cd Mon Sep 17 00:00:00 2001 From: David Higueros <111460331+TheDavSmasher@users.noreply.github.com> Date: Fri, 19 Jun 2026 21:35:03 -0600 Subject: [PATCH] Created abstract class for Option list --- .../OptionTypes/ModdedOptionList.cs | 120 ++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 MiraAPI/GameOptions/OptionTypes/ModdedOptionList.cs diff --git a/MiraAPI/GameOptions/OptionTypes/ModdedOptionList.cs b/MiraAPI/GameOptions/OptionTypes/ModdedOptionList.cs new file mode 100644 index 0000000..be14e14 --- /dev/null +++ b/MiraAPI/GameOptions/OptionTypes/ModdedOptionList.cs @@ -0,0 +1,120 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using BepInEx.Configuration; +using MiraAPI.Networking; +using MiraAPI.PluginLoading; +using Reactor.Localization.Utilities; +using Reactor.Networking.Rpc; +using Reactor.Utilities; +using UnityEngine; + +namespace MiraAPI.GameOptions.OptionTypes; + +/// +/// Represents a modded option list. +/// +public abstract class ModdedOptionList : IModdedOptionList +{ + protected IMiraPlugin? _parentMod; + + /// + public int Count { get; } + + /// + public IReadOnlyList Ids { get; } + + /// + public string Title { get; set; } + + /// + public bool ZeroIndexTitle { get; set; } + + /// + public IReadOnlyList StringNames { get; } + + /// + public IReadOnlyList Data { get; protected set; } = []; + + /// + public IMiraPlugin? ParentMod + { + get => _parentMod; + set + { + if (_parentMod != null || value == null) return; + _parentMod = value; + + OnParentModChange(); + } + } + + protected abstract void OnParentModChange(); + + /// + public Func Visible { get; set; } + + /// + public bool IncludeInPreset { get; set; } + + protected readonly OptionBehaviour?[] _optionBehaviours; + + /// + public IReadOnlyList OptionBehaviours => _optionBehaviours; + + /// + public ConfigDefinition?[] ConfigDefinitions { get; set; } + + /// + /// Initializes a new instance of the class. + /// + /// The options' title. + /// The option list's length. + /// Whether to include the options in the preset. + /// Whether the first option's title index is 0, else 1. + protected ModdedOptionList(string title, int count, bool includeInPreset = true, bool zeroIndexTitle = false) + { + Count = count; + ZeroIndexTitle = zeroIndexTitle; + + var range = Enumerable.Range(0, Count); + var titleOffset = ZeroIndexTitle ? 0 : 1; + + Ids = range.Select(_ => ModdedOptionsManager.NextId).ToList(); + Title = title; + StringNames = range.Select(i => CustomStringName.CreateAndRegister($"{Title}{i + titleOffset}")).ToList(); + Visible = _ => true; + IncludeInPreset = includeInPreset; + + _optionBehaviours = range.Select(_ => null).ToArray(); + ConfigDefinitions = range.Select(_ => null).ToArray(); + } + + /// + public abstract void SaveToPreset(ConfigFile presetConfig, bool saveDefault = false); + + /// + public abstract void Bind(ConfigFile config); + + /// + public abstract void LoadFromPreset(ConfigFile presetConfig); + + /// + public abstract float GetFloatData(int idx); + + /// + public abstract NetData GetNetData(int idx); + + /// + public abstract void HandleNetData(int idx, byte[] data); + + /// + public abstract OptionBehaviour CreateOption( + int idx, + ToggleOption toggleOpt, + NumberOption numberOpt, + StringOption stringOpt, + PlayerOption playerOpt, + Transform container); +}