From 0fc0b9eead9001c7b1eedc5c08bc89346f853fa7 Mon Sep 17 00:00:00 2001 From: CreepyCrafter24 <33260128+CreepyCrafter24@users.noreply.github.com> Date: Wed, 8 Apr 2020 18:41:57 +0200 Subject: [PATCH] Class containing some duplicate code for Update Checks --- Installer/Program.cs | 14 ++++++-------- UpTool2/Program.cs | 26 ++++++++++++++++---------- UpToolCLI/Program.cs | 27 ++++++++++++++------------- UpToolLib/DataStructures/App.cs | 2 +- UpToolLib/Tool/RepoManagement.cs | 2 +- UpToolLib/UpToolLib.csproj | 2 ++ UpToolLib/UpdateCheck.cs | 28 ++++++++++++++++++++++++++++ 7 files changed, 68 insertions(+), 33 deletions(-) create mode 100644 UpToolLib/UpdateCheck.cs diff --git a/Installer/Program.cs b/Installer/Program.cs index f741122..78985c2 100644 --- a/Installer/Program.cs +++ b/Installer/Program.cs @@ -37,21 +37,19 @@ namespace Installer ExternalFunctionalityManager.Init(new UtLibFunctionsCli()); WebClient client = new WebClient(); Console.WriteLine("Downloading metadata"); - XElement meta = XDocument.Load("https://github.com/JFronny/UpTool2/releases/latest/download/meta.xml") - .Element("meta"); + UpdateCheck.Reload("https://github.com/JFronny/UpTool2/releases/latest/download/meta.xml"); Console.WriteLine("Downloading binary"); - byte[] dl = client.DownloadData(meta.Element("File").Value); + byte[] dl = client.DownloadData(UpdateCheck.App); Console.WriteLine("Verifying integrity"); using (SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider()) { string pkgHash = BitConverter.ToString(sha256.ComputeHash(dl)).Replace("-", string.Empty).ToUpper(); - if (pkgHash != meta.Element("Hash").Value.ToUpper()) - throw new Exception( - $"The hash is not equal to the one stored in the repo:\r\nPackage: {pkgHash}\r\nOnline: {meta.Element("Hash").Value.ToUpper()}"); + if (pkgHash != UpdateCheck.AppHash) + throw new Exception($@"The hash is not equal to the one stored in the repo: +Package: {pkgHash} +Online: {UpdateCheck.AppHash}"); } Console.WriteLine("Extracting"); - //if (Directory.Exists(PathTool.GetRelative("Install"))) - // Directory.Delete(PathTool.GetRelative("Install"), true); if (Directory.Exists(PathTool.GetRelative("Install"))) { foreach (string file in Directory.GetFiles(PathTool.GetRelative("Install"))) File.Delete(file); diff --git a/UpTool2/Program.cs b/UpTool2/Program.cs index 6d2d5ec..9107c3c 100644 --- a/UpTool2/Program.cs +++ b/UpTool2/Program.cs @@ -53,8 +53,15 @@ namespace UpTool2 Directory.CreateDirectory(PathTool.Dir); FixXml(); SetSplash(2, "Performing checks"); - string metaXml = XDocument.Load(PathTool.InfoXml).Element("meta").Element("UpdateSource").Value; - Online = new Uri(metaXml).Ping(); + try + { + UpToolLib.UpdateCheck.Reload(); + Online = true; + } + catch + { + Online = false; + } if (Application.ExecutablePath != PathTool.GetRelative("Install", "UpTool2.dll")) Splash.Invoke((Action) (() => MessageBox.Show(Splash, $"WARNING!{Environment.NewLine}Running from outside the install directory is not recommended!") @@ -63,7 +70,7 @@ namespace UpTool2 Directory.CreateDirectory(PathTool.GetRelative("Apps")); if (!Online) SetSplash(7, "Opening"); - if (!Online || UpdateCheck(metaXml)) + if (!Online || UpdateCheck()) Application.Run(new MainForm()); #if !DEBUG } @@ -184,15 +191,14 @@ namespace UpTool2 } } - private static bool UpdateCheck(string metaXml) + private static bool UpdateCheck() { SetSplash(3, "Comparing online version"); - XElement meta = XDocument.Load(metaXml).Element("meta"); - if (Assembly.GetExecutingAssembly().GetName().Version >= Version.Parse(meta.Element("Version").Value)) + if (Assembly.GetExecutingAssembly().GetName().Version >= UpToolLib.UpdateCheck.OnlineVersion) return true; byte[] dl; SetSplash(4, "Downloading latest"); - using (DownloadDialog dlg = new DownloadDialog(meta.Element("Installer").Value)) + using (DownloadDialog dlg = new DownloadDialog(UpToolLib.UpdateCheck.Installer.AbsoluteUri)) { if (dlg.ShowDialog() != DialogResult.OK) throw new Exception("Failed to update"); @@ -202,9 +208,9 @@ namespace UpTool2 using (SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider()) { string pkgHash = BitConverter.ToString(sha256.ComputeHash(dl)).Replace("-", string.Empty).ToUpper(); - if (pkgHash != meta.Element("InstallerHash").Value.ToUpper()) - throw new Exception("The hash is not equal to the one stored in the repo:\r\nPackage: " + pkgHash + - "\r\nOnline: " + meta.Element("InstallerHash").Value.ToUpper()); + if (pkgHash != UpToolLib.UpdateCheck.InstallerHash) + throw new Exception( + $"The hash is not equal to the one stored in the repo:\r\nPackage: {pkgHash}\r\nOnline: {UpToolLib.UpdateCheck.InstallerHash}"); } SetSplash(9, "Installing"); if (Directory.Exists(PathTool.GetRelative("Install", "tmp"))) diff --git a/UpToolCLI/Program.cs b/UpToolCLI/Program.cs index d66a676..c29a427 100644 --- a/UpToolCLI/Program.cs +++ b/UpToolCLI/Program.cs @@ -49,10 +49,12 @@ namespace UpToolCLI upgrade.Handler = CommandHandler.Create(Upgrade); rootCommand.AddCommand(upgrade); - rootCommand.AddCommand(new Command("upgrade-self", "Upgrades UpToolCLI") + Command command = new Command("upgrade-self", "Upgrades UpToolCLI") { - Handler = CommandHandler.Create(UpgradeSelf) - }); + new Option(new[] {"--force", "-f"}, "Overwrites older files") + }; + command.Handler = CommandHandler.Create(UpgradeSelf); + rootCommand.AddCommand(command); Command reinstall = new Command("reinstall", "Reinstall a package") { @@ -115,23 +117,23 @@ namespace UpToolCLI } } - private static void UpgradeSelf() + private static void UpgradeSelf(bool force) { #if DEBUG Console.WriteLine("Not enabled in debug builds"); #else - XElement meta = XDocument.Load(XDocument.Load(PathTool.InfoXml).Element("meta").Element("UpdateSource").Value).Element("meta"); Console.WriteLine("Downloading latest"); - (bool success, byte[] dl) = Functions.Download(new Uri(meta.Element("Installer").Value)); + (bool success, byte[] dl) = Functions.Download(UpdateCheck.Installer); if (!success) throw new Exception("Failed to update"); Console.WriteLine("Verifying"); using (SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider()) { string pkgHash = BitConverter.ToString(sha256.ComputeHash(dl)).Replace("-", string.Empty).ToUpper(); - if (pkgHash != meta.Element("InstallerHash").Value.ToUpper()) - throw new Exception("The hash is not equal to the one stored in the repo:\r\nPackage: " + pkgHash + - "\r\nOnline: " + meta.Element("InstallerHash").Value.ToUpper()); + if (pkgHash != UpdateCheck.InstallerHash) + throw new Exception($@"The hash is not equal to the one stored in the repo: +Package: {pkgHash} +Online: {UpdateCheck.InstallerHash}"); } Console.WriteLine("Installing"); if (Directory.Exists(PathTool.GetRelative("Install", "tmp"))) @@ -169,9 +171,8 @@ namespace UpToolCLI : $@"Found {updatableCount} Updates: {string.Join(Environment.NewLine, apps.Select(s => $"- {s.Name} ({s.Version})"))}"); #if !DEBUG - XElement meta = XDocument.Load(XDocument.Load(PathTool.InfoXml).Element("meta").Element("UpdateSource").Value).Element("meta"); Version vLocal = Assembly.GetExecutingAssembly().GetName().Version; - Version vOnline = Version.Parse(meta.Element("Version").Value); + Version vOnline = UpdateCheck.OnlineVersion; if (vLocal < vOnline) Console.WriteLine($"uptool is outdated ({vLocal} vs {vOnline}), update using \"uptool upgrade-self\""); #endif @@ -202,10 +203,10 @@ namespace UpToolCLI AppExtras.Update(app.Value, false); } #if !DEBUG - if (Assembly.GetExecutingAssembly().GetName().Version < Version.Parse(XDocument.Load(XDocument.Load(PathTool.InfoXml).Element("meta").Element("UpdateSource").Value).Element("meta").Element("Version").Value)) + if (Assembly.GetExecutingAssembly().GetName().Version < UpdateCheck.OnlineVersion) { Console.WriteLine("Updating self"); - UpgradeSelf(); + UpgradeSelf(false); } #endif Console.WriteLine("Done!"); diff --git a/UpToolLib/DataStructures/App.cs b/UpToolLib/DataStructures/App.cs index 84ba6f4..9e18469 100644 --- a/UpToolLib/DataStructures/App.cs +++ b/UpToolLib/DataStructures/App.cs @@ -59,7 +59,7 @@ namespace UpToolLib.DataStructures public override string ToString() => $@"Name: {Name} Description: -{string.Join(NewLine, Description.Split('\n').Select(s => { if (s.EndsWith("\r")) s.Remove(s.Length - 1, 1); return "> " + s; }))} +{string.Join(NewLine, Description.Split('\n').Select(s => { if (s.EndsWith("\r")) s.Remove(s.Length - 1, 1); return $"> {s}"; }))} Version: {Version} File: {File} Local: {Local} diff --git a/UpToolLib/Tool/RepoManagement.cs b/UpToolLib/Tool/RepoManagement.cs index d096a44..2bf96d5 100644 --- a/UpToolLib/Tool/RepoManagement.cs +++ b/UpToolLib/Tool/RepoManagement.cs @@ -142,7 +142,7 @@ namespace UpToolLib.Tool { XElement data = XDocument.Load(PathTool.GetInfoPath(tmp)).Element("app"); GlobalVariables.Apps.Add(tmp, - new App("(local) " + data.Element("Name").Value, data.Element("Description").Value, + new App($"(local) {data.Element("Name").Value}", data.Element("Description").Value, GlobalVariables.MinimumVer, "", true, "", tmp, Color.Red, ExternalFunctionalityManager.Instance.GetDefaultIcon(), data.Element("MainFile") != null, diff --git a/UpToolLib/UpToolLib.csproj b/UpToolLib/UpToolLib.csproj index fcaf778..9ee9dc9 100644 --- a/UpToolLib/UpToolLib.csproj +++ b/UpToolLib/UpToolLib.csproj @@ -2,6 +2,8 @@ netstandard2.1 + false + 1.0.* diff --git a/UpToolLib/UpdateCheck.cs b/UpToolLib/UpdateCheck.cs new file mode 100644 index 0000000..12abafd --- /dev/null +++ b/UpToolLib/UpdateCheck.cs @@ -0,0 +1,28 @@ +using System; +using System.Xml.Linq; +using UpToolLib.Tool; + +namespace UpToolLib +{ + public static class UpdateCheck + { + private static XElement _meta; + + private static XElement Meta + { + get + { + if (_meta is null) Reload(); + return _meta; + } + set => _meta = value; + } + public static Version OnlineVersion => Version.Parse(Meta.Element("Version").Value); + public static Uri Installer => new Uri(Meta.Element("Installer").Value).TryUnshorten(); + public static string InstallerHash => Meta.Element("InstallerHash").Value.ToUpper(); + public static Uri App => new Uri(Meta.Element("File").Value).TryUnshorten(); + public static string AppHash => Meta.Element("Hash").Value.ToUpper(); + public static void Reload() => Reload(XDocument.Load(PathTool.InfoXml).Element("meta").Element("UpdateSource").Value); + public static void Reload(string source) => Meta = XDocument.Load(source).Element("meta"); + } +} \ No newline at end of file