Skip to content

Commit

Permalink
Server RoundEnding small change (#306)
Browse files Browse the repository at this point in the history
* IDK why don't work

* fix offset

* RemoveDebug

* little change

* Oups

* fix
  • Loading branch information
louis1706 authored Dec 11, 2024
1 parent a37c26a commit 941aacd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 48 deletions.
27 changes: 5 additions & 22 deletions EXILED/Exiled.Events/EventArgs/Server/EndingRoundEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public class EndingRoundEventArgs : IDeniableEvent
/// <summary>
/// Initializes a new instance of the <see cref="EndingRoundEventArgs" /> class.
/// </summary>
/// <param name="leadingTeam">
/// <inheritdoc cref="LeadingTeam" />
/// </param>
/// <param name="classList">
/// <inheritdoc cref="RoundSummary.SumInfo_ClassList" />
/// </param>
Expand All @@ -27,10 +30,10 @@ public class EndingRoundEventArgs : IDeniableEvent
/// <param name="isAllowed">
/// <inheritdoc cref="IsAllowed" />
/// </param>
public EndingRoundEventArgs(RoundSummary.SumInfo_ClassList classList, bool isForceEnded, bool isAllowed)
public EndingRoundEventArgs(LeadingTeam leadingTeam, RoundSummary.SumInfo_ClassList classList, bool isForceEnded, bool isAllowed)
{
LeadingTeam = leadingTeam;
ClassList = classList;
LeadingTeam = GetLeadingTeam(classList);
IsForceEnded = isForceEnded;
IsAllowed = isAllowed;
}
Expand All @@ -54,25 +57,5 @@ public EndingRoundEventArgs(RoundSummary.SumInfo_ClassList classList, bool isFor
/// Gets or sets a value indicating whether the round is going to finish or not.
/// </summary>
public bool IsAllowed { get; set; }

private LeadingTeam GetLeadingTeam(RoundSummary.SumInfo_ClassList classList)
{
// 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;
}
}
}
50 changes: 24 additions & 26 deletions EXILED/Exiled.Events/Patches/Events/Server/RoundEnd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,35 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi

newInstructions[index].labels.Add(jmp);

// Get the whole leadingteam logic
offset = -16;
index = newInstructions.FindIndex(x => x.StoresField(Field(PrivateType, LeadingTeam))) + offset;
int offset2 = 1;
int index2 = newInstructions.FindLastIndex(x => x.StoresField(Field(PrivateType, LeadingTeam))) + offset2;
List<CodeInstruction> leadingTeamLogic = newInstructions.GetRange(index, index2 - index);
List<Label> moveLabel = newInstructions[index2].ExtractLabels();
newInstructions.RemoveRange(index, index2 - index);

// put the LeadingTeam logic before the event
offset = -1;
index = newInstructions.FindIndex(x => x.LoadsField(Field(typeof(RoundSummary), nameof(RoundSummary._roundEnded)))) + offset;
newInstructions.InsertRange(index, leadingTeamLogic);

// recorect the index because of the LeadingTeamLogic that got moved
offset = -1;
index = newInstructions.FindIndex(x => x.LoadsField(Field(typeof(RoundSummary), nameof(RoundSummary._roundEnded)))) + offset;
LocalBuilder evEndingRound = generator.DeclareLocal(typeof(EndingRoundEventArgs));

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

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

// isForceEnd
Expand Down Expand Up @@ -113,35 +132,14 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
new(OpCodes.Ldloc_S, evEndingRound.LocalIndex),
new(OpCodes.Callvirt, PropertyGetter(typeof(EndingRoundEventArgs), nameof(EndingRoundEventArgs.IsAllowed))),
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[]
{
// 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)),
});

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

0 comments on commit 941aacd

Please sign in to comment.