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/UpTool2/Program.cs

219 lines
8.6 KiB
C#
Raw Permalink Normal View History

using System;
2019-09-09 16:53:11 +02:00
using System.Diagnostics;
2020-02-28 12:59:59 +01:00
using System.Drawing;
2019-09-09 16:53:11 +02:00
using System.IO;
2020-02-28 12:59:59 +01:00
using System.IO.Compression;
2019-09-09 16:53:11 +02:00
using System.Reflection;
using System.Security.Cryptography;
2019-09-08 21:39:04 +02:00
using System.Windows.Forms;
2019-11-09 22:52:10 +01:00
using System.Xml;
2020-03-24 22:43:48 +01:00
using UpTool2.Tool;
2020-03-24 20:53:23 +01:00
using UpToolLib;
using UpToolLib.v1.Tool;
using UpToolLib.v2;
2019-09-08 21:39:04 +02:00
namespace UpTool2
{
2020-02-28 12:59:59 +01:00
internal static class Program
2019-09-08 21:39:04 +02:00
{
2021-02-03 22:14:47 +01:00
public static Form? Splash;
private static int _splashProgress;
2021-02-03 22:14:47 +01:00
private static string? _splashMessage;
2020-02-28 14:26:16 +01:00
public static bool Online;
public static UpToolLibMain Lib;
2020-02-28 12:59:59 +01:00
2019-09-08 21:39:04 +02:00
[STAThread]
2020-06-21 16:46:34 +02:00
private static void Main(string[] args)
2019-09-08 21:39:04 +02:00
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
2020-03-20 12:35:46 +01:00
BuildSplash();
2021-02-03 22:14:47 +01:00
Splash.Show();
2020-03-24 22:43:48 +01:00
try
{
Lib = new UpToolLibMain(new UtLibFunctions());
2020-03-24 22:43:48 +01:00
}
catch (MutexLockLockedException)
{
Console.WriteLine("Mutex property of other process, quitting");
Process[] processes = Process.GetProcessesByName("UpTool2");
if (processes.Length > 0)
WindowHelper.BringProcessToFront(Process.GetProcessesByName("UpTool2")[0]);
return;
}
2019-09-09 17:05:53 +02:00
#if !DEBUG
try
{
2019-09-09 17:05:53 +02:00
#endif
2020-03-20 12:35:46 +01:00
SetSplash(1, "Initializing paths");
if (!Directory.Exists(Lib.V1.PathTool.Dir))
Directory.CreateDirectory(Lib.V1.PathTool.Dir);
2020-03-16 15:53:14 +01:00
FixXml();
2020-03-20 12:35:46 +01:00
SetSplash(2, "Performing checks");
try
{
Lib.V2.UpdateChecker.Check();
Online = true;
}
catch
{
Online = false;
}
if (!Directory.Exists(Lib.V1.PathTool.GetRelative("Apps")))
Directory.CreateDirectory(Lib.V1.PathTool.GetRelative("Apps"));
2020-03-20 12:35:46 +01:00
if (!Online)
SetSplash(7, "Opening");
if (!Online || UpdateCheck())
2020-03-16 13:42:37 +01:00
Application.Run(new MainForm());
2019-09-09 17:05:53 +02:00
#if !DEBUG
}
catch (Exception e1)
{
2020-03-20 12:35:46 +01:00
try
{
2020-03-24 22:43:48 +01:00
Splash.Invoke((Action) Splash.Hide);
2020-03-20 12:35:46 +01:00
}
catch
{
Console.WriteLine("Failed to hide splash");
}
MessageBox.Show(e1.ToString());
}
finally
{
Lib.Dispose();
}
2019-09-09 17:05:53 +02:00
#endif
2019-09-08 21:39:04 +02:00
}
2019-10-21 16:12:07 +02:00
2020-03-20 12:35:46 +01:00
private static void BuildSplash()
2019-11-09 22:52:10 +01:00
{
2020-02-28 14:26:16 +01:00
Splash = new Form
2019-11-09 22:52:10 +01:00
{
StartPosition = FormStartPosition.CenterScreen,
FormBorderStyle = FormBorderStyle.None,
ControlBox = false,
MaximizeBox = false,
MinimizeBox = false,
ShowIcon = false,
ShowInTaskbar = false,
Size = new Size(700, 400),
2020-03-20 12:35:46 +01:00
BackColor = Color.Black
2019-11-09 22:52:10 +01:00
};
2020-02-28 14:26:16 +01:00
Splash.MaximumSize = Splash.Size;
Splash.MinimumSize = Splash.Size;
2020-03-20 12:35:46 +01:00
Splash.Paint += (sender, e) =>
{
Graphics g = e.Graphics;
//Draw background
2020-03-24 22:43:48 +01:00
Brush[] brushes =
{Brushes.Purple, Brushes.MediumPurple, Brushes.Indigo, Brushes.Fuchsia, Brushes.OrangeRed};
2020-03-20 12:35:46 +01:00
const int barWidth = 50;
const int topOffset = 100;
for (int i = 0; i < Splash.Width + topOffset; i += barWidth)
g.FillPolygon(brushes[i / barWidth % brushes.Length], new[]
2020-03-20 12:35:46 +01:00
{
new PointF(i, 0),
new PointF(i + barWidth, 0),
new PointF(i, Splash.Height),
new PointF(i - topOffset, Splash.Height)
});
//Draw Text: UpTool2 (by JFronny)^
Font font = new Font(FontFamily.GenericSansSerif, 40, FontStyle.Bold);
const string text = "UpTool2";
SizeF size = g.MeasureString(text, font);
RectangleF rect = new RectangleF(Splash.Width / 2f - size.Width / 2,
Splash.Height / 2f - size.Height / 2, size.Width, size.Height);
2020-03-20 12:35:46 +01:00
g.DrawString(text, font, Brushes.White, rect);
Font smallFont = new Font(FontFamily.GenericSansSerif, 10);
const string subtitle = "by JFronny";
SizeF size2 = g.MeasureString(subtitle, smallFont);
2020-03-24 22:43:48 +01:00
g.DrawString(subtitle, smallFont, Brushes.White,
new RectangleF(rect.Right - size2.Width, rect.Bottom - size2.Height, size2.Width, size2.Height));
2020-03-20 12:35:46 +01:00
//Draw progress bar
Rectangle bar = new Rectangle(3 * Splash.Width / 8, Splash.Height * 3 / 4 - 10, Splash.Width / 4,
2020-03-24 22:43:48 +01:00
20);
2020-03-20 12:35:46 +01:00
g.FillRectangle(Brushes.Gray, bar);
2020-03-24 22:43:48 +01:00
g.FillRectangle(Brushes.Black,
new Rectangle(bar.X, bar.Y, bar.Width * _splashProgress / 10, bar.Height));
2020-03-20 12:35:46 +01:00
g.DrawRectangle(Pens.DimGray, bar);
//g.DrawString(SplashMessage, smallFont, Brushes.White, new PointF(bar.Left, bar.Bottom));
g.DrawString(_splashMessage, smallFont, Brushes.White, bar,
2020-03-24 22:43:48 +01:00
new StringFormat {Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Center});
2020-03-20 12:35:46 +01:00
};
Splash.Load += (sender, e) => Splash.BringToFront();
Splash.FormClosed += (sender, e) => Splash.Dispose();
2019-11-09 22:52:10 +01:00
}
2020-03-20 12:35:46 +01:00
public static void SetSplash(int progress, string status) => Splash.Invoke(new Action(() =>
{
2020-06-21 16:46:34 +02:00
Console.WriteLine($"{progress} - {status}");
Debug.WriteLine($"{progress} - {status}");
_splashProgress = progress;
_splashMessage = status;
2020-03-20 12:35:46 +01:00
Splash.Invoke((Action) Splash.Invalidate);
}));
2020-02-28 14:26:16 +01:00
public static void FixXml(bool throwOnError = false)
2019-11-09 22:52:10 +01:00
{
try
{
Lib.V1.XmlTool.FixXml();
2019-11-09 22:52:10 +01:00
}
catch (XmlException)
{
2020-02-28 14:26:16 +01:00
if (throwOnError) throw;
MessageBox.Show("Something went wrong while trying to parse XML. Retrying...");
File.Delete(Lib.V1.PathTool.InfoXml);
2020-02-28 14:26:16 +01:00
FixXml();
2019-11-09 22:52:10 +01:00
}
}
private static bool UpdateCheck()
2019-11-09 22:52:10 +01:00
{
UpdateCheck check = Lib.V2.UpdateChecker.Check();
2020-03-20 12:35:46 +01:00
SetSplash(3, "Comparing online version");
if (Assembly.GetExecutingAssembly().GetName().Version >= check.OnlineVersion)
2020-06-21 16:46:34 +02:00
{
SetSplash(7, "Opening");
2020-02-28 14:26:16 +01:00
return true;
2020-06-21 16:46:34 +02:00
}
byte[] dl;
2020-03-20 12:35:46 +01:00
SetSplash(4, "Downloading latest");
using (DownloadDialog dlg = new DownloadDialog(check.Installer.AbsoluteUri))
{
if (dlg.ShowDialog() != DialogResult.OK)
throw new Exception("Failed to update");
2020-02-28 14:26:16 +01:00
dl = dlg.Result;
}
2020-03-20 12:35:46 +01:00
SetSplash(8, "Verifying");
using (SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider())
{
2020-02-28 14:26:16 +01:00
string pkgHash = BitConverter.ToString(sha256.ComputeHash(dl)).Replace("-", string.Empty).ToUpper();
if (pkgHash != check.InstallerHash)
throw new Exception(
$"The hash is not equal to the one stored in the repo:\r\nPackage: {pkgHash}\r\nOnline: {check.InstallerHash}");
}
2020-03-20 12:35:46 +01:00
SetSplash(9, "Installing");
if (Directory.Exists(Lib.V1.PathTool.GetRelative("Install", "tmp")))
Directory.Delete(Lib.V1.PathTool.GetRelative("Install", "tmp"), true);
Directory.CreateDirectory(Lib.V1.PathTool.GetRelative("Install", "tmp"));
using (MemoryStream ms = new MemoryStream(dl))
{
using ZipArchive ar = new ZipArchive(ms);
ar.ExtractToDirectory(Lib.V1.PathTool.GetRelative("Install", "tmp"), true);
}
2020-02-28 14:26:16 +01:00
Splash.Hide();
Process.Start(new ProcessStartInfo
{
FileName = Lib.V1.PathTool.GetRelative("Install", "tmp", "Installer.exe"),
2020-04-09 13:58:06 +02:00
Arguments = "i -p",
2020-03-16 15:53:14 +01:00
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
WorkingDirectory = Lib.V1.PathTool.GetRelative("Install")
2020-02-28 14:26:16 +01:00
});
2020-06-21 16:46:34 +02:00
SetSplash(7, "Installing");
2020-03-16 15:53:14 +01:00
return false;
}
2019-09-08 21:39:04 +02:00
}
2020-06-21 16:46:34 +02:00
}