Add IUpdateAware plugin interface

This commit is contained in:
Archi 2021-12-08 19:48:59 +01:00
parent 0f489f55e4
commit 5d33bca611
No known key found for this signature in database
GPG key ID: 6B138B4C64555AEA
3 changed files with 79 additions and 0 deletions

View file

@ -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();

View file

@ -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 {
/// <summary>
/// ASF will call this method after update to a particular ASF version has been finished, just before restart of the process.
/// </summary>
/// <param name="currentVersion">The current (old) version of ASF program.</param>
/// <param name="newVersion">The target (new) version of ASF program.</param>
Task OnUpdateFinished(Version currentVersion, Version newVersion);
/// <summary>
/// ASF will call this method before proceeding with an update to a particular ASF version.
/// </summary>
/// <param name="currentVersion">The current (old) version of ASF program.</param>
/// <param name="newVersion">The target (new) version of ASF program.</param>
Task OnUpdateProceeding(Version currentVersion, Version newVersion);
}

View file

@ -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<IUpdateAware>().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<IUpdateAware>().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<Assembly>? LoadAssembliesFrom(string path) {
if (string.IsNullOrEmpty(path)) {