Skip to content

Commit

Permalink
Multiple events fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
xNexusACS committed Dec 6, 2024
1 parent f8409c4 commit 0475412
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 33 deletions.
12 changes: 10 additions & 2 deletions Exiled.Events/EventArgs/Player/AddingItemEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ namespace Exiled.Events.EventArgs.Player
using Exiled.API.Features;
using Exiled.API.Features.Items;
using Exiled.API.Features.Pickups;
using Exiled.Events.EventArgs.Interfaces;
using Interfaces;
using InventorySystem.Items;

/// <summary>
/// Contains all information before adding an item to a player's inventory.
Expand All @@ -23,12 +24,14 @@ public class AddingItemEventArgs : IPlayerEvent, IDeniableEvent, IItemEvent, IPi
/// <param name="player"><inheritdoc cref="Player"/></param>
/// <param name="item"><inheritdoc cref="Item"/></param>
/// <param name="pickup"><inheritdoc cref="Pickup"/></param>
/// <param name="addReason"><inheritdoc cref="AddReason"/></param>
/// <param name="isAllowed"><inheritdoc cref="IsAllowed"/></param>
public AddingItemEventArgs(Player player, Item item, Pickup pickup, bool isAllowed = true)
public AddingItemEventArgs(Player player, Item item, Pickup pickup, ItemAddReason addReason, bool isAllowed = true)
{
Player = player;
Item = item;
Pickup = pickup;
AddReason = addReason;
IsAllowed = isAllowed;
}

Expand All @@ -45,6 +48,11 @@ public AddingItemEventArgs(Player player, Item item, Pickup pickup, bool isAllow
/// </summary>
public Pickup Pickup { get; }

/// <summary>
/// Gets or sets the reason of why the item was added to the player inventory.
/// </summary>
public ItemAddReason AddReason { get; set; }

/// <inheritdoc/>
public bool IsAllowed { get; set; }
}
Expand Down
16 changes: 11 additions & 5 deletions Exiled.Events/Patches/Events/Player/AddingItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace Exiled.Events.Patches.Events.Player
using static HarmonyLib.AccessTools;

/// <summary>
/// Patches <see cref="InventoryExtensions.ServerAddItem(Inventory, ItemType, ushort, ItemPickupBase)"/>.
/// Patches <see cref="InventoryExtensions.ServerAddItem(Inventory, ItemType, ItemAddReason, ushort, ItemPickupBase)"/>.
/// Adds the <see cref="Handlers.Player.AddingItem"/> event.
/// </summary>
[EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.AddingItem))]
Expand All @@ -40,28 +40,34 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
int index = newInstructions.FindIndex(i => i.opcode == OpCodes.Ldarg_3) + offset;

Label ret = generator.DefineLabel();
LocalBuilder ev = generator.DeclareLocal(typeof(AddingItemEventArgs));

newInstructions.InsertRange(index, new CodeInstruction[]
newInstructions.InsertRange(index, new[]
{
new CodeInstruction(OpCodes.Ldarg_0).MoveLabelsFrom(newInstructions[index]),
new(OpCodes.Ldfld, Field(typeof(Inventory), nameof(Inventory._hub))),
new(OpCodes.Call, Method(typeof(Player), nameof(Player.Get), new[] { typeof(ReferenceHub) })),
new(OpCodes.Ldloc_1),
new(OpCodes.Call, Method(typeof(Item), nameof(Item.Get), new[] { typeof(ItemBase) })),
new(OpCodes.Ldarg_3),
new(OpCodes.Ldarg_S, 4),
new(OpCodes.Call, Method(typeof(Pickup), nameof(Pickup.Get), new[] { typeof(ItemPickupBase) })),
new(OpCodes.Ldc_I4_1),
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(AddingItemEventArgs))[0]),
new(OpCodes.Dup),
new(OpCodes.Dup),
new(OpCodes.Stloc_S, ev.LocalIndex),
new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnAddingItem))),
new(OpCodes.Callvirt, PropertyGetter(typeof(AddingItemEventArgs), nameof(AddingItemEventArgs.IsAllowed))),
new(OpCodes.Brfalse_S, ret),
new(OpCodes.Ldloc_S, ev.LocalIndex),
new(OpCodes.Callvirt, PropertyGetter(typeof(AddingItemEventArgs), nameof(AddingItemEventArgs.AddReason))),
new(OpCodes.Starg_S, 3),
});

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

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

ListPool<CodeInstruction>.Pool.Return(newInstructions);
}
Expand Down
12 changes: 6 additions & 6 deletions Exiled.Events/Patches/Events/Player/EnteringKillerCollision.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ namespace Exiled.Events.Patches.Events.Player
using static HarmonyLib.AccessTools;

/// <summary>
/// Patches <see cref="CheckpointKiller.OnTriggerEnter"/>.
/// Patches <see cref="PitKiller.OnTriggerEnter"/>.
/// Adds the <see cref="Handlers.Player.EnteringKillerCollision"/> event.
/// </summary>
[EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.EnteringKillerCollision))]
[HarmonyPatch(typeof(CheckpointKiller), nameof(CheckpointKiller.OnTriggerEnter))]
[HarmonyPatch(typeof(PitKiller), nameof(PitKiller.OnTriggerEnter))]
internal static class EnteringKillerCollision
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
Expand All @@ -32,8 +32,8 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi

Label ret = generator.DefineLabel();

int offset = -5;
int index = newInstructions.FindLastIndex(instruction => instruction.opcode == OpCodes.Newobj) + offset;
const int offset = -1;
int index = newInstructions.FindLastIndex(instruction => instruction.opcode == OpCodes.Ldfld) + offset;

