From bb737dc6aac20faf3178eb2ccddd76d1d93cc877 Mon Sep 17 00:00:00 2001 From: harrand Date: Sun, 6 Aug 2023 20:32:58 +0100 Subject: [PATCH] [dbgui] further improved the lua console. in addition, gained access to imgui's InputText function overload for a std::string, which makes life super easy --- lib/CMakeLists.txt | 4 +++- src/tz/dbgui/dbgui.cpp | 13 +++++++------ src/tz/lua/state.cpp | 23 +++++++++++++++++++++-- src/tz/lua/state.hpp | 2 ++ 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index c28b495b7b..3c936ed567 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -13,7 +13,9 @@ add_library(imgui STATIC imgui/imgui_demo.cpp imgui/imgui_draw.cpp imgui/imgui_tables.cpp - imgui/imgui_widgets.cpp) + imgui/imgui_widgets.cpp + + imgui/misc/cpp/imgui_stdlib.cpp) target_include_directories(imgui PUBLIC "${PROJECT_SOURCE_DIR}/lib/imgui") target_compile_definitions(imgui PUBLIC "-DIMGUI_USER_CONFIG=\"../tz_imgui_config.hpp\"") diff --git a/src/tz/dbgui/dbgui.cpp b/src/tz/dbgui/dbgui.cpp index 539c906589..bc0f0f5710 100644 --- a/src/tz/dbgui/dbgui.cpp +++ b/src/tz/dbgui/dbgui.cpp @@ -13,6 +13,7 @@ #include "tz/gl/imported_shaders.hpp" #include "tz/lua/state.hpp" #include "imgui.h" +#include "misc/cpp/imgui_stdlib.h" #if TZ_VULKAN #include "tz/gl/impl/vulkan/detail/tz_vulkan.hpp" @@ -41,7 +42,7 @@ namespace tz::dbgui float frame_period = 0.0f; tz::duration fps_update_duration; - char lua_console_buf[512] = ""; + std::string lua_console_buf = ""; std::string lua_console_history = ""; }; @@ -732,7 +733,7 @@ namespace tz::dbgui void draw_tz_lua_console() { - if(ImGui::Begin("Lua Console", &tab_tz.show_lua_console)) + if(ImGui::Begin("Lua Console", &tab_tz.show_lua_console, ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse)) { ImGui::BeginChild("ScrollingRegion", ImVec2(0, -ImGui::GetTextLineHeightWithSpacing()), false, ImGuiWindowFlags_HorizontalScrollbar); ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(4, 1)); // Adjust item spacing to make it look more like a console @@ -743,17 +744,17 @@ namespace tz::dbgui ImGui::Separator(); ImGuiInputTextFlags input_text_flags = ImGuiInputTextFlags_EnterReturnsTrue; - if(ImGui::InputText("Input", global_platform_data->lua_console_buf, IM_ARRAYSIZE(global_platform_data->lua_console_buf), input_text_flags)) + if(ImGui::InputText("Input", &global_platform_data->lua_console_buf, input_text_flags)) { if(ImGui::IsItemDeactivatedAfterEdit()) // Check if Enter key caused deactivation of the input field { global_platform_data->lua_console_history += std::string("\n>") + global_platform_data->lua_console_buf; - bool success = tz::lua::get_state().execute(global_platform_data->lua_console_buf, false); + bool success = tz::lua::get_state().execute(global_platform_data->lua_console_buf.c_str(), false); if (!success) { - global_platform_data->lua_console_history += "\nLua Error\n"; + global_platform_data->lua_console_history += "\nLua Error: \"" + tz::lua::get_state().get_last_error() + "\"\n"; } - global_platform_data->lua_console_buf[0] = '\0'; + global_platform_data->lua_console_buf.clear(); ImGui::SetKeyboardFocusHere(-1); // Set focus back to the input text field } } diff --git a/src/tz/lua/state.cpp b/src/tz/lua/state.cpp index 167fb09c3a..6caca08e89 100644 --- a/src/tz/lua/state.cpp +++ b/src/tz/lua/state.cpp @@ -28,7 +28,12 @@ namespace tz::lua { auto* s = static_cast(this->lstate); bool ret = luaL_dofile(s, path) == false; - tz::assert(!assert_on_failure || ret, "Lua Error: %s", lua_tostring(s, -1)); + const char* err = lua_tostring(s, -1); + if(err != nullptr) + { + this->last_error = err; + } + tz::assert(!assert_on_failure || ret, "Lua Error: %s", err); return ret; } @@ -36,7 +41,12 @@ namespace tz::lua { auto* s = static_cast(this->lstate); bool ret = luaL_dostring(s, lua_src) == false; - tz::assert(!assert_on_failure || ret, "Lua Error: %s", lua_tostring(s, -1)); + const char* err = lua_tostring(s, -1); + if(err != nullptr) + { + this->last_error = err; + } + tz::assert(!assert_on_failure || ret, "Lua Error: %s", err); return ret; } @@ -224,6 +234,11 @@ namespace tz::lua return ret + "=== end ==="; } + const std::string& state::get_last_error() const + { + return this->last_error; + } + bool state::impl_check_stack(std::size_t sz) const { auto* s = static_cast(this->lstate); @@ -254,6 +269,10 @@ namespace tz::lua lua_Debug ar; lua_getstack(state, 1, &ar); lua_getinfo(state, "nSl", &ar); + if(!b && TZ_DEBUG) + { + tz::dbgui::add_to_lua_log("<>"); + } tz::assert(b, "Lua Assertion Failure: ```lua\n\n%s\n\n```\nOn line %d\nStack:\n%s", ar.source, ar.currentline, stack.c_str()); return 0; } diff --git a/src/tz/lua/state.hpp b/src/tz/lua/state.hpp index a047e18e35..4f4ec92ac2 100644 --- a/src/tz/lua/state.hpp +++ b/src/tz/lua/state.hpp @@ -63,8 +63,10 @@ namespace tz::lua std::optional get_int(const char* varname) const; std::optional get_uint(const char* varname) const; std::string collect_stack() const; + const std::string& get_last_error() const; private: bool impl_check_stack(std::size_t sz) const; + mutable std::string last_error = ""; void* lstate = nullptr; };