Webs.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using UnityEngine;
  8. public class Webs : MonoBehaviour
  9. {
  10. public bool Init()
  11. {
  12. int num = 49553;
  13. try
  14. {
  15. this.m_listener = new TcpListener(IPAddress.Any, num);
  16. this.m_listener.Start();
  17. UnityEngine.Debug.Log("Port{0}のListenを開始しました。" + num.ToString());
  18. }
  19. catch (Exception ex)
  20. {
  21. NDebug.MessageBox("エラー", string.Concat(new object[]
  22. {
  23. "インターネット機能:ポート番号",
  24. num,
  25. "は既に利用されています。\n他の通信アプリケーションを終了してゲームを再起動して下さい。\n\n",
  26. ex.Message
  27. }));
  28. this.m_listener = null;
  29. return false;
  30. }
  31. return true;
  32. }
  33. public void Uninit()
  34. {
  35. if (this.m_listener != null)
  36. {
  37. this.m_listener.Stop();
  38. }
  39. this.m_listener = null;
  40. }
  41. public void OnDestroy()
  42. {
  43. this.Uninit();
  44. }
  45. private void Listening()
  46. {
  47. if (Webs.nsWEB == null)
  48. {
  49. if (this.m_listener == null)
  50. {
  51. return;
  52. }
  53. try
  54. {
  55. if (!this.m_listener.Pending())
  56. {
  57. return;
  58. }
  59. }
  60. catch (Exception ex)
  61. {
  62. UnityEngine.Debug.LogError("[DLC]TcpListener::Pending 例外。" + ex.Message);
  63. this.m_listener = null;
  64. return;
  65. }
  66. Webs.tcpWEB = this.m_listener.AcceptTcpClient();
  67. if (Webs.tcpWEB == null)
  68. {
  69. return;
  70. }
  71. UnityEngine.Debug.Log("[DLC]クライアントが接続しました。");
  72. Webs.nsWEB = Webs.tcpWEB.GetStream();
  73. if (Webs.nsWEB == null)
  74. {
  75. Webs.tcpWEB.Close();
  76. Webs.nsWEB = null;
  77. Webs.tcpWEB = null;
  78. return;
  79. }
  80. Webs.tcpWEB_count = 0;
  81. }
  82. else
  83. {
  84. if (!Webs.nsWEB.DataAvailable)
  85. {
  86. Webs.tcpWEB_count++;
  87. if (Webs.tcpWEB_count > 100)
  88. {
  89. Webs.nsWEB.Close();
  90. Webs.tcpWEB.Close();
  91. Webs.nsWEB = null;
  92. Webs.tcpWEB = null;
  93. UnityEngine.Debug.Log("NotCanread クライアントを切断しました");
  94. }
  95. return;
  96. }
  97. byte[] array = new byte[4096];
  98. MemoryStream memoryStream = new MemoryStream();
  99. int num = Webs.nsWEB.Read(array, 0, array.Length);
  100. if (num == 0)
  101. {
  102. Webs.nsWEB.Close();
  103. Webs.tcpWEB.Close();
  104. Webs.nsWEB = null;
  105. Webs.tcpWEB = null;
  106. UnityEngine.Debug.Log("クライアントから切断されました。");
  107. return;
  108. }
  109. memoryStream.Write(array, 0, num);
  110. string @string = this.enc.GetString(memoryStream.ToArray());
  111. memoryStream.Close();
  112. UnityEngine.Debug.Log(@string);
  113. if (@string == null)
  114. {
  115. Webs.nsWEB.Close();
  116. Webs.tcpWEB.Close();
  117. Webs.nsWEB = null;
  118. Webs.tcpWEB = null;
  119. UnityEngine.Debug.Log("resMsg==null");
  120. return;
  121. }
  122. string text = "HTTP/1.0 204 OK\r\nConnection: close\r\n\r\n";
  123. string text2 = string.Empty;
  124. string text3 = string.Empty;
  125. for (int i = 6; i < @string.Length; i++)
  126. {
  127. if (@string[i] == '=' || @string[i] == ' ')
  128. {
  129. for (int j = i + 1; j < @string.Length; j++)
  130. {
  131. if (@string[j] == ' ')
  132. {
  133. break;
  134. }
  135. if (@string[j] == '&')
  136. {
  137. break;
  138. }
  139. if (@string[j] == '\r')
  140. {
  141. break;
  142. }
  143. text3 += @string[j];
  144. }
  145. break;
  146. }
  147. text2 += @string[i];
  148. }
  149. UnityEngine.Debug.Log("[DLC]" + text2 + ":" + text3);
  150. try
  151. {
  152. int num2 = text2.IndexOf("itemid") + 7;
  153. int num3 = text2.IndexOf("/", num2);
  154. if (num3 == -1)
  155. {
  156. num3 = text2.Length;
  157. }
  158. string text4 = text2.Substring(num2, num3 - num2);
  159. num2 = text2.IndexOf("ott") + 4;
  160. num3 = text2.IndexOf("/", num2);
  161. if (num3 == -1)
  162. {
  163. num3 = text2.Length;
  164. }
  165. string text5 = text2.Substring(num2, num3 - num2);
  166. num2 = text2.IndexOf("itoken") + 7;
  167. num3 = text2.IndexOf("/", num2);
  168. if (num3 == -1)
  169. {
  170. num3 = text2.Length;
  171. }
  172. string text6 = text2.Substring(num2, num3 - num2);
  173. string text7 = "0";
  174. int num4 = text2.IndexOf("cmd");
  175. if (num4 != -1)
  176. {
  177. num2 = num4 + 4;
  178. num3 = text2.IndexOf("/", num2);
  179. if (num3 == -1)
  180. {
  181. num3 = text2.Length;
  182. }
  183. text7 = text2.Substring(num2, num3 - num2);
  184. }
  185. string str = UTY.gameProjectPath + "\\";
  186. string text8 = str + "COM3D2.exe";
  187. if (File.Exists(text8))
  188. {
  189. new Process
  190. {
  191. StartInfo =
  192. {
  193. FileName = text8,
  194. Arguments = string.Concat(new string[]
  195. {
  196. "/dl:",
  197. text4,
  198. ":",
  199. text5,
  200. ":",
  201. text6,
  202. ":",
  203. text7
  204. })
  205. }
  206. }.Start();
  207. }
  208. else
  209. {
  210. UnityEngine.Debug.LogError("Launcher not found. -> " + text8);
  211. }
  212. }
  213. catch (Exception ex2)
  214. {
  215. UnityEngine.Debug.LogError("[DLC]ダウンロードに失敗しました。" + ex2.Message);
  216. }
  217. UnityEngine.Debug.Log("[DLC]完了");
  218. byte[] bytes = this.enc.GetBytes(text);
  219. Webs.nsWEB.Write(bytes, 0, bytes.Length);
  220. UnityEngine.Debug.Log(text);
  221. Webs.nsWEB.Close();
  222. Webs.tcpWEB.Close();
  223. Webs.nsWEB = null;
  224. Webs.tcpWEB = null;
  225. }
  226. }
  227. private void Update()
  228. {
  229. this.Listening();
  230. }
  231. private TcpListener m_listener;
  232. private Encoding enc = Encoding.UTF8;
  233. private static NetworkStream nsWEB;
  234. private static TcpClient tcpWEB;
  235. private static int tcpWEB_count;
  236. }