Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions EXILED/Exiled.Events/Patches/Events/Player/PickingUpAmmo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,20 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
// Handlers.Player.OnPickingUpItem(ev)
new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnPickingUpItem))),

// if (ev.IsAllowed)
// goto continueLabel;
// if (!ev.IsAllowed)
// TargetPickup.ServerHandleAbort(hub);
// return;
new(OpCodes.Callvirt, PropertyGetter(typeof(PickingUpItemEventArgs), nameof(PickingUpItemEventArgs.IsAllowed))),
new(OpCodes.Brtrue_S, continueLabel),

// PickupSyncInfo info = this.TargetPickup.Info;
new(OpCodes.Ldarg_0),
new(OpCodes.Ldfld, Field(typeof(AmmoSearchCompletor), nameof(AmmoSearchCompletor.TargetPickup))),
new(OpCodes.Ldfld, Field(typeof(ItemPickupBase), nameof(ItemPickupBase.Info))),
new(OpCodes.Stloc_S, 4),

// info.InUse = false;
new(OpCodes.Ldloca_S, 4),
new(OpCodes.Ldc_I4_0),
new(OpCodes.Call, PropertySetter(typeof(PickupSyncInfo), nameof(PickupSyncInfo.InUse))),

// this.TargetPickup.NetworkInfo = info;
// this.Hub
new(OpCodes.Ldarg_0),
new(OpCodes.Ldfld, Field(typeof(AmmoSearchCompletor), nameof(AmmoSearchCompletor.TargetPickup))),
new(OpCodes.Ldloc_S, 4),
new(OpCodes.Call, PropertySetter(typeof(ItemPickupBase), nameof(ItemPickupBase.NetworkInfo))),
new(OpCodes.Callvirt, PropertyGetter(typeof(AmmoSearchCompletor), nameof(AmmoSearchCompletor.Hub))),

// return;
new(OpCodes.Callvirt, Method(typeof(ItemPickupBase), nameof(ItemPickupBase.ServerHandleAbort))),
new(OpCodes.Ret),
});

Expand Down
25 changes: 19 additions & 6 deletions EXILED/Exiled.Events/Patches/Events/Player/PickingUpArmor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ namespace Exiled.Events.Patches.Events.Player

using API.Features;
using API.Features.Pools;

using Exiled.Events.Attributes;
using Exiled.Events.EventArgs.Player;

using HarmonyLib;

using InventorySystem.Items.Pickups;
using InventorySystem.Searching;

using static HarmonyLib.AccessTools;
Expand All @@ -34,15 +36,17 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

Label returnLabel = generator.DefineLabel();
Label continueLabel = generator.DefineLabel();

int offset = -5;
int index = newInstructions.FindIndex(i => i.opcode == OpCodes.Newobj && (ConstructorInfo)i.operand == GetDeclaredConstructors(typeof(LabApi.Events.Arguments.PlayerEvents.PlayerPickingUpArmorEventArgs))[0]) + offset;

newInstructions[index].labels.Add(continueLabel);

newInstructions.InsertRange(
index,
new CodeInstruction[]
{
[

// this.Hub
new(OpCodes.Ldarg_0),
new(OpCodes.Callvirt, PropertyGetter(typeof(ArmorSearchCompletor), nameof(ArmorSearchCompletor.Hub))),
Expand All @@ -62,12 +66,21 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnPickingUpItem))),

// if (!ev.IsAllowed)
// TargetPickup.ServerHandleAbort(hub);
// return;
new(OpCodes.Callvirt, PropertyGetter(typeof(PickingUpItemEventArgs), nameof(PickingUpItemEventArgs.IsAllowed))),
new(OpCodes.Brfalse, returnLabel),
});
new(OpCodes.Brtrue_S, continueLabel),

new(OpCodes.Ldarg_0),
new(OpCodes.Ldfld, Field(typeof(ArmorSearchCompletor), nameof(ArmorSearchCompletor.TargetPickup))),

// this.Hub
new(OpCodes.Ldarg_0),
new(OpCodes.Callvirt, PropertyGetter(typeof(ArmorSearchCompletor), nameof(ArmorSearchCompletor.Hub))),

newInstructions[newInstructions.Count - 1].WithLabels(returnLabel);
new(OpCodes.Callvirt, Method(typeof(ItemPickupBase), nameof(ItemPickupBase.ServerHandleAbort))),
new(OpCodes.Ret),
]);

for (int z = 0; z < newInstructions.Count; z++)
yield return newInstructions[z];
Expand Down
31 changes: 7 additions & 24 deletions EXILED/Exiled.Events/Patches/Events/Player/PickingUpItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,8 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

Label retLabel = generator.DefineLabel();
Label continueLabel = generator.DefineLabel();

LocalBuilder info = generator.DeclareLocal(typeof(PickupSyncInfo));
LocalBuilder ev = generator.DeclareLocal(typeof(PickingUpItemEventArgs));

int offset = -4;
int index = newInstructions.FindIndex(i => i.opcode == OpCodes.Newobj && (ConstructorInfo)i.operand == GetDeclaredConstructors(typeof(LabApi.Events.Arguments.PlayerEvents.PlayerPickingUpItemEventArgs))[0]) + offset;

