using System; using System.CommandLine; using System.CommandLine.Invocation; using System.IO; using System.IO.Compression; using System.Net; using System.Security.Cryptography; using System.Threading; using UpToolLib; using UpToolLib.v1.Tool; using UpToolLib.v2; namespace Installer { public static class Program { public static bool Basic; private static UpToolLibMain _lib; public static int Main(string[] args) { Thread.Sleep(2000); try { Basic = args.Length > 0 && args[0].ToLower() == "--basic"; _lib = new UpToolLibMain(new UtLibFunctions()); RootCommand rootCommand = new(); rootCommand.AddGlobalOption(new Option("--basic", "Use only basic console functionality. Must be the first parameter in the call")); Command install = new("install", "Install UpTool") { new Option(new[] {"--noPrep", "-p"}, "Doesn't initialize repos. Use with caution!") }; install.AddAlias("-i"); install.AddAlias("i"); install.Handler = CommandHandler.Create(Install); rootCommand.AddCommand(install); return rootCommand.InvokeAsync(args).Result; } catch (Exception e) { Console.WriteLine($"FAILED: {e}"); return 1; } finally { _lib?.Dispose(); } } private static void Install(bool noPrep) { WebClient client = new(); Console.WriteLine("Downloading metadata"); UpdateCheck check = _lib.V2.UpdateChecker.Check( "https://gitlab.com/JFronny/UpTool2/-/jobs/artifacts/master/raw/meta.xml?job=uptool"); PathTool pathTool = _lib.V1.PathTool; Console.WriteLine("Downloading binary"); byte[] dl = client.DownloadData(check.App); Console.WriteLine("Verifying integrity"); using (SHA256CryptoServiceProvider sha256 = new()) { string pkgHash = BitConverter.ToString(sha256.ComputeHash(dl)).Replace("-", string.Empty) .ToUpper(); if (pkgHash != check.AppHash) throw new Exception($@"The hash is not equal to the one stored in the repo: Package: {pkgHash} Online: {check.AppHash}"); } Console.WriteLine("Extracting"); if (Directory.Exists(pathTool.GetRelative("Install"))) { foreach (string file in Directory.GetFiles(pathTool.GetRelative("Install"))) File.Delete(file); foreach (string dir in Directory.GetDirectories(pathTool.GetRelative("Install"))) if (Path.GetFileName(dir) != "tmp") Directory.Delete(dir, true); } Directory.CreateDirectory(pathTool.GetRelative("Install")); using (MemoryStream ms = new(dl)) { using ZipArchive ar = new(ms); ar.ExtractToDirectory(pathTool.GetRelative("Install"), true); } if (noPrep) return; Console.WriteLine("Preparing Repos"); _lib.V1.XmlTool.FixXml(); _lib.V2.RepoManagement.FetchRepos(); } } }