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

Fix Exiled.Events.EventArgs.Scp079.ElevatorTeleportingEventArgs & Lift.List empty #268

Merged
merged 2 commits into from
Nov 29, 2024
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
2 changes: 1 addition & 1 deletion EXILED/Exiled.API/Features/Lift.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class Lift : IWrapper<ElevatorChamber>, IWorldSpace
/// <summary>
/// A <see cref="Dictionary{TKey,TValue}"/> containing all known <see cref="ElevatorChamber"/>s and their corresponding <see cref="Lift"/>.
/// </summary>
internal static readonly Dictionary<ElevatorChamber, Lift> ElevatorChamberToLift = new(8, new ComponentsEqualityComparer());
internal static readonly Dictionary<ElevatorChamber, Lift> ElevatorChamberToLift = new(9, new ComponentsEqualityComparer());

/// <summary>
/// Internal list that contains all ElevatorDoor for current group.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ public class ElevatorTeleportingEventArgs : IScp079Event, IRoomEvent, IDeniableE
/// <param name="room">
/// <inheritdoc cref="Room" />
/// </param>
/// <param name="elevatorDoor">
/// <param name="elevatorChamber">
/// <inheritdoc cref="Lift" />
/// </param>
/// <param name="auxiliaryPowerCost">
/// <inheritdoc cref="AuxiliaryPowerCost" />
/// </param>
public ElevatorTeleportingEventArgs(Player player, RoomIdentifier room, ElevatorDoor elevatorDoor, float auxiliaryPowerCost)
public ElevatorTeleportingEventArgs(Player player, RoomIdentifier room, ElevatorChamber elevatorChamber, float auxiliaryPowerCost)
{
Player = player;
Scp079 = player.Role.As<Scp079Role>();
Room = Room.Get(room);
Lift = Lift.Get(elevatorDoor.Chamber);
Lift = Lift.Get(elevatorChamber);
AuxiliaryPowerCost = auxiliaryPowerCost;
IsAllowed = auxiliaryPowerCost <= Scp079.Energy;
}
Expand Down
108 changes: 46 additions & 62 deletions EXILED/Exiled.Events/Patches/Events/Scp079/ElevatorTeleporting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ namespace Exiled.Events.Patches.Events.Scp079
using Exiled.Events.Attributes;
using Exiled.Events.EventArgs.Scp079;
using Exiled.Events.Handlers;

using HarmonyLib;

using Mirror;

using PlayerRoles.PlayableScps.Scp079;
using PlayerRoles.PlayableScps.Scp079.Cameras;
using PlayerRoles.Subroutines;
Expand All @@ -40,71 +37,58 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

Label returnLabel = generator.DefineLabel();

LocalBuilder ev = generator.DeclareLocal(typeof(ElevatorTeleportingEventArgs));

int offset = 0;
int index = newInstructions.FindIndex(instruction => instruction.opcode == OpCodes.Ldloc_3) + offset;

// ElevatorTeleportingEventArgs ev = new(Player.Get(base.Owner), base.CurrentCamSync.CurrentCamera.Room, elevatorDoor, (float)this._cost);
//
// Handlers.Scp079.OnElevatorTeleporting(ev);
//
// if (!ev.IsAllowed)
// return;
newInstructions.InsertRange(
index,
new CodeInstruction[]
{
// Player.Get(base.Owner)
new CodeInstruction(OpCodes.Ldarg_0).MoveLabelsFrom(newInstructions[index]),
new(OpCodes.Call, PropertyGetter(typeof(StandardSubroutine<Scp079Role>), nameof(StandardSubroutine<Scp079Role>.Owner))),
new(OpCodes.Call, Method(typeof(Player), nameof(Player.Get), new[] { typeof(ReferenceHub) })),

// base.CurrentCamSync.CurrentCamera.Room
new(OpCodes.Ldarg_0),
new(OpCodes.Call, PropertyGetter(typeof(Scp079AbilityBase), nameof(Scp079AbilityBase.CurrentCamSync))),
new(OpCodes.Callvirt, PropertyGetter(typeof(Scp079CurrentCameraSync), nameof(Scp079CurrentCameraSync.CurrentCamera))),
new(OpCodes.Callvirt, PropertyGetter(typeof(Scp079Camera), nameof(Scp079Camera.Room))),

// elevatorDoor
new(OpCodes.Ldloc_3),

// (float)this._cost
new(OpCodes.Ldarg_0),
new(OpCodes.Ldfld, Field(typeof(Scp079ElevatorStateChanger), nameof(Scp079ElevatorStateChanger._cost))),
new(OpCodes.Conv_R4),

// ElevatorTeleportingEventArgs ev = new(Player, RoomIdentifier, ElevatorDoor, float)
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(ElevatorTeleportingEventArgs))[0]),
new(OpCodes.Dup),
new(OpCodes.Dup),
new(OpCodes.Stloc_S, ev.LocalIndex),

// Scp079.OnElevatorTeleporting(ev);
new(OpCodes.Call, Method(typeof(Scp079), nameof(Scp079.OnElevatorTeleporting))),

