VRCMelonAssistant/ModAssistant/App.xaml.cs

212 lines
7.7 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;
2020-05-10 09:40:22 +12:00
public static bool ReinstallInstalledMods;
public static bool CloseWindowOnFinish;
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();
public static MainWindow window;
2020-05-20 11:13:55 +12:00
public static string Arguments;
2020-05-13 13:57:05 +12:00
public static bool Update = true;
public static bool GUI = true;
2019-04-22 18:41:43 +12:00
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;
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;
2020-05-10 09:40:22 +12:00
ReinstallInstalledMods = ModAssistant.Properties.Settings.Default.ReinstallInstalled;
CloseWindowOnFinish = ModAssistant.Properties.Settings.Default.CloseWindowOnFinish;
2019-04-22 18:41:43 +12:00
2020-05-13 13:57:05 +12:00
await ArgumentHandler(e.Args);
2020-05-19 20:10:44 +12:00
await Init();
2020-05-13 13:57:05 +12:00
}
2020-05-19 20:10:44 +12:00
private async Task Init()
2020-05-13 13:57:05 +12:00
{
if (Update)
2019-04-22 18:41:43 +12:00
{
2020-05-20 11:13:55 +12:00
try
{
await Task.Run(async () => await Updater.Run());
}
catch (UnauthorizedAccessException e)
{
Utils.StartAsAdmin(Arguments, true);
}
2020-05-13 13:57:05 +12:00
}
2020-05-13 13:57:05 +12:00
if (GUI)
{
window = new MainWindow();
2019-04-22 18:41:43 +12:00
window.Show();
}
2020-05-19 20:10:44 +12:00
else
{
//Application.Current.Shutdown();
2020-05-19 20:10:44 +12:00
}
2020-05-13 13:57:05 +12:00
}
private async Task ArgumentHandler(string[] args)
{
2020-05-20 11:13:55 +12:00
Arguments = string.Join(" ", args);
2020-05-13 13:57:05 +12:00
while (args.Length > 0)
2019-04-22 18:41:43 +12:00
{
2020-05-13 13:57:05 +12:00
switch (args[0])
{
case "--install":
if (args.Length < 2 || string.IsNullOrEmpty(args[1]))
{
Utils.SendNotify(string.Format((string)Current.FindResource("App:InvalidArgument"), "--install"));
}
else
{
await OneClickInstaller.InstallAsset(args[1]);
}
if (CloseWindowOnFinish)
{
await Task.Delay(5 * 1000);
Current.Shutdown();
}
2020-05-13 13:57:05 +12:00
Update = false;
GUI = false;
args = Shift(args, 2);
break;
case "--no-update":
Update = false;
args = Shift(args);
break;
case "--language":
if (args.Length < 2 || string.IsNullOrEmpty(args[1]))
{
Utils.SendNotify(string.Format((string)Current.FindResource("App:InvalidArgument"), "--language"));
}
else
{
2020-05-30 15:41:03 +12:00
Languages.LoadLanguage(args[1]);
2020-05-13 13:57:05 +12:00
}
args = Shift(args, 2);
break;
case "--register":
if (args.Length < 2 || string.IsNullOrEmpty(args[1]))
{
Utils.SendNotify(string.Format((string)Current.FindResource("App:InvalidArgument"), "--register"));
}
else
{
OneClickInstaller.Register(args[1], true);
}
Update = false;
GUI = false;
args = Shift(args, 2);
break;
case "--unregister":
if (args.Length < 2 || string.IsNullOrEmpty(args[1]))
{
Utils.SendNotify(string.Format((string)Current.FindResource("App:InvalidArgument"), "--unregister"));
}
else
{
OneClickInstaller.Unregister(args[1], true);
}
Update = false;
GUI = false;
args = Shift(args, 2);
break;
2020-05-19 20:10:44 +12:00
case "--runforever":
while (true)
{
}
2020-05-13 13:57:05 +12:00
default:
Utils.SendNotify((string)Current.FindResource("App:UnrecognizedArgument"));
args = Shift(args);
break;
}
2019-04-22 18:41:43 +12:00
}
}
2020-05-13 13:57:05 +12:00
private static string[] Shift(string[] array, int places = 1)
2019-04-22 18:41:43 +12:00
{
2020-05-13 13:57:05 +12:00
if (places >= array.Length) return Array.Empty<string>();
string[] newArray = new string[array.Length - places];
for(int i = places; i < array.Length; i++)
2019-04-30 04:34:41 +12:00
{
2020-05-13 13:57:05 +12:00
newArray[i - places] = array[i];
2019-04-30 04:34:41 +12:00
}
2020-02-03 18:55:42 +13:00
2020-05-13 13:57:05 +12:00
return newArray;
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
}
}
}