< Summary

Information
Class: Lattice.ECS.Session.SessionRunner
Assembly: Lattice
File(s): /home/runner/work/Sideline/Sideline/godot/scripts/lattice/ECS/Session/SessionRunner.cs
Line coverage
82%
Covered lines: 151
Uncovered lines: 31
Coverable lines: 182
Total lines: 461
Line coverage: 82.9%
Branch coverage
87%
Covered branches: 47
Total branches: 54
Branch coverage: 87%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Definition()100%11100%
get_Runtime()100%11100%
get_Session()100%210%
get_Context()100%11100%
get_Name()100%11100%
get_IsRunning()100%11100%
get_State()100%11100%
get_LastShutdownCause()100%11100%
get_LastResetReason()100%11100%
get_LastFailure()100%11100%
get_RuntimeOptions()100%11100%
get_RuntimeKind()100%11100%
get_RuntimeBoundary()100%11100%
.ctor(...)50%44100%
.ctor(...)50%2276.92%
Start()100%22100%
Step()100%22100%
Step(...)100%44100%
Stop()100%11100%
ResetRuntime()100%4476%
Dispose()100%6676.92%
StopCore(...)60%141064.7%
HandleFailure(...)100%66100%
TryStopAfterFailure(...)100%1153.84%
InvokeRuntimeCreated(...)100%2271.42%
InvokeLifecycleHook(...)100%22100%
InvokeLifecycleHook(...)100%2271.42%
TryInvokeFailureHook(...)100%2271.42%
ThrowIfDisposed()100%22100%
ResolveFailureKind(...)100%22100%
ResolveOperationName(...)100%22100%
.ctor(...)100%11100%
get_HookName()100%11100%

File(s)

/home/runner/work/Sideline/Sideline/godot/scripts/lattice/ECS/Session/SessionRunner.cs

