Skip to content

Commit 8cdda64

Browse files
committed
Merge branch 'technic-plus-v1-api-compat' into dev
2 parents d7dd846 + 448e5f5 commit 8cdda64

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

technic/machines/compat/api.lua

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,28 @@ local S = technic.getter
33
-- Registration compatibility shim to transform Technic 1.x arguments to 2.x
44
-- This could be made stricter for new style API by utilizing `assert`
55

6+
local function shallow_copy(t)
7+
local result = {}
8+
for k, v in pairs(t) do
9+
result[k] = v
10+
end
11+
return setmetatable(result, getmetatable(t))
12+
end
13+
614
function technic.register_compat_v1_to_v2(name, data, default_name)
715
local modname = minetest.get_current_modname()
816
local colon, def
917
if type(name) == "table" then
1018
-- Log old API usage, swap name to def and set name from def table
1119
local msg = "Deprecated Technic registration API call: %s (%s)"
12-
def = table.copy(name)
20+
def = shallow_copy(name)
1321
name = def.machine_name or default_name
1422
def.machine_name = nil
1523
def.description = def.machine_desc
1624
def.machine_desc = nil
1725
minetest.log("warning", msg:format(tostring(name), tostring(modname)))
1826
else
19-
def = table.copy(data)
27+
def = shallow_copy(data)
2028
end
2129
-- Input name can be "modname:nodename", ":modname:nodename" or "nodename".
2230
-- If name is presented as "nodename" then check for old def.modname field.

technic/spec/api_spec.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,12 @@ describe("Technic API", function()
6868
end,
6969
}
7070
-- Make sure that original definition will not be changed during registration
71-
setmetatable(data, { __newindex = function() error("Attempt to modify original definition") end })
71+
setmetatable(data, {
72+
__newindex = function(self, key, value)
73+
assert(self ~= data, "Attempt to modify original definition")
74+
rawset(self, key, value)
75+
end
76+
})
7277
technic.register_solar_array("my_mod:my_solar_array", data)
7378

7479
-- Verify node registration

0 commit comments

Comments
 (0)