Skip to content

Commit

Permalink
Merge branch 'dev' into Adding-TapeUsingEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
louis1706 committed Dec 26, 2024
2 parents be2d4b3 + d513ff1 commit 4547fd0
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 29 deletions.
4 changes: 2 additions & 2 deletions EXILED/Exiled.API/Features/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2781,7 +2781,7 @@ public void AddItem(Firearm item, IEnumerable<AttachmentIdentifier> identifiers)
/// <param name="pickup">The <see cref="Pickup"/> of the item to be added.</param>
/// <param name="addReason">The reason the item was added.</param>
/// <returns>The <see cref="Item"/> that was added.</returns>
public Item AddItem(Pickup pickup, ItemAddReason addReason = ItemAddReason.Undefined) => Item.Get(Inventory.ServerAddItem(pickup.Type, addReason, pickup.Serial, pickup.Base));
public Item AddItem(Pickup pickup, ItemAddReason addReason = ItemAddReason.AdminCommand) => Item.Get(Inventory.ServerAddItem(pickup.Type, addReason, pickup.Serial, pickup.Base));

/// <summary>
/// Adds an item to the player's inventory.
Expand All @@ -2791,7 +2791,7 @@ public void AddItem(Firearm item, IEnumerable<AttachmentIdentifier> identifiers)
/// <returns>The <see cref="Item"/> that was added.</returns>
public Item AddItem(FirearmPickup pickup, IEnumerable<AttachmentIdentifier> identifiers)
{
Firearm firearm = Item.Get<Firearm>(Inventory.ServerAddItem(pickup.Type, ItemAddReason.Undefined, pickup.Serial, pickup.Base));
Firearm firearm = Item.Get<Firearm>(Inventory.ServerAddItem(pickup.Type, ItemAddReason.AdminCommand, pickup.Serial, pickup.Base));

if (identifiers is not null)
firearm.AddAttachment(identifiers);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// -----------------------------------------------------------------------
// <copyright file="ChangingMicroHIDPickupStateEventArgs.cs" company="ExMod Team">
// Copyright (c) ExMod Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.Events.EventArgs.Item
{
using Exiled.API.Features.Pickups;

using Interfaces;
using InventorySystem.Items.MicroHID.Modules;
using InventorySystem.Items.Pickups;

/// <summary>
/// Contains all information before MicroHID pickup state is changed.
/// </summary>
public class ChangingMicroHIDPickupStateEventArgs : IDeniableEvent, IPickupEvent
{
/// <summary>
/// Initializes a new instance of the <see cref="ChangingMicroHIDPickupStateEventArgs" /> class.
/// </summary>
/// <param name="microHID">
/// <inheritdoc cref="MicroHID" />
/// </param>
/// <param name="newPhase">
/// <inheritdoc cref="NewPhase" />
/// </param>
/// <param name="isAllowed">
/// <inheritdoc cref="IsAllowed" />
/// </param>
public ChangingMicroHIDPickupStateEventArgs(ItemPickupBase microHID, MicroHidPhase newPhase, bool isAllowed = true)
{
MicroHID = Pickup.Get<MicroHIDPickup>(microHID);
NewPhase = newPhase;
IsAllowed = isAllowed;
}

/// <summary>
/// Gets the MicroHID instance.
/// </summary>
public MicroHIDPickup MicroHID { get; }

/// <summary>
/// Gets or sets the new MicroHID state.
/// </summary>
public MicroHidPhase NewPhase { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the MicroHID state can be changed.
/// </summary>
public bool IsAllowed { get; set; }

/// <inheritdoc/>
public Pickup Pickup => MicroHID;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace Exiled.Events.EventArgs.Player
using API.Features;
using API.Features.Items;
using Interfaces;

using InventorySystem.Items;
using InventorySystem.Items.MicroHID;
using InventorySystem.Items.MicroHID.Modules;

Expand All @@ -32,9 +34,9 @@ public class ChangingMicroHIDStateEventArgs : IDeniableEvent, IItemEvent
/// <param name="isAllowed">
/// <inheritdoc cref="IsAllowed" />
/// </param>
public ChangingMicroHIDStateEventArgs(Item microHID, MicroHidPhase newPhase, bool isAllowed = true)
public ChangingMicroHIDStateEventArgs(ItemBase microHID, MicroHidPhase newPhase, bool isAllowed = true)
{
MicroHID = microHID.As<MicroHid>();
MicroHID = Item.Get<MicroHid>(microHID);
NewPhase = newPhase;
IsAllowed = isAllowed;
}
Expand Down
11 changes: 11 additions & 0 deletions EXILED/Exiled.Events/Handlers/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public static class Item
/// </summary>
public static Event<UsingRadioPickupBatteryEventArgs> UsingRadioPickupBattery { get; set; } = new();

/// <summary>
/// Invoked before a<see cref="API.Features.Pickups.MicroHIDPickup"/> state is changed.
/// </summary>
public static Event<ChangingMicroHIDPickupStateEventArgs> ChangingMicroHIDPickupState { get; set; } = new();

/// <summary>
/// Called before the ammo of an firearm is changed.
/// </summary>
Expand Down Expand Up @@ -94,5 +99,11 @@ public static class Item
/// </summary>
/// <param name="ev">The <see cref="UsingRadioPickupBatteryEventArgs"/> instance.</param>
public static void OnUsingRadioPickupBattery(UsingRadioPickupBatteryEventArgs ev) => UsingRadioPickupBattery.InvokeSafely(ev);

/// <summary>
/// Called before a <see cref="API.Features.Pickups.MicroHIDPickup"/> state is changed.
/// </summary>
/// <param name="ev">The <see cref="ChangingMicroHIDPickupStateEventArgs"/> instance.</param>
public static void OnChangingMicroHIDPickupState(ChangingMicroHIDPickupStateEventArgs ev) => ChangingMicroHIDPickupState.InvokeSafely(ev);
}
}
70 changes: 45 additions & 25 deletions EXILED/Exiled.Events/Patches/Events/Player/ChangingMicroHIDState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Exiled.Events.Patches.Events.Player
using API.Features.Items;
using API.Features.Pools;
using Exiled.Events.Attributes;
using Exiled.Events.EventArgs.Item;
using Exiled.Events.EventArgs.Player;
using HarmonyLib;
using InventorySystem.Items.MicroHID;
Expand All @@ -34,43 +35,43 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

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

LocalBuilder ev = generator.DeclareLocal(typeof(ChangingMicroHIDStateEventArgs));
LocalBuilder item = generator.DeclareLocal(typeof(MicroHIDItem));

int offset = 1;
int index = newInstructions.FindIndex(x => x.opcode == OpCodes.Ret) + offset;

newInstructions.InsertRange(index, new[]
{
// Item.Get(this.Serial);
// MicroHIDItem item = Item.Get(this._prevItem);
// if (item is null)
// pickup code;
// else
// itemCode;
new CodeInstruction(OpCodes.Ldarg_0).MoveLabelsFrom(newInstructions[index]),
new(OpCodes.Ldfld, Field(typeof(CycleController), nameof(CycleController.Serial))),
new(OpCodes.Call, GetDeclaredMethods(typeof(Item)).Find(x => !x.IsGenericMethod && x.IsStatic && x.GetParameters().FirstOrDefault()?.ParameterType == typeof(ushort))),
new(OpCodes.Ldfld, Field(typeof(CycleController), nameof(CycleController._prevItem))),
new(OpCodes.Dup),
new(OpCodes.Stloc_S, item.LocalIndex),
new(OpCodes.Brfalse_S, pickupLabel),

// value
new(OpCodes.Ldarg_1),
// bool allowed = CallItemEvent(microHidItem, phase)
new(OpCodes.Ldloc_S, item.LocalIndex),
new(OpCodes.Ldarga_S, 1),
new(OpCodes.Call, Method(typeof(ChangingMicroHIDState), nameof(ChangingMicroHIDState.CallItemEvent))),

// true
new(OpCodes.Ldc_I4_1),
new CodeInstruction(OpCodes.Br_S, continueLabel),

// ChangerMicroHIDStateEventArgs ev = new(Item.Get(this.Serial), value, true);
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(ChangingMicroHIDStateEventArgs))[0]),
new(OpCodes.Dup),
new(OpCodes.Dup),
new(OpCodes.Stloc_S, ev.LocalIndex),

// Handlers.Player.OnChangingMicroHIDState(ev);
new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnChangingMicroHIDState))),
// bool allowed = CallPickupEvent(Serial, phase)
new CodeInstruction(OpCodes.Ldarg_0).WithLabels(pickupLabel),
new(OpCodes.Ldfld, Field(typeof(CycleController), nameof(CycleController.Serial))),
new(OpCodes.Ldarga_S, 1),
new(OpCodes.Call, Method(typeof(ChangingMicroHIDState), nameof(ChangingMicroHIDState.CallPickupEvent))),

