1- // -----------------------------------------------------------------------
1+ // -----------------------------------------------------------------------
22// <copyright file="ChangingDisruptorMode.cs" company="ExMod Team">
33// Copyright (c) ExMod Team. All rights reserved.
44// Licensed under the CC BY-SA 3.0 license.
88namespace Exiled . Events . Patches . Events . Player
99{
1010 using System . Collections . Generic ;
11- using System . Linq ;
1211 using System . Reflection . Emit ;
1312
14- using Exiled . API . Features . Items ;
1513 using Exiled . API . Features . Pools ;
1614 using Exiled . Events . Attributes ;
1715 using Exiled . Events . EventArgs . Player ;
1816 using HarmonyLib ;
19- using InventorySystem . Items ;
17+
2018 using InventorySystem . Items . Firearms . Modules ;
2119 using Mirror ;
2220
@@ -30,36 +28,37 @@ namespace Exiled.Events.Patches.Events.Player
3028 [ HarmonyPatch ( typeof ( DisruptorModeSelector ) , nameof ( DisruptorModeSelector . ServerProcessCmd ) ) ]
3129 internal static class ChangingDisruptorMode
3230 {
33- private static IEnumerable < CodeInstruction > Transpiler ( IEnumerable < CodeInstruction > instructions )
31+ private static IEnumerable < CodeInstruction > Transpiler ( IEnumerable < CodeInstruction > instructions , ILGenerator generator )
3432 {
3533 List < CodeInstruction > newInstructions = ListPool < CodeInstruction > . Pool . Get ( instructions ) ;
3634
37- int index = newInstructions . FindLastIndex ( x => x . opcode == OpCodes . Ldarg_0 ) ;
35+ LocalBuilder newMode = generator . DeclareLocal ( typeof ( bool ) ) ;
36+
37+ int offset = 2 ;
38+ int index = newInstructions . FindIndex ( x => x . Calls ( Method ( typeof ( NetworkReaderExtensions ) , nameof ( NetworkReaderExtensions . ReadBool ) ) ) ) ;
3839
39- newInstructions . InsertRange ( index , new CodeInstruction [ ]
40+ newInstructions . InsertRange ( index + offset , new CodeInstruction [ ]
4041 {
41- // Item.Get( this.Firearm) ;
42+ // this.Firearm;
4243 new ( OpCodes . Ldarg_0 ) ,
4344 new ( OpCodes . Callvirt , PropertyGetter ( typeof ( DisruptorModeSelector ) , nameof ( DisruptorModeSelector . Firearm ) ) ) ,
44- new ( OpCodes . Call , GetDeclaredMethods ( typeof ( Item ) ) . Find ( x => ! x . IsGenericMethod && x . GetParameters ( ) . FirstOrDefault ( ) ? . ParameterType == typeof ( ItemBase ) ) ) ,
4545
46- // reader.ReadBool();
47- new ( OpCodes . Ldarg_1 ) ,
48- new ( OpCodes . Call , Method ( typeof ( NetworkReaderExtensions ) , nameof ( NetworkReaderExtensions . ReadBool ) ) ) ,
46+ // newMode;
47+ new ( OpCodes . Ldloc , newMode ) ,
4948
50- // ChangerDisruptorModeEventArgs ev = new(Item.Get( this.Firearm), reader.ReadBool(), true );
49+ // ChangerDisruptorModeEventArgs ev = new(this.Firearm, newMode );
5150 new ( OpCodes . Newobj , GetDeclaredConstructors ( typeof ( ChangingDisruptorModeEventArgs ) ) [ 0 ] ) ,
5251
5352 // Handlers.Player.OnChangingDisruptorMode(ev);
5453 new ( OpCodes . Call , Method ( typeof ( Handlers . Player ) , nameof ( Handlers . Player . OnChangingDisruptorMode ) ) ) ,
54+ } ) ;
5555
56- // reader.Position--;
57- new ( OpCodes . Ldarg_1 ) ,
58- new ( OpCodes . Ldarg_1 ) ,
59- new ( OpCodes . Ldfld , Field ( typeof ( NetworkReader ) , nameof ( NetworkReader . Position ) ) ) ,
60- new ( OpCodes . Ldc_I4_M1 ) ,
61- new ( OpCodes . Add ) ,
62- new ( OpCodes . Stfld , Field ( typeof ( NetworkReader ) , nameof ( NetworkReader . Position ) ) ) ,
56+ offset = 1 ;
57+ newInstructions . InsertRange ( index + offset , new CodeInstruction [ ]
58+ {
59+ // bool newMode = reader.ReadBool();
60+ new ( OpCodes . Dup ) ,
61+ new ( OpCodes . Stloc , newMode ) ,
6362 } ) ;
6463
6564 for ( int z = 0 ; z < newInstructions . Count ; z ++ )
@@ -68,4 +67,4 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
6867 ListPool < CodeInstruction > . Pool . Return ( newInstructions ) ;
6968 }
7069 }
71- }
70+ }
0 commit comments