| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using Lattice.ECS.Framework; |
| | | 4 | | |
| | | 5 | | namespace Lattice.ECS.Session |
| | | 6 | | { |
| | | 7 | | /// <summary> |
| | | 8 | | /// SessionRunner 的不可变运行定义。 |
| | | 9 | | /// 承载稳定公开的启动参数、运行时工厂以及系统装配信息。 |
| | | 10 | | /// </summary> |
| | | 11 | | public sealed class SessionRunnerDefinition |
| | | 12 | | { |
| | | 13 | | private readonly SessionRuntimeFactory _runtimeFactory; |
| | | 14 | | private readonly ISystem[] _systems; |
| | | 15 | | private readonly SessionRuntimeContextConfigurator[] _contextConfigurators; |
| | | 16 | | private readonly SessionRunnerLifecycleHooks _lifecycleHooks; |
| | | 17 | | |
| | 26 | 18 | | internal SessionRunnerDefinition( |
| | 26 | 19 | | string runnerName, |
| | 26 | 20 | | SessionRuntimeOptions runtimeOptions, |
| | 26 | 21 | | SessionRuntimeFactory runtimeFactory, |
| | 26 | 22 | | IReadOnlyList<ISystem> systems, |
| | 26 | 23 | | IReadOnlyList<SessionRuntimeContextConfigurator> contextConfigurators, |
| | 26 | 24 | | SessionRunnerLifecycleHooks lifecycleHooks) |
| | | 25 | | { |
| | 26 | 26 | | if (string.IsNullOrWhiteSpace(runnerName)) |
| | | 27 | | { |
| | 0 | 28 | | throw new ArgumentException("Runner name cannot be null or whitespace.", nameof(runnerName)); |
| | | 29 | | } |
| | | 30 | | |
| | 26 | 31 | | RunnerName = runnerName; |
| | 26 | 32 | | RuntimeOptions = runtimeOptions ?? throw new ArgumentNullException(nameof(runtimeOptions)); |
| | 26 | 33 | | _runtimeFactory = runtimeFactory ?? throw new ArgumentNullException(nameof(runtimeFactory)); |
| | 26 | 34 | | ArgumentNullException.ThrowIfNull(systems); |
| | 26 | 35 | | ArgumentNullException.ThrowIfNull(contextConfigurators); |
| | | 36 | | |
| | 26 | 37 | | _systems = new ISystem[systems.Count]; |
| | 26 | 38 | | if (_systems.Length > 0) |
| | | 39 | | { |
| | 24 | 40 | | for (int i = 0; i < _systems.Length; i++) |
| | | 41 | | { |
| | 8 | 42 | | _systems[i] = systems[i] ?? throw new ArgumentNullException(nameof(systems), "System registration ca |
| | | 43 | | } |
| | | 44 | | } |
| | | 45 | | |
| | 26 | 46 | | _contextConfigurators = new SessionRuntimeContextConfigurator[contextConfigurators.Count]; |
| | 60 | 47 | | for (int i = 0; i < _contextConfigurators.Length; i++) |
| | | 48 | | { |
| | 4 | 49 | | _contextConfigurators[i] = contextConfigurators[i] |
| | 4 | 50 | | ?? throw new ArgumentNullException(nameof(contextConfigurators), "Context configurator cannot be nul |
| | | 51 | | } |
| | | 52 | | |
| | 26 | 53 | | _lifecycleHooks = (lifecycleHooks ?? throw new ArgumentNullException(nameof(lifecycleHooks))).Clone(); |
| | 26 | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// 运行器名称。 |
| | | 58 | | /// 用于标识一个稳定的运行入口,而不是具体某一帧状态对象。 |
| | | 59 | | /// </summary> |
| | 50 | 60 | | public string RunnerName { get; } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// 稳定公开的运行时参数。 |
| | | 64 | | /// </summary> |
| | 91 | 65 | | public SessionRuntimeOptions RuntimeOptions { get; } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// 当前定义中注册的系统数量。 |
| | | 69 | | /// </summary> |
| | 0 | 70 | | public int SystemCount => _systems.Length; |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// 当前定义中注册的上下文配置器数量。 |
| | | 74 | | /// </summary> |
| | 0 | 75 | | public int ContextConfiguratorCount => _contextConfigurators.Length; |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// 按定义创建一个新的运行时实例并完成系统装配。 |
| | | 79 | | /// </summary> |
| | | 80 | | public SessionRuntime BuildRuntime() |
| | | 81 | | { |
| | 30 | 82 | | SessionRuntime runtime = _runtimeFactory(RuntimeOptions) |
| | 30 | 83 | | ?? throw new InvalidOperationException("Session runtime factory returned null."); |
| | | 84 | | |
| | 30 | 85 | | if (runtime.DeltaTime != RuntimeOptions.DeltaTime || |
| | 30 | 86 | | runtime.LocalPlayerId != RuntimeOptions.LocalPlayerId) |
| | | 87 | | { |
| | 1 | 88 | | throw new InvalidOperationException( |
| | 1 | 89 | | "Session runtime factory returned a runtime whose stable public parameters do not match SessionRunne |
| | | 90 | | } |
| | | 91 | | |
| | 29 | 92 | | runtime.BindContextRunnerName(RunnerName); |
| | | 93 | | |
| | 70 | 94 | | for (int i = 0; i < _contextConfigurators.Length; i++) |
| | | 95 | | { |
| | 6 | 96 | | _contextConfigurators[i](runtime.Context); |
| | | 97 | | } |
| | | 98 | | |
| | 74 | 99 | | for (int i = 0; i < _systems.Length; i++) |
| | | 100 | | { |
| | 8 | 101 | | runtime.RegisterSystem(_systems[i]); |
| | | 102 | | } |
| | | 103 | | |
| | 29 | 104 | | return runtime; |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | /// <summary> |
| | | 108 | | /// 按定义创建运行器。 |
| | | 109 | | /// </summary> |
| | | 110 | | public SessionRunner CreateRunner() |
| | | 111 | | { |
| | 20 | 112 | | return new SessionRunner(this); |
| | | 113 | | } |
| | | 114 | | |
| | 20 | 115 | | internal SessionRunnerLifecycleHooks LifecycleHooks => _lifecycleHooks; |
| | | 116 | | } |
| | | 117 | | } |