Skip to content

Commit

Permalink
change KeyValuePairs in generated code to primitive arrays (#125)
Browse files Browse the repository at this point in the history
fix #123
  • Loading branch information
dealloc authored Nov 15, 2024
1 parent ce4ea75 commit 4a96610
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
25 changes: 25 additions & 0 deletions src/Helldivers-2-Models/Domain/Localization/LocalizedMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,29 @@ public static LocalizedMessage FromStrings(IEnumerable<KeyValuePair<string, stri

return new LocalizedMessage(messages);
}

/// <summary>
/// Factory method for creating a <see cref="LocalizedMessage" /> from a list of {language, value} pairs.
/// </summary>
public static LocalizedMessage FromStrings(IEnumerable<string[]> 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<CultureInfo, string>(culture, value),
new KeyValuePair<CultureInfo, string>(parent, value)
};
})
.DistinctBy(pair => pair.Key)
.ToDictionary(pair => pair.Key, pair => pair.Value);

return new LocalizedMessage(messages);
}
}
6 changes: 3 additions & 3 deletions src/Helldivers-2-SourceGen/Parsers/PlanetsParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class PlanetsParser : BaseJsonParser
/// <inheritdoc />
protected override (string Type, string Source) Parse(string json)
{
var builder = new StringBuilder("new Dictionary<int, (LocalizedMessage Name, string Sector, string Biome, List<string> Environmentals)>()\n\t{\n");
var builder = new StringBuilder("new Dictionary<int, (LocalizedMessage Name, string Sector, string Biome, string[] Environmentals)>()\n\t{\n");
var document = JsonDocument.Parse(json);
foreach (var property in document.RootElement.EnumerateObject())
{
Expand All @@ -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<string, string>(""{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<int, (LocalizedMessage Name, string Sector, string Biome, List<string> Environmentals)>", builder.ToString());
return ("IReadOnlyDictionary<int, (LocalizedMessage Name, string Sector, string Biome, string[] Environmentals)>", builder.ToString());
}
}

0 comments on commit 4a96610

Please sign in to comment.