Refactor some Lua API functions in preparation for async env

This commit is contained in:
sfan5 2022-04-09 14:47:59 +02:00
parent e6385e2ab7
commit 56a558baf8
8 changed files with 37 additions and 45 deletions

@ -63,6 +63,8 @@ public:
virtual IRollbackManager* getRollbackManager() { return NULL; } virtual IRollbackManager* getRollbackManager() { return NULL; }
// Shorthands // Shorthands
// TODO: these should be made const-safe so that a const IGameDef* is
// actually usable
IItemDefManager *idef() { return getItemDefManager(); } IItemDefManager *idef() { return getItemDefManager(); }
const NodeDefManager *ndef() { return getNodeDefManager(); } const NodeDefManager *ndef() { return getNodeDefManager(); }
ICraftDefManager *cdef() { return getCraftDefManager(); } ICraftDefManager *cdef() { return getCraftDefManager(); }

@ -1371,7 +1371,7 @@ void push_inventory_lists(lua_State *L, const Inventory &inv)
/******************************************************************************/ /******************************************************************************/
void read_inventory_list(lua_State *L, int tableindex, void read_inventory_list(lua_State *L, int tableindex,
Inventory *inv, const char *name, Server* srv, int forcesize) Inventory *inv, const char *name, IGameDef *gdef, int forcesize)
{ {
if(tableindex < 0) if(tableindex < 0)
tableindex = lua_gettop(L) + 1 + tableindex; tableindex = lua_gettop(L) + 1 + tableindex;
@ -1383,7 +1383,7 @@ void read_inventory_list(lua_State *L, int tableindex,
} }
// Get Lua-specified items to insert into the list // Get Lua-specified items to insert into the list
std::vector<ItemStack> items = read_items(L, tableindex,srv); std::vector<ItemStack> items = read_items(L, tableindex, gdef);
size_t listsize = (forcesize >= 0) ? forcesize : items.size(); size_t listsize = (forcesize >= 0) ? forcesize : items.size();
// Create or resize/clear list // Create or resize/clear list
@ -1635,7 +1635,7 @@ void push_items(lua_State *L, const std::vector<ItemStack> &items)
} }
/******************************************************************************/ /******************************************************************************/
std::vector<ItemStack> read_items(lua_State *L, int index, Server *srv) std::vector<ItemStack> read_items(lua_State *L, int index, IGameDef *gdef)
{ {
if(index < 0) if(index < 0)
index = lua_gettop(L) + 1 + index; index = lua_gettop(L) + 1 + index;
@ -1651,7 +1651,7 @@ std::vector<ItemStack> read_items(lua_State *L, int index, Server *srv)
if (items.size() < (u32) key) { if (items.size() < (u32) key) {
items.resize(key); items.resize(key);
} }
items[key - 1] = read_item(L, -1, srv->idef()); items[key - 1] = read_item(L, -1, gdef->idef());
lua_pop(L, 1); lua_pop(L, 1);
} }
return items; return items;

