Browse Source

Minor script api fixes/cleanups

sfan5 1 year ago
parent
commit
524d446757

+ 2 - 2
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);

+ 3 - 3
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

+ 1 - 0
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);
 }
 
 /******************************************************************************/

+ 9 - 2
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="<<cobj->getId()<<std::endl;
+	assert(getType() == ScriptingType::Server);
 
 	// Create object on stack
 	ObjectRef::create(L, cobj); // Puts ObjectRef (as userdata) on stack
@@ -434,7 +434,7 @@ void ScriptApiBase::addObjectReference(ServerActiveObject *cobj)
 void ScriptApiBase::removeObjectReference(ServerActiveObject *cobj)
 {
 	SCRIPTAPI_PRECHECKHEADER
-	//infostream<<"scriptapi_rm_object_reference: id="<<cobj->getId()<<std::endl;
+	assert(getType() == ScriptingType::Server);
 
 	// Get core.object_refs table
 	lua_getglobal(L, "core");
@@ -459,6 +459,7 @@ void ScriptApiBase::removeObjectReference(ServerActiveObject *cobj)
 void ScriptApiBase::objectrefGetOrCreate(lua_State *L,
 		ServerActiveObject *cobj)
 {
+	assert(getType() == ScriptingType::Server);
 	if (cobj == NULL || cobj->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<Server *>(m_gamedef);
 }
+
 #ifndef SERVER
 Client* ScriptApiBase::getClient()
 {

+ 3 - 2
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

+ 1 - 2
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

+ 1 - 11
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])

+ 1 - 4
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);

+ 13 - 6
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<LuaVoxelManip>(L, 1);
 	bool update_light = !lua_isboolean(L, 2) || readParam<bool>(L, 2);
 
-	GET_ENV_PTR;
+	if (o->vm->isOrphan())
+		return 0;
+
 	ServerMap *map = &(env->getServerMap());
 
 	std::map<v3s16, MapBlock*> 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<LuaVoxelManip>(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)

+ 2 - 0
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);

+ 1 - 4
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");
 }