From 3d208fdaef4cf31e8b82e4ebd9d51c5ec33544cf Mon Sep 17 00:00:00 2001 From: Miepee Date: Tue, 25 Oct 2022 22:00:16 +0200 Subject: [PATCH] invert if condition for updater --- AM2RLauncher/AM2RLauncher/LauncherUpdater.cs | 221 +++++++++---------- 1 file changed, 110 insertions(+), 111 deletions(-) diff --git a/AM2RLauncher/AM2RLauncher/LauncherUpdater.cs b/AM2RLauncher/AM2RLauncher/LauncherUpdater.cs index 471da3d..67818c4 100644 --- a/AM2RLauncher/AM2RLauncher/LauncherUpdater.cs +++ b/AM2RLauncher/AM2RLauncher/LauncherUpdater.cs @@ -100,147 +100,146 @@ public static class LauncherUpdater // Check settings if autoUpdateLauncher is set to true bool autoUpdate = Boolean.Parse(MainForm.ReadFromConfig("AutoUpdateLauncher")); - if (autoUpdate) + if (!autoUpdate) { - log.Info("AutoUpdate Launcher set to true!"); + log.Info("AutoUpdate Launcher set to false. Exiting update check."); + return; + } + + log.Info("AutoUpdate Launcher set to true!"); - // This is supposed to fix the updater throwing an exception on windows 7 and earlier(?) - // See this for information: https://stackoverflow.com/q/2859790 and https://stackoverflow.com/a/50977774 - if (OS.IsWindows) - { - ServicePointManager.Expect100Continue = true; - ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; - } + // This is supposed to fix the updater throwing an exception on windows 7 and earlier(?) + // See this for information: https://stackoverflow.com/q/2859790 and https://stackoverflow.com/a/50977774 + if (OS.IsWindows) + { + ServicePointManager.Expect100Continue = true; + ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; + } - HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://github.com/AM2R-Community-Developers/AM2RLauncher/releases/latest"); - HttpWebResponse response; - try - { - response = (HttpWebResponse)request.GetResponse(); - } - catch (WebException) - { - log.Error("WebException caught! Displaying MessageBox."); - MessageBox.Show(Language.Text.NoInternetConnection); - return; - } + HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://github.com/AM2R-Community-Developers/AM2RLauncher/releases/latest"); + HttpWebResponse response; + try + { + response = (HttpWebResponse)request.GetResponse(); + } + catch (WebException) + { + log.Error("WebException caught! Displaying MessageBox."); + MessageBox.Show(Language.Text.NoInternetConnection); + return; + } - Uri realUri = response.ResponseUri; - string onlineVersion = realUri.AbsoluteUri.Substring(realUri.AbsoluteUri.LastIndexOf('/') + 1); - bool isCurrentVersionOutdated = false; + Uri realUri = response.ResponseUri; + string onlineVersion = realUri.AbsoluteUri.Substring(realUri.AbsoluteUri.LastIndexOf('/') + 1); + bool isCurrentVersionOutdated = false; - string[] localVersionArray = VERSION.Split('.'); - string[] onlineVersionArray = onlineVersion.Split('.'); + string[] localVersionArray = VERSION.Split('.'); + string[] onlineVersionArray = onlineVersion.Split('.'); - for (int i = 0; i < localVersionArray.Length; i++) + for (int i = 0; i < localVersionArray.Length; i++) + { + int onlineNum = Int32.Parse(onlineVersionArray[i]); + int localNum = Int32.Parse(localVersionArray[i]); + if (onlineNum > localNum) { - int onlineNum = Int32.Parse(onlineVersionArray[i]); - int localNum = Int32.Parse(localVersionArray[i]); - if (onlineNum > localNum) - { - isCurrentVersionOutdated = true; - break; - } - if (localNum > onlineNum) - break; + isCurrentVersionOutdated = true; + break; } + if (localNum > onlineNum) + break; + } - log.Info((isCurrentVersionOutdated ? "Updating" : "Not Updating") + " from " + VERSION + " to " + onlineVersion); + log.Info((isCurrentVersionOutdated ? "Updating" : "Not Updating") + " from " + VERSION + " to " + onlineVersion); - // No new update, exiting - if (!isCurrentVersionOutdated) - return; + // No new update, exiting + if (!isCurrentVersionOutdated) + return; - // For mac, we just show a message box that a new version is available, because I don't want to support it yet. - // hardcoded string, since also temporarily until it gets supported one day. - if (OS.IsMac) - { - MessageBox.Show("Your current version is outdated! The newest version is " + onlineVersion + ". " + - "Please recompile AM2RLauncher again or disable auto-updating"); - return; - } + // For mac, we just show a message box that a new version is available, because I don't want to support it yet. + // hardcoded string, since also temporarily until it gets supported one day. + if (OS.IsMac) + { + MessageBox.Show("Your current version is outdated! The newest version is " + onlineVersion + ". " + + "Please recompile AM2RLauncher again or disable auto-updating"); + return; + } - log.Info("Current version (" + VERSION + ") is outdated! Initiating update for version " + onlineVersion + "."); + log.Info("Current version (" + VERSION + ") is outdated! Initiating update for version " + onlineVersion + "."); - string tmpUpdatePath = CrossPlatformOperations.CurrentPath + "/tmpupdate/"; - string zipPath = CrossPlatformOperations.CurrentPath + "/launcher.zip"; + string tmpUpdatePath = CrossPlatformOperations.CurrentPath + "/tmpupdate/"; + string zipPath = CrossPlatformOperations.CurrentPath + "/launcher.zip"; - // Clean tmpupdate - if (Directory.Exists(tmpUpdatePath)) - Directory.Delete(tmpUpdatePath, true); - if (!Directory.Exists(tmpUpdatePath)) - Directory.CreateDirectory(tmpUpdatePath); + // Clean tmpupdate + if (Directory.Exists(tmpUpdatePath)) + Directory.Delete(tmpUpdatePath, true); + if (!Directory.Exists(tmpUpdatePath)) + Directory.CreateDirectory(tmpUpdatePath); - try - { - using WebClient client = new WebClient(); - string platformSuffix = ""; - if (OS.IsWindows) platformSuffix = "_win"; - else if (OS.IsLinux) platformSuffix = "_lin"; + try + { + using WebClient client = new WebClient(); + string platformSuffix = ""; + if (OS.IsWindows) platformSuffix = "_win"; + else if (OS.IsLinux) platformSuffix = "_lin"; - log.Info("Downloading https://github.com/AM2R-Community-Developers/AM2RLauncher/releases/latest/download/AM2RLauncher_" + onlineVersion + platformSuffix + ".zip to " + zipPath + "."); + log.Info("Downloading https://github.com/AM2R-Community-Developers/AM2RLauncher/releases/latest/download/AM2RLauncher_" + onlineVersion + platformSuffix + ".zip to " + zipPath + "."); - client.DownloadFile("https://github.com/AM2R-Community-Developers/AM2RLauncher/releases/latest/download/AM2RLauncher_" + onlineVersion + platformSuffix + ".zip", zipPath); + client.DownloadFile("https://github.com/AM2R-Community-Developers/AM2RLauncher/releases/latest/download/AM2RLauncher_" + onlineVersion + platformSuffix + ".zip", zipPath); - log.Info("File successfully downloaded."); - } - catch (UnauthorizedAccessException) - { - log.Error("UnauthorizedAccessException caught! Displaying MessageBox."); - MessageBox.Show(Language.Text.UnauthorizedAccessMessage); - return; - } + log.Info("File successfully downloaded."); + } + catch (UnauthorizedAccessException) + { + log.Error("UnauthorizedAccessException caught! Displaying MessageBox."); + MessageBox.Show(Language.Text.UnauthorizedAccessMessage); + return; + } + + ZipFile.ExtractToDirectory(zipPath, tmpUpdatePath); + log.Info("Updates successfully extracted to " + tmpUpdatePath); - ZipFile.ExtractToDirectory(zipPath, tmpUpdatePath); - log.Info("Updates successfully extracted to " + tmpUpdatePath); + File.Delete(zipPath); + File.Move(updatePath + "/" + CrossPlatformOperations.LauncherName, CrossPlatformOperations.CurrentPath + "/AM2RLauncher.bak"); + if (OS.IsWindows) File.Move(CrossPlatformOperations.LauncherName + ".config", CrossPlatformOperations.LauncherName + ".oldCfg"); - File.Delete(zipPath); - File.Move(updatePath + "/" + CrossPlatformOperations.LauncherName, CrossPlatformOperations.CurrentPath + "/AM2RLauncher.bak"); - if (OS.IsWindows) File.Move(CrossPlatformOperations.LauncherName + ".config", CrossPlatformOperations.LauncherName + ".oldCfg"); + foreach (FileInfo file in new DirectoryInfo(tmpUpdatePath).GetFiles()) + { + log.Info("Moving " + file.FullName + " to " + CrossPlatformOperations.CurrentPath + "/" + file.Name); + File.Copy(file.FullName, updatePath + "/" + file.Name, true); + } + // For windows, the actual application is in "AM2RLauncher.dll". Which means, we need to update the lib folder as well. + if (OS.IsWindows && Directory.Exists(CrossPlatformOperations.CurrentPath + "/lib")) + { + // So, because Windows behavior is dumb... - foreach (FileInfo file in new DirectoryInfo(tmpUpdatePath).GetFiles()) + // Rename all files in lib to *.bak + foreach (FileInfo file in new DirectoryInfo(CrossPlatformOperations.CurrentPath + "/lib").GetFiles()) { - log.Info("Moving " + file.FullName + " to " + CrossPlatformOperations.CurrentPath + "/" + file.Name); - File.Copy(file.FullName, updatePath + "/" + file.Name, true); + file.CopyTo(file.Directory + "/" + file.Name + ".bak"); } - // For windows, the actual application is in "AM2RLauncher.dll". Which means, we need to update the lib folder as well. - if (OS.IsWindows && Directory.Exists(CrossPlatformOperations.CurrentPath + "/lib")) - { - // So, because Windows behavior is dumb... - // Rename all files in lib to *.bak - foreach (FileInfo file in new DirectoryInfo(CrossPlatformOperations.CurrentPath + "/lib").GetFiles()) - { - file.CopyTo(file.Directory + "/" + file.Name + ".bak"); - } - - // Do the same for each sub directory - foreach (DirectoryInfo dir in new DirectoryInfo(CrossPlatformOperations.CurrentPath + "/lib").GetDirectories()) + // Do the same for each sub directory + foreach (DirectoryInfo dir in new DirectoryInfo(CrossPlatformOperations.CurrentPath + "/lib").GetDirectories()) + { + foreach (FileInfo file in dir.GetFiles()) { - foreach (FileInfo file in dir.GetFiles()) - { - file.CopyTo(file.Directory + "/" + file.Name + ".bak"); - } + file.CopyTo(file.Directory + "/" + file.Name + ".bak"); } - - // Yes, the above calls could be recursive. No, I can't be bothered to make them as such. - if (Directory.Exists(tmpUpdatePath + "lib")) - HelperMethods.DirectoryCopy(tmpUpdatePath + "lib", CrossPlatformOperations.CurrentPath + "/lib"); } - Directory.Delete(tmpUpdatePath, true); + // Yes, the above calls could be recursive. No, I can't be bothered to make them as such. + if (Directory.Exists(tmpUpdatePath + "lib")) + HelperMethods.DirectoryCopy(tmpUpdatePath + "lib", CrossPlatformOperations.CurrentPath + "/lib"); + } - MainForm.CopyOldConfigToNewConfig(); + Directory.Delete(tmpUpdatePath, true); - log.Info("Files extracted. Preparing to restart executable..."); - if (OS.IsLinux) Process.Start("chmod", "+x " + updatePath + "./AM2RLauncher.Gtk"); + MainForm.CopyOldConfigToNewConfig(); - Process.Start(updatePath + "/" + CrossPlatformOperations.LauncherName); - Environment.Exit(0); - } - else - { - log.Info("AutoUpdate Launcher set to false. Exiting update check."); - } + log.Info("Files extracted. Preparing to restart executable..."); + if (OS.IsLinux) Process.Start("chmod", "+x " + updatePath + "./AM2RLauncher.Gtk"); + + Process.Start(updatePath + "/" + CrossPlatformOperations.LauncherName); + Environment.Exit(0); } } \ No newline at end of file