Skip to content

Commit

Permalink
improvements to custom role (added SetChance and SetCount, improved G…
Browse files Browse the repository at this point in the history
…etChance and GetCount)
  • Loading branch information
XtraCube committed Sep 19, 2024
1 parent aa53af1 commit 9f99f6a
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 24 deletions.
6 changes: 2 additions & 4 deletions MiraAPI/Patches/Options/RoleSettingMenuPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,12 @@ private static void ValueChanged(OptionBehaviour obj)
{
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)
Expand Down
30 changes: 24 additions & 6 deletions MiraAPI/Patches/Roles/RoleOptionsCollectionPatch.cs
Original file line number Diff line number Diff line change
@@ -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)
{
Expand All @@ -25,17 +30,24 @@ public static bool GetChancePrefix([HarmonyArgument(0)] RoleTypes roleType, ref
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)
{
Expand All @@ -48,8 +60,14 @@ public static bool GetNumPrefix([HarmonyArgument(0)] RoleTypes roleType, ref int
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;
}
}
11 changes: 2 additions & 9 deletions MiraAPI/Roles/CustomRoleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,8 @@ internal static void HandleSyncRoleOptions(NetData[] data)

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)
{
Expand Down
47 changes: 45 additions & 2 deletions MiraAPI/Roles/ICustomRole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using MiraAPI.Modifiers;
using MiraAPI.PluginLoading;
using MiraAPI.Utilities;
using Reactor.Utilities;
using UnityEngine;

namespace MiraAPI.Roles;
Expand Down Expand Up @@ -59,11 +60,16 @@ void PlayerControlFixedUpdate(PlayerControl playerControl)
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);
Expand All @@ -73,7 +79,7 @@ void PlayerControlFixedUpdate(PlayerControl playerControl)
}

/// <summary>
/// Get the role count option.
/// Gets the role count option.
/// </summary>
/// <returns>The role count option.</returns>
public int? GetCount()
Expand All @@ -85,6 +91,43 @@ void PlayerControlFixedUpdate(PlayerControl playerControl)

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>
Expand Down
18 changes: 15 additions & 3 deletions MiraAPI/Utilities/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 9f99f6a

Please sign in to comment.