diff --git a/Core/LuaContext.cpp b/Core/LuaContext.cpp index 59755a3301..ba27375fb7 100644 --- a/Core/LuaContext.cpp +++ b/Core/LuaContext.cpp @@ -4,73 +4,48 @@ #include "Common/StringUtils.h" #include "Core/LuaContext.h" -// lua_writeline - -/* - -#define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout) -#define lua_writeline() (lua_writestring("\n", 1), fflush(stdout)) -#define lua_writestringerror(s,p) \ - (fprintf(stderr, (s), (p)), fflush(stderr)) - - */ - -extern "C" { -#include "ext/lua/lua.h" -#include "ext/lua/lauxlib.h" -#include "ext/lua/lualib.h" -} +std::string g_stringBuf; // Sol is expensive to include so we only do it here. #include "ext/sol/sol.hpp" LuaContext g_lua; -void LuaContext::Init() { - _dbg_assert_(L == nullptr); +// Custom print function +static void log(const std::string& message) { + INFO_LOG(Log::System, "%s", message.c_str()); + g_stringBuf = message; +} - L = luaL_newstate(); - luaopen_base(L); - luaopen_table(L); - luaopen_string(L); - luaopen_math(L); +void LuaContext::Init() { + + _dbg_assert_(lua_ == nullptr); + lua_.reset(new sol::state()); + lua_->open_libraries(sol::lib::base); + lua_->open_libraries(sol::lib::table); + lua_->open_libraries(sol::lib::bit32); + lua_->open_libraries(sol::lib::string); + lua_->open_libraries(sol::lib::math); + + // Not sure if we can safely override print(). So making a new function. + lua_->set("log", &log); } void LuaContext::Shutdown() { - lua_close(L); - L = nullptr; + lua_.reset(); } void LuaContext::Load(const char *code) { - /* - while (fgets(buff, sizeof(buff), stdin) != NULL) { - error = luaL_loadbuffer(L, buff, strlen(buff), "line") || - lua_pcall(L, 0, 0, 0); - if (error) { - fprintf(stderr, "%s", lua_tostring(L, -1)); - lua_pop(L, 1); // pop error message from the stack - } - }*/ + } void LuaContext::Execute(std::string_view cmd, std::string *output) { - int error = luaL_loadbuffer(L, cmd.data(), cmd.length(), "line"); - if (error) { - ERROR_LOG(Log::System, "%s", lua_tostring(L, -1)); - *output = lua_tostring(L, -1); - lua_pop(L, 1); /* pop error message from the stack */ - return; + try { + lua_->script(cmd); + *output = g_stringBuf; + g_stringBuf.clear(); + } catch (sol::error e) { + ERROR_LOG(Log::System, "Exception: %s", e.what()); + *output = e.what(); } - - lua_pcall(L, 0, 0, 0); - - /* Get the number of values which have been pushed */ - int res_count = lua_tointeger(L, -1); - if (!lua_isnumber(L, -1)) - *output = "function `f' must return a number"; - int value = lua_tonumber(L, -1); - /* Remove the number of values */ - lua_pop(L, 1); - - *output = StringFromFormat("%d", value); } diff --git a/Core/LuaContext.h b/Core/LuaContext.h index be2032cb75..eaf59e80ed 100644 --- a/Core/LuaContext.h +++ b/Core/LuaContext.h @@ -2,6 +2,9 @@ #include #include +#include + +#include "ext/sol/forward.hpp" struct lua_State; @@ -15,6 +18,8 @@ public: void Execute(std::string_view cmd, std::string *output); private: + std::unique_ptr lua_; + // Naming it L is a common convention. lua_State *L = nullptr; }; diff --git a/UI/ImDebugger/ImConsole.cpp b/UI/ImDebugger/ImConsole.cpp index 6924a88736..321dc93492 100644 --- a/UI/ImDebugger/ImConsole.cpp +++ b/UI/ImDebugger/ImConsole.cpp @@ -17,7 +17,6 @@ ImConsole::ImConsole() { Commands.push_back("HELP"); Commands.push_back("HISTORY"); Commands.push_back("CLEAR"); - Commands.push_back("CLASSIFY"); AutoScroll = true; ScrollToBottom = false; AddLog("Welcome to Dear ImGui!"); @@ -27,6 +26,7 @@ ImConsole::~ImConsole() { ClearLog(); for (int i = 0; i < History.Size; i++) ImGui::MemFree(History[i]); + AddLog("# Enter 'HELP' for help."); } // Portable helpers @@ -90,7 +90,7 @@ void ImConsole::Draw(ImConfig &cfg) { } // Options, Filter - ImGui::SetNextItemShortcut(ImGuiMod_Ctrl | ImGuiKey_O, ImGuiInputFlags_Tooltip); + //ImGui::SetNextItemShortcut(ImGuiMod_Ctrl | ImGuiKey_O, ImGuiInputFlags_Tooltip); if (ImGui::Button("Options")) ImGui::OpenPopup("Options"); ImGui::SameLine(); diff --git a/ext/imgui/imgui_impl_platform.cpp b/ext/imgui/imgui_impl_platform.cpp index e20b629758..3beea4cfb7 100644 --- a/ext/imgui/imgui_impl_platform.cpp +++ b/ext/imgui/imgui_impl_platform.cpp @@ -189,6 +189,7 @@ ImGuiKey KeyCodeToImGui(InputKeyCode keyCode) { case NKCODE_COMMA: return ImGuiKey_Comma; case NKCODE_PERIOD: return ImGuiKey_Period; case NKCODE_MINUS: return ImGuiKey_Minus; + case NKCODE_PLUS: return ImGuiKey_Equal; // Hmm case NKCODE_EQUALS: return ImGuiKey_Equal; case NKCODE_LEFT_BRACKET: return ImGuiKey_LeftBracket; case NKCODE_RIGHT_BRACKET: return ImGuiKey_RightBracket;