| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.ComponentModel; |
| | | 4 | | |
| | | 5 | | namespace Lattice.ECS.Session |
| | | 6 | | { |
| | | 7 | | /// <summary> |
| | | 8 | | /// 当前运行时正式推荐的玩家输入模型。 |
| | | 9 | | /// 输入对象本身只表达“哪个玩家、哪个 tick 的输入状态是什么”, |
| | | 10 | | /// 不再把传输层 payload 序列化职责直接绑进核心输入接口。 |
| | | 11 | | /// </summary> |
| | | 12 | | public interface IPlayerInput |
| | | 13 | | { |
| | | 14 | | int PlayerId { get; } |
| | | 15 | | |
| | | 16 | | int Tick { get; } |
| | | 17 | | } |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// 输入 payload 的外层编解码适配器。 |
| | | 21 | | /// 当运行时需要把输入状态与网络 / 文件 / 回放 payload 对接时,应通过外层 codec 完成, |
| | | 22 | | /// 而不是把序列化职责重新塞回 `IPlayerInput` 主契约。 |
| | | 23 | | /// 若该 codec 会形成稳定 payload 协议,请优先实现 `IVersionedInputPayloadCodec{TInput}`, |
| | | 24 | | /// 显式声明 codec 身份与版本。 |
| | | 25 | | /// </summary> |
| | | 26 | | public interface IInputPayloadCodec<TInput> |
| | | 27 | | where TInput : IPlayerInput |
| | | 28 | | { |
| | | 29 | | byte[] Serialize(TInput input); |
| | | 30 | | |
| | | 31 | | TInput Deserialize(int playerId, int tick, byte[] payload); |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// 兼容旧输入接口。 |
| | | 36 | | /// 新代码默认应优先使用 `IPlayerInput` 与外层 `IInputPayloadCodec{TInput}`。 |
| | | 37 | | /// </summary> |
| | | 38 | | [Obsolete("Use IPlayerInput for the runtime input model and move payload serialization into IInputPayloadCodec<TInpu |
| | | 39 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | | 40 | | public interface IInputCommand : IPlayerInput |
| | | 41 | | { |
| | | 42 | | byte[] Serialize(); |
| | | 43 | | |
| | | 44 | | void Deserialize(byte[] data); |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// 当前运行时正式承诺的输入契约模型类型。 |
| | | 49 | | /// </summary> |
| | | 50 | | public enum SessionInputContractKind |
| | | 51 | | { |
| | | 52 | | /// <summary> |
| | | 53 | | /// 以 tick 为范围聚合多个玩家输入,形成稳定 `SessionInputSet`。 |
| | | 54 | | /// </summary> |
| | | 55 | | TickScopedPlayerInputSet = 0 |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// 缺失输入的正式语义。 |
| | | 60 | | /// </summary> |
| | | 61 | | public enum SessionMissingInputPolicy |
| | | 62 | | { |
| | | 63 | | /// <summary> |
| | | 64 | | /// 缺失输入不会自动补默认值或沿用上一帧输入,而是直接从当前 tick 的输入集中省略。 |
| | | 65 | | /// </summary> |
| | | 66 | | OmitMissingPlayers = 0 |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// 同一 `(playerId, tick)` 被重复写入时的正式语义。 |
| | | 71 | | /// </summary> |
| | | 72 | | public enum SessionInputWritePolicy |
| | | 73 | | { |
| | | 74 | | /// <summary> |
| | | 75 | | /// 同一玩家同一 tick 的输入以最后一次写入为准。 |
| | | 76 | | /// </summary> |
| | | 77 | | LatestWriteWins = 0 |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// 当前运行时对 tick 内玩家输入遍历顺序的正式语义。 |
| | | 82 | | /// </summary> |
| | | 83 | | public enum SessionInputOrder |
| | | 84 | | { |
| | | 85 | | /// <summary> |
| | | 86 | | /// 按玩家 ID 升序遍历输入,避免依赖字典枚举顺序。 |
| | | 87 | | /// </summary> |
| | | 88 | | PlayerIdAscending = 0 |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// 当前运行时正式承诺的输入契约能力。 |
| | | 93 | | /// </summary> |
| | | 94 | | [Flags] |
| | | 95 | | public enum SessionRuntimeInputCapability |
| | | 96 | | { |
| | | 97 | | None = 0, |
| | | 98 | | TickScopedInputAggregation = 1 << 0, |
| | | 99 | | SparsePlayerInput = 1 << 1, |
| | | 100 | | StablePlayerOrdering = 1 << 2, |
| | | 101 | | LatestWriteWins = 1 << 3, |
| | | 102 | | TransportDecoupledInputModel = 1 << 4 |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// 当前运行时明确不承诺的输入契约能力。 |
| | | 107 | | /// </summary> |
| | | 108 | | [Flags] |
| | | 109 | | public enum UnsupportedSessionRuntimeInputCapability |
| | | 110 | | { |
| | | 111 | | None = 0, |
| | | 112 | | ImplicitPreviousInputCarryForward = 1 << 0, |
| | | 113 | | BuiltInDefaultInputSynthesis = 1 << 1, |
| | | 114 | | ConfigurableInputAggregation = 1 << 2, |
| | | 115 | | ConfigurableMissingInputPolicy = 1 << 3, |
| | | 116 | | BuiltInTransportSerialization = 1 << 4 |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | /// <summary> |
| | | 120 | | /// 当前运行时的正式输入契约边界描述。 |
| | | 121 | | /// 用于把“输入模型、聚合顺序、缺失语义、重复写入语义”显式暴露在代码层。 |
| | | 122 | | /// </summary> |
| | | 123 | | public sealed class SessionRuntimeInputBoundary |
| | | 124 | | { |
| | | 125 | | public SessionRuntimeInputBoundary( |
| | | 126 | | SessionInputContractKind contractKind, |
| | | 127 | | SessionMissingInputPolicy missingInputPolicy, |
| | | 128 | | SessionInputWritePolicy writePolicy, |
| | | 129 | | SessionInputOrder playerOrder, |
| | | 130 | | SessionRuntimeInputCapability supportedCapabilities, |
| | | 131 | | UnsupportedSessionRuntimeInputCapability unsupportedCapabilities) |
| | | 132 | | { |
| | | 133 | | ContractKind = contractKind; |
| | | 134 | | MissingInputPolicy = missingInputPolicy; |
| | | 135 | | WritePolicy = writePolicy; |
| | | 136 | | PlayerOrder = playerOrder; |
| | | 137 | | SupportedCapabilities = supportedCapabilities; |
| | | 138 | | UnsupportedCapabilities = unsupportedCapabilities; |
| | | 139 | | } |
| | | 140 | | |
| | | 141 | | public SessionInputContractKind ContractKind { get; } |
| | | 142 | | |
| | | 143 | | public SessionMissingInputPolicy MissingInputPolicy { get; } |
| | | 144 | | |
| | | 145 | | public SessionInputWritePolicy WritePolicy { get; } |
| | | 146 | | |
| | | 147 | | public SessionInputOrder PlayerOrder { get; } |
| | | 148 | | |
| | | 149 | | public SessionRuntimeInputCapability SupportedCapabilities { get; } |
| | | 150 | | |
| | | 151 | | public UnsupportedSessionRuntimeInputCapability UnsupportedCapabilities { get; } |
| | | 152 | | |
| | | 153 | | public bool Supports(SessionRuntimeInputCapability capability) |
| | | 154 | | { |
| | | 155 | | return (SupportedCapabilities & capability) == capability; |
| | | 156 | | } |
| | | 157 | | |
| | | 158 | | public bool ExplicitlyDoesNotSupport(UnsupportedSessionRuntimeInputCapability capability) |
| | | 159 | | { |
| | | 160 | | return (UnsupportedCapabilities & capability) == capability; |
| | | 161 | | } |
| | | 162 | | } |
| | | 163 | | |
| | | 164 | | /// <summary> |
| | | 165 | | /// 单个玩家在某个 tick 内的输入条目。 |
| | | 166 | | /// </summary> |
| | | 167 | | public readonly struct SessionPlayerInput |
| | | 168 | | { |
| | | 169 | | public SessionPlayerInput(IPlayerInput input) |
| | | 170 | | { |
| | | 171 | | ArgumentNullException.ThrowIfNull(input); |
| | | 172 | | Input = input; |
| | | 173 | | } |
| | | 174 | | |
| | | 175 | | public IPlayerInput Input { get; } |
| | | 176 | | |
| | | 177 | | public int PlayerId => Input.PlayerId; |
| | | 178 | | |
| | | 179 | | public int Tick => Input.Tick; |
| | | 180 | | |
| | | 181 | | public bool TryGet<TInput>(out TInput? input) |
| | | 182 | | where TInput : class, IPlayerInput |
| | | 183 | | { |
| | | 184 | | input = Input as TInput; |
| | | 185 | | return input != null; |
| | | 186 | | } |
| | | 187 | | } |
| | | 188 | | |
| | | 189 | | /// <summary> |
| | | 190 | | /// 某个 tick 聚合后的输入集合。 |
| | | 191 | | /// 当前正式语义是:仅包含实际写入的玩家输入,并按玩家 ID 升序稳定遍历。 |
| | | 192 | | /// </summary> |
| | | 193 | | public readonly struct SessionInputSet |
| | | 194 | | { |
| | | 195 | | private readonly List<SessionPlayerInput>? _inputs; |
| | | 196 | | |
| | | 197 | | internal SessionInputSet(int tick, List<SessionPlayerInput> inputs) |
| | | 198 | | { |
| | 625 | 199 | | Tick = tick; |
| | 625 | 200 | | _inputs = inputs; |
| | 625 | 201 | | } |
| | | 202 | | |
| | 103 | 203 | | public int Tick { get; } |
| | | 204 | | |
| | 0 | 205 | | public int Count => _inputs?.Count ?? 0; |
| | | 206 | | |
| | 0 | 207 | | public SessionPlayerInput this[int index] => _inputs![index]; |
| | | 208 | | |
| | | 209 | | public bool TryGetPlayerInput<TInput>(int playerId, out TInput? input) |
| | | 210 | | where TInput : class, IPlayerInput |
| | | 211 | | { |
| | 595 | 212 | | if (_inputs != null) |
| | | 213 | | { |
| | 1194 | 214 | | for (int i = 0; i < _inputs.Count; i++) |
| | | 215 | | { |
| | 409 | 216 | | if (_inputs[i].PlayerId == playerId && _inputs[i].TryGet(out input)) |
| | | 217 | | { |
| | 407 | 218 | | return true; |
| | | 219 | | } |
| | | 220 | | } |
| | | 221 | | } |
| | | 222 | | |
| | 188 | 223 | | input = null; |
| | 188 | 224 | | return false; |
| | | 225 | | } |
| | | 226 | | |
| | | 227 | | public Enumerator GetEnumerator() |
| | | 228 | | { |
| | 1 | 229 | | return new Enumerator(_inputs); |
| | | 230 | | } |
| | | 231 | | |
| | | 232 | | public struct Enumerator |
| | | 233 | | { |
| | | 234 | | private readonly List<SessionPlayerInput>? _inputs; |
| | | 235 | | private int _index; |
| | | 236 | | |
| | | 237 | | internal Enumerator(List<SessionPlayerInput>? inputs) |
| | | 238 | | { |
| | 1 | 239 | | _inputs = inputs; |
| | 1 | 240 | | _index = -1; |
| | 1 | 241 | | } |
| | | 242 | | |
| | 2 | 243 | | public SessionPlayerInput Current => _inputs![_index]; |
| | | 244 | | |
| | | 245 | | public bool MoveNext() |
| | | 246 | | { |
| | 3 | 247 | | if (_inputs == null) |
| | | 248 | | { |
| | 0 | 249 | | return false; |
| | | 250 | | } |
| | | 251 | | |
| | 3 | 252 | | int next = _index + 1; |
| | 3 | 253 | | if (next >= _inputs.Count) |
| | | 254 | | { |
| | 1 | 255 | | return false; |
| | | 256 | | } |
| | | 257 | | |
| | 2 | 258 | | _index = next; |
| | 2 | 259 | | return true; |
| | | 260 | | } |
| | | 261 | | } |
| | | 262 | | } |
| | | 263 | | } |