further modifier improvements (massive changes)

This commit is contained in:
XtraCube committed 2025-02-25 17:22:00 -05:00
1 parent 4a8fb38380
commit caa27642db
14 files changed
+276 -139

No files matched your search

+6
View File
@@ -15,6 +15,12 @@ public class TestButton : CustomActionButton
protected override void OnClick() protected override void OnClick()
{ {
if (PlayerControl.LocalPlayer.HasModifier<ModifierParams>())
{
PlayerControl.LocalPlayer.RpcRemoveModifier<ModifierParams>();
return;
}
var randomPlayer = PlayerControl.AllPlayerControls.ToArray().Random(); var randomPlayer = PlayerControl.AllPlayerControls.ToArray().Random();
PlayerControl.LocalPlayer.RpcAddModifier<ModifierParams>("test", 1, randomPlayer); PlayerControl.LocalPlayer.RpcAddModifier<ModifierParams>("test", 1, randomPlayer);
} }
@@ -1,4 +1,4 @@
namespace MiraAPI.Events.Vanilla; namespace MiraAPI.Events.Vanilla.Usables;
/// <summary> /// <summary>
/// Event for <see cref="PlayerControl"/> using a <see cref="IUsable"/> from Vanilla Among Us. Will always be ran locally. /// Event for <see cref="PlayerControl"/> using a <see cref="IUsable"/> from Vanilla Among Us. Will always be ran locally.
+7 -6
View File
@@ -43,13 +43,14 @@ public abstract class BaseModifier
public bool Initialized { get; internal set; } public bool Initialized { get; internal set; }
/// <summary> /// <summary>
/// Get the ID of the modifier. /// Gets the unique ID of the modifier.
/// </summary> /// </summary>
/// <returns>The ID of the modifier.</returns> public uint UniqueId { get; internal set; }
public uint GetId()
{ /// <summary>
return ModifierManager.GetModifierId(GetType()) ?? throw new InvalidOperationException("Modifier is not registered."); /// Gets the type ID of the modifier.
} /// </summary>
public uint TypeId => ModifierManager.GetModifierTypeId(GetType()) ?? throw new InvalidOperationException("Modifier is not registered.");
/// <summary> /// <summary>
/// Gets the HUD information for this modifier. Defaults to the modifier name. Does nothing if <see cref="HideOnUi"/> is true. /// Gets the HUD information for this modifier. Defaults to the modifier name. Does nothing if <see cref="HideOnUi"/> is true.
+106 -49
View File
@@ -37,6 +37,14 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr)
private readonly List<BaseModifier> _toAdd = []; private readonly List<BaseModifier> _toAdd = [];
private uint _nextUniqueId;
private uint GetNextUniqueId()
{
_nextUniqueId++;
return _nextUniqueId;
}
internal void ClearModifiers() internal void ClearModifiers()
{ {
_toRemove.AddRange(Modifiers); _toRemove.AddRange(Modifiers);
@@ -104,14 +112,12 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr)
return; return;
} }
var filteredModifiers = Modifiers.Where(mod => !mod.HideOnUi); var filteredModifiers = Modifiers.Where(mod => !mod.HideOnUi).ToArray();
var baseModifiers = filteredModifiers as BaseModifier[] ?? filteredModifiers.ToArray(); if (filteredModifiers.Length != 0 && !MeetingHud.Instance)
if (baseModifiers.Length != 0 && !MeetingHud.Instance)
{ {
var stringBuild = new StringBuilder(); var stringBuild = new StringBuilder();
foreach (var mod in baseModifiers) foreach (var mod in filteredModifiers)
{ {
stringBuild.Append(CultureInfo.InvariantCulture, $"\n{mod.GetHudString()}"); stringBuild.Append(CultureInfo.InvariantCulture, $"\n{mod.GetHudString()}");
} }
@@ -128,11 +134,11 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr)
/// </summary> /// </summary>
/// <param name="predicate">The predicate to check the modifier by.</param> /// <param name="predicate">The predicate to check the modifier by.</param>
/// <typeparam name="T">The Type of the Modifier.</typeparam> /// <typeparam name="T">The Type of the Modifier.</typeparam>
/// <returns>The Modifier if it is found, null otherwise.</returns> /// <returns>A collection of modifiers.</returns>
[HideFromIl2Cpp] [HideFromIl2Cpp]
public IEnumerable<T> GetModifiersByType<T>(Func<T, bool>? predicate=null) where T : BaseModifier public IEnumerable<T> GetModifiers<T>(Func<T, bool>? predicate=null) where T : BaseModifier
{ {
return ActiveModifiers.OfType<T>().Where(x => predicate == null || predicate(x)); return Modifiers.OfType<T>().Where(x => predicate == null || predicate(x));
} }
/// <summary> /// <summary>
@@ -140,26 +146,26 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr)
/// </summary> /// </summary>
/// <param name="type">The modifier type.</param> /// <param name="type">The modifier type.</param>
/// <param name="predicate">The predicate to check the modifier by.</param> /// <param name="predicate">The predicate to check the modifier by.</param>
/// <returns>The Modifier if it is found, null otherwise.</returns> /// <returns>A collection of modifiers.</returns>
[HideFromIl2Cpp] [HideFromIl2Cpp]
public IEnumerable<BaseModifier> GetModifiersByType(Type type, Func<BaseModifier, bool>? predicate=null) public IEnumerable<BaseModifier> GetModifiers(Type type, Func<BaseModifier, bool>? predicate=null)
{ {
return ActiveModifiers.Where(x => x.GetType() == type && (predicate == null || predicate(x))); return Modifiers.Where(x => x.GetType() == type && (predicate == null || predicate(x)));
} }
/// <summary> /// <summary>
/// Gets a collection of modifiers by ID. /// Gets a collection of modifiers by type ID.
/// </summary> /// </summary>
/// <param name="id">The Modifier ID.</param> /// <param name="id">The modifier's type ID.</param>
/// <param name="predicate">The predicate to check the modifier by.</param> /// <param name="predicate">The predicate to check the modifier by.</param>
/// <returns>The Modifier if it is found, null otherwise.</returns> /// <returns>A collection of modifiers.</returns>
[HideFromIl2Cpp] [HideFromIl2Cpp]
public IEnumerable<BaseModifier> GetModifiersByType(uint id, Func<BaseModifier, bool>? predicate=null) public IEnumerable<BaseModifier> GetModifiers(uint id, Func<BaseModifier, bool>? predicate=null)
{ {
var type = ModifierManager.GetModifierType(id) ?? throw new InvalidOperationException( var type = ModifierManager.GetModifierType(id) ?? throw new InvalidOperationException(
$"Cannot get modifier with id {id} because it is not registered."); $"Cannot get modifier with id {id} because it is not registered.");
return GetModifiersByType(type, predicate); return GetModifiers(type, predicate);
} }
/// <summary> /// <summary>
@@ -191,23 +197,34 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr)
} }
/// <summary> /// <summary>
/// Tries to get a modifier by its ID. /// Tries to get a modifier by its type ID.
/// </summary> /// </summary>
/// <param name="id">The modifier ID.</param> /// <param name="id">The modifier type ID.</param>
/// <param name="modifier">The modifier or null.</param> /// <param name="modifier">The modifier or null.</param>
/// <param name="predicate">The predicate to check the modifier by.</param> /// <param name="predicate">The predicate to check the modifier by.</param>
/// <returns>True if the modifier was found, false otherwise.</returns> /// <returns>True if the modifier was found, false otherwise.</returns>
[HideFromIl2Cpp] [HideFromIl2Cpp]
public bool TryGetModifier(uint id, out BaseModifier? modifier, Func<BaseModifier, bool>? predicate = null) public bool TryGetModifierByTypeId(uint id, out BaseModifier? modifier, Func<BaseModifier, bool>? predicate = null)
{ {
var type = ModifierManager.GetModifierType(id) ?? throw new InvalidOperationException( modifier = GetModifierByTypeId(id, predicate);
$"Cannot get modifier with id {id} because it is not registered."); return modifier != null;
return TryGetModifier(type, out modifier, predicate);
} }
/// <summary> /// <summary>
/// Gets a modifier by its type, or null if the player doesn't have it. /// Tries to get a modifier by its unique ID.
/// </summary>
/// <param name="id">The modifier unique ID.</param>
/// <param name="modifier">The modifier or null.</param>
/// <returns>True if the modifier was found, false otherwise.</returns>
[HideFromIl2Cpp]
public bool TryGetModifierByUniqueId(uint id, out BaseModifier? modifier)
{
modifier = GetModifierByUniqueId(id);
return modifier != null;
}
/// <summary>
/// Gets a modifier by its type.
/// </summary> /// </summary>
/// <param name="predicate">The predicate to check the modifier by.</param> /// <param name="predicate">The predicate to check the modifier by.</param>
/// <typeparam name="T">The Type of the Modifier.</typeparam> /// <typeparam name="T">The Type of the Modifier.</typeparam>
@@ -215,11 +232,11 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr)
[HideFromIl2Cpp] [HideFromIl2Cpp]
public T? GetModifier<T>(Func<T, bool>? predicate = null) where T : BaseModifier public T? GetModifier<T>(Func<T, bool>? predicate = null) where T : BaseModifier
{ {
return GetModifiersByType(predicate).FirstOrDefault(); return GetModifiers(predicate).FirstOrDefault();
} }
/// <summary> /// <summary>
/// Gets a modifier by its type, or null if the player doesn't have it. /// Gets a modifier by its type.
/// </summary> /// </summary>
/// <param name="type">The modifier type.</param> /// <param name="type">The modifier type.</param>
/// <param name="predicate">The predicate to check the modifier by.</param> /// <param name="predicate">The predicate to check the modifier by.</param>
@@ -227,17 +244,17 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr)
[HideFromIl2Cpp] [HideFromIl2Cpp]
public BaseModifier? GetModifier(Type type, Func<BaseModifier, bool>? predicate = null) public BaseModifier? GetModifier(Type type, Func<BaseModifier, bool>? predicate = null)
{ {
return GetModifiersByType(type).FirstOrDefault(predicate ?? (_ => true)); return GetModifiers(type).FirstOrDefault(predicate ?? (_ => true));
} }
/// <summary> /// <summary>
/// Gets a modifier by its type, or null if the player doesn't have it. /// Gets a modifier by its type ID.
/// </summary> /// </summary>
/// <param name="id">The modifier ID.</param> /// <param name="id">The modifier ID.</param>
/// <param name="predicate">The predicate to check the modifier by.</param> /// <param name="predicate">The predicate to check the modifier by.</param>
/// <returns>The Modifier if it is found, null otherwise.</returns> /// <returns>The Modifier if it is found, null otherwise.</returns>
[HideFromIl2Cpp] [HideFromIl2Cpp]
public BaseModifier? GetModifier(uint id, Func<BaseModifier, bool>? predicate = null) public BaseModifier? GetModifierByTypeId(uint id, Func<BaseModifier, bool>? predicate = null)
{ {
var type = ModifierManager.GetModifierType(id) ?? throw new InvalidOperationException( var type = ModifierManager.GetModifierType(id) ?? throw new InvalidOperationException(
$"Cannot get modifier with id {id} because it is not registered."); $"Cannot get modifier with id {id} because it is not registered.");
@@ -245,6 +262,17 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr)
return GetModifier(type, predicate); return GetModifier(type, predicate);
} }
/// <summary>
/// Gets a modifier by unique ID.
/// </summary>
/// <param name="id">The modifier's unique ID.</param>
/// <returns>The modifier if it is found, or null.</returns>
[HideFromIl2Cpp]
public BaseModifier? GetModifierByUniqueId(uint id)
{
return Modifiers.Find(x => x.UniqueId == id);
}
/// <summary> /// <summary>
/// Removes a modifier from the player. /// Removes a modifier from the player.
/// </summary> /// </summary>
@@ -280,19 +308,6 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr)
RemoveModifier(modifier); RemoveModifier(modifier);
} }
/// <summary>
/// Removes a modifier from the player.
/// </summary>
/// <param name="modifierId">The modifier ID.</param>
/// <param name="predicate">The predicate to check the modifier by.</param>
[HideFromIl2Cpp]
public void RemoveModifier(uint modifierId, Func<BaseModifier, bool>? predicate = null)
{
var type = ModifierManager.GetModifierType(modifierId) ?? throw new InvalidOperationException(
$"Cannot remove modifier with id {modifierId} because it is not registered.");
RemoveModifier(type, predicate);
}
/// <summary> /// <summary>
/// Removes a modifier from the player. /// Removes a modifier from the player.
/// </summary> /// </summary>
@@ -309,6 +324,36 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr)
_toRemove.Add(modifier); _toRemove.Add(modifier);
} }
/// <summary>
/// Removes a modifier from the player.
/// </summary>
/// <param name="typeId">The modifier's type ID.</param>
/// <param name="predicate">The predicate to check the modifier by.</param>
[HideFromIl2Cpp]
public void RemoveModifierByTypeId(uint typeId, Func<BaseModifier, bool>? predicate = null)
{
var type = ModifierManager.GetModifierType(typeId) ?? throw new InvalidOperationException(
$"Cannot remove modifier with id {typeId} because it is not registered.");
RemoveModifier(type, predicate);
}
/// <summary>
/// Removes a modifier from the player.
/// </summary>
/// <param name="uniqueId">The modifier's unique ID.</param>
[HideFromIl2Cpp]
public void RemoveModifierByUniqueId(uint uniqueId)
{
var modifier = GetModifierByUniqueId(uniqueId);
if (modifier == null)
{
Logger<MiraApiPlugin>.Error($"Cannot remove modifier with unique id {uniqueId} because it is not active.");
return;
}
RemoveModifier(modifier);
}
/// <summary> /// <summary>
/// Adds a modifier to the player. /// Adds a modifier to the player.
/// </summary> /// </summary>
@@ -329,8 +374,8 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr)
[HideFromIl2Cpp] [HideFromIl2Cpp]
public BaseModifier? AddModifier(BaseModifier modifier) public BaseModifier? AddModifier(BaseModifier modifier)
{ {
var id = modifier.GetId(); var id = modifier.TypeId;
if (modifier.Unique && Modifiers.Find(x => x.GetId() == id) != null) if (modifier.Unique && Modifiers.Find(x => x.TypeId == id) != null)
{ {
Logger<MiraApiPlugin>.Error($"Player already has modifier with id {id}!"); Logger<MiraApiPlugin>.Error($"Player already has modifier with id {id}!");
return null; return null;
@@ -345,6 +390,7 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr)
_toAdd.Add(modifier); _toAdd.Add(modifier);
modifier.Player = _player; modifier.Player = _player;
modifier.ModifierComponent = this; modifier.ModifierComponent = this;
modifier.UniqueId = GetNextUniqueId();
return modifier; return modifier;
} }
@@ -398,7 +444,7 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr)
[HideFromIl2Cpp] [HideFromIl2Cpp]
public bool HasModifier<T>(Func<T, bool>? predicate=null) where T : BaseModifier public bool HasModifier<T>(Func<T, bool>? predicate=null) where T : BaseModifier
{ {
return ActiveModifiers.Exists(x => x is T modifier && (predicate == null || predicate(modifier))); return Modifiers.Exists(x => x is T modifier && (predicate == null || predicate(modifier)));
} }
/// <summary> /// <summary>
@@ -410,21 +456,32 @@ public class ModifierComponent(IntPtr cppPtr) : MonoBehaviour(cppPtr)
[HideFromIl2Cpp] [HideFromIl2Cpp]
public bool HasModifier(Type type, Func<BaseModifier, bool>? predicate=null) public bool HasModifier(Type type, Func<BaseModifier, bool>? predicate=null)
{ {
return ActiveModifiers.Exists(x => x.GetType() == type && (predicate == null || predicate(x))); return Modifiers.Exists(x => x.GetType() == type && (predicate == null || predicate(x)));
} }
/// <summary> /// <summary>
/// Checks if a player has an active modifier by its ID. /// Checks if a player has an active modifier by its type ID
/// </summary> /// </summary>
/// <param name="id">The Modifier ID.</param> /// <param name="id">The Modifier type ID.</param>
/// <param name="predicate">The predicate to check the modifier.</param> /// <param name="predicate">The predicate to check the modifier.</param>
/// <returns>True if the Modifier is present, false otherwise.</returns> /// <returns>True if the Modifier is present, false otherwise.</returns>
[HideFromIl2Cpp] [HideFromIl2Cpp]
public bool HasModifier(uint id, Func<BaseModifier, bool>? predicate=null) public bool HasModifierByTypeId(uint id, Func<BaseModifier, bool>? predicate=null)
{ {
var type = ModifierManager.GetModifierType(id) ?? throw new InvalidOperationException( var type = ModifierManager.GetModifierType(id) ?? throw new InvalidOperationException(
$"Cannot get modifier with id {id} because it is not registered."); $"Cannot get modifier with id {id} because it is not registered.");
return HasModifier(type, predicate); return HasModifier(type, predicate);
} }
/// <summary>
/// Checks if a player has an active modifier by its unique ID
/// </summary>
/// <param name="id">The Modifier type ID.</param>
/// <returns>True if the Modifier is present, false otherwise.</returns>
[HideFromIl2Cpp]
public bool HasModifierByUniqueId(uint id)
{
return Modifiers.Exists(x => x.UniqueId == id);
}
} }
+132 -59
View File
@@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using MiraAPI.Networking; using MiraAPI.Networking;
using MiraAPI.Networking.Modifiers;
using Reactor.Networking.Attributes; using Reactor.Networking.Attributes;
using Reactor.Networking.Rpc; using Reactor.Networking.Rpc;
using Reactor.Utilities; using Reactor.Utilities;
@@ -21,14 +22,14 @@ public static class ModifierExtensions
/// Remote Procedure Call to add a modifier to a player. /// Remote Procedure Call to add a modifier to a player.
/// </summary> /// </summary>
/// <param name="target">The player to add the modifier to.</param> /// <param name="target">The player to add the modifier to.</param>
/// <param name="modifierId">The modifier ID.</param> /// <param name="typeId">The modifier type ID.</param>
/// <param name="args">The arguments to initialize the modifier constructor with.</param> /// <param name="args">The arguments to initialize the modifier constructor with.</param>
public static void RpcAddModifier(this PlayerControl target, uint modifierId, params object[] args) public static void RpcAddModifier(this PlayerControl target, uint typeId, params object[] args)
{ {
_ = ModifierManager.GetModifierType(modifierId) ?? throw new InvalidOperationException( _ = ModifierManager.GetModifierType(typeId) ?? throw new InvalidOperationException(
$"Modifier with ID {modifierId} is not registered."); $"Modifier with ID {typeId} is not registered.");
Rpc<AddModifierRpc>.Instance.Send(target, new ModifierData(modifierId, args)); Rpc<AddModifierRpc>.Instance.Send(target, new ModifierData(typeId, args));
} }
/// <summary> /// <summary>
@@ -39,7 +40,7 @@ public static class ModifierExtensions
/// <param name="args">The arguments to initialize the modifier constructor with.</param> /// <param name="args">The arguments to initialize the modifier constructor with.</param>
public static void RpcAddModifier(this PlayerControl player, Type type, params object[] args) public static void RpcAddModifier(this PlayerControl player, Type type, params object[] args)
{ {
var id = ModifierManager.GetModifierId(type) ?? throw new InvalidOperationException( var id = ModifierManager.GetModifierTypeId(type) ?? throw new InvalidOperationException(
$"Modifier {type.Name} is not registered."); $"Modifier {type.Name} is not registered.");
player.RpcAddModifier(id, args); player.RpcAddModifier(id, args);
@@ -60,28 +61,61 @@ public static class ModifierExtensions
/// Remote Procedure Call to remove a modifier from a player. /// Remote Procedure Call to remove a modifier from a player.
/// </summary> /// </summary>
/// <param name="target">The player to remove the modifier from.</param> /// <param name="target">The player to remove the modifier from.</param>
/// <param name="modifierId">The ID of the modifier.</param> /// <param name="uniqueId">The unique ID of the modifier.</param>
[MethodRpc((uint)MiraRpc.RemoveModifier)] [MethodRpc((uint)MiraRpc.RemoveModifier)]
public static void RpcRemoveModifier(this PlayerControl target, uint modifierId) public static void RpcRemoveModifierByUniqueId(this PlayerControl target, uint uniqueId)
{ {
target.RemoveModifier(modifierId); target.RemoveModifierByUniqueId(uniqueId);
}
/// <summary>
/// Remote Procedure Call to remove a modifier from a player by type ID.
/// </summary>
/// <param name="target">The player to remove the modifier from.</param>
/// <param name="typeId">The type ID of the modifier.</param>
/// <param name="predicate">Optional predicate to filter the modifiers.</param>
public static void RpcRemoveModifierByTypeId(
this PlayerControl target,
uint typeId,
Func<BaseModifier, bool>? predicate = null)
{
var modifier = target.GetModifierByTypeId(typeId, predicate);
if (modifier is null)
{
Logger<MiraApiPlugin>.Error($"Player {target.PlayerId} does not have modifier with type ID {typeId}.");
return;
}
target.RpcRemoveModifierByUniqueId(modifier.UniqueId);
} }
/// <summary> /// <summary>
/// Remote Procedure Call to remove a modifier from a player. /// Remote Procedure Call to remove a modifier from a player.
/// </summary> /// </summary>
/// <param name="player">The player to remove the modifier from.</param> /// <param name="player">The player to remove the modifier from.</param>
/// <typeparam name="T">The Type of the Modifier.</typeparam> /// <param name="type">The type of the modifier.</param>
public static void RpcRemoveModifier<T>(this PlayerControl player) where T : BaseModifier /// <param name="predicate">Optional predicate to filter the modifiers.</param>
public static void RpcRemoveModifier(
this PlayerControl player,
Type type,
Func<BaseModifier, bool>? predicate = null)
{ {
var id = ModifierManager.GetModifierId(typeof(T)); var id = ModifierManager.GetModifierTypeId(type) ?? throw new InvalidOperationException(
if (id == null) $"Modifier {type.Name} is not registered.");
{
Logger<MiraApiPlugin>.Error($"Cannot add modifier {typeof(T).Name} because it is not registered.");
return;
}
player.RpcRemoveModifier(id.Value); player.RpcRemoveModifierByTypeId(id, predicate);
}
/// <summary>
/// Remote Procedure Call to remove a modifier from a player.
/// </summary>
/// <param name="player">The player to remove the modifier from.</param>
/// <param name="predicate">Optional predicate to filter the modifiers.</param>
/// <typeparam name="T">The Type of the Modifier.</typeparam>
public static void RpcRemoveModifier<T>(this PlayerControl player, Func<BaseModifier, bool>? predicate = null)
where T : BaseModifier
{
player.RpcRemoveModifier(typeof(T), predicate);
} }
/// <summary> /// <summary>
@@ -89,7 +123,7 @@ public static class ModifierExtensions
/// </summary> /// </summary>
/// <param name="player">The PlayerControl object.</param> /// <param name="player">The PlayerControl object.</param>
/// <returns>A ModifierComponent if there is one, null otherwise.</returns> /// <returns>A ModifierComponent if there is one, null otherwise.</returns>
public static ModifierComponent? GetModifierComponent(this PlayerControl player) public static ModifierComponent GetModifierComponent(this PlayerControl player)
{ {
if (ModifierComponents.TryGetValue(player, out var component)) if (ModifierComponents.TryGetValue(player, out var component))
{ {
@@ -97,9 +131,9 @@ public static class ModifierExtensions
} }
component = player.GetComponent<ModifierComponent>(); component = player.GetComponent<ModifierComponent>();
if (component == null) if (!component)
{ {
return null; throw new InvalidOperationException("ModifierComponent is not attached to the player.");
} }
ModifierComponents[player] = component; ModifierComponents[player] = component;
@@ -115,7 +149,7 @@ public static class ModifierExtensions
/// <returns>True if the player has the modifier, false otherwise.</returns> /// <returns>True if the player has the modifier, false otherwise.</returns>
public static bool HasModifier<T>(this PlayerControl player, Func<T, bool>? predicate = null) where T : BaseModifier public static bool HasModifier<T>(this PlayerControl player, Func<T, bool>? predicate = null) where T : BaseModifier
{ {
return player.GetModifierComponent()!.HasModifier(predicate); return player.GetModifierComponent().HasModifier(predicate);
} }
/// <summary> /// <summary>
@@ -127,19 +161,33 @@ public static class ModifierExtensions
/// <returns>True if the player has the modifier, false otherwise.</returns> /// <returns>True if the player has the modifier, false otherwise.</returns>
public static bool HasModifier(this PlayerControl player, Type type, Func<BaseModifier, bool>? predicate = null) public static bool HasModifier(this PlayerControl player, Type type, Func<BaseModifier, bool>? predicate = null)
{ {
return player.GetModifierComponent()!.HasModifier(type, predicate); return player.GetModifierComponent().HasModifier(type, predicate);
} }
/// <summary> /// <summary>
/// Checks if the player has a specific modifier by ID. /// Checks if the player has a specific modifier by type ID.
/// </summary> /// </summary>
/// <param name="player">The PlayerControl instance.</param> /// <param name="player">The PlayerControl instance.</param>
/// <param name="id">The ID of the modifier.</param> /// <param name="id">The type ID of the modifier.</param>
/// <param name="predicate">Optional predicate to filter the modifiers.</param> /// <param name="predicate">Optional predicate to filter the modifiers.</param>
/// <returns>True if the player has the modifier, false otherwise.</returns> /// <returns>True if the player has the modifier, false otherwise.</returns>
public static bool HasModifier(this PlayerControl player, uint id, Func<BaseModifier, bool>? predicate = null) public static bool HasModifierByTypeId(
this PlayerControl player,
uint id,
Func<BaseModifier, bool>? predicate = null)
{ {
return player.GetModifierComponent()!.HasModifier(id, predicate); return player.GetModifierComponent().HasModifierByTypeId(id, predicate);
}
/// <summary>
/// Checks if the player has a specific modifier by unique ID.
/// </summary>
/// <param name="player">The PlayerControl instance.</param>
/// <param name="id">The unique ID of the modifier.</param>
/// <returns>True if the player has the modifier, false otherwise.</returns>
public static bool HasModifierByUniqueId(this PlayerControl player, uint id)
{
return player.GetModifierComponent().HasModifierByUniqueId(id);
} }
/// <summary> /// <summary>
@@ -151,7 +199,7 @@ public static class ModifierExtensions
/// <returns>The modifier if found, null otherwise.</returns> /// <returns>The modifier if found, null otherwise.</returns>
public static T? GetModifier<T>(this PlayerControl player, Func<T, bool>? predicate = null) where T : BaseModifier public static T? GetModifier<T>(this PlayerControl player, Func<T, bool>? predicate = null) where T : BaseModifier
{ {
return player.GetModifierComponent()!.GetModifier(predicate); return player.GetModifierComponent().GetModifier(predicate);
} }
/// <summary> /// <summary>
@@ -166,7 +214,22 @@ public static class ModifierExtensions
Type type, Type type,
Func<BaseModifier, bool>? predicate = null) Func<BaseModifier, bool>? predicate = null)
{ {
return player.GetModifierComponent()!.GetModifier(type, predicate); return player.GetModifierComponent().GetModifier(type, predicate);
}
/// <summary>
/// Gets a specific modifier from the player by type ID.
/// </summary>
/// <param name="player">The PlayerControl instance.</param>
/// <param name="id">The type ID of the modifier.</param>
/// <param name="predicate">Optional predicate to filter the modifiers.</param>
/// <returns>The modifier if found, null otherwise.</returns>
public static BaseModifier? GetModifierByTypeId(
this PlayerControl player,
uint id,
Func<BaseModifier, bool>? predicate = null)
{
return player.GetModifierComponent().GetModifierByTypeId(id, predicate);
} }
/// <summary> /// <summary>
@@ -174,14 +237,12 @@ public static class ModifierExtensions
/// </summary> /// </summary>
/// <param name="player">The PlayerControl instance.</param> /// <param name="player">The PlayerControl instance.</param>
/// <param name="id">The ID of the modifier.</param> /// <param name="id">The ID of the modifier.</param>
/// <param name="predicate">Optional predicate to filter the modifiers.</param>
/// <returns>The modifier if found, null otherwise.</returns> /// <returns>The modifier if found, null otherwise.</returns>
public static BaseModifier? GetModifier( public static BaseModifier? GetModifierByUniqueId(
this PlayerControl player, this PlayerControl player,
uint id, uint id)
Func<BaseModifier, bool>? predicate = null)
{ {
return player.GetModifierComponent()!.GetModifier(id, predicate); return player.GetModifierComponent().GetModifierByUniqueId(id);
} }
/// <summary> /// <summary>
@@ -191,10 +252,10 @@ public static class ModifierExtensions
/// <param name="player">The PlayerControl instance.</param> /// <param name="player">The PlayerControl instance.</param>
/// <param name="predicate">Optional predicate to filter the modifiers.</param> /// <param name="predicate">Optional predicate to filter the modifiers.</param>
/// <returns>An enumerable of modifiers.</returns> /// <returns>An enumerable of modifiers.</returns>
public static IEnumerable<T> GetModifiersByType<T>(this PlayerControl player, Func<T, bool>? predicate = null) public static IEnumerable<T> GetModifiers<T>(this PlayerControl player, Func<T, bool>? predicate = null)
where T : BaseModifier where T : BaseModifier
{ {
return player.GetModifierComponent()!.GetModifiersByType(predicate); return player.GetModifierComponent().GetModifiers(predicate);
} }
/// <summary> /// <summary>
@@ -204,27 +265,37 @@ public static class ModifierExtensions
/// <param name="type">The type of the modifiers.</param> /// <param name="type">The type of the modifiers.</param>
/// <param name="predicate">Optional predicate to filter the modifiers.</param> /// <param name="predicate">Optional predicate to filter the modifiers.</param>
/// <returns>An enumerable of modifiers.</returns> /// <returns>An enumerable of modifiers.</returns>
public static IEnumerable<BaseModifier> GetModifiersByType( public static IEnumerable<BaseModifier> GetModifiers(
this PlayerControl player, this PlayerControl player,
Type type, Type type,
Func<BaseModifier, bool>? predicate = null) Func<BaseModifier, bool>? predicate = null)
{ {
return player.GetModifierComponent()!.GetModifiersByType(type, predicate); return player.GetModifierComponent().GetModifiers(type, predicate);
} }
/// <summary> /// <summary>
/// Gets all modifiers of a specific type from the player by ID. /// Gets all modifiers of a specific type from the player by type ID.
/// </summary> /// </summary>
/// <param name="player">The PlayerControl instance.</param> /// <param name="player">The PlayerControl instance.</param>
/// <param name="id">The ID of the modifiers.</param> /// <param name="id">The type ID of the modifiers.</param>
/// <param name="predicate">Optional predicate to filter the modifiers.</param> /// <param name="predicate">Optional predicate to filter the modifiers.</param>
/// <returns>An enumerable of modifiers.</returns> /// <returns>An enumerable of modifiers.</returns>
public static IEnumerable<BaseModifier> GetModifiersByType( public static IEnumerable<BaseModifier> GetModifiers(
this PlayerControl player, this PlayerControl player,
uint id, uint id,
Func<BaseModifier, bool>? predicate = null) Func<BaseModifier, bool>? predicate = null)
{ {
return player.GetModifierComponent()!.GetModifiersByType(id, predicate); return player.GetModifierComponent().GetModifiers(id, predicate);
}
/// <summary>
/// Removes a specific modifier from the player.
/// </summary>
/// <param name="player">The PlayerControl instance.</param>
/// <param name="modifier">The modifier to remove.</param>
public static void RemoveModifier(this PlayerControl player, BaseModifier modifier)
{
player.GetModifierComponent().RemoveModifier(modifier);
} }
/// <summary> /// <summary>
@@ -236,7 +307,7 @@ public static class ModifierExtensions
public static void RemoveModifier<T>(this PlayerControl player, Func<T, bool>? predicate = null) public static void RemoveModifier<T>(this PlayerControl player, Func<T, bool>? predicate = null)
where T : BaseModifier where T : BaseModifier
{ {
player.GetModifierComponent()!.RemoveModifier(predicate); player.GetModifierComponent().RemoveModifier(predicate);
} }
/// <summary> /// <summary>
@@ -247,31 +318,33 @@ public static class ModifierExtensions
/// <param name="predicate">Optional predicate to filter the modifiers.</param> /// <param name="predicate">Optional predicate to filter the modifiers.</param>
public static void RemoveModifier(this PlayerControl player, Type type, Func<BaseModifier, bool>? predicate = null) public static void RemoveModifier(this PlayerControl player, Type type, Func<BaseModifier, bool>? predicate = null)
{ {
player.GetModifierComponent()!.RemoveModifier(type, predicate); player.GetModifierComponent().RemoveModifier(type, predicate);
} }
/// <summary> /// <summary>
/// Removes a specific modifier from the player by ID. /// Removes a specific modifier from the player by ID.
/// </summary> /// </summary>
/// <param name="player">The PlayerControl instance.</param> /// <param name="player">The PlayerControl instance.</param>
/// <param name="modifierId">The ID of the modifier.</param> /// <param name="typeId">The ID of the modifier.</param>
/// <param name="predicate">Optional predicate to filter the modifiers.</param> /// <param name="predicate">Optional predicate to filter the modifiers.</param>
public static void RemoveModifier( public static void RemoveModifierByTypeId(
this PlayerControl player, this PlayerControl player,
uint modifierId, uint typeId,
Func<BaseModifier, bool>? predicate = null) Func<BaseModifier, bool>? predicate = null)
{ {
player.GetModifierComponent()!.RemoveModifier(modifierId, predicate); player.GetModifierComponent().RemoveModifierByTypeId(typeId, predicate);
} }
/// <summary> /// <summary>
/// Removes a specific modifier from the player. /// Removes a specific modifier from the player by ID.
/// </summary> /// </summary>
/// <param name="player">The PlayerControl instance.</param> /// <param name="player">The PlayerControl instance.</param>
/// <param name="modifier">The modifier to remove.</param> /// <param name="uniqueId">The ID of the modifier.</param>
public static void RemoveModifier(this PlayerControl player, BaseModifier modifier) public static void RemoveModifierByUniqueId(
this PlayerControl player,
uint uniqueId)
{ {
player.GetModifierComponent()!.RemoveModifier(modifier); player.GetModifierComponent().RemoveModifierByUniqueId(uniqueId);
} }
/// <summary> /// <summary>
@@ -283,7 +356,7 @@ public static class ModifierExtensions
/// <returns>The added modifier.</returns> /// <returns>The added modifier.</returns>
public static T? AddModifier<T>(this PlayerControl player, params object[] args) where T : BaseModifier public static T? AddModifier<T>(this PlayerControl player, params object[] args) where T : BaseModifier
{ {
return player.GetModifierComponent()!.AddModifier<T>(args); return player.GetModifierComponent().AddModifier<T>(args);
} }
/// <summary> /// <summary>
@@ -294,7 +367,7 @@ public static class ModifierExtensions
/// <returns>The added modifier.</returns> /// <returns>The added modifier.</returns>
public static BaseModifier? AddModifier(this PlayerControl player, BaseModifier modifier) public static BaseModifier? AddModifier(this PlayerControl player, BaseModifier modifier)
{ {
return player.GetModifierComponent()!.AddModifier(modifier); return player.GetModifierComponent().AddModifier(modifier);
} }
/// <summary> /// <summary>
@@ -306,18 +379,18 @@ public static class ModifierExtensions
/// <returns>The added modifier.</returns> /// <returns>The added modifier.</returns>
public static BaseModifier? AddModifier(this PlayerControl player, Type type, params object[] args) public static BaseModifier? AddModifier(this PlayerControl player, Type type, params object[] args)
{ {
return player.GetModifierComponent()!.AddModifier(type, args); return player.GetModifierComponent().AddModifier(type, args);
} }
/// <summary> /// <summary>
/// Adds a specific modifier to the player by ID. /// Adds a specific modifier to the player by type ID.
/// </summary> /// </summary>
/// <param name="player">The PlayerControl instance.</param> /// <param name="player">The PlayerControl instance.</param>
/// <param name="id">The ID of the modifier.</param> /// <param name="typeId">The type ID of the modifier.</param>
/// <param name="args">The arguments to initialize the modifier constructor with.</param> /// <param name="args">The arguments to initialize the modifier constructor with.</param>
/// <returns>The added modifier.</returns> /// <returns>The added modifier.</returns>
public static BaseModifier? AddModifier(this PlayerControl player, uint id, params object[] args) public static BaseModifier? AddModifier(this PlayerControl player, uint typeId, params object[] args)
{ {
return player.GetModifierComponent()!.AddModifier(id, args); return player.GetModifierComponent().AddModifier(typeId, args);
} }
} }
+12 -12
View File
@@ -4,6 +4,7 @@ using System.Linq;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using MiraAPI.Modifiers.Types; using MiraAPI.Modifiers.Types;
using MiraAPI.Networking; using MiraAPI.Networking;
using MiraAPI.Networking.Modifiers;
using MiraAPI.PluginLoading; using MiraAPI.PluginLoading;
using MiraAPI.Roles; using MiraAPI.Roles;
using MiraAPI.Utilities; using MiraAPI.Utilities;
@@ -23,16 +24,16 @@ public static class ModifierManager
private static readonly Dictionary<Type, uint> TypeToIdModifierMap = []; private static readonly Dictionary<Type, uint> TypeToIdModifierMap = [];
private static readonly Dictionary<int, List<uint>> PrioritiesToIdsMap = []; private static readonly Dictionary<int, List<uint>> PrioritiesToIdsMap = [];
private static uint _nextId; private static uint _nextTypeId;
private static uint GetNextId() private static uint GetNextTypeId()
{ {
_nextId++; _nextTypeId++;
return _nextId; return _nextTypeId;
} }
/// <summary> /// <summary>
/// Gets the modifier type from the id. /// Gets the modifier type from the type id.
/// </summary> /// </summary>
/// <param name="id">The ID.</param> /// <param name="id">The ID.</param>
/// <returns>The Type of the modifier.</returns> /// <returns>The Type of the modifier.</returns>
@@ -46,7 +47,7 @@ public static class ModifierManager
/// </summary> /// </summary>
/// <param name="type">The Type.</param> /// <param name="type">The Type.</param>
/// <returns>The ID of the modifier.</returns> /// <returns>The ID of the modifier.</returns>
public static uint? GetModifierId(Type type) public static uint? GetModifierTypeId(Type type)
{ {
if (!TypeToIdModifierMap.TryGetValue(type, out var id)) if (!TypeToIdModifierMap.TryGetValue(type, out var id))
{ {
@@ -63,8 +64,8 @@ public static class ModifierManager
return false; return false;
} }
IdToTypeModifierMap.Add(GetNextId(), modifierType); IdToTypeModifierMap.Add(GetNextTypeId(), modifierType);
TypeToIdModifierMap.Add(modifierType, _nextId); TypeToIdModifierMap.Add(modifierType, _nextTypeId);
var bm = (BaseModifier)FormatterServices.GetUninitializedObject(modifierType); // this probably isn't a great idea var bm = (BaseModifier)FormatterServices.GetUninitializedObject(modifierType); // this probably isn't a great idea
info.Modifiers.Add(bm); info.Modifiers.Add(bm);
@@ -81,7 +82,7 @@ public static class ModifierManager
PrioritiesToIdsMap[priority] = list = []; PrioritiesToIdsMap[priority] = list = [];
} }
list.Add(_nextId); list.Add(_nextTypeId);
return true; return true;
} }
@@ -143,7 +144,7 @@ public static class ModifierManager
var mod = Activator.CreateInstance(IdToTypeModifierMap[id]) as GameModifier; var mod = Activator.CreateInstance(IdToTypeModifierMap[id]) as GameModifier;
var plr = plrs.Where(x => IsGameModifierValid(x, mod!, id)).Random(); var plr = plrs.Where(x => IsGameModifierValid(x, mod!, id)).Random();
if (plr == null) if (!plr || plr == null)
{ {
Logger<MiraApiPlugin>.Warning($"Somehow valid players for modifier {mod!.ModifierName} disappeared"); Logger<MiraApiPlugin>.Warning($"Somehow valid players for modifier {mod!.ModifierName} disappeared");
} }
@@ -158,8 +159,7 @@ public static class ModifierManager
private static bool IsGameModifierValid(PlayerControl player, GameModifier modifier, uint modifierId) private static bool IsGameModifierValid(PlayerControl player, GameModifier modifier, uint modifierId)
{ {
return (player.Data.Role is not ICustomRole role || role.IsModifierApplicable(modifier)) && return (player.Data.Role is not ICustomRole role || role.IsModifierApplicable(modifier)) &&
modifier.IsModifierValidOn(player.Data.Role) && modifier.IsModifierValidOn(player.Data.Role) && !player.HasModifierByTypeId(modifierId);
!player.HasModifier(modifierId);
} }
internal static void SyncAllModifiers(int targetId = -1) internal static void SyncAllModifiers(int targetId = -1)
@@ -5,7 +5,7 @@ using Reactor.Networking.Attributes;
using Reactor.Networking.Rpc; using Reactor.Networking.Rpc;
using Reactor.Networking.Serialization; using Reactor.Networking.Serialization;
namespace MiraAPI.Networking; namespace MiraAPI.Networking.Modifiers;
/// <summary> /// <summary>
/// Remote procedure call for adding a modifier. /// Remote procedure call for adding a modifier.
@@ -3,7 +3,7 @@ using MiraAPI.Modifiers;
using Reactor.Networking.Attributes; using Reactor.Networking.Attributes;
using Reactor.Networking.Rpc; using Reactor.Networking.Rpc;
namespace MiraAPI.Networking; namespace MiraAPI.Networking.Modifiers;
[RegisterCustomRpc((uint)MiraRpc.SyncModifiers)] [RegisterCustomRpc((uint)MiraRpc.SyncModifiers)]
internal sealed class SyncModifiersRpc(MiraApiPlugin plugin, uint id) : PlayerCustomRpc<MiraApiPlugin, NetData[]>(plugin, id) internal sealed class SyncModifiersRpc(MiraApiPlugin plugin, uint id) : PlayerCustomRpc<MiraApiPlugin, NetData[]>(plugin, id)
@@ -2,7 +2,7 @@
using MiraAPI.Events; using MiraAPI.Events;
using MiraAPI.Events.Vanilla.Gameplay; using MiraAPI.Events.Vanilla.Gameplay;
namespace MiraAPI.Patches; namespace MiraAPI.Patches.Events;
/// <summary> /// <summary>
/// General patches for the KillAnimation class. /// General patches for the KillAnimation class.
+1 -1
View File
@@ -1,8 +1,8 @@
using HarmonyLib; using HarmonyLib;
using MiraAPI.Events; using MiraAPI.Events;
using MiraAPI.Events.Vanilla;
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection; using System.Reflection;
using MiraAPI.Events.Vanilla.Usables;
namespace MiraAPI.Patches.Events; namespace MiraAPI.Patches.Events;
@@ -16,7 +16,7 @@ internal static class TaskAddButtonPatches
if (uint.TryParse(__instance.name, out var result)) if (uint.TryParse(__instance.name, out var result))
{ {
__instance.Overlay.sprite = __instance.CheckImage; __instance.Overlay.sprite = __instance.CheckImage;
__instance.Overlay.enabled = PlayerControl.LocalPlayer.HasModifier(result); __instance.Overlay.enabled = PlayerControl.LocalPlayer.HasModifierByTypeId(result);
return false; return false;
} }
+4 -4
View File
@@ -326,7 +326,7 @@ internal static class TaskAdderPatches
} }
var taskAddButton = Object.Instantiate(instance.RoleButton); var taskAddButton = Object.Instantiate(instance.RoleButton);
taskAddButton.name = modifier.GetId().ToString(NumberFormatInfo.InvariantInfo); taskAddButton.name = modifier.TypeId.ToString(NumberFormatInfo.InvariantInfo);
taskAddButton.role = null; taskAddButton.role = null;
taskAddButton.MyTask = null; taskAddButton.MyTask = null;
taskAddButton.SafePositionWorld = instance.SafePositionWorld; taskAddButton.SafePositionWorld = instance.SafePositionWorld;
@@ -336,10 +336,10 @@ internal static class TaskAdderPatches
taskAddButton.Button.OnClick = new Button.ButtonClickedEvent(); taskAddButton.Button.OnClick = new Button.ButtonClickedEvent();
taskAddButton.Button.OnClick.AddListener((UnityEngine.Events.UnityAction)(() => taskAddButton.Button.OnClick.AddListener((UnityEngine.Events.UnityAction)(() =>
{ {
var id = modifier.GetId(); var id = modifier.TypeId;
if (PlayerControl.LocalPlayer.HasModifier(id)) if (PlayerControl.LocalPlayer.HasModifierByTypeId(id))
{ {
PlayerControl.LocalPlayer.RemoveModifier(id); PlayerControl.LocalPlayer.RemoveModifierByTypeId(id);
taskAddButton.Overlay.enabled = false; taskAddButton.Overlay.enabled = false;
} }
else else
@@ -24,7 +24,7 @@ public static class EndGameDidWinPatch
var didWin = networkedPlayerInfo.Role.DidWin(gameOverReason); var didWin = networkedPlayerInfo.Role.DidWin(gameOverReason);
var modifierWin = networkedPlayerInfo.Object?.GetModifierComponent()?.ActiveModifiers.OfType<GameModifier>() var modifierWin = networkedPlayerInfo.Object?.GetModifierComponent().GetModifiers<GameModifier>()
.FirstOrDefault(x => x.DidWin(gameOverReason) != null)?.DidWin(gameOverReason); .FirstOrDefault(x => x.DidWin(gameOverReason) != null)?.DidWin(gameOverReason);
if (modifierWin != null) if (modifierWin != null)
{ {
+2 -2
View File
@@ -35,8 +35,8 @@ public static class VentPatches
var canVent = role is ICustomRole customRole ? customRole.Configuration.CanUseVent : role.CanVent; var canVent = role is ICustomRole customRole ? customRole.Configuration.CanUseVent : role.CanVent;
couldUse = canVent; couldUse = canVent;
var modifiers = @object.GetModifierComponent()?.ActiveModifiers; var modifiers = @object.GetModifierComponent().ActiveModifiers;
if (modifiers is { Count: > 0 }) if (modifiers.Count > 0)
{ {
switch (canVent) switch (canVent)
{ {