| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.ComponentModel; |
| | | 4 | | using Lattice.ECS.Framework; |
| | | 5 | | using Lattice.Math; |
| | | 6 | | |
| | | 7 | | namespace Lattice.ECS.Session |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// 会话运行器构建器。 |
| | | 11 | | /// 为代码侧提供最小但正式的运行时装配入口。 |
| | | 12 | | /// 当前构建目标是 `SessionRuntime`,默认实例为 `MinimalPredictionSession`。 |
| | | 13 | | /// </summary> |
| | | 14 | | public sealed class SessionRunnerBuilder |
| | | 15 | | { |
| | 30 | 16 | | private readonly List<ISystem> _systems = new(); |
| | 30 | 17 | | private readonly List<SessionRuntimeContextConfigurator> _contextConfigurators = new(); |
| | 30 | 18 | | private readonly SessionRunnerLifecycleHooks _lifecycleHooks = new(); |
| | | 19 | | private SessionRuntimeFactory? _runtimeFactory; |
| | 30 | 20 | | private SessionRuntimeOptions _runtimeOptions = SessionRuntimeOptions.Default; |
| | 30 | 21 | | private string _runnerName = "Default"; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// 设置运行器名称。 |
| | | 25 | | /// </summary> |
| | | 26 | | public SessionRunnerBuilder WithRunnerName(string runnerName) |
| | | 27 | | { |
| | 12 | 28 | | if (string.IsNullOrWhiteSpace(runnerName)) |
| | | 29 | | { |
| | 2 | 30 | | throw new ArgumentException("Runner name cannot be null or whitespace.", nameof(runnerName)); |
| | | 31 | | } |
| | | 32 | | |
| | 10 | 33 | | _runnerName = runnerName; |
| | 10 | 34 | | return this; |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// 设置固定时间步长。 |
| | | 39 | | /// </summary> |
| | | 40 | | public SessionRunnerBuilder WithDeltaTime(FP deltaTime) |
| | | 41 | | { |
| | 9 | 42 | | _runtimeOptions = _runtimeOptions.WithDeltaTime(deltaTime); |
| | 9 | 43 | | return this; |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// 设置本地玩家 ID。 |
| | | 48 | | /// </summary> |
| | | 49 | | public SessionRunnerBuilder WithLocalPlayerId(int localPlayerId) |
| | | 50 | | { |
| | 4 | 51 | | _runtimeOptions = _runtimeOptions.WithLocalPlayerId(localPlayerId); |
| | 4 | 52 | | return this; |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// 设置运行时基础配置。 |
| | | 57 | | /// 当前仅承载稳定公开的基础参数,不提前暴露历史窗口和采样策略等内部实现细节。 |
| | | 58 | | /// </summary> |
| | | 59 | | public SessionRunnerBuilder WithRuntimeOptions(SessionRuntimeOptions runtimeOptions) |
| | | 60 | | { |
| | 9 | 61 | | _runtimeOptions = runtimeOptions ?? throw new ArgumentNullException(nameof(runtimeOptions)); |
| | 8 | 62 | | return this; |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// 设置自定义运行时工厂。 |
| | | 67 | | /// </summary> |
| | | 68 | | public SessionRunnerBuilder WithRuntimeFactory(SessionRuntimeFactory runtimeFactory) |
| | | 69 | | { |
| | 10 | 70 | | _runtimeFactory = runtimeFactory ?? throw new ArgumentNullException(nameof(runtimeFactory)); |
| | 9 | 71 | | return this; |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// 设置自定义运行时工厂。 |
| | | 76 | | /// 该重载保留旧的 `(deltaTime, localPlayerId)` 形式,内部会适配到 `SessionRuntimeOptions` 工厂。 |
| | | 77 | | /// </summary> |
| | | 78 | | [Obsolete("请改用 WithRuntimeFactory(SessionRuntimeFactory)。WithSessionFactory 仅保留为兼容入口。", false)] |
| | | 79 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | | 80 | | public SessionRunnerBuilder WithSessionFactory(Func<FP, int, SessionRuntime> sessionFactory) |
| | | 81 | | { |
| | 0 | 82 | | ArgumentNullException.ThrowIfNull(sessionFactory); |
| | 0 | 83 | | return WithRuntimeFactory(options => sessionFactory(options.DeltaTime, options.LocalPlayerId)); |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// 注册系统。 |
| | | 88 | | /// </summary> |
| | | 89 | | public SessionRunnerBuilder AddSystem(ISystem system) |
| | | 90 | | { |
| | 9 | 91 | | ArgumentNullException.ThrowIfNull(system); |
| | 8 | 92 | | _systems.Add(system); |
| | 8 | 93 | | return this; |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// 配置运行时上下文。 |
| | | 98 | | /// 用于挂载显式声明的 runtime shared service, |
| | | 99 | | /// 而不是继续侵入 `Frame` / `SessionRuntime` 或把 Context 当成万能对象包。 |
| | | 100 | | /// </summary> |
| | | 101 | | public SessionRunnerBuilder ConfigureContext(SessionRuntimeContextConfigurator configurator) |
| | | 102 | | { |
| | 4 | 103 | | ArgumentNullException.ThrowIfNull(configurator); |
| | 4 | 104 | | _contextConfigurators.Add(configurator); |
| | 4 | 105 | | return this; |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// 配置宿主生命周期钩子。 |
| | | 110 | | /// 用于在 runtime 创建、启动、停止、重建与失败收敛时补齐明确的宿主责任边界。 |
| | | 111 | | /// </summary> |
| | | 112 | | public SessionRunnerBuilder ConfigureLifecycle(Action<SessionRunnerLifecycleHooks> configurator) |
| | | 113 | | { |
| | 7 | 114 | | ArgumentNullException.ThrowIfNull(configurator); |
| | 6 | 115 | | configurator(_lifecycleHooks); |
| | 6 | 116 | | return this; |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | /// <summary> |
| | | 120 | | /// 构建不可变运行定义。 |
| | | 121 | | /// </summary> |
| | | 122 | | public SessionRunnerDefinition BuildDefinition() |
| | | 123 | | { |
| | 47 | 124 | | SessionRuntimeFactory runtimeFactory = _runtimeFactory ?? (options => new MinimalPredictionSession(options)) |
| | 26 | 125 | | return new SessionRunnerDefinition(_runnerName, _runtimeOptions, runtimeFactory, _systems, _contextConfigura |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// 构建运行时实例。 |
| | | 130 | | /// </summary> |
| | | 131 | | public SessionRuntime BuildRuntime() |
| | | 132 | | { |
| | 2 | 133 | | return BuildDefinition().BuildRuntime(); |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// 构建运行时实例。 |
| | | 138 | | /// 保留 `BuildSession()` 名称作为兼容入口。 |
| | | 139 | | /// </summary> |
| | | 140 | | [Obsolete("请改用 BuildRuntime()。BuildSession 仅保留为兼容入口。", false)] |
| | | 141 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | | 142 | | public SessionRuntime BuildSession() |
| | | 143 | | { |
| | 0 | 144 | | return BuildRuntime(); |
| | | 145 | | } |
| | | 146 | | |
| | | 147 | | /// <summary> |
| | | 148 | | /// 构建运行器。 |
| | | 149 | | /// </summary> |
| | | 150 | | public SessionRunner Build() |
| | | 151 | | { |
| | 13 | 152 | | return BuildDefinition().CreateRunner(); |
| | | 153 | | } |
| | | 154 | | } |
| | | 155 | | } |