diff options
author | SmallJoker <mk939@ymail.com> | 2019-06-25 21:18:08 +0200 |
---|---|---|
committer | SmallJoker <mk939@ymail.com> | 2019-09-14 19:42:25 +0200 |
commit | 23677be951b4dc2b9cc174ceb92f0e0b8e8eb867 (patch) | |
tree | 87e8a9bee105bbe77d964e109d0cefd1102f07e7 /src/script/lua_api | |
parent | 720aedb46740ba203b4a0f3e049e368abc4932b3 (diff) | |
download | minetest-23677be951b4dc2b9cc174ceb92f0e0b8e8eb867.tar.gz minetest-23677be951b4dc2b9cc174ceb92f0e0b8e8eb867.tar.bz2 minetest-23677be951b4dc2b9cc174ceb92f0e0b8e8eb867.zip |
Load CSM environment after the restrictions are known
Safety-guards for CSM callbacks to abort on a bad implementation
Only run callbacks when the mods are loaded (and with it: builtin)
Duplication checks inside constructors
Diffstat (limited to 'src/script/lua_api')
-rw-r--r-- | src/script/lua_api/l_camera.cpp | 21 | ||||
-rw-r--r-- | src/script/lua_api/l_localplayer.cpp | 22 |
2 files changed, 26 insertions, 17 deletions
diff --git a/src/script/lua_api/l_camera.cpp b/src/script/lua_api/l_camera.cpp index 462006777..80071b3b8 100644 --- a/src/script/lua_api/l_camera.cpp +++ b/src/script/lua_api/l_camera.cpp @@ -31,19 +31,24 @@ LuaCamera::LuaCamera(Camera *m) : m_camera(m) void LuaCamera::create(lua_State *L, Camera *m) { + lua_getglobal(L, "core"); + luaL_checktype(L, -1, LUA_TTABLE); + int objectstable = lua_gettop(L); + lua_getfield(L, -1, "camera"); + + // Duplication check + if (lua_type(L, -1) == LUA_TUSERDATA) { + lua_pop(L, 1); + return; + } + LuaCamera *o = new LuaCamera(m); *(void **)(lua_newuserdata(L, sizeof(void *))) = o; luaL_getmetatable(L, className); lua_setmetatable(L, -2); - int camera_object = lua_gettop(L); - - lua_getglobal(L, "core"); - luaL_checktype(L, -1, LUA_TTABLE); - int coretable = lua_gettop(L); - - lua_pushvalue(L, camera_object); - lua_setfield(L, coretable, "camera"); + lua_pushvalue(L, lua_gettop(L)); + lua_setfield(L, objectstable, "camera"); } int LuaCamera::l_set_camera_mode(lua_State *L) diff --git a/src/script/lua_api/l_localplayer.cpp b/src/script/lua_api/l_localplayer.cpp index 7444d0e88..3e14e48e4 100644 --- a/src/script/lua_api/l_localplayer.cpp +++ b/src/script/lua_api/l_localplayer.cpp @@ -30,20 +30,24 @@ LuaLocalPlayer::LuaLocalPlayer(LocalPlayer *m) : m_localplayer(m) void LuaLocalPlayer::create(lua_State *L, LocalPlayer *m) { + lua_getglobal(L, "core"); + luaL_checktype(L, -1, LUA_TTABLE); + int objectstable = lua_gettop(L); + lua_getfield(L, -1, "localplayer"); + + // Duplication check + if (lua_type(L, -1) == LUA_TUSERDATA) { + lua_pop(L, 1); + return; + } + LuaLocalPlayer *o = new LuaLocalPlayer(m); *(void **)(lua_newuserdata(L, sizeof(void *))) = o; luaL_getmetatable(L, className); lua_setmetatable(L, -2); - // Keep localplayer object stack id - int localplayer_object = lua_gettop(L); - - lua_getglobal(L, "core"); - luaL_checktype(L, -1, LUA_TTABLE); - int coretable = lua_gettop(L); - - lua_pushvalue(L, localplayer_object); - lua_setfield(L, coretable, "localplayer"); + lua_pushvalue(L, lua_gettop(L)); + lua_setfield(L, objectstable, "localplayer"); } int LuaLocalPlayer::l_get_velocity(lua_State *L) |