diff --git a/CTZLauncher/CTZLauncher.csproj b/CTZLauncher/CTZLauncher.csproj index 3ea860e..cce0e0b 100644 --- a/CTZLauncher/CTZLauncher.csproj +++ b/CTZLauncher/CTZLauncher.csproj @@ -97,6 +97,8 @@ Designer + + @@ -197,6 +199,25 @@ true + + + + + + + + + + + + + + + + + + + diff --git a/CTZLauncher/MainWindow.xaml.cs b/CTZLauncher/MainWindow.xaml.cs index 5dd849f..82cbb6b 100644 --- a/CTZLauncher/MainWindow.xaml.cs +++ b/CTZLauncher/MainWindow.xaml.cs @@ -1,4 +1,5 @@ using CTZLauncher.Modules.CTZServer; +using CTZLauncher.Tools; using KMCCC.Authentication; using KMCCC.Launcher; using KMCCC.Tools; @@ -8,6 +9,7 @@ using System.Collections.Generic; using System.Configuration; using System.IO; using System.Linq; +using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Input; @@ -94,7 +96,7 @@ namespace CTZLauncher } else { - ServerWindow.Visibility = System.Windows.Visibility.Visible; + ServerWindow.Visibility = Visibility.Visible; RotateTransform rtf = new RotateTransform(); ServerWindow.RenderTransform = rtf; LoginWindow.RenderTransform = rtf; @@ -237,6 +239,13 @@ namespace CTZLauncher serverinfo.Content = server.Info; selserver.Content = server.Name; needclient.Content = server.Version; + online.Content = "获取中..."; + MinecraftServer ms = new MinecraftServer(server.Address, server.Port); + var info = ms.DoAsync(); + if (info != null) + online.Content = info.Players.Online + "/" + info.Players.Max; + else + online.Content = "获取失败!"; } private void StartGame_Click(object sender, RoutedEventArgs e) @@ -262,5 +271,17 @@ namespace CTZLauncher Console.WriteLine(log); } #endregion + + private void username_KeyDown(object sender, KeyEventArgs e) + { + if (e.Key == Key.Enter) + password.Focus(); + } + + private void password_KeyDown(object sender, KeyEventArgs e) + { + if (e.Key == Key.Enter) + Login_Click(sender, e); + } } } diff --git a/CTZLauncher/Modules/Minecraft/Serverinfo.cs b/CTZLauncher/Modules/Minecraft/Serverinfo.cs new file mode 100644 index 0000000..bd38b35 --- /dev/null +++ b/CTZLauncher/Modules/Minecraft/Serverinfo.cs @@ -0,0 +1,49 @@ +using LitJson; +using System.Collections.Generic; + +namespace CTZLauncher.Modules.Minecraft +{ + public class ServerInfo + { + [JsonPropertyName("description")] + public string Description { get; set; } + [JsonPropertyName("players")] + public Players Players { get; set; } + [JsonPropertyName("version")] + public Version Version { get; set; } + [JsonPropertyName("modinfo")] + public ModInfo ModInfo { get; set; } + } + + public class ModInfo + { + [JsonPropertyName("type")] + public string Type { get; set; } + [JsonPropertyName("modList")] + public List ModList { get; set; } + } + + public class Mod + { + [JsonPropertyName("modid")] + public string Modid { get; set; } + [JsonPropertyName("version")] + public string Version { get; set; } + } + + public class Version + { + [JsonPropertyName("name")] + public string Name { get; set; } + [JsonPropertyName("protocol")] + public int Protocol { get; set; } + } + + public class Players + { + [JsonPropertyName("max")] + public int Max { get; set; } + [JsonPropertyName("online")] + public int Online { get; set; } + } +} diff --git a/CTZLauncher/Tools/MinecraftTools.cs b/CTZLauncher/Tools/MinecraftTools.cs new file mode 100644 index 0000000..c9aba2b --- /dev/null +++ b/CTZLauncher/Tools/MinecraftTools.cs @@ -0,0 +1,98 @@ +using CTZLauncher.Modules.Minecraft; +using LitJson; +using System; +using System.Net.Sockets; +using System.Text; +using System.Threading; +using System.Windows.Threading; + +namespace CTZLauncher.Tools +{ + public class MinecraftServer + { + string address; + int port; + bool finish; + ServerInfo serverinfo; + + /// + /// 获取服务器详细信息 + /// + /// 服务器地址 + /// 服务器端口 + public MinecraftServer(string address, int port) + { + this.address = address; + this.port = port; + } + /// + /// 模仿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 ServerInfo DoAsync() + { + new Thread(new ThreadStart(GetInfoAsync)).Start(); + while (finish == false) { DoEvent(); } + return serverinfo; + } + /// + /// 获取服务器信息 + /// + /// 服务器信息 + public ServerInfo Do() + { + return GetInfo(); + } + public void GetInfoAsync() + { + finish = false; + serverinfo = GetInfo(); + finish = true; + } + public ServerInfo GetInfo() + { + using (Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) + { + try + { + client.Connect(address, port); + if (client.Connected) + { + client.Send(new byte[] { 0x0f, 0x00, 0x04, 0x09, 0x31, 0x32, 0x37, 0x2e, 0x30, 0x2e, 0x30, 0x2e, 0x31, 0x63, 0xdd, 0x01 }); + client.Send(new byte[] { 0x01, 0x00 }); + byte[] header = new byte[5]; + client.Receive(header, header.Length, SocketFlags.None); + byte[] buffer = new byte[1024]; + int recover = client.Receive(buffer, buffer.Length, SocketFlags.None); + byte[] data = new byte[recover]; + Array.Copy(buffer, data, recover); + string info = Encoding.UTF8.GetString(data); + return JsonMapper.ToObject(info); + } + Console.WriteLine("网络连接失败!"); + return null; + } + catch (Exception e) + { + Console.WriteLine(e.Message); + Console.WriteLine(e.StackTrace); + return null; + } + } + } + } +} diff --git a/CTZLauncher/bg/bg10.jpg b/CTZLauncher/bg/bg10.jpg new file mode 100644 index 0000000..09b03a2 Binary files /dev/null and b/CTZLauncher/bg/bg10.jpg differ diff --git a/CTZLauncher/bg/bg11.jpg b/CTZLauncher/bg/bg11.jpg new file mode 100644 index 0000000..e61beb1 Binary files /dev/null and b/CTZLauncher/bg/bg11.jpg differ diff --git a/CTZLauncher/bg/bg12.jpg b/CTZLauncher/bg/bg12.jpg new file mode 100644 index 0000000..ee57a8f Binary files /dev/null and b/CTZLauncher/bg/bg12.jpg differ diff --git a/CTZLauncher/bg/bg13.jpg b/CTZLauncher/bg/bg13.jpg new file mode 100644 index 0000000..d00a71c Binary files /dev/null and b/CTZLauncher/bg/bg13.jpg differ diff --git a/CTZLauncher/bg/bg14.jpg b/CTZLauncher/bg/bg14.jpg new file mode 100644 index 0000000..9e1d4ed Binary files /dev/null and b/CTZLauncher/bg/bg14.jpg differ diff --git a/CTZLauncher/bg/bg15.jpg b/CTZLauncher/bg/bg15.jpg new file mode 100644 index 0000000..9c03a8b Binary files /dev/null and b/CTZLauncher/bg/bg15.jpg differ diff --git a/CTZLauncher/bg/bg16.jpg b/CTZLauncher/bg/bg16.jpg new file mode 100644 index 0000000..e8ed0fc Binary files /dev/null and b/CTZLauncher/bg/bg16.jpg differ diff --git a/CTZLauncher/bg/bg17.jpg b/CTZLauncher/bg/bg17.jpg new file mode 100644 index 0000000..915356f Binary files /dev/null and b/CTZLauncher/bg/bg17.jpg differ diff --git a/CTZLauncher/bg/bg18.jpg b/CTZLauncher/bg/bg18.jpg new file mode 100644 index 0000000..f1754e8 Binary files /dev/null and b/CTZLauncher/bg/bg18.jpg differ diff --git a/CTZLauncher/bg/bg19.jpg b/CTZLauncher/bg/bg19.jpg new file mode 100644 index 0000000..27ca000 Binary files /dev/null and b/CTZLauncher/bg/bg19.jpg differ diff --git a/CTZLauncher/bg/bg20.jpg b/CTZLauncher/bg/bg20.jpg new file mode 100644 index 0000000..2d9d3b8 Binary files /dev/null and b/CTZLauncher/bg/bg20.jpg differ diff --git a/CTZLauncher/bg/bg21.jpg b/CTZLauncher/bg/bg21.jpg new file mode 100644 index 0000000..803c27a Binary files /dev/null and b/CTZLauncher/bg/bg21.jpg differ diff --git a/CTZLauncher/bg/bg22.jpg b/CTZLauncher/bg/bg22.jpg new file mode 100644 index 0000000..6176364 Binary files /dev/null and b/CTZLauncher/bg/bg22.jpg differ diff --git a/CTZLauncher/bg/bg6.jpg b/CTZLauncher/bg/bg6.jpg new file mode 100644 index 0000000..b90af49 Binary files /dev/null and b/CTZLauncher/bg/bg6.jpg differ diff --git a/CTZLauncher/bg/bg7.jpg b/CTZLauncher/bg/bg7.jpg new file mode 100644 index 0000000..3161eda Binary files /dev/null and b/CTZLauncher/bg/bg7.jpg differ diff --git a/CTZLauncher/bg/bg8.jpg b/CTZLauncher/bg/bg8.jpg new file mode 100644 index 0000000..b7ff82c Binary files /dev/null and b/CTZLauncher/bg/bg8.jpg differ diff --git a/CTZLauncher/bg/bg9.jpg b/CTZLauncher/bg/bg9.jpg new file mode 100644 index 0000000..192dfc2 Binary files /dev/null and b/CTZLauncher/bg/bg9.jpg differ diff --git a/MinecraftServerInfo/Program.cs b/MinecraftServerInfo/Program.cs index c774402..8fab712 100644 --- a/MinecraftServerInfo/Program.cs +++ b/MinecraftServerInfo/Program.cs @@ -11,7 +11,7 @@ namespace MinecraftServerInfo { static void Main(string[] args) { - using (Socket client = new Socket(SocketType.Stream, ProtocolType.Tcp)) + using (Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) try { client.Connect("four.mengcraft.com", 11133); @@ -19,7 +19,7 @@ namespace MinecraftServerInfo if (client.Connected) { Console.WriteLine("发送数据"); - client.Send(new byte[] { 0x0F, 0x00, 0x2F, 0x09, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30, 0x2E, 0x31, 0x63, 0xDD, 0x01 }); + client.Send(new byte[] { 0x0F, 0x00, 0x04, 0x09, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30, 0x2E, 0x31, 0x63, 0xDD, 0x01 }); client.Send(new byte[] { 0x01, 0x00 }); Console.WriteLine("读取头数据"); byte[] header = new byte[5];