mirror of
https://e.coding.net/circlecloud/CTZLauncher.git
synced 2024-11-24 02:28:49 +00:00
选择服务器后自动获取服务器在线人数...
This commit is contained in:
parent
a0da921432
commit
86b5e916f9
@ -97,6 +97,8 @@
|
||||
<SubType>Designer</SubType>
|
||||
</ApplicationDefinition>
|
||||
<Compile Include="Modules\CTZServer\CTZServer.cs" />
|
||||
<Compile Include="Modules\Minecraft\ServerInfo.cs" />
|
||||
<Compile Include="Tools\MinecraftTools.cs" />
|
||||
<Compile Include="Tools\SystemTools.cs" />
|
||||
<Compile Include="Tools\UsefulTools.cs" />
|
||||
<Compile Include="Tools\ZipTools.cs" />
|
||||
|
@ -42,10 +42,6 @@
|
||||
<Image HorizontalAlignment="Left" Height="50" Margin="15,5,0,0" VerticalAlignment="Top" Width="50" Source="ico.ico" RenderTransformOrigin="0.56,0.24"/>
|
||||
<Label Content="魔方" HorizontalAlignment="Left" Height="50" Margin="90,5,0,0" VerticalAlignment="Top" Width="74" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontFamily="STXingkai" Foreground="White" FontSize="35" />
|
||||
</Grid>
|
||||
|
||||
|
||||
|
||||
|
||||
<Grid Name="LoginWindow" Margin="20,60,20,20" Background="White">
|
||||
<Grid Height="270" VerticalAlignment="Top">
|
||||
<Grid Margin="20,20,0,0" HorizontalAlignment="Left" Width="400" >
|
||||
@ -105,10 +101,6 @@
|
||||
</Grid>
|
||||
</Grid>
|
||||
</Grid>
|
||||
|
||||
|
||||
|
||||
|
||||
<Grid Margin="20,60,20,20" Background="White" x:Name="ServerWindow" Visibility="Hidden">
|
||||
<!--Visibility="Hidden"-->
|
||||
<Grid HorizontalAlignment="Left" Width="400" Margin="20,20,0,20">
|
||||
|
@ -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;
|
||||
@ -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)
|
||||
|
49
CTZLauncher/Modules/Minecraft/Serverinfo.cs
Normal file
49
CTZLauncher/Modules/Minecraft/Serverinfo.cs
Normal file
@ -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<Mod> 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; }
|
||||
}
|
||||
}
|
98
CTZLauncher/Tools/MinecraftTools.cs
Normal file
98
CTZLauncher/Tools/MinecraftTools.cs
Normal file
@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// 获取服务器详细信息
|
||||
/// </summary>
|
||||
/// <param name="address">服务器地址</param>
|
||||
/// <param name="port">服务器端口</param>
|
||||
public MinecraftServer(string address, int port)
|
||||
{
|
||||
this.address = address;
|
||||
this.port = port;
|
||||
}
|
||||
/// <summary>
|
||||
/// 模仿C#的Application.Doevent函数。可以适当添加try catch 模块
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
/// <summary>
|
||||
/// 异步获取服务器信息
|
||||
/// </summary>
|
||||
/// <returns>服务器信息</returns>
|
||||
public ServerInfo DoAsync()
|
||||
{
|
||||
new Thread(new ThreadStart(GetInfoAsync)).Start();
|
||||
while (finish == false) { DoEvent(); }
|
||||
return serverinfo;
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取服务器信息
|
||||
/// </summary>
|
||||
/// <returns>服务器信息</returns>
|
||||
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<ServerInfo>(info);
|
||||
}
|
||||
Console.WriteLine("网络连接失败!");
|
||||
return null;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e.Message);
|
||||
Console.WriteLine(e.StackTrace);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user