Lua - 如何打破元表或使其无法使用?

在我的游戏中,大多数利用者都使用元表来阻止封禁,我希望打破它们,我不使用它们,这只会伤害利用者。即使这会破坏 Lua 的其他部分,我也可以并且会修复它,但这需要停止,这是我的最佳选择。

点赞
用户4403144
用户4403144

从《Lua 编程》第四版第20章:

函数 setmetatablegetmetatable 也使用元字段,这将保护元表。假设我们想要保护集合,以便用户既不能看到也不能更改它们的元表。如果我们在元表中设置了 __metatable 字段,getmetatable 将返回该字段的值,而 setmetatable 将引发错误:

mt.__metatable = "not your business"

s1 = Set.new{}
print(getmetatable(s1))     --> not your business
setmetatable(s1, {})
    stdin:1: cannot change protected metatable

因此,你的答案是什么? 将 __metatable 字段设置为防止访问表的元表。 对于所有需要保护的表都应该这样做。

2018-08-26 03:26:22