< Summary

Information
Class: Lattice.ECS.Core.ComponentCommandRegistry
Assembly: Lattice
File(s): /home/runner/work/Sideline/Sideline/godot/scripts/lattice/ECS/Core/ComponentCommandRegistry.cs
Line coverage
64%
Covered lines: 57
Uncovered lines: 31
Coverable lines: 88
Total lines: 183
Line coverage: 64.7%
Branch coverage
61%
Covered branches: 22
Total branches: 36
Branch coverage: 61.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
Register(...)94.44%191887.5%
GetComponentSize(...)50%2280%
PlaybackAdd(...)50%2283.33%
PlaybackSet(...)50%2283.33%
PlaybackRemove(...)50%2283.33%
GetStorageSnapshotSize(...)0%620%
WriteStorageSnapshot(...)0%620%
ReadStorageSnapshot(...)0%620%
CreateStorage(...)0%620%
ValidateTypeId(...)50%2266.66%

File(s)

/home/runner/work/Sideline/Sideline/godot/scripts/lattice/ECS/Core/ComponentCommandRegistry.cs

#LineLine coverage
 1// Copyright (c) 2026 Sideline Authors. All rights reserved.
 2// Licensed under GPL-3.0.
 3
 4using System;
 5using Lattice.Core;
 6
 7namespace 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    {
 127        private static readonly int[] ComponentSizes = new int[Frame.MaxComponentTypes];
 128        private static readonly ComponentCommandDataHandler?[] AddHandlers = new ComponentCommandDataHandler[Frame.MaxCo
 129        private static readonly ComponentCommandDataHandler?[] SetHandlers = new ComponentCommandDataHandler[Frame.MaxCo
 130        private static readonly ComponentCommandRemoveHandler?[] RemoveHandlers = new ComponentCommandRemoveHandler[Fram
 131        private static readonly ComponentStorageSnapshotSizeHandler?[] StorageSnapshotSizeHandlers = new ComponentStorag
 132        private static readonly ComponentStorageSnapshotWriteHandler?[] StorageSnapshotWriteHandlers = new ComponentStor
 133        private static readonly ComponentStorageSnapshotReadHandler?[] StorageSnapshotReadHandlers = new ComponentStorag
 134        private static readonly ComponentStorageCreateHandler?[] StorageCreateHandlers = new ComponentStorageCreateHandl
 35
 36        public static void Register<T>(int typeId) where T : unmanaged, IComponent
 37        {
 4938            ComponentSizes[typeId] = sizeof(T);
 100439            AddHandlers[typeId] = static (frame, entity, data) => frame.Add(entity, *(T*)data);
 4940            SetHandlers[typeId] = static (frame, entity, data) =>
 4941            {
 342                T value = *(T*)data;
 343                if (frame.Has<T>(entity))
 4944                {
 345                    frame.Get<T>(entity) = value;
 346                    return;
 4947                }
 4948
 049                frame.Add(entity, value);
 4950            };
 4951            RemoveHandlers[typeId] = static (frame, entity) =>
 4952            {
 353                if (frame.Has<T>(entity))
 4954                {
 355                    frame.Remove<T>(entity);
 4956                }
 5257            };
 4958            StorageSnapshotSizeHandlers[typeId] = static storage => ((Storage<T>*)storage)->GetSnapshotSize();
 4959            StorageSnapshotWriteHandlers[typeId] = static (storage, buffer, bufferSize) =>
 4960                ((Storage<T>*)storage)->WriteSnapshot(buffer, bufferSize);
 4961            StorageSnapshotReadHandlers[typeId] = static (storage, buffer, bufferSize) =>
 4962                ((Storage<T>*)storage)->ReadSnapshot(buffer, bufferSize);
 4963            StorageCreateHandlers[typeId] = static (allocator, maxEntities, registeredTypeId) =>
 4964            {
 065                var storage = (Storage<T>*)allocator->Alloc(sizeof(Storage<T>));
 066                storage->Initialize(maxEntities, allocator, StorageFlags.None, componentTypeId: registeredTypeId);
 067                return storage;
 4968            };
 4969        }
 70
 71        public static int GetComponentSize(int typeId)
 72        {
 95873            ValidateTypeId(typeId);
 74
 95875            int size = ComponentSizes[typeId];
 95876            if (size <= 0)
 77            {
 078                throw new InvalidOperationException($"组件类型 ID {typeId} 尚未注册命令回放信息。");
 79            }
 80
 95881            return size;
 82        }
 83
 84        public static void PlaybackAdd(Frame frame, EntityRef entity, int typeId, byte* data)
 85        {
 95586            ValidateTypeId(typeId);
 87
 95588            ComponentCommandDataHandler? handler = AddHandlers[typeId];
 95589            if (handler == null)
 90            {
 091                throw new InvalidOperationException($"组件类型 ID {typeId} 未注册 Add 回放处理器。");
 92            }
 93
 95594            handler(frame, entity, data);
 95595        }
 96
 97        public static void PlaybackSet(Frame frame, EntityRef entity, int typeId, byte* data)
 98        {
 399            ValidateTypeId(typeId);
 100
 3101            ComponentCommandDataHandler? handler = SetHandlers[typeId];
 3102            if (handler == null)
 103            {
 0104                throw new InvalidOperationException($"组件类型 ID {typeId} 未注册 Set 回放处理器。");
 105            }
 106
 3107            handler(frame, entity, data);
 3108        }
 109
 110        public static void PlaybackRemove(Frame frame, EntityRef entity, int typeId)
 111        {
 3112            ValidateTypeId(typeId);
 113
 3114            ComponentCommandRemoveHandler? handler = RemoveHandlers[typeId];
 3115            if (handler == null)
 116            {
 0117                throw new InvalidOperationException($"组件类型 ID {typeId} 未注册 Remove 回放处理器。");
 118            }
 119
 3120            handler(frame, entity);
 3121        }
 122
 123        public static int GetStorageSnapshotSize(int typeId, void* storage)
 124        {
 0125            ValidateTypeId(typeId);
 126
 0127            ComponentStorageSnapshotSizeHandler? handler = StorageSnapshotSizeHandlers[typeId];
 0128            if (handler == null)
 129            {
 0130                throw new InvalidOperationException($"组件类型 ID {typeId} 未注册 Storage 快照大小处理器。");
 131            }
 132
 0133            return handler(storage);
 134        }
 135
 136        public static void WriteStorageSnapshot(int typeId, void* storage, byte* buffer, int bufferSize)
 137        {
 0138            ValidateTypeId(typeId);
 139
 0140            ComponentStorageSnapshotWriteHandler? handler = StorageSnapshotWriteHandlers[typeId];
 0141            if (handler == null)
 142            {
 0143                throw new InvalidOperationException($"组件类型 ID {typeId} 未注册 Storage 快照写入处理器。");
 144            }
 145
 0146            handler(storage, buffer, bufferSize);
 0147        }
 148
 149        public static void ReadStorageSnapshot(int typeId, void* storage, byte* buffer, int bufferSize)
 150        {
 0151            ValidateTypeId(typeId);
 152
 0153            ComponentStorageSnapshotReadHandler? handler = StorageSnapshotReadHandlers[typeId];
 0154            if (handler == null)
 155            {
 0156                throw new InvalidOperationException($"组件类型 ID {typeId} 未注册 Storage 快照读取处理器。");
 157            }
 158
 0159            handler(storage, buffer, bufferSize);
 0160        }
 161
 162        public static void* CreateStorage(int typeId, Allocator* allocator, int maxEntities)
 163        {
 0164            ValidateTypeId(typeId);
 165
 0166            ComponentStorageCreateHandler? handler = StorageCreateHandlers[typeId];
 0167            if (handler == null)
 168            {
 0169                throw new InvalidOperationException($"组件类型 ID {typeId} 未注册 Storage 创建处理器。");
 170            }
 171
 0172            return handler(allocator, maxEntities, typeId);
 173        }
 174
 175        private static void ValidateTypeId(int typeId)
 176        {
 1919177            if ((uint)typeId >= Frame.MaxComponentTypes)
 178            {
 0179                throw new ArgumentOutOfRangeException(nameof(typeId));
 180            }
 1919181        }
 182    }
 183}