| | | 1 | | // Copyright (c) 2026 Sideline Authors. All rights reserved. |
| | | 2 | | // Licensed under GPL-3.0. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using Lattice.Core; |
| | | 6 | | |
| | | 7 | | namespace Lattice.ECS.Core |
| | | 8 | | { |
| | | 9 | | internal unsafe delegate void ComponentCommandDataHandler(Frame frame, EntityRef entity, byte* data); |
| | | 10 | | |
| | | 11 | | internal unsafe delegate void ComponentCommandRemoveHandler(Frame frame, EntityRef entity); |
| | | 12 | | |
| | | 13 | | internal unsafe delegate int ComponentStorageSnapshotSizeHandler(void* storage); |
| | | 14 | | |
| | | 15 | | internal unsafe delegate void ComponentStorageSnapshotWriteHandler(void* storage, byte* buffer, int bufferSize); |
| | | 16 | | |
| | | 17 | | internal unsafe delegate void ComponentStorageSnapshotReadHandler(void* storage, byte* buffer, int bufferSize); |
| | | 18 | | |
| | | 19 | | internal unsafe delegate void* ComponentStorageCreateHandler(Allocator* allocator, int maxEntities, int typeId); |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// 组件命令回放注册表。 |
| | | 23 | | /// 为 CommandBuffer 提供按组件类型 ID 分发的 Add/Set/Remove 入口。 |
| | | 24 | | /// </summary> |
| | | 25 | | internal static unsafe class ComponentCommandRegistry |
| | | 26 | | { |
| | 1 | 27 | | private static readonly int[] ComponentSizes = new int[Frame.MaxComponentTypes]; |
| | 1 | 28 | | private static readonly ComponentCommandDataHandler?[] AddHandlers = new ComponentCommandDataHandler[Frame.MaxCo |
| | 1 | 29 | | private static readonly ComponentCommandDataHandler?[] SetHandlers = new ComponentCommandDataHandler[Frame.MaxCo |
| | 1 | 30 | | private static readonly ComponentCommandRemoveHandler?[] RemoveHandlers = new ComponentCommandRemoveHandler[Fram |
| | 1 | 31 | | private static readonly ComponentStorageSnapshotSizeHandler?[] StorageSnapshotSizeHandlers = new ComponentStorag |
| | 1 | 32 | | private static readonly ComponentStorageSnapshotWriteHandler?[] StorageSnapshotWriteHandlers = new ComponentStor |
| | 1 | 33 | | private static readonly ComponentStorageSnapshotReadHandler?[] StorageSnapshotReadHandlers = new ComponentStorag |
| | 1 | 34 | | private static readonly ComponentStorageCreateHandler?[] StorageCreateHandlers = new ComponentStorageCreateHandl |
| | | 35 | | |
| | | 36 | | public static void Register<T>(int typeId) where T : unmanaged, IComponent |
| | | 37 | | { |
| | 49 | 38 | | ComponentSizes[typeId] = sizeof(T); |
| | 1004 | 39 | | AddHandlers[typeId] = static (frame, entity, data) => frame.Add(entity, *(T*)data); |
| | 49 | 40 | | SetHandlers[typeId] = static (frame, entity, data) => |
| | 49 | 41 | | { |
| | 3 | 42 | | T value = *(T*)data; |
| | 3 | 43 | | if (frame.Has<T>(entity)) |
| | 49 | 44 | | { |
| | 3 | 45 | | frame.Get<T>(entity) = value; |
| | 3 | 46 | | return; |
| | 49 | 47 | | } |
| | 49 | 48 | | |
| | 0 | 49 | | frame.Add(entity, value); |
| | 49 | 50 | | }; |
| | 49 | 51 | | RemoveHandlers[typeId] = static (frame, entity) => |
| | 49 | 52 | | { |
| | 3 | 53 | | if (frame.Has<T>(entity)) |
| | 49 | 54 | | { |
| | 3 | 55 | | frame.Remove<T>(entity); |
| | 49 | 56 | | } |
| | 52 | 57 | | }; |
| | 49 | 58 | | StorageSnapshotSizeHandlers[typeId] = static storage => ((Storage<T>*)storage)->GetSnapshotSize(); |
| | 49 | 59 | | StorageSnapshotWriteHandlers[typeId] = static (storage, buffer, bufferSize) => |
| | 49 | 60 | | ((Storage<T>*)storage)->WriteSnapshot(buffer, bufferSize); |
| | 49 | 61 | | StorageSnapshotReadHandlers[typeId] = static (storage, buffer, bufferSize) => |
| | 49 | 62 | | ((Storage<T>*)storage)->ReadSnapshot(buffer, bufferSize); |
| | 49 | 63 | | StorageCreateHandlers[typeId] = static (allocator, maxEntities, registeredTypeId) => |
| | 49 | 64 | | { |
| | 0 | 65 | | var storage = (Storage<T>*)allocator->Alloc(sizeof(Storage<T>)); |
| | 0 | 66 | | storage->Initialize(maxEntities, allocator, StorageFlags.None, componentTypeId: registeredTypeId); |
| | 0 | 67 | | return storage; |
| | 49 | 68 | | }; |
| | 49 | 69 | | } |
| | | 70 | | |
| | | 71 | | public static int GetComponentSize(int typeId) |
| | | 72 | | { |
| | 958 | 73 | | ValidateTypeId(typeId); |
| | | 74 | | |
| | 958 | 75 | | int size = ComponentSizes[typeId]; |
| | 958 | 76 | | if (size <= 0) |
| | | 77 | | { |
| | 0 | 78 | | throw new InvalidOperationException($"组件类型 ID {typeId} 尚未注册命令回放信息。"); |
| | | 79 | | } |
| | | 80 | | |
| | 958 | 81 | | return size; |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | public static void PlaybackAdd(Frame frame, EntityRef entity, int typeId, byte* data) |
| | | 85 | | { |
| | 955 | 86 | | ValidateTypeId(typeId); |
| | | 87 | | |
| | 955 | 88 | | ComponentCommandDataHandler? handler = AddHandlers[typeId]; |
| | 955 | 89 | | if (handler == null) |
| | | 90 | | { |
| | 0 | 91 | | throw new InvalidOperationException($"组件类型 ID {typeId} 未注册 Add 回放处理器。"); |
| | | 92 | | } |
| | | 93 | | |
| | 955 | 94 | | handler(frame, entity, data); |
| | 955 | 95 | | } |
| | | 96 | | |
| | | 97 | | public static void PlaybackSet(Frame frame, EntityRef entity, int typeId, byte* data) |
| | | 98 | | { |
| | 3 | 99 | | ValidateTypeId(typeId); |
| | | 100 | | |
| | 3 | 101 | | ComponentCommandDataHandler? handler = SetHandlers[typeId]; |
| | 3 | 102 | | if (handler == null) |
| | | 103 | | { |
| | 0 | 104 | | throw new InvalidOperationException($"组件类型 ID {typeId} 未注册 Set 回放处理器。"); |
| | | 105 | | } |
| | | 106 | | |
| | 3 | 107 | | handler(frame, entity, data); |
| | 3 | 108 | | } |
| | | 109 | | |
| | | 110 | | public static void PlaybackRemove(Frame frame, EntityRef entity, int typeId) |
| | | 111 | | { |
| | 3 | 112 | | ValidateTypeId(typeId); |
| | | 113 | | |
| | 3 | 114 | | ComponentCommandRemoveHandler? handler = RemoveHandlers[typeId]; |
| | 3 | 115 | | if (handler == null) |
| | | 116 | | { |
| | 0 | 117 | | throw new InvalidOperationException($"组件类型 ID {typeId} 未注册 Remove 回放处理器。"); |
| | | 118 | | } |
| | | 119 | | |
| | 3 | 120 | | handler(frame, entity); |
| | 3 | 121 | | } |
| | | 122 | | |
| | | 123 | | public static int GetStorageSnapshotSize(int typeId, void* storage) |
| | | 124 | | { |
| | 0 | 125 | | ValidateTypeId(typeId); |
| | | 126 | | |
| | 0 | 127 | | ComponentStorageSnapshotSizeHandler? handler = StorageSnapshotSizeHandlers[typeId]; |
| | 0 | 128 | | if (handler == null) |
| | | 129 | | { |
| | 0 | 130 | | throw new InvalidOperationException($"组件类型 ID {typeId} 未注册 Storage 快照大小处理器。"); |
| | | 131 | | } |
| | | 132 | | |
| | 0 | 133 | | return handler(storage); |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | public static void WriteStorageSnapshot(int typeId, void* storage, byte* buffer, int bufferSize) |
| | | 137 | | { |
| | 0 | 138 | | ValidateTypeId(typeId); |
| | | 139 | | |
| | 0 | 140 | | ComponentStorageSnapshotWriteHandler? handler = StorageSnapshotWriteHandlers[typeId]; |
| | 0 | 141 | | if (handler == null) |
| | | 142 | | { |
| | 0 | 143 | | throw new InvalidOperationException($"组件类型 ID {typeId} 未注册 Storage 快照写入处理器。"); |
| | | 144 | | } |
| | | 145 | | |
| | 0 | 146 | | handler(storage, buffer, bufferSize); |
| | 0 | 147 | | } |
| | | 148 | | |
| | | 149 | | public static void ReadStorageSnapshot(int typeId, void* storage, byte* buffer, int bufferSize) |
| | | 150 | | { |
| | 0 | 151 | | ValidateTypeId(typeId); |
| | | 152 | | |
| | 0 | 153 | | ComponentStorageSnapshotReadHandler? handler = StorageSnapshotReadHandlers[typeId]; |
| | 0 | 154 | | if (handler == null) |
| | | 155 | | { |
| | 0 | 156 | | throw new InvalidOperationException($"组件类型 ID {typeId} 未注册 Storage 快照读取处理器。"); |
| | | 157 | | } |
| | | 158 | | |
| | 0 | 159 | | handler(storage, buffer, bufferSize); |
| | 0 | 160 | | } |
| | | 161 | | |
| | | 162 | | public static void* CreateStorage(int typeId, Allocator* allocator, int maxEntities) |
| | | 163 | | { |
| | 0 | 164 | | ValidateTypeId(typeId); |
| | | 165 | | |
| | 0 | 166 | | ComponentStorageCreateHandler? handler = StorageCreateHandlers[typeId]; |
| | 0 | 167 | | if (handler == null) |
| | | 168 | | { |
| | 0 | 169 | | throw new InvalidOperationException($"组件类型 ID {typeId} 未注册 Storage 创建处理器。"); |
| | | 170 | | } |
| | | 171 | | |
| | 0 | 172 | | return handler(allocator, maxEntities, typeId); |
| | | 173 | | } |
| | | 174 | | |
| | | 175 | | private static void ValidateTypeId(int typeId) |
| | | 176 | | { |
| | 1919 | 177 | | if ((uint)typeId >= Frame.MaxComponentTypes) |
| | | 178 | | { |
| | 0 | 179 | | throw new ArgumentOutOfRangeException(nameof(typeId)); |
| | | 180 | | } |
| | 1919 | 181 | | } |
| | | 182 | | } |
| | | 183 | | } |