Skip to content

Commit bea5058

Browse files
committed
don't clone hidden fragments (while they are not explicit)
1 parent f816d42 commit bea5058

2 files changed

Lines changed: 137 additions & 26 deletions

File tree

develop/untests.lua

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local evo = require 'evolved'
55

66
evo.debug_mode(true)
77

8-
do
8+
if os.getenv("LOCAL_LUA_DEBUGGER_VSCODE") == nil then
99
local i = evo.id()
1010

1111
for _ = 1, 0xFFFFE do
@@ -6156,3 +6156,82 @@ do
61566156
assert(evo.has(e3g, f3) and evo.get(e3g, f3) == 33)
61576157
end
61586158
end
6159+
6160+
do
6161+
local f1, f2, f3 = evo.id(3)
6162+
6163+
evo.set(f2, evo.HIDDEN)
6164+
evo.set(f3, evo.HIDDEN)
6165+
6166+
do
6167+
local p = evo.spawn { [f1] = 11, [f2] = 22 }
6168+
local e = evo.clone(p)
6169+
6170+
assert(evo.has(p, f1) and evo.get(p, f1) == 11)
6171+
assert(evo.has(p, f2) and evo.get(p, f2) == 22)
6172+
6173+
assert(evo.has(e, f1) and evo.get(e, f1) == 11)
6174+
assert(not evo.has(e, f2) and evo.get(e, f2) == nil)
6175+
end
6176+
6177+
do
6178+
local p = evo.spawn { [f1] = 11, [f2] = 22, [f3] = 33 }
6179+
local e = evo.clone(p)
6180+
6181+
assert(evo.has(p, f1) and evo.get(p, f1) == 11)
6182+
assert(evo.has(p, f2) and evo.get(p, f2) == 22)
6183+
assert(evo.has(p, f3) and evo.get(p, f3) == 33)
6184+
6185+
assert(evo.has(e, f1) and evo.get(e, f1) == 11)
6186+
assert(not evo.has(e, f2) and evo.get(e, f2) == nil)
6187+
assert(not evo.has(e, f3) and evo.get(e, f3) == nil)
6188+
end
6189+
6190+
do
6191+
local p = evo.spawn { [f2] = 22 }
6192+
local e = evo.clone(p)
6193+
6194+
assert(not evo.has(p, f1) and evo.get(p, f1) == nil)
6195+
assert(evo.has(p, f2) and evo.get(p, f2) == 22)
6196+
assert(not evo.has(p, f3) and evo.get(p, f3) == nil)
6197+
6198+
assert(not evo.has(e, f1) and evo.get(e, f1) == nil)
6199+
assert(not evo.has(e, f2) and evo.get(e, f2) == nil)
6200+
assert(not evo.has(e, f3) and evo.get(e, f3) == nil)
6201+
end
6202+
do
6203+
local p = evo.spawn { [f2] = 22, [f3] = 33 }
6204+
local e = evo.clone(p)
6205+
6206+
assert(not evo.has(p, f1) and evo.get(p, f1) == nil)
6207+
assert(evo.has(p, f2) and evo.get(p, f2) == 22)
6208+
assert(evo.has(p, f3) and evo.get(p, f3) == 33)
6209+
6210+
assert(not evo.has(e, f1) and evo.get(e, f1) == nil)
6211+
assert(not evo.has(e, f2) and evo.get(e, f2) == nil)
6212+
assert(not evo.has(e, f3) and evo.get(e, f3) == nil)
6213+
end
6214+
6215+
do
6216+
local p = evo.spawn { [f1] = 11, [f2] = 22 }
6217+
local e = evo.clone(p, { [f2] = 2 })
6218+
6219+
assert(evo.has(p, f1) and evo.get(p, f1) == 11)
6220+
assert(evo.has(p, f2) and evo.get(p, f2) == 22)
6221+
6222+
assert(evo.has(e, f1) and evo.get(e, f1) == 11)
6223+
assert(evo.has(e, f2) and evo.get(e, f2) == 2)
6224+
end
6225+
6226+
do
6227+
local p = evo.spawn { [f1] = 11, [f2] = 22 }
6228+
local e = evo.clone(p, { [f2] = 2, [f3] = 3 })
6229+
6230+
assert(evo.has(p, f1) and evo.get(p, f1) == 11)
6231+
assert(evo.has(p, f2) and evo.get(p, f2) == 22)
6232+
6233+
assert(evo.has(e, f1) and evo.get(e, f1) == 11)
6234+
assert(evo.has(e, f2) and evo.get(e, f2) == 2)
6235+
assert(evo.has(e, f3) and evo.get(e, f3) == 3)
6236+
end
6237+
end

