Skip to content

Commit

Permalink
Added support for ASFEnhance features
Browse files Browse the repository at this point in the history
  • Loading branch information
Citrinate committed Mar 3, 2024
1 parent d60bcbf commit 3efdd7b
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
57 changes: 57 additions & 0 deletions BoosterManager/AdapterBridge.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Reflection;
using ArchiSteamFarm.Core;

// ASFEnhanced Adapter https://github.com/chr233/ASFEnhanceAdapterDemoPlugin

namespace BoosterManager;
internal static class AdapterBridge
{
/// <summary>
/// 注册子模块
/// </summary>
/// <param name="pluginName">插件名称</param>
/// <param name="pluginId">插件唯一标识符</param>
/// <param name="cmdPrefix">命令前缀</param>
/// <param name="repoName">自动更新仓库</param>
/// <param name="cmdHandler">命令处理函数</param>
/// <returns></returns>
public static bool InitAdapter(string pluginName, string pluginId, string? cmdPrefix, string? repoName, MethodInfo? cmdHandler)
{
try
{
var adapterEndpoint = Assembly.Load("ASFEnhance").GetType("ASFEnhance._Adapter_.Endpoint");
var registerModule = adapterEndpoint?.GetMethod("RegisterModule", BindingFlags.Static | BindingFlags.Public);
var pluinVersion = Assembly.GetExecutingAssembly().GetName().Version;

if (registerModule != null && adapterEndpoint != null)
{
var result = registerModule?.Invoke(null, new object?[] { pluginName, pluginId, cmdPrefix, repoName, pluinVersion, cmdHandler });

if (result is string str)
{
if (str == pluginName)
{
return true;
}
else
{
ASF.ArchiLogger.LogGenericWarning(str);
}
}
}
}
#if DEBUG
catch (Exception ex)
{
ASF.ArchiLogger.LogGenericException(ex, "Community with ASFEnhance failed");
}
#else
catch (Exception)
{
ASF.ArchiLogger.LogGenericDebug("Community with ASFEnhance failed");
}
#endif
return false;
}
}
20 changes: 19 additions & 1 deletion BoosterManager/BoosterManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,37 @@
using ArchiSteamFarm.Steam.Exchange;
using System.Text.Json;
using ArchiSteamFarm.Helpers.Json;
using System.Reflection;

namespace BoosterManager {
[Export(typeof(IPlugin))]
public sealed class BoosterManager : IASF, IBotModules, IBotCommand2, IBotTradeOfferResults {
public string Name => nameof(BoosterManager);
public Version Version => typeof(BoosterManager).Assembly.GetName().Version ?? new Version("0");
private bool ASFEnhanceEnabled = false;

public Task OnLoaded() {
ASF.ArchiLogger.LogGenericInfo("BoosterManager ASF Plugin by Citrinate");

// ASFEnhanced Adapter https://github.com/chr233/ASFEnhanceAdapterDemoPlugin
var flag = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
var handler = typeof(Commands).GetMethod(nameof(Commands.Response), flag);
const string pluginId = nameof(BoosterManager);
const string cmdPrefix = "BOOSTERMANAGER";
const string repoName = "Citrinate/BoosterManager";
var registered = AdapterBridge.InitAdapter(Name, pluginId, cmdPrefix, repoName, handler);
ASFEnhanceEnabled = registered;

return Task.CompletedTask;
}

public async Task<string?> OnBotCommand(Bot bot, EAccess access, string message, string[] args, ulong steamID = 0) => await Commands.Response(bot, access, steamID, message, args).ConfigureAwait(false);
public async Task<string?> OnBotCommand(Bot bot, EAccess access, string message, string[] args, ulong steamID = 0) {
if (ASFEnhanceEnabled) {
return null;
}

return await Commands.Response(bot, access, steamID, message, args).ConfigureAwait(false);
}

public Task OnASFInit(IReadOnlyDictionary<string, JsonElement>? additionalConfigProperties = null) {
if (additionalConfigProperties == null) {
Expand Down
4 changes: 3 additions & 1 deletion BoosterManager/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using ArchiSteamFarm.Localization;
using ArchiSteamFarm.Steam.Data;
using System.ComponentModel;
using System.Collections.Immutable;
using System.Reflection;

namespace BoosterManager {
Expand All @@ -24,6 +23,9 @@ internal static class Commands {
switch (args.Length) {
case 1:
switch (args[0].ToUpperInvariant()) {
case "BOOSTERMANAGER" when access >= EAccess.FamilySharing:
return String.Format("{0} {1}", nameof(BoosterManager), (typeof(BoosterManager).Assembly.GetName().Version ?? new Version("0")).ToString());

case "BDROP" or "BDROPS":
return await ResponseBoosterDrops(bot, access).ConfigureAwait(false);

Expand Down

0 comments on commit 3efdd7b

Please sign in to comment.