From 5d33bca611b04c6d95829856b4d6d5bc6478c62f Mon Sep 17 00:00:00 2001 From: Archi Date: Wed, 8 Dec 2021 19:48:59 +0100 Subject: [PATCH] Add IUpdateAware plugin interface --- ArchiSteamFarm/Core/ASF.cs | 4 ++ .../Plugins/Interfaces/IUpdateAware.cs | 43 +++++++++++++++++++ ArchiSteamFarm/Plugins/PluginsCore.cs | 32 ++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 ArchiSteamFarm/Plugins/Interfaces/IUpdateAware.cs diff --git a/ArchiSteamFarm/Core/ASF.cs b/ArchiSteamFarm/Core/ASF.cs index 19a0ed329..a99cccd84 100644 --- a/ArchiSteamFarm/Core/ASF.cs +++ b/ArchiSteamFarm/Core/ASF.cs @@ -315,6 +315,8 @@ public static class ASF { return SharedInfo.Version; } + await PluginsCore.OnUpdateProceeding(newVersion).ConfigureAwait(false); + try { // We disable ArchiKestrel here as the update process moves the core files and might result in IPC crash // TODO: It might fail if the update was triggered from the API, this should be something to improve in the future, by changing the structure into request -> return response -> finish update @@ -343,6 +345,8 @@ public static class ASF { ArchiLogger.LogGenericInfo(Strings.UpdateFinished); + await PluginsCore.OnUpdateFinished(newVersion).ConfigureAwait(false); + return newVersion; } finally { UpdateSemaphore.Release(); diff --git a/ArchiSteamFarm/Plugins/Interfaces/IUpdateAware.cs b/ArchiSteamFarm/Plugins/Interfaces/IUpdateAware.cs new file mode 100644 index 000000000..b8cc6a9e3 --- /dev/null +++ b/ArchiSteamFarm/Plugins/Interfaces/IUpdateAware.cs @@ -0,0 +1,43 @@ +// _ _ _ ____ _ _____ +// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___ +// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \ +// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | | +// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_| +// | +// Copyright 2015-2021 Ɓukasz "JustArchi" Domeradzki +// Contact: JustArchi@JustArchi.net +// | +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// | +// http://www.apache.org/licenses/LICENSE-2.0 +// | +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Threading.Tasks; +using JetBrains.Annotations; + +namespace ArchiSteamFarm.Plugins.Interfaces; + +[PublicAPI] +public interface IUpdateAware : IPlugin { + /// + /// ASF will call this method after update to a particular ASF version has been finished, just before restart of the process. + /// + /// The current (old) version of ASF program. + /// The target (new) version of ASF program. + Task OnUpdateFinished(Version currentVersion, Version newVersion); + + /// + /// ASF will call this method before proceeding with an update to a particular ASF version. + /// + /// The current (old) version of ASF program. + /// The target (new) version of ASF program. + Task OnUpdateProceeding(Version currentVersion, Version newVersion); +} diff --git a/ArchiSteamFarm/Plugins/PluginsCore.cs b/ArchiSteamFarm/Plugins/PluginsCore.cs index 93e7670a1..30b281c41 100644 --- a/ArchiSteamFarm/Plugins/PluginsCore.cs +++ b/ArchiSteamFarm/Plugins/PluginsCore.cs @@ -632,6 +632,38 @@ internal static class PluginsCore { } } + internal static async Task OnUpdateFinished(Version newVersion) { + if (newVersion == null) { + throw new ArgumentNullException(nameof(newVersion)); + } + + if ((ActivePlugins == null) || (ActivePlugins.Count == 0)) { + return; + } + + try { + await Utilities.InParallel(ActivePlugins.OfType().Select(plugin => plugin.OnUpdateFinished(SharedInfo.Version, newVersion))).ConfigureAwait(false); + } catch (Exception e) { + ASF.ArchiLogger.LogGenericException(e); + } + } + + internal static async Task OnUpdateProceeding(Version newVersion) { + if (newVersion == null) { + throw new ArgumentNullException(nameof(newVersion)); + } + + if ((ActivePlugins == null) || (ActivePlugins.Count == 0)) { + return; + } + + try { + await Utilities.InParallel(ActivePlugins.OfType().Select(plugin => plugin.OnUpdateProceeding(SharedInfo.Version, newVersion))).ConfigureAwait(false); + } catch (Exception e) { + ASF.ArchiLogger.LogGenericException(e); + } + } + [UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2026:RequiresUnreferencedCode", Justification = "We don't care about trimmed assemblies, as we need it to work only with the known (used) ones")] private static HashSet? LoadAssembliesFrom(string path) { if (string.IsNullOrEmpty(path)) {