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;
|
2019-05-04 23:07:52 +12:00
|
|
|
using System.Reflection;
|
2020-02-03 00:01:48 +13:00
|
|
|
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;
|
2019-05-04 23:07:52 +12:00
|
|
|
public static bool SelectInstalledMods;
|
2020-05-10 09:40:22 +12:00
|
|
|
public static bool ReinstallInstalledMods;
|
2020-05-20 10:48:35 +12:00
|
|
|
public static bool CloseWindowOnFinish;
|
2019-05-04 23:07:52 +12:00
|
|
|
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();
|
2020-05-20 05:47:45 +12:00
|
|
|
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;
|
2020-06-22 16:05:53 +12:00
|
|
|
public static string OCIWindow;
|
2019-04-22 18:41:43 +12:00
|
|
|
|
2020-02-03 00:01:48 +13: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;
|
|
|
|
|
2019-05-26 16:17:27 +12:00
|
|
|
if (ModAssistant.Properties.Settings.Default.UpgradeRequired)
|
|
|
|
{
|
|
|
|
ModAssistant.Properties.Settings.Default.Upgrade();
|
|
|
|
ModAssistant.Properties.Settings.Default.UpgradeRequired = false;
|
|
|
|
ModAssistant.Properties.Settings.Default.Save();
|
|
|
|
}
|
|
|
|
|
2019-05-05 03:05:48 +12:00
|
|
|
Version = Version.Substring(0, Version.Length - 2);
|
2020-06-22 16:05:53 +12:00
|
|
|
OCIWindow = ModAssistant.Properties.Settings.Default.OCIWindow;
|
|
|
|
if (string.IsNullOrEmpty(OCIWindow))
|
|
|
|
{
|
|
|
|
OCIWindow = "Yes";
|
|
|
|
}
|
2020-06-22 12:52:55 +12:00
|
|
|
Pages.Options options = Pages.Options.Instance;
|
|
|
|
options.InstallDirectory =
|
|
|
|
BeatSaberInstallDirectory = Utils.GetInstallDir();
|
|
|
|
|
|
|
|
Languages.LoadLanguages();
|
2019-05-30 16:03:28 +12:00
|
|
|
|
2020-02-02 21:42:15 +13:00
|
|
|
while (string.IsNullOrEmpty(App.BeatSaberInstallDirectory))
|
2019-05-30 16:03:28 +12:00
|
|
|
{
|
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)
|
2019-05-30 16:03:28 +12:00
|
|
|
{
|
|
|
|
App.BeatSaberInstallDirectory = Utils.GetManualDir();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Environment.Exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 12:52:55 +12:00
|
|
|
options.InstallType =
|
|
|
|
BeatSaberInstallType = ModAssistant.Properties.Settings.Default.StoreType;
|
|
|
|
options.SaveSelection =
|
|
|
|
SaveModSelection = ModAssistant.Properties.Settings.Default.SaveSelected;
|
|
|
|
options.CheckInstalledMods =
|
|
|
|
CheckInstalledMods = ModAssistant.Properties.Settings.Default.CheckInstalled;
|
|
|
|
options.SelectInstalledMods =
|
|
|
|
SelectInstalledMods = ModAssistant.Properties.Settings.Default.SelectInstalled;
|
|
|
|
options.ReinstallInstalledMods =
|
|
|
|
ReinstallInstalledMods = ModAssistant.Properties.Settings.Default.ReinstallInstalled;
|
|
|
|
options.CloseWindowOnFinish =
|
|
|
|
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-06-22 16:05:53 +12:00
|
|
|
options.UpdateOCIWindow(OCIWindow);
|
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
|
|
|
}
|
2019-05-05 03:05:48 +12:00
|
|
|
|
2020-05-13 13:57:05 +12:00
|
|
|
if (GUI)
|
|
|
|
{
|
2020-05-20 05:47:45 +12:00
|
|
|
window = new MainWindow();
|
2019-04-22 18:41:43 +12:00
|
|
|
window.Show();
|
|
|
|
}
|
2020-05-19 20:10:44 +12:00
|
|
|
else
|
|
|
|
{
|
2020-05-20 05:47:45 +12:00
|
|
|
//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]);
|
|
|
|
}
|
|
|
|
|
2020-05-20 10:48:35 +12:00
|
|
|
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-06-03 10:47:59 +12:00
|
|
|
if (Languages.LoadLanguage(args[1]))
|
|
|
|
{
|
|
|
|
ModAssistant.Properties.Settings.Default.LanguageCode = args[1];
|
|
|
|
ModAssistant.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 < 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;
|
2019-05-08 04:56:54 +12:00
|
|
|
Application.Current.Shutdown();
|
2019-04-22 18:41:43 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|