From a2cf6cb74f0eaa1e23b0defa44adf76ae2f11f9e Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 20 Oct 2024 16:04:07 -0400 Subject: [PATCH] Add back most world operation validators --- benchmarks/query.bench.luau | 3 --- benchmarks/stress.bench.luau | 20 ++++---------------- benchmarks/without.bench.luau | 4 ---- example/src/shared/start.luau | 8 -------- lib/World.luau | 23 +++++++++++++++-------- lib/component.luau | 2 +- 6 files changed, 20 insertions(+), 40 deletions(-) diff --git a/benchmarks/query.bench.luau b/benchmarks/query.bench.luau index 43733d3..d1ae6d8 100644 --- a/benchmarks/query.bench.luau +++ b/benchmarks/query.bench.luau @@ -16,7 +16,6 @@ for i = 1, 10_000 do pinnedWorld:spawnAt(i, pinnedA({}), pinnedB({})) end -print(world.archetypes, world.archetypes:count()) return { ParameterGenerator = function() return @@ -28,8 +27,6 @@ return { for _ in world:query(A, B) do count += 1 end - - print("new", count) end, ["Old Matter"] = function() local count = 0 diff --git a/benchmarks/stress.bench.luau b/benchmarks/stress.bench.luau index 583ccab..1188bca 100644 --- a/benchmarks/stress.bench.luau +++ b/benchmarks/stress.bench.luau @@ -83,13 +83,6 @@ end print("archetypes:", archetypes) print(total, "different archetypes") -print("tree:", world.archetypes:count()) -local total = 0 -for _, count in world.archetypes:count() do - total += count -end -print(total, "archetypes") - return { ParameterGenerator = function() return @@ -97,21 +90,16 @@ return { Functions = { ["Old Matter"] = function() - --local count = 0 + local count = 0 for _ in pinnedWorld:query(pinnedB, pinnedA) do - --count += 1 + count += 1 end - - --print("old:", count) end, ["New Matter"] = function() - --local count = 0 + local count = 0 for _ in world:query(B, A) do - --count += 1 - --print("entt", a, b) + count += 1 end - - --print("new:", count) end, }, } diff --git a/benchmarks/without.bench.luau b/benchmarks/without.bench.luau index c2f750a..c755e5f 100644 --- a/benchmarks/without.bench.luau +++ b/benchmarks/without.bench.luau @@ -75,16 +75,12 @@ return { for _ in pinnedWorld:query(pinnedB):without(pinnedC) do count += 1 end - - --print("old:", count) end, ["New Matter"] = function() local count = 0 for _ in world:query(B):without(C) do count += 1 end - - print("new:", count) end, }, } diff --git a/example/src/shared/start.luau b/example/src/shared/start.luau index 0cb2486..07df655 100644 --- a/example/src/shared/start.luau +++ b/example/src/shared/start.luau @@ -93,14 +93,6 @@ local function start(containers) end) end - if RunService:IsServer() then - task.spawn(function() - while task.wait(1) do - print(world.archetypes) - end - end) - end - return world, state end diff --git a/lib/World.luau b/lib/World.luau index def64a4..3af4112 100644 --- a/lib/World.luau +++ b/lib/World.luau @@ -8,6 +8,7 @@ local topoRuntime = require(script.Parent.topoRuntime) local assertValidComponentInstances = Component.assertValidComponentInstances local assertValidComponent = Component.assertValidComponent +local assertComponentArgsProvided = Component.assertComponentArgsProvided type EntityId = Archetype.EntityId type ComponentId = Archetype.ComponentId @@ -55,6 +56,15 @@ type ReplaceCommand = { type Command = DespawnCommand | InsertCommand | RemoveCommand | ReplaceCommand +local function assertEntityExists(world, id: number) + assert(world:contains(id), "Entity doesn't exist, use world:contains to check if needed") +end + +local function assertWorldOperationIsValid(world, id: number, ...) + assertEntityExists(world, id) + assertComponentArgsProvided(...) +end + --[=[ @class World @@ -500,6 +510,7 @@ end function World:replace(id, ...) local componentInstances = { ... } assertValidComponentInstances(componentInstances) + bufferCommand(self, { type = "replace", entityId = id, componentInstances = componentInstances }) end @@ -551,15 +562,13 @@ end @param ... Component -- The components to fetch @return ... -- Returns the component values in the same order they were passed in ]=] -function World.get(self: World, entityId, ...: Component) - local entityRecord = self.allEntities[entityId] - if entityRecord == nil then - error(ERROR_NO_ENTITY, 2) - end +function World:get(entityId, ...: Component) + assertWorldOperationIsValid(self, entityId, ...) local length = select("#", ...) local componentInstances = table.create(length, nil) + local entityRecord = self.allEntities[entityId] local archetype = entityRecord.archetype local idToIndex = archetype.idToIndex for i = 1, length do @@ -1244,9 +1253,7 @@ end @param ... ComponentInstance -- The component values to insert ]=] function World:insert(id, ...) - if not self:contains(id) then - error(ERROR_NO_ENTITY, 2) - end + assertWorldOperationIsValid(self, id, ...) local componentInstances = { ... } assertValidComponentInstances(componentInstances) diff --git a/lib/component.luau b/lib/component.luau index aa562fa..c257ab1 100644 --- a/lib/component.luau +++ b/lib/component.luau @@ -181,6 +181,6 @@ return { newComponent = newComponent, assertValidComponentInstance = assertValidComponentInstance, assertValidComponentInstances = assertValidComponentInstances, - assertValidComponentArgsProvided = assertValidComponentArgsProvided, + assertComponentArgsProvided = assertComponentArgsProvided, assertValidComponent = assertValidComponent, }