diff --git a/EXILED/Exiled.Events/EventArgs/Interfaces/IMicroHIDEvent.cs b/EXILED/Exiled.Events/EventArgs/Interfaces/IMicroHIDEvent.cs new file mode 100644 index 0000000000..bc17988cc2 --- /dev/null +++ b/EXILED/Exiled.Events/EventArgs/Interfaces/IMicroHIDEvent.cs @@ -0,0 +1,22 @@ +// ----------------------------------------------------------------------- +// +// Copyright (c) ExMod Team. All rights reserved. +// Licensed under the CC BY-SA 3.0 license. +// +// ----------------------------------------------------------------------- + +namespace Exiled.Events.EventArgs.Interfaces +{ + using API.Features.Items; + + /// + /// Event args used for all related events. + /// + public interface IMicroHIDEvent : IItemEvent + { + /// + /// Gets the triggering the event. + /// + public MicroHid MicroHID { get; } + } +} \ No newline at end of file diff --git a/EXILED/Exiled.Events/EventArgs/Player/ChangingMicroHIDStateEventArgs.cs b/EXILED/Exiled.Events/EventArgs/Player/ChangingMicroHIDStateEventArgs.cs index 2881241cdd..3e3c1eee9c 100644 --- a/EXILED/Exiled.Events/EventArgs/Player/ChangingMicroHIDStateEventArgs.cs +++ b/EXILED/Exiled.Events/EventArgs/Player/ChangingMicroHIDStateEventArgs.cs @@ -20,7 +20,7 @@ namespace Exiled.Events.EventArgs.Player /// /// Contains all information before MicroHID state is changed. /// - public class ChangingMicroHIDStateEventArgs : IDeniableEvent, IItemEvent + public class ChangingMicroHIDStateEventArgs : IDeniableEvent, IMicroHIDEvent { /// /// Initializes a new instance of the class. diff --git a/EXILED/Exiled.Events/EventArgs/Player/ExplodingMicroHIDEventArgs.cs b/EXILED/Exiled.Events/EventArgs/Player/ExplodingMicroHIDEventArgs.cs new file mode 100644 index 0000000000..c2727317aa --- /dev/null +++ b/EXILED/Exiled.Events/EventArgs/Player/ExplodingMicroHIDEventArgs.cs @@ -0,0 +1,51 @@ +// ----------------------------------------------------------------------- +// +// Copyright (c) ExMod Team. All rights reserved. +// Licensed under the CC BY-SA 3.0 license. +// +// ----------------------------------------------------------------------- + +namespace Exiled.Events.EventArgs.Player +{ + using Exiled.API.Features.Items; + using Exiled.Events.EventArgs.Interfaces; + using InventorySystem.Items.MicroHID; + + /// + /// Contains all information before the micro hid explode. + /// + public class ExplodingMicroHIDEventArgs : IDeniableEvent, IMicroHIDEvent + { + /// + /// Initializes a new instance of the class. + /// + /// + /// Whether the Micro HID can explode the player or not. + public ExplodingMicroHIDEventArgs(MicroHIDItem item, bool isAllowed = true) + { + MicroHID = Item.Get(item); + Player = MicroHID.Owner; + IsAllowed = isAllowed; + } + + /// + /// Gets the item. + /// + public Item Item => MicroHID; + + /// + /// Gets the player in owner of the item. + /// + public Exiled.API.Features.Player Player { get; } + + /// + /// Gets MicroHid item. + /// + public MicroHid MicroHID { get; } + + /// + /// Gets or sets a value indicating whether the player can explode the micro HID. + /// + public bool IsAllowed { get; set; } + } +} \ No newline at end of file diff --git a/EXILED/Exiled.Events/EventArgs/Player/MicroHIDOpeningDoorEventArgs.cs b/EXILED/Exiled.Events/EventArgs/Player/MicroHIDOpeningDoorEventArgs.cs index a8bf27b722..36ff69e9b1 100644 --- a/EXILED/Exiled.Events/EventArgs/Player/MicroHIDOpeningDoorEventArgs.cs +++ b/EXILED/Exiled.Events/EventArgs/Player/MicroHIDOpeningDoorEventArgs.cs @@ -17,7 +17,7 @@ namespace Exiled.Events.EventArgs.Player /// /// Contains all information before the micro opens a doors. /// - public class MicroHIDOpeningDoorEventArgs : IDeniableEvent, IDoorEvent, IItemEvent + public class MicroHIDOpeningDoorEventArgs : IDeniableEvent, IDoorEvent, IMicroHIDEvent { /// /// Initializes a new instance of the class. diff --git a/EXILED/Exiled.Events/EventArgs/Player/UsingMicroHIDEnergyEventArgs.cs b/EXILED/Exiled.Events/EventArgs/Player/UsingMicroHIDEnergyEventArgs.cs index cbf292d86f..2cfe7fee35 100644 --- a/EXILED/Exiled.Events/EventArgs/Player/UsingMicroHIDEnergyEventArgs.cs +++ b/EXILED/Exiled.Events/EventArgs/Player/UsingMicroHIDEnergyEventArgs.cs @@ -17,7 +17,7 @@ namespace Exiled.Events.EventArgs.Player /// /// Contains all information before MicroHID energy is changed. /// - public class UsingMicroHIDEnergyEventArgs : IDeniableEvent, IItemEvent + public class UsingMicroHIDEnergyEventArgs : IDeniableEvent, IMicroHIDEvent { /// /// Initializes a new instance of the class. diff --git a/EXILED/Exiled.Events/Handlers/Player.cs b/EXILED/Exiled.Events/Handlers/Player.cs index 940778ef29..60cbd55ff6 100644 --- a/EXILED/Exiled.Events/Handlers/Player.cs +++ b/EXILED/Exiled.Events/Handlers/Player.cs @@ -583,6 +583,11 @@ public class Player /// public static Event ChangingDisruptorMode { get; set; } = new(); + /// + /// Invoked before the player explode with the micro HID. + /// + public static Event ExplodingMicroHID { get; set; } = new(); + /// /// Invoked before the micro HID opens a door. /// @@ -1265,6 +1270,12 @@ public static void OnItemRemoved(ReferenceHub referenceHub, InventorySystem.Item /// The instance. public static void OnChangingDisruptorMode(ChangingDisruptorModeEventArgs ev) => ChangingDisruptorMode.InvokeSafely(ev); + /// + /// Called before disruptor's mode is changed. + /// + /// The instance. + public static void OnExplodingMicroHID(ExplodingMicroHIDEventArgs ev) => ExplodingMicroHID.InvokeSafely(ev); + /// /// Called before the micro HID opens a door. /// diff --git a/EXILED/Exiled.Events/Patches/Events/Player/ExplodingMicroHID.cs b/EXILED/Exiled.Events/Patches/Events/Player/ExplodingMicroHID.cs new file mode 100644 index 0000000000..2fdf38da46 --- /dev/null +++ b/EXILED/Exiled.Events/Patches/Events/Player/ExplodingMicroHID.cs @@ -0,0 +1,70 @@ +// ----------------------------------------------------------------------- +// +// Copyright (c) ExMod Team. All rights reserved. +// Licensed under the CC BY-SA 3.0 license. +// +// ----------------------------------------------------------------------- + +namespace Exiled.Events.Patches.Events.Player +{ + using System.Collections.Generic; + using System.Reflection.Emit; + + using Exiled.API.Features.Pools; + using Exiled.Events.Attributes; + using Exiled.Events.EventArgs.Player; + using HarmonyLib; + using InventorySystem.Items.MicroHID.Modules; + + using static HarmonyLib.AccessTools; + + /// + /// Patches . + /// Adds the event. + /// + [EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.ExplodingMicroHID))] + [HarmonyPatch(typeof(ChargeFireModeModule), nameof(ChargeFireModeModule.ServerExplode))] + internal static class ExplodingMicroHID + { + private static IEnumerable Transpiler(IEnumerable instructions, ILGenerator generator) + { + List newInstructions = ListPool.Pool.Get(instructions); + + int offset = -2; + int index = newInstructions.FindIndex(x => x.StoresField(Field(typeof(ChargeFireModeModule), nameof(ChargeFireModeModule._alreadyExploded)))) + offset; + + Label retLabel = generator.DefineLabel(); + + newInstructions[newInstructions.Count - 1].labels.Add(retLabel); + + newInstructions.InsertRange( + index, + new[] + { + // this.MicroHid; + new CodeInstruction(OpCodes.Ldarg_0).MoveLabelsFrom(newInstructions[index]), + new(OpCodes.Call, PropertyGetter(typeof(ChargeFireModeModule), nameof(ChargeFireModeModule.MicroHid))), + + // true + new(OpCodes.Ldc_I4_1), + + // ExplodingMicroHIDEventArgs ev = new(MicroHIDItem, true); + new(OpCodes.Newobj, GetDeclaredConstructors(typeof(ExplodingMicroHIDEventArgs))[0]), + new(OpCodes.Dup), + + // Handlers.Player.OnExplodingMicroHID(ev); + new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnExplodingMicroHID))), + + // if (!ev.IsAllowed) + // return; + new(OpCodes.Callvirt, PropertyGetter(typeof(ExplodingMicroHIDEventArgs), nameof(ExplodingMicroHIDEventArgs.IsAllowed))), + new(OpCodes.Brfalse_S, retLabel), + }); + + for (int z = 0; z < newInstructions.Count; z++) + yield return newInstructions[z]; + + ListPool.Pool.Return(newInstructions); + } + } +} \ No newline at end of file diff --git a/EXILED/Exiled.Events/Patches/Events/Player/UsingMicroHIDEnergy.cs b/EXILED/Exiled.Events/Patches/Events/Player/UsingMicroHIDEnergy.cs index 15634602db..115ffc8391 100644 --- a/EXILED/Exiled.Events/Patches/Events/Player/UsingMicroHIDEnergy.cs +++ b/EXILED/Exiled.Events/Patches/Events/Player/UsingMicroHIDEnergy.cs @@ -10,14 +10,11 @@ namespace Exiled.Events.Patches.Events.Player using System.Collections.Generic; using System.Reflection.Emit; - using API.Features; using API.Features.Pools; using Exiled.Events.Attributes; using Exiled.Events.EventArgs.Player; using HarmonyLib; - using InventorySystem.Items.MicroHID; using InventorySystem.Items.MicroHID.Modules; - using UnityEngine; using static HarmonyLib.AccessTools;