diff --git a/CTZLauncher/Authentication/CTZAuthenticator.cs b/CTZLauncher/Authentication/CTZAuthenticator.cs
index a09f7f3..831e86c 100644
--- a/CTZLauncher/Authentication/CTZAuthenticator.cs
+++ b/CTZLauncher/Authentication/CTZAuthenticator.cs
@@ -1,74 +1,79 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Windows.Threading;
-using KMCCC.Authentication;
-using CityCraft;
-using System.Threading.Tasks;
-using System.Threading;
-
-namespace KMCCC.Authentication
-{
- class CTZAuthenticator
- {
- ///
- /// 玩家的名字
- ///
- public readonly string Username;
- public readonly string Password;
- HttpHelper http = new HttpHelper();
- ///
- /// 验证服务器地址
- ///
- public readonly string Address;
- ///
- /// 验证服务器端口
- ///
- public readonly int Port;
- ///
- /// 构造离线验证器
- ///
- /// 玩家的名字
- public CTZAuthenticator(string username, string password, string address, int port = 25565)
- {
- Username = username;
- Password = password;
- Address = address.IndexOf("http") > 0 ? address : "http://" + address;
- Port = port;
- }
-
- ///
- /// 标注外部登陆验证器
- ///
- public string Type
- {
- get { return "KMCCC.CTZ"; }
- }
-
- public bool isRegistered()
- {
- return getResult(Address + ":" + Port + "/isregistered?username=" + Username);
- }
-
- public bool Register()
- {
- return getResult(Address + ":" + Port + "/register?username=" + Username + "&password=" + Password);
- }
-
- public bool isLogin()
- {
- return getResult(Address + ":" + Port + "/islogin?username=" + Username);
- }
-
- public bool Login()
- {
- return getResult(Address + ":" + Port + "/login?username=" + Username + "&password=" + Password);
- }
-
- public bool getResult(string url)
- {
- return http.Send(HttpMethod.GET, url).Contains("true") ? true : false;
- }
- }
-}
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Windows.Threading;
+using KMCCC.Authentication;
+using CityCraft;
+using System.Threading.Tasks;
+using System.Threading;
+
+namespace KMCCC.Authentication
+{
+ class CTZAuthenticator
+ {
+ ///
+ /// 玩家的名字
+ ///
+ public readonly string Username;
+ public readonly string Password;
+ HttpHelper http = new HttpHelper();
+ ///
+ /// 验证服务器地址
+ ///
+ public readonly string Address;
+ ///
+ /// 验证服务器端口
+ ///
+ public readonly int Port;
+ ///
+ /// 构造离线验证器
+ ///
+ /// 玩家的名字
+ public CTZAuthenticator(string username, string password, string address, int port = 25565)
+ {
+ Username = username;
+ Password = password;
+ Address = address.IndexOf("http") > 0 ? address : "http://" + address;
+ Port = port;
+ }
+
+ ///
+ /// 标注外部登陆验证器
+ ///
+ public string Type
+ {
+ get { return "KMCCC.CTZ"; }
+ }
+
+ public bool isRegistered()
+ {
+ return getResult(Address + ":" + Port + "/isregistered?username=" + Username);
+ }
+
+ public bool Register()
+ {
+ return getResult(Address + ":" + Port + "/register?username=" + Username + "&password=" + Password);
+ }
+
+ public bool isLogin()
+ {
+ return getResult(Address + ":" + Port + "/islogin?username=" + Username);
+ }
+
+ public bool Login()
+ {
+ return getResult(Address + ":" + Port + "/login?username=" + Username + "&password=" + Password);
+ }
+
+ public string getServerList()
+ {
+ return http.Send(HttpMethod.GET, Address + ":" + Port + "/serverlist");
+ }
+
+ public bool getResult(string url)
+ {
+ return http.Send(HttpMethod.GET, url).Contains("true") ? true : false;
+ }
+ }
+}
diff --git a/CTZLauncher/CTZLauncher.csproj b/CTZLauncher/CTZLauncher.csproj
index 75f0601..96f7b48 100644
--- a/CTZLauncher/CTZLauncher.csproj
+++ b/CTZLauncher/CTZLauncher.csproj
@@ -5,7 +5,7 @@
Debug
AnyCPU
{EC25362D-5BA7-4CB3-BDA2-C575B9318086}
- WinExe
+ Exe
Properties
CTZLauncher
CTZLauncher
@@ -94,6 +94,7 @@
MSBuild:Compile
Designer
+
@@ -171,7 +172,6 @@
-
diff --git a/CTZLauncher/HttpHelper.cs b/CTZLauncher/HttpHelper.cs
index b4ea1fe..b91bc06 100644
--- a/CTZLauncher/HttpHelper.cs
+++ b/CTZLauncher/HttpHelper.cs
@@ -1,415 +1,415 @@
-using System;
-using System.Collections;
-using System.Collections.Generic;
-using System.Diagnostics;
-using System.IO;
-using System.IO.Compression;
-using System.Net;
-using System.Net.Security;
-using System.Net.Sockets;
-using System.Security.Authentication;
-using System.Security.Cryptography.X509Certificates;
-using System.Text;
-using System.Text.RegularExpressions;
-using System.Threading;
-using System.Windows.Threading;
-
-namespace CityCraft
-{
- public enum HttpMethod
- {
- GET,
- POST
- }
- public class HttpArgs
- {
- public string Url { get; set; }
- public string Host { get; set; }
- public int Port { get; set; }
- public string Accept { get; set; }
- public string Referer { get; set; }
- public string Cookie { get; set; }
- public string Data { get; set; }
- public string UA { get; set; }
- public HttpMethod Method { get; set; }
- }
- public enum HttpReadyState
- {
- 未初始化,
- 载入,
- 载入完成,
- 交互,
- 完成
- }
- public class HttpHelper
- {
- public HttpReadyState readyState = HttpReadyState.未初始化;
- public int Status = 0;
- public string responseBody = "";
- public string responseText = "";
- public byte[] responseByte = null;
- public HttpArgs args = new HttpArgs();
- public string ErrMsg = string.Empty;
- ///
- /// 提交方法
- ///
- #region HttpWebRequest & HttpWebResponse
-
- ///
- /// Get方法
- ///
- /// 请求地址
- /// Cookies存储器
- /// 请求返回的Stream
- public string Send(HttpMethod method, string url, bool Async = false)
- {
- if (string.IsNullOrEmpty(url))
- {
- return string.Empty;
- }
- readyState = HttpReadyState.载入;
- ParseURL(url);
- args.Method = method;
- new Thread(new ThreadStart(ReciveData)).Start();
- if (!Async)
- {
- while (readyState != HttpReadyState.完成) { this.DoEvent(); }
- return responseBody;
- }
- return null;
- }
-
- ///
- /// 模仿C#的Application.Doevent函数。可以适当添加try catch 模块
- ///
- public void DoEvent()
- {
- DispatcherFrame frame = new DispatcherFrame();
- Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(ExitFrame), frame);
- Dispatcher.PushFrame(frame);
- }
- public object ExitFrame(object f)
- {
- ((DispatcherFrame)f).Continue = false;
- return null;
- }
-
- public void ReciveData()
- {
- responseBody = InternalSocketHttp();
- readyState = HttpReadyState.完成;
- }
- ///
- /// Post方法
- ///
- /// 请求地址
- /// Post数据
- /// Cllkies存储器
- /// 请求返回的流
- public string Post(string url,
- byte[] bytes,
- CookieContainer cookies,
- Encoding encoding)
- {
- return null;
- }
-
- ///
- /// 根据Url得到host
- ///
- /// url字符串
- /// host字符串
- private void ParseURL(string strUrl)
- {
- if (args == null)
- args = new HttpArgs();
-
- args.Host = "";
- args.Port = 80;
- args.Referer = "";
- args.Cookie = "";
- args.Url = "";
- args.Accept = "text/html";//,application/xhtml+xml,application/xml,application/json;";
- args.UA = "Mozilla/5.0+(Compatible;+Baiduspider/2.0;++http://www.baidu.com/search/spider.html)";
-
- //http://www.alibaba.com/products/Egg_Laying_Block_Machine/1.html
- int iIndex = strUrl.IndexOf(@"//");
- if (iIndex <= 0)
- args = null;
- //www.alibaba.com:80/products/Egg_Laying_Block_Machine/1.html
- string nohttpurl = strUrl.Substring(iIndex + 2);
- string address = nohttpurl;
- iIndex = nohttpurl.IndexOf(@"/");
- if (iIndex > 0)
- {
- //www.alibaba.com:80
- address = nohttpurl.Substring(0, iIndex);
- args.Url = nohttpurl.Substring(iIndex);
- }
- else
- {
- args.Url = "/";
- }
- iIndex = nohttpurl.IndexOf(@":");
- if (iIndex > 0)
- {
- string[] tempargs = address.Trim().Split(char.Parse(":"));
- args.Host = tempargs[0];
- args.Port = int.Parse(tempargs[1]);
- }
- else
- {
- //www.alibaba.com:80
- args.Host = address;
- args.Port = 80;
- }
- }
- #endregion
-
- #region Socket
-
- string InternalSocketHttp()
- {
- using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
- {
- try
- {
- socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 1000);
- socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 5000);
- socket.Connect(args.Host, args.Port);
- if (socket.Connected)
- {
- byte[] buff = ParseHttpArgs();
- if (socket.Send(buff) > 0)
- {
- List responseBytes = new List();
- byte[] buffer = new byte[1024];
- int iNumber = socket.Receive(buffer, buffer.Length, SocketFlags.None);
- while (iNumber > 0)//使用了Connection: Close 所以判断长度为0 时停止接受
- {
- responseBytes.AddRange(new List(buffer));//添加数据到List
- iNumber = socket.Receive(buffer, buffer.Length, SocketFlags.None);//继续接收数据
- }
- responseByte = responseBytes.ToArray();
- readyState = HttpReadyState.载入完成;
- return ParseResponse(responseByte);
- }
- }
- }
- catch (Exception e)
- {
- ErrMsg = e.Message;
- }
- return string.Empty;
- }
- }
-
- private string ParseResponse(byte[] responseBytes)
- {
- string responseStr = Encoding.UTF8.GetString(responseBytes);
- int splitindex = responseStr.IndexOf("\r\n\r\n");
- if (splitindex > 0)
- {
- string responseHeader = responseStr.Substring(0, splitindex);
- string responseBody = responseStr.Substring(splitindex + 4);
- //Console.WriteLine(responseHeader);
- if (responseHeader.StartsWith("HTTP/1.1 400"))
- {
- Status = 400;
- return string.Empty;
- }
- else if (responseHeader.StartsWith("HTTP/1.1 404"))
- {
- Status = 404;
- return string.Empty;
- }
- else if (responseHeader.StartsWith("HTTP/1.1 302") || responseHeader.StartsWith("HTTP/1.1 301"))
- {
- Status = 302;
- int start = responseHeader.ToUpper().IndexOf("LOCATION");
- if (start > 0)
- {
- string temp = responseHeader.Substring(start, responseHeader.Length - start);
- string[] sArry = Regex.Split(temp, "\r\n");
- args.Url = sArry[0].Remove(0, 10);
- if (args.Url == "")
- return string.Empty;
- return InternalSocketHttp(); //注意:302协议需要重定向
- }
- }
- else if (responseHeader.StartsWith("HTTP/1.1 200")) //读取内容
- {
- Status = 200;
- //解压
- DecompressWebPage(ref responseBytes, responseHeader);
- //转码
- responseBody = DecodeWebStringByHttpHeader(responseBytes, responseHeader);
- responseBody = DecodeWebStringByHtmlPageInfo(responseBytes, responseBody);
- }
- splitindex = responseBody.IndexOf("\r\n\r\n");
- if (splitindex > 0)
- responseBody = responseBody.Substring(splitindex + 4);
- else
- responseBody = string.Empty;
- return responseBody;
- }
- return string.Empty;
- }
- #endregion
-
- #region Helper
-
- ///
- /// 解压网页
- ///
- /// 网页字节数组含http头
- /// 数组长度
- /// Http头字符串
- /// 网页正文开始位置
- private void DecompressWebPage(ref byte[] responseBytes, string strHeader)
- {
- Regex regZip = new Regex(@"Content-Encoding:\s+gzip[^\n]*\r\n", RegexOptions.IgnoreCase);
-
- if (regZip.IsMatch(strHeader))
- {
- responseBytes = Decompress(responseBytes);
- }
- }
-
- ///
- /// 解压gzip网页
- ///
- /// 压缩过的字符串字节数组
- /// 解压后的字节数组
- private byte[] Decompress(byte[] szSource)
- {
- MemoryStream msSource = new MemoryStream(szSource);
- //DeflateStream 也可以这儿
- GZipStream stream = new GZipStream(msSource, CompressionMode.Decompress);
- byte[] szTotal = new byte[40 * 1024];
- long lTotal = 0;
- byte[] buffer = new byte[8];
- int iCount = 0;
- do
- {
- iCount = stream.Read(buffer, 0, 8);
- if (szTotal.Length <= lTotal + iCount) //放大数组
- {
- byte[] temp = new byte[szTotal.Length * 10];
- szTotal.CopyTo(temp, 0);
- szTotal = temp;
- }
- buffer.CopyTo(szTotal, lTotal);
- lTotal += iCount;
- } while (iCount != 0);
- byte[] szDest = new byte[lTotal];
- Array.Copy(szTotal, 0, szDest, 0, lTotal);
- return szDest;
- }
-
- ///
- /// 根据Http头标记里面的字符编码解析字符串
- ///
- /// 网页内容字节数组(除http头以外的内容)
- /// 网页内容字节数组长度
- /// http头的字符串
- /// 转好的字符串
- private string DecodeWebStringByHttpHeader(byte[] responseBytes, string strHeader)
- {
- string strResponse = "";
- if (strHeader.Contains("charset=GBK") || strHeader.Contains("charset=gb2312"))
- {
- strResponse = Encoding.GetEncoding("GBK").GetString(responseBytes);
- }
- else
- strResponse = Encoding.UTF8.GetString(responseBytes);
- return strResponse;
- }
-
- ///
- /// 根据网页meta标记里面的字符编码解析字符串
- ///
- /// 网页内容字节数组(除http头以外的内容)
- /// 网页内容字节数组长度
- /// 网页内容字符串, 可能已经根据其它转码要求转换过的字符串
- /// 转好的字符串
- private string DecodeWebStringByHtmlPageInfo(byte[] responseBytes, string strResponse)
- {
- Regex regGB2312 = new Regex(@"]+Content-Type[^>]+gb2312[^>]*>", RegexOptions.IgnoreCase);
- Regex regGBK = new Regex(@"]+Content-Type[^>]+gbk[^>]*>", RegexOptions.IgnoreCase);
- Regex regBig5 = new Regex(@"]+Content-Type[^>]+Big5[^>]*>", RegexOptions.IgnoreCase);
- if (regGB2312.IsMatch(strResponse) || regGBK.IsMatch(strResponse))
- strResponse = Encoding.GetEncoding("GBK").GetString(responseBytes);
- if (regBig5.IsMatch(strResponse))
- strResponse = Encoding.GetEncoding("Big5").GetString(responseBytes);
- return strResponse;
- }
-
- private byte[] ParseHttpArgs()
- {
- StringBuilder bulider = new StringBuilder();
- if (args.Method == HttpMethod.POST)
- {
- bulider.AppendLine(string.Format("POST {0} HTTP/1.1", args.Url));
- bulider.AppendLine("Content-Type: application/x-www-form-urlencoded");
- }
- else
- {
- bulider.AppendLine(string.Format("GET {0} HTTP/1.1", args.Url));
- }
-
- bulider.AppendLine(string.Format("Host: {0}:{1}", args.Host, args.Port));
-
- bulider.AppendLine("User-Agent: " + args.UA);
- //"User-Agent: Mozilla/5.0+(Compatible;+Baiduspider/2.0;++http://www.baidu.com/search/spider.html)";Mozilla/5.0 (Windows NT 6.1; IE 9.0)
- if (!string.IsNullOrEmpty(args.Referer))
- bulider.AppendLine(string.Format("Referer: {0}", args.Referer));
-
- //bulider.AppendLine("Connection: close");
-
- bulider.AppendLine("Connection: Close");
-
- if (!string.IsNullOrEmpty(args.Accept))
- bulider.AppendLine(string.Format("Accept: {0}", args.Accept));
-
- if (!string.IsNullOrEmpty(args.Cookie))
- bulider.AppendLine(string.Format("Cookie: {0}", args.Cookie));
-
- if (args.Method == HttpMethod.POST)
- {
- bulider.AppendLine(string.Format("Content-Length: {0}\r\n", Encoding.Default.GetBytes(args.Data).Length));
- bulider.Append(args.Data);
- }
- else
- {
- bulider.Append("\r\n");
- }
-
- string header = bulider.ToString();
- //Console.WriteLine(header);
- return Encoding.Default.GetBytes(header);
- }
- #endregion
-
- }
-
- public class MilliTimer
- {
- private static double times { get; set; }
- public static void start()
- {
- times = getTotalMilliseconds();
- }
-
- public static double getTimes()
- {
- return getTotalMilliseconds() - times;
- }
-
-
- public static double getTotalMilliseconds()
- {
- return DateTime.Now.Subtract(DateTime.Parse("1970-1-1")).TotalMilliseconds;
- }
- }
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.IO;
+using System.IO.Compression;
+using System.Net;
+using System.Net.Security;
+using System.Net.Sockets;
+using System.Security.Authentication;
+using System.Security.Cryptography.X509Certificates;
+using System.Text;
+using System.Text.RegularExpressions;
+using System.Threading;
+using System.Windows.Threading;
+
+namespace CityCraft
+{
+ public enum HttpMethod
+ {
+ GET,
+ POST
+ }
+ public class HttpArgs
+ {
+ public string Url { get; set; }
+ public string Host { get; set; }
+ public int Port { get; set; }
+ public string Accept { get; set; }
+ public string Referer { get; set; }
+ public string Cookie { get; set; }
+ public string Data { get; set; }
+ public string UA { get; set; }
+ public HttpMethod Method { get; set; }
+ }
+ public enum HttpReadyState
+ {
+ 未初始化,
+ 载入,
+ 载入完成,
+ 交互,
+ 完成
+ }
+ public class HttpHelper
+ {
+ public HttpReadyState readyState = HttpReadyState.未初始化;
+ public int Status = 0;
+ public string responseBody = "";
+ public string responseText = "";
+ public byte[] responseByte = null;
+ public HttpArgs args = new HttpArgs();
+ public string ErrMsg = string.Empty;
+ ///
+ /// 提交方法
+ ///
+ #region HttpWebRequest & HttpWebResponse
+
+ ///
+ /// Get方法
+ ///
+ /// 请求地址
+ /// Cookies存储器
+ /// 请求返回的Stream
+ public string Send(HttpMethod method, string url, bool Async = false)
+ {
+ if (string.IsNullOrEmpty(url))
+ {
+ return string.Empty;
+ }
+ readyState = HttpReadyState.载入;
+ ParseURL(url);
+ args.Method = method;
+ new Thread(new ThreadStart(ReciveData)).Start();
+ if (!Async)
+ {
+ while (readyState != HttpReadyState.完成) { this.DoEvent(); }
+ return responseBody;
+ }
+ return null;
+ }
+
+ ///
+ /// 模仿C#的Application.Doevent函数。可以适当添加try catch 模块
+ ///
+ public void DoEvent()
+ {
+ DispatcherFrame frame = new DispatcherFrame();
+ Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(ExitFrame), frame);
+ Dispatcher.PushFrame(frame);
+ }
+ public object ExitFrame(object f)
+ {
+ ((DispatcherFrame)f).Continue = false;
+ return null;
+ }
+
+ public void ReciveData()
+ {
+ responseBody = InternalSocketHttp();
+ readyState = HttpReadyState.完成;
+ }
+ ///
+ /// Post方法
+ ///
+ /// 请求地址
+ /// Post数据
+ /// Cllkies存储器
+ /// 请求返回的流
+ public string Post(string url,
+ byte[] bytes,
+ CookieContainer cookies,
+ Encoding encoding)
+ {
+ return null;
+ }
+
+ ///
+ /// 根据Url得到host
+ ///
+ /// url字符串
+ /// host字符串
+ private void ParseURL(string strUrl)
+ {
+ if (args == null)
+ args = new HttpArgs();
+
+ args.Host = "";
+ args.Port = 80;
+ args.Referer = "";
+ args.Cookie = "";
+ args.Url = "";
+ args.Accept = "text/html";//,application/xhtml+xml,application/xml,application/json;";
+ args.UA = "Mozilla/5.0+(Compatible;+Baiduspider/2.0;++http://www.baidu.com/search/spider.html)";
+
+ //http://www.alibaba.com/products/Egg_Laying_Block_Machine/1.html
+ int iIndex = strUrl.IndexOf(@"//");
+ if (iIndex <= 0)
+ args = null;
+ //www.alibaba.com:80/products/Egg_Laying_Block_Machine/1.html
+ string nohttpurl = strUrl.Substring(iIndex + 2);
+ string address = nohttpurl;
+ iIndex = nohttpurl.IndexOf(@"/");
+ if (iIndex > 0)
+ {
+ //www.alibaba.com:80
+ address = nohttpurl.Substring(0, iIndex);
+ args.Url = nohttpurl.Substring(iIndex);
+ }
+ else
+ {
+ args.Url = "/";
+ }
+ iIndex = nohttpurl.IndexOf(@":");
+ if (iIndex > 0)
+ {
+ string[] tempargs = address.Trim().Split(char.Parse(":"));
+ args.Host = tempargs[0];
+ args.Port = int.Parse(tempargs[1]);
+ }
+ else
+ {
+ //www.alibaba.com:80
+ args.Host = address;
+ args.Port = 80;
+ }
+ }
+ #endregion
+
+ #region Socket
+
+ string InternalSocketHttp()
+ {
+ using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
+ {
+ try
+ {
+ socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 1000);
+ socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 5000);
+ socket.Connect(args.Host, args.Port);
+ if (socket.Connected)
+ {
+ byte[] buff = ParseHttpArgs();
+ if (socket.Send(buff) > 0)
+ {
+ List responseBytes = new List();
+ byte[] buffer = new byte[1024];
+ int iNumber = socket.Receive(buffer, buffer.Length, SocketFlags.None);
+ while (iNumber > 0)//使用了Connection: Close 所以判断长度为0 时停止接受
+ {
+ responseBytes.AddRange(new List(buffer));//添加数据到List
+ iNumber = socket.Receive(buffer, buffer.Length, SocketFlags.None);//继续接收数据
+ }
+ responseByte = responseBytes.ToArray();
+ readyState = HttpReadyState.载入完成;
+ return ParseResponse(responseByte);
+ }
+ }
+ }
+ catch (Exception e)
+ {
+ ErrMsg = e.Message;
+ }
+ return string.Empty;
+ }
+ }
+
+ private string ParseResponse(byte[] responseBytes)
+ {
+ string responseStr = Encoding.UTF8.GetString(responseBytes);
+ int splitindex = responseStr.IndexOf("\r\n\r\n");
+ if (splitindex > 0)
+ {
+ string responseHeader = responseStr.Substring(0, splitindex);
+ string responseBody = responseStr.Substring(splitindex + 4);
+ //Console.WriteLine(responseHeader);
+ if (responseHeader.StartsWith("HTTP/1.1 400"))
+ {
+ Status = 400;
+ return string.Empty;
+ }
+ else if (responseHeader.StartsWith("HTTP/1.1 404"))
+ {
+ Status = 404;
+ return string.Empty;
+ }
+ else if (responseHeader.StartsWith("HTTP/1.1 302") || responseHeader.StartsWith("HTTP/1.1 301"))
+ {
+ Status = 302;
+ int start = responseHeader.ToUpper().IndexOf("LOCATION");
+ if (start > 0)
+ {
+ string temp = responseHeader.Substring(start, responseHeader.Length - start);
+ string[] sArry = Regex.Split(temp, "\r\n");
+ args.Url = sArry[0].Remove(0, 10);
+ if (args.Url == "")
+ return string.Empty;
+ return InternalSocketHttp(); //注意:302协议需要重定向
+ }
+ }
+ else if (responseHeader.StartsWith("HTTP/1.1 200")) //读取内容
+ {
+ Status = 200;
+ //解压
+ DecompressWebPage(ref responseBytes, responseHeader);
+ //转码
+ responseBody = DecodeWebStringByHttpHeader(responseBytes, responseHeader);
+ responseBody = DecodeWebStringByHtmlPageInfo(responseBytes, responseBody);
+ }
+ splitindex = responseBody.IndexOf("\r\n\r\n");
+ if (splitindex > 0)
+ responseBody = responseBody.Substring(splitindex + 4);
+ else
+ responseBody = string.Empty;
+ return responseBody;
+ }
+ return string.Empty;
+ }
+ #endregion
+
+ #region Helper
+
+ ///
+ /// 解压网页
+ ///
+ /// 网页字节数组含http头
+ /// 数组长度
+ /// Http头字符串
+ /// 网页正文开始位置
+ private void DecompressWebPage(ref byte[] responseBytes, string strHeader)
+ {
+ Regex regZip = new Regex(@"Content-Encoding:\s+gzip[^\n]*\r\n", RegexOptions.IgnoreCase);
+
+ if (regZip.IsMatch(strHeader))
+ {
+ responseBytes = Decompress(responseBytes);
+ }
+ }
+
+ ///
+ /// 解压gzip网页
+ ///
+ /// 压缩过的字符串字节数组
+ /// 解压后的字节数组
+ private byte[] Decompress(byte[] szSource)
+ {
+ MemoryStream msSource = new MemoryStream(szSource);
+ //DeflateStream 也可以这儿
+ GZipStream stream = new GZipStream(msSource, CompressionMode.Decompress);
+ byte[] szTotal = new byte[40 * 1024];
+ long lTotal = 0;
+ byte[] buffer = new byte[8];
+ int iCount = 0;
+ do
+ {
+ iCount = stream.Read(buffer, 0, 8);
+ if (szTotal.Length <= lTotal + iCount) //放大数组
+ {
+ byte[] temp = new byte[szTotal.Length * 10];
+ szTotal.CopyTo(temp, 0);
+ szTotal = temp;
+ }
+ buffer.CopyTo(szTotal, lTotal);
+ lTotal += iCount;
+ } while (iCount != 0);
+ byte[] szDest = new byte[lTotal];
+ Array.Copy(szTotal, 0, szDest, 0, lTotal);
+ return szDest;
+ }
+
+ ///
+ /// 根据Http头标记里面的字符编码解析字符串
+ ///
+ /// 网页内容字节数组(除http头以外的内容)
+ /// 网页内容字节数组长度
+ /// http头的字符串
+ /// 转好的字符串
+ private string DecodeWebStringByHttpHeader(byte[] responseBytes, string strHeader)
+ {
+ string strResponse = "";
+ if (strHeader.Contains("charset=GBK") || strHeader.Contains("charset=gb2312"))
+ {
+ strResponse = Encoding.GetEncoding("GBK").GetString(responseBytes);
+ }
+ else
+ strResponse = Encoding.UTF8.GetString(responseBytes);
+ return strResponse;
+ }
+
+ ///
+ /// 根据网页meta标记里面的字符编码解析字符串
+ ///
+ /// 网页内容字节数组(除http头以外的内容)
+ /// 网页内容字节数组长度
+ /// 网页内容字符串, 可能已经根据其它转码要求转换过的字符串
+ /// 转好的字符串
+ private string DecodeWebStringByHtmlPageInfo(byte[] responseBytes, string strResponse)
+ {
+ Regex regGB2312 = new Regex(@"]+Content-Type[^>]+gb2312[^>]*>", RegexOptions.IgnoreCase);
+ Regex regGBK = new Regex(@"]+Content-Type[^>]+gbk[^>]*>", RegexOptions.IgnoreCase);
+ Regex regBig5 = new Regex(@"]+Content-Type[^>]+Big5[^>]*>", RegexOptions.IgnoreCase);
+ if (regGB2312.IsMatch(strResponse) || regGBK.IsMatch(strResponse))
+ strResponse = Encoding.GetEncoding("GBK").GetString(responseBytes);
+ if (regBig5.IsMatch(strResponse))
+ strResponse = Encoding.GetEncoding("Big5").GetString(responseBytes);
+ return strResponse;
+ }
+
+ private byte[] ParseHttpArgs()
+ {
+ StringBuilder bulider = new StringBuilder();
+ if (args.Method == HttpMethod.POST)
+ {
+ bulider.AppendLine(string.Format("POST {0} HTTP/1.1", args.Url));
+ bulider.AppendLine("Content-Type: application/x-www-form-urlencoded");
+ }
+ else
+ {
+ bulider.AppendLine(string.Format("GET {0} HTTP/1.1", args.Url));
+ }
+
+ bulider.AppendLine(string.Format("Host: {0}:{1}", args.Host, args.Port));
+
+ bulider.AppendLine("User-Agent: " + args.UA);
+ //"User-Agent: Mozilla/5.0+(Compatible;+Baiduspider/2.0;++http://www.baidu.com/search/spider.html)";Mozilla/5.0 (Windows NT 6.1; IE 9.0)
+ if (!string.IsNullOrEmpty(args.Referer))
+ bulider.AppendLine(string.Format("Referer: {0}", args.Referer));
+
+ //bulider.AppendLine("Connection: close");
+
+ bulider.AppendLine("Connection: Close");
+
+ if (!string.IsNullOrEmpty(args.Accept))
+ bulider.AppendLine(string.Format("Accept: {0}", args.Accept));
+
+ if (!string.IsNullOrEmpty(args.Cookie))
+ bulider.AppendLine(string.Format("Cookie: {0}", args.Cookie));
+
+ if (args.Method == HttpMethod.POST)
+ {
+ bulider.AppendLine(string.Format("Content-Length: {0}\r\n", Encoding.Default.GetBytes(args.Data).Length));
+ bulider.Append(args.Data);
+ }
+ else
+ {
+ bulider.Append("\r\n");
+ }
+
+ string header = bulider.ToString();
+ //Console.WriteLine(header);
+ return Encoding.Default.GetBytes(header);
+ }
+ #endregion
+
+ }
+
+ public class MilliTimer
+ {
+ private static double times { get; set; }
+ public static void start()
+ {
+ times = getTotalMilliseconds();
+ }
+
+ public static double getTimes()
+ {
+ return getTotalMilliseconds() - times;
+ }
+
+
+ public static double getTotalMilliseconds()
+ {
+ return DateTime.Now.Subtract(DateTime.Parse("1970-1-1")).TotalMilliseconds;
+ }
+ }
}
\ No newline at end of file
diff --git a/CTZLauncher/MainWindow.xaml b/CTZLauncher/MainWindow.xaml
index 9a46a76..a524838 100644
--- a/CTZLauncher/MainWindow.xaml
+++ b/CTZLauncher/MainWindow.xaml
@@ -1,170 +1,168 @@
-
-
-
-
- 32
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+ 32
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/CTZLauncher/MainWindow.xaml.cs b/CTZLauncher/MainWindow.xaml.cs
index b9f367a..a900021 100644
--- a/CTZLauncher/MainWindow.xaml.cs
+++ b/CTZLauncher/MainWindow.xaml.cs
@@ -1,199 +1,238 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Windows;
-using System.Windows.Controls;
-using System.Windows.Data;
-using System.Windows.Documents;
-using System.Windows.Input;
-using System.Windows.Media;
-using System.Windows.Media.Imaging;
-using System.Windows.Navigation;
-using System.Windows.Shapes;
-using KMCCC.Authentication;
-using KMCCC.Launcher;
-using KMCCC.Tools;
-using KMCCC.Modules;
-using KMCCC.Modules.JVersion;
-using CityCraft;
-using System.IO;
-using System.Windows.Threading;
-using System.Windows.Media.Animation;
-
-namespace CTZLauncher
-{
- ///
- /// MainWindow.xaml 的交互逻辑
- ///
- public partial class MainWindow : Window
- {
- LauncherCore launcher = null;
- int serverport = 25580;
- public MainWindow()
- {
- InitializeComponent();
- }
-
- private void Window_Initialized(object sender, EventArgs e)
- {
- if (!Directory.Exists(".minecraft"))
- {
- Directory.CreateDirectory(".minecraft");
- }
- launcher = LauncherCore.Create(".minecraft");
- }
-
- private void barclick_MouseDown(object sender, MouseButtonEventArgs e)
- {
- Label bar = (Label)sender;
- MessageBox.Show(bar.Name);
- switch (bar.Name.Substring(4))
- {
- case "l1":
- break;
- case "l2":
- break;
- case "l3":
- break;
- case "l4":
- break;
- case "r1":
- break;
- case "r2":
- break;
- case "r3":
- break;
- case "r4":
- break;
- }
- }
-
- private void Login_Click(object sender, RoutedEventArgs e)
- {
- if (username.Text.Length == 0 || password.Text.Length == 0)
- {
- MessageBox.Show("请输入账号密码!");
- return;
- }
- Login.IsEnabled = false;
- CTZAuthenticator auth = new CTZAuthenticator(username.Text, password.Text, "citycraft.cn", serverport);
- if (auth.isLogin())
- {
- MessageBox.Show("当前玩家已登录服务器!");
- }
- else
- {
- if (!auth.Login())
- {
- if (!auth.isRegistered())
- MessageBox.Show("该用户名未注册 请先注册!");
- else
- MessageBox.Show("登录失败 账号不存在 或 密码错误 !");
- }
- else
- {
- ServerWindow.Visibility = System.Windows.Visibility.Visible;
- RotateTransform rtf = new RotateTransform();
- ServerWindow.RenderTransform = rtf;
- LoginWindow.RenderTransform = rtf;
- DoubleAnimation dbAscending = new DoubleAnimation(0, 360, new Duration(TimeSpan.FromSeconds(0.75)));
- dbAscending.RepeatBehavior = new RepeatBehavior(1);
- rtf.BeginAnimation(RotateTransform.AngleProperty, dbAscending);
- }
- }
- Login.IsEnabled = true;
- }
-
- ///
- /// 模仿C#的Application.Doevent函数。可以适当添加try catch 模块
- ///
- public void DoEvent()
- {
- DispatcherFrame frame = new DispatcherFrame();
- Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(ExitFrame), frame);
- Dispatcher.PushFrame(frame);
- }
- public object ExitFrame(object f)
- {
- ((DispatcherFrame)f).Continue = false;
- return null;
- }
-
- private void outline_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- if (e.LeftButton == MouseButtonState.Pressed)
- this.DragMove();
- }
-
- private void StartGame_Click(object sender, RoutedEventArgs e)
- {
- Console.WriteLine("StartGame");
- LaunchOptions option = new LaunchOptions();
- option.Mode = LaunchMode.MCLauncher;
- option.MaxMemory = 2048;
- option.Authenticator = new OfflineAuthenticator(username.Text); // offline
- option.Version = launcher.GetVersion("1.8");
- launcher.JavaPath = SystemTools.FindJava().First();
- launcher.GameLog += launcher_GameLog;
- launcher.Launch(option);
- }
-
- void launcher_GameLog(LaunchHandle arg1, string log)
- {
- Console.WriteLine(log);
- }
-
- private void close_Click(object sender, RoutedEventArgs e)
- {
- this.Close();
- }
-
- private void maxmem_TextChanged(object sender, TextChangedEventArgs e)
- {
- //屏蔽中文输入和非法字符粘贴输入
- TextBox textBox = sender as TextBox;
- TextChange[] change = new TextChange[e.Changes.Count];
- e.Changes.CopyTo(change, 0);
-
- int offset = change[0].Offset;
- if (change[0].AddedLength > 0)
- {
- double num = 0;
- if (!Double.TryParse(textBox.Text, out num))
- {
- textBox.Text = textBox.Text.Remove(offset, change[0].AddedLength);
- textBox.Select(offset, 0);
- }
- }
- }
-
- private void register_Click(object sender, RoutedEventArgs e)
- {
- if (username.Text.Length == 0 || password.Text.Length == 0)
- {
- MessageBox.Show("请输入账号密码!");
- return;
- }
- CTZAuthenticator auth = new CTZAuthenticator(username.Text, password.Text, "citycraft.cn", serverport);
- if (auth.isRegistered())
- {
- MessageBox.Show("该用户名已注册 请更换用户名!");
- return;
- }
- if (auth.Register())
- {
- MessageBox.Show("注册成功!");
- auth.Login();
- ServerWindow.Visibility = System.Windows.Visibility.Visible;
- }
- else
- MessageBox.Show("注册失败!");
- }
-
- private void s(object sender, RoutedEventArgs e)
- {
-
- }
- }
-}
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Data;
+using System.Windows.Documents;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+using System.Windows.Navigation;
+using System.Windows.Shapes;
+using KMCCC.Authentication;
+using KMCCC.Launcher;
+using KMCCC.Tools;
+using KMCCC.Modules;
+using KMCCC.Modules.JVersion;
+using CityCraft;
+using System.IO;
+using System.Windows.Threading;
+using System.Windows.Media.Animation;
+using LitJson;
+using CTZLauncher.Modules.CTZServer;
+
+namespace CTZLauncher
+{
+ ///
+ /// MainWindow.xaml 的交互逻辑
+ ///
+ public partial class MainWindow : Window
+ {
+ LauncherCore launcher = null;
+ LaunchOptions option = new LaunchOptions();
+ string serveraddress = "";
+ int serverport = 25580;
+
+ #region 初始化部分
+ public MainWindow()
+ {
+ InitializeComponent();
+ }
+
+ private void Window_Initialized(object sender, EventArgs e)
+ {
+ if (!Directory.Exists(".minecraft"))
+ {
+ Directory.CreateDirectory(".minecraft");
+ }
+ launcher = LauncherCore.Create(".minecraft");
+ }
+ #endregion
+
+ #region 登录界面
+ private void barclick_MouseDown(object sender, MouseButtonEventArgs e)
+ {
+ Label bar = (Label)sender;
+ MessageBox.Show(bar.Name);
+ switch (bar.Name.Substring(4))
+ {
+ case "l1":
+ break;
+ case "l2":
+ break;
+ case "l3":
+ break;
+ case "l4":
+ break;
+ case "r1":
+ break;
+ case "r2":
+ break;
+ case "r3":
+ break;
+ case "r4":
+ break;
+ }
+ }
+
+ private void Login_Click(object sender, RoutedEventArgs e)
+ {
+ if (username.Text.Length == 0 || password.Text.Length == 0)
+ {
+ MessageBox.Show("请输入账号密码!");
+ return;
+ }
+ Login.IsEnabled = false;
+ CTZAuthenticator auth = new CTZAuthenticator(username.Text, password.Text, serveraddress, serverport);
+ if (auth.isLogin())
+ {
+ MessageBox.Show("当前玩家已登录服务器!");
+ }
+ else
+ {
+ if (!auth.Login())
+ {
+ if (!auth.isRegistered())
+ MessageBox.Show("该用户名未注册 请先注册!");
+ else
+ MessageBox.Show("登录失败 账号不存在 或 密码错误 !");
+ }
+ else
+ {
+ ServerWindow.Visibility = System.Windows.Visibility.Visible;
+ RotateTransform rtf = new RotateTransform();
+ ServerWindow.RenderTransform = rtf;
+ LoginWindow.RenderTransform = rtf;
+ DoubleAnimation dbAscending = new DoubleAnimation(0, 360, new Duration(TimeSpan.FromSeconds(0.75)));
+ dbAscending.RepeatBehavior = new RepeatBehavior(1);
+ rtf.BeginAnimation(RotateTransform.AngleProperty, dbAscending);
+ CTZServer areas = JsonMapper.ToObject(auth.getServerList());
+ LoadAreas(areas);
+ }
+ }
+ Login.IsEnabled = true;
+ }
+
+ private void outline_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
+ {
+ if (e.LeftButton == MouseButtonState.Pressed)
+ this.DragMove();
+ }
+
+ private void close_Click(object sender, RoutedEventArgs e)
+ {
+ this.Close();
+ }
+
+ private void maxmem_TextChanged(object sender, TextChangedEventArgs e)
+ {
+ //屏蔽中文输入和非法字符粘贴输入
+ TextBox textBox = sender as TextBox;
+ TextChange[] change = new TextChange[e.Changes.Count];
+ e.Changes.CopyTo(change, 0);
+
+ int offset = change[0].Offset;
+ if (change[0].AddedLength > 0)
+ {
+ double num = 0;
+ if (!Double.TryParse(textBox.Text, out num))
+ {
+ textBox.Text = textBox.Text.Remove(offset, change[0].AddedLength);
+ textBox.Select(offset, 0);
+ }
+ }
+ }
+
+ private void register_Click(object sender, RoutedEventArgs e)
+ {
+ if (username.Text.Length == 0 || password.Text.Length == 0)
+ {
+ MessageBox.Show("请输入账号密码!");
+ return;
+ }
+ CTZAuthenticator auth = new CTZAuthenticator(username.Text, password.Text, "citycraft.cn", serverport);
+ if (auth.isRegistered())
+ {
+ MessageBox.Show("该用户名已注册 请更换用户名!");
+ return;
+ }
+ if (auth.Register())
+ {
+ MessageBox.Show("注册成功!");
+ auth.Login();
+ ServerWindow.Visibility = System.Windows.Visibility.Visible;
+ }
+ else
+ MessageBox.Show("注册失败!");
+ }
+
+ private void forget_Click(object sender, RoutedEventArgs e)
+ {
+
+ }
+ #endregion
+
+ #region 服务器选择与启动
+ private void LoadAreas(CTZServer areas)
+ {
+ for (int i = 1; i <= areas.Areas.Count; i++)
+ {
+ Area area = areas.Areas[i];
+ RadioButton arearb = GameArea.FindName("a" + i) as RadioButton;
+ arearb.Content = area.Name;
+ arearb.Visibility = Visibility.Visible;
+ arearb.Tag = area.Servers;
+ }
+ }
+
+ private void LoadServers(List servers)
+ {
+ for (int i = 1; i <= servers.Count; i++)
+ {
+ Server server = servers[i];
+ RadioButton serverrb = GameArea.FindName("s" + i) as RadioButton;
+ serverrb.Content = server.Name;
+ serverrb.Visibility = Visibility.Visible;
+ serverrb.Tag = server;
+ }
+ }
+
+ private void area_Checked(object sender, RoutedEventArgs e)
+ {
+ RadioButton rb = (RadioButton)sender;
+ LoadServers((List)rb.Tag);
+ }
+
+ private void server_Checked(object sender, RoutedEventArgs e)
+ {
+ RadioButton rb = (RadioButton)sender;
+ Server server = (Server)rb.Tag;
+ option.Server = new ServerInfo
+ {
+ Address = server.Address,
+ Port = server.Port
+ };
+ option.Version = launcher.GetVersion(server.Version);
+ }
+
+ private void StartGame_Click(object sender, RoutedEventArgs e)
+ {
+ Console.WriteLine("StartGame");
+ option.Mode = LaunchMode.MCLauncher;
+ option.MaxMemory = 2048;
+ option.Authenticator = new OfflineAuthenticator(username.Text); // offline
+ option.Version = launcher.GetVersion("1.8");
+ launcher.JavaPath = SystemTools.FindJava().First();
+ launcher.GameLog += launcher_GameLog;
+ launcher.Launch(option);
+ }
+
+ void launcher_GameLog(LaunchHandle arg1, string log)
+ {
+ Console.WriteLine(log);
+ }
+ #endregion
+ }
+}
diff --git a/CTZLauncher/Modules/CTZServer/CTZServer.cs b/CTZLauncher/Modules/CTZServer/CTZServer.cs
new file mode 100644
index 0000000..ad25e42
--- /dev/null
+++ b/CTZLauncher/Modules/CTZServer/CTZServer.cs
@@ -0,0 +1,87 @@
+using LitJson;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace CTZLauncher.Modules.CTZServer
+{
+ ///
+ /// 实例化服务器信息
+ ///
+ public class CTZServer
+ {
+ ///
+ /// 大区列表
+ ///
+ [JsonPropertyName("areas")]
+ public List Areas { get; set; }
+ }
+ ///
+ /// 服务器大区信息
+ ///
+ public class Area
+ {
+ ///
+ /// 大区名称
+ ///
+ [JsonPropertyName("name")]
+ public string Name { get; set; }
+ ///
+ /// 大区服务器列表
+ ///
+ [JsonPropertyName("servers")]
+ public List Servers { get; set; }
+ }
+ ///
+ /// 服务器信息
+ ///
+ public class Server
+ {
+ ///
+ /// 服务器名称
+ ///
+ [JsonPropertyName("name")]
+ public string Name { get; set; }
+
+ ///
+ /// 服务器地址
+ ///
+ [JsonPropertyName("address")]
+ public string Address { get; set; }
+
+ ///
+ /// 服务器端口
+ ///
+ [JsonPropertyName("port")]
+ public ushort Port { get; set; }
+
+ ///
+ /// 服务器简介
+ ///
+ [JsonPropertyName("info")]
+ public string Info { get; set; }
+
+ ///
+ /// 客户端版本
+ ///
+ [JsonPropertyName("version")]
+ public string Version { get; set; }
+
+ ///
+ /// 客户端下载地址
+ ///
+ [JsonPropertyName("url")]
+ public string Url { get; set; }
+
+ ///
+ /// 获得服务器链接
+ ///
+ /// 服务器链接
+ public override string ToString()
+ {
+ return string.Format("{0}:{1}", Address, Port);
+ }
+ }
+
+}