Minor script api fixes/cleanups

This commit is contained in:
sfan5 2022-12-28 23:37:31 +01:00
parent 5b6bc8a12b
commit 524d446757
11 changed files with 37 additions and 36 deletions

View File

@ -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);

View File

@ -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

View File

@ -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);
}
/******************************************************************************/

View File

@ -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()
{

View File

@ -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

View File

@ -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

View File

@ -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])

View File

@ -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);

View File

@ -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)

View File

@ -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);

View File

@ -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");
}