我正在尝试找到一种方法来检查Metatable是否为只读
例如
local mt = metatable(game)
if mt == "readonly" do
print("Attempt to modify Metatables")
end我希望有一种方法可以实现这一点,这样我就可以防止GUI篡改
发布于 2018-12-22 03:03:38
您可以使用getmetatable()查看元表的内容是否受到保护。
示例:
local mt = getmetatable(game)
if mt ~= nil and type(mt) ~= "table" then -- not perfect, as __metatable could be set to {}
print("This metatable is protected!")
end或者,如果要查看表本身是否为只读,则需要检查两种行为
只读表示例:
local protected_table = {'1', '2', '3'}
local table = setmetatable({}, {-- create a dumby table with a metatable
__index = function(_, k)
return protected_table[k]-- access protected table
end,
__newindex = function()
error('This value is read-only.')
end,
__pairs = function(_)
return function(_, k)
return next(protected_table, k)
end
end,
__metatable = false,
})可能的交互示例:
table[4] = "4" -- read-only error will be generated by _newindex setting
table[1] = "0" -- read-only error will be generated by _newindex setting
first = table[1] -- will retrieve first value of protected_tablehttps://stackoverflow.com/questions/53679564
复制相似问题