VRCMelonAssistant/ModAssistant/App.xaml.cs

167 lines
5.9 KiB
C#
Raw Normal View History

2020-02-03 18:48:08 +13:00
using System;
2019-04-22 18:41:43 +12:00
using System.Collections.Generic;
2020-02-02 17:03:49 +13:00
using System.Globalization;
using System.IO;
2019-04-22 18:41:43 +12:00
using System.Linq;
2020-05-03 09:08:47 +12:00
using System.Net;
using System.Reflection;
using System.Threading.Tasks;
2019-04-22 18:41:43 +12:00
using System.Windows;
namespace ModAssistant
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
public static string BeatSaberInstallDirectory;
public static string BeatSaberInstallType;
public static bool SaveModSelection;
public static bool CheckInstalledMods;
public static bool SelectInstalledMods;
public static string Version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
2019-04-22 18:41:43 +12:00
public static List<string> SavedMods = ModAssistant.Properties.Settings.Default.SavedMods.Split(',').ToList();
private async void Application_Startup(object sender, StartupEventArgs e)
2019-04-22 18:41:43 +12:00
{
2020-05-03 09:08:47 +12:00
// Set SecurityProtocol to prevent crash with TLS
System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
2020-02-02 17:03:49 +13:00
// Load localisation languages
LoadLanguage(CultureInfo.CurrentCulture.Name);
2020-02-02 17:03:49 +13:00
// Uncomment the next line to debug localisation
2020-02-02 20:46:53 +13:00
// LoadLanguage("en-DEBUG");
2020-02-02 17:03:49 +13:00
if (ModAssistant.Properties.Settings.Default.UpgradeRequired)
{
ModAssistant.Properties.Settings.Default.Upgrade();
ModAssistant.Properties.Settings.Default.UpgradeRequired = false;
ModAssistant.Properties.Settings.Default.Save();
}
Version = Version.Substring(0, Version.Length - 2);
2019-04-22 18:41:43 +12:00
BeatSaberInstallDirectory = Utils.GetInstallDir();
2020-02-02 21:42:15 +13:00
while (string.IsNullOrEmpty(App.BeatSaberInstallDirectory))
{
2020-02-03 18:48:08 +13:00
string title = (string)Current.FindResource("App:InstallDirDialog:Title");
string body = (string)Current.FindResource("App:InstallDirDialog:OkCancel");
if (System.Windows.Forms.MessageBox.Show(body, title, System.Windows.Forms.MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
{
App.BeatSaberInstallDirectory = Utils.GetManualDir();
}
else
{
Environment.Exit(0);
}
}
2019-04-22 18:41:43 +12:00
BeatSaberInstallType = ModAssistant.Properties.Settings.Default.StoreType;
SaveModSelection = ModAssistant.Properties.Settings.Default.SaveSelected;
CheckInstalledMods = ModAssistant.Properties.Settings.Default.CheckInstalled;
SelectInstalledMods = ModAssistant.Properties.Settings.Default.SelectInstalled;
2019-04-22 18:41:43 +12:00
if (e.Args.Length == 0)
{
await Task.Run(async () => await Updater.Run());
2019-04-22 18:41:43 +12:00
MainWindow window = new MainWindow();
window.Show();
}
else
{
2020-02-29 01:24:51 +13:00
await ArgumentHandler(e.Args);
2019-04-22 18:41:43 +12:00
}
}
2020-02-29 01:24:51 +13:00
private async Task ArgumentHandler(string[] args)
2019-04-22 18:41:43 +12:00
{
2020-02-03 18:55:42 +13:00
switch (args[0])
2019-04-30 04:34:41 +12:00
{
case "--install":
if (args.Length < 2 || string.IsNullOrEmpty(args[1]))
{
2020-02-03 18:48:08 +13:00
Utils.SendNotify(string.Format((string)Current.FindResource("App:InvalidArgument"), "--install"));
}
else
{
2020-02-28 21:28:02 +13:00
await OneClickInstaller.InstallAsset(args[1]);
2020-02-03 18:55:42 +13:00
}
2019-04-30 04:34:41 +12:00
break;
case "--no-update":
MainWindow window = new MainWindow();
window.Show();
2019-04-30 04:34:41 +12:00
break;
2019-05-05 04:08:54 +12:00
case "--register":
if (args.Length < 2 || string.IsNullOrEmpty(args[1]))
{
2020-02-03 18:48:08 +13:00
Utils.SendNotify(string.Format((string)Current.FindResource("App:InvalidArgument"), "--register"));
}
else
{
OneClickInstaller.Register(args[1], true);
2020-02-03 18:55:42 +13:00
}
break;
case "--unregister":
if (args.Length < 2 || string.IsNullOrEmpty(args[1]))
{
2020-02-03 18:48:08 +13:00
Utils.SendNotify(string.Format((string)Current.FindResource("App:InvalidArgument"), "--unregister"));
2020-02-03 18:55:42 +13:00
}
else
{
OneClickInstaller.Unregister(args[1], true);
2020-02-03 18:55:42 +13:00
}
2019-05-05 04:08:54 +12:00
break;
2019-04-30 04:34:41 +12:00
default:
2020-02-03 18:48:08 +13:00
Utils.SendNotify((string)Current.FindResource("App:UnrecognizedArgument"));
2019-04-30 04:34:41 +12:00
break;
}
2020-02-03 18:55:42 +13:00
2019-04-30 04:34:41 +12:00
Current.Shutdown();
2019-04-22 18:41:43 +12:00
}
private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
2020-02-03 18:48:08 +13:00
string title = (string)Current.FindResource("App:Exception");
string body = (string)Current.FindResource("App:UnhandledException");
MessageBox.Show($"{body}: {e.Exception}", "Exception", MessageBoxButton.OK, MessageBoxImage.Warning);
2019-04-22 18:41:43 +12:00
e.Handled = true;
Application.Current.Shutdown();
2019-04-22 18:41:43 +12:00
}
2020-02-02 17:03:49 +13:00
private ResourceDictionary LanguagesDict
{
get
{
return Resources.MergedDictionaries[1];
}
}
2020-02-02 17:03:49 +13:00
private void LoadLanguage(string culture)
{
try
{
LanguagesDict.Source = new Uri($"Localisation/{culture}.xaml", UriKind.Relative);
}
catch (IOException)
{
2020-02-28 15:52:20 +13:00
if (culture.Contains("-"))
{
LoadLanguage(culture.Split('-').First());
}
2020-02-02 17:03:49 +13:00
// Can't load language file
}
}
2019-04-22 18:41:43 +12:00
}
}