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