Skip to content

Commit f32027d

Browse files
committed
fix(world): validate component types on add/remove
Added validation for component types when adding or removing components in the World class. This includes checks for invalid types and restrictions on wildcard relation components to prevent direct addition. Enhanced the command execution flow to handle wildcard relation removals by identifying and removing all matching relation components.
1 parent 8c34621 commit f32027d

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

src/world.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,16 @@ export class World<ExtraParams extends any[] = [deltaTime: number]> {
164164
if (!this.hasEntity(entityId)) {
165165
throw new Error(`Entity ${entityId} does not exist`);
166166
}
167+
168+
// Validate component type
169+
const detailedType = getDetailedIdType(componentType);
170+
if (detailedType.type === "invalid") {
171+
throw new Error(`Invalid component type: ${componentType}`);
172+
}
173+
if (detailedType.type === "wildcard-relation") {
174+
throw new Error(`Cannot directly add wildcard relation components: ${componentType}`);
175+
}
176+
167177
this.commandBuffer.addComponent(entityId, componentType, component);
168178
}
169179

@@ -174,6 +184,13 @@ export class World<ExtraParams extends any[] = [deltaTime: number]> {
174184
if (!this.hasEntity(entityId)) {
175185
throw new Error(`Entity ${entityId} does not exist`);
176186
}
187+
188+
// Validate component type
189+
const detailedType = getDetailedIdType(componentType);
190+
if (detailedType.type === "invalid") {
191+
throw new Error(`Invalid component type: ${componentType}`);
192+
}
193+
177194
this.commandBuffer.removeComponent(entityId, componentType);
178195
}
179196

@@ -459,8 +476,23 @@ export class World<ExtraParams extends any[] = [deltaTime: number]> {
459476
break;
460477
case "removeComponent":
461478
if (cmd.componentType) {
462-
removes.add(cmd.componentType);
463-
adds.delete(cmd.componentType); // Remove from adds if it was going to be added
479+
const detailedType = getDetailedIdType(cmd.componentType);
480+
if (detailedType.type === "wildcard-relation") {
481+
// For wildcard relation removal, find all matching relation components
482+
const baseComponentId = detailedType.componentId!;
483+
for (const componentType of currentArchetype.componentTypes) {
484+
const componentDetailedType = getDetailedIdType(componentType);
485+
if (componentDetailedType.type === "entity-relation" || componentDetailedType.type === "component-relation") {
486+
if (componentDetailedType.componentId === baseComponentId) {
487+
removes.add(componentType);
488+
adds.delete(componentType); // Remove from adds if it was going to be added
489+
}
490+
}
491+
}
492+
} else {
493+
removes.add(cmd.componentType);
494+
adds.delete(cmd.componentType); // Remove from adds if it was going to be added
495+
}
464496
}
465497
break;
466498
}

0 commit comments

Comments
 (0)