Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to ASF 6 #14

Merged
merged 13 commits into from
Mar 9, 2024
2 changes: 1 addition & 1 deletion ArchiSteamFarm
Submodule ArchiSteamFarm updated 177 files
32 changes: 32 additions & 0 deletions BoosterManager/AdapterBridge.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Reflection;
using ArchiSteamFarm.Core;

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

namespace BoosterManager;
internal static class AdapterBridge {
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);
}
}
}
} catch (Exception ex) {
ASF.ArchiLogger.LogGenericException(ex, "Community with ASFEnhance failed");
}

return false;
}
}
67 changes: 43 additions & 24 deletions BoosterManager/BoosterManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,67 +6,86 @@
using ArchiSteamFarm.Core;
using ArchiSteamFarm.Steam;
using ArchiSteamFarm.Plugins.Interfaces;
using Newtonsoft.Json.Linq;
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, JToken>? additionalConfigProperties = null) {
public Task OnASFInit(IReadOnlyDictionary<string, JsonElement>? additionalConfigProperties = null) {
if (additionalConfigProperties == null) {
return Task.FromResult(0);
}

foreach (KeyValuePair<string, JToken> configProperty in additionalConfigProperties) {
foreach (KeyValuePair<string, JsonElement> configProperty in additionalConfigProperties) {
switch (configProperty.Key) {
case "AllowCraftUntradableBoosters" when configProperty.Value.Type == JTokenType.Boolean: {
case "AllowCraftUntradableBoosters" when (configProperty.Value.ValueKind == JsonValueKind.True || configProperty.Value.ValueKind == JsonValueKind.False): {
ASF.ArchiLogger.LogGenericInfo("Allow Craft Untradable Boosters : " + configProperty.Value);
BoosterHandler.AllowCraftUntradableBoosters = configProperty.Value.ToObject<bool>();
BoosterHandler.AllowCraftUntradableBoosters = configProperty.Value.GetBoolean();
break;
}
case "BoosterDelayBetweenBots" when configProperty.Value.Type == JTokenType.Integer: {
case "BoosterDelayBetweenBots" when configProperty.Value.ValueKind == JsonValueKind.Number: {
ASF.ArchiLogger.LogGenericInfo("Booster Delay Between Bots : " + configProperty.Value);
BoosterHandler.UpdateBotDelays((int)configProperty.Value.ToObject<uint>());
BoosterHandler.UpdateBotDelays(configProperty.Value.GetInt32());
break;
}
case "BoosterDataAPI" when configProperty.Value.Type == JTokenType.String: {
case "BoosterDataAPI" when configProperty.Value.ValueKind == JsonValueKind.String: {
ASF.ArchiLogger.LogGenericInfo("Booster Data API : " + configProperty.Value);
DataHandler.BoosterDataAPI = new Uri(configProperty.Value.ToObject<string>()!);
DataHandler.BoosterDataAPI = new Uri(configProperty.Value.GetString()!);
break;
}
case "InventoryHistoryAPI" when configProperty.Value.Type == JTokenType.String: {
case "InventoryHistoryAPI" when configProperty.Value.ValueKind == JsonValueKind.String: {
ASF.ArchiLogger.LogGenericInfo("Inventory History API : " + configProperty.Value);
DataHandler.InventoryHistoryAPI = new Uri(configProperty.Value.ToObject<string>()!);
DataHandler.InventoryHistoryAPI = new Uri(configProperty.Value.GetString()!);
break;
}
case "MarketListingsAPI" when configProperty.Value.Type == JTokenType.String: {
case "MarketListingsAPI" when configProperty.Value.ValueKind == JsonValueKind.String: {
ASF.ArchiLogger.LogGenericInfo("Market Listings API : " + configProperty.Value);
DataHandler.MarketListingsAPI = new Uri(configProperty.Value.ToObject<string>()!);
DataHandler.MarketListingsAPI = new Uri(configProperty.Value.GetString()!);
break;
}
case "MarketHistoryAPI" when configProperty.Value.Type == JTokenType.String: {
case "MarketHistoryAPI" when configProperty.Value.ValueKind == JsonValueKind.String: {
ASF.ArchiLogger.LogGenericInfo("Market History API : " + configProperty.Value);
DataHandler.MarketHistoryAPI = new Uri(configProperty.Value.ToObject<string>()!);
DataHandler.MarketHistoryAPI = new Uri(configProperty.Value.GetString()!);
break;
}
case "LogDataPageDelay" or "MarketHistoryDelay" when configProperty.Value.Type == JTokenType.Integer: {
case "LogDataPageDelay" or "MarketHistoryDelay" when configProperty.Value.ValueKind == JsonValueKind.Number: {
ASF.ArchiLogger.LogGenericInfo("Log Data Page Delay : " + configProperty.Value);
DataHandler.LogDataPageDelay = configProperty.Value.ToObject<uint>();
DataHandler.LogDataPageDelay = configProperty.Value.GetUInt32();
break;
}
case "InventoryHistoryAppFilter" when configProperty.Value.Type == JTokenType.Array && configProperty.Value.Any(): {
case "InventoryHistoryAppFilter" when configProperty.Value.ValueKind == JsonValueKind.Array && configProperty.Value.GetArrayLength() > 0: {
ASF.ArchiLogger.LogGenericInfo("Inventory History App Filter : " + string.Join(",", configProperty.Value));
List<uint>? appIDs = configProperty.Value.ToObject<List<uint>>();
List<uint>? appIDs = configProperty.Value.ToJsonObject<List<uint>>();
if (appIDs == null) {
ASF.ArchiLogger.LogNullError(appIDs);
} else {
Expand All @@ -80,18 +99,18 @@ public Task OnASFInit(IReadOnlyDictionary<string, JToken>? additionalConfigPrope
return Task.FromResult(0);
}

public Task OnBotInitModules(Bot bot, IReadOnlyDictionary<string, JToken>? additionalConfigProperties = null) {
public Task OnBotInitModules(Bot bot, IReadOnlyDictionary<string, JsonElement>? additionalConfigProperties = null) {
BoosterHandler.AddHandler(bot);

if (additionalConfigProperties == null) {
return Task.FromResult(0);
}

foreach (KeyValuePair<string, JToken> configProperty in additionalConfigProperties) {
foreach (KeyValuePair<string, JsonElement> configProperty in additionalConfigProperties) {
switch (configProperty.Key) {
case "GamesToBooster" when configProperty.Value.Type == JTokenType.Array && configProperty.Value.Any(): {
case "GamesToBooster" when configProperty.Value.ValueKind == JsonValueKind.Array && configProperty.Value.GetArrayLength() > 0: {
bot.ArchiLogger.LogGenericInfo("Games To Booster : " + string.Join(",", configProperty.Value));
HashSet<uint>? gameIDs = configProperty.Value.ToObject<HashSet<uint>>();
HashSet<uint>? gameIDs = configProperty.Value.ToJsonObject<HashSet<uint>>();
if (gameIDs == null) {
bot.ArchiLogger.LogNullError(gameIDs);
} else {
Expand Down
2 changes: 1 addition & 1 deletion BoosterManager/BoosterManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<Authors>Citrinate</Authors>
<AssemblyVersion>2.8.2</AssemblyVersion>
<AssemblyVersion>2.8.3</AssemblyVersion>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<TargetFramework>net8.0</TargetFramework>
Expand Down
12 changes: 8 additions & 4 deletions BoosterManager/Boosters/BoosterDatabase.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using ArchiSteamFarm.Core;
using ArchiSteamFarm.Helpers;
using ArchiSteamFarm.Helpers.Json;
using ArchiSteamFarm.Localization;
using Newtonsoft.Json;

namespace BoosterManager {
internal sealed class BoosterDatabase : SerializableFile {
[JsonProperty(Required = Required.DisallowNull)]
private readonly ConcurrentDictionary<uint, BoosterLastCraft> BoosterLastCrafts = new();
[JsonInclude]
private ConcurrentDictionary<uint, BoosterLastCraft> BoosterLastCrafts { get; init; } = new();

[JsonConstructor]
private BoosterDatabase() { }
Expand All @@ -22,6 +24,8 @@ private BoosterDatabase(string filePath) : this() {
FilePath = filePath;
}

protected override Task Save() => Save(this);

internal static BoosterDatabase? CreateOrLoad(string filePath) {
if (string.IsNullOrEmpty(filePath)) {
throw new ArgumentNullException(nameof(filePath));
Expand All @@ -42,7 +46,7 @@ private BoosterDatabase(string filePath) : this() {
return null;
}

boosterDatabase = JsonConvert.DeserializeObject<BoosterDatabase>(json);
boosterDatabase = json.ToJsonObject<BoosterDatabase>();
} catch (Exception e) {
ASF.ArchiLogger.LogGenericException(e);

Expand Down
12 changes: 7 additions & 5 deletions BoosterManager/Boosters/BoosterLastCraft.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using System;
using Newtonsoft.Json;
using System.Text.Json.Serialization;

namespace BoosterManager {
internal sealed class BoosterLastCraft {
[JsonProperty(Required = Required.Always)]
internal DateTime CraftTime;
[JsonInclude]
[JsonRequired]
internal DateTime CraftTime { get; set; }

[JsonProperty(Required = Required.Always)]
internal int BoosterDelay;
[JsonInclude]
[JsonRequired]
internal int BoosterDelay { get; set; }

[JsonConstructor]
private BoosterLastCraft() { }
Expand Down
4 changes: 2 additions & 2 deletions BoosterManager/Boosters/BoosterPageResponse.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.RegularExpressions;
using AngleSharp.Dom;
using ArchiSteamFarm.Localization;
using ArchiSteamFarm.Steam;
using Newtonsoft.Json;

namespace BoosterManager {
internal sealed class BoosterPageResponse {
Expand Down Expand Up @@ -37,7 +37,7 @@ internal BoosterPageResponse(Bot bot, IDocument? boosterPage) {

IEnumerable<Steam.BoosterInfo>? enumerableBoosters;
try {
enumerableBoosters = JsonConvert.DeserializeObject<IEnumerable<Steam.BoosterInfo>>(info.Value, new Steam.BoosterInfoDateConverter());
enumerableBoosters = JsonSerializer.Deserialize<IEnumerable<Steam.BoosterInfo>>(info.Value, new JsonSerializerOptions { NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.AllowReadingFromString });
} catch (JsonException ex) {
Bot.ArchiLogger.LogGenericError(ex.Message);

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
8 changes: 4 additions & 4 deletions BoosterManager/Data/ItemIdentifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ internal bool IsItemMatch(Asset item) {
}

if (TextID != null) {
string? name = item.AdditionalPropertiesReadOnly?["name"].ToObject<string>();
string? marketName = item.AdditionalPropertiesReadOnly?["market_name"].ToObject<string>();
string? marketHashName = item.AdditionalPropertiesReadOnly?["market_hash_name"].ToObject<string>();
string? type = item.AdditionalPropertiesReadOnly?["type"].ToObject<string>();
string? name = item.AdditionalPropertiesReadOnly?["name"].GetString();
string? marketName = item.AdditionalPropertiesReadOnly?["market_name"].GetString();
string? marketHashName = item.AdditionalPropertiesReadOnly?["market_hash_name"].GetString();
string? type = item.AdditionalPropertiesReadOnly?["type"].GetString();

if ((name == null || !name.Contains(TextID))
&& (marketName == null || !marketName.Contains(TextID))
Expand Down
13 changes: 7 additions & 6 deletions BoosterManager/Data/ItemListing.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Text.Json.Nodes;
using ArchiSteamFarm.Core;
using Newtonsoft.Json.Linq;
using ArchiSteamFarm.Helpers.Json;

namespace BoosterManager {
internal sealed class ItemListing {
Expand All @@ -12,10 +13,10 @@ internal sealed class ItemListing {
internal ulong ContextID;
internal ulong ClassID;

internal ItemListing(JObject listing) {
internal ItemListing(JsonObject listing) {
string? name = listing["asset"]?["name"]?.ToString();
if (name == null) {
ASF.ArchiLogger.LogNullError(name);
ASF.ArchiLogger.LogNullError(name);
throw new InvalidOperationException();
}

Expand All @@ -37,19 +38,19 @@ internal ItemListing(JObject listing) {
throw new InvalidOperationException();
}

uint? appID = listing["asset"]?["appid"]?.ToObject<uint>();
uint? appID = listing["asset"]?["appid"]?.ToString().ToJsonObject<uint>();
if (appID == null) {
ASF.ArchiLogger.LogNullError(appID);
throw new InvalidOperationException();
}

ulong? contextID = listing["asset"]?["contextid"]?.ToObject<ulong>();
ulong? contextID = listing["asset"]?["contextid"]?.ToString().ToJsonObject<ulong>();
if (contextID == null) {
ASF.ArchiLogger.LogNullError(contextID);
throw new InvalidOperationException();
}

ulong? classID = listing["asset"]?["classid"]?.ToObject<ulong>();
ulong? classID = listing["asset"]?["classid"]?.ToString().ToJsonObject<ulong>();
if (classID == null) {
ASF.ArchiLogger.LogNullError(classID);
throw new InvalidOperationException();
Expand Down
27 changes: 16 additions & 11 deletions BoosterManager/Data/SteamData.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
using System;
using System.Text.Json.Serialization;
using ArchiSteamFarm.Steam;
using Newtonsoft.Json;

namespace BoosterManager {
internal sealed class SteamData<T> {
[JsonProperty(PropertyName = "steamid")]
public ulong SteamID;
[JsonInclude]
[JsonPropertyName("steamid")]
public ulong SteamID { get; private init; }

[JsonProperty(PropertyName = "source")]
public string Source;
[JsonInclude]
[JsonPropertyName("source")]
public string Source { get; private init; }

[JsonProperty(PropertyName = "page")]
public uint? Page;
[JsonInclude]
[JsonPropertyName("page")]
public uint? Page { get; private init; }

[JsonProperty(PropertyName = "cursor")]
public Steam.InventoryHistoryCursor? Cursor;
[JsonInclude]
[JsonPropertyName("cursor")]
public Steam.InventoryHistoryCursor? Cursor { get; private init; }

[JsonProperty(PropertyName = "data")]
public T Data;
[JsonInclude]
[JsonPropertyName("data")]
public T Data { get; private init; }

internal SteamData(Bot bot, T steamData, Uri source, uint? page, Steam.InventoryHistoryCursor? cursor) {
SteamID = bot.SteamID;
Expand Down
Loading