Files
Mitria_Minecraft_Project/Mitria_Minecraft_Launcher/Program.cs
2022-10-09 12:27:47 +09:00

146 lines
7.3 KiB
C#

using System;
using System.IO;
using System.Windows.Forms;
namespace Mitria_Minecraft_Launcher
{
internal static class Program
{
/// <summary>
/// 해당 애플리케이션의 주 진입점입니다.
/// </summary>
[STAThread]
private static void Main()
{
string appGuid = Application.ProductName;
using (System.Threading.Mutex mutex = new System.Threading.Mutex(false, "Global\\" + appGuid))
{
if (!mutex.WaitOne(0, false))
{
MessageBox.Show(Application.ProductName + " has been duplicated.\r\nThe program is already running.", "Duplicate execution error.", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
CommonLibrary.Log.logLevel = CommonLibrary.LogLevel.Trace;
#region delete temp Directory
string tempDirectory = Path.GetFullPath("temp");
CommonLibrary.Log.INFO("delete temp Directory Process");
if (System.IO.Directory.Exists(tempDirectory))
{
System.IO.Directory.Delete(Path.GetFullPath("temp"), true);
}
#endregion delete temp Directory
#region Crate CustomDirectory
if (!System.IO.Directory.Exists(Settings.CustomDataDirectory))
{
System.IO.Directory.CreateDirectory(Settings.CustomDataDirectory);
}
#endregion Crate CustomDirectory
#region LauncherUpdate
Updater.LauncherUpdate launcherUpdate = new Updater.LauncherUpdate();
Updater.LauncherUpdateStatus status = launcherUpdate.Start();
switch (status)
{
case Updater.LauncherUpdateStatus.Update:
MessageBox.Show("Launcher has been update\r\n Press ok to Restart", "Update Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
Application.Restart();
break;
case Updater.LauncherUpdateStatus.Same:
Settings.LoadUserLauncherConfig();
bool result = DownloadServerList();
if (result)
{
if (Settings.UserLauncherConfig.Profiles != null)
{
var newList = Settings.UserLauncherConfig.Profiles;
for (int i = 0; i < Settings.UserLauncherConfig.Profiles.Count; i++)
{
bool existence = false;
for (int j = 0; j < Settings.ServerInformation.Servers.Count; j++)
{
if (Settings.UserLauncherConfig.Profiles[i].ServerName == Settings.ServerInformation.Servers[j].ServerName)
{
existence = true;
if (Settings.UserLauncherConfig.Profiles[i].OriginalArgument != Settings.ServerInformation.Servers[j].BaseArgument)
{
Settings.UserLauncherConfig.Profiles[i] = new LauncherConfig.Profile()
{
ServerName = Settings.UserLauncherConfig.Profiles[i].ServerName,
Argument = Settings.ServerInformation.Servers[j].BaseArgument,
OriginalArgument = Settings.ServerInformation.Servers[j].BaseArgument,
RuntimeVersion = Settings.UserLauncherConfig.Profiles[i].RuntimeVersion,
CustomData = Settings.UserLauncherConfig.Profiles[i].CustomData
};
}
}
}
if (!existence)
{
newList.Remove(Settings.UserLauncherConfig.Profiles[i]);
string path = CommonLibrary.Extensions.PathCombineW(Settings.UserLauncherConfig.GameDirectory, Settings.UserLauncherConfig.Profiles[i].ServerName);
if (File.Exists(path))
{
Directory.Delete(path, true);
}
path = CommonLibrary.Extensions.PathCombineW(Settings.CustomDataDirectory, Settings.UserLauncherConfig.Profiles[i].ServerName);
if (System.IO.Directory.Exists(path))
{
System.IO.Directory.Delete(path, true);
}
}
}
Settings.UserLauncherConfig.Profiles = newList;
}
ProgramRun();
}
else
{
CommonLibrary.Log.FATAL("ServerList Get Failed");
MessageBox.Show("ServerList get failed. \r\nPlease provide a log to the administrator and solve it.", "FATAL Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
break;
default:
CommonLibrary.Log.FATAL("Launcher Update Failed");
MessageBox.Show("Launcher update failed. \r\nPlease provide a log to the administrator and solve it.", "FATAL Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
}
#endregion LauncherUpdate
}
}
}
private static bool DownloadServerList()
{
Downloader downloader = new Downloader();
string data = downloader.DownloadString(Settings.ServerBaseUrl + Settings.ServerInformationFile);
if (data == string.Empty)
{
return false;
}
Settings.ServerInformation = CommonLibrary.XmlSystem.LoadFromData<CommonLibrary.ServerInformation>(data);
return true;
}
private static void ProgramRun()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new LauncherForm());
}
}
}