Skip to content

Commit

Permalink
Minor script api fixes/cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
sfan5 committed Jun 23, 2023
1 parent 5b6bc8a commit 524d446
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 36 deletions.
4 changes: 2 additions & 2 deletions src/map_settings_manager.cpp
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/map_settings_manager.h
Expand Up @@ -50,10 +50,10 @@ class MapSettingsManager {
// 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
Expand Down
1 change: 1 addition & 0 deletions src/script/cpp_api/s_async.cpp
Expand Up @@ -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);
}

/******************************************************************************/
Expand Down
11 changes: 9 additions & 2 deletions src/script/cpp_api/s_base.cpp
Expand Up @@ -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
Expand All @@ -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");
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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()
{
Expand Down
5 changes: 3 additions & 2 deletions src/script/cpp_api/s_base.h
Expand Up @@ -58,7 +58,7 @@ extern "C" {
setOriginFromTableRaw(index, __FUNCTION__)

enum class ScriptingType: u8 {
Async,
Async, // either mainmenu (client) or ingame (server)
Client,
MainMenu,
Server
Expand Down Expand Up @@ -100,9 +100,10 @@ class ScriptApiBase : protected LuaHelper {
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
Expand Down
3 changes: 1 addition & 2 deletions src/script/cpp_api/s_entity.cpp
Expand Up @@ -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
Expand Down
12 changes: 1 addition & 11 deletions src/script/lua_api/l_env.cpp
Expand Up @@ -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])
Expand Down
5 changes: 1 addition & 4 deletions src/script/lua_api/l_mapgen.cpp
Expand Up @@ -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);
Expand Down
19 changes: 13 additions & 6 deletions src/script/lua_api/l_vmanip.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/script/lua_api/l_vmanip.h
Expand Up @@ -71,6 +71,8 @@ class LuaVoxelManip : public ModApiBase
// 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);
Expand Down
5 changes: 1 addition & 4 deletions src/script/scripting_server.cpp
Expand Up @@ -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");
}

0 comments on commit 524d446

Please sign in to comment.