diff --git a/src/ai/lua/core.cpp b/src/ai/lua/core.cpp index 2380ce1430ac..b9036768c8f4 100644 --- a/src/ai/lua/core.cpp +++ b/src/ai/lua/core.cpp @@ -820,7 +820,7 @@ static int impl_ai_aspect_get(lua_State* L) (void) aspect; } else if(typesafe_aspect* aspect = try_aspect_as(iter->second)) { const unit_advancements_aspect& val = aspect->get(); - int my_side = luaW_getglobal(L, "ai", "side", nullptr) - 1; + int my_side = luaW_getglobal(L, "ai", "side") - 1; lua_newtable(L); for (unit_map::const_iterator u = resources::units->begin(); u != resources::units->end(); ++u) { if (!u.valid() || u->side() != my_side) { diff --git a/src/scripting/game_lua_kernel.cpp b/src/scripting/game_lua_kernel.cpp index 9527819d0fed..a1bf10df12b7 100644 --- a/src/scripting/game_lua_kernel.cpp +++ b/src/scripting/game_lua_kernel.cpp @@ -4075,7 +4075,7 @@ namespace { { lua_State *L = mState; config cfg; - if (!luaW_getglobal(L, "wesnoth", "theme_items", name.c_str(), nullptr)) + if (!luaW_getglobal(L, "wesnoth", "theme_items", name)) return cfg; if (!luaW_pcall(L, 0, 1)) return cfg; luaW_toconfig(L, -1, cfg); @@ -4546,7 +4546,7 @@ game_lua_kernel::game_lua_kernel(CVideo * video, game_state & gs, play_controlle { set_wml_action(handler.first, handler.second); } - luaW_getglobal(L, "wesnoth", "effects", nullptr); + luaW_getglobal(L, "wesnoth", "effects"); BOOST_FOREACH(const std::string& effect, unit::builtin_effects) { lua_pushstring(L, effect.c_str()); push_builtin_effect(); @@ -4678,7 +4678,7 @@ void game_lua_kernel::load_game(const config& level) { lua_State *L = mState; - if (!luaW_getglobal(L, "wesnoth", "game_events", "on_load", nullptr)) + if (!luaW_getglobal(L, "wesnoth", "game_events", "on_load")) return; lua_newtable(L); @@ -4705,7 +4705,7 @@ void game_lua_kernel::save_game(config &cfg) { lua_State *L = mState; - if (!luaW_getglobal(L, "wesnoth", "game_events", "on_save", nullptr)) + if (!luaW_getglobal(L, "wesnoth", "game_events", "on_save")) return; if (!luaW_pcall(L, 0, 1, false)) @@ -4743,7 +4743,7 @@ bool game_lua_kernel::run_event(game_events::queued_event const &ev) { lua_State *L = mState; - if (!luaW_getglobal(L, "wesnoth", "game_events", "on_event", nullptr)) + if (!luaW_getglobal(L, "wesnoth", "game_events", "on_event")) return false; queued_event_context dummy(&ev, queued_events_); @@ -4847,7 +4847,7 @@ bool game_lua_kernel::run_wml_action(std::string const &cmd, vconfig const &cfg, lua_State *L = mState; - if (!luaW_getglobal(L, "wesnoth", "wml_actions", cmd.c_str(), nullptr)) + if (!luaW_getglobal(L, "wesnoth", "wml_actions", cmd)) return false; queued_event_context dummy(&ev, queued_events_); @@ -4868,7 +4868,7 @@ bool game_lua_kernel::run_wml_conditional(std::string const &cmd, vconfig const lua_State *L = mState; - if (!luaW_getglobal(L, "wesnoth", "wml_conditionals", cmd.c_str(), nullptr)) { + if (!luaW_getglobal(L, "wesnoth", "wml_conditionals", cmd)) { std::string err_msg = "unknown conditional wml: ["; err_msg += cmd; err_msg += "]"; @@ -4896,7 +4896,7 @@ bool game_lua_kernel::run_filter(char const *name, unit const &u) if (!ui.valid()) return false; // Get the user filter by name. - if(!luaW_getglobal(L, name, nullptr)) + if(!luaW_getglobal(L, name)) { std::string message = std::string() + "function " + name + " not found"; log_error(message.c_str(), "Lua SUF Error"); @@ -4928,7 +4928,7 @@ std::string game_lua_kernel::apply_effect(const std::string& name, unit& u, cons // (Note: The unit needs to be on the stack twice to prevent untimely GC.) luaW_pushconfig(L, cfg); // Stack: unit, cfg - if(luaW_getglobal(L, "wesnoth", "effects", name.c_str(), nullptr)) { + if(luaW_getglobal(L, "wesnoth", "effects", name)) { map_locker(this); // Stack: unit, cfg, effect if(lua_istable(L, -1)) { @@ -4997,7 +4997,7 @@ void game_lua_kernel::mouse_over_hex_callback(const map_location& loc) { lua_State *L = mState; - if (!luaW_getglobal(L, "wesnoth", "game_events", "on_mouse_move", nullptr)) { + if (!luaW_getglobal(L, "wesnoth", "game_events", "on_mouse_move")) { return; } lua_push(L, loc.x + 1); @@ -5010,7 +5010,7 @@ void game_lua_kernel::select_hex_callback(const map_location& loc) { lua_State *L = mState; - if (!luaW_getglobal(L, "wesnoth", "game_events", "on_mouse_action", nullptr)) { + if (!luaW_getglobal(L, "wesnoth", "game_events", "on_mouse_action")) { return; } lua_push(L, loc.x + 1); diff --git a/src/scripting/lua_common.cpp b/src/scripting/lua_common.cpp index 9f4df5738d9e..b00b7b7501c4 100644 --- a/src/scripting/lua_common.cpp +++ b/src/scripting/lua_common.cpp @@ -790,30 +790,22 @@ vconfig luaW_checkvconfig(lua_State *L, int index, bool allow_missing) return result; } - -#ifdef __GNUC__ -__attribute__((sentinel)) -#endif -bool luaW_getglobal(lua_State *L, ...) +bool luaW_getglobal(lua_State *L, const std::vector& path) { lua_pushglobaltable(L); - va_list ap; - va_start(ap, L); - while (const char *s = va_arg(ap, const char *)) + for (const std::string& s : path) { if (!lua_istable(L, -1)) goto discard; - lua_pushstring(L, s); + lua_pushlstring(L, s.c_str(), s.size()); lua_rawget(L, -2); lua_remove(L, -2); } if (lua_isnil(L, -1)) { discard: - va_end(ap); lua_pop(L, 1); return false; } - va_end(ap); return true; } diff --git a/src/scripting/lua_common.hpp b/src/scripting/lua_common.hpp index 471a717c3b6f..0cf21113e49b 100644 --- a/src/scripting/lua_common.hpp +++ b/src/scripting/lua_common.hpp @@ -28,6 +28,7 @@ class vconfig; #include "scripting/lua_types.hpp" #include "variable_info.hpp" #include "map/location.hpp" +#include namespace lua_common { int intf_textdomain(lua_State *L); @@ -139,7 +140,17 @@ vconfig luaW_checkvconfig(lua_State *L, int index, bool allow_missing = false); * value is not nil. * @return true if an element was pushed. */ -bool luaW_getglobal(lua_State *L, ...); +bool luaW_getglobal(lua_State *L, const std::vector& path); + +/** + * Pushes the value found by following the variadic names (char *), if the + * value is not nil. + * @return true if an element was pushed. + */ +template +bool luaW_getglobal(lua_State *L, T... path) { + return luaW_getglobal(L, std::vector {path...} ); +} bool luaW_toboolean(lua_State *L, int n);