diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index af318f305..11cefcc06 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -2230,11 +2230,34 @@ static int internal_getDir(lua_State *L) static int internal_runCommand(lua_State *L) { - luaL_checktype(L, 1, LUA_TSTRING); - CoreSuspender suspend; - std::string command = lua_tostring(L, 1); buffered_color_ostream out; - command_result res = Core::getInstance().runCommand(out, command); + command_result res; + if (lua_gettop(L) == 0) + { + lua_pushstring(L, ""); + } + if (lua_type(L, 1) == LUA_TTABLE) + { + std::string command = ""; + std::vector args; + lua_pushnil(L); // first key + while (lua_next(L, 1) != 0) + { + if (command == "") + command = lua_tostring(L, -1); + else + args.push_back(lua_tostring(L, -1)); + lua_pop(L, 1); // remove value, leave key + } + CoreSuspender suspend; + res = Core::getInstance().runCommand(out, command, args); + } + else + { + std::string command = lua_tostring(L, 1); + CoreSuspender suspend; + res = Core::getInstance().runCommand(out, command); + } auto fragments = out.fragments(); lua_newtable(L); lua_pushinteger(L, (int)res); diff --git a/library/lua/dfhack.lua b/library/lua/dfhack.lua index 970d50a4a..3cc98e882 100644 --- a/library/lua/dfhack.lua +++ b/library/lua/dfhack.lua @@ -369,9 +369,17 @@ end function dfhack.run_command(...) args = {...} if type(args[1]) == 'table' then - command = table.concat(args[1], ' ') + command = args[1] + elseif #args > 1 and type(args[2]) == 'table' then + -- {args[1]} + args[2] + command = args[2] + table.insert(command, 1, args[1]) + elseif #args == 1 and type(args[1]) == 'string' then + command = args[1] + elseif #args > 1 and type(args[1]) == 'string' then + command = args else - command = table.concat(args, ' ') + error('Invalid arguments') end result = internal.runCommand(command) output = ""