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 86af6c9 commit 82d48ed
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
57 changes: 57 additions & 0 deletions FreePackages/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 FreePackages;
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;
}
}
3 changes: 3 additions & 0 deletions FreePackages/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ internal static class Commands {
switch (args.Length) {
case 1:
switch (args[0].ToUpperInvariant()) {
case "FREEPACKAGES" when access >= EAccess.FamilySharing:
return String.Format("{0} {1}", nameof(FreePackages), (typeof(FreePackages).Assembly.GetName().Version ?? new Version("0")).ToString());

case "CLEARFREEPACKAGESQUEUE":
return ResponseClearQueue(bot, access);

Expand Down
20 changes: 19 additions & 1 deletion FreePackages/FreePackages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,38 @@
using SteamKit2;
using System.Text.Json;
using ArchiSteamFarm.Helpers.Json;
using System.Reflection;

namespace FreePackages {
[Export(typeof(IPlugin))]
public sealed class FreePackages : IASF, IBotModules, ISteamPICSChanges, IBotSteamClient, IBotConnection, IBotCommand2 {
public string Name => nameof(FreePackages);
public Version Version => typeof(FreePackages).Assembly.GetName().Version ?? new Version("0");
internal static GlobalCache? GlobalCache;
private bool ASFEnhanceEnabled = false;

public Task OnLoaded() {
ASF.ArchiLogger.LogGenericInfo("Free Packages 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(FreePackages);
const string cmdPrefix = "FREEPACKAGES";
const string repoName = "Citrinate/FreePackages";
var registered = AdapterBridge.InitAdapter(Name, pluginId, cmdPrefix, repoName, handler);
ASFEnhanceEnabled = registered;

return Task.CompletedTask;
}

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

return Task.FromResult(Commands.Response(bot, access, steamID, message, args));
}

public async Task OnASFInit(IReadOnlyDictionary<string, JsonElement>? additionalConfigProperties = null) {
if (GlobalCache == null) {
Expand Down

0 comments on commit 82d48ed

Please sign in to comment.