1
0
mirror of https://e.coding.net/circlecloud/CTZLauncher.git synced 2024-11-22 02:08:49 +00:00

完成服务器列表获取模块 添加启动标题修改 修复HttpHelper获取到空数组...

This commit is contained in:
j502647092 2015-08-10 20:45:44 +08:00
parent 282d195955
commit f739a359bf
9 changed files with 372 additions and 291 deletions

View File

@ -69,12 +69,17 @@ namespace KMCCC.Authentication
public string getServerList() public string getServerList()
{ {
string sl = http.Send(HttpMethod.GET, Address + ":" + Port + "/serverlist"); string sl = http.Send(HttpMethod.GET, Address + ":" + Port + "/serverlist");
return sl.Replace("\0",""); if (string.IsNullOrEmpty(sl))
throw new ArgumentNullException("服务器返回了空的字符串或网络连接异常!");
return sl;
} }
public bool getResult(string url) public bool getResult(string url)
{ {
return http.Send(HttpMethod.GET, url).Contains("true") ? true : false; string result = http.Send(HttpMethod.GET, url);
if (string.IsNullOrEmpty(result))
throw new ArgumentNullException("服务器返回了空的字符串或网络连接异常!");
return result.Contains("true") ? true : false;
} }
} }
} }

View File

@ -187,7 +187,14 @@ namespace CityCraft
int iNumber = socket.Receive(buffer, buffer.Length, SocketFlags.None); int iNumber = socket.Receive(buffer, buffer.Length, SocketFlags.None);
while (iNumber > 0)//使用了Connection: Close 所以判断长度为0 时停止接受 while (iNumber > 0)//使用了Connection: Close 所以判断长度为0 时停止接受
{ {
responseBytes.AddRange(new List<byte>(buffer));//添加数据到List if (iNumber < 1024)
{
byte[] shotbuffer = new byte[iNumber];
Array.Copy(buffer, shotbuffer, iNumber);
responseBytes.AddRange(new List<byte>(shotbuffer));//添加数据到List
}
else
responseBytes.AddRange(new List<byte>(buffer));//添加数据到List
iNumber = socket.Receive(buffer, buffer.Length, SocketFlags.None);//继续接收数据 iNumber = socket.Receive(buffer, buffer.Length, SocketFlags.None);//继续接收数据
} }
responseByte = responseBytes.ToArray(); responseByte = responseBytes.ToArray();

View File

@ -43,7 +43,7 @@
{ {
public override bool Operate(LauncherCore core, MinecraftLaunchArguments args) public override bool Operate(LauncherCore core, MinecraftLaunchArguments args)
{ {
args.Tokens["game_directory"] = String.Format(@".\versions\{0}\", args.Version.Id); args.Tokens["game_directory"] = String.Format(@".minecraft\versions\{0}\", args.Version.Id);
return true; return true;
} }
} }

View File

@ -109,15 +109,20 @@
} }
/// <summary> /// <summary>
/// 游戏退出事件 /// 游戏退出事件
/// </summary> /// </summary>
public event Action<LaunchHandle, int> GameExit; public event Action<LaunchHandle, int> GameExit;
/// <summary> /// <summary>
/// 游戏Log事件 /// 游戏Log事件
/// </summary> /// </summary>
public event Action<LaunchHandle, string> GameLog; public event Action<LaunchHandle, string> GameLog;
}
/// <summary>
/// 游戏启动事件
/// </summary>
public event Action<LaunchHandle, string> GameStart;
}
/// <summary> /// <summary>
/// 启动器核心选项 /// 启动器核心选项

View File

