improvements to custom role (added SetChance and SetCount, improved GetChance and GetCount)

This commit is contained in:
XtraCube committed 2024-09-23 16:05:21 -04:00
1 parent aa2350bedf
commit 49489ff0d1
5 files changed
+89 -25

No files matched your search

@@ -168,14 +168,12 @@ public static class RoleSettingMenuPatches
{ {
if (role.Configuration.MaxRoleCount != 0) if (role.Configuration.MaxRoleCount != 0)
{ {
role.ParentMod.PluginConfig.TryGetEntry<int>(role.NumConfigDefinition, out var numEntry); role.SetCount(roleSetting.RoleMaxCount);
numEntry.Value = Mathf.Clamp(roleSetting.RoleMaxCount, 0, role.Configuration.MaxRoleCount);
} }
if (role.Configuration.CanModifyChance) if (role.Configuration.CanModifyChance)
{ {
role.ParentMod.PluginConfig.TryGetEntry<int>(role.ChanceConfigDefinition, out var chanceEntry); role.SetChance(roleSetting.RoleChance);
chanceEntry.Value = roleSetting.RoleChance;
} }
} }
catch (Exception e) catch (Exception e)
@@ -1,18 +1,23 @@
using AmongUs.GameOptions; using AmongUs.GameOptions;
using HarmonyLib; using HarmonyLib;
using MiraAPI.Roles; using MiraAPI.Roles;
using Reactor.Utilities;
namespace MiraAPI.Patches.Roles; namespace MiraAPI.Patches.Roles;
/// <summary>
/// Patches to return the correct role counts.
/// </summary>
[HarmonyPatch(typeof(RoleOptionsCollectionV08))] [HarmonyPatch(typeof(RoleOptionsCollectionV08))]
public static class RoleOptionsCollectionPatch public static class RoleOptionsCollectionPatch
{ {
/// <summary> /// <summary>
/// Set the role chance for custom Launchpad roles based on config /// Set the role chance for custom Launchpad roles based on config
/// </summary> /// </summary>
/// <returns>Return false to skip original method, true to not.</returns>
[HarmonyPrefix] [HarmonyPrefix]
[HarmonyPatch(nameof(RoleOptionsCollectionV08.GetChancePerGame))] [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) if (!CustomRoleManager.GetCustomRoleBehaviour(roleType, out var customRole) || customRole == null)
{ {
@@ -25,17 +30,24 @@ public static class RoleOptionsCollectionPatch
return false; return false;
} }
customRole.ParentMod.PluginConfig.TryGetEntry<int>(customRole.ChanceConfigDefinition, out var entry); var chance = customRole.GetChance();
__result = entry.Value; if (chance == null)
{
Logger<MiraApiPlugin>.Error($"Chance is null, defaulting to zero.");
chance = 0;
}
__result = chance.Value;
return false; return false;
} }
/// <summary> /// <summary>
/// Set the amount for custom Launchpad roles based on config /// Set the amount for custom Launchpad roles based on config
/// </summary> /// </summary>
/// <returns>Return false to skip original method, true to not.</returns>
[HarmonyPrefix] [HarmonyPrefix]
[HarmonyPatch(nameof(RoleOptionsCollectionV08.GetNumPerGame))] [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) if (!CustomRoleManager.GetCustomRoleBehaviour(roleType, out var customRole) || customRole == null)
{ {
@@ -48,8 +60,14 @@ public static class RoleOptionsCollectionPatch
return false; return false;
} }
customRole.ParentMod.PluginConfig.TryGetEntry<int>(customRole.NumConfigDefinition, out var entry); var count = customRole.GetCount();
__result = entry.Value; if (count == null)
{
Logger<MiraApiPlugin>.Error($"Count is null, defaulting to zero.");
count = 0;
}
__result = count.Value;
return false; return false;
} }
} }
+2 -9
View File
@@ -228,15 +228,8 @@ public static class CustomRoleManager
try try
{ {
customRole.ParentMod.PluginConfig.TryGetEntry<int>( customRole.SetCount(num);
customRole.NumConfigDefinition, customRole.SetChance(chance);
out var numEntry);
customRole.ParentMod.PluginConfig.TryGetEntry<int>(
customRole.ChanceConfigDefinition,
out var chanceEntry);
numEntry.Value = num;
chanceEntry.Value = chance;
} }
catch (Exception e) catch (Exception e)
{ {
+46 -3
View File
@@ -3,6 +3,7 @@ using MiraAPI.Modifiers;
using MiraAPI.PluginLoading; using MiraAPI.PluginLoading;
using MiraAPI.Utilities; using MiraAPI.Utilities;
using System.Text; using System.Text;
using Reactor.Utilities;
using UnityEngine; using UnityEngine;
namespace MiraAPI.Roles; namespace MiraAPI.Roles;
@@ -38,7 +39,7 @@ public interface ICustomRole
ModdedRoleTeams Team { get; } ModdedRoleTeams Team { get; }
/// <summary> /// <summary>
/// Configure advanced settings of the role. /// Gets advanced settings of the role.
/// </summary> /// </summary>
CustomRoleConfiguration Configuration { get; } CustomRoleConfiguration Configuration { get; }
@@ -59,11 +60,16 @@ public interface ICustomRole
internal ConfigDefinition ChanceConfigDefinition => new("Roles", $"Chance {GetType().FullName}"); internal ConfigDefinition ChanceConfigDefinition => new("Roles", $"Chance {GetType().FullName}");
/// <summary> /// <summary>
/// Get the role chance option. /// Gets the role chance option.
/// </summary> /// </summary>
/// <returns>The role chance option.</returns> /// <returns>The role chance option.</returns>
public int? GetChance() public int? GetChance()
{ {
if (!Configuration.CanModifyChance)
{
return Configuration.DefaultChance;
}
if (ParentMod.PluginConfig.TryGetEntry(ChanceConfigDefinition, out ConfigEntry<int> entry)) if (ParentMod.PluginConfig.TryGetEntry(ChanceConfigDefinition, out ConfigEntry<int> entry))
{ {
return Mathf.Clamp(entry.Value, 0, 100); return Mathf.Clamp(entry.Value, 0, 100);
@@ -73,7 +79,7 @@ public interface ICustomRole
} }
/// <summary> /// <summary>
/// Get the role count option. /// Gets the role count option.
/// </summary> /// </summary>
/// <returns>The role count option.</returns> /// <returns>The role count option.</returns>
public int? GetCount() public int? GetCount()
@@ -85,6 +91,43 @@ public interface ICustomRole
return null; return null;
} }
/// <summary>
/// Sets the role chance option.
/// </summary>
/// <param name="chance">The chance between 0 and 100.</param>
public void SetChance(int chance)
{
if (!Configuration.CanModifyChance)
{
Logger<MiraApiPlugin>.Error($"Cannot modify chance for role: {RoleName}");
return;
}
if (ParentMod.PluginConfig.TryGetEntry(ChanceConfigDefinition, out ConfigEntry<int> entry))
{
entry.Value = Mathf.Clamp(chance, 0, 100);
return;
}
Logger<MiraApiPlugin>.Error($"Error getting chance configuration for role: {RoleName}");
}
/// <summary>
/// Sets the role count option.
/// </summary>
/// <param name="count">The amount of this role between zero and its MaxRoleCount in the Configuration.</param>
public void SetCount(int count)
{
if (ParentMod.PluginConfig.TryGetEntry(NumConfigDefinition, out ConfigEntry<int> entry))
{
entry.Value = Mathf.Clamp(count, 0, Configuration.MaxRoleCount);
return;
}
Logger<MiraApiPlugin>.Error($"Error getting count configuration for role: {RoleName}");
}
/// <summary> /// <summary>
/// This method runs on the HudManager.Update method ONLY when the LOCAL player has this role. /// This method runs on the HudManager.Update method ONLY when the LOCAL player has this role.
/// </summary> /// </summary>
+15 -3
View File
@@ -19,12 +19,24 @@ public static class Extensions
{ {
internal static NetData GetNetData(this ICustomRole role) internal static NetData GetNetData(this ICustomRole role)
{ {
role.ParentMod.PluginConfig.TryGetEntry<int>(role.NumConfigDefinition, out var numEntry); var count = role.GetCount();
role.ParentMod.PluginConfig.TryGetEntry<int>(role.ChanceConfigDefinition, out var chanceEntry); var chance = role.GetChance();
if (count == null)
{
Logger<MiraApiPlugin>.Error("Couldn't get role count for NetData, defaulting to zero.");
count = 0;
}
if (chance == null)
{
Logger<MiraApiPlugin>.Error("Couldn't get role chance for NetData, defaulting to zero.");
chance = 0;
}
return new NetData( return new NetData(
RoleId.Get(role.GetType()), RoleId.Get(role.GetType()),
BitConverter.GetBytes(numEntry.Value).AddRangeToArray(BitConverter.GetBytes(chanceEntry.Value))); BitConverter.GetBytes(count.Value).AddRangeToArray(BitConverter.GetBytes(chance.Value)));
} }
/// <summary> /// <summary>