77
88namespace Exiled . Events . Patches . Events . Scp3114
99{
10- #pragma warning disable SA1402 // File may only contain a single type
11-
1210 using System . Collections . Generic ;
1311 using System . Linq ;
1412 using System . Reflection . Emit ;
@@ -35,7 +33,7 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
3533 {
3634 List < CodeInstruction > newInstructions = ListPool < CodeInstruction > . Pool . Get ( instructions ) ;
3735
38- int index = newInstructions . FindIndex ( x => x . opcode == OpCodes . Brfalse_S ) ;
36+ int index = newInstructions . FindIndex ( x => x . opcode == OpCodes . Brfalse ) ;
3937
4038 Label continueLabel = generator . DefineLabel ( ) ;
4139
@@ -58,64 +56,40 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
5856 new ( OpCodes . Newobj , GetDeclaredConstructors ( typeof ( DancingEventArgs ) ) [ 0 ] ) ,
5957 new ( OpCodes . Dup ) ,
6058 new ( OpCodes . Dup ) ,
61- new ( OpCodes . Stloc_S , ev . LocalIndex ) ,
59+ new ( OpCodes . Stloc , ev ) ,
6260
6361 // Handlers.Scp3114.OnDancing(ev);
6462 new ( OpCodes . Call , Method ( typeof ( Handlers . Scp3114 ) , nameof ( Handlers . Scp3114 . OnDancing ) ) ) ,
6563
6664 // if (ev.IsAllowed)
6765 // goto continueLabel;
6866 new ( OpCodes . Callvirt , PropertyGetter ( typeof ( DancingEventArgs ) , nameof ( DancingEventArgs . IsAllowed ) ) ) ,
69- new ( OpCodes . Brtrue_S , continueLabel ) ,
67+ new ( OpCodes . Brtrue , continueLabel ) ,
7068
7169 // return
7270 new ( OpCodes . Ret ) ,
7371
7472 // ev.NewState
75- new CodeInstruction ( OpCodes . Ldloc_S , ev . LocalIndex ) . WithLabels ( continueLabel ) ,
73+ new CodeInstruction ( OpCodes . Ldloc , ev ) . WithLabels ( continueLabel ) ,
7674 new ( OpCodes . Callvirt , PropertyGetter ( typeof ( DancingEventArgs ) , nameof ( DancingEventArgs . IsDancing ) ) ) ,
7775 } ) ;
7876
79- for ( int z = 0 ; z < newInstructions . Count ; z ++ )
80- yield return newInstructions [ z ] ;
77+ index = newInstructions . FindIndex ( x => x . opcode == OpCodes . Ldc_I4_0 ) ;
8178
82- ListPool < CodeInstruction > . Pool . Return ( newInstructions ) ;
83- }
84- }
79+ newInstructions . RemoveRange ( index , 3 ) ;
8580
86- /// <summary>
87- /// Patches <see cref="Scp3114Dance.ServerProcessCmd"/>
88- /// to add <see cref="Handlers.Scp3114.Dancing"/> event.
89- /// </summary>
90- [ HarmonyPatch ( typeof ( Scp3114Dance ) , nameof ( Scp3114Dance . ServerWriteRpc ) ) ]
91- internal class ChooseDanceType
92- {
93- private static IEnumerable < CodeInstruction > Transpiler ( IEnumerable < CodeInstruction > instructions , ILGenerator generator )
94- {
95- List < CodeInstruction > newInstructions = ListPool < CodeInstruction > . Pool . Get ( instructions ) ;
96-
97- int offset = - 4 ;
98- int index = newInstructions . FindIndex ( x => x . operand == ( object ) Method ( typeof ( NetworkWriter ) , nameof ( NetworkWriter . WriteByte ) ) ) + offset ;
99-
100- newInstructions . RemoveRange ( index , 4 ) ;
101-
102- // replace "writer.WriteByte((byte)UnityEngine.Random.Range(0, 255))"
103- // with "writer.WriteByte(ChooseDanceType.DanceType)"
104- newInstructions . InsertRange ( index , new CodeInstruction [ ]
81+ newInstructions . InsertRange (
82+ index ,
83+ new [ ]
10584 {
106- // Handle(Player.Get(this.Owner));
107- new ( OpCodes . Ldarg_0 ) ,
108-
109- new ( OpCodes . Call , Method ( typeof ( ChooseDanceType ) , nameof ( Handle ) ) ) ,
85+ new CodeInstruction ( OpCodes . Ldloc , ev ) ,
86+ new ( OpCodes . Call , PropertyGetter ( typeof ( DancingEventArgs ) , nameof ( DancingEventArgs . DanceType ) ) ) ,
11087 } ) ;
11188
11289 for ( int z = 0 ; z < newInstructions . Count ; z ++ )
11390 yield return newInstructions [ z ] ;
11491
11592 ListPool < CodeInstruction > . Pool . Return ( newInstructions ) ;
11693 }
117-
118- private static byte Handle ( Scp3114Dance scp3114Dance )
119- => ( byte ) Player . Get ( scp3114Dance . Owner ) . Role . As < API . Features . Roles . Scp3114Role > ( ) . DanceType ;
12094 }
12195}
0 commit comments