@ -66,7 +66,7 @@
args.Tokens.Add("user_properties", authentication.Properties); args.Tokens.Add("user_properties", authentication.Properties);
args.Tokens.Add("user_type", authentication.UserType); args.Tokens.Add("user_type", authentication.UserType);
args.AdvencedArguments = new List<string> { "-Dfml.ignoreInvalidMinecraftCertificates=true -Dfml.ignorePatchDiscrepancies=true" }; args.AdvencedArguments = new List<string> { "-Dfml.ignoreInvalidMinecraftCertificates=true", "-Dfml.ignorePatchDiscrepancies=true" };
args.Authentication = authentication; args.Authentication = authentication;
args.Version = options.Version; args.Version = options.Version;
@ -139,6 +139,7 @@
}) })
}; };
handle.Work(); handle.Work();
Start(handle, handle.GetTile());
Task.Factory.StartNew(handle.Process.WaitForExit).ContinueWith(t => Exit(handle, handle.Process.ExitCode)); Task.Factory.StartNew(handle.Process.WaitForExit).ContinueWith(t => Exit(handle, handle.Process.ExitCode));
return new LaunchResult { Success = true, Handle = handle }; return new LaunchResult { Success = true, Handle = handle };
} }
@ -212,6 +213,14 @@
} }
} }
internal void Start(LaunchHandle handle, string title)
{
if (GameStart != null)
{
GameStart(handle, title);
}
}
#endregion #endregion
} }
} }

View File

