Class containing some duplicate code for Update Checks
This commit is contained in:
parent
a64d3ab4bc
commit
0fc0b9eead
|
@ -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);
|
||||
|
|
|
@ -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")))
|
||||
|
|
|
@ -49,10 +49,12 @@ namespace UpToolCLI
|
|||
upgrade.Handler = CommandHandler.Create<string, bool>(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<bool>(new[] {"--force", "-f"}, "Overwrites older files")
|
||||
};
|
||||
command.Handler = CommandHandler.Create<bool>(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!");
|
||||
|
|
|
@ -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}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<Deterministic>false</Deterministic>
|
||||
<AssemblyVersion>1.0.*</AssemblyVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
28
UpToolLib/UpdateCheck.cs
Normal file
28
UpToolLib/UpdateCheck.cs
Normal file
|
@ -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");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user