// if (!ev.IsAllowed)
// return;
new(OpCodes.Callvirt, PropertyGetter(typeof(ElevatorTeleportingEventArgs), nameof(ElevatorTeleportingEventArgs.IsAllowed))),
new(OpCodes.Brfalse, returnLabel),
});

// Replace "(float)this._cost" with "ev.AuxiliaryPowerCost"
int offset = -3;
int index = newInstructions.FindLastIndex(instruction => instruction.opcode == OpCodes.Ldloc_2) + offset;

newInstructions.InsertRange(index, new[]
{
// Player.Get(base.Owner)
new CodeInstruction(OpCodes.Ldarg_0).MoveLabelsFrom(newInstructions[index]),
new(OpCodes.Call, PropertyGetter(typeof(StandardSubroutine<Scp079Role>), nameof(StandardSubroutine<Scp079Role>.Owner))),
new(OpCodes.Call, Method(typeof(Player), nameof(Player.Get), new[] { typeof(ReferenceHub) })),

// base.CurrentCamSync.CurrentCamera.Room
new(OpCodes.Ldarg_0),
new(OpCodes.Call, PropertyGetter(typeof(Scp079AbilityBase), nameof(Scp079AbilityBase.CurrentCamSync))),
new(OpCodes.Callvirt, PropertyGetter(typeof(Scp079CurrentCameraSync), nameof(Scp079CurrentCameraSync.CurrentCamera))),
new(OpCodes.Callvirt, PropertyGetter(typeof(Scp079Camera), nameof(Scp079Camera.Room))),

// chamber
new(OpCodes.Ldloc_2),

// (float)this._cost
new(OpCodes.Ldarg_0),
new(OpCodes.Ldfld, Field(typeof(Scp079ElevatorStateChanger), nameof(Scp079ElevatorStateChanger._cost))),
new(OpCodes.Conv_R4),

// ElevatorTeleportingEventArgs ev = new(Player, RoomIdentifier, ElevatorChamber, float)
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(ElevatorTeleportingEventArgs))[0]),
new(OpCodes.Dup),
new(OpCodes.Dup),
new(OpCodes.Stloc_S, ev.LocalIndex),

// Scp079.OnElevatorTeleporting(ev);
new(OpCodes.Call, Method(typeof(Scp079), nameof(Scp079.OnElevatorTeleporting))),

// if (!ev.IsAllowed)
// return;
new(OpCodes.Callvirt, PropertyGetter(typeof(ElevatorTeleportingEventArgs), nameof(ElevatorTeleportingEventArgs.IsAllowed))),
new(OpCodes.Brfalse, returnLabel),
});

offset = -1;
index = newInstructions.FindLastIndex(
instruction => instruction.LoadsField(Field(typeof(Scp079ElevatorStateChanger), nameof(Scp079ElevatorStateChanger._cost)))) + offset;
index = newInstructions.FindLastIndex(instruction => instruction.LoadsField(Field(typeof(Scp079ElevatorStateChanger), nameof(Scp079ElevatorStateChanger._cost)))) + offset;

newInstructions.RemoveRange(index, 3);

newInstructions.InsertRange(
index,
new CodeInstruction[]
{
// ev.AuxiliaryPowerCost
new(OpCodes.Ldloc, ev.LocalIndex),
new(OpCodes.Callvirt, PropertyGetter(typeof(ElevatorTeleportingEventArgs), nameof(ElevatorTeleportingEventArgs.AuxiliaryPowerCost))),
});
newInstructions.InsertRange(index, new CodeInstruction[]
{
// ev.AuxiliaryPowerCost
new(OpCodes.Ldloc, ev.LocalIndex),
new(OpCodes.Callvirt, PropertyGetter(typeof(ElevatorTeleportingEventArgs), nameof(ElevatorTeleportingEventArgs.AuxiliaryPowerCost))),
});

newInstructions[newInstructions.Count - 1].WithLabels(returnLabel);

Expand Down
19 changes: 9 additions & 10 deletions EXILED/Exiled.Events/Patches/Generic/LiftList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,24 @@

namespace Exiled.Events.Patches.Generic
{
#pragma warning disable SA1313 // Parameter names should begin with lower-case letter
using API.Features;

using HarmonyLib;
using Interactables.Interobjects;

/// <summary>
/// Patches <see cref="ElevatorManager.SpawnAllChambers"/>.
/// Patches to control <see cref="Lift.List"/>.
/// </summary>
[HarmonyPatch(typeof(ElevatorManager), nameof(ElevatorManager.SpawnAllChambers))]
[HarmonyPatch]
internal class LiftList
{
private static void Postfix()
{
Lift.ElevatorChamberToLift.Clear();
[HarmonyPatch(typeof(ElevatorChamber), nameof(ElevatorChamber.Start))]
[HarmonyPostfix]
private static void Adding(ElevatorChamber __instance) => _ = new Lift(__instance);

foreach (ElevatorChamber lift in ElevatorChamber.AllChambers)
{
_ = new Lift(lift);
}
}
[HarmonyPatch(typeof(ElevatorChamber), nameof(ElevatorChamber.OnDestroy))]
[HarmonyPostfix]
private static void Removing(ElevatorChamber __instance) => Lift.ElevatorChamberToLift.Remove(__instance);
}
}