This repository has been archived on 2022-08-05. You can view files and clone it, but cannot push or open issues or pull requests.
UpTool2/Installer/Program.cs
2020-03-24 23:14:40 +01:00

69 lines
3.1 KiB
C#

using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Windows.Forms;
using System.Xml.Linq;
namespace Installer
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
private static void Main(string[] args)
{
MutexLock.Lock();
try
{
if (!args.Any(s => new[] {"install", "i"}.Contains(s.TrimStart('-', '/').ToLower())))
{
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new InstallerForm());
}
else
{
WebClient client = new WebClient();
Console.WriteLine("Downloading metadata");
XElement meta = XDocument.Load("https://github.com/JFronny/UpTool2/releases/latest/download/meta.xml")
.Element("meta");
Console.WriteLine("Downloading binary");
byte[] dl = client.DownloadData(meta.Element("File").Value);
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()}");
}
Console.WriteLine("Extracting");
if (Directory.Exists(PathTool.GetRelative("Install")))
Directory.Delete(PathTool.GetRelative("Install"), true);
Directory.CreateDirectory(PathTool.GetRelative("Install"));
using (MemoryStream ms = new MemoryStream(dl))
{
using ZipArchive ar = new ZipArchive(ms);
ar.ExtractToDirectory(PathTool.GetRelative("Install"), true);
}
Console.WriteLine("Creating shortcut");
Shortcut.Make(PathTool.GetRelative("Install", "UpTool2.exe"),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Programs), "UpTool2.lnk"));
Console.WriteLine("Creating PATH entry");
if (!PATH.Content.Contains(PATH.GetName(PathTool.GetRelative("Install"))))
PATH.Append(PathTool.GetRelative("Install"));
}
}
finally
{
MutexLock.Unlock();
}
}
}
}