diff options
Diffstat (limited to 'src/script/lua_api')
-rw-r--r-- | src/script/lua_api/l_areastore.cpp | 4 | ||||
-rw-r--r-- | src/script/lua_api/l_base.cpp | 21 | ||||
-rw-r--r-- | src/script/lua_api/l_base.h | 16 | ||||
-rw-r--r-- | src/script/lua_api/l_client.cpp | 8 | ||||
-rw-r--r-- | src/script/lua_api/l_craft.cpp | 8 | ||||
-rw-r--r-- | src/script/lua_api/l_env.cpp | 18 | ||||
-rw-r--r-- | src/script/lua_api/l_http.cpp | 4 | ||||
-rw-r--r-- | src/script/lua_api/l_inventory.cpp | 4 | ||||
-rw-r--r-- | src/script/lua_api/l_item.cpp | 2 | ||||
-rw-r--r-- | src/script/lua_api/l_mainmenu.cpp | 13 | ||||
-rw-r--r-- | src/script/lua_api/l_mapgen.cpp | 36 | ||||
-rw-r--r-- | src/script/lua_api/l_metadata.cpp | 2 | ||||
-rw-r--r-- | src/script/lua_api/l_nodemeta.cpp | 4 | ||||
-rw-r--r-- | src/script/lua_api/l_object.cpp | 23 | ||||
-rw-r--r-- | src/script/lua_api/l_particles.cpp | 4 | ||||
-rw-r--r-- | src/script/lua_api/l_server.cpp | 18 | ||||
-rw-r--r-- | src/script/lua_api/l_settings.cpp | 4 | ||||
-rw-r--r-- | src/script/lua_api/l_sound.cpp | 2 | ||||
-rw-r--r-- | src/script/lua_api/l_storage.cpp | 2 | ||||
-rw-r--r-- | src/script/lua_api/l_util.cpp | 10 | ||||
-rw-r--r-- | src/script/lua_api/l_vmanip.cpp | 4 |
21 files changed, 88 insertions, 119 deletions
diff --git a/src/script/lua_api/l_areastore.cpp b/src/script/lua_api/l_areastore.cpp index 1e30e704e..d53d74aa8 100644 --- a/src/script/lua_api/l_areastore.cpp +++ b/src/script/lua_api/l_areastore.cpp @@ -156,7 +156,7 @@ int LuaAreaStore::l_get_areas_in_area(lua_State *L) bool include_data = false; bool accept_overlap = false; if (lua_isboolean(L, 4)) { - accept_overlap = lua_toboolean(L, 4); + accept_overlap = readParam<bool>(L, 4); get_data_and_border_flags(L, 5, &include_borders, &include_data); } std::vector<Area *> res; @@ -328,7 +328,7 @@ int LuaAreaStore::create_object(lua_State *L) NO_MAP_LOCK_REQUIRED; LuaAreaStore *o = (lua_isstring(L, 1)) ? - new LuaAreaStore(lua_tostring(L, 1)) : + new LuaAreaStore(readParam<std::string>(L, 1)) : new LuaAreaStore(); *(void **)(lua_newuserdata(L, sizeof(void *))) = o; diff --git a/src/script/lua_api/l_base.cpp b/src/script/lua_api/l_base.cpp index 052f661bb..b401db05a 100644 --- a/src/script/lua_api/l_base.cpp +++ b/src/script/lua_api/l_base.cpp @@ -63,8 +63,8 @@ GUIEngine *ModApiBase::getGuiEngine(lua_State *L) std::string ModApiBase::getCurrentModPath(lua_State *L) { lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME); - const char *current_mod_name = lua_tostring(L, -1); - if (!current_mod_name) + std::string current_mod_name = readParam<std::string>(L, -1, ""); + if (current_mod_name.empty()) return "."; const ModSpec *mod = getServer(L)->getModSpec(current_mod_name); @@ -85,20 +85,3 @@ bool ModApiBase::registerFunction(lua_State *L, const char *name, return true; } - -bool ModApiBase::isNaN(lua_State *L, int idx) -{ - return lua_type(L, idx) == LUA_TNUMBER && std::isnan(lua_tonumber(L, idx)); -} - -/* - * Read template functions - */ -template<> -float ModApiBase::readParam(lua_State *L, int index) -{ - if (isNaN(L, index)) - throw LuaError("NaN value is not allowed."); - - return (float) luaL_checknumber(L, index); -} diff --git a/src/script/lua_api/l_base.h b/src/script/lua_api/l_base.h index d0160f03b..12c1a86cc 100644 --- a/src/script/lua_api/l_base.h +++ b/src/script/lua_api/l_base.h @@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "common/c_types.h" #include "common/c_internal.h" +#include "common/helper.h" #include "gamedef.h" extern "C" { @@ -37,7 +38,7 @@ class Server; class Environment; class GUIEngine; -class ModApiBase { +class ModApiBase : protected LuaHelper { public: static ScriptApiBase* getScriptApiBase(lua_State *L); @@ -69,17 +70,4 @@ public: const char* name, lua_CFunction func, int top); - - static bool isNaN(lua_State *L, int idx); - - /** - * Read a value using a template type T from Lua State L and index - * - * @tparam T type to read from Lua - * @param L Lua state - * @param index Lua Index to read - * @return read value from Lua - */ - template<typename T> - static T readParam(lua_State *L, int index); }; diff --git a/src/script/lua_api/l_client.cpp b/src/script/lua_api/l_client.cpp index f70e65f0f..72826775b 100644 --- a/src/script/lua_api/l_client.cpp +++ b/src/script/lua_api/l_client.cpp @@ -46,8 +46,8 @@ int ModApiClient::l_get_current_modname(lua_State *L) int ModApiClient::l_get_last_run_mod(lua_State *L) { lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME); - const char *current_mod = lua_tostring(L, -1); - if (current_mod == NULL || current_mod[0] == '\0') { + 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()); } @@ -303,7 +303,7 @@ int ModApiClient::l_get_item_def(lua_State *L) if (!lua_isstring(L, 1)) return 0; - const std::string &name(lua_tostring(L, 1)); + std::string name = readParam<std::string>(L, 1); if (!idef->isKnown(name)) return 0; const ItemDefinition &def = idef->get(name); @@ -331,7 +331,7 @@ int ModApiClient::l_get_node_def(lua_State *L) return 0; // clang-format on - const std::string &name = lua_tostring(L, 1); + std::string name = readParam<std::string>(L, 1); const ContentFeatures &cf = ndef->get(ndef->getId(name)); if (cf.name != name) // Unknown node. | name = <whatever>, cf.name = ignore return 0; diff --git a/src/script/lua_api/l_craft.cpp b/src/script/lua_api/l_craft.cpp index 7bf1d314b..64177109e 100644 --- a/src/script/lua_api/l_craft.cpp +++ b/src/script/lua_api/l_craft.cpp @@ -57,7 +57,7 @@ bool ModApiCraft::readCraftRecipeShaped(lua_State *L, int index, // key at index -2 and value at index -1 if(!lua_isstring(L, -1)) return false; - recipe.emplace_back(lua_tostring(L, -1)); + recipe.emplace_back(readParam<std::string>(L, -1)); // removes value, keeps key for next iteration lua_pop(L, 1); colcount++; @@ -90,7 +90,7 @@ bool ModApiCraft::readCraftRecipeShapeless(lua_State *L, int index, // key at index -2 and value at index -1 if(!lua_isstring(L, -1)) return false; - recipe.emplace_back(lua_tostring(L, -1)); + recipe.emplace_back(readParam<std::string>(L, -1)); // removes value, keeps key for next iteration lua_pop(L, 1); } @@ -115,12 +115,12 @@ bool ModApiCraft::readCraftReplacements(lua_State *L, int index, lua_rawgeti(L, -1, 1); if(!lua_isstring(L, -1)) return false; - std::string replace_from = lua_tostring(L, -1); + std::string replace_from = readParam<std::string>(L, -1); lua_pop(L, 1); lua_rawgeti(L, -1, 2); if(!lua_isstring(L, -1)) return false; - std::string replace_to = lua_tostring(L, -1); + std::string replace_to = readParam<std::string>(L, -1); lua_pop(L, 1); replacements.pairs.emplace_back(replace_from, replace_to); // removes value, keeps key for next iteration diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp index 246732a17..4944968da 100644 --- a/src/script/lua_api/l_env.cpp +++ b/src/script/lua_api/l_env.cpp @@ -165,10 +165,10 @@ int LuaRaycast::create_object(lua_State *L) v3f pos1 = checkFloatPos(L, 1); v3f pos2 = checkFloatPos(L, 2); if (lua_isboolean(L, 3)) { - objects = lua_toboolean(L, 3); + objects = readParam<bool>(L, 3); } if (lua_isboolean(L, 4)) { - liquids = lua_toboolean(L, 4); + liquids = readParam<bool>(L, 4); } LuaRaycast *o = new LuaRaycast(core::line3d<f32>(pos1, pos2), @@ -757,15 +757,15 @@ int ModApiEnvMod::l_find_node_near(lua_State *L) while (lua_next(L, 3) != 0) { // key at index -2 and value at index -1 luaL_checktype(L, -1, LUA_TSTRING); - ndef->getIds(lua_tostring(L, -1), filter); + ndef->getIds(readParam<std::string>(L, -1), filter); // removes value, keeps key for next iteration lua_pop(L, 1); } } else if (lua_isstring(L, 3)) { - ndef->getIds(lua_tostring(L, 3), filter); + ndef->getIds(readParam<std::string>(L, 3), filter); } - int start_radius = (lua_toboolean(L, 4)) ? 0 : 1; + int start_radius = (lua_isboolean(L, 4) && readParam<bool>(L, 4)) ? 0 : 1; #ifndef SERVER // Client API limitations @@ -815,12 +815,12 @@ int ModApiEnvMod::l_find_nodes_in_area(lua_State *L) while (lua_next(L, 3) != 0) { // key at index -2 and value at index -1 luaL_checktype(L, -1, LUA_TSTRING); - ndef->getIds(lua_tostring(L, -1), filter); + ndef->getIds(readParam<std::string>(L, -1), filter); // removes value, keeps key for next iteration lua_pop(L, 1); } } else if (lua_isstring(L, 3)) { - ndef->getIds(lua_tostring(L, 3), filter); + ndef->getIds(readParam<std::string>(L, 3), filter); } std::vector<u32> individual_count; @@ -884,12 +884,12 @@ int ModApiEnvMod::l_find_nodes_in_area_under_air(lua_State *L) while (lua_next(L, 3) != 0) { // key at index -2 and value at index -1 luaL_checktype(L, -1, LUA_TSTRING); - ndef->getIds(lua_tostring(L, -1), filter); + ndef->getIds(readParam<std::string>(L, -1), filter); // removes value, keeps key for next iteration lua_pop(L, 1); } } else if (lua_isstring(L, 3)) { - ndef->getIds(lua_tostring(L, 3), filter); + ndef->getIds(readParam<std::string>(L, 3), filter); } lua_newtable(L); diff --git a/src/script/lua_api/l_http.cpp b/src/script/lua_api/l_http.cpp index 641f4194a..ac261cd60 100644 --- a/src/script/lua_api/l_http.cpp +++ b/src/script/lua_api/l_http.cpp @@ -59,7 +59,7 @@ void ModApiHttp::read_http_fetch_request(lua_State *L, HTTPFetchRequest &req) lua_pop(L, 1); } } else if (lua_isstring(L, 2)) { - req.post_data = lua_tostring(L, 2); + req.post_data = readParam<std::string>(L, 2); } lua_pop(L, 1); @@ -154,7 +154,7 @@ int ModApiHttp::l_request_http_api(lua_State *L) return 0; } - const char *mod_name = lua_tostring(L, -1); + std::string mod_name = readParam<std::string>(L, -1); std::string http_mods = g_settings->get("secure.http_mods"); http_mods.erase(std::remove(http_mods.begin(), http_mods.end(), ' '), http_mods.end()); std::vector<std::string> mod_list_http = str_split(http_mods, ','); diff --git a/src/script/lua_api/l_inventory.cpp b/src/script/lua_api/l_inventory.cpp index f3097582e..04fa3a196 100644 --- a/src/script/lua_api/l_inventory.cpp +++ b/src/script/lua_api/l_inventory.cpp @@ -336,7 +336,7 @@ int InvRef::l_contains_item(lua_State *L) InventoryList *list = getlist(L, ref, listname); bool match_meta = false; if (lua_isboolean(L, 4)) - match_meta = lua_toboolean(L, 4); + match_meta = readParam<bool>(L, 4); if (list) { lua_pushboolean(L, list->containsItem(item, match_meta)); } else { @@ -525,7 +525,7 @@ int ModApiInventory::l_create_detached_inventory_raw(lua_State *L) { NO_MAP_LOCK_REQUIRED; const char *name = luaL_checkstring(L, 1); - const char *player = lua_isstring(L, 2) ? lua_tostring(L, 2) : ""; + std::string player = readParam<std::string>(L, 2, ""); if (getServer(L)->createDetachedInventory(name, player) != NULL) { InventoryLocation loc; loc.setDetached(name); diff --git a/src/script/lua_api/l_item.cpp b/src/script/lua_api/l_item.cpp index 46c1c98a0..e41d23fd1 100644 --- a/src/script/lua_api/l_item.cpp +++ b/src/script/lua_api/l_item.cpp @@ -508,7 +508,7 @@ int ModApiItemMod::l_register_item_raw(lua_State *L) std::string name; lua_getfield(L, table, "name"); if(lua_isstring(L, -1)){ - name = lua_tostring(L, -1); + name = readParam<std::string>(L, -1); verbosestream<<"register_item_raw: "<<name<<std::endl; } else { throw LuaError("register_item_raw: name is not defined or not a string"); diff --git a/src/script/lua_api/l_mainmenu.cpp b/src/script/lua_api/l_mainmenu.cpp index 238e3e32f..03b8fe223 100644 --- a/src/script/lua_api/l_mainmenu.cpp +++ b/src/script/lua_api/l_mainmenu.cpp @@ -83,7 +83,7 @@ int ModApiMainMenu::getBoolData(lua_State *L, std::string name,bool& valid) } valid = true; - return lua_toboolean(L, -1); + return readParam<bool>(L, -1); } /******************************************************************************/ @@ -158,7 +158,7 @@ int ModApiMainMenu::l_set_background(lua_State *L) unsigned int minsize = 16; if (!lua_isnone(L, 3)) { - tile_image = lua_toboolean(L, 3); + tile_image = readParam<bool>(L, 3); } if (!lua_isnone(L, 4)) { @@ -195,7 +195,7 @@ int ModApiMainMenu::l_set_clouds(lua_State *L) GUIEngine* engine = getGuiEngine(L); sanity_check(engine != NULL); - bool value = lua_toboolean(L,1); + bool value = readParam<bool>(L,1); engine->m_clouds_enabled = value; @@ -627,7 +627,8 @@ int ModApiMainMenu::l_set_topleft_text(lua_State *L) int ModApiMainMenu::l_get_mapgen_names(lua_State *L) { std::vector<const char *> names; - Mapgen::getMapgenNames(&names, lua_toboolean(L, 1)); + bool include_hidden = lua_isboolean(L, 1) && readParam<bool>(L, 1); + Mapgen::getMapgenNames(&names, include_hidden); lua_newtable(L); for (size_t i = 0; i != names.size(); i++) { @@ -722,7 +723,7 @@ int ModApiMainMenu::l_copy_dir(lua_State *L) if ((!lua_isnone(L,3)) && (!lua_isnil(L,3))) { - keep_source = lua_toboolean(L,3); + keep_source = readParam<bool>(L,3); } std::string absolute_destination = fs::RemoveRelativePathComponents(destination); @@ -871,7 +872,7 @@ int ModApiMainMenu::l_show_path_select_dialog(lua_State *L) const char *formname= luaL_checkstring(L, 1); const char *title = luaL_checkstring(L, 2); - bool is_file_select = lua_toboolean(L, 3); + bool is_file_select = readParam<bool>(L, 3); GUIFileSelectMenu* fileOpenMenu = new GUIFileSelectMenu(RenderingEngine::get_gui_env(), diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index 6fe0d322e..b8e52bd49 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -855,26 +855,26 @@ int ModApiMapgen::l_set_mapgen_params(lua_State *L) lua_getfield(L, 1, "mgname"); if (lua_isstring(L, -1)) - settingsmgr->setMapSetting("mg_name", lua_tostring(L, -1), true); + settingsmgr->setMapSetting("mg_name", readParam<std::string>(L, -1), true); lua_getfield(L, 1, "seed"); if (lua_isnumber(L, -1)) - settingsmgr->setMapSetting("seed", lua_tostring(L, -1), true); + settingsmgr->setMapSetting("seed", readParam<std::string>(L, -1), true); lua_getfield(L, 1, "water_level"); if (lua_isnumber(L, -1)) - settingsmgr->setMapSetting("water_level", lua_tostring(L, -1), true); + settingsmgr->setMapSetting("water_level", readParam<std::string>(L, -1), true); lua_getfield(L, 1, "chunksize"); if (lua_isnumber(L, -1)) - settingsmgr->setMapSetting("chunksize", lua_tostring(L, -1), true); + settingsmgr->setMapSetting("chunksize", readParam<std::string>(L, -1), true); warn_if_field_exists(L, 1, "flagmask", "Deprecated: flags field now includes unset flags."); lua_getfield(L, 1, "flags"); if (lua_isstring(L, -1)) - settingsmgr->setMapSetting("mg_flags", lua_tostring(L, -1), true); + settingsmgr->setMapSetting("mg_flags", readParam<std::string>(L, -1), true); return 0; } @@ -924,7 +924,7 @@ int ModApiMapgen::l_set_mapgen_setting(lua_State *L) const char *name = luaL_checkstring(L, 1); const char *value = luaL_checkstring(L, 2); - bool override_meta = lua_isboolean(L, 3) && lua_toboolean(L, 3); + bool override_meta = readParam<bool>(L, 3, false); if (!settingsmgr->setMapSetting(name, value, override_meta)) { errorstream << "set_mapgen_setting: cannot set '" @@ -953,7 +953,7 @@ int ModApiMapgen::l_set_mapgen_setting_noiseparams(lua_State *L) return 0; } - bool override_meta = lua_isboolean(L, 3) && lua_toboolean(L, 3); + bool override_meta = readParam<bool>(L, 3, false); if (!settingsmgr->setMapSettingNoiseParams(name, &np, override_meta)) { errorstream << "set_mapgen_setting_noiseparams: cannot set '" @@ -979,7 +979,7 @@ int ModApiMapgen::l_set_noiseparams(lua_State *L) return 0; } - bool set_default = !lua_isboolean(L, 3) || lua_toboolean(L, 3); + bool set_default = !lua_isboolean(L, 3) || readParam<bool>(L, 3); g_settings->setNoiseParams(name, np, set_default); @@ -1614,14 +1614,14 @@ int ModApiMapgen::l_place_schematic(lua_State *L) //// Read rotation int rot = ROTATE_0; - const char *enumstr = lua_tostring(L, 3); - if (enumstr) - string_to_enum(es_Rotation, rot, std::string(enumstr)); + std::string enumstr = readParam<std::string>(L, 3, ""); + if (!enumstr.empty()) + string_to_enum(es_Rotation, rot, enumstr); //// Read force placement bool force_placement = true; if (lua_isboolean(L, 5)) - force_placement = lua_toboolean(L, 5); + force_placement = readParam<bool>(L, 5); //// Read node replacements StringMap replace_names; @@ -1662,14 +1662,14 @@ int ModApiMapgen::l_place_schematic_on_vmanip(lua_State *L) //// Read rotation int rot = ROTATE_0; - const char *enumstr = lua_tostring(L, 4); - if (enumstr) + std::string enumstr = readParam<std::string>(L, 4, ""); + if (!enumstr.empty()) string_to_enum(es_Rotation, rot, std::string(enumstr)); //// Read force placement bool force_placement = true; if (lua_isboolean(L, 6)) - force_placement = lua_toboolean(L, 6); + force_placement = readParam<bool>(L, 6); //// Read node replacements StringMap replace_names; @@ -1720,9 +1720,9 @@ int ModApiMapgen::l_serialize_schematic(lua_State *L) //// Read format of definition to save as int schem_format = SCHEM_FMT_MTS; - const char *enumstr = lua_tostring(L, 2); - if (enumstr) - string_to_enum(es_SchematicFormatType, schem_format, std::string(enumstr)); + std::string enumstr = readParam<std::string>(L, 2, ""); + if (!enumstr.empty()) + string_to_enum(es_SchematicFormatType, schem_format, enumstr); //// Serialize to binary string std::ostringstream os(std::ios_base::binary); diff --git a/src/script/lua_api/l_metadata.cpp b/src/script/lua_api/l_metadata.cpp index 59017dbff..4f64cc8a6 100644 --- a/src/script/lua_api/l_metadata.cpp +++ b/src/script/lua_api/l_metadata.cpp @@ -275,7 +275,7 @@ bool MetaDataRef::handleFromTable(lua_State *L, int table, Metadata *meta) lua_pushnil(L); while (lua_next(L, fieldstable) != 0) { // key at index -2 and value at index -1 - std::string name = lua_tostring(L, -2); + std::string name = readParam<std::string>(L, -2); size_t cl; const char *cs = lua_tolstring(L, -1, &cl); meta->setString(name, std::string(cs, cl)); diff --git a/src/script/lua_api/l_nodemeta.cpp b/src/script/lua_api/l_nodemeta.cpp index f390664a4..33b158ae0 100644 --- a/src/script/lua_api/l_nodemeta.cpp +++ b/src/script/lua_api/l_nodemeta.cpp @@ -109,12 +109,12 @@ int NodeMetaRef::l_mark_as_private(lua_State *L) while (lua_next(L, 2) != 0) { // key at index -2 and value at index -1 luaL_checktype(L, -1, LUA_TSTRING); - meta->markPrivate(lua_tostring(L, -1), true); + meta->markPrivate(readParam<std::string>(L, -1), true); // removes value, keeps key for next iteration lua_pop(L, 1); } } else if (lua_isstring(L, 2)) { - meta->markPrivate(lua_tostring(L, 2), true); + meta->markPrivate(readParam<std::string>(L, 2), true); } ref->reportMetadataChange(); diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index f87c02a6d..b6f37d51b 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -156,7 +156,7 @@ int ObjectRef::l_move_to(lua_State *L) // pos v3f pos = checkFloatPos(L, 2); // continuous - bool continuous = lua_toboolean(L, 3); + bool continuous = readParam<bool>(L, 3); // Do it co->moveTo(pos, continuous); return 0; @@ -243,7 +243,8 @@ int ObjectRef::l_set_hp(lua_State *L) lua_pushvalue(L, 3); lua_getfield(L, -1, "type"); - if (lua_isstring(L, -1) && !reason.setTypeFromString(lua_tostring(L, -1))) { + if (lua_isstring(L, -1) && + !reason.setTypeFromString(readParam<std::string>(L, -1))) { errorstream << "Bad type given!" << std::endl; } lua_pop(L, 1); @@ -466,7 +467,7 @@ int ObjectRef::l_set_animation(lua_State *L) frame_blend = lua_tonumber(L, 4); bool frame_loop = true; if (lua_isboolean(L, 5)) - frame_loop = lua_toboolean(L, 5); + frame_loop = readParam<bool>(L, 5); co->setAnimation(frames, frame_speed, frame_blend, frame_loop); return 0; } @@ -609,7 +610,7 @@ int ObjectRef::l_set_bone_position(lua_State *L) // Do it std::string bone; if (!lua_isnil(L, 2)) - bone = lua_tostring(L, 2); + bone = readParam<std::string>(L, 2); v3f position = v3f(0, 0, 0); if (!lua_isnil(L, 3)) position = check_v3f(L, 3); @@ -631,7 +632,7 @@ int ObjectRef::l_get_bone_position(lua_State *L) // Do it std::string bone; if (!lua_isnil(L, 2)) - bone = lua_tostring(L, 2); + bone = readParam<std::string>(L, 2); v3f position = v3f(0, 0, 0); v3f rotation = v3f(0, 0, 0); @@ -668,7 +669,7 @@ int ObjectRef::l_set_attach(lua_State *L) bone = ""; if (!lua_isnil(L, 3)) - bone = lua_tostring(L, 3); + bone = readParam<std::string>(L, 3); position = v3f(0, 0, 0); if (!lua_isnil(L, 4)) position = read_v3f(L, 4); @@ -963,7 +964,7 @@ int ObjectRef::l_set_sprite(lua_State *L) framelength = lua_tonumber(L, 4); bool select_horiz_by_yawpitch = false; if (!lua_isnil(L, 5)) - select_horiz_by_yawpitch = lua_toboolean(L, 5); + select_horiz_by_yawpitch = readParam<bool>(L, 5); co->setSprite(p, num_frames, framelength, select_horiz_by_yawpitch); return 0; } @@ -1536,7 +1537,7 @@ int ObjectRef::l_hud_set_hotbar_image(lua_State *L) if (player == NULL) return 0; - std::string name = lua_tostring(L, 2); + std::string name = readParam<std::string>(L, 2); getServer(L)->hudSetHotbarImage(player, name); return 1; @@ -1565,7 +1566,7 @@ int ObjectRef::l_hud_set_hotbar_selected_image(lua_State *L) if (player == NULL) return 0; - std::string name = lua_tostring(L, 2); + std::string name = readParam<std::string>(L, 2); getServer(L)->hudSetHotbarSelectedImage(player, name); return 1; @@ -1605,7 +1606,7 @@ int ObjectRef::l_set_sky(lua_State *L) while (lua_next(L, 4) != 0) { // key at index -2 and value at index -1 if (lua_isstring(L, -1)) - params.emplace_back(lua_tostring(L, -1)); + params.emplace_back(readParam<std::string>(L, -1)); else params.emplace_back(""); // removes value, keeps key for next iteration @@ -1618,7 +1619,7 @@ int ObjectRef::l_set_sky(lua_State *L) bool clouds = true; if (lua_isboolean(L, 5)) - clouds = lua_toboolean(L, 5); + clouds = readParam<bool>(L, 5); getServer(L)->setSky(player, bgcolor, type, params, clouds); lua_pushboolean(L, true); diff --git a/src/script/lua_api/l_particles.cpp b/src/script/lua_api/l_particles.cpp index 8a7de8b4f..15bcbf3b5 100644 --- a/src/script/lua_api/l_particles.cpp +++ b/src/script/lua_api/l_particles.cpp @@ -66,7 +66,7 @@ int ModApiParticles::l_add_particle(lua_State *L) acc = check_v3f(L, 3); expirationtime = luaL_checknumber(L, 4); size = luaL_checknumber(L, 5); - collisiondetection = lua_toboolean(L, 6); + collisiondetection = readParam<bool>(L, 6); texture = luaL_checkstring(L, 7); if (lua_gettop(L) == 8) // only spawn for a single player playername = luaL_checkstring(L, 8); @@ -177,7 +177,7 @@ int ModApiParticles::l_add_particlespawner(lua_State *L) maxexptime = luaL_checknumber(L, 10); minsize = luaL_checknumber(L, 11); maxsize = luaL_checknumber(L, 12); - collisiondetection = lua_toboolean(L, 13); + collisiondetection = readParam<bool>(L, 13); texture = luaL_checkstring(L, 14); if (lua_gettop(L) == 15) // only spawn for a single player playername = luaL_checkstring(L, 15); diff --git a/src/script/lua_api/l_server.cpp b/src/script/lua_api/l_server.cpp index 7eba79565..6017a5475 100644 --- a/src/script/lua_api/l_server.cpp +++ b/src/script/lua_api/l_server.cpp @@ -33,7 +33,7 @@ int ModApiServer::l_request_shutdown(lua_State *L) { NO_MAP_LOCK_REQUIRED; const char *msg = lua_tolstring(L, 1, NULL); - bool reconnect = lua_toboolean(L, 2); + bool reconnect = readParam<bool>(L, 2); float seconds_before_shutdown = lua_tonumber(L, 3); getServer(L)->requestShutdown(msg ? msg : "", reconnect, seconds_before_shutdown); return 0; @@ -310,15 +310,11 @@ int ModApiServer::l_kick_player(lua_State *L) { NO_MAP_LOCK_REQUIRED; const char *name = luaL_checkstring(L, 1); - std::string message; + std::string message("Kicked"); if (lua_isstring(L, 2)) - { - message = std::string("Kicked: ") + lua_tostring(L, 2); - } + message.append(": ").append(readParam<std::string>(L, 2)); else - { - message = "Kicked."; - } + message.append("."); RemotePlayer *player = dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name); if (player == NULL) { @@ -475,7 +471,7 @@ int ModApiServer::l_notify_authentication_modified(lua_State *L) NO_MAP_LOCK_REQUIRED; std::string name; if(lua_isstring(L, 1)) - name = lua_tostring(L, 1); + name = readParam<std::string>(L, 1); getServer(L)->reportPrivsModified(name); return 0; } @@ -485,8 +481,8 @@ int ModApiServer::l_get_last_run_mod(lua_State *L) { NO_MAP_LOCK_REQUIRED; lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME); - const char *current_mod = lua_tostring(L, -1); - if (current_mod == NULL || current_mod[0] == '\0') { + 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()); } diff --git a/src/script/lua_api/l_settings.cpp b/src/script/lua_api/l_settings.cpp index 1d56aed5f..cc2c73789 100644 --- a/src/script/lua_api/l_settings.cpp +++ b/src/script/lua_api/l_settings.cpp @@ -102,7 +102,7 @@ int LuaSettings::l_get_bool(lua_State* L) } else { // Push default value if (lua_isboolean(L, 3)) - lua_pushboolean(L, lua_toboolean(L, 3)); + lua_pushboolean(L, readParam<bool>(L, 3)); else lua_pushnil(L); } @@ -152,7 +152,7 @@ int LuaSettings::l_set_bool(lua_State* L) LuaSettings* o = checkobject(L, 1); std::string key = std::string(luaL_checkstring(L, 2)); - bool value = lua_toboolean(L, 3); + bool value = readParam<bool>(L, 3); SET_SECURITY_CHECK(L, key); diff --git a/src/script/lua_api/l_sound.cpp b/src/script/lua_api/l_sound.cpp index e7d517ce0..b86eda53e 100644 --- a/src/script/lua_api/l_sound.cpp +++ b/src/script/lua_api/l_sound.cpp @@ -28,7 +28,7 @@ int ModApiSound::l_sound_play(lua_State *L) { SimpleSoundSpec spec; read_soundspec(L, 1, spec); - bool looped = lua_toboolean(L, 2); + bool looped = readParam<bool>(L, 2); s32 handle = getGuiEngine(L)->playSound(spec, looped); diff --git a/src/script/lua_api/l_storage.cpp b/src/script/lua_api/l_storage.cpp index caa1a5551..cba34fb63 100644 --- a/src/script/lua_api/l_storage.cpp +++ b/src/script/lua_api/l_storage.cpp @@ -30,7 +30,7 @@ int ModApiStorage::l_get_mod_storage(lua_State *L) return 0; } - std::string mod_name = lua_tostring(L, -1); + std::string mod_name = readParam<std::string>(L, -1); ModMetadata *store = new ModMetadata(mod_name); if (IGameDef *gamedef = getGameDef(L)) { diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp index b25697611..a58c3a196 100644 --- a/src/script/lua_api/l_util.cpp +++ b/src/script/lua_api/l_util.cpp @@ -135,7 +135,7 @@ int ModApiUtil::l_write_json(lua_State *L) bool styled = false; if (!lua_isnone(L, 2)) { - styled = lua_toboolean(L, 2); + styled = readParam<bool>(L, 2); lua_pop(L, 1); } @@ -231,7 +231,7 @@ int ModApiUtil::l_is_yes(lua_State *L) lua_getglobal(L, "tostring"); // function to be called lua_pushvalue(L, 1); // 1st argument lua_call(L, 1, 1); // execute function - std::string str(lua_tostring(L, -1)); // get result + std::string str = readParam<std::string>(L, -1); // get result lua_pop(L, 1); bool yes = is_yes(str); @@ -342,7 +342,7 @@ int ModApiUtil::l_get_dir_list(lua_State *L) NO_MAP_LOCK_REQUIRED; const char *path = luaL_checkstring(L, 1); bool list_all = !lua_isboolean(L, 2); // if its not a boolean list all - bool list_dirs = lua_toboolean(L, 2); // true: list dirs, false: list files + bool list_dirs = readParam<bool>(L, 2); // true: list dirs, false: list files CHECK_SECURE_PATH(L, path, false); @@ -410,7 +410,7 @@ int ModApiUtil::l_request_insecure_environment(lua_State *L) } // Check secure.trusted_mods - const char *mod_name = lua_tostring(L, -1); + std::string mod_name = readParam<std::string>(L, -1); std::string trusted_mods = g_settings->get("secure.trusted_mods"); trusted_mods.erase(std::remove_if(trusted_mods.begin(), trusted_mods.end(), static_cast<int(*)(int)>(&std::isspace)), @@ -451,7 +451,7 @@ int ModApiUtil::l_sha1(lua_State *L) NO_MAP_LOCK_REQUIRED; size_t size; const char *data = luaL_checklstring(L, 1, &size); - bool hex = !lua_isboolean(L, 2) || !lua_toboolean(L, 2); + bool hex = !lua_isboolean(L, 2) || !readParam<bool>(L, 2); // Compute actual checksum of data std::string data_sha1; diff --git a/src/script/lua_api/l_vmanip.cpp b/src/script/lua_api/l_vmanip.cpp index f6239339c..9eb246f1e 100644 --- a/src/script/lua_api/l_vmanip.cpp +++ b/src/script/lua_api/l_vmanip.cpp @@ -111,7 +111,7 @@ int LuaVoxelManip::l_write_to_map(lua_State *L) MAP_LOCK_REQUIRED; LuaVoxelManip *o = checkobject(L, 1); - bool update_light = !lua_isboolean(L, 2) || lua_toboolean(L, 2); + bool update_light = !lua_isboolean(L, 2) || readParam<bool>(L, 2); GET_ENV_PTR; ServerMap *map = &(env->getServerMap()); if (o->is_mapgen_vm || !update_light) { @@ -197,7 +197,7 @@ int LuaVoxelManip::l_calc_lighting(lua_State *L) v3s16 fpmax = vm->m_area.MaxEdge; v3s16 pmin = lua_istable(L, 2) ? check_v3s16(L, 2) : fpmin + yblock; v3s16 pmax = lua_istable(L, 3) ? check_v3s16(L, 3) : fpmax - yblock; - bool propagate_shadow = !lua_isboolean(L, 4) || lua_toboolean(L, 4); + bool propagate_shadow = !lua_isboolean(L, 4) || readParam<bool>(L, 4); sortBoxVerticies(pmin, pmax); if (!vm->m_area.contains(VoxelArea(pmin, pmax))) |