From 5558735679e71c6c4d911dc8ec3217a5fb74d805 Mon Sep 17 00:00:00 2001 From: Deadpikle Date: Fri, 16 Aug 2024 18:10:59 +0900 Subject: [PATCH] Add UpdateDetectedAsync See #571 --- UPGRADING.md | 1 + src/NetSparkle/NetSparkleDelegates.cs | 11 ++++++++++- src/NetSparkle/SparkleUpdater.cs | 24 +++++++++++++++++++----- src/NetSparkle/SparkleUpdaterEvents.cs | 16 +++++++++++++++- 4 files changed, 45 insertions(+), 7 deletions(-) diff --git a/UPGRADING.md b/UPGRADING.md index 065c1512..17b1bf51 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -94,6 +94,7 @@ * `AppCast? SparkleUpdater.AppCastCache` holds the most recently deserialized app cast information. * `AppCastItem` has a new `Channel` property. Use it along with `ChannelAppCastFilter` if you want to use channels that way instead of via your `` property. In the app cast generator, use the `--channel` option to set this (note that using this will set ALL items added to the app cast to that specific channel; to add only new items to your app cast instead of rewriting the whole thing, use the `--reparse-existing` option). * App cast generator has a new `--use-ed25519-signature-attribute` to use `edSignature` in its XML output instead of `signature` (json output unaffected) to match the original Sparkle library +* Added `UpdateDetectedAsync` -- prioritized over `UpdateDetected` if both `UpdateDetectedAsync` and `UpdateDetected` are implemented. ## Updating from 0.X or 1.X to 2.X diff --git a/src/NetSparkle/NetSparkleDelegates.cs b/src/NetSparkle/NetSparkleDelegates.cs index 3518e279..4e66c563 100644 --- a/src/NetSparkle/NetSparkleDelegates.cs +++ b/src/NetSparkle/NetSparkleDelegates.cs @@ -22,12 +22,21 @@ namespace NetSparkleUpdater public delegate void LoopFinishedOperation(object sender, bool updateRequired); /// - /// An update was detected for the user's currently running software + /// An update was detected for the user's currently running software. + /// If both this and UpdateDetectedAsync are implemented, UpdateDetectedAsync is prioritized. /// /// the object that initiated the call /// Information about the update that was detected public delegate void UpdateDetected(object sender, UpdateDetectedEventArgs e); + /// + /// An update was detected for the user's currently running software. + /// If both this and UpdateDetected are implemented, UpdateDetectedAsync is prioritized. + /// + /// the object that initiated the call + /// Information about the update that was detected + public delegate Task UpdateDetectedAsync(object sender, UpdateDetectedEventArgs e); + /// /// has started checking for updates /// diff --git a/src/NetSparkle/SparkleUpdater.cs b/src/NetSparkle/SparkleUpdater.cs index ac93ee62..863d8b9e 100644 --- a/src/NetSparkle/SparkleUpdater.cs +++ b/src/NetSparkle/SparkleUpdater.cs @@ -1801,9 +1801,16 @@ private async Task CheckForUpdates(bool isUserManuallyCheckingForUpd // UpdateDetected allows for catching and overriding the update handling, // so if the user has implemented it, tell them there is an update and stop // handling everything. - if (UpdateDetected != null) + if (UpdateDetected != null || UpdateDetectedAsync != null) { - UpdateDetected(this, ev); // event's next action can change, here + if (UpdateDetectedAsync != null) + { + await UpdateDetectedAsync(this, ev); // event's next action can change, here + } + else if (UpdateDetected != null) + { + UpdateDetected(this, ev); // event's next action can change, here + } switch (ev.NextAction) { case NextUpdateAction.PerformUpdateUnattended: @@ -1977,12 +1984,19 @@ private async void OnWorkerDoWork() updates[0], updates ); - if (UpdateDetected != null) + if (UpdateDetected != null || UpdateDetectedAsync != null) { var re = new AutoResetEvent(false); - _syncContextForWorkerLoop.Post((state) => + _syncContextForWorkerLoop.Post(async (state) => { - UpdateDetected?.Invoke(this, ev); + if (UpdateDetectedAsync != null) + { + await UpdateDetectedAsync(this, ev); + } + else if (UpdateDetected != null) + { + UpdateDetected.Invoke(this, ev); + } re.Set(); }, null); re.WaitOne(); // wait for UpdateDetected to finish even though we called back to UI thread diff --git a/src/NetSparkle/SparkleUpdaterEvents.cs b/src/NetSparkle/SparkleUpdaterEvents.cs index 609291a0..47a898e2 100644 --- a/src/NetSparkle/SparkleUpdaterEvents.cs +++ b/src/NetSparkle/SparkleUpdaterEvents.cs @@ -1,6 +1,7 @@ using System; using System.ComponentModel; using System.Diagnostics; +using NetSparkleUpdater.Events; namespace NetSparkleUpdater { @@ -21,10 +22,23 @@ public partial class SparkleUpdater : IDisposable public event UpdateCheckStarted? UpdateCheckStarted; /// /// This event can be used to override the standard user interface - /// process when an update is detected + /// process when an update is detected. To modify the next action, + /// change the value of the + /// property. + /// If both this and UpdateDetectedAsync are implemented, UpdateDetectedAsync is + /// used, and UpdateDetected is not called. /// public event UpdateDetected? UpdateDetected; /// + /// This event can be used to override the standard user interface + /// process when an update is detected. To modify the next action, + /// change the value of the + /// property. + /// If both this and UpdateDetected are implemented, UpdateDetectedAsync is + /// used, and UpdateDetected is not called. + /// + public event UpdateDetectedAsync? UpdateDetectedAsync; + /// /// Called when update check is all done. may have been /// called between the start and end of the update check. ///