From a8954120852ef344cbf8c767961392140eefba66 Mon Sep 17 00:00:00 2001 From: IRacle Date: Thu, 26 Dec 2024 20:40:51 +0300 Subject: [PATCH] unloading + refactor --- .../Player/ReloadingWeaponEventArgs.cs | 8 +-- .../Player/UnloadingWeaponEventArgs.cs | 13 +--- .../Patches/Events/Player/ReloadingWeapon.cs | 9 ++- .../Patches/Events/Player/UnloadingWeapon.cs | 66 +++++++++++++++++++ 4 files changed, 75 insertions(+), 21 deletions(-) create mode 100644 EXILED/Exiled.Events/Patches/Events/Player/UnloadingWeapon.cs diff --git a/EXILED/Exiled.Events/EventArgs/Player/ReloadingWeaponEventArgs.cs b/EXILED/Exiled.Events/EventArgs/Player/ReloadingWeaponEventArgs.cs index 5bf1cd62d..f1233e39e 100644 --- a/EXILED/Exiled.Events/EventArgs/Player/ReloadingWeaponEventArgs.cs +++ b/EXILED/Exiled.Events/EventArgs/Player/ReloadingWeaponEventArgs.cs @@ -23,20 +23,16 @@ public class ReloadingWeaponEventArgs : IPlayerEvent, IFirearmEvent, IDeniableEv /// /// /// - /// - /// - /// - public ReloadingWeaponEventArgs(InventorySystem.Items.Firearms.Firearm firearm, bool isAllowed = true) + public ReloadingWeaponEventArgs(InventorySystem.Items.Firearms.Firearm firearm) { Firearm = Item.Get(firearm); Player = Firearm.Owner; - IsAllowed = isAllowed; } /// /// Gets or sets a value indicating whether the weapon can be reloaded. /// - public bool IsAllowed { get; set; } + public bool IsAllowed { get; set; } = false; /// /// Gets the being reloaded. diff --git a/EXILED/Exiled.Events/EventArgs/Player/UnloadingWeaponEventArgs.cs b/EXILED/Exiled.Events/EventArgs/Player/UnloadingWeaponEventArgs.cs index a5c8df5f0..a7f8b6326 100644 --- a/EXILED/Exiled.Events/EventArgs/Player/UnloadingWeaponEventArgs.cs +++ b/EXILED/Exiled.Events/EventArgs/Player/UnloadingWeaponEventArgs.cs @@ -20,26 +20,19 @@ public class UnloadingWeaponEventArgs : IPlayerEvent, IFirearmEvent, IDeniableEv /// /// Initializes a new instance of the class. /// - /// - /// - /// /// /// /// - /// - /// - /// - public UnloadingWeaponEventArgs(Player player, Firearm firearm, bool isAllowed = true) + public UnloadingWeaponEventArgs(Firearm firearm) { Firearm = firearm; - Player = player; - IsAllowed = isAllowed; + Player = firearm.Owner; } /// /// Gets or sets a value indicating whether the weapon can be unloaded. /// - public bool IsAllowed { get; set; } + public bool IsAllowed { get; set; } = true; /// /// Gets the being unloaded. diff --git a/EXILED/Exiled.Events/Patches/Events/Player/ReloadingWeapon.cs b/EXILED/Exiled.Events/Patches/Events/Player/ReloadingWeapon.cs index 8dc7bc6e7..fea606842 100644 --- a/EXILED/Exiled.Events/Patches/Events/Player/ReloadingWeapon.cs +++ b/EXILED/Exiled.Events/Patches/Events/Player/ReloadingWeapon.cs @@ -11,6 +11,8 @@ namespace Exiled.Events.Patches.Events.Player using System.Reflection.Emit; using API.Features.Pools; + + using Exiled.API.Extensions; using Exiled.Events.Attributes; using Exiled.Events.EventArgs.Player; using HarmonyLib; @@ -31,7 +33,7 @@ private static IEnumerable Transpiler(IEnumerable newInstructions = ListPool.Pool.Get(instructions); int offset = 2; - int index = newInstructions.FindIndex(x => x.operand == (object)Method(typeof(IReloadUnloadValidatorModule), nameof(IReloadUnloadValidatorModule.ValidateReload))) + offset; + int index = newInstructions.FindIndex(x => x.Calls(Method(typeof(IReloadUnloadValidatorModule), nameof(IReloadUnloadValidatorModule.ValidateReload)))) + offset; Label skip = (Label)newInstructions[index - 1].operand; newInstructions.InsertRange( @@ -42,10 +44,7 @@ private static IEnumerable Transpiler(IEnumerable +// 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 API.Features.Pools; + + using Exiled.API.Extensions; + using Exiled.Events.Attributes; + using Exiled.Events.EventArgs.Player; + using HarmonyLib; + using InventorySystem.Items.Firearms.Modules; + + using static HarmonyLib.AccessTools; + + /// + /// Patches . + /// Adds the event. + /// + [EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.UnloadingWeapon))] + [HarmonyPatch(typeof(AnimatorReloaderModuleBase), nameof(AnimatorReloaderModuleBase.ServerProcessCmd))] + internal static class UnloadingWeapon + { + private static IEnumerable Transpiler(IEnumerable instructions, ILGenerator generator) + { + List newInstructions = ListPool.Pool.Get(instructions); + + int offset = 2; + int index = newInstructions.FindIndex(x => x.Calls(Method(typeof(IReloadUnloadValidatorModule), nameof(IReloadUnloadValidatorModule.ValidateUnload)))) + offset; + + Label skip = (Label)newInstructions[index - 1].operand; + newInstructions.InsertRange( + index, + new[] + { + // player + new CodeInstruction(OpCodes.Ldarg_0), + new(OpCodes.Callvirt, PropertyGetter(typeof(AnimatorReloaderModuleBase), nameof(AnimatorReloaderModuleBase.Firearm))), + + // UnloadingWeaponEventArgs ev = new(firearm) + new(OpCodes.Newobj, GetDeclaredConstructors(typeof(UnloadingWeaponEventArgs))[0]), + new(OpCodes.Dup), + + // Player.OnUnloadingWeapon(ev) + new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnUnloadingWeapon))), + + // if (!ev.IsAllowed) + // goto skip; + new(OpCodes.Callvirt, PropertyGetter(typeof(UnloadingWeaponEventArgs), nameof(UnloadingWeaponEventArgs.IsAllowed))), + new(OpCodes.Brfalse, skip), + }); + + for (int z = 0; z < newInstructions.Count; z++) + yield return newInstructions[z]; + + ListPool.Pool.Return(newInstructions); + } + } +}