#LineLine coverage
 1using System;
 2using System.ComponentModel;
 3
 4namespace Lattice.ECS.Session
 5{
 6    /// <summary>
 7    /// 会话运行器。
 8    /// 为纯 C# 环境提供统一的 Start / Step / Stop 驱动入口。
 9    /// 当前面向 `SessionRuntime` 工作,而不是绑定某一个具体运行模式。
 10    /// </summary>
 11    public sealed class SessionRunner : IDisposable
 12    {
 13        private bool _disposed;
 14        private readonly bool _ownsRuntime;
 15        private readonly SessionRunnerLifecycleHooks _lifecycleHooks;
 16
 17        /// <summary>
 18        /// 当前运行定义。
 19        /// 由 `SessionRunnerBuilder` 构建的 runner 一定具备定义;手动传入 runtime 的兼容入口则没有。
 20        /// </summary>
 921        public SessionRunnerDefinition? Definition { get; }
 22
 23        /// <summary>
 24        /// 当前绑定的运行时实例。
 25        /// </summary>
 24926        public SessionRuntime Runtime { get; private set; }
 27
 28        /// <summary>
 29        /// 兼容入口:当前运行时实例。
 30        /// </summary>
 31        [Obsolete("请改用 Runtime。Session 仅保留为兼容属性名。", false)]
 32        [EditorBrowsable(EditorBrowsableState.Never)]
 033        public SessionRuntime Session => Runtime;
 34
 35        /// <summary>
 36        /// 当前运行时上下文。
 37        /// </summary>
 1238        public SessionRuntimeContext Context => Runtime.Context;
 39
 40        /// <summary>
 41        /// 运行器名称。
 42        /// </summary>
 1643        public string Name { get; }
 44
 45        /// <summary>
 46        /// 运行器当前是否处于运行状态。
 47        /// </summary>
 1348        public bool IsRunning => State == SessionRunnerState.Running;
 49
 50        /// <summary>
 51        /// 当前运行器状态。
 52        /// </summary>
 12053        public SessionRunnerState State { get; private set; }
 54
 55        /// <summary>
 56        /// 最近一次停止原因。
 57        /// </summary>
 2658        public SessionRunnerShutdownCause LastShutdownCause { get; private set; }
 59
 60        /// <summary>
 61        /// 最近一次 runtime 重建原因。
 62        /// </summary>
 763        public SessionRunnerResetReason LastResetReason { get; private set; }
 64
 65        /// <summary>
 66        /// 最近一次生命周期失败信息。
 67        /// </summary>
 1968        public SessionRunnerFailureInfo? LastFailure { get; private set; }
 69
 70        /// <summary>
 71        /// 当前运行时的稳定公开参数。
 72        /// </summary>
 173        public SessionRuntimeOptions RuntimeOptions => Runtime.RuntimeOptions;
 74
 75        /// <summary>
 76        /// 当前运行时类型。
 77        /// </summary>
 378        public SessionRuntimeKind RuntimeKind => Runtime.RuntimeKind;
 79
 80        /// <summary>
 81        /// 当前运行时边界。
 82        /// </summary>
 483        public SessionRuntimeBoundary RuntimeBoundary => Runtime.RuntimeBoundary;
 84
 985        public SessionRunner(SessionRuntime session)
 86        {
 987            Runtime = session ?? throw new ArgumentNullException(nameof(session));
 988            Name = session.GetType().Name;
 989            session.BindContextRunnerName(Name);
 990            State = session.IsRunning ? SessionRunnerState.Running : SessionRunnerState.Created;
 991            _ownsRuntime = false;
 992            _lifecycleHooks = new SessionRunnerLifecycleHooks();
 993        }
 94
 2095        public SessionRunner(SessionRunnerDefinition definition)
 96        {
 2097            Definition = definition ?? throw new ArgumentNullException(nameof(definition));
 2098            Name = definition.RunnerName;
 2099            _lifecycleHooks = definition.LifecycleHooks.Clone();
 20100            Runtime = definition.BuildRuntime();
 101
 102            try
 103            {
 20104                InvokeRuntimeCreated(Runtime);
 20105            }
 0106            catch
 107            {
 0108                Runtime.Dispose();
 0109                throw;
 110            }
 111
 20112            State = SessionRunnerState.Created;
 20113            _ownsRuntime = true;
 20114        }
 115
 116        /// <summary>
 117        /// 启动运行器。
 118        /// 该方法是幂等的。
 119        /// </summary>
 120        public void Start()
 121        {
 17122            ThrowIfDisposed();
 123
 16124            if (Runtime.IsRunning)
 125            {
 1126                State = SessionRunnerState.Running;
 1127                return;
 128            }
 129
 130            try
 131            {
 15132                InvokeLifecycleHook(_lifecycleHooks.BeforeStart, "BeforeStart");
 14133                Runtime.Start();
 13134                State = SessionRunnerState.Running;
 13135                InvokeLifecycleHook(_lifecycleHooks.AfterStart, "AfterStart");
 13136            }
 2137            catch (Exception ex)
 138            {
 2139                HandleFailure(ResolveFailureKind(SessionRunnerFailureKind.Start, ex), ResolveOperationName("Start", ex),
 2140                throw;
 141            }
 13142        }
 143
 144        /// <summary>
 145        /// 推进一步固定帧。
 146        /// </summary>
 147        public void Step()
 148        {
 21149            ThrowIfDisposed();
 150
 20151            if (!Runtime.IsRunning)
 152            {
 1153                throw new InvalidOperationException("SessionRunner must be started before stepping.");
 154            }
 155
 156            try
 157            {
 19158                Runtime.Update();
 18159            }
 1160            catch (Exception ex)
 161            {
 1162                HandleFailure(ResolveFailureKind(SessionRunnerFailureKind.Step, ex), ResolveOperationName("Step", ex), e
 1163                throw;
 164            }
 18165        }
 166
 167        /// <summary>
 168        /// 推进多个固定帧。
 169        /// </summary>
 170        public void Step(int stepCount)
 171        {
 5172            ThrowIfDisposed();
 173
 5174            if (stepCount < 0)
 175            {
 1176                throw new ArgumentOutOfRangeException(nameof(stepCount));
 177            }
 178
 20179            for (int i = 0; i < stepCount; i++)
 180            {
 6181                Step();
 182            }
 4183        }
 184
 185        /// <summary>
 186        /// 停止运行器。
 187        /// 该方法是幂等的。
 188        /// </summary>
 189        public void Stop()
 190        {
 7191            ThrowIfDisposed();
 6192            StopCore(SessionRunnerShutdownCause.HostStopRequested, failureKind: SessionRunnerFailureKind.Stop, operation
 6193        }
 194
 195        /// <summary>
 196        /// 依据当前定义重建一个新的运行时实例。
 197        /// 仅适用于由 `SessionRunnerDefinition` 创建的 runner。
 198        /// </summary>
 199        public void ResetRuntime()
 200        {
 4201            ThrowIfDisposed();
 202
 4203            if (Definition == null)
 204            {
 1205                throw new InvalidOperationException("ResetRuntime is only available for runners created from SessionRunn
 206            }
 207
 3208            SessionRunnerResetReason reason = SessionRunnerResetReason.HostRequested;
 209
 210            try
 211            {
 3212                InvokeLifecycleHook(_lifecycleHooks.BeforeReset, reason, "BeforeReset");
 213
 3214                if (Runtime.IsRunning)
 215                {
 1216                    StopCore(SessionRunnerShutdownCause.RuntimeReset, failureKind: SessionRunnerFailureKind.Stop, operat
 217                }
 218                else
 219                {
 2220                    LastShutdownCause = SessionRunnerShutdownCause.RuntimeReset;
 221                }
 222
 3223                SessionRuntime previousRuntime = Runtime;
 3224                SessionRuntime nextRuntime = Definition.BuildRuntime();
 225
 226                try
 227                {
 3228                    InvokeRuntimeCreated(nextRuntime);
 3229                }
 0230                catch
 231                {
 0232                    nextRuntime.Dispose();
 0233                    throw;
 234                }
 235
 3236                Runtime = nextRuntime;
 3237                previousRuntime.Dispose();
 3238                State = SessionRunnerState.Created;
 3239                LastResetReason = reason;
 3240                InvokeLifecycleHook(_lifecycleHooks.AfterReset, reason, "AfterReset");
 3241            }
 0242            catch (Exception ex)
 243            {
 0244                HandleFailure(ResolveFailureKind(SessionRunnerFailureKind.Reset, ex), ResolveOperationName("ResetRuntime
 0245                throw;
 246            }
 3247        }
 248
 249        public void Dispose()
 250        {
 30251            if (_disposed)
 252            {
 1253                return;
 254            }
 255
 256            try
 257            {
 29258                if (Runtime.IsRunning)
 259                {
 7260                    StopCore(SessionRunnerShutdownCause.RunnerDisposed, failureKind: SessionRunnerFailureKind.Dispose, o
 261                }
 262
 29263                if (_ownsRuntime)
 264                {
 20265                    Runtime.Dispose();
 266                }
 29267            }
 0268            catch (Exception ex)
 269            {
 0270                HandleFailure(ResolveFailureKind(SessionRunnerFailureKind.Dispose, ex), ResolveOperationName("Dispose", 
 0271                throw;
 272            }
 273
 29274            State = SessionRunnerState.Disposed;
 29275            _disposed = true;
 29276        }
 277
 278        private void StopCore(SessionRunnerShutdownCause cause, SessionRunnerFailureKind failureKind, string operationNa
 279        {
 14280            if (!Runtime.IsRunning)
 281            {
 2282                if (cause == SessionRunnerShutdownCause.HostStopRequested &&
 2283                    (State == SessionRunnerState.Created || State == SessionRunnerState.Stopped))
 284                {
 2285                    return;
 286                }
 287
 0288                if (cause != SessionRunnerShutdownCause.None)
 289                {
 0290                    LastShutdownCause = cause;
 291                }
 292
 0293                return;
 294            }
 295
 296            try
 297            {
 12298                InvokeLifecycleHook(_lifecycleHooks.BeforeStop, cause, "BeforeStop");
 12299                Runtime.Stop();
 12300                State = SessionRunnerState.Stopped;
 12301                LastShutdownCause = cause;
 12302                InvokeLifecycleHook(_lifecycleHooks.AfterStop, cause, "AfterStop");
 12303            }
 0304            catch (Exception ex)
 305            {
 0306                HandleFailure(ResolveFailureKind(failureKind, ex), ResolveOperationName(operationName, ex), ex, cause, s
 0307                throw;
 308            }
 12309        }
 310
 311        private void HandleFailure(
 312            SessionRunnerFailureKind kind,
 313            string operationName,
 314            Exception exception,
 315            SessionRunnerShutdownCause shutdownCause,
 316            bool stopRuntimeIfNeeded)
 317        {
 3318            LastFailure = new SessionRunnerFailureInfo(kind, operationName, exception);
 3319            State = SessionRunnerState.Faulted;
 320
 3321            if (shutdownCause != SessionRunnerShutdownCause.None)
 322            {
 3323                LastShutdownCause = shutdownCause;
 324            }
 325
 3326            if (stopRuntimeIfNeeded && Runtime.IsRunning)
 327            {
 2328                TryStopAfterFailure(shutdownCause);
 329            }
 330
 3331            TryInvokeFailureHook(LastFailure);
 3332        }
 333
 334        private void TryStopAfterFailure(SessionRunnerShutdownCause cause)
 335        {
 336            try
 337            {
 2338                InvokeLifecycleHook(_lifecycleHooks.BeforeStop, cause, "BeforeStop");
 2339            }
 0340            catch
 341            {
 0342            }
 343
 344            try
 345            {
 2346                Runtime.Stop();
 2347            }
 0348            catch
 349            {
 0350            }
 351
 352            try
 353            {
 2354                InvokeLifecycleHook(_lifecycleHooks.AfterStop, cause, "AfterStop");
 2355            }
 0356            catch
 357            {
 0358            }
 2359        }
 360
 361        private void InvokeRuntimeCreated(SessionRuntime runtime)
 362        {
 23363            if (_lifecycleHooks.RuntimeCreated == null)
 364            {
 20365                return;
 366            }
 367
 368            try
 369            {
 3370                _lifecycleHooks.RuntimeCreated(this, runtime);
 3371            }
 0372            catch (Exception ex)
 373            {
 0374                throw new SessionRunnerLifecycleHookException("RuntimeCreated", ex);
 375            }
 3376        }
 377
 378        private void InvokeLifecycleHook(Action<SessionRunner>? hook, string hookName)
 379        {
 28380            if (hook == null)
 381            {
 25382                return;
 383            }
 384
 385            try
 386            {
 3387                hook(this);
 2388            }
 1389            catch (Exception ex)
 390            {
 1391                throw new SessionRunnerLifecycleHookException(hookName, ex);
 392            }
 2393        }
 394
 395        private void InvokeLifecycleHook<TArg>(Action<SessionRunner, TArg>? hook, TArg argument, string hookName)
 396        {
 34397            if (hook == null)
 398            {
 30399                return;
 400            }
 401
 402            try
 403            {
 4404                hook(this, argument);
 4405            }
 0406            catch (Exception ex)
 407            {
 0408                throw new SessionRunnerLifecycleHookException(hookName, ex);
 409            }
 4410        }
 411
 412        private void TryInvokeFailureHook(SessionRunnerFailureInfo failure)
 413        {
 3414            if (_lifecycleHooks.Failure == null)
 415            {
 1416                return;
 417            }
 418
 419            try
 420            {
 2421                _lifecycleHooks.Failure(this, failure);
 2422            }
 0423            catch
 424            {
 0425            }
 2426        }
 427
 428        private void ThrowIfDisposed()
 429        {
 54430            if (_disposed)
 431            {
 3432                throw new ObjectDisposedException(nameof(SessionRunner));
 433            }
 51434        }
 435
 436        private static SessionRunnerFailureKind ResolveFailureKind(SessionRunnerFailureKind defaultKind, Exception excep
 437        {
 3438            return exception is SessionRunnerLifecycleHookException
 3439                ? SessionRunnerFailureKind.LifecycleHook
 3440                : defaultKind;
 441        }
 442
 443        private static string ResolveOperationName(string defaultOperationName, Exception exception)
 444        {
 3445            return exception is SessionRunnerLifecycleHookException hookException
 3446                ? hookException.HookName
 3447                : defaultOperationName;
 448        }
 449
 450        private sealed class SessionRunnerLifecycleHookException : InvalidOperationException
 451        {
 452            public SessionRunnerLifecycleHookException(string hookName, Exception innerException)
 1453                : base($"SessionRunner lifecycle hook {hookName} failed.", innerException)
 454            {
 1455                HookName = hookName;
 1456            }
 457
 1458            public string HookName { get; }
 459        }
 460    }
 461}