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());
|
ExternalFunctionalityManager.Init(new UtLibFunctionsCli());
|
||||||
WebClient client = new WebClient();
|
WebClient client = new WebClient();
|
||||||
Console.WriteLine("Downloading metadata");
|
Console.WriteLine("Downloading metadata");
|
||||||
XElement meta = XDocument.Load("https://github.com/JFronny/UpTool2/releases/latest/download/meta.xml")
|
UpdateCheck.Reload("https://github.com/JFronny/UpTool2/releases/latest/download/meta.xml");
|
||||||
.Element("meta");
|
|
||||||
Console.WriteLine("Downloading binary");
|
Console.WriteLine("Downloading binary");
|
||||||
byte[] dl = client.DownloadData(meta.Element("File").Value);
|
byte[] dl = client.DownloadData(UpdateCheck.App);
|
||||||
Console.WriteLine("Verifying integrity");
|
Console.WriteLine("Verifying integrity");
|
||||||
using (SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider())
|
using (SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider())
|
||||||
{
|
{
|
||||||
string pkgHash = BitConverter.ToString(sha256.ComputeHash(dl)).Replace("-", string.Empty).ToUpper();
|
string pkgHash = BitConverter.ToString(sha256.ComputeHash(dl)).Replace("-", string.Empty).ToUpper();
|
||||||
if (pkgHash != meta.Element("Hash").Value.ToUpper())
|
if (pkgHash != UpdateCheck.AppHash)
|
||||||
throw new Exception(
|
throw new Exception($@"The hash is not equal to the one stored in the repo:
|
||||||
$"The hash is not equal to the one stored in the repo:\r\nPackage: {pkgHash}\r\nOnline: {meta.Element("Hash").Value.ToUpper()}");
|
Package: {pkgHash}
|
||||||
|
Online: {UpdateCheck.AppHash}");
|
||||||
}
|
}
|
||||||
Console.WriteLine("Extracting");
|
Console.WriteLine("Extracting");
|
||||||
//if (Directory.Exists(PathTool.GetRelative("Install")))
|
|
||||||
// Directory.Delete(PathTool.GetRelative("Install"), true);
|
|
||||||
if (Directory.Exists(PathTool.GetRelative("Install")))
|
if (Directory.Exists(PathTool.GetRelative("Install")))
|
||||||
{
|
{
|
||||||
foreach (string file in Directory.GetFiles(PathTool.GetRelative("Install"))) File.Delete(file);
|
foreach (string file in Directory.GetFiles(PathTool.GetRelative("Install"))) File.Delete(file);
|
||||||
|
@ -53,8 +53,15 @@ namespace UpTool2
|
|||||||
Directory.CreateDirectory(PathTool.Dir);
|
Directory.CreateDirectory(PathTool.Dir);
|
||||||
FixXml();
|
FixXml();
|
||||||
SetSplash(2, "Performing checks");
|
SetSplash(2, "Performing checks");
|
||||||
string metaXml = XDocument.Load(PathTool.InfoXml).Element("meta").Element("UpdateSource").Value;
|
try
|
||||||
Online = new Uri(metaXml).Ping();
|
{
|
||||||
|
UpToolLib.UpdateCheck.Reload();
|
||||||
|
Online = true;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
Online = false;
|
||||||
|
}
|
||||||
if (Application.ExecutablePath != PathTool.GetRelative("Install", "UpTool2.dll"))
|
if (Application.ExecutablePath != PathTool.GetRelative("Install", "UpTool2.dll"))
|
||||||
Splash.Invoke((Action) (() => MessageBox.Show(Splash,
|
Splash.Invoke((Action) (() => MessageBox.Show(Splash,
|
||||||
$"WARNING!{Environment.NewLine}Running from outside the install directory is not recommended!")
|
$"WARNING!{Environment.NewLine}Running from outside the install directory is not recommended!")
|
||||||
@ -63,7 +70,7 @@ namespace UpTool2
|
|||||||
Directory.CreateDirectory(PathTool.GetRelative("Apps"));
|
Directory.CreateDirectory(PathTool.GetRelative("Apps"));
|
||||||
if (!Online)
|
if (!Online)
|
||||||
SetSplash(7, "Opening");
|
SetSplash(7, "Opening");
|
||||||
if (!Online || UpdateCheck(metaXml))
|
if (!Online || UpdateCheck())
|
||||||
Application.Run(new MainForm());
|
Application.Run(new MainForm());
|
||||||
#if !DEBUG
|
#if !DEBUG
|
||||||
}
|
}
|
||||||
@ -184,15 +191,14 @@ namespace UpTool2
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool UpdateCheck(string metaXml)
|
private static bool UpdateCheck()
|
||||||
{
|
{
|
||||||
SetSplash(3, "Comparing online version");
|
SetSplash(3, "Comparing online version");
|
||||||
XElement meta = XDocument.Load(metaXml).Element("meta");
|
if (Assembly.GetExecutingAssembly().GetName().Version >= UpToolLib.UpdateCheck.OnlineVersion)
|
||||||
if (Assembly.GetExecutingAssembly().GetName().Version >= Version.Parse(meta.Element("Version").Value))
|
|
||||||
return true;
|
return true;
|
||||||
byte[] dl;
|
byte[] dl;
|
||||||
SetSplash(4, "Downloading latest");
|
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)
|
if (dlg.ShowDialog() != DialogResult.OK)
|
||||||
throw new Exception("Failed to update");
|
throw new Exception("Failed to update");
|
||||||
@ -202,9 +208,9 @@ namespace UpTool2
|
|||||||
using (SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider())
|
using (SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider())
|
||||||
{
|
{
|
||||||
string pkgHash = BitConverter.ToString(sha256.ComputeHash(dl)).Replace("-", string.Empty).ToUpper();
|
string pkgHash = BitConverter.ToString(sha256.ComputeHash(dl)).Replace("-", string.Empty).ToUpper();
|
||||||
if (pkgHash != 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 +
|
throw new Exception(
|
||||||
"\r\nOnline: " + meta.Element("InstallerHash").Value.ToUpper());
|
$"The hash is not equal to the one stored in the repo:\r\nPackage: {pkgHash}\r\nOnline: {UpToolLib.UpdateCheck.InstallerHash}");
|
||||||
}
|
}
|
||||||
SetSplash(9, "Installing");
|
SetSplash(9, "Installing");
|
||||||
if (Directory.Exists(PathTool.GetRelative("Install", "tmp")))
|
if (Directory.Exists(PathTool.GetRelative("Install", "tmp")))
|
||||||
|
@ -49,10 +49,12 @@ namespace UpToolCLI
|
|||||||
upgrade.Handler = CommandHandler.Create<string, bool>(Upgrade);
|
upgrade.Handler = CommandHandler.Create<string, bool>(Upgrade);
|
||||||
rootCommand.AddCommand(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")
|
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
|
#if DEBUG
|
||||||
Console.WriteLine("Not enabled in debug builds");
|
Console.WriteLine("Not enabled in debug builds");
|
||||||
#else
|
#else
|
||||||
XElement meta = XDocument.Load(XDocument.Load(PathTool.InfoXml).Element("meta").Element("UpdateSource").Value).Element("meta");
|
|
||||||
Console.WriteLine("Downloading latest");
|
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)
|
if (!success)
|
||||||
throw new Exception("Failed to update");
|
throw new Exception("Failed to update");
|
||||||
Console.WriteLine("Verifying");
|
Console.WriteLine("Verifying");
|
||||||
using (SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider())
|
using (SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider())
|
||||||
{
|
{
|
||||||
string pkgHash = BitConverter.ToString(sha256.ComputeHash(dl)).Replace("-", string.Empty).ToUpper();
|
string pkgHash = BitConverter.ToString(sha256.ComputeHash(dl)).Replace("-", string.Empty).ToUpper();
|
||||||
if (pkgHash != meta.Element("InstallerHash").Value.ToUpper())
|
if (pkgHash != UpdateCheck.InstallerHash)
|
||||||
throw new Exception("The hash is not equal to the one stored in the repo:\r\nPackage: " + pkgHash +
|
throw new Exception($@"The hash is not equal to the one stored in the repo:
|
||||||
"\r\nOnline: " + meta.Element("InstallerHash").Value.ToUpper());
|
Package: {pkgHash}
|
||||||
|
Online: {UpdateCheck.InstallerHash}");
|
||||||
}
|
}
|
||||||
Console.WriteLine("Installing");
|
Console.WriteLine("Installing");
|
||||||
if (Directory.Exists(PathTool.GetRelative("Install", "tmp")))
|
if (Directory.Exists(PathTool.GetRelative("Install", "tmp")))
|
||||||
@ -169,9 +171,8 @@ namespace UpToolCLI
|
|||||||
: $@"Found {updatableCount} Updates:
|
: $@"Found {updatableCount} Updates:
|
||||||
{string.Join(Environment.NewLine, apps.Select(s => $"- {s.Name} ({s.Version})"))}");
|
{string.Join(Environment.NewLine, apps.Select(s => $"- {s.Name} ({s.Version})"))}");
|
||||||
#if !DEBUG
|
#if !DEBUG
|
||||||
XElement meta = XDocument.Load(XDocument.Load(PathTool.InfoXml).Element("meta").Element("UpdateSource").Value).Element("meta");
|
|
||||||
Version vLocal = Assembly.GetExecutingAssembly().GetName().Version;
|
Version vLocal = Assembly.GetExecutingAssembly().GetName().Version;
|
||||||
Version vOnline = Version.Parse(meta.Element("Version").Value);
|
Version vOnline = UpdateCheck.OnlineVersion;
|
||||||
if (vLocal < vOnline)
|
if (vLocal < vOnline)
|
||||||
Console.WriteLine($"uptool is outdated ({vLocal} vs {vOnline}), update using \"uptool upgrade-self\"");
|
Console.WriteLine($"uptool is outdated ({vLocal} vs {vOnline}), update using \"uptool upgrade-self\"");
|
||||||
#endif
|
#endif
|
||||||
@ -202,10 +203,10 @@ namespace UpToolCLI
|
|||||||
AppExtras.Update(app.Value, false);
|
AppExtras.Update(app.Value, false);
|
||||||
}
|
}
|
||||||
#if !DEBUG
|
#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");
|
Console.WriteLine("Updating self");
|
||||||
UpgradeSelf();
|
UpgradeSelf(false);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
Console.WriteLine("Done!");
|
Console.WriteLine("Done!");
|
||||||
|
@ -59,7 +59,7 @@ namespace UpToolLib.DataStructures
|
|||||||
|
|
||||||
public override string ToString() => $@"Name: {Name}
|
public override string ToString() => $@"Name: {Name}
|
||||||
Description:
|
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}
|
Version: {Version}
|
||||||
File: {File}
|
File: {File}
|
||||||
Local: {Local}
|
Local: {Local}
|
||||||
|
@ -142,7 +142,7 @@ namespace UpToolLib.Tool
|
|||||||
{
|
{
|
||||||
XElement data = XDocument.Load(PathTool.GetInfoPath(tmp)).Element("app");
|
XElement data = XDocument.Load(PathTool.GetInfoPath(tmp)).Element("app");
|
||||||
GlobalVariables.Apps.Add(tmp,
|
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,
|
GlobalVariables.MinimumVer, "", true, "", tmp, Color.Red,
|
||||||
ExternalFunctionalityManager.Instance.GetDefaultIcon(),
|
ExternalFunctionalityManager.Instance.GetDefaultIcon(),
|
||||||
data.Element("MainFile") != null,
|
data.Element("MainFile") != null,
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netstandard2.1</TargetFramework>
|
<TargetFramework>netstandard2.1</TargetFramework>
|
||||||
|
<Deterministic>false</Deterministic>
|
||||||
|
<AssemblyVersion>1.0.*</AssemblyVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<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