From 524d44675709b6a776b336a74d30f627ac80ad24 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Wed, 28 Dec 2022 23:37:31 +0100 Subject: [PATCH] Minor script api fixes/cleanups --- src/map_settings_manager.cpp | 4 ++-- src/map_settings_manager.h | 6 +++--- src/script/cpp_api/s_async.cpp | 1 + src/script/cpp_api/s_base.cpp | 11 +++++++++-- src/script/cpp_api/s_base.h | 5 +++-- src/script/cpp_api/s_entity.cpp | 3 +-- src/script/lua_api/l_env.cpp | 12 +----------- src/script/lua_api/l_mapgen.cpp | 5 +---- src/script/lua_api/l_vmanip.cpp | 19 +++++++++++++------ src/script/lua_api/l_vmanip.h | 2 ++ src/script/scripting_server.cpp | 5 +---- 11 files changed, 37 insertions(+), 36 deletions(-) diff --git a/src/map_settings_manager.cpp b/src/map_settings_manager.cpp index 7c19b1f11..264a941c2 100644 --- a/src/map_settings_manager.cpp +++ b/src/map_settings_manager.cpp @@ -50,14 +50,14 @@ MapSettingsManager::~MapSettingsManager() bool MapSettingsManager::getMapSetting( - const std::string &name, std::string *value_out) + const std::string &name, std::string *value_out) const { return m_map_settings->getNoEx(name, *value_out); } bool MapSettingsManager::getMapSettingNoiseParams( - const std::string &name, NoiseParams *value_out) + const std::string &name, NoiseParams *value_out) const { // TODO: Rename to "getNoiseParams" return m_map_settings->getNoiseParams(name, *value_out); diff --git a/src/map_settings_manager.h b/src/map_settings_manager.h index fa271268d..72b86f517 100644 --- a/src/map_settings_manager.h +++ b/src/map_settings_manager.h @@ -50,10 +50,10 @@ public: // Finalized map generation parameters MapgenParams *mapgen_params = nullptr; - bool getMapSetting(const std::string &name, std::string *value_out); + bool getMapSetting(const std::string &name, std::string *value_out) const; - bool getMapSettingNoiseParams( - const std::string &name, NoiseParams *value_out); + bool getMapSettingNoiseParams(const std::string &name, + NoiseParams *value_out) const; // Note: Map config becomes read-only after makeMapgenParams() gets called // (i.e. mapgen_params is non-NULL). Attempts to set map config after diff --git a/src/script/cpp_api/s_async.cpp b/src/script/cpp_api/s_async.cpp index ca88031fd..75b1a8205 100644 --- a/src/script/cpp_api/s_async.cpp +++ b/src/script/cpp_api/s_async.cpp @@ -298,6 +298,7 @@ AsyncWorkerThread::AsyncWorkerThread(AsyncEngine* jobDispatcher, // can't throw from here so we're stuck with this isErrored = true; } + lua_pop(L, 1); } /******************************************************************************/ diff --git a/src/script/cpp_api/s_base.cpp b/src/script/cpp_api/s_base.cpp index bb3e56502..73f8c49a1 100644 --- a/src/script/cpp_api/s_base.cpp +++ b/src/script/cpp_api/s_base.cpp @@ -413,7 +413,7 @@ void ScriptApiBase::setOriginFromTableRaw(int index, const char *fxn) void ScriptApiBase::addObjectReference(ServerActiveObject *cobj) { SCRIPTAPI_PRECHECKHEADER - //infostream<<"scriptapi_add_object_reference: id="<getId()<getId() == 0) { ObjectRef::create(L, cobj); } else { @@ -472,6 +473,7 @@ void ScriptApiBase::objectrefGetOrCreate(lua_State *L, void ScriptApiBase::pushPlayerHPChangeReason(lua_State *L, const PlayerHPChangeReason &reason) { + assert(getType() == ScriptingType::Server); if (reason.hasLuaReference()) lua_rawgeti(L, LUA_REGISTRYINDEX, reason.lua_reference); else @@ -503,8 +505,13 @@ void ScriptApiBase::pushPlayerHPChangeReason(lua_State *L, const PlayerHPChangeR Server* ScriptApiBase::getServer() { + // Since the gamedef is the server it's still possible to retrieve it in + // e.g. the async environment, but this isn't meant to happen. + // TODO: still needs work + //assert(getType() == ScriptingType::Server); return dynamic_cast(m_gamedef); } + #ifndef SERVER Client* ScriptApiBase::getClient() { diff --git a/src/script/cpp_api/s_base.h b/src/script/cpp_api/s_base.h index 1dc810e08..a83965e18 100644 --- a/src/script/cpp_api/s_base.h +++ b/src/script/cpp_api/s_base.h @@ -58,7 +58,7 @@ extern "C" { setOriginFromTableRaw(index, __FUNCTION__) enum class ScriptingType: u8 { - Async, + Async, // either mainmenu (client) or ingame (server) Client, MainMenu, Server @@ -100,9 +100,10 @@ public: void addObjectReference(ServerActiveObject *cobj); void removeObjectReference(ServerActiveObject *cobj); + ScriptingType getType() { return m_type; } + IGameDef *getGameDef() { return m_gamedef; } Server* getServer(); - ScriptingType getType() { return m_type; } #ifndef SERVER Client* getClient(); #endif diff --git a/src/script/cpp_api/s_entity.cpp b/src/script/cpp_api/s_entity.cpp index 852a27ddc..1eb20e27f 100644 --- a/src/script/cpp_api/s_entity.cpp +++ b/src/script/cpp_api/s_entity.cpp @@ -59,8 +59,7 @@ bool ScriptApiEntity::luaentity_Add(u16 id, const char *name) // This should be userdata with metatable ObjectRef push_objectRef(L, id); luaL_checktype(L, -1, LUA_TUSERDATA); - if (!luaL_checkudata(L, -1, "ObjectRef")) - luaL_typerror(L, -1, "ObjectRef"); + luaL_checkudata(L, -1, "ObjectRef"); lua_setfield(L, -2, "object"); // core.luaentities[id] = object diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp index 3b9a20a76..731c50768 100644 --- a/src/script/lua_api/l_env.cpp +++ b/src/script/lua_api/l_env.cpp @@ -1061,17 +1061,7 @@ int ModApiEnvMod::l_get_perlin_map(lua_State *L) // returns voxel manipulator int ModApiEnvMod::l_get_voxel_manip(lua_State *L) { - GET_ENV_PTR; - - Map *map = &(env->getMap()); - LuaVoxelManip *o = (lua_istable(L, 1) && lua_istable(L, 2)) ? - new LuaVoxelManip(map, read_v3s16(L, 1), read_v3s16(L, 2)) : - new LuaVoxelManip(map); - - *(void **)(lua_newuserdata(L, sizeof(void *))) = o; - luaL_getmetatable(L, "VoxelManip"); - lua_setmetatable(L, -2); - return 1; + return LuaVoxelManip::create_object(L); } // clear_objects([options]) diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index 3dbd76ef8..5d6fce4e0 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -621,10 +621,7 @@ int ModApiMapgen::l_get_mapgen_object(lua_State *L) MMVManip *vm = mg->vm; // VoxelManip object - LuaVoxelManip *o = new LuaVoxelManip(vm, true); - *(void **)(lua_newuserdata(L, sizeof(void *))) = o; - luaL_getmetatable(L, "VoxelManip"); - lua_setmetatable(L, -2); + LuaVoxelManip::create(L, vm, true); // emerged min pos push_v3s16(L, vm->m_area.MinEdge); diff --git a/src/script/lua_api/l_vmanip.cpp b/src/script/lua_api/l_vmanip.cpp index 90552a239..7b27402d6 100644 --- a/src/script/lua_api/l_vmanip.cpp +++ b/src/script/lua_api/l_vmanip.cpp @@ -111,12 +111,14 @@ int LuaVoxelManip::l_set_data(lua_State *L) int LuaVoxelManip::l_write_to_map(lua_State *L) { - MAP_LOCK_REQUIRED; + GET_ENV_PTR; LuaVoxelManip *o = checkObject(L, 1); bool update_light = !lua_isboolean(L, 2) || readParam(L, 2); - GET_ENV_PTR; + if (o->vm->isOrphan()) + return 0; + ServerMap *map = &(env->getServerMap()); std::map modified_blocks; @@ -420,6 +422,14 @@ int LuaVoxelManip::create_object(lua_State *L) return 1; } +void LuaVoxelManip::create(lua_State *L, MMVManip *mmvm, bool is_mapgen_vm) +{ + LuaVoxelManip *o = new LuaVoxelManip(mmvm, is_mapgen_vm); + *(void **)(lua_newuserdata(L, sizeof(void *))) = o; + luaL_getmetatable(L, className); + lua_setmetatable(L, -2); +} + void *LuaVoxelManip::packIn(lua_State *L, int idx) { LuaVoxelManip *o = checkObject(L, idx); @@ -442,10 +452,7 @@ void LuaVoxelManip::packOut(lua_State *L, void *ptr) if (env) vm->reparent(&(env->getMap())); - LuaVoxelManip *o = new LuaVoxelManip(vm, false); - *(void **)(lua_newuserdata(L, sizeof(void *))) = o; - luaL_getmetatable(L, className); - lua_setmetatable(L, -2); + create(L, vm, false); } void LuaVoxelManip::Register(lua_State *L) diff --git a/src/script/lua_api/l_vmanip.h b/src/script/lua_api/l_vmanip.h index e03ebdad7..04670bef6 100644 --- a/src/script/lua_api/l_vmanip.h +++ b/src/script/lua_api/l_vmanip.h @@ -71,6 +71,8 @@ public: // LuaVoxelManip() // Creates a LuaVoxelManip and leaves it on top of stack static int create_object(lua_State *L); + // Not callable from Lua + static void create(lua_State *L, MMVManip *mmvm, bool is_mapgen_vm); static void *packIn(lua_State *L, int idx); static void packOut(lua_State *L, void *ptr); diff --git a/src/script/scripting_server.cpp b/src/script/scripting_server.cpp index b462141b0..849ea2337 100644 --- a/src/script/scripting_server.cpp +++ b/src/script/scripting_server.cpp @@ -182,10 +182,7 @@ void ServerScripting::InitializeAsync(lua_State *L, int top) LuaSettings::Register(L); // globals data - lua_getglobal(L, "core"); - luaL_checktype(L, -1, LUA_TTABLE); auto *data = ModApiBase::getServer(L)->m_async_globals_data.get(); script_unpack(L, data); - lua_setfield(L, -2, "transferred_globals"); - lua_pop(L, 1); // pop 'core' + lua_setfield(L, top, "transferred_globals"); }