-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changes: - Refactor source generator for incremental processing. - Add multi-reward support to assignments ( fixes #109 ). - Add header validation for X-Super-Client and X-Super-Contact in RateLimitMiddleware ( fixes #94 ). General: - Update package dependencies. - Update helldivers-2/json. - Format code with dotnet format. - Remove unused package reference.
- Loading branch information
Showing
19 changed files
with
324 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule json
updated
10 files
+111 −1 | items/armor/armor.json | |
+4 −0 | items/armor/passive.json | |
+5 −1 | items/boosters.json | |
+99 −7 | items/item_names.json | |
+4 −1 | items/weapons/fire_modes.json | |
+5 −5 | items/weapons/grenades.json | |
+67 −16 | items/weapons/primary.json | |
+23 −6 | items/weapons/secondary.json | |
+5 −0 | warbonds.json | |
+97 −0 | warbonds/truth_enforcers.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Text; | ||
|
||
namespace Helldivers.SourceGen.Contracts; | ||
|
||
/// <summary> | ||
/// Interface for parsing JSON strings and generating corresponding C# source code. | ||
/// </summary> | ||
public interface IJsonParser | ||
{ | ||
/// <summary> | ||
/// Parses the given source text and generates corresponding C# source code. | ||
/// </summary> | ||
/// <param name="file">The source file to be parsed.</param> | ||
/// <param name="cancellationToken">A <see cref="CancellationToken" /> for cancelling the parse process.</param> | ||
/// <returns>A <see cref="SourceText"/> object representing the generated C# source code.</returns> | ||
SourceText Parse(AdditionalText file, CancellationToken cancellationToken = default); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using Helldivers.SourceGen.Contracts; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Text; | ||
using System.Text; | ||
|
||
namespace Helldivers.SourceGen.Parsers; | ||
|
||
/// <summary> | ||
/// An abstract base class for parsing JSON data and generating corresponding C# source code. | ||
/// </summary> | ||
public abstract class BaseJsonParser : IJsonParser | ||
{ | ||
private const string TEMPLATE = @"// <auto-generated /> | ||
#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; | ||
public static partial class Static | ||
{{ | ||
/// <summary>Public list of {0} entries from {1}</summary> | ||
public static {2} {0} = {3}; | ||
}}"; | ||
|
||
/// <inheritdoc /> | ||
public SourceText Parse(AdditionalText file, CancellationToken cancellationToken = default) | ||
{ | ||
var filename = Path.GetFileName(file.Path); | ||
var json = file.GetText(cancellationToken)?.ToString(); | ||
|
||
var name = Path.GetFileNameWithoutExtension(file.Path); | ||
name = $"{char.ToUpper(name[0])}{name.Substring(1)}"; | ||
|
||
if (string.IsNullOrWhiteSpace(json) is false) | ||
{ | ||
var (type, csharp) = Parse(json!); | ||
|
||
var output = string.Format(TEMPLATE, name, filename, type, csharp); | ||
return SourceText.From(output, Encoding.UTF8); | ||
} | ||
|
||
return SourceText.From("// Could not read JSON file"); | ||
} | ||
|
||
/// <summary> | ||
/// Convert the JSON string into C# code that can be injected. | ||
/// </summary> | ||
protected abstract (string Type, string Source) Parse(string json); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.Text; | ||
using System.Text.Json; | ||
|
||
namespace Helldivers.SourceGen.Parsers; | ||
|
||
/// <summary> | ||
/// BiomesParser is responsible for parsing a JSON string representation of biomes. | ||
/// </summary> | ||
public class BiomesParser : BaseJsonParser | ||
{ | ||
/// <inheritdoc /> | ||
protected override (string Type, string Source) Parse(string json) | ||
{ | ||
var builder = new StringBuilder("new Dictionary<string, Helldivers.Models.V1.Planets.Biome>()\n\t{\n"); | ||
var entries = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, string>>>(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<string, Helldivers.Models.V1.Planets.Biome>", builder.ToString()); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Helldivers-2-SourceGen/Parsers/EnvironmentalsParser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System.Text; | ||
using System.Text.Json; | ||
|
||
namespace Helldivers.SourceGen.Parsers; | ||
|
||
/// <summary> | ||
/// The EnvironmentalsParser class is responsible for parsing JSON strings | ||
/// representing environmental hazards and converting them into C# source code. | ||
/// </summary> | ||
public class EnvironmentalsParser : BaseJsonParser | ||
{ | ||
/// <inheritdoc /> | ||
protected override (string Type, string Source) Parse(string json) | ||
{ | ||
var builder = new StringBuilder("new Dictionary<string, Helldivers.Models.V1.Planets.Hazard>()\n\t{\n"); | ||
var entries = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, string>>>(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<string, Helldivers.Models.V1.Planets.Hazard>", builder.ToString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.Text; | ||
using System.Text.Json; | ||
|
||
namespace Helldivers.SourceGen.Parsers; | ||
|
||
/// <summary> | ||
/// Parses JSON data of factions and generates the corresponding C# source code representation. | ||
/// </summary> | ||
public class FactionsParser : BaseJsonParser | ||
{ | ||
/// <inheritdoc /> | ||
protected override (string Type, string Source) Parse(string json) | ||
{ | ||
var builder = new StringBuilder("new Dictionary<int, string>()\n\t{\n"); | ||
var entries = JsonSerializer.Deserialize<Dictionary<string, string>>(json)!; | ||
foreach (var pair in entries) | ||
builder.AppendLine($@"{'\t'}{'\t'}{{ {pair.Key}, ""{pair.Value}"" }},"); | ||
|
||
builder.Append("\t}"); | ||
return ("IReadOnlyDictionary<int, string>", builder.ToString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System.Text; | ||
using System.Text.Json; | ||
|
||
namespace Helldivers.SourceGen.Parsers; | ||
|
||
/// <summary> | ||
/// Handles parsing the planets.json and generating the resulting planet data. | ||
/// </summary> | ||
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 document = JsonDocument.Parse(json); | ||
foreach (var property in document.RootElement.EnumerateObject()) | ||
{ | ||
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<string, string>(""{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()); | ||
} | ||
} |
Oops, something went wrong.