From 428e51a8a6f2c1f5cbeb5bf63eb67c6b6ddfa853 Mon Sep 17 00:00:00 2001 From: Banalny-Banan Date: Thu, 26 Dec 2024 20:00:16 +0200 Subject: [PATCH] Rewrote ChangingAttachments transpiler --- .../Item/ChangingAttachmentsEventArgs.cs | 31 +++----- .../Events/Item/ChangingAttachments.cs | 74 +++++++------------ 2 files changed, 36 insertions(+), 69 deletions(-) diff --git a/EXILED/Exiled.Events/EventArgs/Item/ChangingAttachmentsEventArgs.cs b/EXILED/Exiled.Events/EventArgs/Item/ChangingAttachmentsEventArgs.cs index c0d621a29..6f4dc2ef6 100644 --- a/EXILED/Exiled.Events/EventArgs/Item/ChangingAttachmentsEventArgs.cs +++ b/EXILED/Exiled.Events/EventArgs/Item/ChangingAttachmentsEventArgs.cs @@ -28,31 +28,20 @@ public class ChangingAttachmentsEventArgs : IPlayerEvent, IDeniableEvent, IFirea /// /// Initializes a new instance of the class. /// - /// - /// + /// + /// The request received from client. /// - /// - /// - /// - /// The attachments code. - /// - /// - /// - public ChangingAttachmentsEventArgs(Player player, InventorySystem.Items.Firearms.Firearm firearm, uint code, bool isAllowed = true) + public ChangingAttachmentsEventArgs(AttachmentsChangeRequest request) { - Player = player; - Firearm = Item.Get(firearm); - CurrentAttachmentIdentifiers = Firearm.AttachmentIdentifiers; - NewAttachmentIdentifiers = Firearm.FirearmType.GetAttachmentIdentifiers(code).ToList(); - CurrentCode = firearm.GetCurrentAttachmentsCode(); - NewCode = code; - IsAllowed = isAllowed; + Firearm = Item.Get(request.WeaponSerial); + Player = Firearm.Owner; + NewAttachmentIdentifiers = Firearm.FirearmType.GetAttachmentIdentifiers(request.AttachmentsCode).ToList(); } /// /// Gets the old . /// - public IEnumerable CurrentAttachmentIdentifiers { get; } + public IEnumerable CurrentAttachmentIdentifiers => Firearm.AttachmentIdentifiers; /// /// Gets or sets the new . @@ -62,17 +51,17 @@ public ChangingAttachmentsEventArgs(Player player, InventorySystem.Items.Firearm /// /// Gets the code. /// - public uint CurrentCode { get; } + public uint CurrentCode => Firearm.Base.GetCurrentAttachmentsCode(); /// /// Gets the code. /// - public uint NewCode { get; } + public uint NewCode => NewAttachmentIdentifiers.GetAttachmentsCode(); /// /// Gets or sets a value indicating whether the attachments can be changed. /// - public bool IsAllowed { get; set; } + public bool IsAllowed { get; set; } = true; /// /// Gets the which is being modified. diff --git a/EXILED/Exiled.Events/Patches/Events/Item/ChangingAttachments.cs b/EXILED/Exiled.Events/Patches/Events/Item/ChangingAttachments.cs index c820ab438..e4069148f 100644 --- a/EXILED/Exiled.Events/Patches/Events/Item/ChangingAttachments.cs +++ b/EXILED/Exiled.Events/Patches/Events/Item/ChangingAttachments.cs @@ -38,72 +38,50 @@ private static IEnumerable Transpiler(IEnumerable newInstructions = ListPool.Pool.Get(instructions); - const int offset = -3; - int index = newInstructions.FindLastIndex(instruction => instruction.opcode == OpCodes.Ldc_I4_1) + offset; - LocalBuilder ev = generator.DeclareLocal(typeof(ChangingAttachmentsEventArgs)); - LocalBuilder curCode = generator.DeclareLocal(typeof(uint)); - Label ret = generator.DefineLabel(); + Label returnLabel = generator.DefineLabel(); + + /* + [] <= Here, right after msg validation. + IL_0039: ldloc.1 // curInstance + IL_003a: ldarg.1 // msg + IL_003b: ldfld unsigned int32 InventorySystem.Items.Firearms.Attachments.AttachmentsChangeRequest::AttachmentsCode + IL_0040: ldc.i4.1 + IL_0041: call void InventorySystem.Items.Firearms.Attachments.AttachmentsUtils::ApplyAttachmentsCode(class InventorySystem.Items.Firearms.Firearm, unsigned int32, bool) + */ + int index = newInstructions.FindIndex(i => i.IsLdarg(1)) - 1; + List