diff --git a/Exiled.Events/EventArgs/Player/AddingItemEventArgs.cs b/Exiled.Events/EventArgs/Player/AddingItemEventArgs.cs index 6fa3c24b28..5e0db7400d 100644 --- a/Exiled.Events/EventArgs/Player/AddingItemEventArgs.cs +++ b/Exiled.Events/EventArgs/Player/AddingItemEventArgs.cs @@ -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; /// /// Contains all information before adding an item to a player's inventory. @@ -23,12 +24,14 @@ public class AddingItemEventArgs : IPlayerEvent, IDeniableEvent, IItemEvent, IPi /// /// /// + /// /// - 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; } @@ -45,6 +48,11 @@ public AddingItemEventArgs(Player player, Item item, Pickup pickup, bool isAllow /// public Pickup Pickup { get; } + /// + /// Gets or sets the reason of why the item was added to the player inventory. + /// + public ItemAddReason AddReason { get; set; } + /// public bool IsAllowed { get; set; } } diff --git a/Exiled.Events/Patches/Events/Player/AddingItem.cs b/Exiled.Events/Patches/Events/Player/AddingItem.cs index f6b1914322..06e6ca23d1 100644 --- a/Exiled.Events/Patches/Events/Player/AddingItem.cs +++ b/Exiled.Events/Patches/Events/Player/AddingItem.cs @@ -25,7 +25,7 @@ namespace Exiled.Events.Patches.Events.Player using static HarmonyLib.AccessTools; /// - /// Patches . + /// Patches . /// Adds the event. /// [EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.AddingItem))] @@ -40,28 +40,34 @@ private static IEnumerable Transpiler(IEnumerable 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.Pool.Return(newInstructions); } diff --git a/Exiled.Events/Patches/Events/Player/EnteringKillerCollision.cs b/Exiled.Events/Patches/Events/Player/EnteringKillerCollision.cs index 842e3890cd..854f33e0b8 100644 --- a/Exiled.Events/Patches/Events/Player/EnteringKillerCollision.cs +++ b/Exiled.Events/Patches/Events/Player/EnteringKillerCollision.cs @@ -19,11 +19,11 @@ namespace Exiled.Events.Patches.Events.Player using static HarmonyLib.AccessTools; /// - /// Patches . + /// Patches . /// Adds the event. /// [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 Transpiler(IEnumerable instructions, ILGenerator generator) @@ -32,8 +32,8 @@ private static IEnumerable Transpiler(IEnumerable instruction.opcode == OpCodes.Newobj) + offset; + const int offset = -1; + int index = newInstructions.FindLastIndex(instruction => instruction.opcode == OpCodes.Ldfld) + offset; newInstructions.InsertRange( index, @@ -61,8 +61,8 @@ private static IEnumerable Transpiler(IEnumerable.Pool.Return(newInstructions); } diff --git a/Exiled.Events/Patches/Events/Player/InteractingElevator.cs b/Exiled.Events/Patches/Events/Player/InteractingElevator.cs index 801cdfbad7..9c8f3d4748 100644 --- a/Exiled.Events/Patches/Events/Player/InteractingElevator.cs +++ b/Exiled.Events/Patches/Events/Player/InteractingElevator.cs @@ -24,21 +24,21 @@ namespace Exiled.Events.Patches.Events.Player using static HarmonyLib.AccessTools; /// - /// Patches . + /// Patches . /// Adds the event. /// [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 Transpiler(IEnumerable instructions, ILGenerator generator) { List newInstructions = ListPool.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); // @@ -51,11 +51,11 @@ private static IEnumerable Transpiler(IEnumerable Transpiler(IEnumerable.Pool.Return(newInstructions); } diff --git a/Exiled.Events/Patches/Events/Player/Joined.cs b/Exiled.Events/Patches/Events/Player/Joined.cs index d152e5c9ab..115df866dd 100644 --- a/Exiled.Events/Patches/Events/Player/Joined.cs +++ b/Exiled.Events/Patches/Events/Player/Joined.cs @@ -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; } diff --git a/Exiled.Events/Patches/Generic/LiftList.cs b/Exiled.Events/Patches/Generic/LiftList.cs index e4c37302ed..b23bfd6455 100644 --- a/Exiled.Events/Patches/Generic/LiftList.cs +++ b/Exiled.Events/Patches/Generic/LiftList.cs @@ -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. + /// - /// Patches . + /// Patches to register all ElevatorChambers inside the Lift wrapper. /// - [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 OnSpawnChamber(IEnumerable instructions, ILGenerator generator) { - Lift.ElevatorChamberToLift.Clear(); + List newInstructions = ListPool.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.Pool.Return(newInstructions); } } + + /// + /// Patches to remove the destroyed ElevatorChambers from the Lift wrapper list. + /// + [HarmonyPatch(typeof(ElevatorChamber), nameof(ElevatorChamber.OnDestroy))] + internal static class LiftListRemove + { + [HarmonyPrefix] + private static void OnDestroy(ElevatorChamber __instance) => Lift.ElevatorChamberToLift.Remove(__instance); + } } \ No newline at end of file