From 257bc3f7ba1861506b845d1fc51a3bdcb080e642 Mon Sep 17 00:00:00 2001 From: foglio Date: Wed, 26 Apr 2017 19:44:03 +0200 Subject: [PATCH] autoupdater fixes --- TCC.UI/App.xaml.cs | 5 +- TCC.UI/UpdateManager.cs | 84 +++++++++++++--------- TCC.Updater/TCC.Updater/TCC.Updater.csproj | 2 +- 3 files changed, 57 insertions(+), 34 deletions(-) diff --git a/TCC.UI/App.xaml.cs b/TCC.UI/App.xaml.cs index 7ecbc0c60..278a0b7bb 100644 --- a/TCC.UI/App.xaml.cs +++ b/TCC.UI/App.xaml.cs @@ -34,7 +34,10 @@ private void OnStartup(object sender, StartupEventArgs ev) var cd = AppDomain.CurrentDomain; cd.UnhandledException += GlobalUnhandledExceptionHandler; System.Diagnostics.Process.GetCurrentProcess().PriorityClass = System.Diagnostics.ProcessPriorityClass.High; - + if (File.Exists(Environment.CurrentDirectory + "/TCCupdater.exe")) + { + File.Delete(Environment.CurrentDirectory + "/TCCupdater.exe"); + } UpdateManager.CheckAppVersion(); UpdateManager.CheckDatabaseVersion(); diff --git a/TCC.UI/UpdateManager.cs b/TCC.UI/UpdateManager.cs index e682e8fd6..02f44433c 100644 --- a/TCC.UI/UpdateManager.cs +++ b/TCC.UI/UpdateManager.cs @@ -21,26 +21,30 @@ public static void CheckDatabaseVersion() { using (WebClient c = new WebClient()) { - c.DownloadFile(databaseVersion, "newDbVer"); - } - int v = 0; - try - { - v = Convert.ToInt32(File.ReadAllText("resources/images/current_version")); - } - catch (Exception) - { - v = 0; - } - int newVer = Convert.ToInt32(File.ReadAllText("newDbVer")); + try + { + var st = c.OpenRead(databaseVersion); + StreamReader sr = new StreamReader(st); - if(v < newVer) - { - //update - if(MessageBox.Show(String.Format("Updated icons database available (v{0}). Download now?", newVer), "TCC", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) + int newVersion = Convert.ToInt32(sr.ReadLine()); + int currentVersion = 0; + if (File.Exists("resources/images/current_version")) + { + currentVersion = Convert.ToInt32(File.OpenText("resources/images/current_version").ReadLine()); + } + + if (newVersion > currentVersion) + { + if (MessageBox.Show(String.Format("Icons database v{0} available. Download now?", newVersion), "TCC", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) + { + DownloadDatabase(); + ExtractDatabase(); + } + } + } + catch (Exception) { - DownloadDatabase(); - ExtractDatabase(); + MessageBox.Show("Error while checking database updates.", "TCC", MessageBoxButton.OK, MessageBoxImage.Error); } } } @@ -99,19 +103,26 @@ public static void CheckAppVersion() { using (WebClient c = new WebClient()) { - var st = c.OpenRead(appVersion); - StreamReader sr = new StreamReader(st); - string newVersionInfo = sr.ReadLine(); - string newVersionUrl = sr.ReadLine(); - - var v = Version.Parse(newVersionInfo); - if(v > Assembly.GetExecutingAssembly().GetName().Version) + try { - if (MessageBox.Show(String.Format("TCC v{0} available. Download now?", newVersionInfo), "TCC", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) + var st = c.OpenRead(appVersion); + StreamReader sr = new StreamReader(st); + string newVersionInfo = sr.ReadLine(); + string newVersionUrl = sr.ReadLine(); + + var v = Version.Parse(newVersionInfo); + if (v > Assembly.GetExecutingAssembly().GetName().Version) { - Update(newVersionUrl); + if (MessageBox.Show(String.Format("TCC v{0} available. Download now?", newVersionInfo), "TCC", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) + { + Update(newVersionUrl); + } } } + catch (Exception) + { + MessageBox.Show("Error while checking updates.", "TCC", MessageBoxButton.OK, MessageBoxImage.Error); + } } } @@ -120,11 +131,20 @@ private static void Update(string url) { using (WebClient c = new WebClient()) { - c.DownloadFile(url, "update.zip"); - ZipFile.ExtractToDirectory("update.zip", Environment.CurrentDirectory + "/tmp"); - File.Move(Environment.CurrentDirectory + "/tmp/TCCupdater.exe", Environment.CurrentDirectory + "/TCCupdater.exe"); - Process.Start(Environment.CurrentDirectory + "/TCCupdater.exe"); - Environment.Exit(0); + try + { + c.DownloadFile(url, "update.zip"); + ZipFile.ExtractToDirectory("update.zip", Environment.CurrentDirectory + "/tmp"); + File.Move(Environment.CurrentDirectory + "/tmp/TCCupdater.exe", Environment.CurrentDirectory + "/TCCupdater.exe"); + Process.Start(Environment.CurrentDirectory + "/TCCupdater.exe"); + Environment.Exit(0); + } + catch (Exception) + { + + MessageBox.Show("Couldn't download update.", "TCC", MessageBoxButton.OK, MessageBoxImage.Error); + + } } } } diff --git a/TCC.Updater/TCC.Updater/TCC.Updater.csproj b/TCC.Updater/TCC.Updater/TCC.Updater.csproj index 51f519cfa..4085c04b8 100644 --- a/TCC.Updater/TCC.Updater/TCC.Updater.csproj +++ b/TCC.Updater/TCC.Updater/TCC.Updater.csproj @@ -26,7 +26,7 @@ AnyCPU pdbonly true - bin\Release\ + ..\..\TCC.UI\bin\Release\ TRACE prompt 4