28 lines
798 B
C#
28 lines
798 B
C#
|
|
using System.Text.Json;
|
|
using Microsoft.Extensions.Options;
|
|
using SpaceGame.Api.Universe.Bootstrap;
|
|
|
|
namespace SpaceGame.Api.Universe.Scenario;
|
|
|
|
public sealed class ScenarioLoader(IOptions<StaticDataOptions> staticDataOptions)
|
|
{
|
|
private readonly JsonSerializerOptions _jsonOptions = new()
|
|
{
|
|
PropertyNameCaseInsensitive = true,
|
|
};
|
|
|
|
public ScenarioDefinition? Load()
|
|
{
|
|
var scenarioPath = Path.Combine(staticDataOptions.Value.DataRoot, "scenario.json");
|
|
if (!File.Exists(scenarioPath))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var json = File.ReadAllText(scenarioPath);
|
|
return JsonSerializer.Deserialize<ScenarioDefinition>(json, _jsonOptions)
|
|
?? throw new InvalidOperationException("Unable to read scenario.json.");
|
|
}
|
|
}
|