Expand All @@ -64,40 +60,27 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
// PickingUpItemEventArgs ev = new(ReferenceHub, ItemPickupBase, bool)
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(PickingUpItemEventArgs))[0]),
new(OpCodes.Dup),
new(OpCodes.Stloc_S, ev.LocalIndex),

// Handlers.Player.OnPickingUpItem(ev)
new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnPickingUpItem))),

// if (ev.IsAllowed)
// goto continueLabel;
new(OpCodes.Ldloc_S, ev.LocalIndex),
// if (!ev.IsAllowed)
// TargetPickup.ServerHandleAbort(hub);
// return;
new(OpCodes.Callvirt, PropertyGetter(typeof(PickingUpItemEventArgs), nameof(PickingUpItemEventArgs.IsAllowed))),
new(OpCodes.Brtrue_S, continueLabel),

// PickupSyncInfo info = this.TargetPickup.Info;
new(OpCodes.Ldarg_0),
new(OpCodes.Ldfld, Field(typeof(ItemSearchCompletor), nameof(ItemSearchCompletor.TargetPickup))),
new(OpCodes.Ldfld, Field(typeof(ItemPickupBase), nameof(ItemPickupBase.Info))),
new(OpCodes.Stloc_S, info.LocalIndex),

// info.InUse = false;
new(OpCodes.Ldloca_S, info.LocalIndex),
new(OpCodes.Ldc_I4_0),
new(OpCodes.Callvirt, PropertySetter(typeof(PickupSyncInfo), nameof(PickupSyncInfo.InUse))),

// this.TargetPickup.NetworkInfo = info
// this.Hub
new(OpCodes.Ldarg_0),
new(OpCodes.Ldfld, Field(typeof(ItemSearchCompletor), nameof(ItemSearchCompletor.TargetPickup))),
new(OpCodes.Ldloc_S, info.LocalIndex),
new(OpCodes.Callvirt, PropertySetter(typeof(ItemPickupBase), nameof(ItemPickupBase.NetworkInfo))),
new(OpCodes.Callvirt, PropertyGetter(typeof(ItemSearchCompletor), nameof(ItemSearchCompletor.Hub))),

// return
new(OpCodes.Br_S, retLabel),
new(OpCodes.Callvirt, Method(typeof(ItemPickupBase), nameof(ItemPickupBase.ServerHandleAbort))),
new(OpCodes.Ret),
});

newInstructions[newInstructions.Count - 1].WithLabels(retLabel);

for (int z = 0; z < newInstructions.Count; z++)
yield return newInstructions[z];

Expand Down
24 changes: 19 additions & 5 deletions EXILED/Exiled.Events/Patches/Events/Player/PickingUpScp244.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ namespace Exiled.Events.Patches.Events.Player
using System.Reflection.Emit;

using API.Features.Pools;

using Exiled.Events.Attributes;
using Exiled.Events.EventArgs.Player;

using HarmonyLib;

using InventorySystem.Items.Pickups;
using InventorySystem.Searching;

using static HarmonyLib.AccessTools;
Expand All @@ -33,17 +35,20 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

Label returnLabel = generator.DefineLabel();
Label continueLabel = generator.DefineLabel();

int offset = -4;
int index = newInstructions.FindIndex(i => i.opcode == OpCodes.Newobj && (ConstructorInfo)i.operand == GetDeclaredConstructors(typeof(LabApi.Events.Arguments.PlayerEvents.PlayerPickingUpItemEventArgs))[0]) + offset;

List<Label> logicLabels = newInstructions[index].ExtractLabels();
newInstructions[index].WithLabels(continueLabel);

newInstructions.InsertRange(
index,
new[]
{
// this.Hub
new CodeInstruction(OpCodes.Ldarg_0).MoveLabelsFrom(newInstructions[index]),
new CodeInstruction(OpCodes.Ldarg_0).WithLabels(logicLabels),
new(OpCodes.Callvirt, PropertyGetter(typeof(Scp244SearchCompletor), nameof(Scp244SearchCompletor.Hub))),

// scp244DeployablePickup
Expand All @@ -60,12 +65,21 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnPickingUpItem))),

// if (!ev.IsAllowed)
// TargetPickup.ServerHandleAbort(hub);
// return;
new(OpCodes.Callvirt, PropertyGetter(typeof(PickingUpItemEventArgs), nameof(PickingUpItemEventArgs.IsAllowed))),
new(OpCodes.Brfalse_S, returnLabel),
});
new(OpCodes.Brtrue_S, continueLabel),

new(OpCodes.Ldarg_0),
new(OpCodes.Ldfld, Field(typeof(Scp244SearchCompletor), nameof(Scp244SearchCompletor.TargetPickup))),

newInstructions[newInstructions.Count - 1].WithLabels(returnLabel);
// this.Hub
new(OpCodes.Ldarg_0),
new(OpCodes.Callvirt, PropertyGetter(typeof(Scp244SearchCompletor), nameof(Scp244SearchCompletor.Hub))),

new(OpCodes.Callvirt, Method(typeof(ItemPickupBase), nameof(ItemPickupBase.ServerHandleAbort))),
new(OpCodes.Ret),
});

for (int z = 0; z < newInstructions.Count; z++)
yield return newInstructions[z];
Expand Down
Loading