Skip to content
This repository was archived by the owner on Jan 19, 2025. It is now read-only.

Commit 47fa0f0

Browse files
committed
Fix TogglingWeaponFlashlight & AimingDownSight events
1 parent 00a919e commit 47fa0f0

File tree

5 files changed

+188
-13
lines changed

5 files changed

+188
-13
lines changed

Exiled.Events/EventArgs/Player/AimingDownSightEventArgs.cs

+6-11
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,20 @@ public class AimingDownSightEventArgs : IPlayerEvent, IFirearmEvent
2626
/// <param name="firearm">
2727
/// <inheritdoc cref="Firearm" />
2828
/// </param>
29-
/// <param name="adsIn">
30-
/// <inheritdoc cref="AdsIn" />
29+
/// <param name="aiming">
30+
/// <inheritdoc cref="Aiming" />
3131
/// </param>
32-
public AimingDownSightEventArgs(Player player, Firearm firearm, bool adsIn)
32+
public AimingDownSightEventArgs(Player player, Firearm firearm, bool aiming)
3333
{
3434
Firearm = firearm;
3535
Player = player;
36-
AdsIn = adsIn;
36+
Aiming = aiming;
3737
}
3838

3939
/// <summary>
40-
/// Gets a value indicating whether or not the player is aiming down sight in.
40+
/// Gets a value indicating whether the player starts aiming or stops aiming.
4141
/// </summary>
42-
public bool AdsIn { get; }
43-
44-
/// <summary>
45-
/// Gets a value indicating whether or not the player is aiming down sight out.
46-
/// </summary>
47-
public bool AdsOut => !AdsIn;
42+
public bool Aiming { get; }
4843

4944
/// <summary>
5045
/// Gets the <see cref="API.Features.Items.Firearm" /> used to trigger the aim action.

Exiled.Events/EventArgs/Player/TogglingWeaponFlashlightEventArgs.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ public TogglingWeaponFlashlightEventArgs(Player player, Firearm firearm, bool ne
4141
}
4242

4343
/// <summary>
44-
/// Gets a value indicating whether the new weapon's flashlight state will be enabled.
44+
/// Gets or sets a value indicating whether the weapon's flashlight will turn on or off.
4545
/// </summary>
46-
public bool NewState { get; }
46+
public bool NewState { get; set; }
4747

