Skip to content

Commit 96c5d3f

Browse files
committed
fix: More unified check of connected clients
1 parent 0aaa952 commit 96c5d3f

File tree

1 file changed

+38
-19
lines changed

1 file changed

+38
-19
lines changed

src/OTAPI.UnifiedServerProcess/Mods/NetplayMod.cs

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,60 @@
11
#pragma warning disable CS8321 // Local function is declared but never used
22
#pragma warning disable CS0436 // Type conflicts with imported type
33
using ModFramework;
4+
using Mono.Cecil;
45
using Mono.Cecil.Cil;
6+
using Mono.Cecil.Rocks;
57
using MonoMod.Cil;
68
using OTAPI.UnifiedServerProcess.Extensions;
9+
using System;
10+
using System.Collections.Generic;
11+
using System.Linq;
712
using System.Runtime.InteropServices;
813
using Terraria;
914

1015
[Modification(ModType.PostMerge, "Netplay Send Data Check", ModPriority.Early)]
1116
[MonoMod.MonoModIgnore]
1217
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");
1618

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+
1832

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();
1937

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"))) {
2543

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;
3048

31-
loadClients.OpCode = OpCodes.Nop;
32-
loadClients.Operand = null;
49+
loadClients.OpCode = OpCodes.Nop;
50+
loadClients.Operand = null;
3351

34-
loadElement.OpCode = OpCodes.Nop;
35-
loadElement.Operand = null;
52+
loadElement.OpCode = OpCodes.Nop;
53+
loadElement.Operand = null;
3654

37-
checkConnection.OpCode = OpCodes.Call;
38-
checkConnection.Operand = checkConnectionMDef;
55+
checkConnection.OpCode = OpCodes.Call;
56+
checkConnection.Operand = checkConnectionMDef;
57+
}
3958
}
4059
}
4160

0 commit comments

Comments
 (0)