| | | 1 | | using System; |
| | | 2 | | |
| | | 3 | | namespace Lattice.ECS.Framework |
| | | 4 | | { |
| | | 5 | | /// <summary> |
| | | 6 | | /// 系统对普通帧状态的访问意图。 |
| | | 7 | | /// 当前主要用于声明系统作者契约,而不是提供一套更重的系统基类家族。 |
| | | 8 | | /// </summary> |
| | | 9 | | public enum SystemFrameAccess |
| | | 10 | | { |
| | | 11 | | ReadOnly = 0, |
| | | 12 | | ReadWrite = 1 |
| | | 13 | | } |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// 系统对全局状态 API 的访问意图。 |
| | | 17 | | /// </summary> |
| | | 18 | | public enum SystemGlobalAccess |
| | | 19 | | { |
| | | 20 | | None = 0, |
| | | 21 | | ReadOnly = 1, |
| | | 22 | | ReadWrite = 2 |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// 系统是否允许发起结构性修改。 |
| | | 27 | | /// 当前正式语义只有“不允许”与“允许发起延迟结构性修改”两档。 |
| | | 28 | | /// </summary> |
| | | 29 | | public enum SystemStructuralChangeAccess |
| | | 30 | | { |
| | | 31 | | None = 0, |
| | | 32 | | Deferred = 1 |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// 轻量系统作者契约。 |
| | | 37 | | /// 用于声明系统在 `OnUpdate(...)` 中允许使用的状态访问能力, |
| | | 38 | | /// 让 phase、global state 与结构性修改规则不再只靠团队默契维持。 |
| | | 39 | | /// </summary> |
| | | 40 | | public readonly struct SystemAuthoringContract : IEquatable<SystemAuthoringContract> |
| | | 41 | | { |
| | | 42 | | public SystemAuthoringContract( |
| | | 43 | | SystemFrameAccess frameAccess = SystemFrameAccess.ReadWrite, |
| | | 44 | | SystemGlobalAccess globalAccess = SystemGlobalAccess.None, |
| | | 45 | | SystemStructuralChangeAccess structuralChangeAccess = SystemStructuralChangeAccess.None) |
| | | 46 | | { |
| | 365 | 47 | | if (frameAccess == SystemFrameAccess.ReadOnly && globalAccess == SystemGlobalAccess.ReadWrite) |
| | | 48 | | { |
| | 0 | 49 | | throw new ArgumentException("Read-only systems cannot declare writable global state access.", nameof(glo |
| | | 50 | | } |
| | | 51 | | |
| | 365 | 52 | | if (frameAccess == SystemFrameAccess.ReadOnly && structuralChangeAccess != SystemStructuralChangeAccess.None |
| | | 53 | | { |
| | 0 | 54 | | throw new ArgumentException("Read-only systems cannot declare structural change access.", nameof(structu |
| | | 55 | | } |
| | | 56 | | |
| | 365 | 57 | | FrameAccess = frameAccess; |
| | 365 | 58 | | GlobalAccess = globalAccess; |
| | 365 | 59 | | StructuralChangeAccess = structuralChangeAccess; |
| | 365 | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// 默认系统契约: |
| | | 64 | | /// 允许读写普通组件数据,但不默认开放 global state 与结构性修改。 |
| | | 65 | | /// </summary> |
| | 3484 | 66 | | public static SystemAuthoringContract Default => new(); |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// 只读系统默认契约。 |
| | | 70 | | /// </summary> |
| | 0 | 71 | | public static SystemAuthoringContract ReadOnly => new(SystemFrameAccess.ReadOnly); |
| | | 72 | | |
| | 2 | 73 | | public SystemFrameAccess FrameAccess { get; } |
| | | 74 | | |
| | 331 | 75 | | public SystemGlobalAccess GlobalAccess { get; } |
| | | 76 | | |
| | 1322 | 77 | | public SystemStructuralChangeAccess StructuralChangeAccess { get; } |
| | | 78 | | |
| | 104 | 79 | | public bool AllowsGlobalReads => GlobalAccess != SystemGlobalAccess.None; |
| | | 80 | | |
| | 225 | 81 | | public bool AllowsGlobalWrites => GlobalAccess == SystemGlobalAccess.ReadWrite; |
| | | 82 | | |
| | 1320 | 83 | | public bool AllowsStructuralChanges => StructuralChangeAccess == SystemStructuralChangeAccess.Deferred; |
| | | 84 | | |
| | | 85 | | public bool Equals(SystemAuthoringContract other) |
| | | 86 | | { |
| | 1 | 87 | | return FrameAccess == other.FrameAccess && |
| | 1 | 88 | | GlobalAccess == other.GlobalAccess && |
| | 1 | 89 | | StructuralChangeAccess == other.StructuralChangeAccess; |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | public override bool Equals(object? obj) |
| | | 93 | | { |
| | 0 | 94 | | return obj is SystemAuthoringContract other && Equals(other); |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | public override int GetHashCode() |
| | | 98 | | { |
| | 0 | 99 | | return HashCode.Combine((int)FrameAccess, (int)GlobalAccess, (int)StructuralChangeAccess); |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | internal void ValidateForPhase(SystemPhase phase, Type systemType) |
| | | 103 | | { |
| | 129 | 104 | | if (!AllowsGlobalWrites) |
| | | 105 | | { |
| | 123 | 106 | | return; |
| | | 107 | | } |
| | | 108 | | |
| | 6 | 109 | | if (phase == SystemPhase.Input || phase == SystemPhase.Resolve || phase == SystemPhase.Cleanup) |
| | | 110 | | { |
| | 5 | 111 | | return; |
| | | 112 | | } |
| | | 113 | | |
| | 1 | 114 | | throw new InvalidOperationException( |
| | 1 | 115 | | $"System {systemType.Name} declares writable global state access, but phase {phase} only supports global |
| | | 116 | | } |
| | | 117 | | } |
| | | 118 | | } |