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

New event for radio #2308

Merged
merged 1 commit into from
Dec 5, 2023
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
45 changes: 45 additions & 0 deletions Exiled.Events/EventArgs/Item/UsingRadioPickupBatteryEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// -----------------------------------------------------------------------
// <copyright file="UsingRadioPickupBatteryEventArgs.cs" company="Exiled Team">
// Copyright (c) Exiled 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 Exiled.Events.EventArgs.Interfaces;

/// <summary>
/// Contains all information before radio pickup battery drains.
/// </summary>
public class UsingRadioPickupBatteryEventArgs : IDeniableEvent, IPickupEvent
{
/// <summary>
/// Initializes a new instance of the <see cref="UsingRadioPickupBatteryEventArgs"/> class.
/// </summary>
/// <param name="pickup"><inheritdoc cref="RadioPickup"/></param>
/// <param name="drain"><inheritdoc cref="Drain"/></param>
/// <param name="isAllowed"><inheritdoc cref="IsAllowed"/></param>
public UsingRadioPickupBatteryEventArgs(InventorySystem.Items.Radio.RadioPickup pickup, float drain, bool isAllowed = true)
{
RadioPickup = Pickup.Get(pickup).As<RadioPickup>();
Drain = drain;
IsAllowed = isAllowed;
}

/// <inheritdoc/>
public bool IsAllowed { get; set; }

/// <inheritdoc/>
public Pickup Pickup => RadioPickup;

/// <inheritdoc cref="Pickup"/>
public RadioPickup RadioPickup { get; }

/// <summary>
/// Gets or sets the radio percent drain.
/// </summary>
public float Drain { get; set; }
}
}
11 changes: 11 additions & 0 deletions Exiled.Events/Handlers/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public static class Item
/// </summary>
public static Event<ChargingJailbirdEventArgs> ChargingJailbird { get; set; } = new();

/// <summary>
/// Invoked before a radio pickup is draining battery.
/// </summary>
public static Event<UsingRadioPickupBatteryEventArgs> UsingRadioPickupBattery { get; set; } = new();

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

/// <summary>
/// Called before radio pickup is draining battery.
/// </summary>
/// <param name="ev">The <see cref="UsingRadioPickupBatteryEventArgs"/> instance.</param>
public static void OnUsingRadioPickupBattery(UsingRadioPickupBatteryEventArgs ev) => UsingRadioPickupBattery.InvokeSafely(ev);
}
}
79 changes: 79 additions & 0 deletions Exiled.Events/Patches/Events/Item/UsingRadioPickupBattery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// -----------------------------------------------------------------------
// <copyright file="UsingRadioPickupBattery.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.Events.Patches.Events.Item
{
using System.Collections.Generic;
using System.Reflection.Emit;

using Exiled.API.Features.Pools;
using Exiled.Events.EventArgs.Item;
using HarmonyLib;
using InventorySystem.Items.Radio;

using static HarmonyLib.AccessTools;

/// <summary>
/// Patches <see cref="RadioPickup.LateUpdate"/>
/// to add <see cref="Handlers.Item.UsingRadioPickupBattery"/> event.
/// </summary>
[HarmonyPatch(typeof(RadioPickup), nameof(RadioPickup.LateUpdate))]
internal class UsingRadioPickupBattery
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

LocalBuilder ev = generator.DeclareLocal(typeof(UsingRadioPickupBatteryEventArgs));

Label continueLabel = generator.DefineLabel();

int index = newInstructions.FindIndex(x => x.opcode == OpCodes.Ldloc_1);

newInstructions.InsertRange(
index,
new[]
{
// this
new(OpCodes.Ldarg_0),

// num
new(OpCodes.Ldloc_1),

// true
new(OpCodes.Ldc_I4_1),

// UsingRadioPickupBatteryEventArgs ev = new(this, num, true);
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(UsingRadioPickupBatteryEventArgs))[0]),
new(OpCodes.Dup),
new(OpCodes.Dup),
new(OpCodes.Stloc_S, ev.LocalIndex),

// Handlers.Item.OnUsingRadioPickupBattery(ev);
new(OpCodes.Call, Method(typeof(Handlers.Item), nameof(Handlers.Item.OnUsingRadioPickupBattery))),

// if (ev.IsAllowed)
// goto continueLabel
new(OpCodes.Callvirt, PropertyGetter(typeof(UsingRadioPickupBatteryEventArgs), nameof(UsingRadioPickupBatteryEventArgs.IsAllowed))),
new(OpCodes.Brtrue_S, continueLabel),

// return
new(OpCodes.Ret),

// num = ev.Drain;
new CodeInstruction(OpCodes.Ldloc_S, ev.LocalIndex).WithLabels(continueLabel),
new(OpCodes.Callvirt, PropertyGetter(typeof(UsingRadioPickupBatteryEventArgs), nameof(UsingRadioPickupBatteryEventArgs.Drain))),
new(OpCodes.Stloc_1),
});

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

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