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

217 lines
5.4 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;
namespace EZPPClient_Installer
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
[DllImport("kernel32.dll")]
private static extern bool CreateSymbolicLink(string lpSymlinkFileName, string lpTargetFileName, SymbolicLink dwFlags);
private static double VERSION = 1.6;
public MainWindow()
{
InitializeComponent();
Init();
}
private async void Init()
{
UpdateInstallButton(false);
UpdateStatus status = await CheckForUpdates();
switch (status)
{
case UpdateStatus.UpdateFound:
ShowUpdateDialog();
return;
case UpdateStatus.Error:
return;
}
string versionString = "v" + VERSION;
InstallerWindow.Title = "EZPPClient Installer " + versionString.Replace(",", ".");
string osuPath = "";
UpdateInstallButton(!string.IsNullOrEmpty(osuPath));
if (!string.IsNullOrEmpty(osuPath))
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<UpdateStatus> CheckForUpdates()
{
#if !DEBUG
try
{
using (WebClient client = new WebClient())
{
string newVersionString = client.DownloadString("https://ez-pp.farm/static/client/installer.ver");
double ver = 0;
try
{
ver = double.Parse(newVersionString, CultureInfo.InvariantCulture);
}
catch (FormatException)
{
}
if (ver > VERSION)
return UpdateStatus.UpdateFound;
}
}
catch (WebException e)
{
ShowErrorDialog(e.Message);
return UpdateStatus.Error;
}
#endif
return UpdateStatus.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 ShowUpdateDialog()
{
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");
ShowUpdateDialog();
}
}
private void UpdateInstallButton(bool enable)
{
InstallButton.IsEnabled = enable;
}
private void Install_Click(object sender, RoutedEventArgs e)
{
}
private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
System.Diagnostics.Process.Start("https://ez-pp.farm/");
e.Handled = true;
}
private void Browse_Click(object sender, RoutedEventArgs e)
{
using (var fbd = new FolderBrowserDialog())
{
DialogResult result = fbd.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
{
string[] files = Directory.GetFiles(fbd.SelectedPath, "*");
string[] subDirs = Directory.GetDirectories(fbd.SelectedPath, "*");
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;
}
if (!hasSongsFolder || !hasSkinsFolder || !hasOsuExecutable)
{
new ContentDialog()
{
Title = "Hmmm..",
Content = "It seems like this Folder is not a osu! Installation.",
PrimaryButtonText = "Okay qwq"
}.ShowAsync();
}
else
{
folderTextbox.Text = fbd.SelectedPath;
UpdateInstallButton(true);
}
}
}
}
private enum SymbolicLink
{
File,
Directory
}
private enum UpdateStatus
{
UpdateFound,
UpToDate,
Error
}
}
}