From 0f47291018452722427a66984d1ba577f33ac720 Mon Sep 17 00:00:00 2001 From: dudantas Date: Fri, 3 Feb 2023 02:46:50 -0300 Subject: [PATCH] Fix some things Added check to close the launcher if the version specified in the packages.json url is the same as the current packages.json Added a thread for extracting, copying and deleting new client data, preventing the program from getting stuck during this time. --- MainWindow.xaml.cs | 94 +++++++++++++++++++------------------------- SplashScreen.xaml.cs | 62 +++++++++++++++++++++++++---- 2 files changed, 95 insertions(+), 61 deletions(-) diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index e721b26..977effd 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -10,6 +10,7 @@ using System.Diagnostics; using Newtonsoft.Json; using System.Net.Http; +using System.Threading.Tasks; namespace CanaryLauncherUpdate { @@ -23,8 +24,9 @@ public partial class MainWindow : Window bool needUpdate = false; string clientName = "client.exe"; string urlClient = "https://github.com/dudantas/CanaryLauncherUpdate/releases/download/download-files/client.zip"; - string urlVersion = "https://github.com/dudantas/CanaryLauncherUpdate/releases/download/download-files/version.txt"; - string currentVersion = ""; + string urlPackage = "https://github.com/dudantas/CanaryLauncherUpdate/releases/download/download-files/package.json"; + string newVersion = ""; + string programVersion = "1.0"; string path = AppDomain.CurrentDomain.BaseDirectory.ToString(); public MainWindow() @@ -32,12 +34,23 @@ public MainWindow() InitializeComponent(); } - private void TibiaLauncher_Load(object sender, RoutedEventArgs e) + // This will pull the version of the "package.json" file from a user-defined url. + private async Task GetPackageVersionFromUrl(string url) + { + using (HttpClient client = new HttpClient()) + { + string json = await client.GetStringAsync(url); + var data = JsonConvert.DeserializeObject(json); + return data.version.ToString(); + } + } + + private async void TibiaLauncher_Load(object sender, RoutedEventArgs e) { ImageLogoServer.Source = new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "pack://application:,,,/Assets/logo.png")); ImageLogoCompany.Source = new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "pack://application:,,,/Assets/logo_company.png")); - currentVersion = httpClient.GetStringAsync(urlVersion).Result; + newVersion = await GetPackageVersionFromUrl(urlPackage); progressbarDownload.Visibility = Visibility.Collapsed; labelClientVersion.Visibility = Visibility.Collapsed; labelDownloadPercent.Visibility = Visibility.Collapsed; @@ -46,45 +59,27 @@ private void TibiaLauncher_Load(object sender, RoutedEventArgs e) { Directory.CreateDirectory(path); } - if (Directory.Exists(path + "/clientlauncherupdate-main")) - { - clientDownloaded = true; - } - if (File.Exists(path + "/version.txt")) + if (File.Exists(path + "/package.json")) { // Read actual client version - StreamReader reader = new StreamReader(path + "/version.txt"); - string? myVersion = reader.ReadLine(); - reader.Close(); - - labelVersion.Text = "v" + myVersion; - - if (currentVersion == myVersion) - { - buttonPlay.Background = new ImageBrush(new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "pack://application:,,,/Assets/button_play.png"))); - buttonPlayIcon.Source = new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "pack://application:,,,/Assets/icon_play.png")); - labelClientVersion.Content = GetClientVersion(path); - labelClientVersion.Visibility = Visibility.Visible; - buttonPlay_tooltip.Text = GetClientVersion(path); - needUpdate = false; - StartClient(); - } + string actualVersion = GetClientVersion(path); + labelVersion.Text = "v" + programVersion; - if (currentVersion != myVersion) + if (newVersion != actualVersion) { buttonPlay.Background = new ImageBrush(new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "pack://application:,,,/Assets/button_update.png"))); buttonPlayIcon.Source = new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "pack://application:,,,/Assets/icon_update.png")); - labelClientVersion.Content = currentVersion; + labelClientVersion.Content = newVersion; labelClientVersion.Visibility = Visibility.Visible; buttonPlay.Visibility = Visibility.Visible; buttonPlay_tooltip.Text = "Update"; needUpdate = true; } } - if (!File.Exists(path + "/version.txt")) + if (!File.Exists(path + "/package.json")) { - labelVersion.Text = "v" + currentVersion; + labelVersion.Text = "v" + programVersion; buttonPlay.Background = new ImageBrush(new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "pack://application:,,,/Assets/button_update.png"))); buttonPlayIcon.Source = new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "pack://application:,,,/Assets/icon_update.png")); labelClientVersion.Content = "Download"; @@ -95,12 +90,6 @@ private void TibiaLauncher_Load(object sender, RoutedEventArgs e) } } - private void StartClient() - { - Process.Start(path + "/bin/" + clientName); - this.Close(); - } - static string GetClientVersion(string path) { string json = path + "/package.json"; @@ -161,15 +150,18 @@ private void buttonPlay_Click(object sender, RoutedEventArgs e) } } - private void Client_DownloadFileCompleted(object? sender, System.ComponentModel.AsyncCompletedEventArgs e) + private async void Client_DownloadFileCompleted(object? sender, System.ComponentModel.AsyncCompletedEventArgs e) { buttonPlay.Background = new ImageBrush(new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "pack://application:,,,/Assets/button_play.png"))); buttonPlayIcon.Source = new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "pack://application:,,,/Assets/icon_play.png")); - Directory.CreateDirectory(path); - ZipFile.ExtractToDirectory(path + "/tibia.zip", path, true); - File.Delete(path + "/tibia.zip"); - File.WriteAllText(Path.Combine(path, "version.txt"), GetClientVersion(path)); + // Adds the task to a secondary task to prevent the program from crashing while this is running + await Task.Run(() => + { + Directory.CreateDirectory(path); + ZipFile.ExtractToDirectory(path + "/tibia.zip", path, true); + File.Delete(path + "/tibia.zip"); + }); progressbarDownload.Value = 100; needUpdate = false; @@ -210,17 +202,14 @@ static string SizeSuffix(Int64 value, int decimalPlaces = 1) private void buttonPlay_MouseEnter(object sender, MouseEventArgs e) { - if (File.Exists(path + "/version.txt")) + if (File.Exists(path + "/package.json")) { - StreamReader reader = new StreamReader(path + "/version.txt"); - string? myVersion = reader.ReadLine(); - reader.Close(); - - if (currentVersion != myVersion) + string actualVersion = GetClientVersion(path); + if (newVersion != actualVersion) { buttonPlay.Background = new ImageBrush(new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "pack://application:,,,/Assets/button_hover_update.png"))); } - if (currentVersion == myVersion) + if (newVersion == actualVersion) { buttonPlay.Background = new ImageBrush(new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "pack://application:,,,/Assets/button_hover_play.png"))); } @@ -233,17 +222,14 @@ private void buttonPlay_MouseEnter(object sender, MouseEventArgs e) private void buttonPlay_MouseLeave(object sender, MouseEventArgs e) { - if (File.Exists(path + "/version.txt")) + if (File.Exists(path + "/package.json")) { - StreamReader reader = new StreamReader(path + "/version.txt"); - string? myVersion = reader.ReadLine(); - reader.Close(); - - if (currentVersion != myVersion) + string actualVersion = GetClientVersion(path); + if (newVersion != actualVersion) { buttonPlay.Background = new ImageBrush(new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "pack://application:,,,/Assets/button_update.png"))); } - if (currentVersion == myVersion) + if (newVersion == actualVersion) { buttonPlay.Background = new ImageBrush(new BitmapImage(new Uri(BaseUriHelper.GetBaseUri(this), "pack://application:,,,/Assets/button_play.png"))); } diff --git a/SplashScreen.xaml.cs b/SplashScreen.xaml.cs index 44a3e93..ad5c13a 100644 --- a/SplashScreen.xaml.cs +++ b/SplashScreen.xaml.cs @@ -4,6 +4,16 @@ using System.Net; using System.Windows.Threading; using System.Net.Http; +using System.Threading.Tasks; +using Newtonsoft.Json; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.IO.Compression; +using System.Diagnostics; +using Newtonsoft.Json; +using System.Threading.Tasks; namespace CanaryLauncherUpdate { @@ -11,10 +21,42 @@ public partial class SplashScreen : Window { static readonly HttpClient httpClient = new HttpClient(); DispatcherTimer timer = new DispatcherTimer(); - string urlClient = "https://github.com/lucasgiovannibr/clientlauncherupdate/archive/refs/heads/main.zip"; - string urlVersion = "https://raw.githubusercontent.com/lucasgiovannibr/clientlauncherupdate/main/version.txt"; - string currentVersion = ""; + string clientName = "client.exe"; + string urlClient = "https://github.com/dudantas/CanaryLauncherUpdate/releases/download/download-files/client.zip"; + string urlPackage = "https://github.com/dudantas/CanaryLauncherUpdate/releases/download/download-files/package.json"; + string newVersion = ""; string path = AppDomain.CurrentDomain.BaseDirectory.ToString(); + + // This will pull the version of the "package.json" file from a user-defined url. + private async Task GetPackageVersionFromUrl(string url) + { + using (HttpClient client = new HttpClient()) + { + string json = await client.GetStringAsync(url); + var data = JsonConvert.DeserializeObject(json); + return data.version.ToString(); + } + } + + static string GetClientVersion(string path) + { + string json = path + "/package.json"; + StreamReader stream = new StreamReader(json); + dynamic jsonString = stream.ReadToEnd(); + dynamic versionclient = JsonConvert.DeserializeObject(jsonString); + foreach (string version in versionclient) + { + return version; + } + + return ""; + } + + private void StartClient() + { + Process.Start(path + "/bin/" + clientName); + this.Close(); + } public SplashScreen() { @@ -26,14 +68,20 @@ public SplashScreen() public async void timer_SplashScreen(object? sender, EventArgs e) { - var requestCurrentVersion = new HttpRequestMessage(HttpMethod.Post, urlVersion); - var responseCurrentVersion = await httpClient.SendAsync(requestCurrentVersion); - currentVersion = await responseCurrentVersion.Content.ReadAsStringAsync(); - if (currentVersion == null) + string newVersion = await GetPackageVersionFromUrl(urlPackage); + if (newVersion == null) { this.Close(); } + // Start the client if the versions are the same + if (File.Exists(path + "/package.json")) { + string actualVersion = GetClientVersion(path); + if (newVersion == actualVersion) { + StartClient(); + } + } + var requestClient = new HttpRequestMessage(HttpMethod.Post, urlClient); var response = await httpClient.SendAsync(requestClient); if (response.StatusCode == HttpStatusCode.NotFound)