| | | 1 | | //======================================================================= |
| | | 2 | | // LSCoroutine.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2026–present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System; |
| | | 9 | | using System.Collections.Generic; |
| | | 10 | | using System.Runtime.ExceptionServices; |
| | | 11 | | |
| | | 12 | | namespace Gravitas.Support; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Represents one context-owned lockstep coroutine. |
| | | 16 | | /// </summary> |
| | | 17 | | public sealed class LSCoroutine |
| | | 18 | | { |
| | | 19 | | private readonly IEnumerator<ILockedYieldInstruction> _enumerator; |
| | | 20 | | private ILockedYieldInstruction? _currentInstruction; |
| | | 21 | | // Cancellation marks Active immediately, but disposal waits until the current user callback unwinds. |
| | | 22 | | private bool _executing; |
| | | 23 | | |
| | 76 | 24 | | internal LSCoroutine(GravitasCoroutineService owner, IEnumerator<ILockedYieldInstruction> enumerator) |
| | | 25 | | { |
| | 76 | 26 | | Owner = owner; |
| | 76 | 27 | | _enumerator = enumerator; |
| | 76 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Gets whether this coroutine is still active. |
| | | 32 | | /// </summary> |
| | | 33 | | public bool Active { get; private set; } = true; |
| | | 34 | | |
| | | 35 | | internal GravitasCoroutineService Owner { get; } |
| | | 36 | | |
| | | 37 | | internal int Index { get; set; } = -1; |
| | | 38 | | |
| | | 39 | | internal void Simulate() |
| | | 40 | | { |
| | 48 | 41 | | Exception? executionException = null; |
| | 48 | 42 | | _executing = true; |
| | | 43 | | try |
| | | 44 | | { |
| | 48 | 45 | | SimulateCore(); |
| | 43 | 46 | | } |
| | | 47 | | catch (Exception exception) |
| | | 48 | | { |
| | 5 | 49 | | executionException = exception; |
| | 5 | 50 | | } |
| | | 51 | | finally |
| | | 52 | | { |
| | 48 | 53 | | _executing = false; |
| | 48 | 54 | | if (!Active) |
| | | 55 | | { |
| | | 56 | | try |
| | | 57 | | { |
| | 15 | 58 | | DisposeResources(); |
| | 13 | 59 | | } |
| | 2 | 60 | | catch (Exception cleanupException) |
| | | 61 | | { |
| | 2 | 62 | | if (executionException != null) |
| | 1 | 63 | | throw new AggregateException(executionException, cleanupException); |
| | | 64 | | |
| | 1 | 65 | | throw; |
| | | 66 | | } |
| | | 67 | | } |
| | 46 | 68 | | } |
| | | 69 | | |
| | 46 | 70 | | if (executionException != null) |
| | 4 | 71 | | ExceptionDispatchInfo.Capture(executionException).Throw(); |
| | 42 | 72 | | } |
| | | 73 | | |
| | | 74 | | private void SimulateCore() |
| | | 75 | | { |
| | 48 | 76 | | if (_currentInstruction != null) |
| | | 77 | | { |
| | 12 | 78 | | bool keepWaiting = _currentInstruction.KeepWaiting; |
| | 11 | 79 | | if (!Active || keepWaiting) |
| | 3 | 80 | | return; |
| | | 81 | | |
| | 8 | 82 | | DisposeCurrentInstruction(); |
| | 8 | 83 | | if (!Active) |
| | 1 | 84 | | return; |
| | | 85 | | } |
| | | 86 | | |
| | 43 | 87 | | if (!_enumerator.MoveNext()) |
| | | 88 | | { |
| | 7 | 89 | | Owner.StopCoroutine(this); |
| | 7 | 90 | | return; |
| | | 91 | | } |
| | | 92 | | |
| | 33 | 93 | | ILockedYieldInstruction? nextInstruction = _enumerator.Current; |
| | 33 | 94 | | _currentInstruction = nextInstruction; |
| | 33 | 95 | | if (!Active) |
| | 5 | 96 | | return; |
| | | 97 | | |
| | 28 | 98 | | if (_currentInstruction != null |
| | 28 | 99 | | && !ReferenceEquals(_currentInstruction.Context, Owner.Context)) |
| | | 100 | | { |
| | 1 | 101 | | throw new InvalidOperationException( |
| | 1 | 102 | | "Coroutine yield instructions must belong to the coroutine service context."); |
| | | 103 | | } |
| | 27 | 104 | | } |
| | | 105 | | |
| | | 106 | | internal void End() |
| | | 107 | | { |
| | 76 | 108 | | Active = false; |
| | 76 | 109 | | if (!_executing) |
| | 61 | 110 | | DisposeResources(); |
| | 73 | 111 | | } |
| | | 112 | | |
| | | 113 | | private void DisposeResources() |
| | | 114 | | { |
| | 76 | 115 | | ILockedYieldInstruction? instruction = _currentInstruction; |
| | 76 | 116 | | _currentInstruction = null; |
| | | 117 | | |
| | 76 | 118 | | if (ReferenceEquals(instruction, _enumerator)) |
| | | 119 | | { |
| | 2 | 120 | | _enumerator.Dispose(); |
| | 2 | 121 | | return; |
| | | 122 | | } |
| | | 123 | | |
| | 74 | 124 | | Exception? instructionException = null; |
| | | 125 | | try |
| | | 126 | | { |
| | 74 | 127 | | instruction?.Dispose(); |
| | 72 | 128 | | } |
| | | 129 | | catch (Exception exception) |
| | | 130 | | { |
| | 2 | 131 | | instructionException = exception; |
| | 2 | 132 | | } |
| | | 133 | | |
| | | 134 | | try |
| | | 135 | | { |
| | 74 | 136 | | _enumerator.Dispose(); |
| | 70 | 137 | | } |
| | 4 | 138 | | catch (Exception enumeratorException) |
| | | 139 | | { |
| | 4 | 140 | | if (instructionException != null) |
| | 1 | 141 | | throw new AggregateException(instructionException, enumeratorException); |
| | | 142 | | |
| | 3 | 143 | | throw; |
| | | 144 | | } |
| | | 145 | | |
| | 70 | 146 | | if (instructionException != null) |
| | 1 | 147 | | ExceptionDispatchInfo.Capture(instructionException).Throw(); |
| | 69 | 148 | | } |
| | | 149 | | |
| | | 150 | | private void DisposeCurrentInstruction() |
| | | 151 | | { |
| | 8 | 152 | | ILockedYieldInstruction instruction = _currentInstruction!; |
| | 8 | 153 | | _currentInstruction = null; |
| | 8 | 154 | | if (!ReferenceEquals(instruction, _enumerator)) |
| | 7 | 155 | | instruction.Dispose(); |
| | 8 | 156 | | } |
| | | 157 | | } |