diff --git a/Assets/Scripts/Gameplay/AetheriaUnityObservedDockingIndex.cs b/Assets/Scripts/Gameplay/AetheriaUnityObservedDockingIndex.cs index 73df68955..887d11a8f 100644 --- a/Assets/Scripts/Gameplay/AetheriaUnityObservedDockingIndex.cs +++ b/Assets/Scripts/Gameplay/AetheriaUnityObservedDockingIndex.cs @@ -3,7 +3,6 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ using System; -using System.Linq; using GameCult.Aetheria.State.Verse; public sealed class AetheriaUnityObservedDockingIndex @@ -35,49 +34,52 @@ public sealed class AetheriaUnityObservedDockingIndex public bool TryResolveCurrentEntity(out Entity entity) { entity = null; - return TryResolveCurrentEntityKey(out var currentEntityKey) && - _observedFacadeIndex.TryResolveEntityByRecordKey(currentEntityKey, out entity); + return TryResolveCurrentDockingSnapshot(out var snapshot) && + !string.IsNullOrWhiteSpace(snapshot.CurrentEntityKey) && + _observedFacadeIndex.TryResolveEntityByRecordKey(snapshot.CurrentEntityKey, out entity); } public bool TryResolveCurrentEntityKey(out string currentEntityKey) { - currentEntityKey = ReadCurrentEntity()?.EntityKey ?? ""; + currentEntityKey = TryResolveCurrentDockingSnapshot(out var snapshot) + ? snapshot.CurrentEntityKey + : ""; return !string.IsNullOrWhiteSpace(currentEntityKey); } public bool TryResolveCurrentEntityDocument(out AetheriaRuntimeCurrentEntityDocument currentEntity) { - currentEntity = ReadCurrentEntity(); + currentEntity = TryResolveCurrentDockingSnapshot(out var snapshot) + ? snapshot.CurrentEntity + : null; return currentEntity != null; } public AetheriaRuntimeStationRefitDocument ResolveStationRefit() { - return ReadStationRefit(); + return TryResolveCurrentDockingSnapshot(out var snapshot) + ? snapshot.StationRefit + : null; } public bool TryResolveCurrentDockingBayRow(out AetheriaRuntimeStationDockingBayRow dockingBay) { - dockingBay = null; - var stationRefit = ReadStationRefit(); - if (stationRefit?.IsDocked != true || stationRefit.DockingBayIndex < 0) - return false; - - dockingBay = (stationRefit.DockingBays ?? Array.Empty()) - .FirstOrDefault(row => row != null && row.DockingBayIndex == stationRefit.DockingBayIndex); + dockingBay = TryResolveCurrentDockingSnapshot(out var snapshot) + ? snapshot.CurrentDockingBay + : null; return dockingBay != null; } public bool TryResolveCurrentDockingBay(out EquippedDockingBay dockingBay) { dockingBay = null; - var stationRefit = ReadStationRefit(); - var dockParentEntityKey = stationRefit?.DockParentEntityKey ?? ""; - if (!TryResolveCurrentDockingBayRow(out var dockingBayRow) || - string.IsNullOrWhiteSpace(dockParentEntityKey) || + if (!TryResolveCurrentDockingSnapshot(out var snapshot) || + snapshot.CurrentDockingBay == null || + snapshot.StationRefit == null || + string.IsNullOrWhiteSpace(snapshot.StationRefit.DockParentEntityKey) || !_observedFacadeIndex.TryResolveDockingBayByRecordKey( - dockParentEntityKey, - dockingBayRow.DockingBayIndex, + snapshot.StationRefit.DockParentEntityKey, + snapshot.CurrentDockingBay.DockingBayIndex, out dockingBay)) { return false; @@ -88,7 +90,9 @@ public sealed class AetheriaUnityObservedDockingIndex public bool TryResolveCurrentDocking(out AetheriaRuntimeCurrentDockingDocument docking) { - docking = ReadCurrentDocking(); + docking = TryResolveCurrentDockingSnapshot(out var snapshot) + ? snapshot.CurrentDocking + : null; return docking != null; } @@ -134,7 +138,9 @@ public sealed class AetheriaUnityObservedDockingIndex Entity entity, out AetheriaRuntimeCurrentDockingDocument docking) { - docking = ReadCurrentDocking(); + docking = TryResolveCurrentDockingSnapshot(out var snapshot) + ? snapshot.CurrentDocking + : null; if (entity == null || docking == null || docking.CurrentEntityIndex != entity.DaemonEntityIndex) @@ -152,50 +158,20 @@ public sealed class AetheriaUnityObservedDockingIndex return true; } - private AetheriaRuntimeCurrentDockingDocument ReadCurrentDocking() + private bool TryResolveCurrentDockingSnapshot(out AetheriaClientDockingSnapshot snapshot) { + snapshot = null; try { - return _resolveClient() + snapshot = _resolveClient() ?.Aetheria() - .Current - .Docking + .DockingState .Latest(); + return snapshot != null; } catch { - return null; - } - } - - private AetheriaRuntimeCurrentEntityDocument ReadCurrentEntity() - { - try - { - return _resolveClient() - ?.Aetheria() - .Current - .Entity - .Latest(); - } - catch - { - return null; - } - } - - private AetheriaRuntimeStationRefitDocument ReadStationRefit() - { - try - { - return _resolveClient() - ?.Aetheria() - .StationRefit - .Latest(); - } - catch - { - return null; + return false; } } diff --git a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaClientState.cs b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaClientState.cs index 01f097d92..293a681bd 100644 --- a/Packages/org.gamecult.aetheria.state/Runtime/AetheriaClientState.cs +++ b/Packages/org.gamecult.aetheria.state/Runtime/AetheriaClientState.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Threading.Tasks; using GameCult.Mesh; using R3; @@ -25,6 +26,7 @@ namespace GameCult.Aetheria.State.Verse StationRefit = stationRefit ?? throw new ArgumentNullException(nameof(stationRefit)); SectorMap = sectorMap ?? throw new ArgumentNullException(nameof(sectorMap)); ZoneRender = zoneRender ?? throw new ArgumentNullException(nameof(zoneRender)); + DockingState = new AetheriaClientDockingState(Current.Entity, Current.Docking, StationRefit); _documents = CultMesh.Documents( Current.Zone, Current.Entity, @@ -37,6 +39,8 @@ namespace GameCult.Aetheria.State.Verse public AetheriaClientCurrentState Current { get; } + public AetheriaClientDockingState DockingState { get; } + public CultMeshDocumentHandle ZoneContacts { get; } public CultMeshDocumentHandle StationRefit { get; } @@ -122,4 +126,73 @@ namespace GameCult.Aetheria.State.Verse public CultMeshDocumentHandle Docking { get; } } + + public sealed class AetheriaClientDockingState + { + private readonly CultMeshDocumentHandle _currentEntity; + private readonly CultMeshDocumentHandle _currentDocking; + private readonly CultMeshDocumentHandle _stationRefit; + + internal AetheriaClientDockingState( + CultMeshDocumentHandle currentEntity, + CultMeshDocumentHandle currentDocking, + CultMeshDocumentHandle stationRefit) + { + _currentEntity = currentEntity ?? throw new ArgumentNullException(nameof(currentEntity)); + _currentDocking = currentDocking ?? throw new ArgumentNullException(nameof(currentDocking)); + _stationRefit = stationRefit ?? throw new ArgumentNullException(nameof(stationRefit)); + } + + public Task LatestAsync() + { + return LatestAsync(_currentEntity, _currentDocking, _stationRefit); + } + + public AetheriaClientDockingSnapshot Latest() + { + return LatestAsync().ConfigureAwait(false).GetAwaiter().GetResult(); + } + + private static async Task LatestAsync( + CultMeshDocumentHandle currentEntity, + CultMeshDocumentHandle currentDocking, + CultMeshDocumentHandle stationRefit) + { + var entity = await currentEntity.LatestAsync().ConfigureAwait(false); + var docking = await currentDocking.LatestAsync().ConfigureAwait(false); + var refit = await stationRefit.LatestAsync().ConfigureAwait(false); + var dockingBay = refit?.IsDocked == true && refit.DockingBayIndex >= 0 + ? (refit.DockingBays ?? Array.Empty()) + .FirstOrDefault(row => row != null && row.DockingBayIndex == refit.DockingBayIndex) + : null; + return new AetheriaClientDockingSnapshot(entity, docking, refit, dockingBay); + } + } + + public sealed class AetheriaClientDockingSnapshot + { + internal AetheriaClientDockingSnapshot( + AetheriaRuntimeCurrentEntityDocument? currentEntity, + AetheriaRuntimeCurrentDockingDocument? currentDocking, + AetheriaRuntimeStationRefitDocument? stationRefit, + AetheriaRuntimeStationDockingBayRow? currentDockingBay) + { + CurrentEntity = currentEntity; + CurrentDocking = currentDocking; + StationRefit = stationRefit; + CurrentDockingBay = currentDockingBay; + } + + public AetheriaRuntimeCurrentEntityDocument? CurrentEntity { get; } + + public AetheriaRuntimeCurrentDockingDocument? CurrentDocking { get; } + + public AetheriaRuntimeStationRefitDocument? StationRefit { get; } + + public AetheriaRuntimeStationDockingBayRow? CurrentDockingBay { get; } + + public string CurrentEntityKey => CurrentEntity?.EntityKey ?? CurrentDocking?.CurrentEntityKey ?? ""; + + public bool IsDocked => StationRefit?.IsDocked == true && CurrentDockingBay != null; + } }