Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change KeyValuePairs in generated code to primitive arrays #125

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}