Files
RAGECOOP-V/Client/Scripts/Menus/Sub/UpdateMenu.cs

134 lines
4.4 KiB
C#
Raw Normal View History

2022-10-23 19:02:39 +08:00
using System;
using System.ComponentModel;
2022-07-20 17:50:01 +08:00
using System.Drawing;
2022-07-14 17:59:41 +08:00
using System.IO;
2022-07-20 17:50:01 +08:00
using System.Net;
using System.Threading.Tasks;
2022-10-23 19:02:39 +08:00
using GTA.UI;
using ICSharpCode.SharpZipLib.Zip;
using LemonUI.Menus;
using RageCoop.Client.Loader;
using RageCoop.Client.Scripting;
using RageCoop.Core;
2022-07-14 17:59:41 +08:00
namespace RageCoop.Client.Menus
{
internal class UpdateMenu
{
2022-09-06 21:46:35 +08:00
private static readonly NativeItem _updatingItem = new NativeItem("Updating...");
2022-10-23 19:02:39 +08:00
private static readonly NativeItem _downloadItem =
new NativeItem("Download", "Download and update to latest nightly");
2022-07-14 17:59:41 +08:00
2022-09-06 21:46:35 +08:00
private static readonly string _downloadPath = Path.Combine(Main.Settings.DataDirectory, "RageCoop.Client.zip");
2022-10-23 19:02:39 +08:00
public static NativeMenu Menu =
new NativeMenu("Update", "Update", "Download and install latest nightly build from GitHub")
{
UseMouse = false,
Alignment = Main.Settings.FlipMenu ? Alignment.Right : Alignment.Left
};
2022-07-14 17:59:41 +08:00
static UpdateMenu()
{
Menu.Banner.Color = Color.FromArgb(225, 0, 0, 0);
Menu.Title.Color = Color.FromArgb(255, 165, 0);
2022-09-06 21:46:35 +08:00
Menu.Opening += Opening;
_downloadItem.Activated += StartUpdate;
2022-07-14 17:59:41 +08:00
}
2022-10-23 19:02:39 +08:00
public static bool IsUpdating { get; private set; }
2022-07-14 17:59:41 +08:00
private static void StartUpdate(object sender, EventArgs e)
{
2022-10-15 15:54:46 +08:00
if (CoreUtils.GetLatestVersion() < Main.Version)
{
2022-10-23 19:02:39 +08:00
Notification.Show("Local version is newer than remote version, update can't continue");
2022-10-15 15:54:46 +08:00
return;
}
2022-10-23 19:02:39 +08:00
2022-09-06 21:46:35 +08:00
IsUpdating = true;
2022-07-14 17:59:41 +08:00
Menu.Clear();
Menu.Add(_updatingItem);
Task.Run(() =>
{
try
{
2022-10-23 19:02:39 +08:00
if (File.Exists(_downloadPath)) File.Delete(_downloadPath);
var client = new WebClient();
2022-07-14 17:59:41 +08:00
// TLS only
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls13 | SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
2022-10-23 19:02:39 +08:00
client.DownloadProgressChanged += (s, e1) =>
{
API.QueueAction(() => { _updatingItem.AltTitle = $"{e1.ProgressPercentage}%"; });
};
2022-09-06 21:46:35 +08:00
client.DownloadFileCompleted += (s, e2) => { Install(); };
2022-10-23 19:02:39 +08:00
client.DownloadFileAsync(
new Uri("https://github.com/RAGECOOP/RAGECOOP-V/releases/download/nightly/RageCoop.Client.zip"),
_downloadPath);
2022-07-14 17:59:41 +08:00
}
catch (Exception ex)
{
Main.Logger.Error(ex);
}
});
}
private static void Install()
{
try
{
2022-10-23 19:02:39 +08:00
API.QueueAction(() => { _updatingItem.AltTitle = "Installing..."; });
2022-10-15 15:30:58 +08:00
var insatllPath = @"RageCoop\Scripts";
Directory.CreateDirectory(insatllPath);
foreach (var f in Directory.GetFiles(insatllPath, "*.dll", SearchOption.AllDirectories))
2022-10-23 19:02:39 +08:00
try
{
File.Delete(f);
}
catch
{
}
new FastZip().ExtractZip(_downloadPath, insatllPath, FastZip.Overwrite.Always, null, null, null, true);
try
2022-08-05 17:24:51 +08:00
{
2022-10-23 19:02:39 +08:00
File.Delete(_downloadPath);
2022-08-05 17:24:51 +08:00
}
2022-10-23 19:02:39 +08:00
catch
{
}
try
{
File.Delete(Path.Combine(insatllPath, "RageCoop.Client.Installer.exe"));
}
catch
{
}
LoaderContext.RequestUnload();
2022-10-15 15:30:58 +08:00
IsUpdating = false;
2022-07-14 17:59:41 +08:00
}
catch (Exception ex)
{
Main.Logger.Error(ex);
}
}
2022-10-23 19:02:39 +08:00
private static void Opening(object sender, CancelEventArgs e)
2022-07-14 17:59:41 +08:00
{
Menu.Clear();
if (Networking.IsOnServer)
2022-07-14 18:52:04 +08:00
Menu.Add(new NativeItem("Disconnect from the server first"));
2022-07-14 17:59:41 +08:00
else if (IsUpdating)
Menu.Add(_updatingItem);
else
Menu.Add(_downloadItem);
}
}
2022-10-23 19:02:39 +08:00
}