From b1bc4eadadc0f5c19f13abfebcd341b02ebb7428 Mon Sep 17 00:00:00 2001 From: miku-666 <74728189+NessieHax@users.noreply.github.com> Date: Thu, 15 Aug 2024 16:57:27 +0200 Subject: [PATCH] Program - Move Update related initialization into Updater class --- PCK-Studio/Internal/Json/UpdateInformation.cs | 24 +++++++ PCK-Studio/Internal/Updater.cs | 62 +++++++++++++++++++ PCK-Studio/MainForm.cs | 2 +- PCK-Studio/PckStudio.csproj | 2 + PCK-Studio/Program.cs | 60 +----------------- 5 files changed, 91 insertions(+), 59 deletions(-) create mode 100644 PCK-Studio/Internal/Json/UpdateInformation.cs create mode 100644 PCK-Studio/Internal/Updater.cs diff --git a/PCK-Studio/Internal/Json/UpdateInformation.cs b/PCK-Studio/Internal/Json/UpdateInformation.cs new file mode 100644 index 00000000..c6d1fd30 --- /dev/null +++ b/PCK-Studio/Internal/Json/UpdateInformation.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json; + +namespace PckStudio.Internal.Json +{ + internal class UpdateInformation + { + [JsonProperty("version")] + public string Version { get; set; } + + [JsonProperty("url")] + public string Url { get; set; } + + [JsonProperty("changelog")] + public string Changelog { get; set; } + + [JsonProperty("mandatory")] + public bool Mandatory { get; set; } + } +} \ No newline at end of file diff --git a/PCK-Studio/Internal/Updater.cs b/PCK-Studio/Internal/Updater.cs new file mode 100644 index 00000000..84a1b86a --- /dev/null +++ b/PCK-Studio/Internal/Updater.cs @@ -0,0 +1,62 @@ +using System; +using System.IO; +using System.Windows.Forms; +using AutoUpdaterDotNET; +using Newtonsoft.Json; +using PckStudio.Internal.Json; +using PckStudio.Properties; + +namespace PckStudio.Internal +{ + internal static class Updater + { + private static Uri _appCast; + + internal static void Initialize(Uri appCast) + { + _appCast = appCast; + //AutoUpdater.ClearAppDirectory = true; +#if DEBUG + AutoUpdater.ReportErrors = true; +#endif + AutoUpdater.DownloadPath = Application.StartupPath; + AutoUpdater.ExecutablePath = "./PCK-Studio.exe"; + AutoUpdater.TopMost = true; + + string jsonPath = Path.Combine(Environment.CurrentDirectory, "updates.json"); + AutoUpdater.PersistenceProvider = new JsonFilePersistenceProvider(jsonPath); + AutoUpdater.ParseUpdateInfoEvent += AutoUpdaterOnParseUpdateInfoEvent; + AutoUpdater.Icon = Resources.ProjectLogo.ToBitmap(); + + if (Settings.Default.AutoUpdate) + { + UpdateToLatest(); + } + } + + internal static void SetOwner(Form owner) => AutoUpdater.SetOwner(owner); + + internal static void UpdateToLatest() + { +#if NDEBUG + string url = $"{_appCast}/main/Version.json"; + AutoUpdater.Start(url); +#endif + } + + private static void AutoUpdaterOnParseUpdateInfoEvent(ParseUpdateInfoEventArgs args) + { + UpdateInformation json = JsonConvert.DeserializeObject(args.RemoteData); + args.UpdateInfo = new UpdateInfoEventArgs + { + CurrentVersion = json.Version, + DownloadURL = json.Url, + ChangelogURL = json.Changelog, + Mandatory = new Mandatory() + { + Value = json.Mandatory, + } + }; + } + } +} diff --git a/PCK-Studio/MainForm.cs b/PCK-Studio/MainForm.cs index 9c64a624..92f9182d 100644 --- a/PCK-Studio/MainForm.cs +++ b/PCK-Studio/MainForm.cs @@ -2251,7 +2251,7 @@ namespace PckStudio private void checkForUpdatesToolStripMenuItem_Click(object sender, EventArgs e) { - Program.UpdateToLatest(); + Updater.UpdateToLatest(); } private void exitToolStripMenuItem_Click(object sender, EventArgs e) diff --git a/PCK-Studio/PckStudio.csproj b/PCK-Studio/PckStudio.csproj index 0c7fe072..5128d99f 100644 --- a/PCK-Studio/PckStudio.csproj +++ b/PCK-Studio/PckStudio.csproj @@ -157,6 +157,7 @@ + @@ -169,6 +170,7 @@ + True True diff --git a/PCK-Studio/Program.cs b/PCK-Studio/Program.cs index e26bddc9..da41cac4 100644 --- a/PCK-Studio/Program.cs +++ b/PCK-Studio/Program.cs @@ -31,74 +31,18 @@ namespace PckStudio [STAThread] static void Main(string[] args) { - AutoUpdater.SetOwner(MainInstance); - //AutoUpdater.ClearAppDirectory = true; -#if DEBUG - AutoUpdater.ReportErrors = true; -#endif - AutoUpdater.DownloadPath = Application.StartupPath; - AutoUpdater.ExecutablePath = "./PCK-Studio.exe"; - AutoUpdater.TopMost = true; - - string jsonPath = Path.Combine(Environment.CurrentDirectory, "updates.json"); - AutoUpdater.PersistenceProvider = new JsonFilePersistenceProvider(jsonPath); - AutoUpdater.ParseUpdateInfoEvent += AutoUpdaterOnParseUpdateInfoEvent; - AutoUpdater.Icon = Resources.ProjectLogo.ToBitmap(); - - if (Settings.Default.AutoUpdate) - { - UpdateToLatest(); - } + Updater.Initialize(RawProjectUrl); ApplicationScope.Initialize(); Trace.TraceInformation("Startup"); RPC.Initialize(); MainInstance = new MainForm(); + Updater.SetOwner(MainInstance); if (args.Length > 0 && File.Exists(args[0]) && args[0].EndsWith(".pck")) MainInstance.InitPckFromFile(args[0]); Application.ApplicationExit += (sender, e) => { RPC.Deinitialize(); }; MainInstance.FocusMe(); Application.Run(MainInstance); } - - - internal static void UpdateToLatest() - { -#if NDEBUG - string url = $"{RawProjectUrl}/main/Version.json"; - AutoUpdater.Start(url); -#endif - } - - class UpdateInfo - { - [JsonProperty("version")] - public string Version { get; set; } - - [JsonProperty("url")] - public string Url { get; set; } - - [JsonProperty("changelog")] - public string Changelog { get; set; } - - [JsonProperty("mandatory")] - public bool Mandatory { get; set; } - } - - private static void AutoUpdaterOnParseUpdateInfoEvent(ParseUpdateInfoEventArgs args) - { - UpdateInfo json = JsonConvert.DeserializeObject(args.RemoteData); - args.UpdateInfo = new UpdateInfoEventArgs - { - CurrentVersion = json.Version, - DownloadURL = json.Url, - ChangelogURL = json.Changelog, - Mandatory = new Mandatory() - { - Value = json.Mandatory, - } - }; - } - } } \ No newline at end of file