This repository has been archived by the owner on Jan 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="ElevatorArrivedEventArgs.cs" company="Exiled Team"> | ||
// Copyright (c) Exiled Team. All rights reserved. | ||
// Licensed under the CC BY-SA 3.0 license. | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
|
||
namespace Exiled.Events.EventArgs.Map | ||
{ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
using Exiled.API.Features; | ||
using Exiled.Events.EventArgs.Interfaces; | ||
|
||
/// <summary> | ||
/// Contains all information after an elevator has arrived. | ||
/// </summary> | ||
public class ElevatorArrivedEventArgs : IExiledEvent | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ElevatorArrivedEventArgs"/> class. | ||
/// </summary> | ||
/// <param name="lift"><inheritdoc cref="Lift"/></param> | ||
public ElevatorArrivedEventArgs(Lift lift) | ||
{ | ||
Lift = lift; | ||
Players = Player.Get(x => x.Lift == Lift).ToList(); | ||
} | ||
|
||
/// <summary> | ||
/// Gets an elevator. | ||
/// </summary> | ||
public Lift Lift { get; } | ||
|
||
/// <summary> | ||
/// Gets the players in the elevator. | ||
/// </summary> | ||
public IReadOnlyCollection<Player> Players { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="ElevatorMovingEventArgs.cs" company="Exiled Team"> | ||
// Copyright (c) Exiled Team. All rights reserved. | ||
// Licensed under the CC BY-SA 3.0 license. | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
|
||
namespace Exiled.Events.EventArgs.Map | ||
{ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
using Exiled.API.Features; | ||
using Exiled.Events.EventArgs.Interfaces; | ||
|
||
/// <summary> | ||
/// Contains all information before elevator starts moving. | ||
/// </summary> | ||
public class ElevatorMovingEventArgs : IDeniableEvent | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ElevatorMovingEventArgs"/> class. | ||
/// </summary> | ||
/// <param name="lift"><inheritdoc cref="Lift"/></param> | ||
/// <param name="isAllowed"><inheritdoc cref="IsAllowed"/></param> | ||
public ElevatorMovingEventArgs(Lift lift, bool isAllowed = true) | ||
{ | ||
Lift = lift; | ||
Players = Player.Get(x => x.Lift == lift).ToList(); | ||
IsAllowed = isAllowed; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the current elevator. | ||
/// </summary> | ||
public Lift Lift { get; } | ||
|
||
/// <summary> | ||
/// Gets a list of all players in elevator. | ||
/// </summary> | ||
public IReadOnlyCollection<Player> Players { get; } | ||
|
||
/// <inheritdoc/> | ||
public bool IsAllowed { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
Exiled.Events/Patches/Events/Map/ElevatorMovingAndArrived.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="ElevatorMovingAndArrived.cs" company="Exiled Team"> | ||
// Copyright (c) Exiled Team. All rights reserved. | ||
// Licensed under the CC BY-SA 3.0 license. | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
|
||
namespace Exiled.Events.Patches.Events.Map | ||
{ | ||
using System.Collections.Generic; | ||
using System.Reflection.Emit; | ||
|
||
using Exiled.API.Features; | ||
using Exiled.API.Features.Core.Generic.Pools; | ||
using Exiled.Events.Attributes; | ||
using Exiled.Events.EventArgs.Map; | ||
using HarmonyLib; | ||
using Interactables.Interobjects; | ||
using Mirror; | ||
|
||
using static HarmonyLib.AccessTools; | ||
|
||
/// <summary> | ||
/// Patches <see cref="ElevatorChamber.TrySetDestination"/> | ||
/// to add <see cref="Handlers.Map.ElevatorArrived"/> and <see cref="Handlers.Map.ElevatorMoving"/> events. | ||
/// </summary> | ||
[EventPatch(typeof(Handlers.Map), nameof(Handlers.Map.ElevatorArrived))] | ||
[EventPatch(typeof(Handlers.Map), nameof(Handlers.Map.ElevatorMoving))] | ||
[HarmonyPatch(typeof(ElevatorChamber), nameof(ElevatorChamber.TrySetDestination))] | ||
internal class ElevatorMovingAndArrived | ||
{ | ||
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator) | ||
{ | ||
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions); | ||
|
||
int index = newInstructions.FindIndex(x => x.Calls(PropertyGetter(typeof(NetworkServer), nameof(NetworkServer.active)))); | ||
|
||
Label returnLabel = generator.DefineLabel(); | ||
|
||
newInstructions.InsertRange( | ||
index, | ||
new[] | ||
{ | ||
// Lift.Get(this); | ||
new CodeInstruction(OpCodes.Ldarg_0).MoveLabelsFrom(newInstructions[index]), | ||
new(OpCodes.Call, Method(typeof(Lift), nameof(Lift.Get), new[] { typeof(ElevatorChamber) })), | ||
|
||
// true | ||
new(OpCodes.Ldc_I4_1), | ||
|
||
// ElevatorMovingEventArgs ev = new(Lift, true); | ||
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(ElevatorMovingEventArgs))), | ||
new(OpCodes.Dup), | ||
new(OpCodes.Dup), | ||
|
||
// Handlers.Map.OnElevatorMoving(ev); | ||
new(OpCodes.Call, Method(typeof(Handlers.Map), nameof(Handlers.Map.OnElevatorMoving))), | ||
|
||
// if (!ev.IsAllowed) | ||
// return; | ||
new(OpCodes.Callvirt, PropertyGetter(typeof(ElevatorMovingEventArgs), nameof(ElevatorMovingEventArgs.IsAllowed))), | ||
new(OpCodes.Brfalse_S, returnLabel), | ||
}); | ||
|
||
int offset = 1; | ||
index = newInstructions.FindLastIndex(x => x.Calls(Method(typeof(ElevatorChamber), nameof(ElevatorChamber.SetInnerDoor)))) + offset; | ||
|
||
newInstructions.InsertRange( | ||
index, | ||
new[] | ||
{ | ||
// Lift.Get(this); | ||
new CodeInstruction(OpCodes.Ldarg_0).MoveLabelsFrom(newInstructions[index]), | ||
new(OpCodes.Call, Method(typeof(Lift), nameof(Lift.Get), new[] { typeof(ElevatorChamber) })), | ||
|
||
// ElevatorArrivedEventArgs ev = new(Lift); | ||
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(ElevatorArrivedEventArgs))), | ||
|
||
// Handlers.Map.OnElevatorArrived(ev); | ||
new(OpCodes.Call, Method(typeof(Handlers.Map), nameof(Handlers.Map.OnElevatorArrived))), | ||
}); | ||
|
||
newInstructions[newInstructions.Count - 1].labels.Add(returnLabel); | ||
|
||
for (int z = 0; z < newInstructions.Count; z++) | ||
yield return newInstructions[z]; | ||
|
||
ListPool<CodeInstruction>.Pool.Return(newInstructions); | ||
} | ||
} | ||
} |