Browse Source

Add a lua debug command

Ideally we should have a better UI for this in the future.
pull/6711/head
Gleb Mazovetskiy 2 years ago
parent
commit
493aaf554e
  1. 11
      Source/debug.cpp
  2. 16
      Source/utils/lua.cpp
  3. 3
      Source/utils/lua.hpp

11
Source/debug.cpp

@ -35,6 +35,7 @@
#include "utils/file_util.h"
#include "utils/language.h"
#include "utils/log.hpp"
#include "utils/lua.hpp"
#include "utils/parse_int.hpp"
#include "utils/str_case.hpp"
#include "utils/str_cat.hpp"
@ -152,6 +153,15 @@ std::string DebugCmdHelp(const std::string_view parameter)
return StrCat("Description: ", dbgCmdItem.description, "\nParameters: ", dbgCmdItem.requiredParameter);
}
std::string DebugCmdLua(std::string_view code)
{
tl::expected<std::string, std::string> result = RunLua(code);
if (!result.has_value()) {
return StrCat("> ", code, "\n") + std::move(result).error();
}
return StrCat("> ", code, "\n") + std::move(result).value();
}
std::string DebugCmdGiveGoldCheat(const std::string_view parameter)
{
Player &myPlayer = *MyPlayer;
@ -1231,6 +1241,7 @@ std::vector<DebugCmdItem> DebugCmdList = {
{ "searchitem", "Searches the automap for {item}", "{item}", &DebugCmdSearchItem },
{ "searchobject", "Searches the automap for {object}", "{object}", &DebugCmdSearchObject },
{ "clearsearch", "Search in the auto map is cleared", "", &DebugCmdClearSearch },
{ "lua", "Run Lua code", "{code}", &DebugCmdLua },
};
} // namespace

16
Source/utils/lua.cpp

@ -4,6 +4,7 @@
#include <string_view>
#include <sol/sol.hpp>
#include <sol/utility/to_string.hpp>
#include "engine/assets.hpp"
#include "engine/dx.h"
@ -137,4 +138,19 @@ void LuaEvent(std::string_view name)
CheckResult(fn());
}
tl::expected<std::string, std::string> RunLua(std::string_view code)
{
sol::state &lua = *luaState;
const sol::protected_function_result result = lua.safe_script(code);
const bool valid = result.valid();
if (!valid) {
if (result.get_type() == sol::type::string) {
return tl::make_unexpected(result.get<std::string>());
}
return tl::make_unexpected("Unknown Lua error");
}
return sol::utility::to_string(sol::object(result));
}
} // namespace devilution

3
Source/utils/lua.hpp

@ -2,10 +2,13 @@
#include <string_view>
#include <expected.hpp>
namespace devilution {
void LuaInitialize();
void LuaShutdown();
void LuaEvent(std::string_view name);
tl::expected<std::string, std::string> RunLua(std::string_view code);
} // namespace devilution

Loading…
Cancel
Save