mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-10-20 01:46:15 +00:00
change IModdedOptionGroup to AbstractOptionGroup, update group singleton class, save options to config
This commit is contained in:
1 parent
dcf6221671
commit
a1219f0656
12 files changed
+64
-48
No files matched your search
@@ -4,10 +4,10 @@ using UnityEngine;
|
|||||||
|
|
||||||
namespace MiraAPI.Example.Options;
|
namespace MiraAPI.Example.Options;
|
||||||
|
|
||||||
public class ExampleOptions : IModdedOptionGroup
|
public class ExampleOptions : AbstractOptionGroup
|
||||||
{
|
{
|
||||||
public string GroupName => "General Group";
|
public override string GroupName => "General Group";
|
||||||
public Color GroupColor => Color.red;
|
public override Color GroupColor => Color.red;
|
||||||
|
|
||||||
[ModdedToggleOption("Use Thing")]
|
[ModdedToggleOption("Use Thing")]
|
||||||
public bool UseThing { get; set; } = false;
|
public bool UseThing { get; set; } = false;
|
||||||
|
|||||||
@@ -3,9 +3,9 @@ using MiraAPI.GameOptions.OptionTypes;
|
|||||||
|
|
||||||
namespace MiraAPI.Example.Options;
|
namespace MiraAPI.Example.Options;
|
||||||
|
|
||||||
public class ExampleOptions2 : IModdedOptionGroup
|
public class ExampleOptions2 : AbstractOptionGroup
|
||||||
{
|
{
|
||||||
public string GroupName => "Example 2";
|
public override string GroupName => "Example 2";
|
||||||
|
|
||||||
public ModdedToggleOption UseThing { get; } = new("Use Thingy 5", false);
|
public ModdedToggleOption UseThing { get; } = new("Use Thingy 5", false);
|
||||||
|
|
||||||
|
|||||||
@@ -5,11 +5,11 @@ using MiraAPI.GameOptions.Attributes;
|
|||||||
|
|
||||||
namespace MiraAPI.Example.Options.Roles;
|
namespace MiraAPI.Example.Options.Roles;
|
||||||
|
|
||||||
public class CustomRole2Settings : IModdedOptionGroup
|
public class CustomRole2Settings : AbstractOptionGroup
|
||||||
{
|
{
|
||||||
public string GroupName => "Custom Role";
|
public override string GroupName => "Custom Role";
|
||||||
|
|
||||||
public Type AdvancedRole => typeof(CustomRole2);
|
public override Type AdvancedRole => typeof(CustomRole2);
|
||||||
|
|
||||||
[ModdedToggleOption("Teleportation ability")] public bool Teleport { get; set; } = true;
|
[ModdedToggleOption("Teleportation ability")] public bool Teleport { get; set; } = true;
|
||||||
|
|
||||||
|
|||||||
@@ -5,11 +5,11 @@ using MiraAPI.GameOptions.Attributes;
|
|||||||
|
|
||||||
namespace MiraAPI.Example.Options.Roles;
|
namespace MiraAPI.Example.Options.Roles;
|
||||||
|
|
||||||
public class CustomRoleSettings : IModdedOptionGroup
|
public class CustomRoleSettings : AbstractOptionGroup
|
||||||
{
|
{
|
||||||
public string GroupName => "Custom Role";
|
public override string GroupName => "Custom Role";
|
||||||
|
|
||||||
public Type AdvancedRole => typeof(CustomRole);
|
public override Type AdvancedRole => typeof(CustomRole);
|
||||||
|
|
||||||
[ModdedNumberOption("Testing level", min: 3, max: 9)] public float TestingLevel { get; set; } = 4;
|
[ModdedNumberOption("Testing level", min: 3, max: 9)] public float TestingLevel { get; set; } = 4;
|
||||||
|
|
||||||
|
|||||||
+8
-5
@@ -1,27 +1,30 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace MiraAPI.GameOptions;
|
namespace MiraAPI.GameOptions;
|
||||||
|
|
||||||
public interface IModdedOptionGroup
|
public abstract class AbstractOptionGroup
|
||||||
{
|
{
|
||||||
|
public List<IModdedOption> Options { get; } = [];
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The name of the group. Visible in options menu.
|
/// The name of the group. Visible in options menu.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string GroupName { get; }
|
public abstract string GroupName { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A function that returns whether the group should be visible or not.
|
/// A function that returns whether the group should be visible or not.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Func<bool> GroupVisible => () => true;
|
public virtual Func<bool> GroupVisible => () => true;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The group color. This is used to color the group in the options menu.
|
/// The group color. This is used to color the group in the options menu.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Color GroupColor => Color.clear;
|
public virtual Color GroupColor => Color.clear;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The role the group is associated with. This is used for the advanced role options menu.
|
/// The role the group is associated with. This is used for the advanced role options menu.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Type AdvancedRole => null;
|
public virtual Type AdvancedRole => null;
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
using MiraAPI.PluginLoading;
|
using MiraAPI.PluginLoading;
|
||||||
using System;
|
using System;
|
||||||
|
using BepInEx.Configuration;
|
||||||
using MiraAPI.Networking;
|
using MiraAPI.Networking;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
@@ -9,12 +10,13 @@ public interface IModdedOption
|
|||||||
{
|
{
|
||||||
public uint Id { get; }
|
public uint Id { get; }
|
||||||
public BaseGameSetting Data { get; }
|
public BaseGameSetting Data { get; }
|
||||||
public IMiraPlugin ParentMod { get; set; }
|
public IMiraPlugin ParentMod { get; init; }
|
||||||
public Type AdvancedRole { get; set; }
|
public Type AdvancedRole { get; set; }
|
||||||
public OptionBehaviour OptionBehaviour { get; protected set; }
|
public OptionBehaviour OptionBehaviour { get; protected set; }
|
||||||
public string Title { get; }
|
public string Title { get; }
|
||||||
public StringNames StringName { get; }
|
public StringNames StringName { get; }
|
||||||
public Func<bool> Visible { get; set; }
|
public Func<bool> Visible { get; set; }
|
||||||
|
public ConfigDefinition ConfigDefinition { get; }
|
||||||
public void ValueChanged(OptionBehaviour optionBehaviour);
|
public void ValueChanged(OptionBehaviour optionBehaviour);
|
||||||
public OptionBehaviour CreateOption(ToggleOption toggleOpt, NumberOption numberOpt, StringOption stringOpt, Transform container);
|
public OptionBehaviour CreateOption(ToggleOption toggleOpt, NumberOption numberOpt, StringOption stringOpt, Transform container);
|
||||||
public NetData GetNetData();
|
public NetData GetNetData();
|
||||||
|
|||||||
@@ -14,20 +14,17 @@ namespace MiraAPI.GameOptions;
|
|||||||
public class ModdedOptionsManager
|
public class ModdedOptionsManager
|
||||||
{
|
{
|
||||||
private static readonly Dictionary<PropertyInfo, ModdedOptionAttribute> OptionAttributes = new();
|
private static readonly Dictionary<PropertyInfo, ModdedOptionAttribute> OptionAttributes = new();
|
||||||
private static readonly Dictionary<Type, IModdedOptionGroup> TypeToGroup = new();
|
private static readonly Dictionary<Type, AbstractOptionGroup> TypeToGroup = new();
|
||||||
|
|
||||||
public static readonly Dictionary<uint, IModdedOption> ModdedOptions = new();
|
public static readonly Dictionary<uint, IModdedOption> ModdedOptions = new();
|
||||||
public static readonly Dictionary<IModdedOptionGroup, List<IModdedOption>> Groups = new();
|
public static readonly List<AbstractOptionGroup> Groups = [];
|
||||||
|
|
||||||
|
|
||||||
public static uint NextId => _nextId++;
|
public static uint NextId => _nextId++;
|
||||||
private static uint _nextId = 1;
|
private static uint _nextId = 1;
|
||||||
|
|
||||||
internal static bool RegisterGroup(Type type, MiraPluginInfo pluginInfo)
|
internal static bool RegisterGroup(Type type, MiraPluginInfo pluginInfo)
|
||||||
{
|
{
|
||||||
var group = (IModdedOptionGroup)Activator.CreateInstance(type);
|
if (Activator.CreateInstance(type) is not AbstractOptionGroup group)
|
||||||
|
|
||||||
if (group == null)
|
|
||||||
{
|
{
|
||||||
Logger<MiraApiPlugin>.Error($"Failed to create group from {type.Name}");
|
Logger<MiraApiPlugin>.Error($"Failed to create group from {type.Name}");
|
||||||
return false;
|
return false;
|
||||||
@@ -39,7 +36,7 @@ public class ModdedOptionsManager
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Groups.Add(group, []);
|
Groups.Add(group);
|
||||||
TypeToGroup.Add(type, group);
|
TypeToGroup.Add(type, group);
|
||||||
pluginInfo.OptionGroups.Add(group);
|
pluginInfo.OptionGroups.Add(group);
|
||||||
|
|
||||||
@@ -53,10 +50,8 @@ public class ModdedOptionsManager
|
|||||||
Logger<MiraApiPlugin>.Error($"Failed to get group for {type.Name}");
|
Logger<MiraApiPlugin>.Error($"Failed to get group for {type.Name}");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var option = (IModdedOption) property.GetValue(group);
|
if (property.GetValue(group) is not IModdedOption option)
|
||||||
|
|
||||||
if (option == null)
|
|
||||||
{
|
{
|
||||||
Logger<MiraApiPlugin>.Error($"Failed to get option for {property.Name}");
|
Logger<MiraApiPlugin>.Error($"Failed to get option for {property.Name}");
|
||||||
return;
|
return;
|
||||||
@@ -101,14 +96,15 @@ public class ModdedOptionsManager
|
|||||||
RegisterOption(option, group, pluginInfo);
|
RegisterOption(option, group, pluginInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void RegisterOption(IModdedOption option, IModdedOptionGroup group, MiraPluginInfo pluginInfo)
|
private static void RegisterOption(IModdedOption option, AbstractOptionGroup group, MiraPluginInfo pluginInfo)
|
||||||
{
|
{
|
||||||
option.ParentMod = pluginInfo.MiraPlugin;
|
option.ParentMod = pluginInfo.MiraPlugin;
|
||||||
option.AdvancedRole = group.AdvancedRole;
|
option.AdvancedRole = group.AdvancedRole;
|
||||||
|
|
||||||
pluginInfo.Options.Add(option);
|
pluginInfo.Options.Add(option);
|
||||||
|
|
||||||
ModdedOptions.Add(option.Id, option);
|
ModdedOptions.Add(option.Id, option);
|
||||||
Groups[group].Add(option);
|
group.Options.Add(option);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static void SyncAllOptions(int targetId=-1)
|
internal static void SyncAllOptions(int targetId=-1)
|
||||||
|
|||||||
+2
-2
@@ -3,13 +3,13 @@ using System.Linq;
|
|||||||
|
|
||||||
namespace MiraAPI.GameOptions;
|
namespace MiraAPI.GameOptions;
|
||||||
|
|
||||||
public class ModdedGroupSingleton<T> where T : IModdedOptionGroup
|
public class OptionGroupSingleton<T> where T : AbstractOptionGroup
|
||||||
{
|
{
|
||||||
private static T _instance;
|
private static T _instance;
|
||||||
|
|
||||||
public static T Instance
|
public static T Instance
|
||||||
{
|
{
|
||||||
get => _instance ??= ModdedOptionsManager.Groups.Keys.OfType<T>().Single();
|
get => _instance ??= ModdedOptionsManager.Groups.OfType<T>().Single();
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
if (_instance != null)
|
if (_instance != null)
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
using MiraAPI.Roles;
|
using MiraAPI.Roles;
|
||||||
using Reactor.Localization.Utilities;
|
using Reactor.Localization.Utilities;
|
||||||
using System;
|
using System;
|
||||||
|
using BepInEx.Configuration;
|
||||||
using MiraAPI.Networking;
|
using MiraAPI.Networking;
|
||||||
using Reactor.Networking.Rpc;
|
using Reactor.Networking.Rpc;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
@@ -10,9 +11,20 @@ namespace MiraAPI.GameOptions.OptionTypes;
|
|||||||
|
|
||||||
public abstract class ModdedOption<T> : IModdedOption
|
public abstract class ModdedOption<T> : IModdedOption
|
||||||
{
|
{
|
||||||
|
private readonly IMiraPlugin _parentMod;
|
||||||
|
|
||||||
public uint Id { get; }
|
public uint Id { get; }
|
||||||
public BaseGameSetting Data { get; protected init; }
|
public BaseGameSetting Data { get; protected init; }
|
||||||
public IMiraPlugin ParentMod { get; set; } = null;
|
public IMiraPlugin ParentMod
|
||||||
|
{
|
||||||
|
get => _parentMod;
|
||||||
|
init
|
||||||
|
{
|
||||||
|
_parentMod = value;
|
||||||
|
var entry = _parentMod.GetConfigFile().Bind(ConfigDefinition, DefaultValue);
|
||||||
|
Value = entry.Value;
|
||||||
|
}
|
||||||
|
}
|
||||||
public T Value { get; protected set; }
|
public T Value { get; protected set; }
|
||||||
public T DefaultValue { get; init; }
|
public T DefaultValue { get; init; }
|
||||||
public Action<T> ChangedEvent { get; set; }
|
public Action<T> ChangedEvent { get; set; }
|
||||||
@@ -21,9 +33,11 @@ public abstract class ModdedOption<T> : IModdedOption
|
|||||||
public Func<bool> Visible { get; set; }
|
public Func<bool> Visible { get; set; }
|
||||||
public Type AdvancedRole { get; set; }
|
public Type AdvancedRole { get; set; }
|
||||||
public OptionBehaviour OptionBehaviour { get; set; }
|
public OptionBehaviour OptionBehaviour { get; set; }
|
||||||
|
public ConfigDefinition ConfigDefinition => new ("Options", Title);
|
||||||
|
|
||||||
protected ModdedOption(string title, T defaultValue, Type roleType) : this()
|
protected ModdedOption(string title, T defaultValue, Type roleType)
|
||||||
{
|
{
|
||||||
|
Id = ModdedOptionsManager.NextId;
|
||||||
Title = title;
|
Title = title;
|
||||||
DefaultValue = defaultValue;
|
DefaultValue = defaultValue;
|
||||||
Value = defaultValue;
|
Value = defaultValue;
|
||||||
@@ -36,11 +50,6 @@ public abstract class ModdedOption<T> : IModdedOption
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private ModdedOption()
|
|
||||||
{
|
|
||||||
Id = ModdedOptionsManager.NextId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ValueChanged(OptionBehaviour optionBehaviour)
|
public void ValueChanged(OptionBehaviour optionBehaviour)
|
||||||
{
|
{
|
||||||
SetValue(GetValueFromOptionBehaviour(optionBehaviour));
|
SetValue(GetValueFromOptionBehaviour(optionBehaviour));
|
||||||
@@ -59,7 +68,13 @@ public abstract class ModdedOption<T> : IModdedOption
|
|||||||
{
|
{
|
||||||
Rpc<SyncOptionsRpc>.Instance.Send(PlayerControl.LocalPlayer, [GetNetData()], true);
|
Rpc<SyncOptionsRpc>.Instance.Send(PlayerControl.LocalPlayer, [GetNetData()], true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ParentMod is not null &&
|
||||||
|
ParentMod.GetConfigFile().TryGetEntry<T>(ConfigDefinition, out var entry))
|
||||||
|
{
|
||||||
|
entry.Value = Value;
|
||||||
|
}
|
||||||
|
|
||||||
OnValueChanged(newValue);
|
OnValueChanged(newValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,9 +28,9 @@ public static class GameOptionsMenuPatch
|
|||||||
|
|
||||||
var filteredGroups = GameSettingMenuPatches.SelectedMod.OptionGroups.Where(x => x.GroupVisible.Invoke() && x.AdvancedRole is null);
|
var filteredGroups = GameSettingMenuPatches.SelectedMod.OptionGroups.Where(x => x.GroupVisible.Invoke() && x.AdvancedRole is null);
|
||||||
|
|
||||||
foreach (IModdedOptionGroup group in filteredGroups)
|
foreach (AbstractOptionGroup group in filteredGroups)
|
||||||
{
|
{
|
||||||
var filteredOpts = ModdedOptionsManager.Groups[group].Where(x => x.Visible.Invoke()).ToList();
|
var filteredOpts = group.Options.Where(x => x.Visible.Invoke()).ToList();
|
||||||
if (filteredOpts.Count == 0)
|
if (filteredOpts.Count == 0)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
@@ -8,12 +8,12 @@ namespace MiraAPI.PluginLoading;
|
|||||||
|
|
||||||
public class MiraPluginInfo(string id, IMiraPlugin miraPlugin, PluginInfo info)
|
public class MiraPluginInfo(string id, IMiraPlugin miraPlugin, PluginInfo info)
|
||||||
{
|
{
|
||||||
public string PluginId { get; set; } = id;
|
public string PluginId { get; } = id;
|
||||||
public IMiraPlugin MiraPlugin { get; set; } = miraPlugin;
|
public IMiraPlugin MiraPlugin { get; } = miraPlugin;
|
||||||
public PluginInfo PluginInfo { get; set; } = info;
|
public PluginInfo PluginInfo { get; } = info;
|
||||||
public ConfigFile PluginConfig { get; set; } = miraPlugin.GetConfigFile();
|
public ConfigFile PluginConfig { get; } = miraPlugin.GetConfigFile();
|
||||||
|
|
||||||
public readonly List<IModdedOptionGroup> OptionGroups = [];
|
public readonly List<AbstractOptionGroup> OptionGroups = [];
|
||||||
public readonly List<IModdedOption> Options = [];
|
public readonly List<IModdedOption> Options = [];
|
||||||
|
|
||||||
public readonly Dictionary<ushort, RoleBehaviour> CustomRoles = [];
|
public readonly Dictionary<ushort, RoleBehaviour> CustomRoles = [];
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ internal class MiraPluginManager
|
|||||||
|
|
||||||
private static void RegisterAllOptions(Assembly assembly, MiraPluginInfo pluginInfo)
|
private static void RegisterAllOptions(Assembly assembly, MiraPluginInfo pluginInfo)
|
||||||
{
|
{
|
||||||
var filteredTypes = assembly.GetTypes().Where(type => type.IsAssignableTo(typeof(IModdedOptionGroup)));
|
var filteredTypes = assembly.GetTypes().Where(type => type.IsAssignableTo(typeof(AbstractOptionGroup)));
|
||||||
|
|
||||||
foreach (var type in filteredTypes)
|
foreach (var type in filteredTypes)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in new issue
Block a user