From dd6d57343e096045e8c99af4898aa41e142855ad Mon Sep 17 00:00:00 2001 From: ang-xd Date: Sat, 14 Sep 2024 15:47:02 -0400 Subject: [PATCH] Role singleton --- MiraAPI.Example/Roles/ChameleonRole.cs | 1 - MiraAPI/Roles/CustomRoleSingleton.cs | 19 ++++++++++++ MiraAPI/Roles/ICustomRole.cs | 41 ++++++++++++++++++++------ MiraAPI/Roles/RoleHintType.cs | 2 +- 4 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 MiraAPI/Roles/CustomRoleSingleton.cs diff --git a/MiraAPI.Example/Roles/ChameleonRole.cs b/MiraAPI.Example/Roles/ChameleonRole.cs index 13db85a..e3eb6c4 100644 --- a/MiraAPI.Example/Roles/ChameleonRole.cs +++ b/MiraAPI.Example/Roles/ChameleonRole.cs @@ -15,7 +15,6 @@ public class ChameloenRole : CrewmateRole, ICustomRole public CustomRoleConfiguration Configuration => new CustomRoleConfiguration(this) { - MaxRoleCount = 0, OptionsScreenshot = ExampleAssets.Banner, }; diff --git a/MiraAPI/Roles/CustomRoleSingleton.cs b/MiraAPI/Roles/CustomRoleSingleton.cs new file mode 100644 index 0000000..808202c --- /dev/null +++ b/MiraAPI/Roles/CustomRoleSingleton.cs @@ -0,0 +1,19 @@ +using System.Linq; + +namespace MiraAPI.Roles; + +/// +/// A utility class to get the instance of a custom role. +/// +/// The role you are trying to access. +public static class CustomRoleSingleton where T : ICustomRole +{ + private static ICustomRole? _instance; + + /// + /// Gets the instance of the role. + /// +#pragma warning disable CA1000 + public static ICustomRole Instance => _instance ??= CustomRoleManager.CustomRoles.Values.OfType().Single(); +#pragma warning restore CA1000 +} diff --git a/MiraAPI/Roles/ICustomRole.cs b/MiraAPI/Roles/ICustomRole.cs index 53b7798..b7adf66 100644 --- a/MiraAPI/Roles/ICustomRole.cs +++ b/MiraAPI/Roles/ICustomRole.cs @@ -37,17 +37,10 @@ public interface ICustomRole /// ModdedRoleTeams Team { get; } - CustomRoleConfiguration Configuration { get; } - - /// - /// Gets the BepInEx ConfigDefinition for the amount of players that can have this role. - /// - ConfigDefinition NumConfigDefinition => new("Roles", $"Num {GetType().FullName}"); - /// - /// Gets the BepInEx ConfigDefinition for the chance of this role being selected. + /// Configure advanced settings of the role. /// - ConfigDefinition ChanceConfigDefinition => new("Roles", $"Chance {GetType().FullName}"); + CustomRoleConfiguration Configuration { get; } /// /// Gets the parent mod of this role. @@ -62,6 +55,36 @@ void PlayerControlFixedUpdate(PlayerControl playerControl) { } + internal ConfigDefinition NumConfigDefinition => new("Roles", $"Num {GetType().FullName}"); + internal ConfigDefinition ChanceConfigDefinition => new("Roles", $"Chance {GetType().FullName}"); + + /// + /// Get the role chance option. + /// + /// The role chance option. + public int? GetChance() + { + if (ParentMod.PluginConfig.TryGetEntry(ChanceConfigDefinition, out ConfigEntry entry)) + { + return Mathf.Clamp(entry.Value, 0, 100); + } + + return null; + } + + /// + /// Get the role count option. + /// + /// The role count option. + public int? GetCount() + { + if (ParentMod.PluginConfig.TryGetEntry(NumConfigDefinition, out ConfigEntry entry)) + { + return Mathf.Clamp(entry.Value, 0, Configuration.MaxRoleCount); + } + + return null; + } /// /// This method runs on the HudManager.Update method ONLY when the LOCAL player has this role. /// diff --git a/MiraAPI/Roles/RoleHintType.cs b/MiraAPI/Roles/RoleHintType.cs index f3d5cc6..d00c5da 100644 --- a/MiraAPI/Roles/RoleHintType.cs +++ b/MiraAPI/Roles/RoleHintType.cs @@ -16,7 +16,7 @@ public enum RoleHintType TaskHint, /// - /// Use custom role tab. + /// Use Mira's custom role tab. /// RoleTab, }