From 49489ff0d1b1ea2b606ad02d4632f4ba22b6ef7c Mon Sep 17 00:00:00 2001 From: XtraCube <72575280+XtraCube@users.noreply.github.com> Date: Thu, 19 Sep 2024 06:40:51 -0400 Subject: [PATCH] improvements to custom role (added SetChance and SetCount, improved GetChance and GetCount) --- .../Patches/Options/RoleSettingMenuPatches.cs | 6 +-- .../Roles/RoleOptionsCollectionPatch.cs | 30 +++++++++--- MiraAPI/Roles/CustomRoleManager.cs | 11 +---- MiraAPI/Roles/ICustomRole.cs | 49 +++++++++++++++++-- MiraAPI/Utilities/Extensions.cs | 18 +++++-- 5 files changed, 89 insertions(+), 25 deletions(-) diff --git a/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs b/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs index 6168718..8d32c15 100644 --- a/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs +++ b/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs @@ -168,14 +168,12 @@ public static class RoleSettingMenuPatches { if (role.Configuration.MaxRoleCount != 0) { - role.ParentMod.PluginConfig.TryGetEntry(role.NumConfigDefinition, out var numEntry); - numEntry.Value = Mathf.Clamp(roleSetting.RoleMaxCount, 0, role.Configuration.MaxRoleCount); + role.SetCount(roleSetting.RoleMaxCount); } if (role.Configuration.CanModifyChance) { - role.ParentMod.PluginConfig.TryGetEntry(role.ChanceConfigDefinition, out var chanceEntry); - chanceEntry.Value = roleSetting.RoleChance; + role.SetChance(roleSetting.RoleChance); } } catch (Exception e) diff --git a/MiraAPI/Patches/Roles/RoleOptionsCollectionPatch.cs b/MiraAPI/Patches/Roles/RoleOptionsCollectionPatch.cs index bfa9e9f..e952fd4 100644 --- a/MiraAPI/Patches/Roles/RoleOptionsCollectionPatch.cs +++ b/MiraAPI/Patches/Roles/RoleOptionsCollectionPatch.cs @@ -1,18 +1,23 @@ using AmongUs.GameOptions; using HarmonyLib; using MiraAPI.Roles; +using Reactor.Utilities; namespace MiraAPI.Patches.Roles; +/// +/// Patches to return the correct role counts. +/// [HarmonyPatch(typeof(RoleOptionsCollectionV08))] public static class RoleOptionsCollectionPatch { /// /// Set the role chance for custom Launchpad roles based on config /// + /// Return false to skip original method, true to not. [HarmonyPrefix] [HarmonyPatch(nameof(RoleOptionsCollectionV08.GetChancePerGame))] - public static bool GetChancePrefix([HarmonyArgument(0)] RoleTypes roleType, ref int __result) + public static bool GetChancePrefix(RoleTypes roleType, ref int __result) { if (!CustomRoleManager.GetCustomRoleBehaviour(roleType, out var customRole) || customRole == null) { @@ -25,17 +30,24 @@ public static class RoleOptionsCollectionPatch return false; } - customRole.ParentMod.PluginConfig.TryGetEntry(customRole.ChanceConfigDefinition, out var entry); - __result = entry.Value; + var chance = customRole.GetChance(); + if (chance == null) + { + Logger.Error($"Chance is null, defaulting to zero."); + chance = 0; + } + + __result = chance.Value; return false; } /// /// Set the amount for custom Launchpad roles based on config /// + /// Return false to skip original method, true to not. [HarmonyPrefix] [HarmonyPatch(nameof(RoleOptionsCollectionV08.GetNumPerGame))] - public static bool GetNumPrefix([HarmonyArgument(0)] RoleTypes roleType, ref int __result) + public static bool GetNumPrefix(RoleTypes roleType, ref int __result) { if (!CustomRoleManager.GetCustomRoleBehaviour(roleType, out var customRole) || customRole == null) { @@ -48,8 +60,14 @@ public static class RoleOptionsCollectionPatch return false; } - customRole.ParentMod.PluginConfig.TryGetEntry(customRole.NumConfigDefinition, out var entry); - __result = entry.Value; + var count = customRole.GetCount(); + if (count == null) + { + Logger.Error($"Count is null, defaulting to zero."); + count = 0; + } + + __result = count.Value; return false; } } diff --git a/MiraAPI/Roles/CustomRoleManager.cs b/MiraAPI/Roles/CustomRoleManager.cs index 417f3a8..8d7f48a 100644 --- a/MiraAPI/Roles/CustomRoleManager.cs +++ b/MiraAPI/Roles/CustomRoleManager.cs @@ -228,15 +228,8 @@ public static class CustomRoleManager try { - customRole.ParentMod.PluginConfig.TryGetEntry( - customRole.NumConfigDefinition, - out var numEntry); - customRole.ParentMod.PluginConfig.TryGetEntry( - customRole.ChanceConfigDefinition, - out var chanceEntry); - - numEntry.Value = num; - chanceEntry.Value = chance; + customRole.SetCount(num); + customRole.SetChance(chance); } catch (Exception e) { diff --git a/MiraAPI/Roles/ICustomRole.cs b/MiraAPI/Roles/ICustomRole.cs index b7adf66..ba318b2 100644 --- a/MiraAPI/Roles/ICustomRole.cs +++ b/MiraAPI/Roles/ICustomRole.cs @@ -3,6 +3,7 @@ using MiraAPI.Modifiers; using MiraAPI.PluginLoading; using MiraAPI.Utilities; using System.Text; +using Reactor.Utilities; using UnityEngine; namespace MiraAPI.Roles; @@ -38,7 +39,7 @@ public interface ICustomRole ModdedRoleTeams Team { get; } /// - /// Configure advanced settings of the role. + /// Gets advanced settings of the role. /// CustomRoleConfiguration Configuration { get; } @@ -59,11 +60,16 @@ public interface ICustomRole internal ConfigDefinition ChanceConfigDefinition => new("Roles", $"Chance {GetType().FullName}"); /// - /// Get the role chance option. + /// Gets the role chance option. /// /// The role chance option. public int? GetChance() { + if (!Configuration.CanModifyChance) + { + return Configuration.DefaultChance; + } + if (ParentMod.PluginConfig.TryGetEntry(ChanceConfigDefinition, out ConfigEntry entry)) { return Mathf.Clamp(entry.Value, 0, 100); @@ -73,7 +79,7 @@ public interface ICustomRole } /// - /// Get the role count option. + /// Gets the role count option. /// /// The role count option. public int? GetCount() @@ -85,6 +91,43 @@ public interface ICustomRole return null; } + + /// + /// Sets the role chance option. + /// + /// The chance between 0 and 100. + public void SetChance(int chance) + { + if (!Configuration.CanModifyChance) + { + Logger.Error($"Cannot modify chance for role: {RoleName}"); + return; + } + + if (ParentMod.PluginConfig.TryGetEntry(ChanceConfigDefinition, out ConfigEntry entry)) + { + entry.Value = Mathf.Clamp(chance, 0, 100); + return; + } + + Logger.Error($"Error getting chance configuration for role: {RoleName}"); + } + + /// + /// Sets the role count option. + /// + /// The amount of this role between zero and its MaxRoleCount in the Configuration. + public void SetCount(int count) + { + if (ParentMod.PluginConfig.TryGetEntry(NumConfigDefinition, out ConfigEntry entry)) + { + entry.Value = Mathf.Clamp(count, 0, Configuration.MaxRoleCount); + return; + } + + Logger.Error($"Error getting count configuration for role: {RoleName}"); + } + /// /// This method runs on the HudManager.Update method ONLY when the LOCAL player has this role. /// diff --git a/MiraAPI/Utilities/Extensions.cs b/MiraAPI/Utilities/Extensions.cs index 38f2567..f7c667a 100644 --- a/MiraAPI/Utilities/Extensions.cs +++ b/MiraAPI/Utilities/Extensions.cs @@ -19,12 +19,24 @@ public static class Extensions { internal static NetData GetNetData(this ICustomRole role) { - role.ParentMod.PluginConfig.TryGetEntry(role.NumConfigDefinition, out var numEntry); - role.ParentMod.PluginConfig.TryGetEntry(role.ChanceConfigDefinition, out var chanceEntry); + var count = role.GetCount(); + var chance = role.GetChance(); + + if (count == null) + { + Logger.Error("Couldn't get role count for NetData, defaulting to zero."); + count = 0; + } + + if (chance == null) + { + Logger.Error("Couldn't get role chance for NetData, defaulting to zero."); + chance = 0; + } return new NetData( RoleId.Get(role.GetType()), - BitConverter.GetBytes(numEntry.Value).AddRangeToArray(BitConverter.GetBytes(chanceEntry.Value))); + BitConverter.GetBytes(count.Value).AddRangeToArray(BitConverter.GetBytes(chance.Value))); } ///