ConsoleProRemoteServer.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using System.Text;
  5. using UnityEngine;
  6. namespace FlyingWormConsole3
  7. {
  8. public class ConsoleProRemoteServer : MonoBehaviour
  9. {
  10. private void Awake()
  11. {
  12. UnityEngine.Object.DontDestroyOnLoad(base.gameObject);
  13. UnityEngine.Debug.Log("Starting Console Pro Server on port : " + this.port);
  14. ConsoleProRemoteServer.listener.Prefixes.Add("http://*:" + this.port + "/");
  15. ConsoleProRemoteServer.listener.Start();
  16. ConsoleProRemoteServer.listener.BeginGetContext(new AsyncCallback(this.ListenerCallback), null);
  17. }
  18. private void OnEnable()
  19. {
  20. Application.logMessageReceived += this.LogCallback;
  21. }
  22. private void OnDisable()
  23. {
  24. Application.logMessageReceived -= this.LogCallback;
  25. }
  26. public void LogCallback(string logString, string stackTrace, LogType type)
  27. {
  28. if (!logString.StartsWith("CPIGNORE"))
  29. {
  30. this.QueueLog(logString, stackTrace, type);
  31. }
  32. }
  33. private void QueueLog(string logString, string stackTrace, LogType type)
  34. {
  35. this.logs.Add(new ConsoleProRemoteServer.QueuedLog
  36. {
  37. message = logString,
  38. stackTrace = stackTrace,
  39. type = type
  40. });
  41. }
  42. private void ListenerCallback(IAsyncResult result)
  43. {
  44. ConsoleProRemoteServer.HTTPContext context = new ConsoleProRemoteServer.HTTPContext(ConsoleProRemoteServer.listener.EndGetContext(result));
  45. this.HandleRequest(context);
  46. ConsoleProRemoteServer.listener.BeginGetContext(new AsyncCallback(this.ListenerCallback), null);
  47. }
  48. private void HandleRequest(ConsoleProRemoteServer.HTTPContext context)
  49. {
  50. bool flag = false;
  51. string command = context.Command;
  52. if (command != null)
  53. {
  54. if (command == "/NewLogs")
  55. {
  56. flag = true;
  57. if (this.logs.Count > 0)
  58. {
  59. string text = string.Empty;
  60. for (int i = 0; i < this.logs.Count; i++)
  61. {
  62. ConsoleProRemoteServer.QueuedLog queuedLog = this.logs[i];
  63. text = text + "::::" + queuedLog.type;
  64. text = text + "||||" + queuedLog.message;
  65. text = text + ">>>>" + queuedLog.stackTrace + ">>>>";
  66. }
  67. context.RespondWithString(text);
  68. this.logs.Clear();
  69. }
  70. else
  71. {
  72. context.RespondWithString(string.Empty);
  73. }
  74. }
  75. }
  76. if (!flag)
  77. {
  78. context.Response.StatusCode = 404;
  79. context.Response.StatusDescription = "Not Found";
  80. }
  81. context.Response.OutputStream.Close();
  82. }
  83. public int port = 51000;
  84. private static HttpListener listener = new HttpListener();
  85. [NonSerialized]
  86. public List<ConsoleProRemoteServer.QueuedLog> logs = new List<ConsoleProRemoteServer.QueuedLog>();
  87. public class HTTPContext
  88. {
  89. public HTTPContext(HttpListenerContext inContext)
  90. {
  91. this.context = inContext;
  92. }
  93. public string Command
  94. {
  95. get
  96. {
  97. return WWW.UnEscapeURL(this.context.Request.Url.AbsolutePath);
  98. }
  99. }
  100. public HttpListenerRequest Request
  101. {
  102. get
  103. {
  104. return this.context.Request;
  105. }
  106. }
  107. public HttpListenerResponse Response
  108. {
  109. get
  110. {
  111. return this.context.Response;
  112. }
  113. }
  114. public void RespondWithString(string inString)
  115. {
  116. this.Response.StatusDescription = "OK";
  117. this.Response.StatusCode = 200;
  118. if (!string.IsNullOrEmpty(inString))
  119. {
  120. this.Response.ContentType = "text/plain";
  121. byte[] bytes = Encoding.UTF8.GetBytes(inString);
  122. this.Response.ContentLength64 = (long)bytes.Length;
  123. this.Response.OutputStream.Write(bytes, 0, bytes.Length);
  124. }
  125. }
  126. public HttpListenerContext context;
  127. public string path;
  128. }
  129. [Serializable]
  130. public class QueuedLog
  131. {
  132. public string message;
  133. public string stackTrace;
  134. public LogType type;
  135. }
  136. }
  137. }