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

84 lines
3.7 KiB
C#
Raw Normal View History

2020-03-16 15:53:14 +01:00
using System;
2020-03-24 23:14:40 +01:00
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
2020-04-08 18:15:21 +02:00
using System.Threading;
2020-03-16 15:53:14 +01:00
using System.Windows.Forms;
2020-04-08 17:08:51 +02:00
using UpToolLib;
using UpToolLib.Tool;
2020-03-16 15:53:14 +01:00
namespace Installer
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
2020-03-24 23:14:40 +01:00
private static void Main(string[] args)
2020-03-16 15:53:14 +01:00
{
2020-03-24 22:43:48 +01:00
try
{
2020-03-24 23:14:40 +01:00
if (!args.Any(s => new[] {"install", "i"}.Contains(s.TrimStart('-', '/').ToLower())))
{
2020-04-08 18:15:21 +02:00
MutexLock.Lock();
2020-03-24 23:14:40 +01:00
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new InstallerForm());
}
else
{
2020-04-08 18:15:21 +02:00
Thread.Sleep(2000);
MutexLock.Lock();
2020-04-08 17:08:51 +02:00
ExternalFunctionalityManager.Init(new UtLibFunctionsCli());
2020-03-24 23:14:40 +01:00
WebClient client = new WebClient();
Console.WriteLine("Downloading metadata");
UpdateCheck.Reload("https://github.com/JFronny/UpTool2/releases/latest/download/meta.xml");
2020-03-24 23:14:40 +01:00
Console.WriteLine("Downloading binary");
byte[] dl = client.DownloadData(UpdateCheck.App);
2020-03-24 23:14:40 +01:00
Console.WriteLine("Verifying integrity");
using (SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider())
{
string pkgHash = BitConverter.ToString(sha256.ComputeHash(dl)).Replace("-", string.Empty)
.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}");
2020-03-24 23:14:40 +01:00
}
Console.WriteLine("Extracting");
if (Directory.Exists(PathTool.GetRelative("Install")))
2020-04-08 18:03:50 +02:00
{
foreach (string file in Directory.GetFiles(PathTool.GetRelative("Install"))) File.Delete(file);
foreach (string dir in Directory.GetDirectories(PathTool.GetRelative("Install")))
if (System.IO.Path.GetFileName(dir) != "tmp")
Directory.Delete(dir, true);
}
2020-03-24 23:14:40 +01:00
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"),
System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Programs),
"UpTool2.lnk"));
2020-03-24 23:14:40 +01:00
Console.WriteLine("Creating PATH entry");
if (!Path.Content.Contains(Path.GetName(PathTool.GetRelative("Install"))))
Path.Append(PathTool.GetRelative("Install"));
2020-04-08 17:08:51 +02:00
Console.WriteLine("Preparing Repos");
XmlTool.FixXml();
RepoManagement.FetchRepos();
2020-03-24 23:14:40 +01:00
}
2020-03-24 22:43:48 +01:00
}
finally
{
MutexLock.Unlock();
}
2020-03-16 15:53:14 +01:00
}
}
}