From 47fa0f06ad50b2821dff7b20c2b1977cb91d5e63 Mon Sep 17 00:00:00 2001 From: xNexusACS <83370388+xNexusACS@users.noreply.github.com> Date: Fri, 6 Dec 2024 06:31:45 +0100 Subject: [PATCH] Fix TogglingWeaponFlashlight & AimingDownSight events --- .../Player/AimingDownSightEventArgs.cs | 17 +-- .../TogglingWeaponFlashlightEventArgs.cs | 4 +- .../Player/FirearmActionEvents/Aiming.cs | 71 ++++++++++++ .../ToggleWeaponFlashlight.cs | 107 ++++++++++++++++++ .../Events/Player/FirearmRequestReceived.cs | 2 + 5 files changed, 188 insertions(+), 13 deletions(-) create mode 100644 Exiled.Events/Patches/Events/Player/FirearmActionEvents/Aiming.cs create mode 100644 Exiled.Events/Patches/Events/Player/FirearmActionEvents/ToggleWeaponFlashlight.cs diff --git a/Exiled.Events/EventArgs/Player/AimingDownSightEventArgs.cs b/Exiled.Events/EventArgs/Player/AimingDownSightEventArgs.cs index 9370ba42b5..5ca76161cf 100644 --- a/Exiled.Events/EventArgs/Player/AimingDownSightEventArgs.cs +++ b/Exiled.Events/EventArgs/Player/AimingDownSightEventArgs.cs @@ -26,25 +26,20 @@ public class AimingDownSightEventArgs : IPlayerEvent, IFirearmEvent /// /// /// - /// - /// + /// + /// /// - public AimingDownSightEventArgs(Player player, Firearm firearm, bool adsIn) + public AimingDownSightEventArgs(Player player, Firearm firearm, bool aiming) { Firearm = firearm; Player = player; - AdsIn = adsIn; + Aiming = aiming; } /// - /// Gets a value indicating whether or not the player is aiming down sight in. + /// Gets a value indicating whether the player starts aiming or stops aiming. /// - public bool AdsIn { get; } - - /// - /// Gets a value indicating whether or not the player is aiming down sight out. - /// - public bool AdsOut => !AdsIn; + public bool Aiming { get; } /// /// Gets the used to trigger the aim action. diff --git a/Exiled.Events/EventArgs/Player/TogglingWeaponFlashlightEventArgs.cs b/Exiled.Events/EventArgs/Player/TogglingWeaponFlashlightEventArgs.cs index a49e64a653..5b0e2cbf7d 100644 --- a/Exiled.Events/EventArgs/Player/TogglingWeaponFlashlightEventArgs.cs +++ b/Exiled.Events/EventArgs/Player/TogglingWeaponFlashlightEventArgs.cs @@ -41,9 +41,9 @@ public TogglingWeaponFlashlightEventArgs(Player player, Firearm firearm, bool ne } /// - /// Gets a value indicating whether the new weapon's flashlight state will be enabled. + /// Gets or sets a value indicating whether the weapon's flashlight will turn on or off. /// - public bool NewState { get; } + public bool NewState { get; set; } /// /// Gets or sets a value indicating whether or not the weapon's flashlight can be toggled. diff --git a/Exiled.Events/Patches/Events/Player/FirearmActionEvents/Aiming.cs b/Exiled.Events/Patches/Events/Player/FirearmActionEvents/Aiming.cs new file mode 100644 index 0000000000..d2954c2b54 --- /dev/null +++ b/Exiled.Events/Patches/Events/Player/FirearmActionEvents/Aiming.cs @@ -0,0 +1,71 @@ +// ----------------------------------------------------------------------- +// +// Copyright (c) Exiled Team. All rights reserved. +// Licensed under the CC BY-SA 3.0 license. +// +// ----------------------------------------------------------------------- + +namespace Exiled.Events.Patches.Events.Player.FirearmActionEvents +{ + using System.Collections.Generic; + using System.Reflection.Emit; + + using Exiled.API.Features.Core.Generic.Pools; + using Exiled.API.Features.Items; + using Exiled.Events.Attributes; + using Exiled.Events.EventArgs.Player; + using HarmonyLib; + using InventorySystem.Items.Firearms.Modules; + + using static HarmonyLib.AccessTools; + + /// + /// Patches . + /// Adds event. + /// + [EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.AimingDownSight))] + [HarmonyPatch(typeof(LinearAdsModule), nameof(LinearAdsModule.ServerProcessCmd))] + internal static class Aiming + { + [HarmonyTranspiler] + private static IEnumerable OnAimStatusChanged(IEnumerable instructions, ILGenerator generator) + { + List newInstructions = ListPool.Pool.Get(instructions); + + Label returnLabel = generator.DefineLabel(); + + int index = newInstructions.FindIndex(instruction => instruction.opcode == OpCodes.Ret); + + newInstructions.InsertRange(index, new[] + { + // Player.Get(this.Firearm.Owner) + new CodeInstruction(OpCodes.Ldarg_0), + new(OpCodes.Callvirt, PropertyGetter(typeof(LinearAdsModule), nameof(LinearAdsModule.Firearm))), + new(OpCodes.Callvirt, PropertyGetter(typeof(InventorySystem.Items.Firearms.Firearm), nameof(InventorySystem.Items.Firearms.Firearm.Owner))), + new(OpCodes.Call, Method(typeof(API.Features.Player), nameof(API.Features.Player.Get), new[] { typeof(ReferenceHub) })), + + // (Firearm)Item.Get(this.Firearm) + new(OpCodes.Ldarg_0), + new(OpCodes.Callvirt, PropertyGetter(typeof(LinearAdsModule), nameof(LinearAdsModule.Firearm))), + new(OpCodes.Call, Method(typeof(Item), nameof(Item.Get), new[] { typeof(InventorySystem.Items.Firearms.Firearm) })), + new(OpCodes.Castclass, typeof(Firearm)), + + // this._userInput + new(OpCodes.Ldfld, Field(typeof(LinearAdsModule), nameof(LinearAdsModule._userInput))), + + // AimingDownSightEventArgs args = new(Player, Firearm, bool) + new(OpCodes.Newobj, GetDeclaredConstructors(typeof(AimingDownSightEventArgs))[0]), + + // Player.OnAimingDownSight(args) + new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnAimingDownSight))), + }); + + newInstructions[newInstructions.Count - 1].labels.Add(returnLabel); + + foreach (CodeInstruction instruction in newInstructions) + yield return instruction; + + ListPool.Pool.Return(newInstructions); + } + } +} \ No newline at end of file diff --git a/Exiled.Events/Patches/Events/Player/FirearmActionEvents/ToggleWeaponFlashlight.cs b/Exiled.Events/Patches/Events/Player/FirearmActionEvents/ToggleWeaponFlashlight.cs new file mode 100644 index 0000000000..e943ce967e --- /dev/null +++ b/Exiled.Events/Patches/Events/Player/FirearmActionEvents/ToggleWeaponFlashlight.cs @@ -0,0 +1,107 @@ +// ----------------------------------------------------------------------- +// +// Copyright (c) Exiled Team. All rights reserved. +// Licensed under the CC BY-SA 3.0 license. +// +// ----------------------------------------------------------------------- + +namespace Exiled.Events.Patches.Events.Player.FirearmActionEvents +{ + using System.Collections.Generic; + using System.Reflection.Emit; + + using Exiled.API.Features.Core.Generic.Pools; + using Exiled.API.Features.Items; + using Exiled.Events.Attributes; + using Exiled.Events.EventArgs.Player; + using HarmonyLib; + using InventorySystem.Items.Firearms.Attachments; + + using static HarmonyLib.AccessTools; + + /// + /// Patches . + /// Adds & event. + /// + [EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.TogglingWeaponFlashlight))] + [HarmonyPatch(typeof(FlashlightAttachment), nameof(FlashlightAttachment.ServerSendStatus))] + internal static class ToggleWeaponFlashlight + { + [HarmonyTranspiler] + private static IEnumerable OnToggleWeaponFlashlight(IEnumerable instructions, ILGenerator generator) + { + List newInstructions = ListPool.Pool.Get(instructions); + + Label returnLabel = generator.DefineLabel(); + LocalBuilder ev = generator.DeclareLocal(typeof(TogglingWeaponFlashlightEventArgs)); + LocalBuilder firearm = generator.DeclareLocal(typeof(Firearm)); + LocalBuilder player = generator.DeclareLocal(typeof(API.Features.Player)); + LocalBuilder status = generator.DeclareLocal(typeof(bool)); + + newInstructions.InsertRange(0, new CodeInstruction[] + { + // Player.Get(this.Firearm.Owner) + new(OpCodes.Ldarg_0), + new(OpCodes.Callvirt, PropertyGetter(typeof(FlashlightAttachment), nameof(FlashlightAttachment.Firearm))), + new(OpCodes.Callvirt, PropertyGetter(typeof(InventorySystem.Items.Firearms.Firearm), nameof(InventorySystem.Items.Firearms.Firearm.Owner))), + new(OpCodes.Call, Method(typeof(API.Features.Player), nameof(API.Features.Player.Get), new[] { typeof(ReferenceHub) })), + new(OpCodes.Stloc_S, player.LocalIndex), + + // (Firearm)Item.Get(this.Firearm) + new(OpCodes.Ldarg_0), + new(OpCodes.Callvirt, PropertyGetter(typeof(FlashlightAttachment), nameof(FlashlightAttachment.Firearm))), + new(OpCodes.Call, Method(typeof(Item), nameof(Item.Get), new[] { typeof(InventorySystem.Items.Firearms.Firearm) })), + new(OpCodes.Castclass, typeof(Firearm)), + new(OpCodes.Stloc_S, firearm.LocalIndex), + + // status + new(OpCodes.Ldarg_1), + + // true + new(OpCodes.Ldc_I4_1), + + // TogglingWeaponFlashlightEventArgs args = new(Player, Firearm, bool, bool) + new(OpCodes.Newobj, GetDeclaredConstructors(typeof(TogglingWeaponFlashlightEventArgs))[0]), + new(OpCodes.Dup), + new(OpCodes.Dup), + new(OpCodes.Stloc_S, ev.LocalIndex), + + new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnTogglingWeaponFlashlight))), + + // if (!args.IsAllowed) + // return; + new(OpCodes.Callvirt, PropertyGetter(typeof(TogglingWeaponFlashlightEventArgs), nameof(TogglingWeaponFlashlightEventArgs.IsAllowed))), + new(OpCodes.Brfalse_S, returnLabel), + + // status = NewState + new(OpCodes.Ldloc_S, ev.LocalIndex), + new(OpCodes.Callvirt, PropertyGetter(typeof(TogglingWeaponFlashlightEventArgs), nameof(TogglingWeaponFlashlightEventArgs.NewState))), + new(OpCodes.Starg_S, 1), + new(OpCodes.Ldarg_1), + new(OpCodes.Stloc_S, status.LocalIndex), + }); + + newInstructions.InsertRange(newInstructions.Count - 1, new CodeInstruction[] + { + // Player + new(OpCodes.Ldloc_S, player.LocalIndex), + + // Firearm + new(OpCodes.Ldloc_S, firearm.LocalIndex), + + // status + new(OpCodes.Ldloc_S, status.LocalIndex), + + new(OpCodes.Newobj, GetDeclaredConstructors(typeof(ToggledWeaponFlashlightEventArgs))[0]), + new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnToggledWeaponFlashlight))), + }); + + newInstructions[newInstructions.Count - 1].labels.Add(returnLabel); + + foreach (CodeInstruction instruction in newInstructions) + yield return instruction; + + ListPool.Pool.Return(newInstructions); + } + } +} \ No newline at end of file diff --git a/Exiled.Events/Patches/Events/Player/FirearmRequestReceived.cs b/Exiled.Events/Patches/Events/Player/FirearmRequestReceived.cs index 823679eac4..bee9dd37ee 100644 --- a/Exiled.Events/Patches/Events/Player/FirearmRequestReceived.cs +++ b/Exiled.Events/Patches/Events/Player/FirearmRequestReceived.cs @@ -28,6 +28,7 @@ namespace Exiled.Events.Patches.Events.Player using static HarmonyLib.AccessTools; + /* /// /// Patches . /// Adds , , @@ -329,4 +330,5 @@ private static IEnumerable Transpiler(IEnumerable.Pool.Return(newInstructions); } } + */ } \ No newline at end of file