28 lines
897 B
C#
28 lines
897 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(string relativePath)
|
|
{
|
|
var scenarioPath = Path.Combine(staticDataOptions.Value.DataRoot, relativePath);
|
|
if (!File.Exists(scenarioPath))
|
|
{
|
|
throw new FileNotFoundException($"Scenario file was not found: {relativePath}", scenarioPath);
|
|
}
|
|
|
|
var json = File.ReadAllText(scenarioPath);
|
|
return JsonSerializer.Deserialize<ScenarioDefinition>(json, _jsonOptions)
|
|
?? throw new InvalidOperationException($"Unable to read {relativePath}.");
|
|
}
|
|
}
|