我正在尝试寻找一种方法来检查Metatable是否为只读
例如
virtual
我希望可以为Roblox做到这一点,这样我就可以防止GUI篡改
答案 0 :(得分:1)
您可以使用getmetatable()
来查看元表的内容是否受保护。
示例:
local mt = getmetatable(game)
if mt and type(mt) ~= "table" do -- not perfect, as someone could set __metatable 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_table