Skip to content

Commit

Permalink
Role singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
ang-xd committed Sep 14, 2024
1 parent fd75c99 commit dd6d573
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 11 deletions.
1 change: 0 additions & 1 deletion MiraAPI.Example/Roles/ChameleonRole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class ChameloenRole : CrewmateRole, ICustomRole

public CustomRoleConfiguration Configuration => new CustomRoleConfiguration(this)
{
MaxRoleCount = 0,
OptionsScreenshot = ExampleAssets.Banner,
};

Expand Down
19 changes: 19 additions & 0 deletions MiraAPI/Roles/CustomRoleSingleton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Linq;

namespace MiraAPI.Roles;

/// <summary>
/// A utility class to get the instance of a custom role.
/// </summary>
/// <typeparam name="T">The role you are trying to access.</typeparam>
public static class CustomRoleSingleton<T> where T : ICustomRole
{
private static ICustomRole? _instance;

/// <summary>
/// Gets the instance of the role.
/// </summary>
#pragma warning disable CA1000
public static ICustomRole Instance => _instance ??= CustomRoleManager.CustomRoles.Values.OfType<T>().Single();
#pragma warning restore CA1000
}
41 changes: 32 additions & 9 deletions MiraAPI/Roles/ICustomRole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,10 @@ public interface ICustomRole
/// </summary>
ModdedRoleTeams Team { get; }

CustomRoleConfiguration Configuration { get; }

/// <summary>
/// Gets the BepInEx ConfigDefinition for the amount of players that can have this role.
/// </summary>
ConfigDefinition NumConfigDefinition => new("Roles", $"Num {GetType().FullName}");

/// <summary>
/// Gets the BepInEx ConfigDefinition for the chance of this role being selected.
/// Configure advanced settings of the role.
/// </summary>
ConfigDefinition ChanceConfigDefinition => new("Roles", $"Chance {GetType().FullName}");
CustomRoleConfiguration Configuration { get; }

/// <summary>
/// Gets the parent mod of this role.
Expand All @@ -62,6 +55,36 @@ void PlayerControlFixedUpdate(PlayerControl playerControl)
{
}

internal ConfigDefinition NumConfigDefinition => new("Roles", $"Num {GetType().FullName}");
internal ConfigDefinition ChanceConfigDefinition => new("Roles", $"Chance {GetType().FullName}");

/// <summary>
/// Get the role chance option.
/// </summary>
/// <returns>The role chance option.</returns>
public int? GetChance()
{
if (ParentMod.PluginConfig.TryGetEntry(ChanceConfigDefinition, out ConfigEntry<int> entry))
{
return Mathf.Clamp(entry.Value, 0, 100);
}

return null;
}

/// <summary>
/// Get the role count option.
/// </summary>
/// <returns>The role count option.</returns>
public int? GetCount()
{
if (ParentMod.PluginConfig.TryGetEntry(NumConfigDefinition, out ConfigEntry<int> entry))
{
return Mathf.Clamp(entry.Value, 0, Configuration.MaxRoleCount);
}

return null;
}
/// <summary>
/// This method runs on the HudManager.Update method ONLY when the LOCAL player has this role.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion MiraAPI/Roles/RoleHintType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public enum RoleHintType
TaskHint,

/// <summary>
/// Use custom role tab.
/// Use Mira's custom role tab.
/// </summary>
RoleTab,
}

0 comments on commit dd6d573

Please sign in to comment.