From 5c9720dd6c2cf1b1137f8a0a7e4b3afb65539cd7 Mon Sep 17 00:00:00 2001 From: Wannes Gennar Date: Fri, 12 Apr 2024 15:33:05 +0200 Subject: [PATCH] update to latest helldivers-2/json module (#69) * update to latest helldivers-2/json module - update source generator to work with new JSON format - add environmental and biome data to planets - made planet names translatable * make `json` folder invisible in project again --- .../Mapping/V1/PlanetMapper.cs | 10 ++- .../Helldivers-2-Models.csproj | 7 +- src/Helldivers-2-Models/V1/Planet.cs | 9 ++- src/Helldivers-2-Models/V1/Planets/Biome.cs | 11 ++++ src/Helldivers-2-Models/V1/Planets/Hazard.cs | 11 ++++ src/Helldivers-2-Models/json | 2 +- .../StaticJsonSourceGenerator.cs | 64 +++++++++++++++---- 7 files changed, 91 insertions(+), 23 deletions(-) create mode 100644 src/Helldivers-2-Models/V1/Planets/Biome.cs create mode 100644 src/Helldivers-2-Models/V1/Planets/Hazard.cs diff --git a/src/Helldivers-2-Core/Mapping/V1/PlanetMapper.cs b/src/Helldivers-2-Core/Mapping/V1/PlanetMapper.cs index 4908f47..723f563 100644 --- a/src/Helldivers-2-Core/Mapping/V1/PlanetMapper.cs +++ b/src/Helldivers-2-Core/Mapping/V1/PlanetMapper.cs @@ -38,14 +38,18 @@ public IEnumerable MapToV1(WarInfo warInfo, WarStatus warStatus, WarSumm /// public Planet MapToV1(PlanetInfo info, PlanetStatus status, PlanetEvent? @event, PlanetStats? stats, List attacking) { - Static.Planets.TryGetValue(info.Index, out var name); + Static.Planets.TryGetValue(info.Index, out var planet); Static.Factions.TryGetValue(info.InitialOwner, out var initialOwner); Static.Factions.TryGetValue(status.Owner, out var currentOwner); + var (name, sector, biomeKey, environmentals) = planet; + return new Planet( Index: info.Index, - Name: name ?? string.Empty, - Sector: Static.Sectors.First(sector => sector.Value.Contains(info.Index)).Key, + Name: name, + Sector: sector, + Biome: Static.Biomes[biomeKey], + Hazards: environmentals.Select(environmental => Static.Environmentals[environmental]).ToList(), Hash: info.SettingsHash, Position: new Position(info.Position.X, info.Position.Y), Waypoints: info.Waypoints.ToList(), diff --git a/src/Helldivers-2-Models/Helldivers-2-Models.csproj b/src/Helldivers-2-Models/Helldivers-2-Models.csproj index 541b6e2..82ce6ec 100644 --- a/src/Helldivers-2-Models/Helldivers-2-Models.csproj +++ b/src/Helldivers-2-Models/Helldivers-2-Models.csproj @@ -6,10 +6,11 @@ - + - - + + + diff --git a/src/Helldivers-2-Models/V1/Planet.cs b/src/Helldivers-2-Models/V1/Planet.cs index bd9f3dc..c13e730 100644 --- a/src/Helldivers-2-Models/V1/Planet.cs +++ b/src/Helldivers-2-Models/V1/Planet.cs @@ -1,4 +1,5 @@ -using Helldivers.Models.V1.Planets; +using Helldivers.Models.Domain.Localization; +using Helldivers.Models.V1.Planets; namespace Helldivers.Models.V1; @@ -8,6 +9,8 @@ namespace Helldivers.Models.V1; /// The unique identifier ArrowHead assigned to this planet. /// The name of the planet, as shown in game. /// The name of the sector the planet is in, as shown in game. +/// The biome this planet has. +/// All s that are applicable to this planet. /// A hash assigned to the planet by ArrowHead, purpose unknown. /// The coordinates of this planet on the galactic war map. /// A list of of all the planets to which this planet is connected. @@ -22,8 +25,10 @@ namespace Helldivers.Models.V1; /// A list of integers that this planet is currently attacking. public record Planet( int Index, - string Name, + LocalizedMessage Name, string Sector, + Biome Biome, + List Hazards, long Hash, Position Position, List Waypoints, diff --git a/src/Helldivers-2-Models/V1/Planets/Biome.cs b/src/Helldivers-2-Models/V1/Planets/Biome.cs new file mode 100644 index 0000000..43926eb --- /dev/null +++ b/src/Helldivers-2-Models/V1/Planets/Biome.cs @@ -0,0 +1,11 @@ +namespace Helldivers.Models.V1.Planets; + +/// +/// Represents information about a biome of a planet. +/// +/// The name of this biome. +/// A human-readable description of the biome. +public sealed record Biome( + string Name, + string Description +); diff --git a/src/Helldivers-2-Models/V1/Planets/Hazard.cs b/src/Helldivers-2-Models/V1/Planets/Hazard.cs new file mode 100644 index 0000000..b0cba41 --- /dev/null +++ b/src/Helldivers-2-Models/V1/Planets/Hazard.cs @@ -0,0 +1,11 @@ +namespace Helldivers.Models.V1.Planets; + +/// +/// Describes an environmental hazard that can be present on a . +/// +/// The name of this environmental hazard. +/// The description of the environmental hazard. +public sealed record Hazard( + string Name, + string Description +); diff --git a/src/Helldivers-2-Models/json b/src/Helldivers-2-Models/json index 982fa0b..9721162 160000 --- a/src/Helldivers-2-Models/json +++ b/src/Helldivers-2-Models/json @@ -1 +1 @@ -Subproject commit 982fa0b0d090f7d57b860e07655591864d5334cd +Subproject commit 97211622e6b03428c95b3a60ca6c30285643c714 diff --git a/src/Helldivers-2-SourceGen/StaticJsonSourceGenerator.cs b/src/Helldivers-2-SourceGen/StaticJsonSourceGenerator.cs index 46ee0bc..feca41f 100644 --- a/src/Helldivers-2-SourceGen/StaticJsonSourceGenerator.cs +++ b/src/Helldivers-2-SourceGen/StaticJsonSourceGenerator.cs @@ -34,15 +34,17 @@ public void Execute(GeneratorExecutionContext context) var (type, value) = name.ToLowerInvariant() switch { - "planets" => ParseNumericDictionary(json), - "factions" => ParseNumericDictionary(json), - "sectors" => ParseNumericListDictionary(json), + "planets" => ParsePlanetsDictionary(json), + "biomes" => ParseBiomesDictionary(json), + "environmentals" => ParseEnvironmentalsDictionary(json), + "factions" => ParseFactionsDictionary(json), _ => throw new Exception($"Generator does not know how to parse {name}") }; var source = $@"// #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member using global::System.Collections.Generic; +using global::Helldivers.Models.Domain.Localization; namespace Helldivers.Models; @@ -78,7 +80,7 @@ public static partial class Static /// /// Parses a JSON file that's an object where keys are numerical and values are names (strings). /// - private (string Type, string Source) ParseNumericDictionary(string json) + private (string Type, string Source) ParseFactionsDictionary(string json) { var builder = new StringBuilder("new Dictionary()\n\t{\n"); var entries = JsonSerializer.Deserialize>(json)!; @@ -89,20 +91,54 @@ public static partial class Static return ("IReadOnlyDictionary", builder.ToString()); } - /// - /// Parses a JSON file that's an object where keys are strings and values are lists of integers. - /// - private (string Type, string Source) ParseNumericListDictionary(string json) + private (string Type, string Source) ParsePlanetsDictionary(string json) { - var builder = new StringBuilder("new Dictionary>()\n\t{\n"); - var entries = JsonSerializer.Deserialize>>(json, new JsonSerializerOptions())!; - foreach (var pair in entries) + var builder = new StringBuilder("new Dictionary Environmentals)>()\n\t{\n"); + var document = JsonDocument.Parse(json); + foreach (var property in document.RootElement.EnumerateObject()) { - var values = string.Join(", ", pair.Value); - builder.AppendLine($@"{'\t'}{'\t'}{{ ""{pair.Key}"", [{values}] }},"); + var index = property.Name; + var name = property.Value.GetProperty("name").GetString(); + var names = property + .Value + .GetProperty("names") + .EnumerateObject() + .ToDictionary(prop => prop.Name, prop => prop.Value.GetString()!); + var sector = property.Value.GetProperty("sector").GetString(); + var biome = property.Value.GetProperty("biome").GetString(); + var environmentals = property + .Value + .GetProperty("environmentals") + .EnumerateArray() + .Select(prop => $@"""{prop.GetString()!}""") + .ToList(); + + builder.AppendLine($@"{'\t'}{'\t'}{{ {index}, (LocalizedMessage.FromStrings([{string.Join(", ", names.Select(pair => $@"new KeyValuePair(""{pair.Key}"", ""{pair.Value}"")"))}]), ""{sector}"", ""{biome}"", [{string.Join(", ", environmentals)}]) }},"); } builder.Append("\t}"); - return ("IReadOnlyDictionary>", builder.ToString()); + return ("IReadOnlyDictionary Environmentals)>", builder.ToString()); + } + + private (string Type, string Source) ParseBiomesDictionary(string json) + { + var builder = new StringBuilder("new Dictionary()\n\t{\n"); + var entries = JsonSerializer.Deserialize>>(json)!; + foreach (var pair in entries) + builder.AppendLine($@"{'\t'}{'\t'}{{ ""{pair.Key}"", new Helldivers.Models.V1.Planets.Biome(""{pair.Value["name"]}"", ""{pair.Value["description"]}"") }},"); + + builder.Append("\t}"); + return ("IReadOnlyDictionary", builder.ToString()); + } + + private (string Type, string Source) ParseEnvironmentalsDictionary(string json) + { + var builder = new StringBuilder("new Dictionary()\n\t{\n"); + var entries = JsonSerializer.Deserialize>>(json)!; + foreach (var pair in entries) + builder.AppendLine($@"{'\t'}{'\t'}{{ ""{pair.Key}"", new Helldivers.Models.V1.Planets.Hazard(""{pair.Value["name"]}"", ""{pair.Value["description"]}"") }},"); + + builder.Append("\t}"); + return ("IReadOnlyDictionary", builder.ToString()); } }