A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
feat(core): domain-scoped TypeId — decouple ECS/event/service/resource ids (RC4)
esengine56c7e2545402
diff --git a/docs/REARCHITECTURE.md b/docs/REARCHITECTURE.mdindex 3fda43847..4b17431aa 100644--- a/docs/REARCHITECTURE.md+++ b/docs/REARCHITECTURE.md@@ -122,5 +122,15 @@- **边界测试可在仓库内运行**:`sdk/tests/helpers/loadWasm.ts` 成为 WASM 路径单一来源(`$ESENGINE_WASM_DIR` → `build/wasm/web` → 旧 `desktop/public/wasm`),8 个集成测试文件不再各自硬编码不存在的 `desktop/public/wasm`。- 验证:SDK typecheck 通过;2012 个测试通过(含 3 个新握手测试,证明 mismatch 在非 strict 下也致命);EHT 幂等。**待补**:C++ 侧 `static_assert` + `getAbiLayoutHash` 的实际编译验证需要 emsdk(随 CI web 构建门禁一起闭环)。-### RC2 / RC3 / RC4 / RC5 — 未开始-按执行顺序依次推进,全程在 RC1 的 `static_assert` + 握手护栏内进行。+### RC4 身份 — ✅ 已落地并**编译+运行验证**+- `core/Types.hpp`:单一全局 `getTypeId<T>()` 计数器拆为**按域独立**的计数器。新增域标签 `ComponentDomain/EventDomain/ServiceDomain/ResourceDomain` 与具名 helper `componentTypeId/eventTypeId/serviceTypeId/resourceTypeId`(各自 0 起、密集)。移除共享的 `getTypeId`/`nextTypeId`。+- 调用点迁移:`Registry`→`componentTypeId`、`Dispatcher`→`eventTypeId`、`ServiceRegistry`→`serviceTypeId`、`LoaderRegistry`→`resourceTypeId`。组件 id 现在密集,`pools_` 向量与 128-bit 组件 mask 不再被无关的事件/服务/资源类型撑大或溢出。+- 全仓已无 `getTypeId` 残留(仅本文档"病灶"描述保留旧名)。各容器(pools_/signals_/services_/loaders_)本就相互独立,跨域 id 无需全局唯一,故拆分零语义风险。+- **验证**:用本机 MSVC(VS Build Tools)+ glm 编译一个独立 harness 包含全部四个被改头文件并运行通过。关键证据:先注册 6 个事件/服务/资源 id 后,组件 id 仍为 `0 1 2`(旧共享计数器下会是 `6 7 8`);Registry emplace/has/tryGet/remove、Dispatcher trigger、ServiceRegistry 均跑通。+- **顺带修复**:`SparseSet.hpp` 用了 `std::array<u32,4096>` 却没 `#include <array>`(依赖 Emscripten/libc++ 的传递包含,MSVC 下编译失败)——补上,消除一处潜在可移植性 bug。++### 验证能力(已确认可用)+本机**存在原生 C++ 工具链**:VS Build Tools(`D:\VisualStudioBuildTools`,cl.exe 14.50)+ ninja + cmake,doctest 已 vendored,glm 子模块已初始化。被改的 ECS/事件/服务/资源代码均为 header-only,可用独立 harness 直接编译+运行验证(如 RC4 所做)。完整 `esengine` 静态库的原生构建还需更多子模块/native 依赖(glfw/glad/box2d/spine 等),属后续 CI 门禁范畴。**结论**:RC2/RC3/RC5 的 header-only 部分可在本机即时编译验证,不再是纯人工审阅。++### RC2 / RC3 / RC5 — 未开始+按执行顺序依次推进,全程在 RC1 的 `static_assert` + 握手护栏内进行,并尽量用独立 harness 即时编译验证。diff --git a/src/esengine/core/ServiceRegistry.hpp b/src/esengine/core/ServiceRegistry.hppindex 67839ceea..ed694d65a 100644--- a/src/esengine/core/ServiceRegistry.hpp+++ b/src/esengine/core/ServiceRegistry.hpp@@ -59,7 +59,7 @@ public:*/template<typename T>void registerService(T* service) {- TypeId id = getTypeId<T>();+ TypeId id = serviceTypeId<T>();ES_ASSERT(services_.find(id) == services_.end(),"Service already registered, use removeService first");services_[id] = static_cast<void*>(service);@@ -72,7 +72,7 @@ public:*/template<typename T>void registerOwned(Unique<T> service) {- TypeId id = getTypeId<T>();+ TypeId id = serviceTypeId<T>();ES_ASSERT(services_.find(id) == services_.end(),"Service already registered, use removeService first");T* raw = service.get();@@ -91,7 +91,7 @@ public:*/template<typename T>T* getService() {- auto it = services_.find(getTypeId<T>());+ auto it = services_.find(serviceTypeId<T>());if (it == services_.end()) return nullptr;return static_cast<T*>(it->second);}@@ -114,7 +114,7 @@ public:*/template<typename T>void removeService() {- TypeId id = getTypeId<T>();+ TypeId id = serviceTypeId<T>();auto it = services_.find(id);if (it == services_.end()) return;diff --git a/src/esengine/core/Types.hpp b/src/esengine/core/Types.hppindex 8f0a4f2b3..d856ea401 100644--- a/src/esengine/core/Types.hpp+++ b/src/esengine/core/Types.hpp@@ -179,35 +179,61 @@ using TypeId = u32;namespace detail {/**- * @brief Generates sequential type IDs- * @return The next available TypeId- * @note Internal use only+ * @brief One independent TypeId counter per Domain tag.+ * @details A single global counter shared across unrelated concerns+ * (components, events, services, resources) coupled them: a+ * component's id depended on how many events/services happened to+ * be instantiated first. Because the ECS uses the component id as+ * a `pools_` vector index AND a per-entity bitmask bit, an inflated+ * id wasted memory and could overflow the 128-bit inline mask.+ * Per-domain counters keep each domain's ids dense and 0-based.+ * @note Internal use only.*/- inline TypeId nextTypeId() {+ template<typename Domain>+ inline TypeId& typeIdCounter() {static TypeId counter = 0;- return counter++;+ return counter;+ }++ template<typename Domain, typename T>+ inline TypeId domainTypeId() {+ static const TypeId id = typeIdCounter<Domain>()++;+ return id;}} // namespace detail/**- * @brief Gets the unique runtime TypeId for a type- * @tparam T The type to get an ID for- * @return The TypeId associated with type T- *- * @details Each unique type T receives a unique ID on first call.- * Subsequent calls return the same ID.- *+ * @brief Domain tags — each yields an independent, dense, 0-based TypeId+ * sequence. Use the typed helpers below rather than the raw domain id.+ */+struct ComponentDomain {};+struct EventDomain {};+struct ServiceDomain {};+struct ResourceDomain {};++/**+ * @brief Dense 0-based runtime id for a component type (ECS pool index + mask bit).+ * @tparam T The component type* @code- * TypeId posId = getTypeId<Position>();- * TypeId velId = getTypeId<Velocity>();+ * TypeId posId = componentTypeId<Position>();+ * TypeId velId = componentTypeId<Velocity>();* assert(posId != velId);* @endcode*/template<typename T>-inline TypeId getTypeId() {- static const TypeId id = detail::nextTypeId();- return id;-}+inline TypeId componentTypeId() { return detail::domainTypeId<ComponentDomain, T>(); }++/** @brief Dense 0-based runtime id for an event type (Dispatcher signal key). */+template<typename T>+inline TypeId eventTypeId() { return detail::domainTypeId<EventDomain, T>(); }++/** @brief Dense 0-based runtime id for a service type (ServiceRegistry key). */+template<typename T>+inline TypeId serviceTypeId() { return detail::domainTypeId<ServiceDomain, T>(); }++/** @brief Dense 0-based runtime id for a resource type (LoaderRegistry key). */+template<typename T>+inline TypeId resourceTypeId() { return detail::domainTypeId<ResourceDomain, T>(); }// =============================================================================// Result Typediff --git a/src/esengine/ecs/Registry.hpp b/src/esengine/ecs/Registry.hppindex 5012e35e0..0ecec9307 100644--- a/src/esengine/ecs/Registry.hpp+++ b/src/esengine/ecs/Registry.hpp@@ -246,7 +246,7 @@ public:ES_ASSERT(valid(entity), "Invalid entity");auto& pool = assurePool<T>();auto& result = pool.emplace(entity, std::forward<Args>(args)...);- component_masks_[entity.index()].set(getTypeId<T>());+ component_masks_[entity.index()].set(componentTypeId<T>());return result;}@@ -271,7 +271,7 @@ public:return comp;}auto& result = pool.emplace(entity, std::forward<Args>(args)...);- component_masks_[entity.index()].set(getTypeId<T>());+ component_masks_[entity.index()].set(componentTypeId<T>());return result;}@@ -289,7 +289,7 @@ public:pool->remove(entity);const u32 idx = entity.index();if (idx < component_masks_.size()) {- component_masks_[idx].clear(getTypeId<T>());+ component_masks_[idx].clear(componentTypeId<T>());}}}@@ -351,7 +351,7 @@ public:return pool.get(entity);}auto& result = pool.emplace(entity, std::forward<Args>(args)...);- component_masks_[entity.index()].set(getTypeId<T>());+ component_masks_[entity.index()].set(componentTypeId<T>());return result;}@@ -578,7 +578,7 @@ private:*/template<typename T>SparseSet<T>& assurePool() {- TypeId typeId = getTypeId<T>();+ TypeId typeId = componentTypeId<T>();if (typeId >= pools_.size()) {pools_.resize(typeId + 1);}@@ -595,14 +595,14 @@ private:*/template<typename T>SparseSet<T>* getPool() {- TypeId typeId = getTypeId<T>();+ TypeId typeId = componentTypeId<T>();if (typeId >= pools_.size() || !pools_[typeId]) return nullptr;return static_cast<SparseSet<T>*>(pools_[typeId].get());}template<typename T>const SparseSet<T>* getPool() const {- TypeId typeId = getTypeId<T>();+ TypeId typeId = componentTypeId<T>();if (typeId >= pools_.size() || !pools_[typeId]) return nullptr;return static_cast<const SparseSet<T>*>(pools_[typeId].get());}diff --git a/src/esengine/ecs/SparseSet.hpp b/src/esengine/ecs/SparseSet.hppindex 37627c2b0..007ec4a3b 100644--- a/src/esengine/ecs/SparseSet.hpp+++ b/src/esengine/ecs/SparseSet.hpp@@ -23,6 +23,7 @@// Standard library#include <algorithm>+#include <array>#include <memory>#include <vector>diff --git a/src/esengine/events/Dispatcher.hpp b/src/esengine/events/Dispatcher.hppindex 010422dbe..c634d5d77 100644--- a/src/esengine/events/Dispatcher.hpp+++ b/src/esengine/events/Dispatcher.hpp@@ -226,7 +226,7 @@ private:template<typename Event>Signal<void(const Event&)>& assure() {- auto typeId = getTypeId<Event>();+ auto typeId = eventTypeId<Event>();auto it = signals_.find(typeId);if (it == signals_.end()) {@@ -241,7 +241,7 @@ private:template<typename Event>Signal<void(const Event&)>* find() {- auto typeId = getTypeId<Event>();+ auto typeId = eventTypeId<Event>();auto it = signals_.find(typeId);if (it != signals_.end()) {return &static_cast<SignalWrapperTyped<Event>*>(it->second.get())->signal;@@ -251,7 +251,7 @@ private:template<typename Event>const Signal<void(const Event&)>* find() const {- auto typeId = getTypeId<Event>();+ auto typeId = eventTypeId<Event>();auto it = signals_.find(typeId);if (it != signals_.end()) {return &static_cast<const SignalWrapperTyped<Event>*>(it->second.get())->signal;diff --git a/src/esengine/resource/LoaderRegistry.hpp b/src/esengine/resource/LoaderRegistry.hppindex 1af185269..d5e11be55 100644--- a/src/esengine/resource/LoaderRegistry.hpp+++ b/src/esengine/resource/LoaderRegistry.hpp@@ -61,7 +61,7 @@ public:template<typename T>void registerLoader(Unique<ResourceLoader<T>> loader) {auto wrapper = makeUnique<LoaderWrapper<T>>(std::move(loader));- loaders_[getTypeId<T>()] = std::move(wrapper);+ loaders_[resourceTypeId<T>()] = std::move(wrapper);}/**@@ -71,7 +71,7 @@ public:*/template<typename T>ResourceLoader<T>* getLoader() {- auto it = loaders_.find(getTypeId<T>());+ auto it = loaders_.find(resourceTypeId<T>());if (it == loaders_.end()) {return nullptr;}@@ -86,7 +86,7 @@ public:*/template<typename T>const ResourceLoader<T>* getLoader() const {- auto it = loaders_.find(getTypeId<T>());+ auto it = loaders_.find(resourceTypeId<T>());if (it == loaders_.end()) {return nullptr;}@@ -101,7 +101,7 @@ public:*/template<typename T>bool hasLoader() const {- return loaders_.find(getTypeId<T>()) != loaders_.end();+ return loaders_.find(resourceTypeId<T>()) != loaders_.end();}/**@@ -110,7 +110,7 @@ public:*/template<typename T>void removeLoader() {- loaders_.erase(getTypeId<T>());+ loaders_.erase(resourceTypeId<T>());}/**