@ -116,36 +116,36 @@
<Label Content="选择大区" Margin="155,20" FontSize="20" VerticalAlignment="Center" VerticalContentAlignment="Center"/> <Label Content="选择大区" Margin="155,20" FontSize="20" VerticalAlignment="Center" VerticalContentAlignment="Center"/>
</Grid> </Grid>
<Grid Height="100" VerticalAlignment="Top" Margin="0,80,0,0" Name="GameArea"> <Grid Height="100" VerticalAlignment="Top" Margin="0,80,0,0" Name="GameArea">
<RadioButton Name="a0" Content="X X大区" Margin="30,30,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="70" Visibility="Hidden" Checked="area_Checked"/> <Button Name="a0" BorderBrush="#FF3299CC" Content="X X大区" Template="{StaticResource CornerButton}" HorizontalAlignment="Left" Margin="30,30,0,0" VerticalAlignment="Top" Width="75" Visibility="Hidden" Click="area_Click"/>
<RadioButton Name="a1" Content="X X大区" Margin="120,30,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="70" Visibility="Hidden" Checked="area_Checked"/> <Button Name="a1" BorderBrush="#FF3299CC" Content="X X大区" Template="{StaticResource CornerButton}" HorizontalAlignment="Left" Margin="120,30,0,0" VerticalAlignment="Top" Width="75" Visibility="Hidden" Click="area_Click"/>
<RadioButton Name="a2" Content="X X大区" Margin="215,30,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="70" Visibility="Hidden" Checked="area_Checked"/> <Button Name="a2" BorderBrush="#FF3299CC" Content="X X大区" Template="{StaticResource CornerButton}" HorizontalAlignment="Left" Margin="215,30,0,0" VerticalAlignment="Top" Width="75" Visibility="Hidden" Click="area_Click"/>
<RadioButton Name="a3" Content="X X大区" Margin="305,30,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="70" Visibility="Hidden" Checked="area_Checked"/> <Button Name="a3" BorderBrush="#FF3299CC" Content="X X大区" Template="{StaticResource CornerButton}" HorizontalAlignment="Left" Margin="305,30,0,0" VerticalAlignment="Top" Width="75" Visibility="Hidden" Click="area_Click"/>
<RadioButton Name="a4" Content="X X大区" Margin="30,60,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="70" Visibility="Hidden" Checked="area_Checked"/> <Button Name="a4" BorderBrush="#FF3299CC" Content="X X大区" Template="{StaticResource CornerButton}" HorizontalAlignment="Left" Margin="30,60,0,0" VerticalAlignment="Top" Width="75" Visibility="Hidden" Click="area_Click"/>
<RadioButton Name="a5" Content="X X大区" Margin="120,60,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="70" Visibility="Hidden" Checked="area_Checked"/> <Button Name="a5" BorderBrush="#FF3299CC" Content="X X大区" Template="{StaticResource CornerButton}" HorizontalAlignment="Left" Margin="120,60,0,0" VerticalAlignment="Top" Width="75" Visibility="Hidden" Click="area_Click"/>
<RadioButton Name="a6" Content="X X大区" Margin="215,60,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="70" Visibility="Hidden" Checked="area_Checked"/> <Button Name="a6" BorderBrush="#FF3299CC" Content="X X大区" Template="{StaticResource CornerButton}" HorizontalAlignment="Left" Margin="215,60,0,0" VerticalAlignment="Top" Width="75" Visibility="Hidden" Click="area_Click"/>
<RadioButton Name="a7" Content="X X大区" Margin="305,60,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="70" Visibility="Hidden" Checked="area_Checked"/> <Button Name="a7" BorderBrush="#FF3299CC" Content="X X大区" Template="{StaticResource CornerButton}" HorizontalAlignment="Left" Margin="305,60,0,0" VerticalAlignment="Top" Width="75" Visibility="Hidden" Click="area_Click"/>
</Grid> </Grid>
<Grid Height="285" VerticalAlignment="Top" Margin="10,185,10,0"> <Grid Height="285" VerticalAlignment="Top" Margin="10,185,10,0">
<Grid Height="50" VerticalAlignment="Top" Margin="10,0,10,0"> <Grid Height="50" VerticalAlignment="Top" Margin="10,0,10,0">
<Label Content="选择服务器" Margin="125,8" FontSize="20" VerticalContentAlignment="Center" VerticalAlignment="Center"/> <Label Content="选择服务器" Margin="125,8" FontSize="20" VerticalContentAlignment="Center" VerticalAlignment="Center"/>
</Grid> </Grid>
<Grid Margin="10,55,10,10" Name="GameServer"> <Grid Margin="10,55,10,10" Name="GameServer">
<RadioButton Name="s0" Content="服务器1" Margin="50,40,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="80" Visibility="Hidden" Checked="server_Checked"/> <Button Name="s0" Content="服务器1" Template="{StaticResource CornerButton}" BorderBrush="#FF3299CC" Margin="50,40,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="80" Visibility="Hidden" Click="server_Click"/>
<RadioButton Name="s1" Content="服务器2" Margin="150,40,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="80" Visibility="Hidden" Checked="server_Checked"/> <Button Name="s1" Content="服务器2" Template="{StaticResource CornerButton}" BorderBrush="#FF3299CC" Margin="150,40,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="80" Visibility="Hidden" Click="server_Click"/>
<RadioButton Name="s2" Content="服务器3" Margin="252,40,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="80" Visibility="Hidden" Checked="server_Checked"/> <Button Name="s2" Content="服务器3" Template="{StaticResource CornerButton}" BorderBrush="#FF3299CC" Margin="252,40,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="80" Visibility="Hidden" Click="server_Click"/>
<RadioButton Name="s3" Content="服务器4" Margin="50,80,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="80" Visibility="Hidden" Checked="server_Checked"/> <Button Name="s3" Content="服务器4" Template="{StaticResource CornerButton}" BorderBrush="#FF3299CC" Margin="50,80,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="80" Visibility="Hidden" Click="server_Click"/>
<RadioButton Name="s4" Content="服务器5" Margin="150,80,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="80" Visibility="Hidden" Checked="server_Checked" /> <Button Name="s4" Content="服务器5" Template="{StaticResource CornerButton}" BorderBrush="#FF3299CC" Margin="150,80,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="80" Visibility="Hidden" Click="server_Click"/>
<RadioButton Name="s5" Content="服务器6" Margin="252,80,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="80" Visibility="Hidden" Checked="server_Checked"/> <Button Name="s5" Content="服务器6" Template="{StaticResource CornerButton}" BorderBrush="#FF3299CC" Margin="252,80,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="80" Visibility="Hidden" Click="server_Click"/>
<RadioButton Name="s6" Content="服务器7" Margin="50,120,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="80" Visibility="Hidden" Checked="server_Checked"/> <Button Name="s6" Content="服务器7" Template="{StaticResource CornerButton}" BorderBrush="#FF3299CC" Margin="50,120,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="80" Visibility="Hidden" Click="server_Click"/>
<RadioButton Name="s7" Content="服务器8" Margin="150,120,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="80" Visibility="Hidden" Checked="server_Checked"/> <Button Name="s7" Content="服务器8" Template="{StaticResource CornerButton}" BorderBrush="#FF3299CC" Margin="150,120,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="80" Visibility="Hidden" Click="server_Click"/>
<RadioButton Name="s8" Content="服务器9" Margin="252,120,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="80" Visibility="Hidden" Checked="server_Checked"/> <Button Name="s8" Content="服务器9" Template="{StaticResource CornerButton}" BorderBrush="#FF3299CC" Margin="252,120,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="80" Visibility="Hidden" Click="server_Click"/>
<RadioButton Name="s9" Content="服务器10" Margin="50,160,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="80" Visibility="Hidden" Checked="server_Checked"/> <Button Name="s9" Content="服务器10" Template="{StaticResource CornerButton}" BorderBrush="#FF3299CC" Margin="50,160,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="80" Visibility="Hidden" Click="server_Click"/>
<RadioButton Name="s10" Content="服务器11" Margin="150,160,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="80" Visibility="Hidden" Checked="server_Checked"/> <Button Name="s10" Content="服务器11" Template="{StaticResource CornerButton}" BorderBrush="#FF3299CC" Margin="150,160,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="80" Visibility="Hidden" Click="server_Click"/>
<RadioButton Name="s11" Content="服务器12" Margin="252,160,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="80" Visibility="Hidden" Checked="server_Checked"/> <Button Name="s11" Content="服务器12" Template="{StaticResource CornerButton}" BorderBrush="#FF3299CC" Margin="252,160,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="80" Visibility="Hidden" Click="server_Click"/>
</Grid> </Grid>
</Grid> </Grid>
</Grid> </Grid>
@ -155,10 +155,16 @@
<Label Content="服务器介绍" Margin="85,2" FontSize="20" VerticalContentAlignment="Center" VerticalAlignment="Center"/> <Label Content="服务器介绍" Margin="85,2" FontSize="20" VerticalContentAlignment="Center" VerticalAlignment="Center"/>
</Grid> </Grid>
<Grid Height="200" VerticalAlignment="Bottom" Margin="10,0,10,10"> <Grid Height="200" VerticalAlignment="Bottom" Margin="10,0,10,10">
<Label Content="" Margin="50,35,50,35"/> <Label Name="serverinfo" Content="" Margin="50,35,50,35" FontSize="15"/>
</Grid> </Grid>
</Grid> </Grid>
<Grid HorizontalAlignment="Right" Width="300" ShowGridLines="True" Height="206" VerticalAlignment="Bottom"> <Grid ShowGridLines="True" Height="200" VerticalAlignment="Bottom">
<Label Content="服务器名称:" Margin="40,20,160,0" FontSize="15" HorizontalContentAlignment="Center" Height="30" VerticalAlignment="Top" />
<Label Name="selserver" Content="" Margin="145,20,40,0" FontSize="15" HorizontalContentAlignment="Center" Height="30" VerticalAlignment="Top" />
<Label Content="客户端版本:" Margin="40,50,160,0" FontSize="15" HorizontalContentAlignment="Center" Height="30" VerticalAlignment="Top" />
<Label Name="needclient" Content="" Margin="145,50,40,0" FontSize="15" HorizontalContentAlignment="Center" Height="30" VerticalAlignment="Top" />
<Label Content="在线人数:" Margin="40,80,160,0" FontSize="15" HorizontalContentAlignment="Center" Height="30" VerticalAlignment="Top" />
<Label Name="online" Content="" Margin="145,80,40,0" FontSize="15" HorizontalContentAlignment="Center" Height="30" VerticalAlignment="Top" />
<Button x:Name="StartGame" BorderBrush="#FF3299CC" Content="启动游戏" Template="{StaticResource CornerButton}" Margin="10,0,10,10" Height="64" VerticalAlignment="Bottom" FontSize="16" Click="StartGame_Click"/> <Button x:Name="StartGame" BorderBrush="#FF3299CC" Content="启动游戏" Template="{StaticResource CornerButton}" Margin="10,0,10,10" Height="64" VerticalAlignment="Bottom" FontSize="16" Click="StartGame_Click"/>
</Grid> </Grid>
</Grid> </Grid>

