Webs.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 void 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. }
  30. }
  31. public void OnDestroy()
  32. {
  33. if (this.m_listener != null)
  34. {
  35. this.m_listener.Stop();
  36. }
  37. this.m_listener = null;
  38. }
  39. private void Listening()
  40. {
  41. if (Webs.nsWEB == null)
  42. {
  43. if (this.m_listener == null)
  44. {
  45. return;
  46. }
  47. try
  48. {
  49. if (!this.m_listener.Pending())
  50. {
  51. return;
  52. }
  53. }
  54. catch (Exception ex)
  55. {
  56. UnityEngine.Debug.LogError("[DLC]TcpListener::Pending 例外。" + ex.Message);
  57. this.m_listener = null;
  58. return;
  59. }
  60. Webs.tcpWEB = this.m_listener.AcceptTcpClient();
  61. if (Webs.tcpWEB == null)
  62. {
  63. return;
  64. }
  65. UnityEngine.Debug.Log("[DLC]クライアントが接続しました。");
  66. Webs.nsWEB = Webs.tcpWEB.GetStream();
  67. if (Webs.nsWEB == null)
  68. {
  69. Webs.tcpWEB.Close();
  70. Webs.nsWEB = null;
  71. Webs.tcpWEB = null;
  72. return;
  73. }
  74. Webs.tcpWEB_count = 0;
  75. }
  76. else
  77. {
  78. if (!Webs.nsWEB.DataAvailable)
  79. {
  80. Webs.tcpWEB_count++;
  81. if (Webs.tcpWEB_count > 100)
  82. {
  83. Webs.nsWEB.Close();
  84. Webs.tcpWEB.Close();
  85. Webs.nsWEB = null;
  86. Webs.tcpWEB = null;
  87. UnityEngine.Debug.Log("NotCanread クライアントを切断しました");
  88. }
  89. return;
  90. }
  91. byte[] array = new byte[4096];
  92. MemoryStream memoryStream = new MemoryStream();
  93. int num = Webs.nsWEB.Read(array, 0, array.Length);
  94. if (num == 0)
  95. {
  96. Webs.nsWEB.Close();
  97. Webs.tcpWEB.Close();
  98. Webs.nsWEB = null;
  99. Webs.tcpWEB = null;
  100. UnityEngine.Debug.Log("クライアントから切断されました。");
  101. return;
  102. }
  103. memoryStream.Write(array, 0, num);
  104. string @string = this.enc.GetString(memoryStream.ToArray());
  105. memoryStream.Close();
  106. UnityEngine.Debug.Log(@string);
  107. if (@string == null)
  108. {
  109. Webs.nsWEB.Close();
  110. Webs.tcpWEB.Close();
  111. Webs.nsWEB = null;
  112. Webs.tcpWEB = null;
  113. UnityEngine.Debug.Log("resMsg==null");
  114. return;
  115. }
  116. string text = "HTTP/1.0 204 OK\r\nConnection: close\r\n\r\n";
  117. string text2 = string.Empty;
  118. string text3 = string.Empty;
  119. for (int i = 6; i < @string.Length; i++)
  120. {
  121. if (@string[i] == '=' || @string[i] == ' ')
  122. {
  123. for (int j = i + 1; j < @string.Length; j++)
  124. {
  125. if (@string[j] == ' ')
  126. {
  127. break;
  128. }
  129. if (@string[j] == '&')
  130. {
  131. break;
  132. }
  133. if (@string[j] == '\r')
  134. {
  135. break;
  136. }
  137. text3 += @string[j];
  138. }
  139. break;
  140. }
  141. text2 += @string[i];
  142. }
  143. UnityEngine.Debug.Log("[DLC]" + text2 + ":" + text3);
  144. try
  145. {
  146. int num2 = text2.IndexOf("itemid") + 7;
  147. int num3 = text2.IndexOf("/", num2);
  148. if (num3 == -1)
  149. {
  150. num3 = text2.Length;
  151. }
  152. string text4 = text2.Substring(num2, num3 - num2);
  153. num2 = text2.IndexOf("ott") + 4;
  154. num3 = text2.IndexOf("/", num2);
  155. if (num3 == -1)
  156. {
  157. num3 = text2.Length;
  158. }
  159. string text5 = text2.Substring(num2, num3 - num2);
  160. num2 = text2.IndexOf("itoken") + 7;
  161. num3 = text2.IndexOf("/", num2);
  162. if (num3 == -1)
  163. {
  164. num3 = text2.Length;
  165. }
  166. string text6 = text2.Substring(num2, num3 - num2);
  167. string text7 = "0";
  168. int num4 = text2.IndexOf("cmd");
  169. if (num4 != -1)
  170. {
  171. num2 = num4 + 4;
  172. num3 = text2.IndexOf("/", num2);
  173. if (num3 == -1)
  174. {
  175. num3 = text2.Length;
  176. }
  177. text7 = text2.Substring(num2, num3 - num2);
  178. }
  179. string fullPath = Path.GetFullPath(".\\");
  180. string text8 = fullPath + "COM3D2.exe";
  181. if (File.Exists(text8))
  182. {
  183. new Process
  184. {
  185. StartInfo =
  186. {
  187. FileName = text8,
  188. Arguments = string.Concat(new string[]
  189. {
  190. "/dl:",
  191. text4,
  192. ":",
  193. text5,
  194. ":",
  195. text6,
  196. ":",
  197. text7
  198. })
  199. }
  200. }.Start();
  201. }
  202. }
  203. catch (Exception ex2)
  204. {
  205. UnityEngine.Debug.LogError("[DLC]ダウンロードに失敗しました。" + ex2.Message);
  206. }
  207. UnityEngine.Debug.Log("[DLC]完了");
  208. byte[] bytes = this.enc.GetBytes(text);
  209. Webs.nsWEB.Write(bytes, 0, bytes.Length);
  210. UnityEngine.Debug.Log(text);
  211. Webs.nsWEB.Close();
  212. Webs.tcpWEB.Close();
  213. Webs.nsWEB = null;
  214. Webs.tcpWEB = null;
  215. }
  216. }
  217. private void Update()
  218. {
  219. this.Listening();
  220. }
  221. private TcpListener m_listener;
  222. private Encoding enc = Encoding.UTF8;
  223. private static NetworkStream nsWEB;
  224. private static TcpClient tcpWEB;
  225. private static int tcpWEB_count;
  226. }