mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-10-19 15:51:48 +00:00
add roles to presets
This commit is contained in:
1 parent
b85358e304
commit
6f1da2de9f
4 files changed
+60
-9
No files matched your search
@@ -126,14 +126,8 @@ public abstract class ModdedOption<T> : IModdedOption
|
||||
Logger<MiraApiPlugin>.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;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
||||
@@ -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<ICustomRole>())
|
||||
{
|
||||
role.LoadFromPreset(PresetConfig);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<ICustomRole>())
|
||||
{
|
||||
role.SaveToPreset(presetConfig);
|
||||
}
|
||||
|
||||
presetConfig.Save();
|
||||
}
|
||||
|
||||
@@ -70,6 +76,11 @@ public static class PresetManager
|
||||
{
|
||||
option.Bind(presetConfig);
|
||||
}
|
||||
|
||||
foreach (var role in plugin.InternalRoles.Values.OfType<ICustomRole>())
|
||||
{
|
||||
role.BindConfig(presetConfig);
|
||||
}
|
||||
plugin.InternalPresets.Add(new OptionPreset(presetName, plugin, presetConfig));
|
||||
}
|
||||
plugin.Presets = [.. plugin.InternalPresets];
|
||||
|
||||
@@ -84,6 +84,45 @@ public interface ICustomRole : IOptionable
|
||||
internal ConfigDefinition NumConfigDefinition => new("Roles", $"Num {GetType().FullName}");
|
||||
internal ConfigDefinition ChanceConfigDefinition => new("Roles", $"Chance {GetType().FullName}");
|
||||
|
||||
/// <summary>
|
||||
/// Binds the configuration options for this role to the provided ConfigFile.
|
||||
/// </summary>
|
||||
/// <param name="config">The ConfigFile to bind the options to.</param>
|
||||
public virtual void BindConfig(ConfigFile config)
|
||||
{
|
||||
config.Bind(NumConfigDefinition, Configuration.DefaultRoleCount);
|
||||
config.Bind(ChanceConfigDefinition, Configuration.DefaultChance);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves the role's configuration to a preset ConfigFile.
|
||||
/// </summary>
|
||||
/// <param name="presetConfig">The ConfigFile to save the preset configuration to.</param>
|
||||
/// <param name="useDefault">Whether to use the default values for the configuration.</param>
|
||||
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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads the role's configuration from a preset ConfigFile.
|
||||
/// </summary>
|
||||
/// <param name="presetConfig">The ConfigFile containing the preset configuration.</param>
|
||||
public virtual void LoadFromPreset(ConfigFile presetConfig)
|
||||
{
|
||||
if (presetConfig.TryGetEntry(NumConfigDefinition, out ConfigEntry<int> numEntry))
|
||||
{
|
||||
SetCount(numEntry.Value);
|
||||
}
|
||||
|
||||
if (presetConfig.TryGetEntry(ChanceConfigDefinition, out ConfigEntry<int> chanceEntry))
|
||||
{
|
||||
SetChance(chanceEntry.Value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the role chance option.
|
||||
/// </summary>
|
||||
|
||||
Reference in new issue
Block a user