VRCMelonAssistant/VRCMelonAssistant/App.xaml.cs

199 lines
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;
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 VRCMelonAssistant
2019-04-22 18:41:43 +12:00
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
public static string VRChatInstallDirectory;
public static string VRChatInstallType;
public static bool CloseWindowOnFinish;
public static string Version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
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
2020-11-17 03:02:28 +13:00
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
2020-05-03 09:08:47 +12:00
if (VRCMelonAssistant.Properties.Settings.Default.UpgradeRequired)
{
VRCMelonAssistant.Properties.Settings.Default.Upgrade();
VRCMelonAssistant.Properties.Settings.Default.UpgradeRequired = false;
VRCMelonAssistant.Properties.Settings.Default.Save();
}
2020-06-22 12:52:55 +12:00
Pages.Options options = Pages.Options.Instance;
options.InstallDirectory =
VRChatInstallDirectory = Utils.GetInstallDir();
2020-06-22 12:52:55 +12:00
Languages.LoadLanguages();
while (string.IsNullOrEmpty(VRChatInstallDirectory))
{
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)
{
VRChatInstallDirectory = Utils.GetManualDir();
}
else
{
Environment.Exit(0);
}
}
2020-06-22 12:52:55 +12:00
options.InstallType =
VRChatInstallType = VRCMelonAssistant.Properties.Settings.Default.StoreType;
2020-11-17 03:19:59 +13:00
options.CloseWindowOnFinish =
CloseWindowOnFinish = VRCMelonAssistant.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());
}
2020-06-03 10:48:30 +12:00
catch (UnauthorizedAccessException)
2020-05-20 11:13:55 +12:00
{
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"));
}
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
{
if (Languages.LoadLanguage(args[1]))
{
VRCMelonAssistant.Properties.Settings.Default.LanguageCode = args[1];
VRCMelonAssistant.Properties.Settings.Default.Save();
Languages.UpdateUI(args[1]);
}
2020-05-13 13:57:05 +12:00
}
args = Shift(args, 2);
break;
case "--register":
if (args.Length < 3 || string.IsNullOrEmpty(args[1]))
2020-05-13 13:57:05 +12:00
{
Utils.SendNotify(string.Format((string)Current.FindResource("App:InvalidArgument"), "--register"));
}
Update = false;
GUI = false;
args = Shift(args, 3);
2020-05-13 13:57:05 +12:00
break;
case "--unregister":
if (args.Length < 2 || string.IsNullOrEmpty(args[1]))
{
Utils.SendNotify(string.Format((string)Current.FindResource("App:InvalidArgument"), "--unregister"));
}
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];
2020-11-17 03:19:59 +13:00
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");
2020-11-17 02:57:05 +13:00
MessageBox.Show($"{body}: {e.Exception}", title, MessageBoxButton.OK, MessageBoxImage.Warning);
2020-02-03 18:48:08 +13:00
2019-04-22 18:41:43 +12:00
e.Handled = true;
2020-11-17 03:02:28 +13:00
Current.Shutdown();
2019-04-22 18:41:43 +12:00
}
}
}