Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
NetClientSocialModule.cs
Go to the documentation of this file.
1using System;
4using System.Text;
5using rail;
6using Terraria.IO;
8using Terraria.Net;
10
12
14{
15 private RailCallBackHelper _callbackHelper = new RailCallBackHelper();
16
17 private bool _hasLocalHost;
18
19 private IPCServer server = new IPCServer();
20
21 private readonly string _serverIDMedataKey = "terraria.serverid";
22
23 private RailID _inviter_id = new RailID();
24
26
28
29 private void OnIPCClientAccess()
30 {
31 WeGameHelper.WriteDebugString("IPC client access");
33 }
34
36 {
37 if (_msgServer == null)
38 {
40 _msgServer.Init("WeGame.Terraria.Message.Server");
41 _msgServer.OnMessage += OnWegameMessage;
42 _msgServer.OnIPCClientAccess += OnIPCClientAccess;
43 CoreSocialModule.OnTick += _msgServer.Tick;
45 }
46 }
47
48 private void OnWegameMessage(IPCMessage message)
49 {
50 if (message.GetCmd() == IPCMessageType.IPCMessageTypeReportServerID)
51 {
52 message.Parse<ReportServerID>(out var value);
54 }
55 }
56
63
74
76 {
77 //IL_0010: Unknown result type (might be due to invalid IL or missing references)
78 rail_api.RailFactory().RailFriends().AsyncSetInviteCommandLine(cmdline, "");
79 }
80
81 private void AsyncSetMyMetaData(string key, string value)
82 {
83 //IL_0006: Unknown result type (might be due to invalid IL or missing references)
84 //IL_000c: Expected O, but got Unknown
85 //IL_0031: Unknown result type (might be due to invalid IL or missing references)
87 RailKeyValue val = new RailKeyValue();
88 val.key = key;
89 val.value = value;
90 list.Add(val);
91 rail_api.RailFactory().RailFriends().AsyncSetMyMetadata(list, "");
92 }
93
94 private bool TryAuthUserByRecvData(RailID user, byte[] data, int length)
95 {
96 WeGameHelper.WriteDebugString("TryAuthUserByRecvData user:{0}", ((RailComparableID)user).id_);
97 if (length < 3)
98 {
99 WeGameHelper.WriteDebugString("Failed to validate authentication packet: Too short. (Length: " + length + ")");
100 return false;
101 }
102 int num = (data[1] << 8) | data[0];
103 if (num != length)
104 {
105 WeGameHelper.WriteDebugString("Failed to validate authentication packet: Packet size mismatch. (" + num + "!=" + length + ")");
106 return false;
107 }
108 if (data[2] != 93)
109 {
110 WeGameHelper.WriteDebugString("Failed to validate authentication packet: Packet type is not correct. (Type: " + data[2] + ")");
111 return false;
112 }
113 return true;
114 }
115
116 private bool OnPacketRead(byte[] data, int size, RailID user)
117 {
119 {
120 return false;
121 }
123 if (connectionState == ConnectionState.Authenticating)
124 {
125 if (!TryAuthUserByRecvData(user, data, size))
126 {
127 WeGameHelper.WriteDebugString(" Auth Server Ticket Failed");
128 Close(user);
129 }
130 else
131 {
132 WeGameHelper.WriteDebugString("OnRailAuthSessionTicket Auth Success..");
134 }
135 return false;
136 }
137 return connectionState == ConnectionState.Connected;
138 }
139
140 private void OnAuthSuccess(RailID remote_peer)
141 {
143 {
146 AsyncSetMyMetaData("status", Language.GetTextValue("Social.StatusInGame"));
148 Main.clrInput();
149 Netplay.ServerPassword = "";
150 Main.GetInputText("");
151 Main.autoPass = false;
152 Main.netMode = 1;
154 WeGameHelper.WriteDebugString("OnConnectToSocialServer server:" + ((RailComparableID)remote_peer).id_);
155 }
156 }
157
159 {
161 foreach (string text in commandLineArgs)
162 {
163 string text2 = "--rail_connect_cmd=";
164 int num = text.IndexOf(text2);
165 if (num != -1)
166 {
167 ulong result = 0uL;
168 if (ulong.TryParse(text.Substring(num + text2.Length), out result))
169 {
170 ((RailComparableID)server_id).id_ = result;
171 return true;
172 }
173 }
174 }
175 return false;
176 }
177
178 private void CheckParameters()
179 {
180 //IL_000e: Unknown result type (might be due to invalid IL or missing references)
181 //IL_0018: Expected O, but got Unknown
182 RailID server_id = new RailID();
184 {
185 return;
186 }
187 if (((RailComparableID)server_id).IsValid())
188 {
190 {
191 Main.ServerSideCharacter = false;
192 playerData.SetAsActive();
193 Main.menuMode = 882;
194 Main.statusText = Language.GetTextValue("Social.Joining");
195 WeGameHelper.WriteDebugString(" CheckParameters, lobby.join");
197 });
198 }
199 else
200 {
201 WeGameHelper.WriteDebugString("Invalid RailID passed to +connect_lobby");
202 }
203 }
204
205 public override void LaunchLocalServer(Process process, ServerMode mode)
206 {
207 //IL_00ec: Unknown result type (might be due to invalid IL or missing references)
208 if (_lobby.State != 0)
209 {
210 _lobby.Leave();
211 }
214 startInfo.Arguments = startInfo.Arguments + " -wegame -localwegameid " + ((RailComparableID)GetLocalPeer()).id_;
215 if (mode.HasFlag(ServerMode.Lobby))
216 {
217 _hasLocalHost = true;
218 if (mode.HasFlag(ServerMode.FriendsCanJoin))
219 {
220 process.StartInfo.Arguments += " -lobby friends";
221 }
222 else
223 {
224 process.StartInfo.Arguments += " -lobby private";
225 }
226 if (mode.HasFlag(ServerMode.FriendsOfFriends))
227 {
228 process.StartInfo.Arguments += " -friendsoffriends";
229 }
230 }
231 string text = default(string);
232 rail_api.RailFactory().RailUtils().GetLaunchAppParameters((EnumRailLaunchAppType)2, ref text);
234 startInfo2.Arguments = startInfo2.Arguments + " " + text;
235 WeGameHelper.WriteDebugString("LaunchLocalServer,cmd_line:" + process.StartInfo.Arguments);
236 AsyncSetMyMetaData("status", Language.GetTextValue("Social.StatusInGame"));
237 Netplay.OnDisconnect += OnDisconnect;
238 process.Start();
239 }
240
241 public override void Shutdown()
242 {
246 base.Shutdown();
247 }
248
249 public override ulong GetLobbyId()
250 {
251 return 0uL;
252 }
253
254 public override bool StartListening(SocketConnectionAccepted callback)
255 {
256 return false;
257 }
258
259 public override void StopListening()
260 {
261 }
262
263 public override void Close(RemoteAddress address)
264 {
266 RailID remote_peer = RemoteAddressToRailId(address);
268 }
269
270 public override bool CanInvite()
271 {
272 if (_hasLocalHost || _lobby.State == LobbyState.Active || Main.LobbyId != 0L)
273 {
274 return Main.netMode != 0;
275 }
276 return false;
277 }
278
279 public override void OpenInviteInterface()
280 {
282 }
283
284 private void Close(RailID remote_peer)
285 {
286 //IL_003e: Unknown result type (might be due to invalid IL or missing references)
288 {
289 WeGameHelper.WriteDebugString("CloseRemotePeer, remote:{0}", ((RailComparableID)remote_peer).id_);
290 rail_api.RailFactory().RailNetworkHelper().CloseSession(GetLocalPeer(), remote_peer);
292 _lobby.Leave();
295 }
296 }
297
298 public override void Connect(RemoteAddress address)
299 {
300 }
301
302 public override void CancelJoin()
303 {
304 if (_lobby.State != 0)
305 {
306 _lobby.Leave();
307 }
308 }
309
310 private void RegisterRailEvent()
311 {
312 //IL_0046: Unknown result type (might be due to invalid IL or missing references)
313 //IL_004d: Unknown result type (might be due to invalid IL or missing references)
314 //IL_0055: Unknown result type (might be due to invalid IL or missing references)
315 //IL_005f: Expected O, but got Unknown
316 RAILEventID[] array = (RAILEventID[])(object)new RAILEventID[7]
317 {
318 (RAILEventID)16001,
319 (RAILEventID)16002,
320 (RAILEventID)13503,
321 (RAILEventID)13501,
322 (RAILEventID)12003,
323 (RAILEventID)12002,
324 (RAILEventID)12010
325 };
326 foreach (RAILEventID val in array)
327 {
328 _callbackHelper.RegisterCallback(val, new RailEventCallBackHandler(OnRailEvent));
329 }
330 }
331
332 private void UnRegisterRailEvent()
333 {
334 _callbackHelper.UnregisterAllCallback();
335 }
336
337 public void OnRailEvent(RAILEventID id, EventBase data)
338 {
339 //IL_0038: Unknown result type (might be due to invalid IL or missing references)
340 //IL_003e: Invalid comparison between Unknown and I4
341 //IL_0059: Unknown result type (might be due to invalid IL or missing references)
342 //IL_005f: Invalid comparison between Unknown and I4
343 //IL_0040: Unknown result type (might be due to invalid IL or missing references)
344 //IL_0046: Invalid comparison between Unknown and I4
345 //IL_0072: Unknown result type (might be due to invalid IL or missing references)
346 //IL_0078: Invalid comparison between Unknown and I4
347 //IL_0061: Unknown result type (might be due to invalid IL or missing references)
348 //IL_0067: Invalid comparison between Unknown and I4
349 //IL_00b9: Unknown result type (might be due to invalid IL or missing references)
350 //IL_00c3: Expected O, but got Unknown
351 //IL_0048: Unknown result type (might be due to invalid IL or missing references)
352 //IL_004e: Invalid comparison between Unknown and I4
353 //IL_0085: Unknown result type (might be due to invalid IL or missing references)
354 //IL_008f: Expected O, but got Unknown
355 //IL_007a: Unknown result type (might be due to invalid IL or missing references)
356 //IL_0080: Invalid comparison between Unknown and I4
357 //IL_00c6: Unknown result type (might be due to invalid IL or missing references)
358 //IL_00d0: Expected O, but got Unknown
359 //IL_0069: Unknown result type (might be due to invalid IL or missing references)
360 //IL_006f: Invalid comparison between Unknown and I4
361 //IL_00ac: Unknown result type (might be due to invalid IL or missing references)
362 //IL_00b6: Expected O, but got Unknown
363 //IL_0050: Unknown result type (might be due to invalid IL or missing references)
364 //IL_0056: Invalid comparison between Unknown and I4
365 //IL_0092: Unknown result type (might be due to invalid IL or missing references)
366 //IL_009c: Expected O, but got Unknown
367 //IL_009f: Unknown result type (might be due to invalid IL or missing references)
368 //IL_00a9: Expected O, but got Unknown
369 //IL_00d3: Unknown result type (might be due to invalid IL or missing references)
370 //IL_00dd: Expected O, but got Unknown
371 WeGameHelper.WriteDebugString("OnRailEvent,id=" + ((object)(RAILEventID)(ref id)).ToString() + " ,result=" + ((object)(RailResult)(ref data.result)).ToString());
372 if ((int)id <= 12010)
373 {
374 if ((int)id != 12002)
375 {
376 if ((int)id != 12003)
377 {
378 if ((int)id == 12010)
379 {
381 }
382 }
383 else
384 {
386 }
387 }
388 else
389 {
391 }
392 }
393 else if ((int)id <= 13503)
394 {
395 if ((int)id != 13501)
396 {
397 if ((int)id == 13503)
398 {
400 }
401 }
402 else
403 {
405 }
406 }
407 else if ((int)id != 16001)
408 {
409 if ((int)id == 16002)
410 {
412 }
413 }
414 else
415 {
417 }
418 }
419
421 {
423 foreach (RailKeyValueResult item in list)
424 {
425 stringBuilder.Append("key: " + item.key + " value: " + item.value);
426 }
427 return stringBuilder.ToString();
428 }
429
431 {
432 string result = null;
433 foreach (RailKeyValueResult item in list)
434 {
435 if (item.key == key)
436 {
437 result = item.value;
438 break;
439 }
440 }
441 return result;
442 }
443
445 {
446 bool result = false;
447 if (_hasLocalHost)
448 {
451 {
453 {
454 _friendList = list
455 };
457 iPCMessage.Build(IPCMessageType.IPCMessageTypeNotifyFriendList, t);
459 WeGameHelper.WriteDebugString("NotifyFriendListToServer: " + result);
460 }
461 }
462 return result;
463 }
464
466 {
467 //IL_0012: Unknown result type (might be due to invalid IL or missing references)
468 //IL_0018: Invalid comparison between Unknown and I4
469 bool result = false;
470 IRailFriends val = rail_api.RailFactory().RailFriends();
471 if (val != null)
472 {
473 result = (int)val.GetFriendsList(list) == 0;
474 }
475 return result;
476 }
477
479 {
480 //IL_0001: Unknown result type (might be due to invalid IL or missing references)
481 //IL_005c: Unknown result type (might be due to invalid IL or missing references)
482 //IL_0062: Expected O, but got Unknown
483 if ((int)((EventBase)data).result != 0 || data.friend_kvs.Count <= 0)
484 {
485 return;
486 }
487 WeGameHelper.WriteDebugString("OnGetFriendMetaData - " + DumpMataDataString(data.friend_kvs));
488 string valueByKey = GetValueByKey(_serverIDMedataKey, data.friend_kvs);
489 if (valueByKey == null)
490 {
491 return;
492 }
493 if (valueByKey.Length > 0)
494 {
495 RailID val = new RailID();
496 ((RailComparableID)val).id_ = ulong.Parse(valueByKey);
497 if (((RailComparableID)val).IsValid())
498 {
499 JoinServer(val);
500 }
501 else
502 {
503 WeGameHelper.WriteDebugString("JoinServer failed, invalid server id");
504 }
505 }
506 else
507 {
508 WeGameHelper.WriteDebugString("can not find server id key");
509 }
510 }
511
512 private void JoinServer(RailID server_id)
513 {
514 //IL_0064: Unknown result type (might be due to invalid IL or missing references)
517 int num = 3;
518 byte[] array = new byte[num];
519 array[0] = (byte)((uint)num & 0xFFu);
520 array[1] = (byte)((uint)(num >> 8) & 0xFFu);
521 array[2] = 93;
522 rail_api.RailFactory().RailNetworkHelper().SendReliableData(GetLocalPeer(), server_id, array, (uint)num);
523 }
524
525 private string GetFriendNickname(RailID rail_id)
526 {
527 if (_player_info_list != null)
528 {
530 {
531 if ((RailComparableID)(object)item.rail_id == (RailComparableID)(object)rail_id)
532 {
533 return item.rail_name;
534 }
535 }
536 }
537 return "";
538 }
539
541 {
542 _player_info_list = data.user_info_list;
543 }
544
546 {
547 if (_hasLocalHost)
548 {
550 }
551 }
552
553 private void AsyncGetFriendsInfo()
554 {
555 //IL_0016: Unknown result type (might be due to invalid IL or missing references)
556 //IL_0061: Unknown result type (might be due to invalid IL or missing references)
557 IRailFriends val = rail_api.RailFactory().RailFriends();
558 if (val == null)
559 {
560 return;
561 }
563 val.GetFriendsList(list);
565 foreach (RailFriendInfo item in list)
566 {
567 list2.Add(item.friend_rail_id);
568 }
569 val.AsyncGetPersonalInfo(list2, "");
570 }
571
572 private void AsyncSetPlayWith(RailID rail_id)
573 {
574 //IL_0006: Unknown result type (might be due to invalid IL or missing references)
575 //IL_000c: Expected O, but got Unknown
576 //IL_002f: Unknown result type (might be due to invalid IL or missing references)
579 val.rail_id = rail_id;
580 list.Add(val);
581 IRailFriends val2 = rail_api.RailFactory().RailFriends();
582 if (val2 != null)
583 {
584 val2.AsyncReportPlayedWithUserList(list, "");
585 }
586 }
587
589 {
590 WeGameHelper.WriteDebugString("OnRailSetMetaData - " + ((object)(RailResult)(ref ((EventBase)data).result)).ToString());
591 }
592
594 {
595 WeGameHelper.WriteDebugString(" request join game");
596 if (_lobby.State != 0)
597 {
598 _lobby.Leave();
599 }
600 _inviter_id = data.inviter_id;
602 {
603 Main.ServerSideCharacter = false;
604 playerData.SetAsActive();
605 Main.menuMode = 882;
606 Main.statusText = Language.GetTextValue("Social.JoiningFriend", GetFriendNickname(data.inviter_id));
607 AsyncGetServerIDByOwener(data.inviter_id);
608 WeGameHelper.WriteDebugString("inviter_id: " + ((RailComparableID)data.inviter_id).id_);
609 });
610 }
611
613 {
614 //IL_0028: Unknown result type (might be due to invalid IL or missing references)
617 IRailFriends val = rail_api.RailFactory().RailFriends();
618 if (val != null)
619 {
620 val.AsyncGetFriendMetadata(ownerID, list, "");
621 }
622 }
623
625 {
626 //IL_0082: Unknown result type (might be due to invalid IL or missing references)
627 WeGameHelper.WriteDebugString("OnRailCreateSessionRequest");
628 if (_connectionStateMap.ContainsKey(result.remote_peer) && _connectionStateMap[result.remote_peer] != 0)
629 {
630 WeGameHelper.WriteDebugString("AcceptSessionRequest, local{0}, remote:{1}", ((RailComparableID)result.local_peer).id_, ((RailComparableID)result.remote_peer).id_);
631 rail_api.RailFactory().RailNetworkHelper().AcceptSessionRequest(result.local_peer, result.remote_peer);
632 }
633 }
634
636 {
637 WeGameHelper.WriteDebugString("OnRailCreateSessionFailed, CloseRemote: local:{0}, remote:{1}", ((RailComparableID)result.local_peer).id_, ((RailComparableID)result.remote_peer).id_);
638 Close(result.remote_peer);
639 }
640
641 private void CleanMyMetaData()
642 {
643 //IL_0014: Unknown result type (might be due to invalid IL or missing references)
644 IRailFriends val = rail_api.RailFactory().RailFriends();
645 if (val != null)
646 {
647 val.AsyncClearAllMyMetadata("");
648 }
649 }
650
651 private void OnDisconnect()
652 {
654 _hasLocalHost = false;
655 Netplay.OnDisconnect -= OnDisconnect;
656 }
657}
void Add(TKey key, TValue value)
static string[] GetCommandLineArgs()
static string GetTextValue(string key)
Definition Language.cs:15
static string GetInputText(string oldString, bool allowMultiLine=false)
Definition Main.cs:18593
static void OpenPlayerSelect(OnPlayerSelected method)
Definition Main.cs:63193
static void clrInput()
Definition Main.cs:18584
static ulong LobbyId
Definition Main.cs:389
static void OnConnectedToSocialServer(ISocket client)
Definition Netplay.cs:208
void OnGetFriendMetaData(RailFriendsGetMetadataResult data)
void OnRailCreateSessionFailed(CreateSessionFailed result)
string DumpMataDataString(List< RailKeyValueResult > list)
void OnRailEvent(RAILEventID id, EventBase data)
override bool StartListening(SocketConnectionAccepted callback)
void OnRailCreateSessionRequest(CreateSessionRequest result)
bool GetRailFriendList(List< RailFriendInfo > list)
override void Connect(RemoteAddress address)
void OnReportServerID(ReportServerID reportServerID)
void OnFriendlistChange(RailFriendsListChanged data)
bool OnPacketRead(byte[] data, int size, RailID user)
override void Close(RemoteAddress address)
override void LaunchLocalServer(Process process, ServerMode mode)
void AsyncSetMyMetaData(string key, string value)
void OnRailRespondInvation(RailUsersRespondInvitation data)
string GetValueByKey(string key, List< RailKeyValueResult > list)
bool TryAuthUserByRecvData(RailID user, byte[] data, int length)
void OnRailSetMetaData(RailFriendsSetMetadataResult data)
RailID RemoteAddressToRailId(RemoteAddress address)
ConcurrentDictionary< RailID, ConnectionState > _connectionStateMap
static void WriteDebugString(string format, params object[] args)
void SetReadEvent(OnReadEvent method)