launcher/Kiseki.Launcher.Windows/Launcher.cs

103 lines
4.4 KiB
C#

using System.Reflection;
using Microsoft.Win32;
using Syroot.Windows.IO;
namespace Kiseki.Launcher.Windows
{
public class Launcher : ILauncher
{
public readonly static string Version = Assembly.GetExecutingAssembly().GetName().Version!.ToString()[..^2];
public static void Install()
{
Directory.CreateDirectory(Directories.Base);
int response = Web.CheckHealth();
if (response != Web.RESPONSE_SUCCESS)
{
if (response != Web.RESPONSE_MAINTENANCE)
{
// The Kiseki website is either down or we can't connect to the internet.
MessageBox.Show($"Failed to connect to the {Constants.ProjectName} website. Please check your internet connection.", Constants.ProjectName, MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(0);
}
else
{
// We are in maintenance mode, so let's ask for a license.
if (!File.Exists(Directories.License))
{
AskForLicense(Directories.License);
}
// ... load the license ...
while (!Web.LoadLicense(File.ReadAllText(Directories.License)))
{
// ... and if it's invalid, keep asking for a new one.
File.Delete(Directories.License);
AskForLicense(Directories.License, false);
}
// ... and then try this again;
Install();
}
}
// okay, now download the launcher from the Kiseki website...
}
private static void AskForLicense(string licensePath, bool showDialog = true)
{
DialogResult answer = showDialog ? MessageBox.Show($"{Constants.ProjectName} is currently under maintenance and requires a license in order to access games. Would you like to look for the license file now?", Constants.ProjectName, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) : DialogResult.Yes;
if (answer == DialogResult.Yes)
{
using OpenFileDialog dialog = new()
{
Title = "Select your license file",
Filter = "License files (*.bin)|*.bin",
InitialDirectory = KnownFolders.Downloads.Path
};
if (dialog.ShowDialog() == DialogResult.OK)
{
File.Copy(dialog.FileName, licensePath, true);
}
}
}
// TODO: Implement this
public static void Register()
{
using (RegistryKey applicationKey = Registry.CurrentUser.CreateSubKey($@"Software\{Constants.ProjectName}"))
{
applicationKey.SetValue("InstallLocation", Directories.Base);
}
using RegistryKey uninstallKey = Registry.CurrentUser.CreateSubKey($@"Software\Microsoft\Windows\CurrentVersion\Uninstall\{Constants.ProjectName}");
uninstallKey.SetValue("DisplayIcon", $"{Directories.Application},0");
uninstallKey.SetValue("DisplayName", Constants.ProjectName);
uninstallKey.SetValue("DisplayVersion", Version);
if (uninstallKey.GetValue("InstallDate") is null)
uninstallKey.SetValue("InstallDate", DateTime.Now.ToString("yyyyMMdd"));
uninstallKey.SetValue("InstallLocation", Directories.Base);
uninstallKey.SetValue("NoRepair", 1);
uninstallKey.SetValue("Publisher", Constants.ProjectName);
uninstallKey.SetValue("ModifyPath", $"\"{Directories.Application}\" -menu");
uninstallKey.SetValue("QuietUninstallString", $"\"{Directories.Application}\" -uninstall -quiet");
uninstallKey.SetValue("UninstallString", $"\"{Directories.Application}\" -uninstall");
uninstallKey.SetValue("URLInfoAbout", $"https://github.com/{Constants.ProjectRepository}");
uninstallKey.SetValue("URLUpdateInfo", $"https://github.com/{Constants.ProjectRepository}/releases/latest");
}
// TODO: Implement this
public static void Unregister()
{
Registry.CurrentUser.DeleteSubKey($@"Software\{Constants.ProjectName}");
}
}
}