VRCMelonAssistant/ModAssistant/MainWindow.xaml.cs

254 lines
8.5 KiB
C#
Raw Normal View History

2020-02-02 21:11:06 +13:00
using System;
2019-04-22 18:41:43 +12:00
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
2019-04-22 18:41:43 +12:00
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using ModAssistant.Pages;
using static ModAssistant.Http;
2019-04-22 18:41:43 +12:00
namespace ModAssistant
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public static MainWindow Instance;
2019-05-19 03:32:14 +12:00
public static bool ModsOpened = false;
public static string GameVersion;
public TaskCompletionSource<bool> VersionLoadStatus = new TaskCompletionSource<bool>();
2019-04-22 18:41:43 +12:00
public string MainText
{
get
{
return MainTextBlock.Text;
}
set
{
Dispatcher.Invoke(new Action(() => { MainWindow.Instance.MainTextBlock.Text = value; }));
}
}
public MainWindow()
{
InitializeComponent();
Instance = this;
VersionText.Text = App.Version;
2019-04-30 04:34:41 +12:00
2020-02-02 21:11:06 +13:00
if (Utils.IsVoid())
{
Main.Content = Invalid.Instance;
MainWindow.Instance.ModsButton.IsEnabled = false;
MainWindow.Instance.OptionsButton.IsEnabled = false;
MainWindow.Instance.IntroButton.IsEnabled = false;
MainWindow.Instance.AboutButton.IsEnabled = false;
MainWindow.Instance.GameVersionsBox.IsEnabled = false;
return;
}
2020-02-03 19:53:12 +13:00
Themes.LoadThemes();
2020-02-17 13:39:52 +13:00
Themes.FirstLoad(Properties.Settings.Default.SelectedTheme);
Task.Run(() => LoadVersionsAsync());
2019-06-28 02:11:40 +12:00
2020-02-02 21:42:15 +13:00
if (!Properties.Settings.Default.Agreed || string.IsNullOrEmpty(Properties.Settings.Default.LastTab))
2019-06-28 02:11:40 +12:00
{
Main.Content = Intro.Instance;
}
else
{
switch (Properties.Settings.Default.LastTab)
{
case "Intro":
Main.Content = Intro.Instance;
break;
case "Mods":
_ = ShowModsPage();
2019-06-28 02:11:40 +12:00
break;
case "About":
Main.Content = About.Instance;
break;
case "Options":
Main.Content = Options.Instance;
Themes.LoadThemes();
2019-06-28 02:11:40 +12:00
break;
default:
Main.Content = Intro.Instance;
break;
}
}
2019-04-22 18:41:43 +12:00
}
private async void LoadVersionsAsync()
{
try
{
var resp = await HttpClient.GetAsync(Utils.Constants.BeatModsAPIUrl + "version");
var body = await resp.Content.ReadAsStringAsync();
List<string> versions = JsonSerializer.Deserialize<string[]>(body).ToList();
Dispatcher.Invoke(() =>
{
GameVersion = GetGameVersion(versions);
GameVersionsBox.ItemsSource = versions;
GameVersionsBox.SelectedValue = GameVersion;
if (!string.IsNullOrEmpty(GameVersion) && Properties.Settings.Default.Agreed)
{
MainWindow.Instance.ModsButton.IsEnabled = true;
}
});
VersionLoadStatus.SetResult(true);
}
catch (Exception e)
{
Dispatcher.Invoke(() =>
{
GameVersionsBox.IsEnabled = false;
2020-02-18 19:32:01 +13:00
MessageBox.Show($"{Application.Current.FindResource("MainWindow:GameVersionLoadFailed")}\n{e}");
});
VersionLoadStatus.SetResult(false);
}
}
private string GetGameVersion(List<string> versions)
{
2019-12-19 16:27:11 +13:00
string version = Utils.GetVersion();
2020-02-02 21:42:15 +13:00
if (!string.IsNullOrEmpty(version) && versions.Contains(version))
{
2019-12-19 16:27:11 +13:00
return version;
}
2019-12-19 16:27:11 +13:00
string versionsString = String.Join(",", versions.ToArray());
if (Properties.Settings.Default.AllGameVersions != versionsString)
{
Properties.Settings.Default.AllGameVersions = versionsString;
Properties.Settings.Default.Save();
2020-02-18 19:25:50 +13:00
string title = (string)Application.Current.FindResource("MainWindow:GameUpdateDialog:Title");
string line1 = (string)Application.Current.FindResource("MainWindow:GameUpdateDialog:Line1");
string line2 = (string)Application.Current.FindResource("MainWindow:GameUpdateDialog:Line2");
Utils.ShowMessageBoxAsync($"{line1}\n\n{line2}", title);
return versions[0];
}
2020-02-02 21:42:15 +13:00
if (!string.IsNullOrEmpty(Properties.Settings.Default.GameVersion) && versions.Contains(Properties.Settings.Default.GameVersion))
return Properties.Settings.Default.GameVersion;
return versions[0];
}
private async Task ShowModsPage()
2019-04-22 18:41:43 +12:00
{
void OpenModsPage()
2019-05-19 03:32:14 +12:00
{
Main.Content = Mods.Instance;
Properties.Settings.Default.LastTab = "Mods";
Properties.Settings.Default.Save();
2019-05-19 03:32:14 +12:00
}
if (ModsOpened == true && Mods.Instance.PendingChanges == false)
2019-05-19 03:32:14 +12:00
{
OpenModsPage();
return;
2019-05-19 03:32:14 +12:00
}
Main.Content = Loading.Instance;
await Mods.Instance.LoadMods();
if (ModsOpened == false) ModsOpened = true;
if (Mods.Instance.PendingChanges == true) Mods.Instance.PendingChanges = false;
OpenModsPage();
}
private void ModsButton_Click(object sender, RoutedEventArgs e)
{
_ = ShowModsPage();
2019-04-22 18:41:43 +12:00
}
private void IntroButton_Click(object sender, RoutedEventArgs e)
{
Main.Content = Intro.Instance;
2019-06-28 02:11:40 +12:00
Properties.Settings.Default.LastTab = "Intro";
Properties.Settings.Default.Save();
2019-04-22 18:41:43 +12:00
}
private void AboutButton_Click(object sender, RoutedEventArgs e)
{
Main.Content = About.Instance;
2019-06-28 02:11:40 +12:00
Properties.Settings.Default.LastTab = "About";
Properties.Settings.Default.Save();
2019-04-22 18:41:43 +12:00
}
private void OptionsButton_Click(object sender, RoutedEventArgs e)
{
Main.Content = Options.Instance;
Themes.LoadThemes();
2019-06-28 02:11:40 +12:00
Properties.Settings.Default.LastTab = "Options";
Properties.Settings.Default.Save();
2019-04-22 18:41:43 +12:00
}
private void InstallButton_Click(object sender, RoutedEventArgs e)
{
Mods.Instance.InstallMods();
}
private void InfoButton_Click(object sender, RoutedEventArgs e)
{
if ((Mods.ModListItem)Mods.Instance.ModsListView.SelectedItem == null)
{
2020-02-18 19:32:01 +13:00
MessageBox.Show((string)Application.Current.FindResource("MainWindow:NoModSelected"));
return;
}
2019-04-22 18:41:43 +12:00
Mods.ModListItem mod = ((Mods.ModListItem)Mods.Instance.ModsListView.SelectedItem);
string infoUrl = mod.ModInfo.link;
2020-02-02 21:42:15 +13:00
if (string.IsNullOrEmpty(infoUrl))
2019-04-22 18:41:43 +12:00
{
2020-02-18 19:32:01 +13:00
MessageBox.Show(string.Format((string)Application.Current.FindResource("MainWindow:NoModInfoPage"), mod.ModName));
2019-04-22 18:41:43 +12:00
}
else
{
System.Diagnostics.Process.Start(infoUrl);
}
}
2019-05-19 03:32:14 +12:00
private async void GameVersionsBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
2019-05-19 03:32:14 +12:00
{
string oldGameVersion = GameVersion;
GameVersion = (sender as ComboBox).SelectedItem.ToString();
2020-02-02 21:42:15 +13:00
if (string.IsNullOrEmpty(oldGameVersion)) return;
2019-05-19 03:32:14 +12:00
Properties.Settings.Default.GameVersion = GameVersion;
Properties.Settings.Default.Save();
if (ModsOpened)
{
var prevPage = Main.Content;
Mods.Instance.PendingChanges = true;
await ShowModsPage();
Main.Content = prevPage;
2019-05-19 03:32:14 +12:00
}
}
2019-12-21 00:06:21 +13:00
private void Window_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
About.Instance.PatUp.IsOpen = false;
About.Instance.PatButton.IsEnabled = true;
2020-01-27 20:50:15 +13:00
About.Instance.HugUp.IsOpen = false;
About.Instance.HugButton.IsEnabled = true;
2019-12-21 00:06:21 +13:00
}
2019-04-22 18:41:43 +12:00
}
}