Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
GameSenseConnection.cs
Go to the documentation of this file.
1using System;
3using System.IO;
4using System.Net;
5using System.Text;
7using FullSerializer;
8using Newtonsoft.Json.Linq;
9using ReLogic.OS;
10using SteelSeries.GameSense;
11
13
15{
16 public delegate void ClientStateEvent();
17
18 public string GameName;
19
20 public string GameDisplayName;
21
22 public IconColor IconColor;
23
24 private Bind_Event[] Events;
25
26 private const string _SceneObjName = "GameSenseManager_Auto";
27
28 private const string _GameSenseObjName = "GameSenseManager";
29
30 private const uint _MsgQueueSize = 100u;
31
32 private const int _ServerProbeInterval = 5000;
33
34 private const int _MsgCheckInterval = 10;
35
36 private const long _MaxIdleTimeBeforeHeartbeat = 1000L;
37
39
41
42 private Uri _uriBase;
43
44 private LocklessQueue<QueueMsg> _mMsgQueue;
45
46 private ClientState _mClientState;
47
48 private string _mServerPort;
49
50 private fsSerializer _mSerializer;
51
52 public ClientStateEvent OnConnectionBecameActive;
53
54 public ClientStateEvent OnConnectionBecameInactive;
55
57
58 public void SetEvents(params Bind_Event[] bindEvents)
59 {
60 Events = bindEvents;
61 }
62
63 public void BeginConnection()
64 {
65 //IL_0001: Unknown result type (might be due to invalid IL or missing references)
66 //IL_000b: Expected O, but got Unknown
67 _mSerializer = new fsSerializer();
68 _mMsgQueue = new LocklessQueue<QueueMsg>(100u);
71 _setClientState((ClientState)1);
72 try
73 {
76 }
77 catch (Exception e)
78 {
79 _logException("Could not start the client thread", e);
80 _setClientState((ClientState)2);
81 }
82 }
83
84 private void _addGUIDefinedEvents()
85 {
86 //IL_0038: Unknown result type (might be due to invalid IL or missing references)
87 if (GameName == null || GameDisplayName == null || Events == null)
88 {
89 _logWarning("Incomplete game registration form");
90 _setClientState((ClientState)2);
91 }
92 else
93 {
95 RegisterEvents(Events);
96 }
97 }
98
99 public void TryRegisteringEvents(Bind_Event[] theEvents)
100 {
101 RegisterEvents(theEvents);
102 }
103
104 private void RegisterEvents(Bind_Event[] theEvents)
105 {
106 //IL_001b: Unknown result type (might be due to invalid IL or missing references)
107 //IL_0021: Expected O, but got Unknown
108 //IL_003b: Unknown result type (might be due to invalid IL or missing references)
109 //IL_0040: Unknown result type (might be due to invalid IL or missing references)
110 //IL_004a: Expected O, but got Unknown
111 //IL_0058: Unknown result type (might be due to invalid IL or missing references)
112 //IL_005e: Expected O, but got Unknown
113 foreach (Bind_Event bind_Event in theEvents)
114 {
115 QueueMsg queueMsg;
116 if (bind_Event.handlers == null || bind_Event.handlers.Length == 0)
117 {
118 queueMsg = (QueueMsg)new QueueMsgRegisterEvent();
119 queueMsg.data = (object)new Register_Event(GameName, bind_Event.eventName, bind_Event.minValue, bind_Event.maxValue, bind_Event.iconId);
120 }
121 else
122 {
123 bind_Event.game = GameName;
124 queueMsg = (QueueMsg)new QueueMsgBindEvent();
125 queueMsg.data = bind_Event;
126 }
127 _mMsgQueue.PEnqueue(queueMsg);
128 }
129 }
130
131 public void RegisterGame(string name, string displayName, IconColor iconColor)
132 {
133 //IL_0014: Unknown result type (might be due to invalid IL or missing references)
134 //IL_0015: Unknown result type (might be due to invalid IL or missing references)
135 //IL_001a: Unknown result type (might be due to invalid IL or missing references)
136 //IL_0020: Expected O, but got Unknown
137 //IL_0039: Unknown result type (might be due to invalid IL or missing references)
138 //IL_003a: Unknown result type (might be due to invalid IL or missing references)
139 //IL_003f: Unknown result type (might be due to invalid IL or missing references)
140 //IL_0045: Expected O, but got Unknown
141 GameName = name.ToUpper();
142 GameDisplayName = displayName;
143 IconColor = iconColor;
144 Register_Game register_Game = new Register_Game();
145 register_Game.game = GameName;
146 register_Game.game_display_name = GameDisplayName;
147 register_Game.icon_color_id = iconColor;
148 QueueMsgRegisterGame queueMsgRegisterGame = new QueueMsgRegisterGame();
149 ((QueueMsg)queueMsgRegisterGame).data = register_Game;
150 _mMsgQueue.PEnqueue((QueueMsg)(object)queueMsgRegisterGame);
151 }
152
153 public void RemoveGame(string name)
154 {
155 //IL_000c: Unknown result type (might be due to invalid IL or missing references)
156 //IL_0012: Expected O, but got Unknown
157 //IL_0019: Unknown result type (might be due to invalid IL or missing references)
158 //IL_0023: Expected O, but got Unknown
159 GameName = name.ToUpper();
160 QueueMsgRemoveGame queueMsgRemoveGame = new QueueMsgRemoveGame();
161 ((QueueMsg)queueMsgRemoveGame).data = (object)new Game(GameName);
162 _mMsgQueue.PEnqueue((QueueMsg)(object)queueMsgRemoveGame);
163 }
164
165 public void EndConnection()
166 {
167 _logDbgMsg("Ending Connection");
169 if (_currentRequest != null)
170 {
172 }
173 }
174
175 private void _gamesenseWrk()
176 {
177 //IL_0089: Expected O, but got Unknown
178 //IL_00a5: Expected O, but got Unknown
179 //IL_010c: Expected O, but got Unknown
180 //IL_0014: Unknown result type (might be due to invalid IL or missing references)
181 //IL_0019: Unknown result type (might be due to invalid IL or missing references)
182 //IL_001a: Unknown result type (might be due to invalid IL or missing references)
183 //IL_002c: Expected I4, but got Unknown
184 //IL_0046: Unknown result type (might be due to invalid IL or missing references)
185 //IL_004c: Expected O, but got Unknown
186 //IL_0053: Unknown result type (might be due to invalid IL or missing references)
187 //IL_005d: Expected O, but got Unknown
188 QueueMsg queueMsg = null;
189 Stopwatch stopwatch = new Stopwatch();
190 stopwatch.Start();
192 {
193 ClientState mClientState = _mClientState;
194 switch ((int)mClientState)
195 {
196 case 0:
197 {
198 QueueMsg queueMsg2;
199 while ((queueMsg2 = _mMsgQueue.CDequeue()) == null)
200 {
201 Thread.Sleep(10);
202 if (stopwatch.ElapsedMilliseconds > 1000)
203 {
204 queueMsg2 = (QueueMsg)new QueueMsgSendHeartbeat();
205 queueMsg2.data = (object)new Game(GameName);
206 break;
207 }
208 }
209 try
210 {
211 _sendMsg(queueMsg2);
212 stopwatch.Reset();
213 stopwatch.Start();
214 }
215 catch (ServerDownException val2)
216 {
217 ServerDownException e2 = val2;
218 _logException("Failed connecting to GameSense server", (Exception)(object)e2);
219 queueMsg = queueMsg2;
220 _setClientState((ClientState)1);
221 }
222 catch (CriticalMessageIllFormedException val3)
223 {
224 CriticalMessageIllFormedException e3 = val3;
225 _logException("Message ill-formed", (Exception)(object)e3);
226 _setClientState((ClientState)2);
227 }
228 catch (Exception e4)
229 {
230 _logException("Failed processing msg", e4);
231 }
232 break;
233 }
234 case 1:
236 if (_mServerPort == null)
237 {
238 _logWarning("Failed to obtain GameSense server port. GameSense will not function");
239 _setClientState((ClientState)2);
240 break;
241 }
243 if (queueMsg != null)
244 {
245 try
246 {
247 _sendMsg(queueMsg);
248 queueMsg = null;
249 }
250 catch (ServerDownException val)
251 {
252 ServerDownException e = val;
253 _logException("Failed connecting to GameSense server", (Exception)(object)e);
254 _logDbgMsg("Retrying in 5 seconds...");
255 Thread.Sleep(5000);
256 break;
257 }
258 }
259 _setClientState((ClientState)0);
260 break;
261 case 2:
262 _logDbgMsg("Entering inactive state");
264 break;
265 default:
266 _logErrorMsg("Unknown GameSense client state");
267 _setClientState((ClientState)2);
268 break;
269 }
270 }
271 _logDbgMsg("Worker exiting");
272 }
273
274 private void _setClientState(ClientState state)
275 {
276 //IL_0001: Unknown result type (might be due to invalid IL or missing references)
277 //IL_0006: Unknown result type (might be due to invalid IL or missing references)
278 //IL_000a: Unknown result type (might be due to invalid IL or missing references)
279 //IL_000b: Unknown result type (might be due to invalid IL or missing references)
280 //IL_0010: Unknown result type (might be due to invalid IL or missing references)
281 //IL_0026: Unknown result type (might be due to invalid IL or missing references)
282 //IL_0028: Invalid comparison between Unknown and I4
283 if (_mClientState != state)
284 {
286 if ((int)state == 0 && OnConnectionBecameActive != null)
287 {
289 }
290 if ((int)state == 2 && OnConnectionBecameInactive != null)
291 {
293 }
294 }
295 }
296
297 private static void _logException(string msg, Exception e)
298 {
299 }
300
301 private static void _logWarning(string msg)
302 {
303 }
304
305 private static void _logDbgMsg(string msg)
306 {
307 }
308
309 private static void _logErrorMsg(string msg)
310 {
311 }
312
313 private void _sendMsg(QueueMsg msg)
314 {
315 //IL_007c: Unknown result type (might be due to invalid IL or missing references)
316 //IL_0070: Unknown result type (might be due to invalid IL or missing references)
317 string text = _toJSON(msg.data);
318 JsonMsg jsonMsg = (JsonMsg)(object)((msg is JsonMsg) ? msg : null);
319 if (jsonMsg != null)
320 {
321 text = jsonMsg.JsonText;
322 }
324 try
325 {
326 _sendServer(msg.uri, text);
327 }
328 catch (WebException ex)
329 {
330 switch (ex.Status)
331 {
332 case WebExceptionStatus.ProtocolError:
333 if (msg.IsCritical())
334 {
335 Stream responseStream = ex.Response.GetResponseStream();
336 string text2 = new StreamReader(responseStream, Encoding.UTF8).ReadToEnd();
337 responseStream.Close();
338 throw new CriticalMessageIllFormedException(text2);
339 }
340 break;
341 case WebExceptionStatus.ConnectFailure:
342 throw new ServerDownException(ex.Message);
343 default:
344 _logException("Unexpected status", ex);
345 break;
346 }
347 }
348 }
349
350 private void SendJson(Uri uri, string data, bool isCritical)
351 {
352 //IL_0051: Unknown result type (might be due to invalid IL or missing references)
353 //IL_0045: Unknown result type (might be due to invalid IL or missing references)
354 _logDbgMsg(data);
355 try
356 {
357 _sendServer(uri, data);
358 }
359 catch (WebException ex)
360 {
361 switch (ex.Status)
362 {
363 case WebExceptionStatus.ProtocolError:
364 if (isCritical)
365 {
366 Stream responseStream = ex.Response.GetResponseStream();
367 string text = new StreamReader(responseStream, Encoding.UTF8).ReadToEnd();
368 responseStream.Close();
369 throw new CriticalMessageIllFormedException(text);
370 }
371 break;
372 case WebExceptionStatus.ConnectFailure:
373 throw new ServerDownException(ex.Message);
374 default:
375 _logException("Unexpected status", ex);
376 throw;
377 }
378 }
379 }
380
381 private static string _getPropsPath()
382 {
384 {
385 return Environment.ExpandEnvironmentVariables("%PROGRAMDATA%/SteelSeries/SteelSeries Engine 3/coreProps.json");
386 }
387 return "/Library/Application Support/SteelSeries Engine 3/coreProps.json";
388 }
389
390 private static string _readProps()
391 {
392 string path = _getPropsPath();
393 string result = null;
394 try
395 {
396 if (File.Exists(path))
397 {
398 result = File.ReadAllText(path);
399 return result;
400 }
401 _logErrorMsg("Could not read server props file, because it can't be found");
402 return result;
403 }
404 catch (Exception e)
405 {
406 _logException("Could not read server props file", e);
407 return result;
408 }
409 }
410
411 private static string _getServerPort()
412 {
413 //IL_0014: Unknown result type (might be due to invalid IL or missing references)
414 //IL_0031: Unknown result type (might be due to invalid IL or missing references)
415 string result = null;
416 string text = _readProps();
417 if (text != null)
418 {
419 try
420 {
421 JObject jObject = JObject.Parse(text);
422 coreProps coreProps = default(coreProps);
423 coreProps.address = (string)jObject.GetValue("address");
424 string[] array = coreProps.address.Split(':');
426 result = array[1];
427 return result;
428 }
429 catch (Exception e)
430 {
431 _logException("Cannot parse port information", e);
432 return result;
433 }
434 }
435 return result;
436 }
437
438 private void _initializeUris()
439 {
440 _uriBase = new Uri("http://127.0.0.1:" + _mServerPort);
441 QueueMsgRegisterGame._uri = new Uri(_uriBase, "game_metadata");
442 QueueMsgBindEvent._uri = new Uri(_uriBase, "bind_game_event");
443 QueueMsgRegisterEvent._uri = new Uri(_uriBase, "register_game_event");
444 QueueMsgSendEvent._uri = new Uri(_uriBase, "game_event");
445 QueueMsgSendHeartbeat._uri = new Uri(_uriBase, "game_heartbeat");
446 QueueMsgRemoveGame._uri = new Uri(_uriBase, "remove_game");
447 JsonMsg._bitmapEventUri = new Uri(_uriBase, "bitmap_event");
448 }
449
450 [DebuggerNonUserCode]
451 private void _sendServer(Uri uri, string data)
452 {
453 byte[] bytes = Encoding.ASCII.GetBytes(data);
454 HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
455 httpWebRequest.ContentType = "application/json";
456 httpWebRequest.Method = "POST";
457 Stream requestStream = httpWebRequest.GetRequestStream();
458 requestStream.Write(bytes, 0, bytes.Length);
459 requestStream.Close();
460 _currentRequest = httpWebRequest;
461 try
462 {
463 ((HttpWebResponse)httpWebRequest.GetResponse()).Close();
464 }
465 catch
466 {
467 }
468 }
469
470 private string _toJSON<T>(T obj)
471 {
472 //IL_0009: Unknown result type (might be due to invalid IL or missing references)
473 //IL_000e: Unknown result type (might be due to invalid IL or missing references)
474 fsData data = default(fsData);
475 fsResult val = _mSerializer.TrySerialize<T>(obj, ref data);
476 if (((fsResult)(ref val)).Succeeded)
477 {
478 return fsJsonPrinter.CompressedJson(data);
479 }
480 throw new Exception("Failed serializing object: " + obj.ToString());
481 }
482
483 private bool _isClientActive()
484 {
485 //IL_0001: Unknown result type (might be due to invalid IL or missing references)
486 //IL_0007: Invalid comparison between Unknown and I4
487 return (int)_mClientState == 0;
488 }
489
490 public void SendEvent(string fullEventJson)
491 {
492 //IL_000e: Unknown result type (might be due to invalid IL or missing references)
493 //IL_0013: Unknown result type (might be due to invalid IL or missing references)
494 //IL_001f: Expected O, but got Unknown
495 if (_isClientActive())
496 {
497 _mMsgQueue.PEnqueue((QueueMsg)new JsonMsg
498 {
499 JsonText = fullEventJson
500 });
501 }
502 }
503
504 public void SendEvent(string upperCaseEventName, int value)
505 {
506 //IL_0008: Unknown result type (might be due to invalid IL or missing references)
507 //IL_000e: Expected O, but got Unknown
508 //IL_002d: Unknown result type (might be due to invalid IL or missing references)
509 //IL_0033: Expected O, but got Unknown
510 if (_isClientActive())
511 {
512 Send_Event send_Event = new Send_Event();
513 send_Event.game = GameName;
514 send_Event.event_name = upperCaseEventName;
515 send_Event.data.value = value;
516 QueueMsgSendEvent queueMsgSendEvent = new QueueMsgSendEvent();
517 ((QueueMsg)queueMsgSendEvent).data = send_Event;
518 _mMsgQueue.PEnqueue((QueueMsg)(object)queueMsgSendEvent);
519 }
520 }
521
522 public void RegisterEvent(string upperCaseEventName, int minValue = 0, int maxValue = 100, EventIconId iconId = 0)
523 {
524 //IL_0008: Unknown result type (might be due to invalid IL or missing references)
525 //IL_000e: Expected O, but got Unknown
526 //IL_0018: Unknown result type (might be due to invalid IL or missing references)
527 //IL_001a: Unknown result type (might be due to invalid IL or missing references)
528 //IL_0024: Expected O, but got Unknown
529 if (_isClientActive())
530 {
531 QueueMsgRegisterEvent queueMsgRegisterEvent = new QueueMsgRegisterEvent();
532 ((QueueMsg)queueMsgRegisterEvent).data = (object)new Register_Event(GameName, upperCaseEventName, minValue, maxValue, iconId);
533 _mMsgQueue.PEnqueue((QueueMsg)(object)queueMsgRegisterEvent);
534 }
535 }
536}
static bool IsWindows
Definition Platform.cs:19
void RegisterEvent(string upperCaseEventName, int minValue=0, int maxValue=100, EventIconId iconId=0)
void RegisterGame(string name, string displayName, IconColor iconColor)
void SendEvent(string upperCaseEventName, int value)
void SendJson(Uri uri, string data, bool isCritical)
static ushort ToUInt16(object? value)
Definition Convert.cs:1177
static string ExpandEnvironmentVariables(string name)
virtual string Message
Definition Exception.cs:100
static bool Exists([NotNullWhen(true)] string? path)
Definition File.cs:97
static string ReadAllText(string path)
Definition File.cs:246
override string ReadToEnd()
virtual void Close()
Definition Stream.cs:644
void Write(byte[] buffer, int offset, int count)
override Stream GetRequestStream()
override WebResponse GetResponse()
WebExceptionStatus Status
static WebRequest Create(Uri requestUri, bool useUriBase)
virtual Stream GetResponseStream()
static Encoding UTF8
Definition Encoding.cs:526
static Encoding ASCII
Definition Encoding.cs:511
static void Sleep(int millisecondsTimeout)
Definition Thread.cs:658
void Start(object? parameter)
Definition Thread.cs:574