Skip to content

Commit 4f8c770

Browse files
committed
refactor(core): overhaul method id generation and improve patcher stability
- rewrite MonoModExtensions.GetIdentifier with generic/anonymous-type edge-case handling - harden NetworkLogicPruner EH pruning and add regression tests - add thread-local/sections reset patches and update GlobalNetwork runtime wiring
1 parent 94c933f commit 4f8c770

31 files changed

Lines changed: 1442 additions & 318 deletions

src/OTAPI.UnifiedServerProcess.GlobalNetwork/CommandHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class CommandHandler
2121
public CommandHandler(Router router, ChatHandler chat) {
2222
this.router = router;
2323
On.Terraria.Chat.Commands.SayChatCommand.ProcessIncomingMessage += ProcessIncomingMessage;
24-
On.OTAPI.HooksSystemContext.MainSystemContext.InvokeCommandProcess += ProcessConsoleMessage;
24+
On.OTAPI.HooksSystemContext.MainSystemContext.InvokeCommandProcess_string_string += ProcessConsoleMessage;
2525
}
2626

2727
public void KeepReadingInput() {
@@ -39,7 +39,7 @@ void ProcessIncomingMessage(On.Terraria.Chat.Commands.SayChatCommand.orig_Proces
3939

4040
orig(self, root, text, clientId);
4141
}
42-
bool ProcessConsoleMessage(On.OTAPI.HooksSystemContext.MainSystemContext.orig_InvokeCommandProcess orig, HooksSystemContext.MainSystemContext self,
42+
bool ProcessConsoleMessage(On.OTAPI.HooksSystemContext.MainSystemContext.orig_InvokeCommandProcess_string_string orig, HooksSystemContext.MainSystemContext self,
4343
string lowered, string raw) {
4444

4545
if (ProcessUSPCommandText(self.root, raw, byte.MaxValue)) {

src/OTAPI.UnifiedServerProcess.GlobalNetwork/Network/NetworkPatcher.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ private static void Modified_StartServer(On.Terraria.NetplaySystemContext.orig_S
2323

2424
var Main = server.Main;
2525

26-
Terraria.Main.rand ??= new UnifiedRandom((int)DateTime.Now.Ticks);
2726
self.ServerIP = IPAddress.Any;
2827
Main.menuMode = 14;
2928
Main.statusText = Lang.menu[8].Value;

src/OTAPI.UnifiedServerProcess.GlobalNetwork/Network/Router.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ public void ServerWarp(byte plr, ServerContext to) {
340340
activePlayer.active = true;
341341

342342
SetClientCurrentlyServer(plr, to);
343-
client.ResetSections(to);
343+
client.mfwh_ResetSections(to);
344344

345345
SyncHelper.SyncServerOnlineToPlayer(to, plr);
346346
SyncHelper.SyncPlayerJoinToOthers(to, plr);

src/OTAPI.UnifiedServerProcess.GlobalNetwork/Network/SyncHelper.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@ static void SendWorldEntities(this ServerContext server, int whoAmI) {
6868
}
6969
}
7070
static void SendWorldInfo(this ServerContext server, int whoAmI) {
71-
for (int i = 0; i < 290; i++) {
72-
server.NetMessage.TrySendData(MessageID.NPCKillCountDeathTally, whoAmI, -1, null, i);
73-
}
7471
server.NetMessage.TrySendData(57, whoAmI);
7572
server.NetMessage.TrySendData(MessageID.MoonlordHorror);
7673
server.NetMessage.TrySendData(MessageID.UpdateTowerShieldStrengths, whoAmI);
@@ -112,7 +109,7 @@ public static void SyncServerOfflineToPlayer(ServerContext offlineServer, int pl
112109
if (!item.active || item.playerIndexTheItemIsReservedFor != plr) {
113110
continue;
114111
}
115-
SendSmallPacket(offlineServer, plr, new ItemOwner((short)i, 255));
112+
SendSmallPacket(offlineServer, plr, new ItemOwner((short)i, 255, item.position));
116113
}
117114
for (int i = 0; i < Terraria.Main.maxProjectiles; i++) {
118115
var proj = offlineServer.Main.projectile[i];

src/OTAPI.UnifiedServerProcess.GlobalNetwork/OTAPI.UnifiedServerProcess.GlobalNetwork.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="MonoMod.RuntimeDetour" Version="25.2.3" />
13+
<PackageReference Include="MonoMod.RuntimeDetour" Version="25.3.3" />
1414
<PackageReference Include="MonoMod.RuntimeDetour.HookGen" Version="22.7.31.1" />
15-
<PackageReference Include="ModFramework.Modules.CSharp" Version="1.1.15" />
15+
<PackageReference Include="ModFramework.Modules.CSharp" Version="1.1.16" />
16+
<PackageReference Include="System.IO.Packaging" Version="10.0.2" />
1617
</ItemGroup>
1718
<ItemGroup>
1819
<Reference Include="OTAPI">

src/OTAPI.UnifiedServerProcess.GlobalNetwork/Program.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,8 @@ static void Main(string[] args) {
4444

4545
WorkRunner.RunTimedWorkAsync("Starting main servers...",
4646
() => {
47-
Task.Run(() => {
48-
server1.Program.LaunchGame(args);
49-
});
50-
Task.Run(() => {
51-
server2.Program.LaunchGame(args);
52-
});
47+
server1.Run(args);
48+
server2.Run(args);
5349
var tcs = new TaskCompletionSource();
5450
router.Started += () => tcs.SetResult();
5551
return tcs;

src/OTAPI.UnifiedServerProcess.GlobalNetwork/Servers/ServerContext.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ public ServerContext(string worldName, byte[] worldFileData) : base(worldName) {
1919
Netplay.ListenPort = -1;
2020
Netplay.UseUPNP = true;
2121
}
22+
public Thread? RunningThread { get; protected set; }
23+
public virtual Thread Run(string[] args) {
24+
Thread result = RunningThread = new Thread(() => RunBlocking(args)) {
25+
IsBackground = true,
26+
};
27+
result.Name = $"Server Instance: {Name}";
28+
result.Start();
29+
return result;
30+
}
31+
public virtual void RunBlocking(string[] args) {
32+
ThreadLocalInitializer.Initialize();
33+
RunningThread = Thread.CurrentThread;
34+
RunningThread.Name = $"Server Instance: {Name}";
35+
Program.LaunchGame(args);
36+
}
2237
public override string ToString() => $"{{ Type:ServerContext, Name:\"{Name}\", Players:{Main.player.Count(p => p.active)} }}";
2338
}
2439
}

src/OTAPI.UnifiedServerProcess.UnitTests/DelegatePlaceholderProcessorByRefTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using OTAPI.UnifiedServerProcess.Core;
55
using OTAPI.UnifiedServerProcess.Core.FunctionalFeatures;
66
using OTAPI.UnifiedServerProcess.Core.Patching.FieldFilterPatching;
7+
using OTAPI.UnifiedServerProcess.Core.Patching.GeneralPatching;
8+
using OTAPI.UnifiedServerProcess.Core.Patching.GeneralPatching.Arguments;
79
using OTAPI.UnifiedServerProcess.Loggers;
810
using System;
911
using System.IO;
@@ -113,8 +115,8 @@ public void Apply_PropagatesDelegateRewriteThroughByRefFieldAndLocalAddresses()
113115
host.Methods.Add(callerLocalByRef);
114116

115117
var analyzers = new AnalyzerGroups(new NullLogger(), module);
116-
var processor = new DelegatePlaceholderProcessor(rootContextDef, analyzers);
117-
var source = new FilterArgumentSource(module, []);
118+
var processor = new DelegatePlaceholderProcessor(analyzers);
119+
var source = new PatcherArgumentSource(module, rootContextDef);
118120

119121
processor.Apply(new TestComponent(new NullLogger()), ref source);
120122
processor.ClearJumpSitesCache();

0 commit comments

Comments
 (0)