Skip to content

Commit

Permalink
Fix round end transpiler (#2167)
Browse files Browse the repository at this point in the history
* Update RoundEnd.cs

* Update RoundEnd.cs

* Update RoundEnd.cs

* Full Fix
  • Loading branch information
louis1706 authored Oct 24, 2023
1 parent 1407b75 commit a214f57
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions Exiled.Events/Patches/Events/Server/RoundEnd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

const string LeadingTeam = "<leadingTeam>5__9";
const string NewList = "<newList>5__3";

// Replace ChaosTargetCount == 0 with ChaosTargetCount <= 0
int offset = 1;
int index = newInstructions.FindIndex(x => x.Calls(PropertyGetter(typeof(RoundSummary), nameof(RoundSummary.ChaosTargetCount)))) + offset;
Expand All @@ -68,11 +71,11 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
{
// this.leadingTeam
new(OpCodes.Ldarg_0),
new(OpCodes.Ldfld, Field(PrivateType, "<leadingTeam>5__9")),
new(OpCodes.Ldfld, Field(PrivateType, LeadingTeam)),

// this.newList
new(OpCodes.Ldarg_0),
new(OpCodes.Ldfld, Field(PrivateType, "<newList>5__3")),
new(OpCodes.Ldfld, Field(PrivateType, NewList)),

// shouldRoundEnd
new(OpCodes.Ldloc_3),
Expand All @@ -89,7 +92,7 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
new(OpCodes.Ldarg_0),
new(OpCodes.Ldloc_S, evEndingRound.LocalIndex),
new(OpCodes.Callvirt, PropertyGetter(typeof(EndingRoundEventArgs), nameof(EndingRoundEventArgs.LeadingTeam))),
new(OpCodes.Stfld, Field(PrivateType, "<leadingTeam>5__9")),
new(OpCodes.Stfld, Field(PrivateType, LeadingTeam)),

// this._roundEnded = ev.IsAllowed
new(OpCodes.Ldloc_1),
Expand All @@ -101,39 +104,34 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
offset = 7;
index = newInstructions.FindLastIndex(x => x.opcode == OpCodes.Ldstr && x.operand == (object)"auto_round_restart_time") + offset;

LocalBuilder evEndedRound = generator.DeclareLocal(typeof(RoundEndedEventArgs));

LocalBuilder timeToRestartIndex = (LocalBuilder)newInstructions[index - 1].operand;
newInstructions.InsertRange(
index,
new CodeInstruction[]
{
// this.leadingTeam
new(OpCodes.Ldarg_0),
new(OpCodes.Ldfld, Field(PrivateType, "<leadingTeam>5__9")),
new(OpCodes.Ldfld, Field(PrivateType, LeadingTeam)),

// this.newList
new(OpCodes.Ldarg_0),
new(OpCodes.Ldfld, Field(PrivateType, "<newList>5__3")),
new(OpCodes.Ldfld, Field(PrivateType, NewList)),

// timeToRestart
new(OpCodes.Ldloc_S, 5),
new(OpCodes.Ldloc_S, timeToRestartIndex),

// RoundEndedEventArgs evEndedRound = new(RoundSummary.LeadingTeam, RoundSummary.SumInfo_ClassList, bool);
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(RoundEndedEventArgs))[0]),
new(OpCodes.Dup),

// Handlers.Server.OnRoundEnded(evEndedRound);
new(OpCodes.Call, Method(typeof(Handlers.Server), nameof(Handlers.Server.OnRoundEnded))),
new(OpCodes.Stloc_S, evEndedRound.LocalIndex),

// timeToRestart = ev.TimeToRestart
new(OpCodes.Ldloc_S, evEndingRound.LocalIndex),
new(OpCodes.Callvirt, PropertyGetter(typeof(RoundEndedEventArgs), nameof(RoundEndedEventArgs.TimeToRestart))),
new(OpCodes.Stloc_S, 5),
new(OpCodes.Stloc_S, timeToRestartIndex),
});

Label continueLabel = generator.DefineLabel();

for (int z = 0; z < newInstructions.Count; z++)
yield return newInstructions[z];

Expand Down

0 comments on commit a214f57

Please sign in to comment.