CTZLauncher/CTZLauncher/MainWindow.xaml.cs

191 lines
5.8 KiB
C#

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;
namespace CTZLauncher
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
LauncherCore launcher = null;
public MainWindow()
{
InitializeComponent();
}
private void Window_Initialized(object sender, EventArgs e)
{
if (Directory.Exists(".minecraft"))
{
launcher = LauncherCore.Create(".minecraft");
launcher.GameLog += launcher_GameLog;
}
else
{
Directory.CreateDirectory(".minecraft");
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
foreach (var item in SystemTools.FindJava())
{
javacombo.Items.Add(item);
}
if (!javacombo.Items.IsEmpty)
javacombo.SelectedIndex = 0;
foreach (var item in launcher.GetVersions())
{
gamecombo.Items.Add(item.Id);
}
if (!gamecombo.Items.IsEmpty)
gamecombo.SelectedIndex = 0;
maxmem.Text = "2048";
}
private void Window_MouseMove(object sender, MouseEventArgs e)
{
}
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 Button_Click(object sender, RoutedEventArgs e)
{
IAuthenticator auth = new YggdrasilLogin("jtb1@163.com", "jtb325325", false);
AuthenticationInfo result = auth.Do();
MessageBox.Show(result.UUID.ToString());
}
private void Login_Click(object sender, RoutedEventArgs e)
{
HttpHelper http = new HttpHelper();
http.Send(HttpMethod.GET, "http://127.0.0.1:2000/isregistered?username=" + username.Text);
while (http.readyState != HttpReadyState.)
{
DoEvent();
}
String result = http.responseBody;
Console.WriteLine("服务器返回结果" + result);
}
/// <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;
}
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 = int.Parse(maxmem.Text);
option.Authenticator = new OfflineAuthenticator(username.Text); // offline
option.Version = launcher.GetVersion(gamecombo.Text);
launcher.JavaPath = javacombo.Text;
launcher.Launch(option);
}
void launcher_GameLog(LaunchHandle arg1, string arg2)
{
Console.WriteLine(arg2);
}
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;
}
}
}
}