@ -59,7 +59,7 @@ class InventoryList;
struct NodeBox; struct NodeBox;
struct ContentFeatures; struct ContentFeatures;
struct TileDef; struct TileDef;
class Server; class IGameDef;
struct DigParams; struct DigParams;
struct HitParams; struct HitParams;
struct EnumString; struct EnumString;
@ -126,7 +126,7 @@ void push_inventory_lists (lua_State *L,
const Inventory &inv); const Inventory &inv);
void read_inventory_list (lua_State *L, int tableindex, void read_inventory_list (lua_State *L, int tableindex,
Inventory *inv, const char *name, Inventory *inv, const char *name,
Server *srv, int forcesize=-1); IGameDef *gdef, int forcesize=-1);
MapNode readnode (lua_State *L, int index, MapNode readnode (lua_State *L, int index,
const NodeDefManager *ndef); const NodeDefManager *ndef);
@ -166,7 +166,7 @@ void push_items (lua_State *L,
std::vector<ItemStack> read_items (lua_State *L, std::vector<ItemStack> read_items (lua_State *L,
int index, int index,
Server* srv); IGameDef* gdef);
void push_soundspec (lua_State *L, void push_soundspec (lua_State *L,
const SimpleSoundSpec &spec); const SimpleSoundSpec &spec);

@ -371,8 +371,9 @@ int ModApiCraft::l_clear_craft(lua_State *L)
int ModApiCraft::l_get_craft_result(lua_State *L) int ModApiCraft::l_get_craft_result(lua_State *L)
{ {
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
IGameDef *gdef = getGameDef(L);
int input_i = 1; const int input_i = 1;
std::string method_s = getstringfield_default(L, input_i, "method", "normal"); std::string method_s = getstringfield_default(L, input_i, "method", "normal");
enum CraftMethod method = (CraftMethod)getenumfield(L, input_i, "method", enum CraftMethod method = (CraftMethod)getenumfield(L, input_i, "method",
es_CraftMethod, CRAFT_METHOD_NORMAL); es_CraftMethod, CRAFT_METHOD_NORMAL);
@ -382,10 +383,9 @@ int ModApiCraft::l_get_craft_result(lua_State *L)
width = luaL_checkinteger(L, -1); width = luaL_checkinteger(L, -1);
lua_pop(L, 1); lua_pop(L, 1);
lua_getfield(L, input_i, "items"); lua_getfield(L, input_i, "items");
std::vector<ItemStack> items = read_items(L, -1,getServer(L)); std::vector<ItemStack> items = read_items(L, -1, gdef);
lua_pop(L, 1); // items lua_pop(L, 1); // items
IGameDef *gdef = getServer(L);
ICraftDefManager *cdef = gdef->cdef(); ICraftDefManager *cdef = gdef->cdef();
CraftInput input(method, width, items); CraftInput input(method, width, items);
CraftOutput output; CraftOutput output;
@ -465,13 +465,13 @@ static void push_craft_recipes(lua_State *L, IGameDef *gdef,
const std::vector<CraftDefinition*> &recipes, const std::vector<CraftDefinition*> &recipes,
const CraftOutput &output) const CraftOutput &output)
{ {
lua_createtable(L, recipes.size(), 0);
if (recipes.empty()) { if (recipes.empty()) {
lua_pushnil(L); lua_pushnil(L);
return; return;
} }
lua_createtable(L, recipes.size(), 0);
std::vector<CraftDefinition*>::const_iterator it = recipes.begin(); std::vector<CraftDefinition*>::const_iterator it = recipes.begin();
for (unsigned i = 0; it != recipes.end(); ++it) { for (unsigned i = 0; it != recipes.end(); ++it) {
lua_newtable(L); lua_newtable(L);
@ -487,10 +487,9 @@ int ModApiCraft::l_get_craft_recipe(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
std::string item = luaL_checkstring(L, 1); std::string item = luaL_checkstring(L, 1);
Server *server = getServer(L); IGameDef *gdef = getGameDef(L);
CraftOutput output(item, 0); CraftOutput output(item, 0);
std::vector<CraftDefinition*> recipes = server->cdef() auto recipes = gdef->cdef()->getCraftRecipes(output, gdef, 1);
->getCraftRecipes(output, server, 1);
lua_createtable(L, 1, 0); lua_createtable(L, 1, 0);
@ -500,7 +499,7 @@ int ModApiCraft::l_get_craft_recipe(lua_State *L)
setintfield(L, -1, "width", 0); setintfield(L, -1, "width", 0);
return 1; return 1;
} }
push_craft_recipe(L, server, recipes[0], output); push_craft_recipe(L, gdef, recipes[0], output);
return 1; return 1;
} }
@ -510,12 +509,11 @@ int ModApiCraft::l_get_all_craft_recipes(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
std::string item = luaL_checkstring(L, 1); std::string item = luaL_checkstring(L, 1);
Server *server = getServer(L); IGameDef *gdef = getGameDef(L);
CraftOutput output(item, 0); CraftOutput output(item, 0);
std::vector<CraftDefinition*> recipes = server->cdef() auto recipes = gdef->cdef()->getCraftRecipes(output, gdef);
->getCraftRecipes(output, server);
push_craft_recipes(L, server, recipes, output); push_craft_recipes(L, gdef, recipes, output);
return 1; return 1;
} }

@ -632,8 +632,8 @@ int ModApiItemMod::l_get_content_id(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
std::string name = luaL_checkstring(L, 1); std::string name = luaL_checkstring(L, 1);
const IItemDefManager *idef = getGameDef(L)->getItemDefManager(); const IItemDefManager *idef = getGameDef(L)->idef();
const NodeDefManager *ndef = getGameDef(L)->getNodeDefManager(); const NodeDefManager *ndef = getGameDef(L)->ndef();
// If this is called at mod load time, NodeDefManager isn't aware of // If this is called at mod load time, NodeDefManager isn't aware of
// aliases yet, so we need to handle them manually // aliases yet, so we need to handle them manually
@ -658,7 +658,7 @@ int ModApiItemMod::l_get_name_from_content_id(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
content_t c = luaL_checkint(L, 1); content_t c = luaL_checkint(L, 1);
const NodeDefManager *ndef = getGameDef(L)->getNodeDefManager(); const NodeDefManager *ndef = getGameDef(L)->ndef();
const char *name = ndef->get(c).name.c_str(); const char *name = ndef->get(c).name.c_str();
lua_pushstring(L, name); lua_pushstring(L, name);

@ -61,11 +61,8 @@ int ModApiServer::l_get_server_uptime(lua_State *L)
int ModApiServer::l_get_server_max_lag(lua_State *L) int ModApiServer::l_get_server_max_lag(lua_State *L)
{ {
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
ServerEnvironment *s_env = dynamic_cast<ServerEnvironment *>(getEnv(L)); GET_ENV_PTR;
if (!s_env) lua_pushnumber(L, env->getMaxLagEstimate());
lua_pushnil(L);
else
lua_pushnumber(L, s_env->getMaxLagEstimate());
return 1; return 1;
} }
@ -395,11 +392,10 @@ int ModApiServer::l_get_modpath(lua_State *L)
{ {
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
std::string modname = luaL_checkstring(L, 1); std::string modname = luaL_checkstring(L, 1);
const ModSpec *mod = getServer(L)->getModSpec(modname); const ModSpec *mod = getGameDef(L)->getModSpec(modname);
if (!mod) { if (!mod)
lua_pushnil(L); lua_pushnil(L);
return 1; else
}
lua_pushstring(L, mod->path.c_str()); lua_pushstring(L, mod->path.c_str());
return 1; return 1;
} }
@ -412,13 +408,14 @@ int ModApiServer::l_get_modnames(lua_State *L)
// Get a list of mods // Get a list of mods
std::vector<std::string> modlist; std::vector<std::string> modlist;
getServer(L)->getModNames(modlist); for (auto &it : getGameDef(L)->getMods())
modlist.emplace_back(it.name);
std::sort(modlist.begin(), modlist.end()); std::sort(modlist.begin(), modlist.end());
// Package them up for Lua // Package them up for Lua
lua_createtable(L, modlist.size(), 0); lua_createtable(L, modlist.size(), 0);
std::vector<std::string>::iterator iter = modlist.begin(); auto iter = modlist.begin();
for (u16 i = 0; iter != modlist.end(); ++iter) { for (u16 i = 0; iter != modlist.end(); ++iter) {
lua_pushstring(L, iter->c_str()); lua_pushstring(L, iter->c_str());
lua_rawseti(L, -2, ++i); lua_rawseti(L, -2, ++i);
@ -430,8 +427,8 @@ int ModApiServer::l_get_modnames(lua_State *L)
int ModApiServer::l_get_worldpath(lua_State *L) int ModApiServer::l_get_worldpath(lua_State *L)
{ {
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
std::string worldpath = getServer(L)->getWorldPath(); const Server *srv = getServer(L);
lua_pushstring(L, worldpath.c_str()); lua_pushstring(L, srv->getWorldPath().c_str());
return 1; return 1;
} }
@ -513,7 +510,8 @@ int ModApiServer::l_dynamic_add_media(lua_State *L)
int ModApiServer::l_is_singleplayer(lua_State *L) int ModApiServer::l_is_singleplayer(lua_State *L)
{ {
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
lua_pushboolean(L, getServer(L)->isSingleplayer()); const Server *srv = getServer(L);
lua_pushboolean(L, srv->isSingleplayer());
return 1; return 1;
} }

@ -3658,11 +3658,6 @@ const ModSpec *Server::getModSpec(const std::string &modname) const
return m_modmgr->getModSpec(modname); return m_modmgr->getModSpec(modname);
} }
void Server::getModNames(std::vector<std::string> &modlist)
{
m_modmgr->getModNames(modlist);
}
std::string Server::getBuiltinLuaPath() std::string Server::getBuiltinLuaPath()
{ {
return porting::path_share + DIR_DELIM + "builtin"; return porting::path_share + DIR_DELIM + "builtin";

@ -292,11 +292,10 @@ public:
virtual const std::vector<ModSpec> &getMods() const; virtual const std::vector<ModSpec> &getMods() const;
virtual const ModSpec* getModSpec(const std::string &modname) const; virtual const ModSpec* getModSpec(const std::string &modname) const;
void getModNames(std::vector<std::string> &modlist);
std::string getBuiltinLuaPath(); std::string getBuiltinLuaPath();
virtual std::string getWorldPath() const { return m_path_world; } virtual std::string getWorldPath() const { return m_path_world; }
inline bool isSingleplayer() inline bool isSingleplayer() const
{ return m_simple_singleplayer_mode; } { return m_simple_singleplayer_mode; }
inline void setAsyncFatalError(const std::string &error) inline void setAsyncFatalError(const std::string &error)