evolved.lua

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,32 @@ local function __chunk_without_fragments(chunk, ...)
12251225
return chunk
12261226
end
12271227

1228+
---@param chunk? evolved.chunk
1229+
---@return evolved.chunk?
1230+
---@nodiscard
1231+
local function __chunk_without_hidden_fragments(chunk)
1232+
if not chunk then
1233+
return nil
1234+
end
1235+
1236+
if not chunk.__has_hidden_fragments then
1237+
return chunk
1238+
end
1239+
1240+
local chunk_fragment_list = chunk.__fragment_list
1241+
local chunk_fragment_count = chunk.__fragment_count
1242+
1243+
for i = chunk_fragment_count, 1, -1 do
1244+
local fragment = chunk_fragment_list[i]
1245+
1246+
if __evolved_has(fragment, __HIDDEN) then
1247+
chunk = __chunk_without_fragment(chunk, fragment)
1248+
end
1249+
end
1250+
1251+
return chunk
1252+
end
1253+
12281254
---
12291255
---
12301256
---
@@ -1660,7 +1686,9 @@ local function __clone_entity(entity, prefab, components)
16601686
local prefab_chunk = __entity_chunks[prefab_index]
16611687
local prefab_place = __entity_places[prefab_index]
16621688

1663-
local chunk = __chunk_with_components(prefab_chunk, components)
1689+
local chunk = __chunk_with_components(
1690+
__chunk_without_hidden_fragments(prefab_chunk),
1691+
components)
16641692

16651693
if not chunk then
16661694
return
@@ -1694,46 +1722,50 @@ local function __clone_entity(entity, prefab, components)
16941722
if prefab_chunk.__has_setup_hooks then
16951723
for prefab_component_index = 1, prefab_component_count do
16961724
local fragment = prefab_component_fragments[prefab_component_index]
1725+
local component_index = chunk_component_indices[fragment]
16971726

1698-
---@type evolved.duplicate?
1699-
local fragment_duplicate =
1700-
__evolved_get(fragment, __DUPLICATE)
1727+
if component_index then
1728+
---@type evolved.duplicate?
1729+
local fragment_duplicate =
1730+
__evolved_get(fragment, __DUPLICATE)
17011731

1702-
local prefab_component_storage = prefab_component_storages[prefab_component_index]
1703-
local prefab_component = prefab_component_storage[prefab_place]
1732+
local prefab_component_storage = prefab_component_storages[prefab_component_index]
1733+
local prefab_component = prefab_component_storage[prefab_place]
17041734

1705-
local new_component = prefab_component
1735+
local new_component = prefab_component
17061736

1707-
if new_component ~= nil and fragment_duplicate then
1708-
new_component = fragment_duplicate(new_component)
1709-
end
1737+
if new_component ~= nil and fragment_duplicate then
1738+
new_component = fragment_duplicate(new_component)
1739+
end
17101740

1711-
if new_component == nil then
1712-
new_component = true
1713-
end
1741+
if new_component == nil then
1742+
new_component = true
1743+
end
17141744

1715-
local component_index = chunk_component_indices[fragment]
1716-
local component_storage = chunk_component_storages[component_index]
1745+
local component_storage = chunk_component_storages[component_index]
17171746

1718-
component_storage[place] = new_component
1747+
component_storage[place] = new_component
1748+
end
17191749
end
17201750
else
17211751
for prefab_component_index = 1, prefab_component_count do
17221752
local fragment = prefab_component_fragments[prefab_component_index]
1753+
local component_index = chunk_component_indices[fragment]
17231754

1724-
local prefab_component_storage = prefab_component_storages[prefab_component_index]
1725-
local prefab_component = prefab_component_storage[prefab_place]
1755+
if component_index then
1756+
local prefab_component_storage = prefab_component_storages[prefab_component_index]
1757+
local prefab_component = prefab_component_storage[prefab_place]
17261758

1727-
local new_component = prefab_component
1759+
local new_component = prefab_component
17281760

1729-
if new_component == nil then
1730-
new_component = true
1731-
end
1761+
if new_component == nil then
1762+
new_component = true
1763+
end
17321764

1733-
local component_index = chunk_component_indices[fragment]
1734-
local component_storage = chunk_component_storages[component_index]
1765+
local component_storage = chunk_component_storages[component_index]
17351766

1736-
component_storage[place] = new_component
1767+
component_storage[place] = new_component
1768+
end
17371769
end
17381770
end
17391771
end

0 commit comments

Comments
 (0)