| | | 1 | | //======================================================================= |
| | | 2 | | // GridDiagnosticSession.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using FixedMathSharp; |
| | | 9 | | using GridForge.Grids; |
| | | 10 | | using GridForge.Grids.Topology; |
| | | 11 | | using GridForge.Spatial; |
| | | 12 | | using SwiftCollections; |
| | | 13 | | using SwiftCollections.Utility; |
| | | 14 | | using System; |
| | | 15 | | |
| | | 16 | | namespace GridForge.Diagnostics; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Captures dirty diagnostic grid, cell, and sparse address changes for one |
| | | 20 | | /// active <see cref="GridWorld"/>. |
| | | 21 | | /// </summary> |
| | | 22 | | public sealed class GridDiagnosticSession : IDisposable |
| | | 23 | | { |
| | | 24 | | private readonly GridWorld _world; |
| | | 25 | | private readonly int _worldSpawnToken; |
| | 7 | 26 | | private readonly SwiftList<GridDiagnosticChange> _changes = new(); |
| | 7 | 27 | | private readonly SwiftDictionary<GridDiagnosticChangeKey, int> _changeIndexes = new(); |
| | 7 | 28 | | private readonly object _syncRoot = new(); |
| | | 29 | | private bool _worldResetPending; |
| | | 30 | | private bool _disposed; |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Creates a diagnostic dirty-tracking session for the supplied active |
| | | 34 | | /// world. |
| | | 35 | | /// </summary> |
| | 7 | 36 | | public GridDiagnosticSession(GridWorld world) |
| | | 37 | | { |
| | 7 | 38 | | if (world == null) |
| | 0 | 39 | | throw new ArgumentNullException(nameof(world)); |
| | | 40 | | |
| | 7 | 41 | | if (!world.IsActive) |
| | 0 | 42 | | throw new InvalidOperationException("Diagnostic sessions require an active GridWorld."); |
| | | 43 | | |
| | 7 | 44 | | _world = world; |
| | 7 | 45 | | _worldSpawnToken = world.SpawnToken; |
| | 7 | 46 | | Subscribe(); |
| | 7 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Clears and fills caller-owned storage with coalesced dirty changes. |
| | | 51 | | /// </summary> |
| | | 52 | | public int GetDirtyChangesInto(SwiftList<GridDiagnosticChange> results) |
| | | 53 | | { |
| | 10 | 54 | | SwiftThrowHelper.ThrowIfNull(results, nameof(results)); |
| | | 55 | | |
| | 10 | 56 | | results.Clear(); |
| | 10 | 57 | | lock (_syncRoot) |
| | | 58 | | { |
| | 44 | 59 | | for (int i = 0; i < _changes.Count; i++) |
| | 12 | 60 | | results.Add(_changes[i]); |
| | 10 | 61 | | } |
| | | 62 | | |
| | 10 | 63 | | if (results.Count > 1) |
| | 4 | 64 | | results.SortInPlace(); |
| | | 65 | | |
| | 10 | 66 | | return results.Count; |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Clears all dirty changes captured so far. |
| | | 71 | | /// </summary> |
| | | 72 | | public void ClearDirtyChanges() |
| | | 73 | | { |
| | 10 | 74 | | lock (_syncRoot) |
| | | 75 | | { |
| | 10 | 76 | | _changes.Clear(); |
| | 10 | 77 | | _changeIndexes.Clear(); |
| | 10 | 78 | | _worldResetPending = false; |
| | 10 | 79 | | } |
| | 10 | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <inheritdoc/> |
| | | 83 | | public void Dispose() |
| | | 84 | | { |
| | 7 | 85 | | if (_disposed) |
| | 0 | 86 | | return; |
| | | 87 | | |
| | 7 | 88 | | Unsubscribe(); |
| | 7 | 89 | | ClearDirtyChanges(); |
| | 7 | 90 | | _disposed = true; |
| | 7 | 91 | | GC.SuppressFinalize(this); |
| | 7 | 92 | | } |
| | | 93 | | |
| | | 94 | | private void Subscribe() |
| | | 95 | | { |
| | 7 | 96 | | _world.OnActiveGridAdded += HandleActiveGridAdded; |
| | 7 | 97 | | _world.OnActiveGridRemoved += HandleActiveGridRemoved; |
| | 7 | 98 | | _world.OnActiveGridChange += HandleActiveGridChanged; |
| | 7 | 99 | | _world.OnReset += HandleWorldReset; |
| | 7 | 100 | | GridObstacleManager.OnObstacleAdded += HandleObstacleChanged; |
| | 7 | 101 | | GridObstacleManager.OnObstacleRemoved += HandleObstacleChanged; |
| | 7 | 102 | | GridObstacleManager.OnObstaclesCleared += HandleObstaclesCleared; |
| | 7 | 103 | | GridOccupantManager.OnOccupantAdded += HandleOccupantChanged; |
| | 7 | 104 | | GridOccupantManager.OnOccupantRemoved += HandleOccupantChanged; |
| | 7 | 105 | | } |
| | | 106 | | |
| | | 107 | | private void Unsubscribe() |
| | | 108 | | { |
| | 7 | 109 | | _world.OnActiveGridAdded -= HandleActiveGridAdded; |
| | 7 | 110 | | _world.OnActiveGridRemoved -= HandleActiveGridRemoved; |
| | 7 | 111 | | _world.OnActiveGridChange -= HandleActiveGridChanged; |
| | 7 | 112 | | _world.OnReset -= HandleWorldReset; |
| | 7 | 113 | | GridObstacleManager.OnObstacleAdded -= HandleObstacleChanged; |
| | 7 | 114 | | GridObstacleManager.OnObstacleRemoved -= HandleObstacleChanged; |
| | 7 | 115 | | GridObstacleManager.OnObstaclesCleared -= HandleObstaclesCleared; |
| | 7 | 116 | | GridOccupantManager.OnOccupantAdded -= HandleOccupantChanged; |
| | 7 | 117 | | GridOccupantManager.OnOccupantRemoved -= HandleOccupantChanged; |
| | 7 | 118 | | } |
| | | 119 | | |
| | | 120 | | private void HandleActiveGridAdded(GridEventInfo eventInfo) |
| | | 121 | | { |
| | 3 | 122 | | if (!CanRecord(eventInfo.WorldSpawnToken)) |
| | 0 | 123 | | return; |
| | | 124 | | |
| | 3 | 125 | | RecordGridChange(eventInfo, GridDiagnosticChangeKind.GridAdded); |
| | 3 | 126 | | } |
| | | 127 | | |
| | | 128 | | private void HandleActiveGridRemoved(GridEventInfo eventInfo) |
| | | 129 | | { |
| | 1 | 130 | | if (!CanRecord(eventInfo.WorldSpawnToken)) |
| | 0 | 131 | | return; |
| | | 132 | | |
| | 1 | 133 | | RecordGridChange(eventInfo, GridDiagnosticChangeKind.GridRemoved); |
| | 1 | 134 | | } |
| | | 135 | | |
| | | 136 | | private void HandleActiveGridChanged(GridEventInfo eventInfo) |
| | | 137 | | { |
| | 6 | 138 | | if (!CanRecord(eventInfo.WorldSpawnToken) |
| | 6 | 139 | | || HasPendingWorldReset()) |
| | | 140 | | { |
| | 1 | 141 | | return; |
| | | 142 | | } |
| | | 143 | | |
| | 5 | 144 | | switch (eventInfo.ChangeKind) |
| | | 145 | | { |
| | | 146 | | case GridEventKind.SparseVoxelAdded: |
| | 1 | 147 | | RecordSparseVoxelChange(eventInfo, GridDiagnosticChangeKind.SparseVoxelAdded); |
| | 1 | 148 | | break; |
| | | 149 | | case GridEventKind.SparseVoxelRemoved: |
| | 1 | 150 | | RecordSparseVoxelChange(eventInfo, GridDiagnosticChangeKind.SparseVoxelRemoved); |
| | 1 | 151 | | break; |
| | | 152 | | default: |
| | 3 | 153 | | RecordGridChange(eventInfo, GridDiagnosticChangeKind.GridChanged); |
| | | 154 | | break; |
| | | 155 | | } |
| | 3 | 156 | | } |
| | | 157 | | |
| | | 158 | | private void HandleWorldReset() |
| | | 159 | | { |
| | 2 | 160 | | if (!CanRecord(_worldSpawnToken)) |
| | 0 | 161 | | return; |
| | | 162 | | |
| | 2 | 163 | | GridDiagnosticChange change = new( |
| | 2 | 164 | | GridDiagnosticChangeKind.WorldReset, |
| | 2 | 165 | | _worldSpawnToken, |
| | 2 | 166 | | ushort.MaxValue, |
| | 2 | 167 | | 0, |
| | 2 | 168 | | default, |
| | 2 | 169 | | default, |
| | 2 | 170 | | default, |
| | 2 | 171 | | default); |
| | 2 | 172 | | GridDiagnosticChangeKey key = GridDiagnosticChangeKey.FromChange(change); |
| | | 173 | | |
| | 2 | 174 | | lock (_syncRoot) |
| | | 175 | | { |
| | 2 | 176 | | _changes.Clear(); |
| | 2 | 177 | | _changeIndexes.Clear(); |
| | 2 | 178 | | _worldResetPending = true; |
| | 2 | 179 | | _changeIndexes.Add(key, 0); |
| | 2 | 180 | | _changes.Add(change); |
| | 2 | 181 | | } |
| | 2 | 182 | | } |
| | | 183 | | |
| | | 184 | | private void HandleObstacleChanged(ObstacleEventInfo eventInfo) |
| | | 185 | | { |
| | 5 | 186 | | if (!CanRecordCell(eventInfo.VoxelIndex)) |
| | 2 | 187 | | return; |
| | | 188 | | |
| | 3 | 189 | | RecordCellChange(eventInfo.VoxelIndex, GridDiagnosticChangeKind.ObstacleChanged); |
| | 3 | 190 | | } |
| | | 191 | | |
| | | 192 | | private void HandleObstaclesCleared(ObstacleClearEventInfo eventInfo) |
| | | 193 | | { |
| | 1 | 194 | | if (!CanRecordCell(eventInfo.VoxelIndex)) |
| | 1 | 195 | | return; |
| | | 196 | | |
| | 0 | 197 | | RecordCellChange(eventInfo.VoxelIndex, GridDiagnosticChangeKind.ObstacleChanged); |
| | 0 | 198 | | } |
| | | 199 | | |
| | | 200 | | private void HandleOccupantChanged(OccupantEventInfo eventInfo) |
| | | 201 | | { |
| | 4 | 202 | | if (!CanRecordCell(eventInfo.VoxelIndex)) |
| | 1 | 203 | | return; |
| | | 204 | | |
| | 3 | 205 | | RecordCellChange(eventInfo.VoxelIndex, GridDiagnosticChangeKind.OccupantChanged); |
| | 3 | 206 | | } |
| | | 207 | | |
| | | 208 | | private bool CanRecord(int worldSpawnToken) => |
| | 22 | 209 | | !_disposed |
| | 22 | 210 | | && worldSpawnToken == _worldSpawnToken; |
| | | 211 | | |
| | | 212 | | private bool CanRecordCell(WorldVoxelIndex worldIndex) => |
| | 10 | 213 | | CanRecord(worldIndex.WorldSpawnToken) |
| | 10 | 214 | | && !HasPendingWorldReset() |
| | 10 | 215 | | && _world.TryGetGrid(worldIndex, out _); |
| | | 216 | | |
| | | 217 | | private bool HasPendingWorldReset() |
| | | 218 | | { |
| | 13 | 219 | | lock (_syncRoot) |
| | 13 | 220 | | return _worldResetPending; |
| | 13 | 221 | | } |
| | | 222 | | |
| | | 223 | | private void RecordGridChange( |
| | | 224 | | GridEventInfo eventInfo, |
| | | 225 | | GridDiagnosticChangeKind kind) |
| | | 226 | | { |
| | 7 | 227 | | RecordChange(new GridDiagnosticChange( |
| | 7 | 228 | | kind, |
| | 7 | 229 | | eventInfo.WorldSpawnToken, |
| | 7 | 230 | | eventInfo.GridIndex, |
| | 7 | 231 | | eventInfo.GridSpawnToken, |
| | 7 | 232 | | default, |
| | 7 | 233 | | default, |
| | 7 | 234 | | eventInfo.BoundsMin, |
| | 7 | 235 | | eventInfo.BoundsMax)); |
| | 7 | 236 | | } |
| | | 237 | | |
| | | 238 | | private void RecordSparseVoxelChange( |
| | | 239 | | GridEventInfo eventInfo, |
| | | 240 | | GridDiagnosticChangeKind kind) |
| | | 241 | | { |
| | 2 | 242 | | WorldVoxelIndex worldIndex = new( |
| | 2 | 243 | | eventInfo.WorldSpawnToken, |
| | 2 | 244 | | eventInfo.GridIndex, |
| | 2 | 245 | | eventInfo.GridSpawnToken, |
| | 2 | 246 | | eventInfo.VoxelIndex); |
| | | 247 | | |
| | 2 | 248 | | RecordChange(new GridDiagnosticChange( |
| | 2 | 249 | | kind, |
| | 2 | 250 | | eventInfo.WorldSpawnToken, |
| | 2 | 251 | | eventInfo.GridIndex, |
| | 2 | 252 | | eventInfo.GridSpawnToken, |
| | 2 | 253 | | worldIndex, |
| | 2 | 254 | | eventInfo.VoxelIndex, |
| | 2 | 255 | | eventInfo.AffectedBoundsMin, |
| | 2 | 256 | | eventInfo.AffectedBoundsMax)); |
| | | 257 | | |
| | 2 | 258 | | RecordSparseAddressRangeChange(eventInfo); |
| | 2 | 259 | | } |
| | | 260 | | |
| | | 261 | | private void RecordSparseAddressRangeChange(GridEventInfo eventInfo) |
| | | 262 | | { |
| | 2 | 263 | | Vector3d boundsMin = eventInfo.AffectedBoundsMin; |
| | 2 | 264 | | Vector3d boundsMax = eventInfo.AffectedBoundsMax; |
| | 2 | 265 | | if (_world.TryGetGrid(eventInfo.GridIndex, out VoxelGrid? grid)) |
| | | 266 | | { |
| | 2 | 267 | | TopologyVoxelAabb bounds = TopologyVoxelAabb.FromIndex(grid!, eventInfo.VoxelIndex); |
| | 2 | 268 | | boundsMin = bounds.Min; |
| | 2 | 269 | | boundsMax = bounds.Max; |
| | | 270 | | } |
| | | 271 | | |
| | 2 | 272 | | RecordChange(new GridDiagnosticChange( |
| | 2 | 273 | | GridDiagnosticChangeKind.SparseAddressChanged, |
| | 2 | 274 | | eventInfo.WorldSpawnToken, |
| | 2 | 275 | | eventInfo.GridIndex, |
| | 2 | 276 | | eventInfo.GridSpawnToken, |
| | 2 | 277 | | default, |
| | 2 | 278 | | eventInfo.VoxelIndex, |
| | 2 | 279 | | boundsMin, |
| | 2 | 280 | | boundsMax)); |
| | 2 | 281 | | } |
| | | 282 | | |
| | | 283 | | private void RecordCellChange( |
| | | 284 | | WorldVoxelIndex worldIndex, |
| | | 285 | | GridDiagnosticChangeKind kind) |
| | | 286 | | { |
| | 6 | 287 | | RecordChange(new GridDiagnosticChange( |
| | 6 | 288 | | kind, |
| | 6 | 289 | | worldIndex.WorldSpawnToken, |
| | 6 | 290 | | worldIndex.GridIndex, |
| | 6 | 291 | | worldIndex.GridSpawnToken, |
| | 6 | 292 | | worldIndex, |
| | 6 | 293 | | worldIndex.VoxelIndex, |
| | 6 | 294 | | default, |
| | 6 | 295 | | default)); |
| | 6 | 296 | | } |
| | | 297 | | |
| | | 298 | | private void RecordChange(GridDiagnosticChange change) |
| | | 299 | | { |
| | 17 | 300 | | GridDiagnosticChangeKey key = GridDiagnosticChangeKey.FromChange(change); |
| | 17 | 301 | | lock (_syncRoot) |
| | | 302 | | { |
| | 17 | 303 | | if (_changeIndexes.TryGetValue(key, out int index)) |
| | | 304 | | { |
| | 4 | 305 | | GridDiagnosticChange existing = _changes[index]; |
| | 4 | 306 | | _changes[index] = existing.WithKind(existing.Kind | change.Kind); |
| | 4 | 307 | | return; |
| | | 308 | | } |
| | | 309 | | |
| | 13 | 310 | | _changeIndexes.Add(key, _changes.Count); |
| | 13 | 311 | | _changes.Add(change); |
| | 13 | 312 | | } |
| | 17 | 313 | | } |
| | | 314 | | |
| | | 315 | | private readonly struct GridDiagnosticChangeKey : IEquatable<GridDiagnosticChangeKey> |
| | | 316 | | { |
| | | 317 | | private readonly GridDiagnosticChangeScope _scope; |
| | | 318 | | private readonly int _worldSpawnToken; |
| | | 319 | | private readonly ushort _gridIndex; |
| | | 320 | | private readonly int _gridSpawnToken; |
| | | 321 | | private readonly WorldVoxelIndex _worldIndex; |
| | | 322 | | private readonly VoxelIndex _voxelIndex; |
| | | 323 | | private readonly Vector3d _boundsMin; |
| | | 324 | | private readonly Vector3d _boundsMax; |
| | | 325 | | |
| | | 326 | | private GridDiagnosticChangeKey( |
| | | 327 | | GridDiagnosticChangeScope scope, |
| | | 328 | | GridDiagnosticChange change) |
| | | 329 | | { |
| | 19 | 330 | | _scope = scope; |
| | 19 | 331 | | _worldSpawnToken = change.WorldSpawnToken; |
| | 19 | 332 | | _gridIndex = change.GridIndex; |
| | 19 | 333 | | _gridSpawnToken = change.GridSpawnToken; |
| | 19 | 334 | | _worldIndex = change.WorldIndex; |
| | 19 | 335 | | _voxelIndex = change.VoxelIndex; |
| | 19 | 336 | | _boundsMin = change.BoundsMin; |
| | 19 | 337 | | _boundsMax = change.BoundsMax; |
| | 19 | 338 | | } |
| | | 339 | | |
| | | 340 | | public static GridDiagnosticChangeKey FromChange(GridDiagnosticChange change) |
| | | 341 | | { |
| | 19 | 342 | | GridDiagnosticChangeScope scope = GetScope(change); |
| | 19 | 343 | | return new GridDiagnosticChangeKey(scope, change); |
| | | 344 | | } |
| | | 345 | | |
| | | 346 | | public bool Equals(GridDiagnosticChangeKey other) => |
| | 4 | 347 | | _scope == other._scope |
| | 4 | 348 | | && _worldSpawnToken == other._worldSpawnToken |
| | 4 | 349 | | && _gridIndex == other._gridIndex |
| | 4 | 350 | | && _gridSpawnToken == other._gridSpawnToken |
| | 4 | 351 | | && _worldIndex.Equals(other._worldIndex) |
| | 4 | 352 | | && _voxelIndex.Equals(other._voxelIndex) |
| | 4 | 353 | | && _boundsMin.Equals(other._boundsMin) |
| | 4 | 354 | | && _boundsMax.Equals(other._boundsMax); |
| | | 355 | | |
| | 0 | 356 | | public override bool Equals(object? obj) => obj is GridDiagnosticChangeKey other && Equals(other); |
| | | 357 | | |
| | | 358 | | public override int GetHashCode() |
| | | 359 | | { |
| | 32 | 360 | | int hash = SwiftHashTools.CombineHashCodes((int)_scope, _worldSpawnToken); |
| | 32 | 361 | | hash = SwiftHashTools.CombineHashCodes(hash, _gridIndex); |
| | 32 | 362 | | hash = SwiftHashTools.CombineHashCodes(hash, _gridSpawnToken); |
| | 32 | 363 | | hash = SwiftHashTools.CombineHashCodes(hash, _worldIndex.GetHashCode()); |
| | 32 | 364 | | hash = SwiftHashTools.CombineHashCodes(hash, _voxelIndex.GetHashCode()); |
| | 32 | 365 | | hash = SwiftHashTools.CombineHashCodes(hash, _boundsMin.GetHashCode()); |
| | 32 | 366 | | return SwiftHashTools.CombineHashCodes(hash, _boundsMax.GetHashCode()); |
| | | 367 | | } |
| | | 368 | | |
| | | 369 | | private static GridDiagnosticChangeScope GetScope(GridDiagnosticChange change) |
| | | 370 | | { |
| | 19 | 371 | | if ((change.Kind & GridDiagnosticChangeKind.WorldReset) != 0) |
| | 2 | 372 | | return GridDiagnosticChangeScope.World; |
| | | 373 | | |
| | 17 | 374 | | if (change.WorldIndex.WorldSpawnToken != 0 || change.WorldIndex.VoxelIndex.IsAllocated) |
| | 8 | 375 | | return GridDiagnosticChangeScope.Cell; |
| | | 376 | | |
| | 9 | 377 | | return (change.Kind & GridDiagnosticChangeKind.SparseAddressChanged) != 0 |
| | 9 | 378 | | ? GridDiagnosticChangeScope.Range |
| | 9 | 379 | | : GridDiagnosticChangeScope.Grid; |
| | | 380 | | } |
| | | 381 | | } |
| | | 382 | | |
| | | 383 | | private enum GridDiagnosticChangeScope |
| | | 384 | | { |
| | | 385 | | World = 0, |
| | | 386 | | Grid = 1, |
| | | 387 | | Cell = 2, |
| | | 388 | | Range = 3 |
| | | 389 | | } |
| | | 390 | | } |