From de9c9250c647f1019902c20ee4ff730455dfcb46 Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Sun, 15 Oct 2023 17:21:36 +0100 Subject: [PATCH] Lua improvements 1. A conformant `print`. 2. `drawString`. 3. `OnGameDrawComplete` event for drawing things on screen. --- .editorconfig | 5 ++++ 3rdParty/sol2/CMakeLists.txt | 5 +++- Packaging/resources/assets/lua/init.lua | 1 + Source/engine/render/scrollrt.cpp | 3 ++ Source/utils/lua.cpp | 39 +++++++++++-------------- 5 files changed, 30 insertions(+), 23 deletions(-) diff --git a/.editorconfig b/.editorconfig index 7ab1cba76384..4278da51c221 100644 --- a/.editorconfig +++ b/.editorconfig @@ -17,6 +17,11 @@ end_of_line = lf [*.po] end_of_line = lf +[*.lua] +indent_style = space +indent_size = 2 +end_of_line = lf + [*.py] indent_style = space indent_size = 4 diff --git a/3rdParty/sol2/CMakeLists.txt b/3rdParty/sol2/CMakeLists.txt index 390dd7c01c80..154257e24f17 100644 --- a/3rdParty/sol2/CMakeLists.txt +++ b/3rdParty/sol2/CMakeLists.txt @@ -10,4 +10,7 @@ FetchContent_Declare(sol2 FetchContent_MakeAvailableExcludeFromAll(sol2) target_include_directories(sol2 SYSTEM BEFORE INTERFACE ${CMAKE_CURRENT_LIST_DIR}/sol_config) -target_compile_definitions(sol2 INTERFACE -DSOL_NO_EXCEPTIONS) +target_compile_definitions(sol2 INTERFACE SOL_NO_EXCEPTIONS=1) +if(AMIGA) + target_compile_definitions(sol2 INTERFACE SOL_NO_CHECK_NUMBER_PRECISION=1) +endif() diff --git a/Packaging/resources/assets/lua/init.lua b/Packaging/resources/assets/lua/init.lua index 4997ebd2201b..ee06d9c9baa2 100644 --- a/Packaging/resources/assets/lua/init.lua +++ b/Packaging/resources/assets/lua/init.lua @@ -24,3 +24,4 @@ end Events:RegisterEvent("OnGameBoot") Events:RegisterEvent("OnGameStart") +Events:RegisterEvent("OnGameDrawComplete") diff --git a/Source/engine/render/scrollrt.cpp b/Source/engine/render/scrollrt.cpp index cfc52f05ec61..280fe51906b5 100644 --- a/Source/engine/render/scrollrt.cpp +++ b/Source/engine/render/scrollrt.cpp @@ -46,6 +46,7 @@ #include "utils/display.h" #include "utils/endian.hpp" #include "utils/log.hpp" +#include "utils/lua.hpp" #include "utils/str_cat.hpp" #ifndef USE_SDL1 @@ -1654,6 +1655,8 @@ void DrawAndBlit() DrawFPS(out); + LuaEvent("OnGameDrawComplete"); + DrawMain(out, hgt, drawInfoBox, drawHealth, drawMana, drawBelt, drawControlButtons); RedrawComplete(); diff --git a/Source/utils/lua.cpp b/Source/utils/lua.cpp index f595af64d8bc..6379b76c3680 100644 --- a/Source/utils/lua.cpp +++ b/Source/utils/lua.cpp @@ -6,6 +6,8 @@ #include #include "engine/assets.hpp" +#include "engine/dx.h" +#include "engine/render/text_render.hpp" #include "plrmsg.h" #include "utils/console.h" #include "utils/log.hpp" @@ -18,24 +20,16 @@ std::optional luaState; int LuaPrint(lua_State *state) { - int nargs = lua_gettop(state); - if (nargs >= 1 && lua_isstring(state, 1)) { - std::string msg = lua_tostring(state, 1); - msg += "\n"; - printInConsole(msg); + const int n = lua_gettop(state); + for (int i = 1; i <= n; i++) { + size_t l; + const char *s = luaL_tolstring(state, i, &l); + if (i > 1) + printInConsole("\t"); + printInConsole(std::string_view(s, l)); + lua_pop(state, 1); } - - return 0; -} - -int LuaPlayerMessage(lua_State *state) -{ - int nargs = lua_gettop(state); - if (nargs >= 1 && lua_isstring(state, 1)) { - std::string_view msg = lua_tostring(state, 1); - EventPlrMsg(msg, UiFlags::ColorRed); - } - + printNewlineInConsole(); return 0; } @@ -80,14 +74,14 @@ void LuaPanic(sol::optional maybe_msg) } // namespace -void Sol2DebugPrintStack(lua_State *L) +void Sol2DebugPrintStack(lua_State *state) { - LogDebug("{}", sol::detail::debug::dump_types(L)); + LogDebug("{}", sol::detail::debug::dump_types(state)); } -void Sol2DebugPrintSection(const std::string &message, lua_State *L) +void Sol2DebugPrintSection(const std::string &message, lua_State *state) { - LogDebug("-- {} -- [ {} ]", message, sol::detail::debug::dump_types(L)); + LogDebug("-- {} -- [ {} ]", message, sol::detail::debug::dump_types(state)); } void LuaInitialize() @@ -115,7 +109,8 @@ void LuaInitialize() // Registering devilutionx object table lua.create_named_table( "devilutionx", - "message", LuaPlayerMessage); + "message", [](std::string_view text) { EventPlrMsg(text, UiFlags::ColorRed); }, + "drawString", [](std::string_view text, int x, int y) { DrawString(GlobalBackBuffer(), text, { x, y }); }); RunScript("lua/init.lua"); RunScript("lua/user.lua");