Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: microhid events #339

Merged
merged 3 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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 = (MicroHIDPickup)Pickup.Get(microHID);
louis1706 marked this conversation as resolved.
Show resolved Hide resolved
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;
}
}
}
}
Loading