VRCMelonAssistant/ModAssistant/Pages/Options.xaml.cs

418 lines
17 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2020-05-20 12:38:08 +12:00
using System.Configuration;
using System.Diagnostics;
2020-02-05 17:58:35 +13:00
using System.IO;
2020-05-20 12:38:08 +12:00
using System.Net;
2020-02-05 17:58:35 +13:00
using System.Net.Http;
2019-04-22 18:41:43 +12:00
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Path = System.IO.Path;
2019-04-22 18:41:43 +12:00
namespace ModAssistant.Pages
{
/// <summary>
/// Interaction logic for Options.xaml
/// </summary>
public partial class Options : Page
{
public static Options Instance = new Options();
public string InstallDirectory { get; set; }
public string InstallType { get; set; }
public bool SaveSelection { get; set; }
public bool CheckInstalledMods { get; set; }
public bool SelectInstalledMods { get; set; }
2020-05-10 09:40:45 +12:00
public bool ReinstallInstalledMods { get; set; }
public bool ModelSaberProtocolHandlerEnabled { get; set; }
public bool BeatSaverProtocolHandlerEnabled { get; set; }
public bool PlaylistsProtocolHandlerEnabled { get; set; }
public bool CloseWindowOnFinish { get; set; }
2019-12-20 18:25:34 +13:00
public string LogURL { get; private set; }
2020-06-22 16:05:53 +12:00
public string OCIWindow { get; set; }
2019-04-22 18:41:43 +12:00
public Options()
{
InitializeComponent();
2020-06-22 12:52:55 +12:00
2020-06-22 16:05:53 +12:00
OCIWindow = App.OCIWindow;
if (!string.IsNullOrEmpty(OCIWindow))
{
UpdateOCIWindow(OCIWindow);
}
if (!CheckInstalledMods)
2020-05-10 09:40:45 +12:00
{
SelectInstalled.IsEnabled = false;
2020-05-10 09:40:45 +12:00
ReinstallInstalled.IsEnabled = false;
}
2020-11-17 03:19:59 +13:00
UpdateHandlerStatus();
2019-04-22 18:41:43 +12:00
this.DataContext = this;
}
public void UpdateHandlerStatus()
{
ModelSaberProtocolHandlerEnabled = OneClickInstaller.IsRegistered("modelsaber");
BeatSaverProtocolHandlerEnabled = OneClickInstaller.IsRegistered("beatsaver");
PlaylistsProtocolHandlerEnabled = OneClickInstaller.IsRegistered("bsplaylist");
}
private void SelectDirButton_Click(object sender, RoutedEventArgs e)
2019-04-22 18:41:43 +12:00
{
Utils.GetManualDir();
2019-05-25 08:32:34 +12:00
DirectoryTextBlock.Text = InstallDirectory;
GameTypeTextBlock.Text = InstallType;
2019-04-22 18:41:43 +12:00
}
2019-12-19 17:13:44 +13:00
private void OpenDirButton_Click(object sender, RoutedEventArgs e)
{
2020-02-28 03:43:03 +13:00
Utils.OpenFolder(InstallDirectory);
2019-12-19 17:13:44 +13:00
}
2019-04-22 18:41:43 +12:00
private void Test_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(Utils.GetSteamDir());
}
private void SaveSelected_Checked(object sender, RoutedEventArgs e)
{
Properties.Settings.Default.SaveSelected = true;
App.SaveModSelection = true;
Properties.Settings.Default.Save();
}
private void SaveSelected_Unchecked(object sender, RoutedEventArgs e)
{
Properties.Settings.Default.SaveSelected = false;
App.SaveModSelection = false;
Properties.Settings.Default.Save();
}
private void CheckInstalled_Checked(object sender, RoutedEventArgs e)
{
Properties.Settings.Default.CheckInstalled = true;
App.CheckInstalledMods = true;
CheckInstalledMods = true;
Properties.Settings.Default.Save();
SelectInstalled.IsEnabled = true;
2020-05-10 09:40:45 +12:00
ReinstallInstalled.IsEnabled = true;
2020-02-03 15:41:09 +13:00
2019-05-19 03:32:14 +12:00
if (MainWindow.ModsOpened)
2020-02-03 15:41:09 +13:00
{
2019-05-19 03:32:14 +12:00
Mods.Instance.PendingChanges = true;
2020-02-03 15:41:09 +13:00
}
2019-04-22 18:41:43 +12:00
}
private void CheckInstalled_Unchecked(object sender, RoutedEventArgs e)
{
Properties.Settings.Default.CheckInstalled = false;
App.CheckInstalledMods = false;
CheckInstalledMods = false;
Properties.Settings.Default.Save();
SelectInstalled.IsEnabled = false;
2020-05-10 09:40:45 +12:00
ReinstallInstalled.IsEnabled = false;
2020-02-03 15:41:09 +13:00
2019-05-19 03:32:14 +12:00
if (MainWindow.ModsOpened)
2020-02-03 15:41:09 +13:00
{
2019-05-19 03:32:14 +12:00
Mods.Instance.PendingChanges = true;
2020-02-03 15:41:09 +13:00
}
2019-04-22 18:41:43 +12:00
}
private void CloseWindowOnFinish_Checked(object sender, RoutedEventArgs e)
{
Properties.Settings.Default.CloseWindowOnFinish = true;
App.CloseWindowOnFinish = true;
CloseWindowOnFinish = true;
Properties.Settings.Default.Save();
}
private void CloseWindowOnFinish_Unchecked(object sender, RoutedEventArgs e)
{
Properties.Settings.Default.CloseWindowOnFinish = false;
App.CloseWindowOnFinish = false;
CloseWindowOnFinish = false;
Properties.Settings.Default.Save();
}
public void ModelSaberProtocolHandler_Checked(object sender, RoutedEventArgs e)
2019-04-22 18:41:43 +12:00
{
OneClickInstaller.Register("modelsaber", Description: "URL:ModelSaber OneClick Install");
}
2019-04-22 18:41:43 +12:00
public void ModelSaberProtocolHandler_Unchecked(object sender, RoutedEventArgs e)
{
OneClickInstaller.Unregister("modelsaber");
2019-04-22 18:41:43 +12:00
}
public void BeatSaverProtocolHandler_Checked(object sender, RoutedEventArgs e)
2019-04-22 18:41:43 +12:00
{
OneClickInstaller.Register("beatsaver", Description: "URL:BeatSaver OneClick Install");
}
2019-04-22 18:41:43 +12:00
public void BeatSaverProtocolHandler_Unchecked(object sender, RoutedEventArgs e)
{
OneClickInstaller.Unregister("beatsaver");
}
public void PlaylistsProtocolHandler_Checked(object sender, RoutedEventArgs e)
{
OneClickInstaller.Register("bsplaylist", Description: "URL:BeatSaver Playlist OneClick Install");
}
public void PlaylistsProtocolHandler_Unchecked(object sender, RoutedEventArgs e)
{
OneClickInstaller.Unregister("bsplaylist");
}
private void SelectInstalled_Checked(object sender, RoutedEventArgs e)
{
Properties.Settings.Default.SelectInstalled = true;
App.SelectInstalledMods = true;
SelectInstalledMods = true;
Properties.Settings.Default.Save();
}
private void SelectInstalled_Unchecked(object sender, RoutedEventArgs e)
{
Properties.Settings.Default.SelectInstalled = false;
App.SelectInstalledMods = false;
SelectInstalledMods = false;
Properties.Settings.Default.Save();
}
2020-05-10 09:40:45 +12:00
private void ReinstallInstalled_Checked(object sender, RoutedEventArgs e)
{
Properties.Settings.Default.ReinstallInstalled = true;
App.ReinstallInstalledMods = true;
ReinstallInstalledMods = true;
Properties.Settings.Default.Save();
}
private void ReinstallInstalled_Unchecked(object sender, RoutedEventArgs e)
{
Properties.Settings.Default.ReinstallInstalled = false;
App.ReinstallInstalledMods = false;
ReinstallInstalledMods = false;
Properties.Settings.Default.Save();
}
2019-12-20 18:25:34 +13:00
private async void OpenLogsDirButton_Click(object sender, RoutedEventArgs e)
{
try
{
2020-02-02 20:46:00 +13:00
MainWindow.Instance.MainText = $"{Application.Current.FindResource("Options:UploadingLog")}...";
await Task.Run(async () => await UploadLog());
2020-11-17 03:02:28 +13:00
Process.Start(LogURL);
Utils.SetClipboard(LogURL);
2020-02-02 20:46:00 +13:00
MainWindow.Instance.MainText = (string)Application.Current.FindResource("Options:LogUrlCopied");
2019-12-20 18:25:34 +13:00
}
catch (Exception exception)
{
2020-02-02 20:46:00 +13:00
MainWindow.Instance.MainText = $"{Application.Current.FindResource("Options:LogUploadFailed")}.";
string title = (string)Application.Current.FindResource("Options:LogUploadFailed:Title");
string body = (string)Application.Current.FindResource("Options:LogUploadFailed:Body");
MessageBox.Show($"{body}\n ================= \n" + exception, title);
2020-02-28 03:43:03 +13:00
Utils.OpenFolder(Path.Combine(InstallDirectory, "Logs"));
2019-12-20 18:25:34 +13:00
}
}
private async Task UploadLog()
2019-12-20 18:25:34 +13:00
{
const string DateFormat = "yyyy-mm-dd HH:mm:ss";
DateTime now = DateTime.Now;
2020-05-20 12:38:08 +12:00
string logPath = Path.GetDirectoryName(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath);
string Log = Path.Combine(logPath, "log.log");
string GameLog = File.ReadAllText(Path.Combine(InstallDirectory, "Logs", "_latest.log"));
string Separator = File.Exists(Log) ? $"\n\n=============================================\n============= Mod Assistant Log =============\n=============================================\n\n" : string.Empty;
string ModAssistantLog = File.Exists(Log) ? File.ReadAllText(Log) : string.Empty;
var nvc = new List<KeyValuePair<string, string>>()
2019-12-20 18:25:34 +13:00
{
new KeyValuePair<string, string>("title", $"_latest.log ({now.ToString(DateFormat)})"),
new KeyValuePair<string, string>("expireUnit", "hour"),
new KeyValuePair<string, string>("expireLength", "5"),
2020-05-20 12:38:08 +12:00
new KeyValuePair<string, string>("code", $"{GameLog}{Separator}{ModAssistantLog}"),
};
2020-05-20 12:38:08 +12:00
string[] items = new string[nvc.Count];
for (int i = 0; i < nvc.Count; i++)
{
2020-05-20 12:38:08 +12:00
KeyValuePair<string, string> item = nvc[i];
items[i] = WebUtility.UrlEncode(item.Key) + "=" + WebUtility.UrlEncode(item.Value);
}
2020-11-17 03:06:24 +13:00
StringContent content = new StringContent(string.Join("&", items), null, "application/x-www-form-urlencoded");
2020-05-20 12:38:08 +12:00
HttpResponseMessage resp = await Http.HttpClient.PostAsync(Utils.Constants.TeknikAPIUrl + "Paste", content);
string body = await resp.Content.ReadAsStringAsync();
2020-05-20 12:38:08 +12:00
Utils.TeknikPasteResponse TeknikResponse = Http.JsonSerializer.Deserialize<Utils.TeknikPasteResponse>(body);
2019-12-20 18:25:34 +13:00
LogURL = TeknikResponse.result.url;
}
2020-02-20 17:53:14 +13:00
private void OpenAppDataButton_Click(object sender, RoutedEventArgs e)
{
string location = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
"AppData", "LocalLow", "Hyperbolic Magnetism");
2020-05-05 04:11:54 +12:00
if (Directory.Exists(location))
{
Utils.OpenFolder(location);
}
else
{
MessageBox.Show((string)Application.Current.FindResource("Options:AppDataNotFound"));
}
2020-02-20 17:53:14 +13:00
}
private async void YeetBSIPAButton_Click(object sender, RoutedEventArgs e)
{
if (Mods.Instance.AllModsList == null)
{
2020-02-02 20:46:00 +13:00
MainWindow.Instance.MainText = $"{Application.Current.FindResource("Options:GettingModList")}...";
await Task.Run(async () => await Mods.Instance.GetAllMods());
2020-02-02 20:46:00 +13:00
MainWindow.Instance.MainText = $"{Application.Current.FindResource("Options:FindingBSIPAVersion")}...";
await Task.Run(() => Mods.Instance.GetBSIPAVersion());
}
2020-02-03 00:04:30 +13:00
foreach (Mod mod in Mods.InstalledMods)
{
if (mod.name.ToLower() == "bsipa")
{
Mods.Instance.UninstallMod(mod);
break;
}
}
2020-02-02 20:46:00 +13:00
MainWindow.Instance.MainText = $"{Application.Current.FindResource("Options:BSIPAUninstalled")}...";
}
private async void YeetModsButton_Click(object sender, RoutedEventArgs e)
{
2020-02-02 20:46:00 +13:00
string title = (string)Application.Current.FindResource("Options:YeetModsBox:Title");
string line1 = (string)Application.Current.FindResource("Options:YeetModsBox:RemoveAllMods");
string line2 = (string)Application.Current.FindResource("Options:YeetModsBox:CannotBeUndone");
var resp = System.Windows.Forms.MessageBox.Show($"{line1}\n{line2}", title, System.Windows.Forms.MessageBoxButtons.YesNo);
if (resp == System.Windows.Forms.DialogResult.Yes)
{
if (Mods.Instance.AllModsList == null)
{
2020-03-28 05:57:30 +13:00
MainWindow.Instance.MainText = $"{Application.Current.FindResource("Options:GettingModList")}...";
await Task.Run(async () => await Mods.Instance.CheckInstalledMods());
}
foreach (Mod mod in Mods.InstalledMods)
{
Mods.Instance.UninstallMod(mod);
}
if (Directory.Exists(Path.Combine(App.BeatSaberInstallDirectory, "Plugins")))
Directory.Delete(Path.Combine(App.BeatSaberInstallDirectory, "Plugins"), true);
if (Directory.Exists(Path.Combine(App.BeatSaberInstallDirectory, "Libs")))
Directory.Delete(Path.Combine(App.BeatSaberInstallDirectory, "Libs"), true);
if (Directory.Exists(Path.Combine(App.BeatSaberInstallDirectory, "IPA")))
Directory.Delete(Path.Combine(App.BeatSaberInstallDirectory, "IPA"), true);
2020-02-02 20:46:00 +13:00
MainWindow.Instance.MainText = $"{Application.Current.FindResource("Options:AllModsUninstalled")}...";
}
}
2020-02-03 19:53:12 +13:00
private void ApplicationThemeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if ((sender as ComboBox).SelectedItem == null)
{
2020-02-17 13:39:52 +13:00
Themes.ApplyWindowsTheme();
2020-02-17 15:16:22 +13:00
MainWindow.Instance.MainText = (string)Application.Current.FindResource("Options:CurrentThemeRemoved");
2020-02-17 13:39:52 +13:00
}
else
{
Themes.ApplyTheme((sender as ComboBox).SelectedItem.ToString());
}
2020-02-03 19:53:12 +13:00
}
2020-05-30 13:17:14 +12:00
public void LanguageSelectComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if ((sender as ComboBox).SelectedItem == null)
{
// Apply default language
Console.WriteLine("Applying default language");
2020-05-30 15:41:03 +12:00
Languages.LoadLanguage("en");
2020-05-30 13:17:14 +12:00
}
else
{
2020-05-30 15:41:03 +12:00
// Get the matching language from the LoadedLanguages array, then try and use it
var languageName = (sender as ComboBox).SelectedItem.ToString();
2020-05-30 15:45:03 +12:00
var selectedLanguage = Languages.LoadedLanguages.Find(language => language.NativeName.CompareTo(languageName) == 0);
if (Languages.LoadLanguage(selectedLanguage.Name))
{
Properties.Settings.Default.LanguageCode = selectedLanguage.Name;
Properties.Settings.Default.Save();
if (Languages.FirstRun)
{
Languages.FirstRun = false;
}
else
{
Process.Start(Utils.ExePath, App.Arguments);
Application.Current.Dispatcher.Invoke(() => { Application.Current.Shutdown(); });
}
}
2020-05-30 13:17:14 +12:00
}
}
private void ApplicationThemeExportTemplate_Click(object sender, RoutedEventArgs e)
{
2020-02-17 13:52:10 +13:00
Themes.WriteThemeToDisk("Ugly Kulu-Ya-Ku");
Themes.LoadThemes();
}
2020-02-08 12:49:03 +13:00
private void ApplicationThemeOpenThemesFolder_Click(object sender, RoutedEventArgs e)
{
2020-02-08 12:55:01 +13:00
if (Directory.Exists(Themes.ThemeDirectory))
{
2020-02-28 03:43:03 +13:00
Utils.OpenFolder(Themes.ThemeDirectory);
2020-02-17 13:39:52 +13:00
}
else
{
2020-02-17 15:16:22 +13:00
MessageBox.Show((string)Application.Current.FindResource("Options:ThemeFolderNotFound"));
2020-02-17 13:39:52 +13:00
}
2020-02-08 12:49:03 +13:00
}
2020-05-18 22:56:16 +12:00
private void InstallPlaylistButton_Click(object sender, RoutedEventArgs e)
{
string playlistFile = Utils.GetManualFile();
2020-05-19 03:05:42 +12:00
if (File.Exists(playlistFile))
{
Task.Run(() => { API.Playlists.DownloadFrom(playlistFile).Wait(); });
2020-05-19 03:05:42 +12:00
}
2020-05-18 22:56:16 +12:00
}
2020-06-22 16:05:53 +12:00
private void ShowOCIWindowComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox comboBox = sender as ComboBox;
if (comboBox.SelectedItem != null)
2020-11-17 03:19:59 +13:00
{
2020-06-22 16:05:53 +12:00
ComboBoxItem comboBoxItem = (ComboBoxItem)comboBox.SelectedItem;
UpdateOCIWindow(comboBoxItem.Tag.ToString());
}
}
public void UpdateOCIWindow(string state)
{
ComboBox comboBox = ShowOCIWindowComboBox;
if (comboBox != null)
{
if (state == "Yes") comboBox.SelectedIndex = 0;
else if (state == "Close") comboBox.SelectedIndex = 1;
else if (state == "No") comboBox.SelectedIndex = 2;
else return;
}
if (!string.IsNullOrEmpty(state))
{
OCIWindow = App.OCIWindow = Properties.Settings.Default.OCIWindow = state;
Properties.Settings.Default.Save();
}
}
2019-04-22 18:41:43 +12:00
}
}