From 74bc84a45755a20b08ce41cb7751974e4dc53d98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ayta=C3=A7=20Kayadelen?= Date: Thu, 14 Mar 2024 09:25:37 +0100 Subject: [PATCH] migrate cs2 to System.Text.Json --- .../Artemis.Plugins.Games.CSGO.csproj | 3 +- src/Artemis.Plugins.Games.CSGO/CsgoModule.cs | 38 ++++++++----------- .../GameDataModels/Map.cs | 20 +++++----- .../GameDataModels/MatchStats.cs | 12 +++--- .../GameDataModels/Player.cs | 18 ++++----- .../GameDataModels/RootGameData.cs | 17 +++------ .../GameDataModels/Round.cs | 6 +-- .../GameDataModels/State.cs | 24 ++++++------ .../GameDataModels/Team.cs | 10 ++--- .../GameDataModels/Weapon.cs | 16 ++++---- .../GameDataModels/Weapons.cs | 10 ++--- .../JsonSourceContext.cs | 7 ++++ 12 files changed, 87 insertions(+), 94 deletions(-) create mode 100644 src/Artemis.Plugins.Games.CSGO/JsonSourceContext.cs diff --git a/src/Artemis.Plugins.Games.CSGO/Artemis.Plugins.Games.CSGO.csproj b/src/Artemis.Plugins.Games.CSGO/Artemis.Plugins.Games.CSGO.csproj index e818726..47c908c 100644 --- a/src/Artemis.Plugins.Games.CSGO/Artemis.Plugins.Games.CSGO.csproj +++ b/src/Artemis.Plugins.Games.CSGO/Artemis.Plugins.Games.CSGO.csproj @@ -7,9 +7,8 @@ - + - diff --git a/src/Artemis.Plugins.Games.CSGO/CsgoModule.cs b/src/Artemis.Plugins.Games.CSGO/CsgoModule.cs index 23829d5..06a7770 100644 --- a/src/Artemis.Plugins.Games.CSGO/CsgoModule.cs +++ b/src/Artemis.Plugins.Games.CSGO/CsgoModule.cs @@ -1,47 +1,36 @@ -using Artemis.Core; using Artemis.Core.Modules; using Artemis.Core.Services; using Artemis.Plugins.Games.CSGO.DataModels; using Artemis.Plugins.Games.CSGO.GameDataModels; -using Serilog; using System.Collections.Generic; - +using System.Text.Json; namespace Artemis.Plugins.Games.CSGO; -public class CsgoModule : Module +public class CsgoModule(IWebServerService webServerService) : Module { - private readonly IWebServerService _webServerService; - private readonly ILogger _logger; - private RootGameData? gameData; - - public override List ActivationRequirements { get; } = new(); + private PluginEndPoint? _endPoint; + public override List ActivationRequirements { get; } = [new ProcessActivationRequirement("cs2")]; - public CsgoModule(IWebServerService webServerService, ILogger logger) + private readonly JsonSerializerOptions _jsonSerializerOptions = new() { - _webServerService = webServerService; - _logger = logger; - } + TypeInfoResolverChain = { JsonSourceContext.Default } + }; public override void ModuleActivated(bool isOverride) { - + //unused } public override void ModuleDeactivated(bool isOverride) { - + //unused } public override void Enable() { - // _webServerService.AddStringEndPoint(this, "update", s => - // { - // _logger.Information(s); - // }); - _webServerService.AddJsonEndPoint(this, "update", newGameData => + _endPoint = webServerService.AddStringEndPoint(this, "update", newGameData => { - gameData = newGameData; - DataModel.Data = newGameData; + DataModel.Data = JsonSerializer.Deserialize(newGameData, _jsonSerializerOptions); //TODO: this is a placeholder. //RootGameData will not change, //but we should create a plugin-specific data structure @@ -53,7 +42,10 @@ public override void Enable() public override void Disable() { - + if (_endPoint != null) + { + webServerService.RemovePluginEndPoint(_endPoint); + } } public override void Update(double deltaTime) diff --git a/src/Artemis.Plugins.Games.CSGO/GameDataModels/Map.cs b/src/Artemis.Plugins.Games.CSGO/GameDataModels/Map.cs index 05bc2e9..ecc82c5 100644 --- a/src/Artemis.Plugins.Games.CSGO/GameDataModels/Map.cs +++ b/src/Artemis.Plugins.Games.CSGO/GameDataModels/Map.cs @@ -1,33 +1,33 @@ -using Newtonsoft.Json; +using System.Text.Json.Serialization; namespace Artemis.Plugins.Games.CSGO.GameDataModels; public class Map { - [JsonProperty("mode")] + [JsonPropertyName("mode")] public string? Mode { get; set; } - [JsonProperty("name")] + [JsonPropertyName("name")] public string? Name { get; set; } - [JsonProperty("phase")] + [JsonPropertyName("phase")] public string? Phase { get; set; } - [JsonProperty("round")] + [JsonPropertyName("round")] public int? Round { get; set; } - [JsonProperty("team_ct")] + [JsonPropertyName("team_ct")] public Team? TeamCt { get; set; } - [JsonProperty("team_t")] + [JsonPropertyName("team_t")] public Team? TeamT { get; set; } - [JsonProperty("num_matches_to_win_series")] + [JsonPropertyName("num_matches_to_win_series")] public int? NumMatchesToWinSeries { get; set; } - [JsonProperty("current_spectators")] + [JsonPropertyName("current_spectators")] public int? CurrentSpectators { get; set; } - [JsonProperty("souvenirs_total")] + [JsonPropertyName("souvenirs_total")] public int? SouvenirsTotal { get; set; } } diff --git a/src/Artemis.Plugins.Games.CSGO/GameDataModels/MatchStats.cs b/src/Artemis.Plugins.Games.CSGO/GameDataModels/MatchStats.cs index 2c825c5..00f3e8c 100644 --- a/src/Artemis.Plugins.Games.CSGO/GameDataModels/MatchStats.cs +++ b/src/Artemis.Plugins.Games.CSGO/GameDataModels/MatchStats.cs @@ -1,21 +1,21 @@ -using Newtonsoft.Json; +using System.Text.Json.Serialization; namespace Artemis.Plugins.Games.CSGO.GameDataModels; public class MatchStats { - [JsonProperty("kills")] + [JsonPropertyName("kills")] public int? Kills { get; set; } - [JsonProperty("assists")] + [JsonPropertyName("assists")] public int? Assists { get; set; } - [JsonProperty("deaths")] + [JsonPropertyName("deaths")] public int? Deaths { get; set; } - [JsonProperty("mvps")] + [JsonPropertyName("mvps")] public int? Mvps { get; set; } - [JsonProperty("score")] + [JsonPropertyName("score")] public int? Score { get; set; } } diff --git a/src/Artemis.Plugins.Games.CSGO/GameDataModels/Player.cs b/src/Artemis.Plugins.Games.CSGO/GameDataModels/Player.cs index 1feaa28..130f531 100644 --- a/src/Artemis.Plugins.Games.CSGO/GameDataModels/Player.cs +++ b/src/Artemis.Plugins.Games.CSGO/GameDataModels/Player.cs @@ -1,30 +1,30 @@ -using Newtonsoft.Json; +using System.Text.Json.Serialization; namespace Artemis.Plugins.Games.CSGO.GameDataModels; public class Player { - [JsonProperty("steamid")] + [JsonPropertyName("steamid")] public string? SteamId { get; set; } - [JsonProperty("name")] + [JsonPropertyName("name")] public string? Name { get; set; } - [JsonProperty("observer_slot")] + [JsonPropertyName("observer_slot")] public int? ObserverSlot { get; set; } - [JsonProperty("team")] + [JsonPropertyName("team")] public string? Team { get; set; } - [JsonProperty("activity")] + [JsonPropertyName("activity")] public string? Activity { get; set; } - [JsonProperty("match_stats")] + [JsonPropertyName("match_stats")] public MatchStats? MatchStats { get; set; } - [JsonProperty("state")] + [JsonPropertyName("state")] public State? State { get; set; } - [JsonProperty("weapons")] + [JsonPropertyName("weapons")] public Weapons? Weapons { get; set; } } diff --git a/src/Artemis.Plugins.Games.CSGO/GameDataModels/RootGameData.cs b/src/Artemis.Plugins.Games.CSGO/GameDataModels/RootGameData.cs index b51f15a..b620ec7 100644 --- a/src/Artemis.Plugins.Games.CSGO/GameDataModels/RootGameData.cs +++ b/src/Artemis.Plugins.Games.CSGO/GameDataModels/RootGameData.cs @@ -1,30 +1,25 @@ -using Newtonsoft.Json; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Text.Json.Serialization; namespace Artemis.Plugins.Games.CSGO.GameDataModels; public class RootGameData { - [JsonProperty("round")] + [JsonPropertyName("round")] public Round? Round { get; set; } - [JsonProperty("map")] + [JsonPropertyName("map")] public Map? Map { get; set; } - [JsonProperty("player")] + [JsonPropertyName("player")] public Player? Player { get; set; } - [JsonProperty("previously")] + [JsonPropertyName("previously")] public RootGameData? Previously { get; set; } //this one is weird, not exactly the same structure. //i think it's just a dictionary of bools. //if true, it got added? //probably not useful to us. - //[JsonProperty("added")] + //[JsonPropertyName("added")] //public RootGameData? Added { get; set; } } diff --git a/src/Artemis.Plugins.Games.CSGO/GameDataModels/Round.cs b/src/Artemis.Plugins.Games.CSGO/GameDataModels/Round.cs index c1828a3..9b88104 100644 --- a/src/Artemis.Plugins.Games.CSGO/GameDataModels/Round.cs +++ b/src/Artemis.Plugins.Games.CSGO/GameDataModels/Round.cs @@ -1,12 +1,12 @@ -using Newtonsoft.Json; +using System.Text.Json.Serialization; namespace Artemis.Plugins.Games.CSGO.GameDataModels; public class Round { - [JsonProperty("phase")] + [JsonPropertyName("phase")] public string? Phase { get; set; } - [JsonProperty("win_team")] + [JsonPropertyName("win_team")] public string? WinTeam { get; set; } } diff --git a/src/Artemis.Plugins.Games.CSGO/GameDataModels/State.cs b/src/Artemis.Plugins.Games.CSGO/GameDataModels/State.cs index 9bb16ca..8a9e797 100644 --- a/src/Artemis.Plugins.Games.CSGO/GameDataModels/State.cs +++ b/src/Artemis.Plugins.Games.CSGO/GameDataModels/State.cs @@ -1,39 +1,39 @@ -using Newtonsoft.Json; +using System.Text.Json.Serialization; namespace Artemis.Plugins.Games.CSGO.GameDataModels; public class State { - [JsonProperty("health")] + [JsonPropertyName("health")] public int? Health { get; set; } - [JsonProperty("armor")] + [JsonPropertyName("armor")] public int? Armor { get; set; } - [JsonProperty("helmet")] + [JsonPropertyName("helmet")] public bool? Helmet { get; set; } - [JsonProperty("defusekit")] + [JsonPropertyName("defusekit")] public bool? DefuseKit { get; set; } - [JsonProperty("flashed")] + [JsonPropertyName("flashed")] public int? Flashed { get; set; } - [JsonProperty("smoked")] + [JsonPropertyName("smoked")] public int? Smoked { get; set; } - [JsonProperty("burning")] + [JsonPropertyName("burning")] public int? Burning { get; set; } - [JsonProperty("money")] + [JsonPropertyName("money")] public int? Money { get; set; } - [JsonProperty("round_kills")] + [JsonPropertyName("round_kills")] public int? RoundKills { get; set; } - [JsonProperty("round_killhs")] + [JsonPropertyName("round_killhs")] public int? RoundKillHeadshot { get; set; } - [JsonProperty("equip_value")] + [JsonPropertyName("equip_value")] public int? EquipValue { get; set; } } diff --git a/src/Artemis.Plugins.Games.CSGO/GameDataModels/Team.cs b/src/Artemis.Plugins.Games.CSGO/GameDataModels/Team.cs index 8b23bc3..c849960 100644 --- a/src/Artemis.Plugins.Games.CSGO/GameDataModels/Team.cs +++ b/src/Artemis.Plugins.Games.CSGO/GameDataModels/Team.cs @@ -1,18 +1,18 @@ -using Newtonsoft.Json; +using System.Text.Json.Serialization; namespace Artemis.Plugins.Games.CSGO.GameDataModels; public class Team { - [JsonProperty("score")] + [JsonPropertyName("score")] public int? Score { get; set; } - [JsonProperty("consecutive_round_losses")] + [JsonPropertyName("consecutive_round_losses")] public int? ConsecutiveRoundLosses { get; set; } - [JsonProperty("timeouts_remaining")] + [JsonPropertyName("timeouts_remaining")] public int? TimeoutsRemaining { get; set; } - [JsonProperty("matches_won_this_series")] + [JsonPropertyName("matches_won_this_series")] public int? MatchesWonThisSeries { get; set; } } diff --git a/src/Artemis.Plugins.Games.CSGO/GameDataModels/Weapon.cs b/src/Artemis.Plugins.Games.CSGO/GameDataModels/Weapon.cs index 924e660..2b38e4c 100644 --- a/src/Artemis.Plugins.Games.CSGO/GameDataModels/Weapon.cs +++ b/src/Artemis.Plugins.Games.CSGO/GameDataModels/Weapon.cs @@ -1,27 +1,27 @@ -using Newtonsoft.Json; +using System.Text.Json.Serialization; namespace Artemis.Plugins.Games.CSGO.GameDataModels; public class Weapon { - [JsonProperty("name")] + [JsonPropertyName("name")] public string? Name { get; set; } - [JsonProperty("paintkit")] + [JsonPropertyName("paintkit")] public string? Paintkit { get; set; } - [JsonProperty("type")] + [JsonPropertyName("type")] public string? Type { get; set; } - [JsonProperty("ammo_clip")] + [JsonPropertyName("ammo_clip")] public int? AmmoClip { get; set; } - [JsonProperty("ammo_clip_max")] + [JsonPropertyName("ammo_clip_max")] public int? AmmoClipMax { get; set; } - [JsonProperty("ammo_reserve")] + [JsonPropertyName("ammo_reserve")] public int? AmmoReserve { get; set; } - [JsonProperty("state")] + [JsonPropertyName("state")] public string? State { get; set; } } diff --git a/src/Artemis.Plugins.Games.CSGO/GameDataModels/Weapons.cs b/src/Artemis.Plugins.Games.CSGO/GameDataModels/Weapons.cs index 86a8b7f..ada089c 100644 --- a/src/Artemis.Plugins.Games.CSGO/GameDataModels/Weapons.cs +++ b/src/Artemis.Plugins.Games.CSGO/GameDataModels/Weapons.cs @@ -1,18 +1,18 @@ -using Newtonsoft.Json; +using System.Text.Json.Serialization; namespace Artemis.Plugins.Games.CSGO.GameDataModels; public class Weapons { - [JsonProperty("weapon_0")] + [JsonPropertyName("weapon_0")] public Weapon? Weapon0 { get; set; } - [JsonProperty("weapon_1")] + [JsonPropertyName("weapon_1")] public Weapon? Weapon1 { get; set; } - [JsonProperty("weapon_2")] + [JsonPropertyName("weapon_2")] public Weapon? Weapon2 { get; set; } - [JsonProperty("weapon_3")] + [JsonPropertyName("weapon_3")] public Weapon? Weapon3 { get; set; } } \ No newline at end of file diff --git a/src/Artemis.Plugins.Games.CSGO/JsonSourceContext.cs b/src/Artemis.Plugins.Games.CSGO/JsonSourceContext.cs new file mode 100644 index 0000000..883a68c --- /dev/null +++ b/src/Artemis.Plugins.Games.CSGO/JsonSourceContext.cs @@ -0,0 +1,7 @@ +using System.Text.Json.Serialization; +using Artemis.Plugins.Games.CSGO.GameDataModels; + +namespace Artemis.Plugins.Games.CSGO; + +[JsonSerializable(typeof(RootGameData))] +internal partial class JsonSourceContext : JsonSerializerContext;