From 94c6bc8063b8d69c9ebd9014aaade9a8b7dc788a Mon Sep 17 00:00:00 2001 From: myk002 Date: Wed, 2 Nov 2022 13:50:52 -0700 Subject: [PATCH] refactor generic lua function caller to Lua ns --- library/LuaTools.cpp | 23 +++++++++++++++++++++++ library/include/LuaTools.h | 15 +++++++++++++++ plugins/overlay.cpp | 34 +++++++--------------------------- 3 files changed, 45 insertions(+), 27 deletions(-) diff --git a/library/LuaTools.cpp b/library/LuaTools.cpp index 3cd6a1f7d..8d317c076 100644 --- a/library/LuaTools.cpp +++ b/library/LuaTools.cpp @@ -820,6 +820,29 @@ bool DFHack::Lua::SafeCall(color_ostream &out, lua_State *L, int nargs, int nres return ok; } +bool DFHack::Lua::CallLuaModuleFunction(color_ostream &out, lua_State *L, + const char *module_name, const char *fn_name, + int nargs, int nres, LuaLambda && args_lambda, LuaLambda && res_lambda, + bool perr){ + if (!lua_checkstack(L, 1 + nargs) || + !Lua::PushModulePublic(out, L, module_name, fn_name)) { + if (perr) + out.printerr("Failed to load %s Lua code\n", module_name); + return false; + } + + std::forward(args_lambda)(L); + + if (!Lua::SafeCall(out, L, nargs, nres, perr)) { + if (perr) + out.printerr("Failed Lua call to '%s.%s'\n", module_name, fn_name); + return false; + } + + std::forward(res_lambda)(L); + return true; +} + // Copied from lcorolib.c, with error handling modifications static int resume_helper(lua_State *L, lua_State *co, int narg, int nres) { diff --git a/library/include/LuaTools.h b/library/include/LuaTools.h index e4245f09a..9e1901f03 100644 --- a/library/include/LuaTools.h +++ b/library/include/LuaTools.h @@ -24,6 +24,7 @@ distribution. #pragma once +#include #include #include #include @@ -218,6 +219,20 @@ namespace DFHack {namespace Lua { */ DFHACK_EXPORT bool SafeCall(color_ostream &out, lua_State *state, int nargs, int nres, bool perr = true); + /** + * Load named module and function and invoke it via SafeCall. Returns true + * on success. If an error is signalled, and perr is true, it is printed and + * popped from the stack. + */ + typedef std::function LuaLambda; + static auto DEFAULT_LUA_LAMBDA = [](lua_State *){}; + DFHACK_EXPORT bool CallLuaModuleFunction(color_ostream &out, + lua_State *state, const char *module_name, const char *fn_name, + int nargs = 0, int nres = 0, + LuaLambda && args_lambda = DEFAULT_LUA_LAMBDA, + LuaLambda && res_lambda = DEFAULT_LUA_LAMBDA, + bool perr = true); + /** * Pops a function from the top of the stack, and pushes a new coroutine. */ diff --git a/plugins/overlay.cpp b/plugins/overlay.cpp index c6cc0ef20..509fa5597 100644 --- a/plugins/overlay.cpp +++ b/plugins/overlay.cpp @@ -102,9 +102,10 @@ namespace DFHack { static df::coord2d screenSize; -template -static void call_overlay_lua(color_ostream *out, const char *fn_name, int nargs, - int nres, FA && args_lambda, FR && res_lambda) { +static void call_overlay_lua(color_ostream *out, const char *fn_name, + int nargs = 0, int nres = 0, + Lua::LuaLambda && args_lambda = Lua::DEFAULT_LUA_LAMBDA, + Lua::LuaLambda && res_lambda = Lua::DEFAULT_LUA_LAMBDA) { DEBUG(event).print("calling overlay lua function: '%s'\n", fn_name); CoreSuspender guard; @@ -115,30 +116,9 @@ static void call_overlay_lua(color_ostream *out, const char *fn_name, int nargs, if (!out) out = &Core::getInstance().getConsole(); - if (!lua_checkstack(L, 1 + nargs) || - !Lua::PushModulePublic( - *out, L, "plugins.overlay", fn_name)) { - out->printerr("Failed to load overlay Lua code\n"); - return; - } - - std::forward(args_lambda)(L); - - if (!Lua::SafeCall(*out, L, nargs, nres)) - out->printerr("Failed Lua call to '%s'\n", fn_name); - - std::forward(res_lambda)(L); -} - -static auto DEFAULT_LAMBDA = [](lua_State *){}; -template -static void call_overlay_lua(color_ostream *out, const char *fn_name, int nargs, - int nres, FA && args_lambda) { - call_overlay_lua(out, fn_name, nargs, nres, args_lambda, DEFAULT_LAMBDA); -} - -static void call_overlay_lua(color_ostream *out, const char *fn_name) { - call_overlay_lua(out, fn_name, 0, 0, DEFAULT_LAMBDA, DEFAULT_LAMBDA); + Lua::CallLuaModuleFunction(*out, L, "plugins.overlay", fn_name, nargs, nres, + std::forward(args_lambda), + std::forward(res_lambda)); } template