158 lines
5 KiB
C#
158 lines
5 KiB
C#
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<AetheriaStateNode> 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<CultRecordHandle<AetheriaWorldState>> PutWorldAsync(AetheriaWorldState world)
|
|
{
|
|
return Database.PutAsync(new CultRecordKey("global:aetheria.world_state.v1"), world);
|
|
}
|
|
|
|
public Task<AetheriaWorldState?> GetWorldAsync()
|
|
{
|
|
return Database.GetAsync<AetheriaWorldState>(new CultRecordKey("global:aetheria.world_state.v1"));
|
|
}
|
|
|
|
public Task<CultRecordHandle<AetheriaMigrationLedger>> PutMigrationLedgerAsync(AetheriaMigrationLedger ledger)
|
|
{
|
|
return Database.PutAsync(new CultRecordKey("global:aetheria.migration_ledger.v1"), ledger);
|
|
}
|
|
|
|
public Task<CultRecordHandle<AetheriaLegacyCatalogQuarantine>> PutLegacyCatalogQuarantineAsync(
|
|
AetheriaLegacyCatalogQuarantine quarantine)
|
|
{
|
|
return Database.PutAsync(new CultRecordKey("global:aetheria.legacy_catalog_quarantine.v1"), quarantine);
|
|
}
|
|
|
|
public Task<AetheriaLegacyCatalogQuarantine?> GetLegacyCatalogQuarantineAsync()
|
|
{
|
|
return Database.GetAsync<AetheriaLegacyCatalogQuarantine>(
|
|
new CultRecordKey("global:aetheria.legacy_catalog_quarantine.v1"));
|
|
}
|
|
|
|
public Task<CultRecordHandle<AetheriaItemDefinition>> PutItemDefinitionAsync(
|
|
CultRecordKey key,
|
|
AetheriaItemDefinition item)
|
|
{
|
|
return Database.PutAsync(key, item);
|
|
}
|
|
|
|
public Task<AetheriaItemDefinition?> GetItemDefinitionAsync(CultRecordKey key)
|
|
{
|
|
return Database.GetAsync<AetheriaItemDefinition>(key);
|
|
}
|
|
|
|
public Task<CultRecordHandle<AetheriaCorporation>> PutCorporationAsync(CultRecordKey key, AetheriaCorporation corporation)
|
|
{
|
|
return Database.PutAsync(key, corporation);
|
|
}
|
|
|
|
public Task<AetheriaCorporation?> GetCorporationAsync(CultRecordKey key)
|
|
{
|
|
return Database.GetAsync<AetheriaCorporation>(key);
|
|
}
|
|
|
|
public Task<CultRecordHandle<AetheriaNameFile>> PutNameFileAsync(CultRecordKey key, AetheriaNameFile nameFile)
|
|
{
|
|
return Database.PutAsync(key, nameFile);
|
|
}
|
|
|
|
public Task<AetheriaNameFile?> GetNameFileAsync(CultRecordKey key)
|
|
{
|
|
return Database.GetAsync<AetheriaNameFile>(key);
|
|
}
|
|
|
|
public Task<CultRecordHandle<AetheriaPlayerSettings>> PutPlayerSettingsAsync(AetheriaPlayerSettings settings)
|
|
{
|
|
return Database.PutAsync(new CultRecordKey("global:aetheria.player_settings.v1"), settings);
|
|
}
|
|
|
|
public Task<AetheriaPlayerSettings?> GetPlayerSettingsAsync()
|
|
{
|
|
return Database.GetAsync<AetheriaPlayerSettings>(new CultRecordKey("global:aetheria.player_settings.v1"));
|
|
}
|
|
|
|
public Task<CultRecordHandle<AetheriaRunState>> PutRunStateAsync(CultRecordKey key, AetheriaRunState run)
|
|
{
|
|
return Database.PutAsync(key, run);
|
|
}
|
|
|
|
public Task<AetheriaRunState?> GetRunStateAsync(CultRecordKey key)
|
|
{
|
|
return Database.GetAsync<AetheriaRunState>(key);
|
|
}
|
|
|
|
public Task FlushAsync(bool soft = false)
|
|
{
|
|
return _node.FlushAsync(soft);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_node.Dispose();
|
|
}
|
|
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
await FlushAsync().ConfigureAwait(false);
|
|
Dispose();
|
|
}
|
|
}
|