Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 28 additions & 22 deletions EXILED/Exiled.Events/EventArgs/Server/EndingRoundEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

namespace Exiled.Events.EventArgs.Server
{
using System;

using API.Enums;
using Interfaces;

Expand All @@ -23,21 +21,18 @@ public class EndingRoundEventArgs : IDeniableEvent
/// <param name="classList">
/// <inheritdoc cref="RoundSummary.SumInfo_ClassList" />
/// </param>
/// <param name="leadingTeam">
/// <inheritdoc cref="LeadingTeam" />
/// </param>
/// <param name="isAllowed">
/// <inheritdoc cref="IsRoundEnded" />
/// </param>
/// <param name="isForceEnded">
/// <inheritdoc cref="IsForceEnded" />
/// </param>
public EndingRoundEventArgs(RoundSummary.LeadingTeam leadingTeam, RoundSummary.SumInfo_ClassList classList, bool isAllowed, bool isForceEnded)
/// <param name="isAllowed">
/// <inheritdoc cref="IsAllowed" />
/// </param>
public EndingRoundEventArgs(RoundSummary.SumInfo_ClassList classList, bool isForceEnded, bool isAllowed)
{
ClassList = classList;
LeadingTeam = (LeadingTeam)leadingTeam;
IsRoundEnded = isAllowed;
LeadingTeam = GetLeadingTeam(classList);
IsForceEnded = isForceEnded;
IsAllowed = isAllowed;
}

/// <summary>
Expand All @@ -51,22 +46,33 @@ public EndingRoundEventArgs(RoundSummary.LeadingTeam leadingTeam, RoundSummary.S
public LeadingTeam LeadingTeam { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the round is going to finish.
/// </summary>
public bool IsRoundEnded { get; set; } // TODO: Obsolete this in Exiled 10

/// <summary>
/// Gets or Sets a value indicating whether the round is ended by API call.
/// Gets or sets a value indicating whether the round is ended by API call.
/// </summary>
public bool IsForceEnded { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the event can be executed.
/// Gets or sets a value indicating whether the round is going to finish or not.
/// </summary>
public bool IsAllowed
public bool IsAllowed { get; set; }

private LeadingTeam GetLeadingTeam(RoundSummary.SumInfo_ClassList classList)
{
get => IsRoundEnded;
set => IsRoundEnded = value;
// NW logic
int facilityForces = classList.mtf_and_guards + classList.scientists;
int chaosInsurgency = classList.chaos_insurgents + classList.class_ds;
int anomalies = classList.scps_except_zombies + classList.zombies;
int num4 = facilityForces > 0 ? 1 : 0;
bool flag1 = chaosInsurgency > 0;
bool flag2 = anomalies > 0;
RoundSummary.LeadingTeam leadingTeam = RoundSummary.LeadingTeam.Draw;
if (num4 != 0)
leadingTeam = RoundSummary.EscapedScientists >= RoundSummary.EscapedClassD ? RoundSummary.LeadingTeam.FacilityForces : RoundSummary.LeadingTeam.Draw;
else if (flag2 || flag2 & flag1)
leadingTeam = RoundSummary.EscapedClassD > RoundSummary.SurvivingSCPs ? RoundSummary.LeadingTeam.ChaosInsurgency : (RoundSummary.SurvivingSCPs > RoundSummary.EscapedScientists ? RoundSummary.LeadingTeam.Anomalies : RoundSummary.LeadingTeam.Draw);
else if (flag1)
leadingTeam = RoundSummary.EscapedClassD >= RoundSummary.EscapedScientists ? RoundSummary.LeadingTeam.ChaosInsurgency : RoundSummary.LeadingTeam.Draw;

return (LeadingTeam)leadingTeam;
}
}
}
}
53 changes: 35 additions & 18 deletions EXILED/Exiled.Events/Patches/Events/Server/RoundEnd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,42 +78,32 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
newInstructions[index].labels.Add(jmp);

offset = -1;
index = newInstructions.FindIndex(x => x.opcode == OpCodes.Ldfld && x.operand == (object)Field(typeof(RoundSummary), nameof(RoundSummary._roundEnded))) + offset;
index = newInstructions.FindIndex(x => x.LoadsField(Field(typeof(RoundSummary), nameof(RoundSummary._roundEnded)))) + offset;
LocalBuilder evEndingRound = generator.DeclareLocal(typeof(EndingRoundEventArgs));

newInstructions.InsertRange(
index,
new CodeInstruction[]
new[]
{
// this.leadingTeam
new CodeInstruction(OpCodes.Ldarg_0).MoveLabelsFrom(newInstructions[index]),
new(OpCodes.Ldfld, Field(PrivateType, LeadingTeam)),

// this.newList
new(OpCodes.Ldarg_0),
new CodeInstruction(OpCodes.Ldarg_0).MoveLabelsFrom(newInstructions[index]),
new(OpCodes.Ldfld, Field(PrivateType, NewList)),

// shouldRoundEnd
new(OpCodes.Ldloc_S, 4),

// isForceEnd
new(OpCodes.Ldloc_1),
new(OpCodes.Ldfld, Field(typeof(RoundSummary), nameof(RoundSummary._roundEnded))),

// EndingRoundEventArgs evEndingRound = new(RoundSummary.LeadingTeam, RoundSummary.SumInfo_ClassList, bool, bool);
// baseGameConditionsSatisfied
new(OpCodes.Ldloc_S, 5),

// EndingRoundEventArgs evEndingRound = new(RoundSummary.SumInfo_ClassList, bool, bool);
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(EndingRoundEventArgs))[0]),
new(OpCodes.Dup),

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

// this.leadingTeam = ev.LeadingTeam
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)),

