| | | 1 | | using System; |
| | | 2 | | using System.Diagnostics.CodeAnalysis; |
| | | 3 | | |
| | | 4 | | namespace Trailblazer.Pathing; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Context-owned API for guide request, return, cache invalidation, and cache diagnostics. |
| | | 8 | | /// </summary> |
| | | 9 | | public sealed class TrailblazerGuideService |
| | | 10 | | { |
| | | 11 | | private readonly TrailblazerWorldContext _context; |
| | | 12 | | private readonly PathingWorldState _state; |
| | | 13 | | |
| | 961 | 14 | | internal TrailblazerGuideService(TrailblazerWorldContext context, PathingWorldState state) |
| | | 15 | | { |
| | 961 | 16 | | _context = context; |
| | 961 | 17 | | _state = state; |
| | 961 | 18 | | } |
| | | 19 | | |
| | | 20 | | /// <inheritdoc cref="PathGuideFactory.TotalAStarGuideCount"/> |
| | | 21 | | public int TotalAStarGuideCount |
| | | 22 | | { |
| | | 23 | | get |
| | | 24 | | { |
| | 9 | 25 | | using (EnterUsableState()) |
| | 7 | 26 | | return PathGuideFactory.TotalAStarGuideCount; |
| | 7 | 27 | | } |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <inheritdoc cref="PathGuideFactory.InUseAStarGuideCount"/> |
| | | 31 | | public int InUseAStarGuideCount |
| | | 32 | | { |
| | | 33 | | get |
| | | 34 | | { |
| | 8 | 35 | | using (EnterUsableState()) |
| | 8 | 36 | | return PathGuideFactory.InUseAStarGuideCount; |
| | 8 | 37 | | } |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <inheritdoc cref="PathGuideFactory.TotalFlowGuideCount"/> |
| | | 41 | | public int TotalFlowGuideCount |
| | | 42 | | { |
| | | 43 | | get |
| | | 44 | | { |
| | 2 | 45 | | using (EnterUsableState()) |
| | 2 | 46 | | return PathGuideFactory.TotalFlowGuideCount; |
| | 2 | 47 | | } |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <inheritdoc cref="PathGuideFactory.InUseFlowGuideCount"/> |
| | | 51 | | public int InUseFlowGuideCount |
| | | 52 | | { |
| | | 53 | | get |
| | | 54 | | { |
| | 1 | 55 | | using (EnterUsableState()) |
| | 1 | 56 | | return PathGuideFactory.InUseFlowGuideCount; |
| | 1 | 57 | | } |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <inheritdoc cref="PathGuideFactory.TotalVolumeGuideCount"/> |
| | | 61 | | public int TotalVolumeGuideCount |
| | | 62 | | { |
| | | 63 | | get |
| | | 64 | | { |
| | 6 | 65 | | using (EnterUsableState()) |
| | 6 | 66 | | return PathGuideFactory.TotalVolumeGuideCount; |
| | 6 | 67 | | } |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <inheritdoc cref="PathGuideFactory.InUseVolumeGuideCount"/> |
| | | 71 | | public int InUseVolumeGuideCount |
| | | 72 | | { |
| | | 73 | | get |
| | | 74 | | { |
| | 1 | 75 | | using (EnterUsableState()) |
| | 1 | 76 | | return PathGuideFactory.InUseVolumeGuideCount; |
| | 1 | 77 | | } |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <inheritdoc cref="PathGuideFactory.TotalHybridRoutePlanCount"/> |
| | | 81 | | public int TotalHybridRoutePlanCount |
| | | 82 | | { |
| | | 83 | | get |
| | | 84 | | { |
| | 2 | 85 | | using (EnterUsableState()) |
| | 2 | 86 | | return PathGuideFactory.TotalHybridRoutePlanCount; |
| | 2 | 87 | | } |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <inheritdoc cref="PathGuideFactory.InUseHybridRoutePlanCount"/> |
| | | 91 | | public int InUseHybridRoutePlanCount |
| | | 92 | | { |
| | | 93 | | get |
| | | 94 | | { |
| | 1 | 95 | | using (EnterUsableState()) |
| | 1 | 96 | | return PathGuideFactory.InUseHybridRoutePlanCount; |
| | 1 | 97 | | } |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <inheritdoc cref="PathGuideFactory.IsPooling"/> |
| | | 101 | | public bool IsPooling |
| | | 102 | | { |
| | | 103 | | get |
| | | 104 | | { |
| | 1 | 105 | | using (EnterUsableState()) |
| | 1 | 106 | | return PathGuideFactory.IsPooling; |
| | 1 | 107 | | } |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <inheritdoc cref="PathGuideFactory.AnyInUse"/> |
| | | 111 | | public bool AnyInUse |
| | | 112 | | { |
| | | 113 | | get |
| | | 114 | | { |
| | 2 | 115 | | using (EnterUsableState()) |
| | 2 | 116 | | return PathGuideFactory.AnyInUse; |
| | 2 | 117 | | } |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Requests a typed guide for the supplied validated path request. |
| | | 122 | | /// </summary> |
| | | 123 | | public bool RequestGuide<T>(IPathRequest request, [NotNullWhen(true)] out T? result) |
| | | 124 | | where T : class, IGuide |
| | | 125 | | { |
| | 21 | 126 | | if (!IsRequestOwnedByThisContext(request)) |
| | | 127 | | { |
| | 1 | 128 | | result = null; |
| | 1 | 129 | | return false; |
| | | 130 | | } |
| | | 131 | | |
| | 20 | 132 | | using (EnterUsableState()) |
| | 20 | 133 | | return PathGuideFactory.RequestGuide(request, out result); |
| | 20 | 134 | | } |
| | | 135 | | |
| | | 136 | | /// <inheritdoc cref="PathGuideFactory.RequestGuide(IPathRequest,out IGuide?)"/> |
| | | 137 | | public bool RequestGuide(IPathRequest request, [NotNullWhen(true)] out IGuide? result) |
| | | 138 | | { |
| | 66 | 139 | | if (!IsRequestOwnedByThisContext(request)) |
| | | 140 | | { |
| | 2 | 141 | | result = null; |
| | 2 | 142 | | return false; |
| | | 143 | | } |
| | | 144 | | |
| | 64 | 145 | | using (EnterUsableState()) |
| | 64 | 146 | | return PathGuideFactory.RequestGuide(request, out result); |
| | 64 | 147 | | } |
| | | 148 | | |
| | | 149 | | /// <inheritdoc cref="PathGuideFactory.ReturnGuide(IGuide?,bool)"/> |
| | | 150 | | public void ReturnGuide(IGuide? guide, bool dispose = false) |
| | | 151 | | { |
| | 30 | 152 | | if (guide == null) |
| | 1 | 153 | | return; |
| | | 154 | | |
| | 29 | 155 | | using (EnterUsableState()) |
| | 29 | 156 | | PathGuideFactory.ReturnGuide(guide, dispose); |
| | 28 | 157 | | } |
| | | 158 | | |
| | | 159 | | /// <inheritdoc cref="PathGuideFactory.InvalidateCacheFor(string)"/> |
| | | 160 | | public void InvalidateCacheFor(string chartKey) |
| | | 161 | | { |
| | 2 | 162 | | using (EnterUsableState()) |
| | 2 | 163 | | PathGuideFactory.InvalidateCacheFor(chartKey); |
| | 2 | 164 | | } |
| | | 165 | | |
| | | 166 | | /// <inheritdoc cref="PathGuideFactory.FlushCache(bool)"/> |
| | | 167 | | public void FlushCache(bool force = false) |
| | | 168 | | { |
| | 1 | 169 | | using (EnterUsableState()) |
| | 1 | 170 | | PathGuideFactory.FlushCache(force); |
| | 1 | 171 | | } |
| | | 172 | | |
| | | 173 | | internal void CullExpiredGuides(int currentFrame) |
| | | 174 | | { |
| | 2299 | 175 | | using (EnterUsableState()) |
| | 2299 | 176 | | PathGuideFactory.CullExpiredGuides(currentFrame); |
| | 2299 | 177 | | } |
| | | 178 | | |
| | | 179 | | internal void InvalidateVolumeCache() |
| | | 180 | | { |
| | 2 | 181 | | using (EnterUsableState()) |
| | 2 | 182 | | PathGuideFactory.InvalidateVolumeCache(); |
| | 2 | 183 | | } |
| | | 184 | | |
| | | 185 | | internal SolidPartitionReachability.SolidPartitionReachabilityStats CaptureReachabilityStats() |
| | | 186 | | { |
| | 8 | 187 | | using (EnterUsableState()) |
| | 8 | 188 | | return SolidPartitionReachability.CaptureStats(); |
| | 8 | 189 | | } |
| | | 190 | | |
| | | 191 | | internal bool TrySeedAStarCacheForBenchmark(int requestKey, string[] chartKeys, bool checkout) |
| | | 192 | | { |
| | 5 | 193 | | using (EnterUsableState()) |
| | 5 | 194 | | return PathGuideFactory.TrySeedAStarCacheForBenchmark(requestKey, chartKeys, checkout); |
| | 5 | 195 | | } |
| | | 196 | | |
| | | 197 | | internal bool TrySeedFlowFieldCacheForBenchmark(int requestKey, string[] chartKeys, bool checkout) |
| | | 198 | | { |
| | 1 | 199 | | using (EnterUsableState()) |
| | 1 | 200 | | return PathGuideFactory.TrySeedFlowFieldCacheForBenchmark(requestKey, chartKeys, checkout); |
| | 1 | 201 | | } |
| | | 202 | | |
| | | 203 | | internal bool TrySeedVolumeCacheForBenchmark(int requestKey, string[] chartKeys, bool checkout) |
| | | 204 | | { |
| | 3 | 205 | | using (EnterUsableState()) |
| | 3 | 206 | | return PathGuideFactory.TrySeedVolumeCacheForBenchmark(requestKey, chartKeys, checkout); |
| | 3 | 207 | | } |
| | | 208 | | |
| | | 209 | | internal bool TrySeedHybridRoutePlanCacheForBenchmark(int requestKey, string[] chartKeys, bool checkout) |
| | | 210 | | { |
| | 1 | 211 | | using (EnterUsableState()) |
| | | 212 | | { |
| | 1 | 213 | | return PathGuideFactory.TrySeedHybridRoutePlanCacheForBenchmark( |
| | 1 | 214 | | requestKey, |
| | 1 | 215 | | chartKeys, |
| | 1 | 216 | | checkout); |
| | | 217 | | } |
| | 1 | 218 | | } |
| | | 219 | | |
| | | 220 | | internal int CountIndexedCacheEntriesForBenchmark(string chartKey) |
| | | 221 | | { |
| | 1 | 222 | | using (EnterUsableState()) |
| | 1 | 223 | | return PathGuideFactory.CountIndexedCacheEntriesForBenchmark(chartKey); |
| | 1 | 224 | | } |
| | | 225 | | |
| | | 226 | | private IDisposable EnterUsableState() |
| | | 227 | | { |
| | 2469 | 228 | | EnsureUsable(); |
| | 2467 | 229 | | return PathManager.EnterState(_state); |
| | | 230 | | } |
| | | 231 | | |
| | | 232 | | private void EnsureUsable() |
| | | 233 | | { |
| | 2469 | 234 | | if (_context.IsDisposed) |
| | 1 | 235 | | throw new ObjectDisposedException(nameof(TrailblazerWorldContext)); |
| | 2468 | 236 | | if (!_context.World.IsActive) |
| | 1 | 237 | | throw new InvalidOperationException("TrailblazerGuideService is bound to an inactive GridWorld."); |
| | 2467 | 238 | | } |
| | | 239 | | |
| | | 240 | | private bool IsRequestOwnedByThisContext(IPathRequest request) => |
| | 87 | 241 | | request != null && ReferenceEquals(request.Context, _context); |
| | | 242 | | } |