// if (!ev.IsAllowed)
// if (!allowed)
// return;
new(OpCodes.Callvirt, PropertyGetter(typeof(ChangingMicroHIDStateEventArgs), nameof(ChangingMicroHIDStateEventArgs.IsAllowed))),
new(OpCodes.Brfalse_S, returnLabel),

// value = ev.NewPhase;
new(OpCodes.Ldloc_S, ev.LocalIndex),
new(OpCodes.Callvirt, PropertyGetter(typeof(ChangingMicroHIDStateEventArgs), nameof(ChangingMicroHIDStateEventArgs.NewPhase))),
new(OpCodes.Starg_S, 1),
new CodeInstruction(OpCodes.Brfalse_S, returnLabel).WithLabels(continueLabel),
});

newInstructions[newInstructions.Count - 1].labels.Add(returnLabel);
Expand All @@ -80,5 +81,24 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi

ListPool<CodeInstruction>.Pool.Return(newInstructions);
}

private static bool CallItemEvent(MicroHIDItem microHIDItem, ref MicroHidPhase phase)
{
ChangingMicroHIDStateEventArgs ev = new(microHIDItem, phase);
Handlers.Player.OnChangingMicroHIDState(ev);
phase = ev.NewPhase;
return ev.IsAllowed;
}

private static bool CallPickupEvent(ushort serial, ref MicroHidPhase phase)
{
if (!MicroHIDPickup.PickupsBySerial.TryGetValue(serial, out MicroHIDPickup pickup))
return true;

ChangingMicroHIDPickupStateEventArgs ev = new(pickup, phase);
Handlers.Item.OnChangingMicroHIDPickupState(ev);
phase = ev.NewPhase;
return ev.IsAllowed;
}
}
}
}

0 comments on commit 4547fd0

Please sign in to comment.