// this._roundEnded = ev.IsForceEnded
new(OpCodes.Ldloc_1),
new(OpCodes.Ldloc_S, evEndingRound.LocalIndex),
Expand All @@ -123,9 +113,36 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
// flag = ev.IsAllowed
new(OpCodes.Ldloc_S, evEndingRound.LocalIndex),
new(OpCodes.Callvirt, PropertyGetter(typeof(EndingRoundEventArgs), nameof(EndingRoundEventArgs.IsAllowed))),
new(OpCodes.Stloc_S, 4),
new(OpCodes.Stloc_S, 5),
});

// Replace NW logic to LeadingTeam leadingTeam = ev.LeadingTeam
offset = 2;
index = newInstructions.FindLastIndex(x => x.LoadsField(Field(typeof(RoundSummary), nameof(RoundSummary._roundEnded)))) + offset;
int offset2 = 1;
int index2 = newInstructions.FindLastIndex(x => x.opcode == OpCodes.Stloc_S && x.operand is LocalBuilder { LocalIndex: 7 }) + offset2;

newInstructions.RemoveRange(index, index2 - index);

offset = -1;
index = newInstructions.FindIndex(x => x.StoresField(Field(PrivateType, LeadingTeam))) + offset;

newInstructions.RemoveAt(index);
newInstructions.InsertRange(
index,
new CodeInstruction[]
{
new(OpCodes.Ldloc_S, evEndingRound.LocalIndex),
new(OpCodes.Callvirt, PropertyGetter(typeof(EndingRoundEventArgs), nameof(EndingRoundEventArgs.LeadingTeam))),
});

offset = 1;
index = newInstructions.FindIndex(x => x.StoresField(Field(PrivateType, LeadingTeam))) + offset;
offset2 = 1;
index2 = newInstructions.FindLastIndex(x => x.StoresField(Field(PrivateType, LeadingTeam))) + offset2;

newInstructions.RemoveRange(index, index2 - index);

// Round.LastClassList = this.newList;
offset = 1;
index = newInstructions.FindIndex(x => x.opcode == OpCodes.Stfld && x.operand == (object)Field(typeof(SumInfo_ClassList), nameof(SumInfo_ClassList.warhead_kills))) + offset;
Expand Down