Skip to content

Commit 6f98361

Browse files
committed
feat: add TrProtocol
1 parent 74aabfd commit 6f98361

File tree

273 files changed

+8708
-18
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

273 files changed

+8708
-18
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,14 @@ void StartListeningIfNeeded() {
221221
if (isListening || !main.IsRunning || GetClientSpace() <= 0) {
222222
return;
223223
}
224-
isListening = true;
225-
listener.Start();
224+
try {
225+
isListening = true;
226+
listener.Start();
227+
}
228+
catch {
229+
isListening = false;
230+
return;
231+
}
226232
Task.Run(ListenLoop);
227233
Task.Run(LaunchBroadcast);
228234
}

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
using Microsoft.Xna.Framework;
22
using OTAPI.UnifiedServerProcess.GlobalNetwork.Servers;
3+
using System.Runtime.CompilerServices;
34
using Terraria;
45
using Terraria.ID;
56
using Terraria.Localization;
7+
using TrProtocol;
8+
using TrProtocol.NetPackets;
9+
using MessageID = Terraria.ID.MessageID;
610

711
namespace OTAPI.UnifiedServerProcess.GlobalNetwork.Network
812
{
@@ -79,10 +83,26 @@ static void SendWorldInfo(this ServerContext server, int whoAmI) {
7983
#endregion
8084

8185
#region Sync Server Offline To Player
82-
static void SendRawData(ServerContext offlineServer, int plr, byte[] data, int offset, int count) {
83-
var client = offlineServer.Netplay.Clients[plr];
84-
offlineServer.Hooks.NetMessage.InvokeSendBytes(client.Socket, data, offset, count, delegate (object state) {
85-
client.ServerWriteCallBack(offlineServer, state);
86+
static void SendRawData(ServerContext server, int plr, byte[] data, int offset, int count) {
87+
var client = server.Netplay.Clients[plr];
88+
server.Hooks.NetMessage.InvokeSendBytes(client.Socket, data, offset, count, delegate (object state) {
89+
client.ServerWriteCallBack(server, state);
90+
},
91+
null, plr);
92+
}
93+
static unsafe void SendSmallPacket<TPacket>(ServerContext server, int plr, TPacket packet) where TPacket : unmanaged, INetPacket {
94+
var client = server.Netplay.Clients[plr];
95+
short size = (short)(sizeof(TPacket) + 4);
96+
var bufferArray = new byte[size];
97+
fixed (byte* buffer = bufferArray) {
98+
void* ptr = buffer + 2;
99+
packet.WriteContent(ref ptr);
100+
size = (short)((byte*)ptr - buffer);
101+
Unsafe.Write(buffer, size);
102+
}
103+
104+
server.Hooks.NetMessage.InvokeSendBytes(client.Socket, bufferArray, 0, size, delegate (object state) {
105+
client.ServerWriteCallBack(server, state);
86106
},
87107
null, plr);
88108
}
@@ -97,6 +117,7 @@ public static void SyncServerOfflineToPlayer(ServerContext offlineServer, int pl
97117
data[3] = (byte)(itemSlot & 0xFF);
98118
data[4] = (byte)(itemSlot >> 8);
99119
SendRawData(offlineServer, plr, data, 0, 6);
120+
SendSmallPacket(offlineServer, plr, new ItemOwner())
100121
}
101122
for (int i = 0; i < Terraria.Main.maxProjectiles; i++) {
102123
var proj = offlineServer.Main.projectile[i];

src/OTAPI.UnifiedServerProcess.sln

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OTAPI.UnifiedServerProcess.
1010
EndProject
1111
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OTAPI.UnifiedServerProcess.ConsoleClient", "OTAPI.UnifiedServerProcess.ConsoleClient\OTAPI.UnifiedServerProcess.ConsoleClient.csproj", "{5BFD176F-57B3-711D-14AE-097425D79738}"
1212
EndProject
13+
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "TrProtocol.Shared", "..\..\UnifierServerProcess.TrProtocol\src\UnifierServerProcess.TrProtocol\src\TrProtocol.Shared\TrProtocol.Shared.shproj", "{768417B4-D342-4006-B196-5F7178582209}"
14+
EndProject
15+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TrProtocol.SerializerGenerator", "..\..\UnifierServerProcess.TrProtocol\src\UnifierServerProcess.TrProtocol\src\TrProtocol.SerializerGenerator\TrProtocol.SerializerGenerator.csproj", "{524A7425-4B98-A45A-4CD0-52AEFA5B7FA2}"
16+
EndProject
17+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TrProtocol", "..\..\UnifierServerProcess.TrProtocol\src\UnifierServerProcess.TrProtocol\src\TrProtocol\TrProtocol.csproj", "{6874627A-27F0-ABDA-5AF1-11D28225FD1B}"
18+
EndProject
1319
Global
1420
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1521
Debug|Any CPU = Debug|Any CPU
@@ -32,11 +38,24 @@ Global
3238
{5BFD176F-57B3-711D-14AE-097425D79738}.Debug|Any CPU.Build.0 = Debug|Any CPU
3339
{5BFD176F-57B3-711D-14AE-097425D79738}.Release|Any CPU.ActiveCfg = Release|Any CPU
3440
{5BFD176F-57B3-711D-14AE-097425D79738}.Release|Any CPU.Build.0 = Release|Any CPU
41+
{524A7425-4B98-A45A-4CD0-52AEFA5B7FA2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
42+
{524A7425-4B98-A45A-4CD0-52AEFA5B7FA2}.Debug|Any CPU.Build.0 = Debug|Any CPU
43+
{524A7425-4B98-A45A-4CD0-52AEFA5B7FA2}.Release|Any CPU.ActiveCfg = Release|Any CPU
44+
{524A7425-4B98-A45A-4CD0-52AEFA5B7FA2}.Release|Any CPU.Build.0 = Release|Any CPU
45+
{6874627A-27F0-ABDA-5AF1-11D28225FD1B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
46+
{6874627A-27F0-ABDA-5AF1-11D28225FD1B}.Debug|Any CPU.Build.0 = Debug|Any CPU
47+
{6874627A-27F0-ABDA-5AF1-11D28225FD1B}.Release|Any CPU.ActiveCfg = Release|Any CPU
48+
{6874627A-27F0-ABDA-5AF1-11D28225FD1B}.Release|Any CPU.Build.0 = Release|Any CPU
3549
EndGlobalSection
3650
GlobalSection(SolutionProperties) = preSolution
3751
HideSolutionNode = FALSE
3852
EndGlobalSection
3953
GlobalSection(ExtensibilityGlobals) = postSolution
4054
SolutionGuid = {A7CFB05D-AB46-4256-92A6-C8723719BBD5}
4155
EndGlobalSection
56+
GlobalSection(SharedMSBuildProjectFiles) = preSolution
57+
..\..\UnifierServerProcess.TrProtocol\src\UnifierServerProcess.TrProtocol\src\TrProtocol.Shared\TrProtocol.Shared.projitems*{524a7425-4b98-a45a-4cd0-52aefa5b7fa2}*SharedItemsImports = 5
58+
..\..\UnifierServerProcess.TrProtocol\src\UnifierServerProcess.TrProtocol\src\TrProtocol.Shared\TrProtocol.Shared.projitems*{6874627a-27f0-abda-5af1-11d28225fd1b}*SharedItemsImports = 5
59+
..\..\UnifierServerProcess.TrProtocol\src\UnifierServerProcess.TrProtocol\src\TrProtocol.Shared\TrProtocol.Shared.projitems*{768417b4-d342-4006-b196-5f7178582209}*SharedItemsImports = 13
60+
EndGlobalSection
4261
EndGlobal

src/OTAPI.UnifiedServerProcess/Core/FunctionalFeatures/IMethodCheckCacheFeature.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,6 @@ public static bool CheckUsedContextBoundField<TFeature>(
7979
}
8080
foreach (var inst in currentCheck.Body.Instructions) {
8181
if (inst.Operand is FieldReference field) {
82-
if (field.Name == "recipe") {
83-
84-
}
8582
if (field.FieldType.FullName == Constants.RootContextFullName) {
8683
return CacheReturn(true, useCache, methodId);
8784
}
@@ -116,7 +113,13 @@ public static bool CheckUsedContextBoundField<TFeature>(
116113
if (inst.OpCode == OpCodes.Ldftn || inst.OpCode == OpCodes.Ldvirtftn) {
117114
var methodRef = (MethodReference)inst.Operand;
118115
if (methodRef.DeclaringType.Name == "<>c") {
119-
worklist.Push(methodRef.Resolve());
116+
var mDef = methodRef.Resolve();
117+
if (mDef is not null) {
118+
worklist.Push(mDef);
119+
}
120+
else {
121+
var md = new MetadataResolver(checkMethod.Module.AssemblyResolver).Resolve(methodRef);
122+
}
120123
}
121124
else if (inheritanceGraph.CheckedMethodImplementationChains.TryGetValue(methodRef.GetIdentifier(), out var implMethods)) {
122125
foreach (var implMethod in implMethods) {

src/OTAPI.UnifiedServerProcess/Core/Patching/FieldFilterPatching/InitialFieldModificationProcessor.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -676,16 +676,25 @@ bool UsedStaticField(MethodDefinition method) {
676676
return true;
677677
}
678678
}
679-
if (inst.OpCode == OpCodes.Call) {
679+
else if (inst.OpCode == OpCodes.Call) {
680680
var resolvedCallee = ((MethodReference)inst.Operand).TryResolve();
681-
if (resolvedCallee is not null
682-
&& resolvedCallee.IsStatic
683-
&& !resolvedCallee.IsConstructor
684-
&& resolvedCallee.ReturnType.FullName == source.MainModule.TypeSystem.Void.FullName) {
685-
if (visited.Add(resolvedCallee.GetIdentifier())) {
686-
stack.Push(resolvedCallee);
687-
}
681+
682+
if (resolvedCallee is null
683+
|| !resolvedCallee.IsStatic
684+
|| resolvedCallee.IsConstructor
685+
|| resolvedCallee.ReturnType.FullName != source.MainModule.TypeSystem.Void.FullName) {
686+
continue;
687+
}
688+
689+
if (!resolvedCallee.HasBody || resolvedCallee.Module.Name != method.Module.Name) {
690+
continue;
688691
}
692+
693+
if (!visited.Add(resolvedCallee.GetIdentifier())) {
694+
continue;
695+
}
696+
697+
stack.Push(resolvedCallee);
689698
}
690699
}
691700
}

0 commit comments

Comments
 (0)