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
+ }
0 commit comments