|
1 | 1 | #pragma warning disable CS8321 // Local function is declared but never used |
2 | 2 | #pragma warning disable CS0436 // Type conflicts with imported type |
3 | 3 | using ModFramework; |
| 4 | +using Mono.Cecil; |
4 | 5 | using Mono.Cecil.Cil; |
| 6 | +using Mono.Cecil.Rocks; |
5 | 7 | using MonoMod.Cil; |
6 | 8 | using OTAPI.UnifiedServerProcess.Extensions; |
| 9 | +using System; |
| 10 | +using System.Collections.Generic; |
| 11 | +using System.Linq; |
7 | 12 | using System.Runtime.InteropServices; |
8 | 13 | using Terraria; |
9 | 14 |
|
10 | 15 | [Modification(ModType.PostMerge, "Netplay Send Data Check", ModPriority.Early)] |
11 | 16 | [MonoMod.MonoModIgnore] |
12 | 17 | void NetplayConnectionCheck(ModFwModder modder) { |
13 | | - var netplay = modder.Module.GetType("Terraria.Netplay"); |
14 | | - var checkConnectionMDef = modder.Module.ImportReference(typeof(NetMessage).GetMethod(nameof(NetMessage.CheckCanSend))!); |
15 | | - var senddata = modder.Module.GetType("Terraria.NetMessage").GetMethod("mfwh_orig_SendData"); |
16 | 18 |
|
17 | | - var cursor = senddata.GetILCursor(); |
| 19 | + List<MethodDefinition> methods = []; |
| 20 | + foreach (var type in modder.Module.GetAllTypes()) { |
| 21 | + foreach (var method in type.Methods) { |
| 22 | + if (!method.HasBody || method.Name == nameof(NetMessage.CheckCanSend)) { |
| 23 | + continue; |
| 24 | + } |
| 25 | + if (method.Body.Instructions.Any(inst => inst is { Operand: MemberReference { Name: "IsConnected", DeclaringType.Name: "RemoteClient" } })) { |
| 26 | + methods.Add(method); |
| 27 | + continue; |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + |
18 | 32 |
|
| 33 | + var netplay = modder.Module.GetType("Terraria.Netplay"); |
| 34 | + var checkConnectionMDef = modder.Module.ImportReference(typeof(NetMessage).GetMethod(nameof(NetMessage.CheckCanSend))!); |
| 35 | + foreach (var method in methods) { |
| 36 | + var cursor = method.GetILCursor(); |
19 | 37 |
|
20 | | - while (cursor.TryGotoNext(MoveType.Before, |
21 | | - inst => inst.MatchLdsfld("Terraria.Netplay", "Clients"), |
22 | | - inst => true, |
23 | | - inst => inst.MatchLdelemRef(), |
24 | | - inst => inst.MatchCallvirt("Terraria.RemoteClient", "IsConnected"))) { |
| 38 | + while (cursor.TryGotoNext(MoveType.Before, |
| 39 | + inst => inst.MatchLdsfld("Terraria.Netplay", "Clients"), |
| 40 | + inst => true, |
| 41 | + inst => inst.MatchLdelemRef(), |
| 42 | + inst => inst.MatchCallvirt("Terraria.RemoteClient", "IsConnected"))) { |
25 | 43 |
|
26 | | - var loadClients = cursor.Next!; |
27 | | - var loadIndex = cursor.Next!.Next!; |
28 | | - var loadElement = cursor.Next!.Next!.Next; |
29 | | - var checkConnection = cursor.Next!.Next!.Next!.Next; |
| 44 | + var loadClients = cursor.Next!; |
| 45 | + var loadIndex = cursor.Next!.Next!; |
| 46 | + var loadElement = cursor.Next!.Next!.Next; |
| 47 | + var checkConnection = cursor.Next!.Next!.Next!.Next; |
30 | 48 |
|
31 | | - loadClients.OpCode = OpCodes.Nop; |
32 | | - loadClients.Operand = null; |
| 49 | + loadClients.OpCode = OpCodes.Nop; |
| 50 | + loadClients.Operand = null; |
33 | 51 |
|
34 | | - loadElement.OpCode = OpCodes.Nop; |
35 | | - loadElement.Operand = null; |
| 52 | + loadElement.OpCode = OpCodes.Nop; |
| 53 | + loadElement.Operand = null; |
36 | 54 |
|
37 | | - checkConnection.OpCode = OpCodes.Call; |
38 | | - checkConnection.Operand = checkConnectionMDef; |
| 55 | + checkConnection.OpCode = OpCodes.Call; |
| 56 | + checkConnection.Operand = checkConnectionMDef; |
| 57 | + } |
39 | 58 | } |
40 | 59 | } |
41 | 60 |
|
|
0 commit comments