From a905f317a7a0c22f002cf41f518d13b7fe0f561d Mon Sep 17 00:00:00 2001 From: Agent X <44549182+Agent-11@users.noreply.github.com> Date: Sun, 3 Dec 2023 19:13:53 -0500 Subject: [PATCH] Use metatables to make gGlobalSoundSource readonly --- autogen/lua_constants/built-in.lua | 14 +++++++++++++- autogen/lua_definitions/constants.lua | 14 +++++++++++++- src/pc/lua/smlua_constants_autogen.c | 13 ++++++++++++- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/autogen/lua_constants/built-in.lua b/autogen/lua_constants/built-in.lua index 398fb58d..00efa5b8 100644 --- a/autogen/lua_constants/built-in.lua +++ b/autogen/lua_constants/built-in.lua @@ -306,8 +306,20 @@ end -- sound -- ----------- +local function read_only(t) + local proxy = {} + local mt = { -- create metatable + __index = t, + __newindex = function() + error('attempt to update a read-only table', 2) + end + } + setmetatable(proxy, mt) + return proxy +end + --- @type Vec3f -gGlobalSoundSource = { x = 0, y = 0, z = 0 } +gGlobalSoundSource = read_only({ x = 0, y = 0, z = 0 }) --- @param bank number --- @param soundID number diff --git a/autogen/lua_definitions/constants.lua b/autogen/lua_definitions/constants.lua index 489f19ab..bd8f54ce 100644 --- a/autogen/lua_definitions/constants.lua +++ b/autogen/lua_definitions/constants.lua @@ -308,8 +308,20 @@ end -- sound -- ----------- +local function read_only(t) + local proxy = {} + local mt = { -- create metatable + __index = t, + __newindex = function() + error('attempt to update a read-only table', 2) + end + } + setmetatable(proxy, mt) + return proxy +end + --- @type Vec3f -gGlobalSoundSource = { x = 0, y = 0, z = 0 } +gGlobalSoundSource = read_only({ x = 0, y = 0, z = 0 }) --- @param bank number --- @param soundID number diff --git a/src/pc/lua/smlua_constants_autogen.c b/src/pc/lua/smlua_constants_autogen.c index 81da2b11..3b1dd128 100644 --- a/src/pc/lua/smlua_constants_autogen.c +++ b/src/pc/lua/smlua_constants_autogen.c @@ -272,8 +272,19 @@ char gSmluaConstants[] = "" "-----------\n" "-- sound --\n" "-----------\n" +"local function read_only(t)\n" +" local proxy = {}\n" +" local mt = { -- create metatable\n" +" __index = t,\n" +" __newindex = function()\n" +" error('attempt to update a read-only table', 2)\n" +" end\n" +" }\n" +" setmetatable(proxy, mt)\n" +" return proxy\n" +"end\n" "--- @type Vec3f\n" -"gGlobalSoundSource = { x = 0, y = 0, z = 0 }\n" +"gGlobalSoundSource = read_only({ x = 0, y = 0, z = 0 })\n" "--- @param bank number\n" "--- @param soundID number\n" "--- @param priority number\n"