Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
NetClientSocialModule.cs
Go to the documentation of this file.
2using Steamworks;
3using Terraria.IO;
5using Terraria.Net;
8
10
12{
13 private Callback<GameLobbyJoinRequested_t> _gameLobbyJoinRequested;
14
15 private Callback<P2PSessionRequest_t> _p2pSessionRequest;
16
17 private Callback<P2PSessionConnectFail_t> _p2pSessionConnectfail;
18
19 private HAuthTicket _authTicket = HAuthTicket.Invalid;
20
21 private byte[] _authData = new byte[1021];
22
23 private uint _authDataLength;
24
25 private bool _hasLocalHost;
26
28 : base(2, 1)
29 {
30 }//IL_0001: Unknown result type (might be due to invalid IL or missing references)
31 //IL_0006: Unknown result type (might be due to invalid IL or missing references)
32
33
34 public override void Initialize()
35 {
36 base.Initialize();
37 _gameLobbyJoinRequested = Callback<GameLobbyJoinRequested_t>.Create((DispatchDelegate<GameLobbyJoinRequested_t>)OnLobbyJoinRequest);
38 _p2pSessionRequest = Callback<P2PSessionRequest_t>.Create((DispatchDelegate<P2PSessionRequest_t>)OnP2PSessionRequest);
39 _p2pSessionConnectfail = Callback<P2PSessionConnectFail_t>.Create((DispatchDelegate<P2PSessionConnectFail_t>)OnSessionConnectFail);
40 Main.OnEngineLoad += CheckParameters;
41 }
42
43 private void CheckParameters()
44 {
45 if (Program.LaunchParameters.ContainsKey("+connect_lobby") && ulong.TryParse(Program.LaunchParameters["+connect_lobby"], out var result))
46 {
47 ConnectToLobby(result);
48 }
49 }
50
51 public void ConnectToLobby(ulong lobbyId)
52 {
53 //IL_000f: Unknown result type (might be due to invalid IL or missing references)
54 //IL_0014: Unknown result type (might be due to invalid IL or missing references)
55 CSteamID lobbySteamId = new CSteamID(lobbyId);
56 if (((CSteamID)(ref lobbySteamId)).IsValid())
57 {
58 Main.OpenPlayerSelect(delegate(PlayerFileData playerData)
59 {
60 //IL_0041: Unknown result type (might be due to invalid IL or missing references)
61 Main.ServerSideCharacter = false;
62 playerData.SetAsActive();
63 Main.menuMode = 882;
64 Main.statusText = Language.GetTextValue("Social.Joining");
65 WeGameHelper.WriteDebugString(" CheckParameters, lobby.join");
66 _lobby.Join(lobbySteamId, OnLobbyEntered);
67 });
68 }
69 }
70
71 public override void LaunchLocalServer(Process process, ServerMode mode)
72 {
73 //IL_0039: Unknown result type (might be due to invalid IL or missing references)
74 WeGameHelper.WriteDebugString("LaunchLocalServer");
75 if (_lobby.State != 0)
76 {
77 _lobby.Leave();
78 }
79 ProcessStartInfo startInfo = process.StartInfo;
80 startInfo.Arguments = startInfo.Arguments + " -steam -localsteamid " + SteamUser.GetSteamID().m_SteamID;
81 if (mode.HasFlag(ServerMode.Lobby))
82 {
83 _hasLocalHost = true;
84 if (mode.HasFlag(ServerMode.FriendsCanJoin))
85 {
86 process.StartInfo.Arguments += " -lobby friends";
87 }
88 else
89 {
90 process.StartInfo.Arguments += " -lobby private";
91 }
92 if (mode.HasFlag(ServerMode.FriendsOfFriends))
93 {
94 process.StartInfo.Arguments += " -friendsoffriends";
95 }
96 }
97 SteamFriends.SetRichPresence("status", Language.GetTextValue("Social.StatusInGame"));
98 Netplay.OnDisconnect += OnDisconnect;
99 process.Start();
100 }
101
102 public override ulong GetLobbyId()
103 {
104 return 0uL;
105 }
106
107 public override bool StartListening(SocketConnectionAccepted callback)
108 {
109 return false;
110 }
111
112 public override void StopListening()
113 {
114 }
115
116 public override void Close(RemoteAddress address)
117 {
118 //IL_0007: Unknown result type (might be due to invalid IL or missing references)
119 //IL_000c: Unknown result type (might be due to invalid IL or missing references)
120 //IL_000e: Unknown result type (might be due to invalid IL or missing references)
121 SteamFriends.ClearRichPresence();
122 CSteamID user = RemoteAddressToSteamId(address);
123 Close(user);
124 }
125
126 public override bool CanInvite()
127 {
128 if (_hasLocalHost || _lobby.State == LobbyState.Active || Main.LobbyId != 0L)
129 {
130 return Main.netMode != 0;
131 }
132 return false;
133 }
134
135 public override void OpenInviteInterface()
136 {
138 }
139
140 private void Close(CSteamID user)
141 {
142 //IL_0006: Unknown result type (might be due to invalid IL or missing references)
143 //IL_000f: Unknown result type (might be due to invalid IL or missing references)
144 //IL_0022: Unknown result type (might be due to invalid IL or missing references)
145 //IL_003a: Unknown result type (might be due to invalid IL or missing references)
146 //IL_0046: Unknown result type (might be due to invalid IL or missing references)
148 {
149 SteamNetworking.CloseP2PSessionWithUser(user);
151 _connectionStateMap[user] = ConnectionState.Inactive;
152 _lobby.Leave();
153 _reader.ClearUser(user);
154 _writer.ClearUser(user);
155 }
156 }
157
158 public override void Connect(RemoteAddress address)
159 {
160 }
161
162 public override void CancelJoin()
163 {
164 if (_lobby.State != 0)
165 {
166 _lobby.Leave();
167 }
168 }
169
170 private void OnLobbyJoinRequest(GameLobbyJoinRequested_t result)
171 {
172 //IL_000e: Unknown result type (might be due to invalid IL or missing references)
173 //IL_000f: Unknown result type (might be due to invalid IL or missing references)
174 //IL_0043: Unknown result type (might be due to invalid IL or missing references)
175 WeGameHelper.WriteDebugString(" OnLobbyJoinRequest");
176 if (_lobby.State != 0)
177 {
178 _lobby.Leave();
179 }
180 string friendName = SteamFriends.GetFriendPersonaName(result.m_steamIDFriend);
182 {
183 Main.OpenPlayerSelect(delegate(PlayerFileData playerData)
184 {
185 //IL_003c: Unknown result type (might be due to invalid IL or missing references)
186 Main.ServerSideCharacter = false;
187 playerData.SetAsActive();
188 Main.menuMode = 882;
189 Main.statusText = Language.GetTextValue("Social.JoiningFriend", friendName);
190 _lobby.Join(result.m_steamIDLobby, OnLobbyEntered);
191 });
192 });
193 }
194
195 private void OnLobbyEntered(LobbyEnter_t result, bool failure)
196 {
197 //IL_001e: Unknown result type (might be due to invalid IL or missing references)
198 //IL_00a4: Unknown result type (might be due to invalid IL or missing references)
199 //IL_00ca: Unknown result type (might be due to invalid IL or missing references)
200 //IL_00db: Unknown result type (might be due to invalid IL or missing references)
201 //IL_0127: Unknown result type (might be due to invalid IL or missing references)
202 //IL_00b2: Unknown result type (might be due to invalid IL or missing references)
203 //IL_002c: Unknown result type (might be due to invalid IL or missing references)
204 //IL_0082: Unknown result type (might be due to invalid IL or missing references)
205 //IL_0094: Unknown result type (might be due to invalid IL or missing references)
206 WeGameHelper.WriteDebugString(" OnLobbyEntered");
207 SteamNetworking.AllowP2PPacketRelay(true);
209 int num = 0;
210 P2PSessionState_t val = default(P2PSessionState_t);
211 while (SteamNetworking.GetP2PSessionState(_lobby.Owner, ref val) && val.m_bConnectionActive != 1)
212 {
213 switch (val.m_eP2PSessionError)
214 {
215 case 2:
217 return;
218 case 1:
220 return;
221 case 3:
223 return;
224 case 5:
226 return;
227 case 4:
228 if (++num > 5)
229 {
231 return;
232 }
233 SteamNetworking.CloseP2PSessionWithUser(_lobby.Owner);
235 break;
236 }
237 }
239 SteamFriends.SetPlayedWith(_lobby.Owner);
240 SteamFriends.SetRichPresence("status", Language.GetTextValue("Social.StatusInGame"));
241 Main.clrInput();
242 Netplay.ServerPassword = "";
243 Main.GetInputText("");
244 Main.autoPass = false;
245 Main.netMode = 1;
247 }
248
249 private void SendAuthTicket(CSteamID address)
250 {
251 //IL_0011: Unknown result type (might be due to invalid IL or missing references)
252 //IL_0016: Unknown result type (might be due to invalid IL or missing references)
253 //IL_0037: Unknown result type (might be due to invalid IL or missing references)
254 //IL_003c: Unknown result type (might be due to invalid IL or missing references)
255 //IL_008e: Unknown result type (might be due to invalid IL or missing references)
256 WeGameHelper.WriteDebugString(" SendAuthTicket");
257 if (_authTicket == HAuthTicket.Invalid)
258 {
259 _authTicket = SteamUser.GetAuthSessionTicket(_authData, _authData.Length, ref _authDataLength);
260 }
261 int num = (int)(_authDataLength + 3);
262 byte[] array = new byte[num];
263 array[0] = (byte)((uint)num & 0xFFu);
264 array[1] = (byte)((uint)(num >> 8) & 0xFFu);
265 array[2] = 93;
266 for (int i = 0; i < _authDataLength; i++)
267 {
268 array[i + 3] = _authData[i];
269 }
270 SteamNetworking.SendP2PPacket(address, array, (uint)num, (EP2PSend)2, 1);
271 }
272
273 private void ClearAuthTicket()
274 {
275 //IL_0001: Unknown result type (might be due to invalid IL or missing references)
276 //IL_0006: Unknown result type (might be due to invalid IL or missing references)
277 //IL_001e: Unknown result type (might be due to invalid IL or missing references)
278 //IL_0023: Unknown result type (might be due to invalid IL or missing references)
279 //IL_0013: Unknown result type (might be due to invalid IL or missing references)
280 if (_authTicket != HAuthTicket.Invalid)
281 {
282 SteamUser.CancelAuthTicket(_authTicket);
283 }
284 _authTicket = HAuthTicket.Invalid;
285 for (int i = 0; i < _authData.Length; i++)
286 {
287 _authData[i] = 0;
288 }
289 _authDataLength = 0u;
290 }
291
292 private void OnDisconnect()
293 {
294 SteamFriends.ClearRichPresence();
295 _hasLocalHost = false;
296 Netplay.OnDisconnect -= OnDisconnect;
297 }
298
299 private void OnSessionConnectFail(P2PSessionConnectFail_t result)
300 {
301 //IL_0011: Unknown result type (might be due to invalid IL or missing references)
302 //IL_0012: Unknown result type (might be due to invalid IL or missing references)
303 WeGameHelper.WriteDebugString(" OnSessionConnectFail");
304 Close(result.m_steamIDRemote);
305 }
306
307 private void OnP2PSessionRequest(P2PSessionRequest_t result)
308 {
309 //IL_0010: Unknown result type (might be due to invalid IL or missing references)
310 //IL_0011: Unknown result type (might be due to invalid IL or missing references)
311 //IL_0016: Unknown result type (might be due to invalid IL or missing references)
312 //IL_001d: Unknown result type (might be due to invalid IL or missing references)
313 //IL_002b: Unknown result type (might be due to invalid IL or missing references)
314 //IL_0033: Unknown result type (might be due to invalid IL or missing references)
315 WeGameHelper.WriteDebugString(" OnP2PSessionRequest");
316 CSteamID steamIDRemote = result.m_steamIDRemote;
317 if (_connectionStateMap.ContainsKey(steamIDRemote) && _connectionStateMap[steamIDRemote] != 0)
318 {
319 SteamNetworking.AcceptP2PSessionWithUser(steamIDRemote);
320 }
321 }
322}
ProcessStartInfo StartInfo
Definition Process.cs:556
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 QueueMainThreadAction(Action action)
Definition Main.cs:15582
static void OnConnectedToSocialServer(ISocket client)
Definition Netplay.cs:208
static Dictionary< string, string > LaunchParameters
Definition Program.cs:29
void Join(CSteamID lobbyId, APIDispatchDelegate< LobbyEnter_t > callResult)
Definition Lobby.cs:62
Callback< P2PSessionRequest_t > _p2pSessionRequest
void OnLobbyEntered(LobbyEnter_t result, bool failure)
void OnP2PSessionRequest(P2PSessionRequest_t result)
override void Connect(RemoteAddress address)
override void Close(RemoteAddress address)
override bool StartListening(SocketConnectionAccepted callback)
void OnLobbyJoinRequest(GameLobbyJoinRequested_t result)
void OnSessionConnectFail(P2PSessionConnectFail_t result)
Callback< GameLobbyJoinRequested_t > _gameLobbyJoinRequested
override void LaunchLocalServer(Process process, ServerMode mode)
Callback< P2PSessionConnectFail_t > _p2pSessionConnectfail
CSteamID RemoteAddressToSteamId(RemoteAddress address)
ConcurrentDictionary< CSteamID, ConnectionState > _connectionStateMap
static void WriteDebugString(string format, params object[] args)