EZPPClient-Installer/EZPPClient Installer/MainWindow.xaml.cs

510 lines
15 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.IO;
using MessageBox = System.Windows.MessageBox;
using System.Net;
using System.Globalization;
using ModernWpf.Controls;
using System.Windows.Forms;
using System.Media;
using System.Threading;
using Windows.System;
namespace EZPPClient_Installer
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public static MainWindow Instance { get; private set; }
[DllImport("kernel32.dll")]
private static extern bool CreateSymbolicLink(string lpSymlinkFileName, string lpTargetFileName, SymbolicLink dwFlags);
private static double VERSION = 1.6;
public MainWindow()
{
Instance = this;
InitializeComponent();
Init();
}
private async void Init()
{
UpdateInstallButton(false);
UpdateState status = await CheckForInstallerUpdates();
switch (status)
{
case UpdateState.UpdateFound:
ShowInstallerUpdateDialog();
return;
case UpdateState.Error:
return;
}
string versionString = "v" + VERSION;
InstallerWindow.Title = "EZPPClient Installer " + versionString.Replace(",", ".");
string osuPath = Util.osuInstallDir();
bool isValidFolder = await isValidOsuFolder(osuPath);
UpdateInstallButton(!string.IsNullOrEmpty(osuPath));
if (!string.IsNullOrEmpty(osuPath) && isValidFolder)
{
bool foundInstall = isEZPPClientInstallationFound(osuPath);
if (foundInstall)
{
string osuEZPPPath = osuPath + @"\EZPPClient";
Uninstall_Button.Visibility = Visibility.Visible;
UpdateInstallButton(false);
List<string> outdatedFiles = DownloadUtil.FilesToDownload(osuEZPPPath);
if (!IsEmpty(outdatedFiles))
{
InstallButton.Content = "Update";
UpdateInstallButton(true);
linkFoldersCheckbox.IsEnabled = false;
}
}
folderTextbox.Text = osuPath;
}
else
await new ContentDialog()
{
Title = "Hmmm..",
Content = "We failed to locate your osu! Installation path, please define it manually qwq",
PrimaryButtonText = "Okay qwq"
}.ShowAsync();
}
private async Task<UpdateState> CheckForInstallerUpdates()
{
#if !DEBUG
try
{
using (WebClient client = new WebClient())
{
client.Headers.Add("user-agent", "EZPPClientInstaller");
string newVersionString = client.DownloadString("https://new.ez-pp.farm/ezppclient?version");
double ver = 0;
try
{
ver = double.Parse(newVersionString, CultureInfo.InvariantCulture);
}
catch (FormatException)
{
}
if (ver > VERSION)
return UpdateState.UpdateFound;
}
}
catch (WebException e)
{
ShowErrorDialog(e.Message);
return UpdateState.Error;
}
#endif
return UpdateState.UpToDate;
}
private async void ShowErrorDialog(string error)
{
var dialog = new ContentDialog()
{
Title = "Oops...",
Content = "Failed to connect to the EZPPFarm Website.\n" + error,
PrimaryButtonText = "Exit"
};
await dialog.ShowAsync();
this.Close();
}
private async void ShowInstallerUpdateDialog()
{
UpdateInstallButton(false);
var dialog = new ContentDialog()
{
Title = "New version available.",
Content = "Please download the latest version.",
PrimaryButtonText = "Download"
};
var result = await dialog.ShowAsync();
if (result == ContentDialogResult.Primary)
{
System.Diagnostics.Process.Start("https://ez-pp.farm/download");
ShowInstallerUpdateDialog();
}
}
private void UpdateInstallButton(bool enable)
{
InstallButton.IsEnabled = enable;
linkFoldersCheckbox.IsEnabled = enable;
}
private async void Install_Click(object sender, RoutedEventArgs e)
{
string osuFolder = folderTextbox.Text;
bool isValid = await isValidOsuFolder(osuFolder);
if (!isValid)
{
SystemSounds.Asterisk.Play();
await new ContentDialog()
{
Title = "Hmmm..",
Content = "It seems like the selected folder is not a osu! installation.",
PrimaryButtonText = "Okay qwq"
}.ShowAsync();
return;
}
if (!Directory.Exists(osuFolder + "\\EZPPClient"))
{
Directory.CreateDirectory(osuFolder + "\\EZPPClient");
}
string EZPPFolder = osuFolder + @"\EZPPClient";
if (!InstallButton.Content.Equals("Update"))
{
if (linkFoldersCheckbox.IsChecked.Value)
{
if (!Directory.Exists(string.Format(@"{0}\Skins\", EZPPFolder)) && Directory.Exists(string.Format(@"{0}\Skins\", osuFolder)))
{
if (!CreateSymbolicLink(string.Format(@"{0}\Skins\", EZPPFolder), string.Format(@"{0}\Skins\", osuFolder), SymbolicLink.Directory))
{
ContentDialog dg = new ContentDialog()
{
Title = "Hmmm..",
Content = "Failed to create Symlink to Skins folder.",
PrimaryButtonText = "Okay :/"
};
await dg.ShowAsync();
}
}
else
{
ContentDialog dg = new ContentDialog()
{
Title = "Hmmm..",
Content = "Failed to locate Skins folder.",
PrimaryButtonText = "Okay :/"
};
await dg.ShowAsync();
}
if (!Directory.Exists(string.Format(@"{0}\Songs\", EZPPFolder)) && Directory.Exists(string.Format(@"{0}\Songs\", osuFolder)))
{
if (!CreateSymbolicLink(string.Format(@"{0}\Songs\", EZPPFolder), string.Format(@"{0}\Songs\", osuFolder), SymbolicLink.Directory))
{
ContentDialog dg = new ContentDialog()
{
Title = "Hmmm..",
Content = "Failed to create Symlink to Songs folder.",
PrimaryButtonText = "Okay :/"
};
await dg.ShowAsync();
}
}
else
{
ContentDialog dg = new ContentDialog()
{
Title = "Hmmm..",
Content = "Failed to locate Songs folder.",
PrimaryButtonText = "Okay :/"
};
await dg.ShowAsync();
}
}
string userName = Environment.UserName;
if(File.Exists(string.Format(@"{0}\osu!.{1}.cfg", osuFolder, userName)) && !File.Exists(string.Format(@"{0}\osu!.{1}.cfg", EZPPFolder, userName)))
{
try
{
File.Copy(string.Format(@"{0}\osu!.{1}.cfg", osuFolder, userName), string.Format(@"{0}\osu!.{1}.cfg", EZPPFolder, userName));
}
catch (Exception)
{
ContentDialog dg = new ContentDialog()
{
Title = "Hmmm..",
Content = string.Format("Failed to copy osu!.{0}.cfg", userName),
PrimaryButtonText = "Okay :/"
};
await dg.ShowAsync();
}
}
if (File.Exists(string.Format(@"{0}\scores.db", osuFolder)) && !File.Exists(string.Format(@"{0}\scores.db", EZPPFolder)))
{
try
{
File.Copy(string.Format(@"{0}\scores.db", osuFolder), string.Format(@"{0}\scores.db", EZPPFolder));
}
catch (Exception)
{
ContentDialog dg = new ContentDialog()
{
Title = "Hmmm..",
Content = "Failed to copy scores.db",
PrimaryButtonText = "Okay :/"
};
await dg.ShowAsync();
}
}
if (File.Exists(string.Format(@"{0}\collection.db", osuFolder)) && !File.Exists(string.Format(@"{0}\collection.db", EZPPFolder)))
{
try
{
File.Copy(string.Format(@"{0}\collection.db", osuFolder), string.Format(@"{0}\collection.db", EZPPFolder));
}
catch (Exception)
{
ContentDialog dg = new ContentDialog()
{
Title = "Hmmm..",
Content = "Failed to copy collection.db",
PrimaryButtonText = "Okay :/"
};
await dg.ShowAsync();
}
}
}
List<string> filesToDownload = DownloadUtil.FilesToDownload(EZPPFolder);
Visibility prevInstallBtnVis = InstallButton.Visibility;
Visibility prevUninstallBtnVis = Uninstall_Button.Visibility;
Visibility prevLinkFCVis = linkFoldersCheckbox.Visibility;
BrowseButton.IsEnabled = false;
InstallButton.Visibility = Visibility.Hidden;
Uninstall_Button.Visibility = Visibility.Hidden;
linkFoldersCheckbox.Visibility = Visibility.Hidden;
Download_Text.Visibility = Visibility.Visible;
Download_Progressbar.Visibility = Visibility.Visible;
await DownloadUtil.DownloadFiles(filesToDownload, EZPPFolder);
InstallButton.Visibility = prevInstallBtnVis;
Uninstall_Button.Visibility = prevUninstallBtnVis;
linkFoldersCheckbox.Visibility = prevLinkFCVis;
BrowseButton.IsEnabled = true;
Download_Text.Visibility = Visibility.Hidden;
Download_Progressbar.Visibility = Visibility.Hidden;
_ = new ContentDialog()
{
Title = "Success!",
Content = InstallButton.Content.Equals("Update") ? "The EZPPClient was successfully updated!" : "The EZPPClient was successfully installed!",
PrimaryButtonText = "Yaaay :3"
}.ShowAsync();
SystemSounds.Asterisk.Play();
this.Focus();
Uninstall_Button.Visibility = Visibility.Visible;
UpdateInstallButton(false);
string osuEZPPPath = folderTextbox.Text + @"\EZPPClient";
List<string> outdatedFiles = DownloadUtil.FilesToDownload(osuEZPPPath);
if (!IsEmpty(outdatedFiles))
{
InstallButton.Content = "Update";
UpdateInstallButton(true);
linkFoldersCheckbox.IsEnabled = false;
}
else
{
InstallButton.Content = "Install";
linkFoldersCheckbox.IsEnabled = false;
}
}
private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
System.Diagnostics.Process.Start("https://ez-pp.farm/");
e.Handled = true;
}
private async void Browse_Click(object sender, RoutedEventArgs e)
{
if (InstallButton.Visibility == Visibility.Hidden)
return;
using (var fbd = new FolderBrowserDialog())
{
DialogResult result = fbd.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
{
bool isValid = await isValidOsuFolder(fbd.SelectedPath);
if (!isValid)
{
SystemSounds.Asterisk.Play();
await new ContentDialog()
{
Title = "Hmmm..",
Content = "It seems like this location is not an osu! installation.",
PrimaryButtonText = "Okay qwq"
}.ShowAsync();
}
else
{
bool foundInstall = isEZPPClientInstallationFound(fbd.SelectedPath);
folderTextbox.Text = fbd.SelectedPath;
if (foundInstall)
{
Uninstall_Button.Visibility = Visibility.Visible;
UpdateInstallButton(false);
string osuEZPPPath = folderTextbox.Text + @"\EZPPClient";
List<string> outdatedFiles = DownloadUtil.FilesToDownload(osuEZPPPath);
if (!IsEmpty(outdatedFiles))
{
InstallButton.Content = "Update";
UpdateInstallButton(true);
}
else
{
InstallButton.Content = "Install";
UpdateInstallButton(true);
}
}
else
{
UpdateInstallButton(true);
}
}
}
}
}
private async void Uninstall_Button_Click(object sender, RoutedEventArgs e)
{
SystemSounds.Asterisk.Play();
ContentDialog cd = new ContentDialog()
{
Title = "Confirm uninstall",
Content = "Are you sure you want to remove the EZPPClient?",
PrimaryButtonText = "Yes",
SecondaryButtonText = "No"
};
ContentDialogResult result = await cd.ShowAsync();
if (result == ContentDialogResult.Primary)
{
string osuEZPPPath = folderTextbox.Text + @"\EZPPClient";
if (Directory.Exists(osuEZPPPath))
Directory.Delete(osuEZPPPath, true);
_ = new ContentDialog()
{
Title = "Success!",
Content = "The EZPPClient was successfully uninstalled!",
PrimaryButtonText = "Okay"
}.ShowAsync();
SystemSounds.Asterisk.Play();
this.Focus();
Uninstall_Button.Visibility = Visibility.Hidden;
UpdateInstallButton(true);
}
}
private bool isEZPPClientInstallationFound(string folder)
{
return Directory.Exists(folder + @"\EZPPClient");
}
private async Task<bool> isValidOsuFolder(string folder)
{
if (string.IsNullOrEmpty(folder) || string.IsNullOrWhiteSpace(folder))
return false;
string[] files = Directory.GetFiles(folder, "*");
string[] subDirs = Directory.GetDirectories(folder, "*");
bool hasSongsFolder = false;
bool hasSkinsFolder = false;
bool hasOsuExecutable = false;
foreach (string file in files)
{
if (file.Contains("osu!.exe"))
{
hasOsuExecutable = true;
break;
}
}
foreach (string dir in subDirs)
{
if (dir.Contains("Songs") && !hasSongsFolder)
hasSongsFolder = true;
if (dir.Contains("Skins") && !hasSkinsFolder)
hasSkinsFolder = true;
if (hasSkinsFolder && hasSongsFolder)
break;
}
return hasOsuExecutable && hasSkinsFolder && hasSongsFolder;
}
public static bool IsEmpty<T>(List<T> list)
{
if (list == null)
{
return true;
}
return !list.Any();
}
private enum SymbolicLink
{
File,
Directory
}
public static void updateProgress(string file, double progress)
{
Instance.Download_Progressbar.IsIndeterminate = (progress == 100);
Instance.Download_Progressbar.Value = progress;
Instance.Download_Text.Content = "Downloading: " + file;
}
}
}