Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Round.IgnoredPlayers fix #2307

Merged
merged 8 commits into from
Dec 16, 2023
2 changes: 1 addition & 1 deletion Exiled.API/Features/Round.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static class Round
/// <summary>
/// Gets a list of players who will be ignored from determining round end.
/// </summary>
public static HashSet<ReferenceHub> IgnoredPlayers { get; } = new(20);
public static HashSet<ReferenceHub> IgnoredPlayers { get; } = new(20); // TODO: Replace ReferenceHub to Player remind to change RoundEnd transpiler

/// <summary>
/// Gets the time elapsed from the start of the round.
Expand Down
39 changes: 30 additions & 9 deletions Exiled.Events/Patches/Events/Server/RoundEnd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,20 @@ namespace Exiled.Events.Patches.Events.Server
using System.Reflection;
using System.Reflection.Emit;

using Exiled.API.Features;
using Exiled.API.Features.Pools;
using Exiled.Events.EventArgs.Server;

using HarmonyLib;
using PlayerRoles;

using static HarmonyLib.AccessTools;

/// <summary>
/// Patches <see cref="RoundSummary.Start" />.
/// Patches <see cref="RoundSummary._ProcessServerSideCode()" />.
/// Adds the <see cref="Handlers.Server.EndingRound" /> and <see cref="Handlers.Server.RoundEnded" /> event.
/// Adds the <see cref="Round.IgnoredPlayers" /> Propperty.
/// </summary>
/* TODO: Removed this when NW will have changed ChaosTargetCount == 0 with ChaosTargetCount <= 0
[EventPatch(typeof(Handlers.Server), nameof(Handlers.Server.EndingRound))]
[EventPatch(typeof(Handlers.Server), nameof(Handlers.Server.RoundEnded))]
*/
[HarmonyPatch]
internal static class RoundEnd
{
Expand All @@ -46,18 +45,40 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
const string LeadingTeam = "<leadingTeam>5__9";
const string NewList = "<newList>5__3";

int offset = -1;
int index = newInstructions.FindIndex(x => x.Calls(Method(typeof(PlayerRolesUtils), nameof(PlayerRolesUtils.GetTeam), new Type[] { typeof(ReferenceHub), }))) + offset;

Label labelcontinu = generator.DefineLabel();
louis1706 marked this conversation as resolved.
Show resolved Hide resolved
NaoUnderscore marked this conversation as resolved.
Show resolved Hide resolved

// if (Round.IgnoredPlayers.Contains(referencehub)
// goto labelcontinu;
louis1706 marked this conversation as resolved.
Show resolved Hide resolved
NaoUnderscore marked this conversation as resolved.
Show resolved Hide resolved
newInstructions.InsertRange(
index,
new CodeInstruction[]
{
new(OpCodes.Call, PropertyGetter(typeof(Round), nameof(Round.IgnoredPlayers))),
new(OpCodes.Ldloc_S, 10),
new(OpCodes.Call, Method(typeof(HashSet<ReferenceHub>), nameof(HashSet<ReferenceHub>.Contains))),
new(OpCodes.Brtrue_S, labelcontinu),
louis1706 marked this conversation as resolved.
Show resolved Hide resolved
NaoUnderscore marked this conversation as resolved.
Show resolved Hide resolved
});

offset = 4;
index = newInstructions.FindIndex(x => x.Calls(Method(typeof(PlayerRolesUtils), nameof(PlayerRolesUtils.GetTeam), new Type[] { typeof(ReferenceHub), }))) + offset;

newInstructions[index].labels.Add(labelcontinu);
louis1706 marked this conversation as resolved.
Show resolved Hide resolved
NaoUnderscore marked this conversation as resolved.
Show resolved Hide resolved

// Replace ChaosTargetCount == 0 with ChaosTargetCount <= 0
int offset = 1;
int index = newInstructions.FindIndex(x => x.Calls(PropertyGetter(typeof(RoundSummary), nameof(RoundSummary.ChaosTargetCount)))) + offset;
offset = 1;
index = newInstructions.FindIndex(x => x.Calls(PropertyGetter(typeof(RoundSummary), nameof(RoundSummary.ChaosTargetCount)))) + offset;
Label label = (Label)newInstructions[index].operand;
newInstructions.RemoveAt(index);

newInstructions.InsertRange(
index,
new CodeInstruction[]
{
new CodeInstruction(OpCodes.Ldc_I4_0),
new CodeInstruction(OpCodes.Bgt_S, label),
new(OpCodes.Ldc_I4_0),
new(OpCodes.Bgt_S, label),
});

offset = -1;
Expand Down
Loading