1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Refactor how script api reads current mod name

This is to prevent future mistakes and make it clearer whether
the mod name can be trusted depending on how it is retrieved.
This commit is contained in:
sfan5 2024-02-14 13:29:53 +01:00
parent cb5fa56e17
commit ce97210eb1
8 changed files with 88 additions and 78 deletions

View file

@ -82,8 +82,7 @@ EmergeThread *ModApiBase::getEmergeThread(lua_State *L)
std::string ModApiBase::getCurrentModPath(lua_State *L)
{
lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
std::string current_mod_name = readParam<std::string>(L, -1, "");
std::string current_mod_name = ScriptApiBase::getCurrentModNameInsecure(L);
if (current_mod_name.empty())
return ".";

View file

@ -62,7 +62,11 @@ const static CSMFlagDesc flagdesc_csm_restriction[] = {
// get_current_modname()
int ModApiClient::l_get_current_modname(lua_State *L)
{
lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
std::string s = ScriptApiBase::getCurrentModNameInsecure(L);
if (!s.empty())
lua_pushstring(L, s.c_str());
else
lua_pushnil(L);
return 1;
}
@ -76,30 +80,6 @@ int ModApiClient::l_get_modpath(lua_State *L)
return 1;
}
// get_last_run_mod()
int ModApiClient::l_get_last_run_mod(lua_State *L)
{
lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
std::string current_mod = readParam<std::string>(L, -1, "");
if (current_mod.empty()) {
lua_pop(L, 1);
lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str());
}
return 1;
}
// set_last_run_mod(modname)
int ModApiClient::l_set_last_run_mod(lua_State *L)
{
if (!lua_isstring(L, 1))
return 0;
const char *mod = lua_tostring(L, 1);
getScriptApiBase(L)->setOriginDirect(mod);
lua_pushboolean(L, true);
return 1;
}
// print(text)
int ModApiClient::l_print(lua_State *L)
{
@ -367,8 +347,6 @@ void ModApiClient::Initialize(lua_State *L, int top)
API_FCT(send_chat_message);
API_FCT(clear_out_chat_queue);
API_FCT(get_player_names);
API_FCT(set_last_run_mod);
API_FCT(get_last_run_mod);
API_FCT(show_formspec);
API_FCT(send_respawn);
API_FCT(gettext);

View file

@ -60,12 +60,6 @@ private:
// gettext(text)
static int l_gettext(lua_State *L);
// get_last_run_mod(n)
static int l_get_last_run_mod(lua_State *L);
// set_last_run_mod(modname)
static int l_set_last_run_mod(lua_State *L);
// get_node(pos)
static int l_get_node_or_nil(lua_State *L);

View file

@ -440,7 +440,11 @@ int ModApiServer::l_show_formspec(lua_State *L)
int ModApiServer::l_get_current_modname(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
std::string s = ScriptApiBase::getCurrentModNameInsecure(L);
if (!s.empty())
lua_pushstring(L, s.c_str());
else
lua_pushnil(L);
return 1;
}
@ -656,11 +660,9 @@ int ModApiServer::l_register_async_dofile(lua_State *L)
std::string path = readParam<std::string>(L, 1);
CHECK_SECURE_PATH(L, path.c_str(), false);
// Find currently running mod name (only at init time)
lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
if (!lua_isstring(L, -1))
return 0;
std::string modname = readParam<std::string>(L, -1);
std::string modname = ScriptApiBase::getCurrentModNameInsecure(L);
if (modname.empty())
throw ModError("cannot determine mod name");
getServer(L)->m_async_init_files.emplace_back(modname, path);
lua_pushboolean(L, true);
@ -675,11 +677,9 @@ int ModApiServer::l_register_mapgen_script(lua_State *L)
std::string path = readParam<std::string>(L, 1);
CHECK_SECURE_PATH(L, path.c_str(), false);
// Find currently running mod name (only at init time)
lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
if (!lua_isstring(L, -1))
return 0;
std::string modname = readParam<std::string>(L, -1);
std::string modname = ScriptApiBase::getCurrentModNameInsecure(L);
if (modname.empty())
throw ModError("cannot determine mod name");
getServer(L)->m_mapgen_init_files.emplace_back(modname, path);
lua_pushboolean(L, true);

View file

@ -632,12 +632,10 @@ int ModApiUtil::l_get_last_run_mod(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
std::string current_mod = readParam<std::string>(L, -1, "");
if (current_mod.empty()) {
lua_pop(L, 1);
lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str());
}
std::string current_mod = ScriptApiBase::getCurrentModNameInsecure(L);
if (current_mod.empty())
current_mod = getScriptApiBase(L)->getOrigin();
lua_pushstring(L, current_mod.c_str());
return 1;
}
@ -735,6 +733,9 @@ void ModApiUtil::InitializeClient(lua_State *L, int top)
API_FCT(colorspec_to_colorstring);
API_FCT(colorspec_to_bytes);
API_FCT(get_last_run_mod);
API_FCT(set_last_run_mod);
API_FCT(urlencode);
LuaSettings::create(L, g_settings, g_settings_path);