mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-10-20 01:46:15 +00:00
99 lines
3.0 KiB
C#
99 lines
3.0 KiB
C#
using System.Linq;
|
|
using AmongUs.GameOptions;
|
|
using HarmonyLib;
|
|
using MiraAPI.Events;
|
|
using MiraAPI.Events.Vanilla.Gameplay;
|
|
using MiraAPI.Modifiers;
|
|
using MiraAPI.Roles;
|
|
using UnityEngine;
|
|
|
|
namespace MiraAPI.Patches.Roles;
|
|
|
|
[HarmonyPatch(typeof(RoleManager))]
|
|
public static class RoleManagerPatches
|
|
{
|
|
[HarmonyPrefix]
|
|
[HarmonyPatch(nameof(RoleManager.SetRole))]
|
|
public static bool SetRolePatch(RoleManager __instance, PlayerControl targetPlayer, RoleTypes roleType)
|
|
{
|
|
if (!targetPlayer)
|
|
{
|
|
return false;
|
|
}
|
|
var data = targetPlayer.Data;
|
|
if (data == null)
|
|
{
|
|
Debug.LogError("It shouldn't be possible, but " + targetPlayer.name + " still doesn't have PlayerData during role selection.");
|
|
return false;
|
|
}
|
|
if (data.Role)
|
|
{
|
|
data.Role.Deinitialize(targetPlayer);
|
|
Object.Destroy(data.Role.gameObject);
|
|
}
|
|
var roleBehaviour = Object.Instantiate<RoleBehaviour>(__instance.AllRoles.First(r => r.Role == roleType), data.gameObject.transform);
|
|
roleBehaviour.Initialize(targetPlayer);
|
|
targetPlayer.Data.Role = roleBehaviour;
|
|
targetPlayer.Data.RoleType = roleType;
|
|
|
|
// basically everything is the same except this one if statement
|
|
// innersloth decided to check the roleType manually instead of the role behaviour.
|
|
if (!roleBehaviour.IsDead)
|
|
{
|
|
targetPlayer.Data.RoleWhenAlive = new Il2CppSystem.Nullable<RoleTypes>(roleType);
|
|
}
|
|
roleBehaviour.AdjustTasks(targetPlayer);
|
|
switch (roleBehaviour.IsDead)
|
|
{
|
|
case true when !targetPlayer.Data.IsDead:
|
|
targetPlayer.Die(DeathReason.Kill, false);
|
|
return false;
|
|
case false when targetPlayer.Data.IsDead:
|
|
targetPlayer.Revive();
|
|
break;
|
|
}
|
|
|
|
var @event = new SetRoleEvent(targetPlayer, roleType);
|
|
MiraEventManager.InvokeEvent(@event);
|
|
|
|
return false;
|
|
}
|
|
|
|
[HarmonyPostfix]
|
|
[HarmonyPatch(nameof(RoleManager.SelectRoles))]
|
|
public static void ModifierSelectionPatches(RoleManager __instance)
|
|
{
|
|
if (!AmongUsClient.Instance.AmHost)
|
|
{
|
|
return;
|
|
}
|
|
|
|
ModifierManager.AssignModifiers(
|
|
PlayerControl.AllPlayerControls.ToArray().Where(plr => !plr.Data.IsDead && !plr.Data.Disconnected)
|
|
.ToList());
|
|
}
|
|
|
|
[HarmonyPrefix]
|
|
[HarmonyPatch(nameof(RoleManager.AssignRoleOnDeath))]
|
|
public static bool AssignRoleOnDeath(RoleManager __instance, [HarmonyArgument(0)] PlayerControl plr)
|
|
{
|
|
if (!plr || !plr.Data.IsDead)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (plr.Data.Role is not ICustomRole role)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if (role.Configuration.GhostRole is RoleTypes.CrewmateGhost or RoleTypes.ImpostorGhost)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
plr.RpcSetRole(role.Configuration.GhostRole);
|
|
return false;
|
|
}
|
|
}
|