4848
/// <summary>
4949
/// Gets or sets a value indicating whether or not the weapon's flashlight can be toggled.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// -----------------------------------------------------------------------
2+
// <copyright file="Aiming.cs" company="Exiled Team">
3+
// Copyright (c) Exiled Team. All rights reserved.
4+
// Licensed under the CC BY-SA 3.0 license.
5+
// </copyright>
6+
// -----------------------------------------------------------------------
7+
8+
namespace Exiled.Events.Patches.Events.Player.FirearmActionEvents
9+
{
10+
using System.Collections.Generic;
11+
using System.Reflection.Emit;
12+
13+
using Exiled.API.Features.Core.Generic.Pools;
14+
using Exiled.API.Features.Items;
15+
using Exiled.Events.Attributes;
16+
using Exiled.Events.EventArgs.Player;
17+
using HarmonyLib;
18+
using InventorySystem.Items.Firearms.Modules;
19+
20+
using static HarmonyLib.AccessTools;
21+
22+
/// <summary>
23+
/// Patches <see cref="LinearAdsModule.ServerProcessCmd" />.
24+
/// Adds <see cref="Handlers.Player.AimingDownSight" /> event.
25+
/// </summary>
26+
[EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.AimingDownSight))]
27+
[HarmonyPatch(typeof(LinearAdsModule), nameof(LinearAdsModule.ServerProcessCmd))]
28+
internal static class Aiming
29+
{
30+
[HarmonyTranspiler]
31+
private static IEnumerable<CodeInstruction> OnAimStatusChanged(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
32+
{
33+
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);
34+
35+
Label returnLabel = generator.DefineLabel();
36+
37+
int index = newInstructions.FindIndex(instruction => instruction.opcode == OpCodes.Ret);
38+
39+
newInstructions.InsertRange(index, new[]
40+
{
41+
// Player.Get(this.Firearm.Owner)
42+
new CodeInstruction(OpCodes.Ldarg_0),
43+
new(OpCodes.Callvirt, PropertyGetter(typeof(LinearAdsModule), nameof(LinearAdsModule.Firearm))),
44+
new(OpCodes.Callvirt, PropertyGetter(typeof(InventorySystem.Items.Firearms.Firearm), nameof(InventorySystem.Items.Firearms.Firearm.Owner))),
45+
new(OpCodes.Call, Method(typeof(API.Features.Player), nameof(API.Features.Player.Get), new[] { typeof(ReferenceHub) })),
46+
47+
// (Firearm)Item.Get(this.Firearm)
48+
new(OpCodes.Ldarg_0),
49+
new(OpCodes.Callvirt, PropertyGetter(typeof(LinearAdsModule), nameof(LinearAdsModule.Firearm))),
50+
new(OpCodes.Call, Method(typeof(Item), nameof(Item.Get), new[] { typeof(InventorySystem.Items.Firearms.Firearm) })),
51+
new(OpCodes.Castclass, typeof(Firearm)),
52+
53+
// this._userInput
54+
new(OpCodes.Ldfld, Field(typeof(LinearAdsModule), nameof(LinearAdsModule._userInput))),
55+
56+
// AimingDownSightEventArgs args = new(Player, Firearm, bool)
57+
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(AimingDownSightEventArgs))[0]),
58+
59+
// Player.OnAimingDownSight(args)
60+
new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnAimingDownSight))),
61+
});
62+
63+
newInstructions[newInstructions.Count - 1].labels.Add(returnLabel);
64+
65+
foreach (CodeInstruction instruction in newInstructions)
66+
yield return instruction;
67+
68+
ListPool<CodeInstruction>.Pool.Return(newInstructions);
69+
}
70+
}
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// -----------------------------------------------------------------------
2+
// <copyright file="ToggleWeaponFlashlight.cs" company="Exiled Team">
3+
// Copyright (c) Exiled Team. All rights reserved.
4+
// Licensed under the CC BY-SA 3.0 license.
5+
// </copyright>
6+
// -----------------------------------------------------------------------
7+
8+
namespace Exiled.Events.Patches.Events.Player.FirearmActionEvents
9+
{
10+
using System.Collections.Generic;
11+
using System.Reflection.Emit;
12+
13+
using Exiled.API.Features.Core.Generic.Pools;
14+
using Exiled.API.Features.Items;
15+
using Exiled.Events.Attributes;
16+
using Exiled.Events.EventArgs.Player;
17+
using HarmonyLib;
18+
using InventorySystem.Items.Firearms.Attachments;
19+
20+
using static HarmonyLib.AccessTools;
21+
22+
/// <summary>
23+
/// Patches <see cref="FlashlightAttachment.ServerSendStatus" />.
24+
/// Adds <see cref="Handlers.Player.TogglingWeaponFlashlight" /> & <see cref="Handlers.Player.ToggledWeaponFlashlight" /> event.
25+
/// </summary>
26+
[EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.TogglingWeaponFlashlight))]
27+
[HarmonyPatch(typeof(FlashlightAttachment), nameof(FlashlightAttachment.ServerSendStatus))]
28+
internal static class ToggleWeaponFlashlight
29+
{
30+
[HarmonyTranspiler]
31+
private static IEnumerable<CodeInstruction> OnToggleWeaponFlashlight(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
32+
{
33+
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);
34+
35+
Label returnLabel = generator.DefineLabel();
36+
LocalBuilder ev = generator.DeclareLocal(typeof(TogglingWeaponFlashlightEventArgs));
37+
LocalBuilder firearm = generator.DeclareLocal(typeof(Firearm));
38+
LocalBuilder player = generator.DeclareLocal(typeof(API.Features.Player));
39+
LocalBuilder status = generator.DeclareLocal(typeof(bool));
40+
41+
newInstructions.InsertRange(0, new CodeInstruction[]
42+
{
43+
// Player.Get(this.Firearm.Owner)
44+
new(OpCodes.Ldarg_0),
45+
new(OpCodes.Callvirt, PropertyGetter(typeof(FlashlightAttachment), nameof(FlashlightAttachment.Firearm))),
46+
new(OpCodes.Callvirt, PropertyGetter(typeof(InventorySystem.Items.Firearms.Firearm), nameof(InventorySystem.Items.Firearms.Firearm.Owner))),
47+
new(OpCodes.Call, Method(typeof(API.Features.Player), nameof(API.Features.Player.Get), new[] { typeof(ReferenceHub) })),
48+
new(OpCodes.Stloc_S, player.LocalIndex),
49+
50+
// (Firearm)Item.Get(this.Firearm)
51+
new(OpCodes.Ldarg_0),
52+
new(OpCodes.Callvirt, PropertyGetter(typeof(FlashlightAttachment), nameof(FlashlightAttachment.Firearm))),
53+
new(OpCodes.Call, Method(typeof(Item), nameof(Item.Get), new[] { typeof(InventorySystem.Items.Firearms.Firearm) })),
54+
new(OpCodes.Castclass, typeof(Firearm)),
55+
new(OpCodes.Stloc_S, firearm.LocalIndex),
56+
57+
// status
58+
new(OpCodes.Ldarg_1),
59+
60+
// true
61+
new(OpCodes.Ldc_I4_1),
62+
63+
// TogglingWeaponFlashlightEventArgs args = new(Player, Firearm, bool, bool)
64+
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(TogglingWeaponFlashlightEventArgs))[0]),
65+
new(OpCodes.Dup),
66+
new(OpCodes.Dup),
67+
new(OpCodes.Stloc_S, ev.LocalIndex),
68+
69+
new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnTogglingWeaponFlashlight))),
70+
71+
// if (!args.IsAllowed)
72+
// return;
73+
new(OpCodes.Callvirt, PropertyGetter(typeof(TogglingWeaponFlashlightEventArgs), nameof(TogglingWeaponFlashlightEventArgs.IsAllowed))),
74+
new(OpCodes.Brfalse_S, returnLabel),
75+
76+
// status = NewState
77+
new(OpCodes.Ldloc_S, ev.LocalIndex),
78+
new(OpCodes.Callvirt, PropertyGetter(typeof(TogglingWeaponFlashlightEventArgs), nameof(TogglingWeaponFlashlightEventArgs.NewState))),
79+
new(OpCodes.Starg_S, 1),
80+
new(OpCodes.Ldarg_1),
81+
new(OpCodes.Stloc_S, status.LocalIndex),
82+
});
83+
84+
newInstructions.InsertRange(newInstructions.Count - 1, new CodeInstruction[]
85+
{
86+
// Player
87+
new(OpCodes.Ldloc_S, player.LocalIndex),
88+
89+
// Firearm
90+
new(OpCodes.Ldloc_S, firearm.LocalIndex),
91+
92+
// status
93+
new(OpCodes.Ldloc_S, status.LocalIndex),
94+
95+
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(ToggledWeaponFlashlightEventArgs))[0]),
96+
new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnToggledWeaponFlashlight))),
97+
});
98+
99+
newInstructions[newInstructions.Count - 1].labels.Add(returnLabel);
100+
101+
foreach (CodeInstruction instruction in newInstructions)
102+
yield return instruction;
103+
104+
ListPool<CodeInstruction>.Pool.Return(newInstructions);
105+
}
106+
}
107+
}

Exiled.Events/Patches/Events/Player/FirearmRequestReceived.cs

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ namespace Exiled.Events.Patches.Events.Player
2828

2929
using static HarmonyLib.AccessTools;
3030

31+
/*
3132
/// <summary>
3233
/// Patches <see cref="FirearmBasicMessagesHandler.ServerRequestReceived" />.
3334
/// Adds <see cref="Player.ReloadingWeapon" />, <see cref="Player.ReloadedWeapon" />,
@@ -329,4 +330,5 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
329330
ListPool<CodeInstruction>.Pool.Return(newInstructions);
330331
}
331332
}
333+
*/
332334
}

0 commit comments

Comments
 (0)