Skip to content

Commit

Permalink
InteractingElevatorFix not being call inside elevator
Browse files Browse the repository at this point in the history
  • Loading branch information
louis1706 committed Dec 24, 2024
1 parent a6b1944 commit 282f6bf
Showing 1 changed file with 58 additions and 3 deletions.
61 changes: 58 additions & 3 deletions EXILED/Exiled.Events/Patches/Events/Player/InteractingElevator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Exiled.Events.Patches.Events.Player
{
#pragma warning disable SA1402 // File may only contain a single type
using System.Collections.Generic;
using System.Reflection.Emit;

Expand All @@ -19,13 +20,12 @@ namespace Exiled.Events.Patches.Events.Player

using Interactables.Interobjects;

using Mirror;

using static HarmonyLib.AccessTools;

/// <summary>
/// Patches <see cref="ElevatorDoor.ServerInteract" />.
/// Adds the <see cref="Handlers.Player.InteractingElevator" /> event.
/// <remarks>This event only get call when interact on the outside button.</remarks>
/// </summary>
[EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.InteractingElevator))]
[HarmonyPatch(typeof(ElevatorDoor), nameof(ElevatorDoor.ServerInteract))]
Expand All @@ -51,7 +51,7 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
new(OpCodes.Ldarg_1),
new(OpCodes.Call, Method(typeof(Player), nameof(Player.Get), new[] { typeof(ReferenceHub) })),

// elevatorChamber
// this.Chamber
new(OpCodes.Ldarg_0),
new(OpCodes.Ldfld, Field(typeof(ElevatorDoor), nameof(ElevatorDoor.Chamber))),

Expand All @@ -77,4 +77,59 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
ListPool<CodeInstruction>.Pool.Return(newInstructions);
}
}

/// <summary>
/// Patches <see cref="ElevatorChamber.ServerInteract" />.
/// Adds the <see cref="Handlers.Player.InteractingElevator" /> event.
/// <remarks>This event only get call when interact on the inside button.</remarks>
/// </summary>
[EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.InteractingElevator))]
[HarmonyPatch(typeof(ElevatorChamber), nameof(ElevatorChamber.ServerInteract))]
internal class InteractingElevator2
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

Label returnLabel = generator.DefineLabel();

newInstructions[newInstructions.Count - 1].labels.Add(returnLabel);

// InteractingElevatorEventArgs ev = new(Player.Get(referenceHub), elevatorChamber, true);
//
// Handlers.Player.OnInteractingElevator(ev);
//
// if (!ev.IsAllowed)
// continue;
newInstructions.InsertRange(0, new CodeInstruction[]
{
// Player.Get(referenceHub)
new(OpCodes.Ldarg_1),
new(OpCodes.Call, Method(typeof(Player), nameof(Player.Get), new[] { typeof(ReferenceHub) })),

// elevatorChamber
new(OpCodes.Ldarg_0),

// true
new(OpCodes.Ldc_I4_1),

// InteractingElevatorEventArgs ev = new(Player, ElevatorChamber, bool)
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(InteractingElevatorEventArgs))[0]),
new(OpCodes.Dup),

// Handlers.Player.OnInteractingElevator(ev)
new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnInteractingElevator))),

// if (!ev.IsAllowed)
// continue;
new(OpCodes.Callvirt, PropertyGetter(typeof(InteractingElevatorEventArgs), nameof(InteractingElevatorEventArgs.IsAllowed))),
new(OpCodes.Brfalse_S, returnLabel),
});

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

ListPool<CodeInstruction>.Pool.Return(newInstructions);
}
}
}

0 comments on commit 282f6bf

Please sign in to comment.