using System; using System.IO; using System.Threading.Tasks; using Aetheria.State.Documents; using GameCult.Caching; using GameCult.Caching.MessagePack; using GameCult.Mesh; using GameCult.Networking; namespace Aetheria.State; public sealed class AetheriaStateNode : IAsyncDisposable, IDisposable { private readonly CultMeshNode _node; private AetheriaStateNode(string statePath, CultMeshNode node) { StatePath = statePath; _node = node; } public string StatePath { get; } public CultCache Cache => _node.Cache; public CultNetDatabase Database => _node.Database; public static async Task OpenAsync( string statePath, string runtimeId = "aetheria-local", bool startServer = false) { if (string.IsNullOrWhiteSpace(statePath)) { throw new ArgumentException("State path must be non-empty.", nameof(statePath)); } Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(statePath)) ?? "."); var cacheRegistry = AetheriaDocumentRegistry.CreateCultCacheRegistry(); var node = await CultMesh.CreateNodeAsync( statePath, new CultMeshNodeOptions { StartServer = startServer, EnableDurableShardLogs = true, CacheOptions = new CultCacheOpenOptions { Registry = cacheRegistry, PullOnOpen = true, StoreFlushOnDispose = true, UseDirectoryStore = true }, DatabaseOptions = new CultNetDatabaseOptions { RuntimeId = runtimeId, DocumentRegistry = AetheriaDocumentRegistry.CreateCultNetRegistry(cacheRegistry) } }).ConfigureAwait(false); return new AetheriaStateNode(statePath, node); } public Task> PutWorldAsync(AetheriaWorldState world) { return Database.PutAsync(new CultRecordKey("global:aetheria.world_state.v1"), world); } public Task GetWorldAsync() { return Database.GetAsync(new CultRecordKey("global:aetheria.world_state.v1")); } public Task> PutMigrationLedgerAsync(AetheriaMigrationLedger ledger) { return Database.PutAsync(new CultRecordKey("global:aetheria.migration_ledger.v1"), ledger); } public Task GetMigrationLedgerAsync() { return Database.GetAsync(new CultRecordKey("global:aetheria.migration_ledger.v1")); } public Task> PutLegacyCatalogQuarantineAsync( AetheriaLegacyCatalogQuarantine quarantine) { return Database.PutAsync(new CultRecordKey("global:aetheria.legacy_catalog_quarantine.v1"), quarantine); } public Task GetLegacyCatalogQuarantineAsync() { return Database.GetAsync( new CultRecordKey("global:aetheria.legacy_catalog_quarantine.v1")); } public Task> PutItemDefinitionAsync( CultRecordKey key, AetheriaItemDefinition item) { return Database.PutAsync(key, item); } public Task> PutLegacyItemDefinitionAsync(AetheriaItemDefinition item) { return PutItemDefinitionAsync(AetheriaCatalogKeys.ItemDefinitionFromLegacyId(item.LegacyId), item); } public Task GetItemDefinitionAsync(CultRecordKey key) { return Database.GetAsync(key); } public Task GetItemDefinitionByLegacyIdAsync(string legacyId) { return GetItemDefinitionAsync(AetheriaCatalogKeys.ItemDefinitionFromLegacyId(legacyId)); } public Task> PutCorporationAsync(CultRecordKey key, AetheriaCorporation corporation) { return Database.PutAsync(key, corporation); } public Task> PutLegacyCorporationAsync(AetheriaCorporation corporation) { return PutCorporationAsync(AetheriaCatalogKeys.CorporationFromLegacyId(corporation.LegacyId), corporation); } public Task GetCorporationAsync(CultRecordKey key) { return Database.GetAsync(key); } public Task GetCorporationByLegacyIdAsync(string legacyId) { return GetCorporationAsync(AetheriaCatalogKeys.CorporationFromLegacyId(legacyId)); } public Task> PutNameFileAsync(CultRecordKey key, AetheriaNameFile nameFile) { return Database.PutAsync(key, nameFile); } public Task> PutLegacyNameFileAsync(AetheriaNameFile nameFile) { return PutNameFileAsync(AetheriaCatalogKeys.NameFileFromLegacyId(nameFile.LegacyId), nameFile); } public Task GetNameFileAsync(CultRecordKey key) { return Database.GetAsync(key); } public Task GetNameFileByLegacyIdAsync(string legacyId) { return GetNameFileAsync(AetheriaCatalogKeys.NameFileFromLegacyId(legacyId)); } public AetheriaCatalogSnapshot ReadCatalogSnapshot() { return new AetheriaCatalogSnapshot( Cache.GetAll(), Cache.GetAll(), Cache.GetAll()); } public Task> PutCatalogSurfaceAsync(EveSurfaceState surface) { return Database.PutAsync(new CultRecordKey(AetheriaCatalogSurfaceProjector.SurfaceKey), surface); } public Task GetCatalogSurfaceAsync() { return Database.GetAsync(new CultRecordKey(AetheriaCatalogSurfaceProjector.SurfaceKey)); } public Task> PutOperationsSurfaceAsync(EveSurfaceState surface) { return Database.PutAsync(new CultRecordKey(AetheriaOperationsSurfaceProjector.SurfaceKey), surface); } public Task GetOperationsSurfaceAsync() { return Database.GetAsync(new CultRecordKey(AetheriaOperationsSurfaceProjector.SurfaceKey)); } public Task> PutProviderAdvertisementAsync( EveProviderAdvertisementState advertisement) { return Database.PutAsync(new CultRecordKey(AetheriaProviderAdvertisementProjector.AdvertisementKey), advertisement); } public Task GetProviderAdvertisementAsync() { return Database.GetAsync( new CultRecordKey(AetheriaProviderAdvertisementProjector.AdvertisementKey)); } public Task> PutPlayerSettingsAsync(AetheriaPlayerSettings settings) { return Database.PutAsync(new CultRecordKey("global:aetheria.player_settings.v1"), settings); } public Task GetPlayerSettingsAsync() { return Database.GetAsync(new CultRecordKey("global:aetheria.player_settings.v1")); } public Task> PutLoadoutTemplateAsync( CultRecordKey key, AetheriaLoadoutTemplate loadout) { return Database.PutAsync(key, loadout); } public Task GetLoadoutTemplateAsync(CultRecordKey key) { return Database.GetAsync(key); } public Task> PutRunStateAsync(CultRecordKey key, AetheriaRunState run) { return Database.PutAsync(key, run); } public Task GetRunStateAsync(CultRecordKey key) { return Database.GetAsync(key); } public Task> PutZoneStateAsync(CultRecordKey key, AetheriaZoneState zone) { return Database.PutAsync(key, zone); } public Task GetZoneStateAsync(CultRecordKey key) { return Database.GetAsync(key); } public Task> PutEntitySnapshotAsync( CultRecordKey key, AetheriaEntitySnapshot entity) { return Database.PutAsync(key, entity); } public Task GetEntitySnapshotAsync(CultRecordKey key) { return Database.GetAsync(key); } public Task> PutRuntimeCommitDrainStatusAsync( AetheriaRuntimeCommitDrainStatus status) { return Database.PutAsync(new CultRecordKey("global:aetheria.runtime_commit_drain_status.v1"), status); } public Task GetRuntimeCommitDrainStatusAsync() { return Database.GetAsync( new CultRecordKey("global:aetheria.runtime_commit_drain_status.v1")); } public Task> PutEveCommandDrainStatusAsync( AetheriaEveCommandDrainStatus status) { return Database.PutAsync(new CultRecordKey("global:aetheria.eve_command_drain_status.v1"), status); } public Task GetEveCommandDrainStatusAsync() { return Database.GetAsync( new CultRecordKey("global:aetheria.eve_command_drain_status.v1")); } public Task FlushAsync(bool soft = false) { return _node.FlushAsync(soft); } public void Dispose() { _node.Dispose(); } public async ValueTask DisposeAsync() { await FlushAsync().ConfigureAwait(false); Dispose(); } }