From 69936d55b6f6e08abfb17029d3edbf458e85924a Mon Sep 17 00:00:00 2001 From: JustArchi Date: Wed, 24 Jun 2020 20:54:41 +0200 Subject: [PATCH] Add GET /Api/Plugins endpoint, In regards to https://github.com/JustArchiNET/ASF-ui/issues/1015 --- .../ExamplePlugin.cs | 5 +++ .../SteamTokenDumperPlugin.cs | 4 ++ .../IPC/Controllers/Api/PluginsController.cs | 39 +++++++++++++++++++ ArchiSteamFarm/Plugins/IPlugin.cs | 3 ++ ArchiSteamFarm/Plugins/PluginsCore.cs | 6 +-- 5 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 ArchiSteamFarm/IPC/Controllers/Api/PluginsController.cs diff --git a/ArchiSteamFarm.CustomPlugins.ExamplePlugin/ExamplePlugin.cs b/ArchiSteamFarm.CustomPlugins.ExamplePlugin/ExamplePlugin.cs index fe8d31b70..79c03e48e 100644 --- a/ArchiSteamFarm.CustomPlugins.ExamplePlugin/ExamplePlugin.cs +++ b/ArchiSteamFarm.CustomPlugins.ExamplePlugin/ExamplePlugin.cs @@ -26,6 +26,7 @@ using System.Linq; using System.Threading.Tasks; using ArchiSteamFarm.Json; using ArchiSteamFarm.Plugins; +using Newtonsoft.Json; using Newtonsoft.Json.Linq; using SteamKit2; @@ -38,6 +39,10 @@ namespace ArchiSteamFarm.CustomPlugins.ExamplePlugin { // This will keep your code compact, efficient and less dependent. You can always add additional interfaces when you'll need them, this example project will inherit quite a bit of them to show you potential usage // ReSharper disable once UnusedType.Global - this is example plugin class that isn't used in our main code internal sealed class ExamplePlugin : IASF, IBot, IBotCommand, IBotConnection, IBotFriendRequest, IBotMessage, IBotModules, IBotTradeOffer { + // Plugins can expose custom properties for our GET /Api/Plugins API call, simply annotate them with [JsonProperty] (or keep public) + [JsonProperty] + public readonly bool CustomIsEnabledField = true; + // This is used for identification purposes, typically you want to use a friendly name of your plugin here, such as the name of your main class // Please note that this property can have direct dependencies only on structures that were initialized by the constructor, as it's possible to be called before OnLoaded() takes place public string Name => nameof(ExamplePlugin); diff --git a/ArchiSteamFarm.OfficialPlugins.SteamTokenDumper/SteamTokenDumperPlugin.cs b/ArchiSteamFarm.OfficialPlugins.SteamTokenDumper/SteamTokenDumperPlugin.cs index 420adf3cd..d446ef758 100644 --- a/ArchiSteamFarm.OfficialPlugins.SteamTokenDumper/SteamTokenDumperPlugin.cs +++ b/ArchiSteamFarm.OfficialPlugins.SteamTokenDumper/SteamTokenDumperPlugin.cs @@ -30,6 +30,7 @@ using System.Threading.Tasks; using ArchiSteamFarm.Localization; using ArchiSteamFarm.Plugins; using JetBrains.Annotations; +using Newtonsoft.Json; using Newtonsoft.Json.Linq; using SteamKit2; @@ -44,10 +45,13 @@ namespace ArchiSteamFarm.OfficialPlugins.SteamTokenDumper { private static GlobalCache GlobalCache; + [JsonProperty] private static bool IsEnabled; + [JsonProperty] public override string Name => nameof(SteamTokenDumperPlugin); + [JsonProperty] public override Version Version => typeof(SteamTokenDumperPlugin).Assembly.GetName().Version ?? throw new ArgumentNullException(nameof(Version)); public Task GetPreferredChangeNumberToStartFrom() => Task.FromResult(IsEnabled ? GlobalCache?.LastChangeNumber ?? 0 : 0); diff --git a/ArchiSteamFarm/IPC/Controllers/Api/PluginsController.cs b/ArchiSteamFarm/IPC/Controllers/Api/PluginsController.cs new file mode 100644 index 000000000..6504fb834 --- /dev/null +++ b/ArchiSteamFarm/IPC/Controllers/Api/PluginsController.cs @@ -0,0 +1,39 @@ +// _ _ _ ____ _ _____ +// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___ +// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \ +// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | | +// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_| +// | +// Copyright 2015-2020 Ɓ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.Collections.Generic; +using System.Net; +using ArchiSteamFarm.IPC.Responses; +using ArchiSteamFarm.Plugins; +using Microsoft.AspNetCore.Mvc; + +namespace ArchiSteamFarm.IPC.Controllers.Api { + [Route("Api/Plugins")] + public sealed class PluginsController : ArchiController { + [HttpGet] + [ProducesResponseType(typeof(GenericResponse>), (int) HttpStatusCode.OK)] + public ActionResult>> PluginsGet() { + IReadOnlyCollection activePlugins = PluginsCore.ActivePlugins ?? (IReadOnlyCollection) new IPlugin[0]; + + return Ok(new GenericResponse>(activePlugins)); + } + } +} diff --git a/ArchiSteamFarm/Plugins/IPlugin.cs b/ArchiSteamFarm/Plugins/IPlugin.cs index 9acca06c2..2b83f5476 100644 --- a/ArchiSteamFarm/Plugins/IPlugin.cs +++ b/ArchiSteamFarm/Plugins/IPlugin.cs @@ -21,6 +21,7 @@ using System; using JetBrains.Annotations; +using Newtonsoft.Json; namespace ArchiSteamFarm.Plugins { [PublicAPI] @@ -29,6 +30,7 @@ namespace ArchiSteamFarm.Plugins { /// ASF will use this property as general plugin identifier for the user. /// /// String that will be used as the name of this plugin. + [JsonProperty] [NotNull] string Name { get; } @@ -37,6 +39,7 @@ namespace ArchiSteamFarm.Plugins { /// You have a freedom in deciding what versioning you want to use, this is for identification purposes only. /// /// Version that will be shown to the user when plugin is loaded. + [JsonProperty] [NotNull] Version Version { get; } diff --git a/ArchiSteamFarm/Plugins/PluginsCore.cs b/ArchiSteamFarm/Plugins/PluginsCore.cs index 6f51f1b7a..1869a7b76 100644 --- a/ArchiSteamFarm/Plugins/PluginsCore.cs +++ b/ArchiSteamFarm/Plugins/PluginsCore.cs @@ -39,10 +39,10 @@ namespace ArchiSteamFarm.Plugins { internal static class PluginsCore { internal static bool HasCustomPluginsLoaded => HasActivePluginsLoaded && ActivePlugins.Any(plugin => !(plugin is OfficialPlugin officialPlugin) || !officialPlugin.HasSameVersion()); - private static bool HasActivePluginsLoaded => ActivePlugins?.Count > 0; - [ImportMany] - private static ImmutableHashSet ActivePlugins { get; set; } + internal static ImmutableHashSet ActivePlugins { get; private set; } + + private static bool HasActivePluginsLoaded => ActivePlugins?.Count > 0; [ItemNotNull] internal static async Task GetBotsComparer() {