-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dimlibs.cs
317 lines (276 loc) · 10.4 KB
/
Dimlibs.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
using Dimlibs.API;
using Dimlibs.Chunks;
using Dimlibs.UI;
using log4net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Reflection;
using System.Threading;
using Dimlibs.API.ReflectionUtils;
using Dimlibs.Network;
using Mono.Cecil;
using Mono.Cecil.Cil;
using ReLogic.OS;
using Terraria;
using Terraria.Localization;
using Terraria.ModLoader;
using Terraria.Net;
using Terraria.Net.Sockets;
using Terraria.Utilities;
namespace Dimlibs
{
public class Dimlibs : Mod
{
private readonly string previousWorldPath;
internal static IList<ModCommand> commandsList = new List<ModCommand>();
internal static Dimlibs Instance;
internal static readonly IDictionary<String, ModDimension> dimensionInstanceHandlers = new Dictionary<String, ModDimension>();
internal static readonly List<DimensionServer> server = new List<DimensionServer>();
public static string dimension = "Dimlibs:OverworldDimension";
public static bool isActive;
public static bool isLan = true;
public override void Load()
{
/*if (!Environment.Is64BitProcess && Main.netMode != 2)
{
throw new Exception("Dimension library might or might not take a huge amount of RAM, it is recommended to download the 64bit version the game to run it\n" +
"The 64bit version got some special patch to allow multiple instance of static thing without having problem\n" +
"To download it, visit the 64bit discord https://discord.gg/DY8cx5T");
}*/
if (Program.LaunchParameters.TryGetValue("-dimension", out string dim))
{
dimension = dim;
}
if (Main.netMode == 2)
Console.Title = "Terraria Project Dimension : " + Dimlibs.dimension + " dimension";
foreach (string launchParametersKey in Program.LaunchParameters.Keys)
{
Logger.Info($"{launchParametersKey} : {Program.LaunchParameters[launchParametersKey]}");
}
Instance = this;
ReflectionUtilities.Load();
GetDimLibsCommand();
for (int i = 0; i < ModLoader.Mods.Length; i++)
{
Mod mod = ModLoader.Mods[i];
try
{
Autoload(mod);
}
catch (Exception e)
{
}
}
if (Main.netMode == 2)
{
On.Terraria.WorldGen.UpdateWorld += WorldUpdate;
}
}
public override void Unload()
{
ReflectionUtilities.Unload();
ChatServer.instance.Unload();
On.Terraria.WorldGen.UpdateWorld -= WorldUpdate;
}
public override void PostSetupContent()
{
// LoadModContent(Autoload);
for (int i = 0; i < ModLoader.Mods.Length; i++)
{
Mod mod = ModLoader.Mods[i];
try
{
Autoload(mod);
}
catch
{
mod.Logger.InfoFormat("Failure to autoload dimensions for mod {0}", mod.DisplayName);
}
}
}
internal void Autoload(Mod mod)
{
if (mod.Code == null)
{
return;
}
TypeInfo[] array = mod.Code.DefinedTypes.OrderBy(type => type.FullName)
.ToArray();
foreach (Type type in mod.Code.GetTypes().OrderBy(type => type.FullName, StringComparer.InvariantCulture))
{
if (type.IsSubclassOf(typeof(ModDimension)))
{
mod.AutoLoadDimension(type);
}
}
}
public override bool HijackGetData(ref byte messageType, ref BinaryReader reader, int playerNumber)
{
if (Main.netMode == 2)
{
if (messageType == 1)
{
if (Main.netMode != 2)
{
return false;
}
if (Netplay.Clients[playerNumber].State != 0)
{
return false;
}
RemoteAddress address = Netplay.Clients[playerNumber].Socket.GetRemoteAddress();
if (isLan && !address.IsLocalHost())
{
NetMessage.SendData(2, playerNumber, -1, NetworkText.FromLiteral("The server you are trying to join is a LAN world."));
return true;
}
}
}
return base.HijackGetData(ref messageType, ref reader, playerNumber);
}
private void GetDimLibsCommand()
{
FieldInfo commandListInfo =
typeof(CommandManager).GetField("Commands", BindingFlags.Static | BindingFlags.NonPublic);
Dictionary<String, List<ModCommand>> tempDictionary = (Dictionary<string, List<ModCommand>>)commandListInfo.GetValue(null);
Dictionary<string, List<ModCommand>>.ValueCollection a = tempDictionary.Values;
foreach (var modCommandList in a)
{
foreach (var modCommand in modCommandList)
{
if (modCommand.mod.Name == Name)
{
commandsList.Add(modCommand);
}
}
}
}
private void ServerLoop(object context)
{
TcpListener listener = context as TcpListener;
TcpClient client = listener.AcceptTcpClient();
TcpSocket terrariaSocket = new TcpSocket(client);
ISocket socket = terrariaSocket as ISocket;
Console.WriteLine("server loop started");
RemoteServer server = new RemoteServer();
server.Socket = terrariaSocket;
while (false)
{
try
{
if (socket.IsConnected())
{
if (socket.IsDataAvailable())
{
client.GetStream().Flush();
byte[] data = new byte[ushort.MaxValue];
using (MemoryStream stream = new MemoryStream(data))
{
BinaryWriter writer = new BinaryWriter(stream);
socket.AsyncSend(writer.BaseStream.ReadAllBytes(), 0, 256, new SocketSendCallback(Netplay.Connection.ClientWriteCallBack), null);
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
listener.Stop();
Console.WriteLine("server loop stopped");
}
#region Client
public void ClientThread(object context)
{
Main.gameMenu = true;
Main.menuMode = 888;
Main.MenuUI.SetState(new UINetworkConnection());
object[] parameter = (object[])context;
bool exitThread = false;
DimPlayer player = (DimPlayer)parameter[0];
int numberOfAttempt = 0;
RemoteAddress adress = new TcpAddress(Netplay.ServerIP, 7776);
ClientLoopSetup(adress);
ISocket secondarySocket = new TcpSocket();
secondarySocket.Connect(new TcpAddress(Netplay.ServerIP, 7776));
while (!exitThread)
{
try
{
Thread.Sleep(2500);
if (secondarySocket.IsDataAvailable())
{
byte[] data = new byte[ushort.MaxValue];
secondarySocket.AsyncReceive(data, 0, ushort.MaxValue, new SocketReceiveCallback(Netplay.Connection.ClientReadCallBack), null);
using (MemoryStream stream = new MemoryStream(data))
{
BinaryReader reader = new BinaryReader(new MemoryStream(data));
}
numberOfAttempt++;
}
else
{
byte[] data = new byte[ushort.MaxValue];
using (MemoryStream stream = new MemoryStream(data))
{
BinaryWriter writer = new BinaryWriter(stream);
writer.Write("hey");
secondarySocket.AsyncSend(writer.BaseStream.ReadAllBytes(), 0, ushort.MaxValue, new SocketSendCallback(Netplay.Connection.ClientWriteCallBack), null);
}
}
}
catch (Exception e)
{
LogManager.GetLogger("Second thread").Error(e.Message, e);
}
}
Netplay.Connection.Socket.Close();
Netplay.StartTcpClient();
player.inTransit = false;
}
private static void ClientLoopSetup(RemoteAddress address)
{
LogManager.GetLogger("Dimension Library Server").InfoFormat("Connecting to {0}", address.GetFriendlyName());
Netplay.ResetNetDiag();
Main.ServerSideCharacter = false;
if (Main.rand == null)
{
Main.rand = new UnifiedRandom((int)DateTime.Now.Ticks);
}
Main.player[Main.myPlayer].hostile = false;
Main.clientPlayer = (Player)Main.player[Main.myPlayer].clientClone();
for (int i = 0; i < 255; i++)
{
if (i != Main.myPlayer)
{
Main.player[i] = new Player();
}
}
Main.netMode = 1;
Netplay.disconnect = false;
Netplay.Connection = new RemoteServer();
Netplay.Connection.ReadBuffer = new byte[ushort.MaxValue];
}
#endregion
public override void MidUpdateTimeWorld()
{
if (Main.netMode == 2)
Console.Title = "Terraria Project Dimension : " + Dimlibs.dimension + " dimension";
}
public static void WorldUpdate(On.Terraria.WorldGen.orig_UpdateWorld orig)
{
if (Main.netMode == 2)
Console.Title = "Terraria Project Dimension : " + Dimlibs.dimension + " dimension";
isActive = Netplay.anyClients;
if (isActive)
{
orig.Invoke();
}
}
}
}