From aa1c41ea0eeb9af79c8a127ce32496e7f6e288aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Thu, 7 Nov 2024 19:55:39 +0100 Subject: [PATCH] Console wip --- Core/LuaContext.cpp | 79 +++++++++++++------------------------ Core/LuaContext.h | 5 +++ UI/ImDebugger/ImConsole.cpp | 9 +---- 3 files changed, 34 insertions(+), 59 deletions(-) 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 9904f2e508..7550d0cbda 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 @@ -74,11 +74,6 @@ void ImConsole::Draw(bool* p_open) { ImGui::EndPopup(); } - ImGui::TextWrapped( - "This example implements a console with basic coloring, completion (TAB key) and history (Up/Down keys). A more elaborate " - "implementation may want to store entries along with extra data such as timestamp, emitter, etc."); - ImGui::TextWrapped("Enter 'HELP' for help."); - // TODO: display items starting from the bottom if (ImGui::SmallButton("Add Debug Text")) { @@ -105,7 +100,7 @@ void ImConsole::Draw(bool* p_open) { } // 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();