Refactor world bootstrap and allow empty startup worlds
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
using FastEndpoints;
|
||||
using FastEndpoints.Swagger;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SpaceGame.Api.Universe.Scenario;
|
||||
using SpaceGame.Api.Universe.Bootstrap;
|
||||
using SpaceGame.Api.Universe.Simulation;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
const string StartupScenarioPath = "scenarios/empty.json";
|
||||
|
||||
builder.Services.AddCors((options) =>
|
||||
{
|
||||
@@ -17,21 +18,48 @@ builder.Services.AddCors((options) =>
|
||||
});
|
||||
});
|
||||
builder.Services
|
||||
.AddOptions<StaticDataOptions>()
|
||||
.Bind(builder.Configuration.GetSection("StaticData"))
|
||||
.Validate(options => !string.IsNullOrWhiteSpace(options.DataRoot), "StaticData:DataRoot must be configured.")
|
||||
.ValidateOnStart();
|
||||
.AddOptions<StaticDataOptions>()
|
||||
.Bind(builder.Configuration.GetSection("StaticData"))
|
||||
.Validate(options => !string.IsNullOrWhiteSpace(options.DataRoot), "StaticData:DataRoot must be configured.")
|
||||
.PostConfigure(options =>
|
||||
{
|
||||
if (Path.IsPathRooted(options.DataRoot))
|
||||
{
|
||||
options.DataRoot = Path.GetFullPath(options.DataRoot);
|
||||
return;
|
||||
}
|
||||
|
||||
var candidatePaths = new[]
|
||||
{
|
||||
Path.GetFullPath(options.DataRoot),
|
||||
Path.GetFullPath(Path.Combine(builder.Environment.ContentRootPath, options.DataRoot)),
|
||||
Path.GetFullPath(Path.Combine(builder.Environment.ContentRootPath, "..", "..", options.DataRoot)),
|
||||
};
|
||||
|
||||
var resolvedPath = candidatePaths.FirstOrDefault(Directory.Exists);
|
||||
if (resolvedPath is null)
|
||||
{
|
||||
throw new InvalidOperationException($"StaticData:DataRoot '{options.DataRoot}' could not be resolved to an existing directory.");
|
||||
}
|
||||
|
||||
options.DataRoot = resolvedPath;
|
||||
})
|
||||
.ValidateOnStart();
|
||||
builder.Services.Configure<BalanceOptions>(builder.Configuration.GetSection("Balance"));
|
||||
builder.Services.Configure<WorldGenerationOptions>(builder.Configuration.GetSection("WorldGeneration"));
|
||||
builder.Services.Configure<OrbitalSimulationOptions>(builder.Configuration.GetSection("OrbitalSimulation"));
|
||||
|
||||
builder.Services.AddSingleton<IBalanceService, BalanceService>();
|
||||
builder.Services.AddTransient<SystemGenerationService>();
|
||||
builder.Services.AddTransient<SpatialBuilder>();
|
||||
builder.Services.AddTransient<WorldSeedingService>();
|
||||
builder.Services.AddTransient<ScenarioValidationService>();
|
||||
builder.Services.AddTransient<ScenarioContentBuilder>();
|
||||
builder.Services.AddTransient<ScenarioLoader>();
|
||||
builder.Services.AddTransient<SystemTemplateLoader>();
|
||||
builder.Services.AddTransient<WorldTopologyBuilder>();
|
||||
builder.Services.AddTransient<WorldRuntimeAssembler>();
|
||||
builder.Services.AddTransient<WorldBuilder>();
|
||||
builder.Services.AddSingleton<WorldBootstrapper>();
|
||||
builder.Services.AddSingleton<IStaticDataProvider, StaticDataProvider>();
|
||||
builder.Services.AddSingleton<WorldService>();
|
||||
builder.Services.AddSingleton<TelemetryService>();
|
||||
builder.Services.AddHostedService<SimulationHostedService>();
|
||||
@@ -40,6 +68,7 @@ builder.Services.AddFastEndpoints();
|
||||
builder.Services.SwaggerDocument();
|
||||
|
||||
var app = builder.Build();
|
||||
app.Services.GetRequiredService<WorldService>().LoadFromScenario(StartupScenarioPath);
|
||||
|
||||
app.UseCors();
|
||||
app.UseFastEndpoints();
|
||||
|
||||
Reference in New Issue
Block a user