Lua: Fix wesnoth.deprecate_api for the case of deprecating a table that had a metamethod other than __index/__newindex
(cherry picked from commit 21823d9332
)
This commit is contained in:
parent
256838796b
commit
3bc8972f1d
1 changed files with 31 additions and 21 deletions
|
@ -204,29 +204,39 @@ function wesnoth.deprecate_api(elem_name, replacement, level, version, elem, det
|
|||
end
|
||||
elseif type(elem) == "table" then
|
||||
-- Don't clobber the old metatable.
|
||||
local old_mt = getmetatable(elem)
|
||||
local mt = {
|
||||
__index = function(self, key)
|
||||
if not msg_shown then
|
||||
msg_shown = true
|
||||
wesnoth.deprecated_message(elem_name, level, version, message)
|
||||
end
|
||||
if type(old_mt) == "table" and old_mt.__index ~= nil then
|
||||
local old_mt = getmetatable(elem) or {}
|
||||
local mt = {}
|
||||
for k,v in pairs(old_mt) do
|
||||
mt[k] = old_mt[v]
|
||||
end
|
||||
mt.__index = function(self, key)
|
||||
if not msg_shown then
|
||||
msg_shown = true
|
||||
wesnoth.deprecated_message(elem_name, level, version, message)
|
||||
end
|
||||
if type(old_mt) == "table" then
|
||||
if type(old_mt.__index) == 'function' then
|
||||
return old_mt.__index(self, key)
|
||||
elseif type(old_mt.__index) == 'table' then
|
||||
return old_mt.__index[key]
|
||||
else
|
||||
-- As of 2019, __index must be either a function or a table. If you ever run into this error,
|
||||
-- add an elseif branch for wrapping old_mt.__index appropriately.
|
||||
wml.error('The wrapped __index metamethod of a deprecated object is neither a function nor a table')
|
||||
end
|
||||
return elem[key]
|
||||
end,
|
||||
__newindex = function(self, key, val)
|
||||
if not msg_shown then
|
||||
msg_shown = true
|
||||
wesnoth.deprecated_message(elem_name, level, version, message)
|
||||
end
|
||||
if type(old_mt) == "table" and old_mt.__newindex ~= nil then
|
||||
return old_mt.__newindex(self, key, val)
|
||||
end
|
||||
elem[key] = val
|
||||
end,
|
||||
}
|
||||
end
|
||||
return elem[key]
|
||||
end
|
||||
mt.__newindex = function(self, key, val)
|
||||
if not msg_shown then
|
||||
msg_shown = true
|
||||
wesnoth.deprecated_message(elem_name, level, version, message)
|
||||
end
|
||||
if type(old_mt) == "table" and old_mt.__newindex ~= nil then
|
||||
return old_mt.__newindex(self, key, val)
|
||||
end
|
||||
elem[key] = val
|
||||
end
|
||||
return setmetatable({}, mt)
|
||||
else
|
||||
wesnoth.log('warn', "Attempted to deprecate something that is not a table or function: " ..
|
||||
|
|
Loading…
Add table
Reference in a new issue