Files
2022-10-09 12:27:47 +09:00

285 lines
12 KiB
C#

using Mitria_Minecraft_Launcher.Updater;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Linq;
namespace Mitria_Minecraft_Launcher
{
public partial class LauncherForm : Form
{
private LoadingScreen _loadingScreen;
public LauncherForm()
{
InitializeComponent();
}
private void LauncherForm_Load(object sender, EventArgs e)
{
this.Text = ProductName + " v" + ProductVersion;
LoadingScreenInitialize();
this.textBox_MinecraftPlayerName.Text = Settings.UserLauncherConfig.MinecraftPlayerName;
int index = 0;
foreach (var server in Settings.ServerInformation.Servers)
{
comboBox_selectServer.Items.Add(server.ServerFullName);
if (server.ServerName == Settings.UserLauncherConfig.LastServerName)
index = comboBox_selectServer.Items.Count - 1;
}
comboBox_selectServer.SelectedIndex = index;
}
private void LoadingScreenInitialize()
{
_loadingScreen = new LoadingScreen(Properties.Resources.Loading)
{
Location = groupBox_MinecraftRun.Location,
Size = groupBox_MinecraftRun.Size
};
_loadingScreen.Visible = false;
_loadingScreen.BackColor = Color.FromArgb(220, 220, 220);
Controls.Add(_loadingScreen);
}
private void Loading(bool loading)
{
label_First.Text = "[0/0]";
label_Second.Text = "[0/0]";
label_First_Percentage.Text = "100%";
label_Second_Percentage.Text = "100%";
progressBar_First.Value = 0;
progressBar_Second.Value = 0;
if (loading)
{
_loadingScreen.Visible = true;
groupBox_MinecraftRun.Visible = false;
}
else
{
_loadingScreen.Visible = true;
groupBox_MinecraftRun.Visible = true;
}
}
private void button_Setup_Click(object sender, EventArgs e)
{
LauncherSetupForm launcherSetupForm = new LauncherSetupForm();
launcherSetupForm.ShowDialog();
}
private async void button_Run_Click(object sender, EventArgs e)
{
if(Settings.UserLauncherConfig.GameDirectory == System.Windows.Forms.Application.StartupPath)
{
MessageBox.Show("The launcher directory and the game directory cannot be the same.", "FATAL Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
textBox_ProgressLog.Text = string.Empty;
Loading(true);
string minecraftPalyerName = this.textBox_MinecraftPlayerName.Text.Trim();
if (minecraftPalyerName == string.Empty)
{
MessageBox.Show("please type Minecraft PlayerName", "FATAL Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Loading(false);
textBox_MinecraftPlayerName.Focus();
return;
}
Settings.UserLauncherConfig.MinecraftPlayerName = minecraftPalyerName;
var gameProcess = Task.Run(() => GameProcess());
bool processResult = await gameProcess;
if (processResult)
{
MinecraftRun();
}
else
{
Loading(false);
}
}
private bool GameProcess()
{
Updater.GameUpdateManager gameUpdateManager = new Updater.GameUpdateManager();
gameUpdateManager.GameUpdateManagerMessage += GameUpdateManager_GameUpdateManagerMessage;
GameUpdateStatus updateResult = gameUpdateManager.Start();
if (updateResult == GameUpdateStatus.Success)
return true;
else
{
return false;
}
}
private void GameUpdateManager_GameUpdateManagerMessage(object sender, Updater.GameUpdateManagerMessageEventArgs e)
{
switch (e.MessageType)
{
case Updater.GameUpdateManagerMessageType.First:
this.Invoke(new MethodInvoker(
delegate ()
{
textBox_ProgressLog.Text += e.Message + Environment.NewLine;
label_First.Text = "[" + e.MinValue.UnitSeparator() + "/" + e.MaxValue.UnitSeparator() + "]";
int percent = GetPercent(e.MinValue, e.MaxValue);
label_First_Percentage.Text = percent + "%";
progressBar_First.Value = percent;
label_Second.Text = "[0/0]";
label_Second_Percentage.Text = "100%";
progressBar_Second.Value = 100;
}));
break;
case Updater.GameUpdateManagerMessageType.Second:
this.Invoke(new MethodInvoker(delegate ()
{
label_Second.Text = "[" + e.MinValue.UnitSeparator() + "/" + e.MaxValue.UnitSeparator() + "]";
int percent = GetPercent(e.MinValue, e.MaxValue);
label_Second_Percentage.Text = percent + "%";
progressBar_Second.Value = percent;
}));
break;
case Updater.GameUpdateManagerMessageType.Message:
this.Invoke(new MethodInvoker(delegate ()
{
textBox_ProgressLog.Text += e.Message + Environment.NewLine;
}));
break;
}
}
private int GetPercent(long minValue, long maxValue)
{
return (int)((double)minValue / maxValue * 100);
}
private void MinecraftRun()
{
Settings.UserLauncherConfig.LastServerName = Settings.NowProfile.ServerName;
Settings.SaveUserLauncherConfig();
Settings.SaveUserClientVersion();
string runtime = System.IO.Path.GetFullPath(CommonLibrary.Extensions.PathCombineW(Settings.RuntimeLocation , Settings.NowProfile.ServerName, "\\bin\\javaw.exe"));
if (!System.IO.File.Exists(runtime))
{
CommonLibrary.Log.FATAL("not found java");
MessageBox.Show("the java could not be found.", "FATAL Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Loading(false);
Settings.NowProfile.RuntimeVersion = "0.0.0.0"; // 버전초기화
return;
}
string excuteArgumentXml = CommonLibrary.Extensions.PathCombineW(Settings.UserLauncherConfig.GameDirectory, Settings.NowProfile.ServerName, @"\Mitria\ExcuteArgument.xml");
ExcuteArgument excuteArgument = CommonLibrary.XmlSystem.LoadFromPath<ExcuteArgument>(excuteArgumentXml);
List<Parameter> launcherParameters = new List<Parameter>();
launcherParameters.Add(new Parameter("GameDirectory", "\"" + CommonLibrary.Extensions.PathCombineW(Settings.UserLauncherConfig.GameDirectory, Settings.NowProfile.ServerName) + "\""));
launcherParameters.Add(new Parameter("argument", Settings.NowProfile.Argument));
launcherParameters.Add(new Parameter("userName", Settings.UserLauncherConfig.MinecraftPlayerName));
launcherParameters.Add(new Parameter("uuid", "uuid"));
launcherParameters.Add(new Parameter("accessToken", "accessToken"));
excuteArgument.Parameters.AddRange(launcherParameters);
string argumentsCommand = Extensions.ApplyExcuteCommand(excuteArgument);
Process process = new Process();
ProcessStartInfo processStartInfo = new ProcessStartInfo
{
FileName = runtime,
CreateNoWindow = true,
Arguments = argumentsCommand,
UseShellExecute = Settings.UserLauncherConfig.ShellView,
WorkingDirectory = CommonLibrary.Extensions.PathCombineW(Settings.UserLauncherConfig.GameDirectory , Settings.NowProfile.ServerName),
};
process.StartInfo = processStartInfo;
CommonLibrary.Log.INFO("[Minecraft Process] FilName : " + processStartInfo.FileName);
CommonLibrary.Log.INFO("[Minecraft Process] CreateNoWindow : " + processStartInfo.CreateNoWindow);
CommonLibrary.Log.INFO("[Minecraft Process] Arguments : " + processStartInfo.Arguments);
CommonLibrary.Log.INFO("[Minecraft Process] UseShellExecute : " + processStartInfo.UseShellExecute);
CommonLibrary.Log.INFO("[Minecraft Process] WorkingDirectory : " + processStartInfo.WorkingDirectory);
// 크래쉬 확인을 위한 시작시간 저장
DateTime criteriaDateTime = DateTime.Now;
process.Start();
this.Hide();
process.WaitForExit();
process.Close();
this.Show();
Loading(false);
string reportPath = ReportCheck(criteriaDateTime);
if (reportPath != string.Empty)
{
string reportText = System.IO.File.ReadAllText(reportPath);
ReportView rv = new ReportView(reportText);
rv.ShowDialog();
}
}
private void button_Report_Click(object sender, EventArgs e)
{
// 개발중..? 뭐 만들려고 했지...?
return;
}
private string ReportCheck(DateTime criteriaDateTime)
{
string crashDirectory = CommonLibrary.Extensions.PathCombineW(Settings.UserLauncherConfigPath, "crash-reports");
string lastFileName = string.Empty;
if (System.IO.Directory.Exists(crashDirectory))
{
var reportList = System.IO.Directory.GetFiles(crashDirectory);
DateTime lastDateTime = criteriaDateTime;
foreach (var report in reportList)
{
FileInfo fi = new FileInfo(report);
if (lastDateTime < fi.CreationTime)
{
lastDateTime = fi.CreationTime;
lastFileName = fi.Name;
}
}
}
return lastFileName;
}
private void button_Open_LauncherDirectory_Click(object sender, EventArgs e)
{
if (!System.IO.Directory.Exists(System.Windows.Forms.Application.StartupPath))
{
System.IO.Directory.CreateDirectory(System.Windows.Forms.Application.StartupPath);
}
System.Diagnostics.Process.Start(System.Windows.Forms.Application.StartupPath);
}
private void button_Open_GameDirectory_Click(object sender, EventArgs e)
{
if (!System.IO.Directory.Exists(Settings.UserLauncherConfig.GameDirectory))
{
System.IO.Directory.CreateDirectory(Settings.UserLauncherConfig.GameDirectory);
}
System.Diagnostics.Process.Start(Settings.UserLauncherConfig.GameDirectory);
}
private void comboBox_selectServer_SelectedIndexChanged(object sender, EventArgs e)
{
Settings.ChangeProfile(Settings.ServerInformation.Servers[comboBox_selectServer.SelectedIndex].ServerName);
}
}
}