From d6f4efd98156c44188cb4cab55141a957fc5b540 Mon Sep 17 00:00:00 2001 From: Citrinate Date: Sat, 24 Feb 2024 19:13:06 -0500 Subject: [PATCH] Update ArchiSteamFarm to 6.0.0.2 --- ArchiSteamFarm | 2 +- CS2Interface/CS2Interface.cs | 14 +-- CS2Interface/Data/InspectItem.cs | 7 +- CS2Interface/Data/InventoryItem.cs | 27 +++-- CS2Interface/Data/Item.cs | 117 ++++++++++++--------- CS2Interface/Data/ItemData.cs | 24 +++-- CS2Interface/Data/ItemDef.cs | 8 +- CS2Interface/Helpers/AttributeConverter.cs | 50 ++++----- CS2Interface/Helpers/KVConverter.cs | 85 +++++++++------ 9 files changed, 186 insertions(+), 148 deletions(-) diff --git a/ArchiSteamFarm b/ArchiSteamFarm index 716b253..7a21c7b 160000 --- a/ArchiSteamFarm +++ b/ArchiSteamFarm @@ -1 +1 @@ -Subproject commit 716b253a044c9560bea1e9a77cb34afede93c6a3 +Subproject commit 7a21c7bc452bceed72c7dd0b0150183acabfe762 diff --git a/CS2Interface/CS2Interface.cs b/CS2Interface/CS2Interface.cs index 1e7c612..899ec0a 100644 --- a/CS2Interface/CS2Interface.cs +++ b/CS2Interface/CS2Interface.cs @@ -5,9 +5,9 @@ using ArchiSteamFarm.Core; using ArchiSteamFarm.Steam; using ArchiSteamFarm.Plugins.Interfaces; -using Newtonsoft.Json.Linq; using SteamKit2; using System.Collections.Concurrent; +using System.Text.Json; namespace CS2Interface { [Export(typeof(IPlugin))] @@ -25,7 +25,7 @@ public Task OnLoaded() { public async Task OnBotCommand(Bot bot, EAccess access, string message, string[] args, ulong steamID = 0) => await Commands.Response(bot, access, steamID, message, args).ConfigureAwait(false); - public Task OnASFInit(IReadOnlyDictionary? additionalConfigProperties = null) { + public Task OnASFInit(IReadOnlyDictionary? additionalConfigProperties = null) { if (additionalConfigProperties == null) { return Task.FromResult(0); } @@ -33,16 +33,16 @@ public Task OnASFInit(IReadOnlyDictionary? additionalConfigPrope return Task.FromResult(0); } - public Task OnBotInitModules(Bot bot, IReadOnlyDictionary? additionalConfigProperties = null) { + public Task OnBotInitModules(Bot bot, IReadOnlyDictionary? additionalConfigProperties = null) { if (additionalConfigProperties == null) { return Task.FromResult(0); } - foreach (KeyValuePair configProperty in additionalConfigProperties) { + foreach (KeyValuePair configProperty in additionalConfigProperties) { switch (configProperty.Key) { - case "AutoStartCS2Interface" when configProperty.Value.Type == JTokenType.Boolean: { - bot.ArchiLogger.LogGenericInfo("AutoStartCS2Interface : " + configProperty.Value); - AutoStart[bot.BotName] = configProperty.Value.ToObject(); + case "AutoStartCS2Interface" when (configProperty.Value.ValueKind == JsonValueKind.True || configProperty.Value.ValueKind == JsonValueKind.False): { + bot.ArchiLogger.LogGenericInfo("AutoStartCS2Interface : " + configProperty.Value.GetBoolean()); + AutoStart[bot.BotName] = configProperty.Value.GetBoolean(); break; } } diff --git a/CS2Interface/Data/InspectItem.cs b/CS2Interface/Data/InspectItem.cs index f61e50f..1419f3c 100644 --- a/CS2Interface/Data/InspectItem.cs +++ b/CS2Interface/Data/InspectItem.cs @@ -1,12 +1,13 @@ using System; using System.Linq; -using Newtonsoft.Json; +using System.Text.Json.Serialization; using SteamKit2.GC.CSGO.Internal; namespace CS2Interface { internal sealed class InspectItem : Item { - [JsonProperty(PropertyName = "iteminfo")] - internal CEconItemPreviewDataBlock ItemInfo; + [JsonInclude] + [JsonPropertyName("iteminfo")] + internal CEconItemPreviewDataBlock ItemInfo { get; private init; } internal string s; internal string a; internal string d; diff --git a/CS2Interface/Data/InventoryItem.cs b/CS2Interface/Data/InventoryItem.cs index 9cd624e..feca126 100644 --- a/CS2Interface/Data/InventoryItem.cs +++ b/CS2Interface/Data/InventoryItem.cs @@ -2,28 +2,33 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.Json.Serialization; using ArchiSteamFarm.Core; -using Newtonsoft.Json; using SteamKit2; using SteamKit2.GC.CSGO.Internal; namespace CS2Interface { internal sealed class InventoryItem : Item { - [JsonProperty(PropertyName = "iteminfo")] - internal CSOEconItem ItemInfo; + [JsonInclude] + [JsonPropertyName("iteminfo")] + internal CSOEconItem ItemInfo { get; private init; } - [JsonProperty(PropertyName = "attributes")] + [JsonInclude] + [JsonPropertyName("attributes")] [JsonConverter (typeof(AttributeConverter))] - Dictionary? Attributes; + internal Dictionary? Attributes { get; private set; } - [JsonProperty(PropertyName = "position")] - internal uint? Position; + [JsonInclude] + [JsonPropertyName("position")] + internal uint? Position { get; private set; } - [JsonProperty(PropertyName = "casket_id")] - internal ulong? CasketID; + [JsonInclude] + [JsonPropertyName("casket_id")] + internal ulong? CasketID { get; private set; } - [JsonProperty(PropertyName = "moveable")] - internal bool? Moveable; + [JsonInclude] + [JsonPropertyName("moveable")] + internal bool? Moveable { get; private set; } public bool ShouldSerializeAttributes() => Attributes != null && ShouldSerializeAdditionalProperties; public bool ShouldSerializePosition() => Position != null && ShouldSerializeAdditionalProperties; diff --git a/CS2Interface/Data/Item.cs b/CS2Interface/Data/Item.cs index bee1645..17d6192 100644 --- a/CS2Interface/Data/Item.cs +++ b/CS2Interface/Data/Item.cs @@ -1,6 +1,6 @@ using System; using System.Linq; -using Newtonsoft.Json; +using System.Text.Json.Serialization; using SteamKit2; namespace CS2Interface { @@ -14,74 +14,97 @@ internal class Item { internal uint Rarity; internal uint Origin; - [JsonProperty(PropertyName = "full_name")] - internal string? FullName; + [JsonInclude] + [JsonPropertyName("full_name")] + internal string? FullName { get; private set; } - [JsonProperty(PropertyName = "full_type_name")] - internal string? FullTypeName; + [JsonInclude] + [JsonPropertyName("full_type_name")] + internal string? FullTypeName { get; private set; } - [JsonProperty(PropertyName = "rarity_name")] - internal string? RarityName; + [JsonInclude] + [JsonPropertyName("rarity_name")] + internal string? RarityName { get; private set; } - [JsonProperty(PropertyName = "quality_name")] - internal string? QualityName; + [JsonInclude] + [JsonPropertyName("quality_name")] + internal string? QualityName { get; private set; } - [JsonProperty(PropertyName = "origin_name")] - internal string? OriginName; + [JsonInclude] + [JsonPropertyName("origin_name")] + internal string? OriginName { get; private set; } - [JsonProperty(PropertyName = "type_name")] - internal string? TypeName; + [JsonInclude] + [JsonPropertyName("type_name")] + internal string? TypeName { get; private set; } - [JsonProperty(PropertyName = "item_name")] - internal string? ItemName; + [JsonInclude] + [JsonPropertyName("item_name")] + internal string? ItemName { get; private set; } - [JsonProperty(PropertyName = "tool_name")] - internal string? ToolName; + [JsonInclude] + [JsonPropertyName("tool_name")] + internal string? ToolName { get; private set; } - [JsonProperty(PropertyName = "tint_name")] - internal string? TintName; + [JsonInclude] + [JsonPropertyName("tint_name")] + internal string? TintName { get; private set; } - [JsonProperty(PropertyName = "weapon_image_url")] - internal string? WeaponImageURL; + [JsonInclude] + [JsonPropertyName("weapon_image_url")] + internal string? WeaponImageURL { get; private set; } - [JsonProperty(PropertyName = "weapon_name")] - internal string? WeaponName; + [JsonInclude] + [JsonPropertyName("weapon_name")] + internal string? WeaponName { get; private set; } - [JsonProperty(PropertyName = "wear_name")] - internal string? WearName; + [JsonInclude] + [JsonPropertyName("wear_name")] + internal string? WearName { get; private set; } - [JsonProperty(PropertyName = "wear")] - internal double? Wear; + [JsonInclude] + [JsonPropertyName("wear")] + internal double? Wear { get; set; } - [JsonProperty(PropertyName = "wear_min")] - internal float? WearMin; + [JsonInclude] + [JsonPropertyName("wear_min")] + internal float? WearMin { get; private set; } - [JsonProperty(PropertyName = "wear_max")] - internal float? WearMax; + [JsonInclude] + [JsonPropertyName("wear_max")] + internal float? WearMax { get; private set; } - [JsonProperty(PropertyName = "name_id")] - internal string? NameID; + [JsonInclude] + [JsonPropertyName("name_id")] + internal string? NameID { get; private set; } - [JsonProperty(PropertyName = "set_name_id")] - internal string? SetNameID; + [JsonInclude] + [JsonPropertyName("set_name_id")] + internal string? SetNameID { get; private set; } - [JsonProperty(PropertyName = "set_name")] - internal string? SetName; + [JsonInclude] + [JsonPropertyName("set_name")] + internal string? SetName { get; private set; } - [JsonProperty(PropertyName = "crate_name_id")] - internal string? CrateNameID; + [JsonInclude] + [JsonPropertyName("crate_name_id")] + internal string? CrateNameID { get; private set; } - [JsonProperty(PropertyName = "crate_defindex")] - internal uint? CrateDefIndex; + [JsonInclude] + [JsonPropertyName("crate_defindex")] + internal uint? CrateDefIndex { get; private set; } - [JsonProperty(PropertyName = "crate_supply_series")] - internal uint? CrateSupplySeries; + [JsonInclude] + [JsonPropertyName("crate_supply_series")] + internal uint? CrateSupplySeries { get; private set; } - [JsonProperty(PropertyName = "crate_name")] - internal string? CrateName; + [JsonInclude] + [JsonPropertyName("crate_name")] + internal string? CrateName { get; private set; } - [JsonProperty(PropertyName = "defs")] - internal ItemData? ItemData; + [JsonInclude] + [JsonPropertyName("defs")] + internal ItemData? ItemData { get; private set; } protected static bool ShouldSerializeAdditionalProperties = true; protected static bool ShouldSerializeDefs = true; diff --git a/CS2Interface/Data/ItemData.cs b/CS2Interface/Data/ItemData.cs index cc65669..ea0ad4b 100644 --- a/CS2Interface/Data/ItemData.cs +++ b/CS2Interface/Data/ItemData.cs @@ -1,21 +1,25 @@ using System; +using System.Text.Json.Serialization; using ArchiSteamFarm.Core; -using Newtonsoft.Json; using SteamKit2; namespace CS2Interface { internal class ItemData { - [JsonProperty(PropertyName = "item_def")] - internal ItemDef ItemDef; + [JsonInclude] + [JsonPropertyName("item_def")] + internal ItemDef ItemDef { get; private init; } - [JsonProperty(PropertyName = "paint_kit_def")] - internal ItemDef? PaintKitDef; + [JsonInclude] + [JsonPropertyName("paint_kit_def")] + internal ItemDef? PaintKitDef { get; private init; } - [JsonProperty(PropertyName = "sticker_kit_def")] - internal ItemDef? StickerKitDef; + [JsonInclude] + [JsonPropertyName("sticker_kit_def")] + internal ItemDef? StickerKitDef { get; private init; } - [JsonProperty(PropertyName = "music_def")] - internal ItemDef? MusicDef; + [JsonInclude] + [JsonPropertyName("music_def")] + internal ItemDef? MusicDef { get; private init; } public bool ShouldSerializeItemDef() => ItemDef != null; public bool ShouldSerializePaintKitDef() => PaintKitDef != null; @@ -48,8 +52,6 @@ private bool MergePrefab(ItemDef itemDef, string? prefab) { return true; } - // STACK OVERFLOW HERE - // Some items have multiple prefabs separated by a space, but only one is valid (it has an entry in ItemsGame) // Ex: "valve weapon_case_key": "valve" isn't valid, but "weapon_case_key" is // Ex: "antwerp2022_sticker_capsule_prefab antwerp2022_sellable_item_with_payment_rules": "antwerp2022_sticker_capsule_prefab" is valid, but "antwerp2022_sellable_item_with_payment_rules" isn't diff --git a/CS2Interface/Data/ItemDef.cs b/CS2Interface/Data/ItemDef.cs index 49162e9..c167b8c 100644 --- a/CS2Interface/Data/ItemDef.cs +++ b/CS2Interface/Data/ItemDef.cs @@ -1,13 +1,15 @@ using System; using System.Collections.Generic; using System.Linq; -using Newtonsoft.Json; +using System.Text.Json.Serialization; using SteamKit2; namespace CS2Interface { internal class ItemDef { - [JsonProperty(PropertyName = "defs", ItemConverterType = typeof(KVConverter))] - internal List Defs = new(); + [JsonInclude] + [JsonPropertyName("defs")] + [JsonConverter(typeof(JsonListItemConverter))] + internal List Defs { get; private init; } = new(); internal ItemDef(KeyValue? def) { AddDef(def); diff --git a/CS2Interface/Helpers/AttributeConverter.cs b/CS2Interface/Helpers/AttributeConverter.cs index 574bcbb..2a9e9d2 100644 --- a/CS2Interface/Helpers/AttributeConverter.cs +++ b/CS2Interface/Helpers/AttributeConverter.cs @@ -1,43 +1,31 @@ using System; using System.Collections.Generic; +using System.Text.Json; +using System.Text.Json.Serialization; using ArchiSteamFarm.Core; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; namespace CS2Interface { - public sealed class AttributeConverter : JsonConverter { - public override bool CanConvert(Type objectType) { - return objectType == typeof(Dictionary); + public sealed class AttributeConverter : JsonConverter> { + + public override Dictionary Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { + throw new NotImplementedException(); } - - public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer) { - JObject json = new(); - if (value is Dictionary attributes) { - foreach (var kvp in attributes) { - var attribute = kvp.Value; - try { - if (attribute.Type == typeof(uint)) { - json.Add(attribute.Name, attribute.ToUInt32()); - } if (attribute.Type == typeof(float)) { - json.Add(attribute.Name, attribute.ToSingle()); - } else if (attribute.Type == typeof(string)) { - json.Add(attribute.Name, attribute.ToString()); - } - } catch (Exception e) { - ASF.ArchiLogger.LogGenericException(e); + + public override void Write(Utf8JsonWriter writer, Dictionary value, JsonSerializerOptions options) { + foreach (var kvp in value) { + var attribute = kvp.Value; + try { + if (attribute.Type == typeof(uint)) { + writer.WriteNumber(attribute.Name, attribute.ToUInt32()); + } if (attribute.Type == typeof(float)) { + writer.WriteNumber(attribute.Name, attribute.ToSingle()); + } else if (attribute.Type == typeof(string)) { + writer.WriteString(attribute.Name, attribute.ToString()); } + } catch (Exception e) { + ASF.ArchiLogger.LogGenericException(e); } } - - json.WriteTo(writer); } - - public override bool CanRead { - get { return false; } - } - - public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) { - throw new NotImplementedException(); - } } } \ No newline at end of file diff --git a/CS2Interface/Helpers/KVConverter.cs b/CS2Interface/Helpers/KVConverter.cs index ceebb16..d01b796 100644 --- a/CS2Interface/Helpers/KVConverter.cs +++ b/CS2Interface/Helpers/KVConverter.cs @@ -1,71 +1,88 @@ using System; +using System.Collections.Generic; using System.Globalization; +using System.Text.Json; +using System.Text.Json.Nodes; +using System.Text.Json.Serialization; using ArchiSteamFarm.Core; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; using SteamKit2; namespace CS2Interface { - public sealed class KVConverter : JsonConverter { - public override bool CanConvert(Type objectType) { - return objectType == typeof(KeyValue); + public sealed class KVConverter : JsonConverter { + public override KeyValue Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { + throw new NotImplementedException(); } - - public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer) { - JToken? json = null; - if (value is KeyValue vdf) { - json = ConvertKVObjectToJson(vdf); - } - if (json == null) { - writer.WriteNull(); - - return; - } - - json.WriteTo(writer); + public override void Write(Utf8JsonWriter writer, KeyValue value, JsonSerializerOptions options) { + ConvertKVObjectToJson(ref writer, value); } - private JToken? ConvertKVObjectToJson (KeyValue vdf) { + private void ConvertKVObjectToJson (ref Utf8JsonWriter writer, KeyValue vdf) { if (vdf.Children.Count > 0) { - JObject json = new(); + writer.WriteStartObject(); foreach (KeyValue child in vdf.Children) { if (child.Name == null) { continue; } try { - json.Add(child.Name, ConvertKVObjectToJson(child)); + writer.WritePropertyName(child.Name); + ConvertKVObjectToJson(ref writer, child); } catch (Exception e) { // item["523"] (Talon Knife) has duplicates of "inventory_image_data", just ignore the duplicates ASF.ArchiLogger.LogGenericException(e); } - } + } + writer.WriteEndObject(); - return json; + return; } if (int.TryParse(vdf.Value, out int intValue)) { - return intValue; + writer.WriteNumberValue(intValue); + + return; } if (long.TryParse(vdf.Value, out long longValue)) { - return longValue; + writer.WriteNumberValue(longValue); + + return; } if (float.TryParse(vdf.Value, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out float floatValue)) { - return floatValue; + writer.WriteNumberValue(floatValue); + + return; } - return vdf.Value; + writer.WriteStringValue(vdf.Value); } - - public override bool CanRead { - get { return false; } - } - - public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) { + } + + // https://github.com/dotnet/runtime/issues/54189#issuecomment-861628532 + public class JsonListItemConverter : JsonConverter> where TConverterType : JsonConverter { + public override List Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { throw new NotImplementedException(); - } + } + + public override void Write(Utf8JsonWriter writer, List value, JsonSerializerOptions options) { + if (value == null) { + writer.WriteNullValue(); + return; + } + + JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions(options); + jsonSerializerOptions.Converters.Clear(); + jsonSerializerOptions.Converters.Add(Activator.CreateInstance()); + + writer.WriteStartArray(); + + foreach (TDatatype data in value) { + JsonSerializer.Serialize(writer, data, jsonSerializerOptions); + } + + writer.WriteEndArray(); + } } } \ No newline at end of file