using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using UnityEngine;

public class Webs : MonoBehaviour
{
	public void Init()
	{
		int num = 49553;
		try
		{
			this.m_listener = new TcpListener(IPAddress.Any, num);
			this.m_listener.Start();
			UnityEngine.Debug.Log("Port{0}のListenを開始しました。" + num.ToString());
		}
		catch (Exception ex)
		{
			NDebug.MessageBox("エラー", string.Concat(new object[]
			{
				"インターネット機能：ポート番号",
				num,
				"は既に利用されています。\n他の通信アプリケーションを終了してゲームを再起動して下さい。\n\n",
				ex.Message
			}));
			this.m_listener = null;
		}
	}

	public void OnDestroy()
	{
		if (this.m_listener != null)
		{
			this.m_listener.Stop();
		}
		this.m_listener = null;
	}

	private void Listening()
	{
		if (Webs.nsWEB == null)
		{
			if (this.m_listener == null)
			{
				return;
			}
			try
			{
				if (!this.m_listener.Pending())
				{
					return;
				}
			}
			catch (Exception ex)
			{
				UnityEngine.Debug.LogError("[DLC]TcpListener::Pending 例外。" + ex.Message);
				this.m_listener = null;
				return;
			}
			Webs.tcpWEB = this.m_listener.AcceptTcpClient();
			if (Webs.tcpWEB == null)
			{
				return;
			}
			UnityEngine.Debug.Log("[DLC]クライアントが接続しました。");
			Webs.nsWEB = Webs.tcpWEB.GetStream();
			if (Webs.nsWEB == null)
			{
				Webs.tcpWEB.Close();
				Webs.nsWEB = null;
				Webs.tcpWEB = null;
				return;
			}
			Webs.tcpWEB_count = 0;
		}
		else
		{
			if (!Webs.nsWEB.DataAvailable)
			{
				Webs.tcpWEB_count++;
				if (Webs.tcpWEB_count > 100)
				{
					Webs.nsWEB.Close();
					Webs.tcpWEB.Close();
					Webs.nsWEB = null;
					Webs.tcpWEB = null;
					UnityEngine.Debug.Log("NotCanread クライアントを切断しました");
				}
				return;
			}
			byte[] array = new byte[4096];
			MemoryStream memoryStream = new MemoryStream();
			int num = Webs.nsWEB.Read(array, 0, array.Length);
			if (num == 0)
			{
				Webs.nsWEB.Close();
				Webs.tcpWEB.Close();
				Webs.nsWEB = null;
				Webs.tcpWEB = null;
				UnityEngine.Debug.Log("クライアントから切断されました。");
				return;
			}
			memoryStream.Write(array, 0, num);
			string @string = this.enc.GetString(memoryStream.ToArray());
			memoryStream.Close();
			UnityEngine.Debug.Log(@string);
			if (@string == null)
			{
				Webs.nsWEB.Close();
				Webs.tcpWEB.Close();
				Webs.nsWEB = null;
				Webs.tcpWEB = null;
				UnityEngine.Debug.Log("resMsg==null");
				return;
			}
			string text = "HTTP/1.0 204 OK\r\nConnection: close\r\n\r\n";
			string text2 = string.Empty;
			string text3 = string.Empty;
			for (int i = 6; i < @string.Length; i++)
			{
				if (@string[i] == '=' || @string[i] == ' ')
				{
					for (int j = i + 1; j < @string.Length; j++)
					{
						if (@string[j] == ' ')
						{
							break;
						}
						if (@string[j] == '&')
						{
							break;
						}
						if (@string[j] == '\r')
						{
							break;
						}
						text3 += @string[j];
					}
					break;
				}
				text2 += @string[i];
			}
			UnityEngine.Debug.Log("[DLC]" + text2 + ":" + text3);
			try
			{
				int num2 = text2.IndexOf("itemid") + 7;
				int num3 = text2.IndexOf("/", num2);
				if (num3 == -1)
				{
					num3 = text2.Length;
				}
				string text4 = text2.Substring(num2, num3 - num2);
				num2 = text2.IndexOf("ott") + 4;
				num3 = text2.IndexOf("/", num2);
				if (num3 == -1)
				{
					num3 = text2.Length;
				}
				string text5 = text2.Substring(num2, num3 - num2);
				num2 = text2.IndexOf("itoken") + 7;
				num3 = text2.IndexOf("/", num2);
				if (num3 == -1)
				{
					num3 = text2.Length;
				}
				string text6 = text2.Substring(num2, num3 - num2);
				string text7 = "0";
				int num4 = text2.IndexOf("cmd");
				if (num4 != -1)
				{
					num2 = num4 + 4;
					num3 = text2.IndexOf("/", num2);
					if (num3 == -1)
					{
						num3 = text2.Length;
					}
					text7 = text2.Substring(num2, num3 - num2);
				}
				string fullPath = Path.GetFullPath(".\\");
				string text8 = fullPath + "COM3D2.exe";
				if (File.Exists(text8))
				{
					new Process
					{
						StartInfo = 
						{
							FileName = text8,
							Arguments = string.Concat(new string[]
							{
								"/dl:",
								text4,
								":",
								text5,
								":",
								text6,
								":",
								text7
							})
						}
					}.Start();
				}
			}
			catch (Exception ex2)
			{
				UnityEngine.Debug.LogError("[DLC]ダウンロードに失敗しました。" + ex2.Message);
			}
			UnityEngine.Debug.Log("[DLC]完了");
			byte[] bytes = this.enc.GetBytes(text);
			Webs.nsWEB.Write(bytes, 0, bytes.Length);
			UnityEngine.Debug.Log(text);
			Webs.nsWEB.Close();
			Webs.tcpWEB.Close();
			Webs.nsWEB = null;
			Webs.tcpWEB = null;
		}
	}

	private void Update()
	{
		this.Listening();
	}

	private TcpListener m_listener;

	private Encoding enc = Encoding.UTF8;

	private static NetworkStream nsWEB;

	private static TcpClient tcpWEB;

	private static int tcpWEB_count;
}