newInstructions.InsertRange(
index,
Expand Down Expand Up @@ -61,8 +61,8 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi

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

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

ListPool<CodeInstruction>.Pool.Return(newInstructions);
}
Expand Down
22 changes: 12 additions & 10 deletions Exiled.Events/Patches/Events/Player/InteractingElevator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ namespace Exiled.Events.Patches.Events.Player
using static HarmonyLib.AccessTools;

/// <summary>
/// Patches <see cref="ElevatorManager.ServerReceiveMessage(NetworkConnection, ElevatorManager.ElevatorSyncMsg)" />.
/// Patches <see cref="ElevatorChamber.ServerInteract(ReferenceHub, byte)" />.
/// Adds the <see cref="Handlers.Player.InteractingElevator" /> event.
/// </summary>
[EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.InteractingElevator))]
[HarmonyPatch(typeof(ElevatorManager), nameof(ElevatorManager.ServerReceiveMessage))]
[HarmonyPatch(typeof(ElevatorChamber), nameof(ElevatorChamber.ServerInteract))]
internal class InteractingElevator
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

Label @break = (Label)newInstructions.FindLast(i => i.opcode == OpCodes.Leave_S).operand;
Label retLabel = generator.DefineLabel();

int offset = -2;
int index = newInstructions.FindLastIndex(i => i.opcode == OpCodes.Newobj) + offset;
const int offset = -1;
int index = newInstructions.FindLastIndex(i => i.opcode == OpCodes.Ldarg_0) + offset;

// InteractingElevatorEventArgs ev = new(Player.Get(referenceHub), elevatorChamber, true);
//
Expand All @@ -51,11 +51,11 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
new[]
{
// Player.Get(referenceHub)
new CodeInstruction(OpCodes.Ldloc_0).MoveLabelsFrom(newInstructions[index]),
new CodeInstruction(OpCodes.Ldarg_1).MoveLabelsFrom(newInstructions[index]),
new(OpCodes.Call, Method(typeof(Player), nameof(Player.Get), new[] { typeof(ReferenceHub) })),

// elevatorChamber
new(OpCodes.Ldloc_3),
new(OpCodes.Ldarg_0),

// true
new(OpCodes.Ldc_I4_1),
Expand All @@ -70,11 +70,13 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
// if (!ev.IsAllowed)
// continue;
new(OpCodes.Callvirt, PropertyGetter(typeof(InteractingElevatorEventArgs), nameof(InteractingElevatorEventArgs.IsAllowed))),
new(OpCodes.Brfalse_S, @break),
new(OpCodes.Brfalse_S, retLabel),
});

for (int z = 0; z < newInstructions.Count; z++)
yield return newInstructions[z];
newInstructions[newInstructions.Count - 1].labels.Add(retLabel);

foreach (CodeInstruction instruction in newInstructions)
yield return instruction;

ListPool<CodeInstruction>.Pool.Return(newInstructions);
}
Expand Down
2 changes: 1 addition & 1 deletion Exiled.Events/Patches/Events/Player/Joined.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal static void CallEvent(ReferenceHub hub, out Player player)
#endif
Player.UnverifiedPlayers.Add(hub.gameObject, player);

if (ReferenceHub.HostHub == null)
if (ReferenceHub._hostHub == null)
{
Server.Host = player;
}
Expand Down
46 changes: 37 additions & 9 deletions Exiled.Events/Patches/Generic/LiftList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,53 @@

namespace Exiled.Events.Patches.Generic
{
using API.Features;
#pragma warning disable SA1313
#pragma warning disable SA1402
using System.Collections.Generic;
using System.Reflection.Emit;

using API.Features;
using Exiled.API.Features.Core.Generic.Pools;
using HarmonyLib;
using Interactables.Interobjects;

using static HarmonyLib.AccessTools;

// TODO: CHECK IF THIS WORKS AS ISTENDED.

/// <summary>
/// Patches <see cref="ElevatorManager.SpawnAllChambers"/>.
/// Patches <see cref="ElevatorManager.SpawnChamber"/> to register all ElevatorChambers inside the Lift wrapper.
/// </summary>
[HarmonyPatch(typeof(ElevatorManager), nameof(ElevatorManager.SpawnAllChambers))]
internal class LiftList
[HarmonyPatch(typeof(ElevatorManager), nameof(ElevatorManager.SpawnChamber))]
internal static class LiftList
{
private static void Postfix()
[HarmonyTranspiler]
private static IEnumerable<CodeInstruction> OnSpawnChamber(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
Lift.ElevatorChamberToLift.Clear();
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

int index = newInstructions.FindLastIndex(x => x.opcode == OpCodes.Ldarg_1);

foreach (ElevatorChamber lift in ElevatorChamber.AllChambers)
newInstructions.InsertRange(index, new CodeInstruction[]
{
Lift.Get(lift);
}
new(OpCodes.Ldloc_0),
new(OpCodes.Call, Method(typeof(Lift), nameof(Lift.Get), new[] { typeof(ElevatorChamber) })),
});

foreach (CodeInstruction instruction in newInstructions)
yield return instruction;

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

/// <summary>
/// Patches <see cref="ElevatorChamber.OnDestroy"/> to remove the destroyed ElevatorChambers from the Lift wrapper list.
/// </summary>
[HarmonyPatch(typeof(ElevatorChamber), nameof(ElevatorChamber.OnDestroy))]
internal static class LiftListRemove
{
[HarmonyPrefix]
private static void OnDestroy(ElevatorChamber __instance) => Lift.ElevatorChamberToLift.Remove(__instance);
}
}

0 comments on commit 0475412

Please sign in to comment.