View File

@ -32,7 +32,7 @@ namespace CTZLauncher
{ {
LauncherCore launcher = null; LauncherCore launcher = null;
LaunchOptions option = new LaunchOptions(); LaunchOptions option = new LaunchOptions();
string serveraddress = "CityCraft.cn"; string serveraddress = "127.0.0.1";
int serverport = 25580; int serverport = 25580;
#region #region
@ -86,33 +86,43 @@ namespace CTZLauncher
} }
Login.IsEnabled = false; Login.IsEnabled = false;
CTZAuthenticator auth = new CTZAuthenticator(username.Text, password.Text, serveraddress, serverport); CTZAuthenticator auth = new CTZAuthenticator(username.Text, password.Text, serveraddress, serverport);
if (auth.isLogin()) try
{ {
MessageBox.Show("当前玩家已登录服务器!"); if (auth.isLogin())
}
else
{
if (!auth.Login())
{ {
if (!auth.isRegistered()) MessageBox.Show("当前玩家已登录服务器!");
MessageBox.Show("该用户名未注册 请先注册!");
else
MessageBox.Show("登录失败 账号不存在 或 密码错误 !");
} }
else else
{ {
ServerWindow.Visibility = System.Windows.Visibility.Visible; if (!auth.Login())
RotateTransform rtf = new RotateTransform(); {
ServerWindow.RenderTransform = rtf; if (!auth.isRegistered())
LoginWindow.RenderTransform = rtf; MessageBox.Show("该用户名未注册 请先注册!");
DoubleAnimation dbAscending = new DoubleAnimation(0, 360, new Duration(TimeSpan.FromSeconds(0.75))); else
dbAscending.RepeatBehavior = new RepeatBehavior(1); MessageBox.Show("登录失败 账号不存在 或 密码错误 !");
rtf.BeginAnimation(RotateTransform.AngleProperty, dbAscending); }
CTZServer areas = JsonMapper.ToObject<CTZServer>(auth.getServerList().ToLower()); else
LoadAreas(areas); {
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<CTZServer>(auth.getServerList().ToLower());
LoadAreas(areas);
}
} }
} }
Login.IsEnabled = true; catch (ArgumentNullException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
Login.IsEnabled = true;
}
} }
private void outline_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) private void outline_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
@ -147,25 +157,38 @@ namespace CTZLauncher
private void register_Click(object sender, RoutedEventArgs e) private void register_Click(object sender, RoutedEventArgs e)
{ {
register.IsEnabled = false;
if (username.Text.Length == 0 || password.Text.Length == 0) if (username.Text.Length == 0 || password.Text.Length == 0)
{ {
MessageBox.Show("请输入账号密码!"); MessageBox.Show("请输入账号密码!");
return; return;
} }
CTZAuthenticator auth = new CTZAuthenticator(username.Text, password.Text, "citycraft.cn", serverport); CTZAuthenticator auth = new CTZAuthenticator(username.Text, password.Text, "citycraft.cn", serverport);
if (auth.isRegistered()) try
{ {
MessageBox.Show("该用户名已注册 请更换用户名!"); if (auth.isRegistered())
return; {
MessageBox.Show("该用户名已注册 请更换用户名!");
return;
}
if (auth.Register())
{
MessageBox.Show("注册成功!");
auth.Login();
ServerWindow.Visibility = System.Windows.Visibility.Visible;
}
else
MessageBox.Show("注册失败!");
} }
if (auth.Register()) catch (ArgumentNullException ex)
{ {
MessageBox.Show("注册成功!"); MessageBox.Show(ex.Message);
auth.Login();
ServerWindow.Visibility = System.Windows.Visibility.Visible;
} }
else finally
MessageBox.Show("注册失败!"); {
register.IsEnabled = true;
}
} }
#endregion #endregion
@ -175,11 +198,16 @@ namespace CTZLauncher
for (int i = 0; i < areas.Areas.Count; i++) for (int i = 0; i < areas.Areas.Count; i++)
{ {
Area area = areas.Areas[i]; Area area = areas.Areas[i];
RadioButton arearb = GameArea.FindName("a" + i) as RadioButton; Button arearb = GameArea.FindName("a" + i) as Button;
arearb.Content = area.Name; arearb.Content = area.Name;
arearb.Visibility = Visibility.Visible; arearb.Visibility = Visibility.Visible;
arearb.Tag = area.Servers; arearb.Tag = area.Servers;
} }
for (int i = areas.Areas.Count; i < 8; i++)
{
Button arearb = GameArea.FindName("a" + i) as Button;
arearb.Visibility = Visibility.Hidden;
}
} }
private void LoadServers(List<Server> servers) private void LoadServers(List<Server> servers)
@ -187,43 +215,57 @@ namespace CTZLauncher
for (int i = 0; i < servers.Count; i++) for (int i = 0; i < servers.Count; i++)
{ {
Server server = servers[i]; Server server = servers[i];
RadioButton serverrb = GameArea.FindName("s" + i) as RadioButton; Button serverrb = GameArea.FindName("s" + i) as Button;
serverrb.Content = server.Name; serverrb.Content = server.Name;
serverrb.Visibility = Visibility.Visible; serverrb.Visibility = Visibility.Visible;
serverrb.Tag = server; serverrb.Tag = server;
} }
for (int i = servers.Count; i < 12; i++)
{
Button serverrb = GameArea.FindName("s" + i) as Button;
serverrb.Visibility = Visibility.Hidden;
}
} }
private void area_Checked(object sender, RoutedEventArgs e) private void area_Click(object sender, RoutedEventArgs e)
{ {
RadioButton rb = (RadioButton)sender; Button arb = (Button)sender;
LoadServers((List<Server>)rb.Tag); LoadServers((List<Server>)arb.Tag);
} }
private void server_Checked(object sender, RoutedEventArgs e) private void server_Click(object sender, RoutedEventArgs e)
{ {
RadioButton rb = (RadioButton)sender; Button srb = (Button)sender;
Server server = (Server)rb.Tag; Server server = (Server)srb.Tag;
option.Server = new ServerInfo option.Server = new ServerInfo
{ {
Address = server.Address, Address = server.Address,
Port = server.Port Port = server.Port
}; };
option.Version = launcher.GetVersion(server.Version); option.Version = launcher.GetVersion(server.Version);
serverinfo.Content = server.Info;
selserver.Content = server.Name;
needclient.Content = server.Version;
} }
private void StartGame_Click(object sender, RoutedEventArgs e) private void StartGame_Click(object sender, RoutedEventArgs e)
{ {
Console.WriteLine("StartGame"); Console.WriteLine("启动游戏");
option.Mode = LaunchMode.MCLauncher; option.Mode = LaunchMode.MCLauncher;
option.MaxMemory = 2048; option.MaxMemory = 2048;
option.Authenticator = new OfflineAuthenticator(username.Text); // offline option.Authenticator = new OfflineAuthenticator(username.Text); // 离线模式
option.Version = launcher.GetVersion("1.8");
launcher.JavaPath = SystemTools.FindJava().First(); launcher.JavaPath = SystemTools.FindJava().First();
launcher.GameLog += launcher_GameLog; launcher.GameLog += launcher_GameLog;
launcher.GameStart += Launcher_GameStart;
launcher.Launch(option); launcher.Launch(option);
} }
private void Launcher_GameStart(LaunchHandle arg1, string arg2)
{
string title = selserver.Content.ToString() + " - " + username.Text;
arg1.SetTitle(title);
}
void launcher_GameLog(LaunchHandle arg1, string log) void launcher_GameLog(LaunchHandle arg1, string log)
{ {
Console.WriteLine(log); Console.WriteLine(log);

View File

@ -74,6 +74,12 @@ namespace CTZLauncher.Modules.CTZServer
[JsonPropertyName("url")] [JsonPropertyName("url")]
public string Url { get; set; } public string Url { get; set; }
/// <summary>
/// 客户端依赖项
/// </summary>
[JsonPropertyName("depend")]
public string Depend { get; set; }
/// <summary> /// <summary>
/// 获得服务器链接 /// 获得服务器链接
/// </summary> /// </summary>

View File

@ -1,239 +1,240 @@
namespace KMCCC.Modules.JVersion namespace KMCCC.Modules.JVersion
{ {
#region #region
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using Launcher; using Launcher;
using LitJson; using LitJson;
using Tools; using Tools;
#endregion #endregion
/// <summary> /// <summary>
/// 默认的版本定位器 /// 默认的版本定位器
/// </summary> /// </summary>
public class JVersionLocator : IVersionLocator public class JVersionLocator : IVersionLocator
{ {
private readonly HashSet<string> _locatingVersion; private readonly HashSet<string> _locatingVersion;
private readonly Dictionary<string, Version> _versions; private readonly Dictionary<string, Version> _versions;
public JVersionLocator() public JVersionLocator()
{ {
_versions = new Dictionary<string, Version>(); _versions = new Dictionary<string, Version>();
_locatingVersion = new HashSet<string>(); _locatingVersion = new HashSet<string>();
} }
public string GameRootPath { get; set; } public string GameRootPath { get; set; }
private LauncherCore _core; private LauncherCore _core;
public LauncherCore Core public LauncherCore Core
{ {
set set
{ {
GameRootPath = value.GameRootPath; GameRootPath = value.GameRootPath;
_core = value; _core = value;
} }
} }
public Version Locate(string id) public Version Locate(string id)
{ {
lock (_locatingVersion) lock (_locatingVersion)
{ {
return GetVersionInternal(id); return GetVersionInternal(id);
} }
} }
public IEnumerable<Version> GetAllVersions() public IEnumerable<Version> GetAllVersions()
{ {
try try
{ {
lock (_locatingVersion) lock (_locatingVersion)
{ {
return new DirectoryInfo(GameRootPath + @"\versions").EnumerateDirectories() return new DirectoryInfo(GameRootPath + @"\versions").EnumerateDirectories()
.Select(dir => GetVersionInternal(dir.Name)).Where(item => item != null); .Select(dir => GetVersionInternal(dir.Name)).Where(item => item != null);
} }
} }
catch catch
{ {
return new Version[0]; return new Version[0];
} }
} }
/// <summary> /// <summary>
/// 获取Version信息当出现错误时会返回null /// 获取Version信息当出现错误时会返回null
/// </summary> /// </summary>
/// <param name="id">版本id</param> /// <param name="id">版本id</param>
/// <returns>Version的信息</returns> /// <returns>Version的信息</returns>
internal Version GetVersionInternal(string id) internal Version GetVersionInternal(string id)
{ {
try try
{ {
if (_locatingVersion.Contains(id)) if (_locatingVersion.Contains(id))
{ {
return null; return null;
} }
_locatingVersion.Add(id); _locatingVersion.Add(id);
Version version; Version version;
if (_versions.TryGetValue(id, out version)) if (_versions.TryGetValue(id, out version))
{ {
return version; return version;
} }
var jver = LoadVersion(_core.GetVersionJsonPath(id)); var jver = LoadVersion(_core.GetVersionJsonPath(id));
if (jver == null) if (jver == null)
{ {
return null; return null;
} }
version = new Version(); version = new Version();
if (string.IsNullOrWhiteSpace(jver.Id)) //if (string.IsNullOrWhiteSpace(jver.Id))
{ //{
jver.Id = id; // jver.Id = id;
} //}
if (string.IsNullOrWhiteSpace(jver.MinecraftArguments)) jver.Id = id;
{ if (string.IsNullOrWhiteSpace(jver.MinecraftArguments))
return null; {
} return null;
if (string.IsNullOrWhiteSpace(jver.MainClass)) }
{ if (string.IsNullOrWhiteSpace(jver.MainClass))
return null; {
} return null;
if (string.IsNullOrWhiteSpace(jver.Assets)) }
{ if (string.IsNullOrWhiteSpace(jver.Assets))
jver.Assets = "legacy"; {
} jver.Assets = "legacy";
if (jver.Libraries == null) }
{ if (jver.Libraries == null)
return null; {
} return null;
version.Id = jver.Id; }
version.MinecraftArguments = jver.MinecraftArguments; version.Id = jver.Id;
version.Assets = jver.Assets; version.MinecraftArguments = jver.MinecraftArguments;
version.MainClass = jver.MainClass; version.Assets = jver.Assets;
version.JarId = jver.JarId; version.MainClass = jver.MainClass;
version.Libraries = new List<Library>(); version.JarId = jver.JarId;
version.Natives = new List<Native>(); version.Libraries = new List<Library>();
foreach (var lib in jver.Libraries) version.Natives = new List<Native>();
{ foreach (var lib in jver.Libraries)
if (string.IsNullOrWhiteSpace(lib.Name)) {
{ if (string.IsNullOrWhiteSpace(lib.Name))
continue; {
} continue;
var names = lib.Name.Split(':'); }
if (names.Length != 3) var names = lib.Name.Split(':');
{ if (names.Length != 3)
continue; {
} continue;
if (lib.Natives == null) }
{ if (lib.Natives == null)
if (!IfAllowed(lib.Rules)) {
{ if (!IfAllowed(lib.Rules))
continue; {
} continue;
version.Libraries.Add(new Library }
{ version.Libraries.Add(new Library
NS = names[0], {
Name = names[1], NS = names[0],
Version = names[2] Name = names[1],
}); Version = names[2]
} });
else }
{ else
if (!IfAllowed(lib.Rules)) {
{ if (!IfAllowed(lib.Rules))
continue; {
} continue;
var native = new Native }
{ var native = new Native
NS = names[0], {
Name = names[1], NS = names[0],
Version = names[2], Name = names[1],
NativeSuffix = lib.Natives["windows"].Replace("${arch}", SystemTools.GetArch()) Version = names[2],
}; NativeSuffix = lib.Natives["windows"].Replace("${arch}", SystemTools.GetArch())
version.Natives.Add(native); };
if (lib.Extract != null) version.Natives.Add(native);
{ if (lib.Extract != null)
native.Options = new UnzipOptions {Exclude = lib.Extract.Exculde}; {
} native.Options = new UnzipOptions { Exclude = lib.Extract.Exculde };
} }
} }
if (jver.InheritsVersion != null) }
{ if (jver.InheritsVersion != null)
var target = GetVersionInternal(jver.InheritsVersion); {
if (target == null) var target = GetVersionInternal(jver.InheritsVersion);
{ if (target == null)
return null; {
} return null;
else }
{ else
version.Assets = version.Assets ?? target.Assets; {
version.JarId = version.JarId ?? target.JarId; version.Assets = version.Assets ?? target.Assets;
version.MainClass = version.MainClass ?? target.MainClass; version.JarId = version.JarId ?? target.JarId;
version.MinecraftArguments = version.MinecraftArguments ?? target.MinecraftArguments; version.MainClass = version.MainClass ?? target.MainClass;
version.Natives.AddRange(target.Natives); version.MinecraftArguments = version.MinecraftArguments ?? target.MinecraftArguments;
version.Libraries.AddRange(target.Libraries); version.Natives.AddRange(target.Natives);
} version.Libraries.AddRange(target.Libraries);
} }
version.JarId = version.JarId ?? version.Id; }
_versions.Add(version.Id, version); version.JarId = version.JarId ?? version.Id;
return version; _versions.Add(version.Id, version);
} return version;
catch }
{ catch
return null; {
} return null;
finally }
{ finally
_locatingVersion.Remove(id); {
} _locatingVersion.Remove(id);
} }
}
public JVersion LoadVersion(string jsonPath) public JVersion LoadVersion(string jsonPath)
{ {
try try
{ {
return JsonMapper.ToObject<JVersion>(File.ReadAllText(jsonPath)); return JsonMapper.ToObject<JVersion>(File.ReadAllText(jsonPath));
} }
catch catch
{ {
return null; return null;
} }
} }
/// <summary> /// <summary>
/// 判断一系列规则后是否启用 /// 判断一系列规则后是否启用
/// </summary> /// </summary>
/// <param name="rules">规则们</param> /// <param name="rules">规则们</param>
/// <returns>是否启用</returns> /// <returns>是否启用</returns>
public bool IfAllowed(List<JRule> rules) public bool IfAllowed(List<JRule> rules)
{ {
if (rules == null) if (rules == null)
{ {
return true; return true;
} }
if (rules.Count == 0) if (rules.Count == 0)
{ {
return true; return true;
} }
var allowed = false; var allowed = false;
foreach (var rule in rules) foreach (var rule in rules)
{ {
if (rule.OS == null) if (rule.OS == null)
{ {
allowed = rule.Action == "allow"; allowed = rule.Action == "allow";
continue; continue;
} }
if (rule.OS.Name == "windows") if (rule.OS.Name == "windows")
{ {
allowed = rule.Action == "allow"; allowed = rule.Action == "allow";
} }
} }
return allowed; return allowed;
} }
} }
} }