From 4a9661010be48eba054a7b56088bb505929df379 Mon Sep 17 00:00:00 2001 From: Wannes Gennar Date: Sat, 16 Nov 2024 00:48:52 +0100 Subject: [PATCH] change KeyValuePairs in generated code to primitive arrays (#125) fix #123 --- .../Domain/Localization/LocalizedMessage.cs | 25 +++++++++++++++++++ .../Parsers/PlanetsParser.cs | 6 ++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/Helldivers-2-Models/Domain/Localization/LocalizedMessage.cs b/src/Helldivers-2-Models/Domain/Localization/LocalizedMessage.cs index 7f09fa8..0d055ce 100644 --- a/src/Helldivers-2-Models/Domain/Localization/LocalizedMessage.cs +++ b/src/Helldivers-2-Models/Domain/Localization/LocalizedMessage.cs @@ -44,4 +44,29 @@ public static LocalizedMessage FromStrings(IEnumerable + /// Factory method for creating a from a list of {language, value} pairs. + /// + public static LocalizedMessage FromStrings(IEnumerable values) + { + var messages = values.SelectMany(pair => + { + var key = pair[0]; + var value = pair[1]; + + var culture = new CultureInfo(key); + var parent = culture.Parent; + + return new[] + { + new KeyValuePair(culture, value), + new KeyValuePair(parent, value) + }; + }) + .DistinctBy(pair => pair.Key) + .ToDictionary(pair => pair.Key, pair => pair.Value); + + return new LocalizedMessage(messages); + } } diff --git a/src/Helldivers-2-SourceGen/Parsers/PlanetsParser.cs b/src/Helldivers-2-SourceGen/Parsers/PlanetsParser.cs index c3248e3..3049a0c 100644 --- a/src/Helldivers-2-SourceGen/Parsers/PlanetsParser.cs +++ b/src/Helldivers-2-SourceGen/Parsers/PlanetsParser.cs @@ -11,7 +11,7 @@ public class PlanetsParser : BaseJsonParser /// protected override (string Type, string Source) Parse(string json) { - var builder = new StringBuilder("new Dictionary Environmentals)>()\n\t{\n"); + var builder = new StringBuilder("new Dictionary()\n\t{\n"); var document = JsonDocument.Parse(json); foreach (var property in document.RootElement.EnumerateObject()) { @@ -31,10 +31,10 @@ protected override (string Type, string Source) Parse(string json) .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.AppendLine($@"{'\t'}{'\t'}{{ {index}, (LocalizedMessage.FromStrings([{string.Join(", ", names.Select(pair => $@"[""{pair.Key}"", ""{pair.Value}""]"))}]), ""{sector}"", ""{biome}"", [{string.Join(", ", environmentals)}]) }},"); } builder.Append("\t}"); - return ("IReadOnlyDictionary Environmentals)>", builder.ToString()); + return ("IReadOnlyDictionary", builder.ToString()); } }