Skip to content

Commit

Permalink
migrate cs2 to System.Text.Json
Browse files Browse the repository at this point in the history
  • Loading branch information
Aytackydln authored and diogotr7 committed Mar 14, 2024
1 parent 106e3b3 commit 74bc84a
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ArtemisRGB.UI.Shared" IncludeAssets="compile;build;buildTransitive" Version="1.2024.303.4" />
<PackageReference Include="ArtemisRGB.UI.Shared" IncludeAssets="compile;build;buildTransitive" Version="1.2024.313.2" />
<PackageReference Include="ArtemisRGB.GameFinder" Version="2.3.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

<ItemGroup>
Expand Down
38 changes: 15 additions & 23 deletions src/Artemis.Plugins.Games.CSGO/CsgoModule.cs
Original file line number Diff line number Diff line change
@@ -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<CsgoDataModel>
public class CsgoModule(IWebServerService webServerService) : Module<CsgoDataModel>
{
private readonly IWebServerService _webServerService;
private readonly ILogger _logger;
private RootGameData? gameData;

public override List<IModuleActivationRequirement> ActivationRequirements { get; } = new();
private PluginEndPoint? _endPoint;
public override List<IModuleActivationRequirement> 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<RootGameData>(this, "update", newGameData =>
_endPoint = webServerService.AddStringEndPoint(this, "update", newGameData =>
{
gameData = newGameData;
DataModel.Data = newGameData;
DataModel.Data = JsonSerializer.Deserialize<RootGameData>(newGameData, _jsonSerializerOptions);
//TODO: this is a placeholder.
//RootGameData will not change,
//but we should create a plugin-specific data structure
Expand All @@ -53,7 +42,10 @@ public override void Enable()

public override void Disable()
{

if (_endPoint != null)
{
webServerService.RemovePluginEndPoint(_endPoint);
}
}

public override void Update(double deltaTime)
Expand Down
20 changes: 10 additions & 10 deletions src/Artemis.Plugins.Games.CSGO/GameDataModels/Map.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
12 changes: 6 additions & 6 deletions src/Artemis.Plugins.Games.CSGO/GameDataModels/MatchStats.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
18 changes: 9 additions & 9 deletions src/Artemis.Plugins.Games.CSGO/GameDataModels/Player.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
17 changes: 6 additions & 11 deletions src/Artemis.Plugins.Games.CSGO/GameDataModels/RootGameData.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
6 changes: 3 additions & 3 deletions src/Artemis.Plugins.Games.CSGO/GameDataModels/Round.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
24 changes: 12 additions & 12 deletions src/Artemis.Plugins.Games.CSGO/GameDataModels/State.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
10 changes: 5 additions & 5 deletions src/Artemis.Plugins.Games.CSGO/GameDataModels/Team.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
16 changes: 8 additions & 8 deletions src/Artemis.Plugins.Games.CSGO/GameDataModels/Weapon.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
10 changes: 5 additions & 5 deletions src/Artemis.Plugins.Games.CSGO/GameDataModels/Weapons.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
7 changes: 7 additions & 0 deletions src/Artemis.Plugins.Games.CSGO/JsonSourceContext.cs
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 74bc84a

Please sign in to comment.