diff --git a/Source/CMakeLists.txt b/Source/CMakeLists.txt index 474663594..51ae44cd9 100644 --- a/Source/CMakeLists.txt +++ b/Source/CMakeLists.txt @@ -132,6 +132,10 @@ set(libdevilutionx_SRCS levels/town.cpp levels/trigs.cpp + lua/lua.cpp + lua/modules/render.cpp + lua/modules/log.cpp + panels/charpanel.cpp panels/info_box.cpp panels/mainpanel.cpp @@ -160,7 +164,6 @@ set(libdevilutionx_SRCS utils/format_int.cpp utils/language.cpp utils/logged_fstream.cpp - utils/lua.cpp utils/paths.cpp utils/parse_int.cpp utils/pcx_to_clx.cpp diff --git a/Source/debug.cpp b/Source/debug.cpp index cbfcc1f2a..d10468746 100644 --- a/Source/debug.cpp +++ b/Source/debug.cpp @@ -23,6 +23,7 @@ #include "inv.h" #include "levels/setmaps.h" #include "lighting.h" +#include "lua/lua.hpp" #include "monstdat.h" #include "monster.h" #include "pack.h" @@ -35,7 +36,6 @@ #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" diff --git a/Source/diablo.cpp b/Source/diablo.cpp index de9baf1e1..5bb9ea089 100644 --- a/Source/diablo.cpp +++ b/Source/diablo.cpp @@ -52,6 +52,7 @@ #include "levels/trigs.h" #include "lighting.h" #include "loadsave.h" +#include "lua/lua.hpp" #include "menu.h" #include "minitext.h" #include "missiles.h" @@ -81,7 +82,6 @@ #include "utils/console.h" #include "utils/display.h" #include "utils/language.h" -#include "utils/lua.hpp" #include "utils/parse_int.hpp" #include "utils/paths.h" #include "utils/screen_reader.hpp" diff --git a/Source/engine/render/scrollrt.cpp b/Source/engine/render/scrollrt.cpp index 695858d06..2f720b93f 100644 --- a/Source/engine/render/scrollrt.cpp +++ b/Source/engine/render/scrollrt.cpp @@ -28,6 +28,7 @@ #include "init.h" #include "inv.h" #include "lighting.h" +#include "lua/lua.hpp" #include "minitext.h" #include "missiles.h" #include "nthread.h" @@ -46,7 +47,6 @@ #include "utils/display.h" #include "utils/endian.hpp" #include "utils/log.hpp" -#include "utils/lua.hpp" #include "utils/str_cat.hpp" #ifndef USE_SDL1 diff --git a/Source/utils/lua.cpp b/Source/lua/lua.cpp similarity index 57% rename from Source/utils/lua.cpp rename to Source/lua/lua.cpp index ddc96bd4a..93a84ed44 100644 --- a/Source/utils/lua.cpp +++ b/Source/lua/lua.cpp @@ -1,16 +1,14 @@ -#include "utils/lua.hpp" +#include "lua/lua.hpp" #include #include -#include -#include #include #include #include "engine/assets.hpp" -#include "engine/dx.h" -#include "engine/render/text_render.hpp" +#include "lua/modules/log.hpp" +#include "lua/modules/render.hpp" #include "plrmsg.h" #include "utils/console.h" #include "utils/log.hpp" @@ -75,68 +73,6 @@ void LuaPanic(sol::optional message) message.value_or("unknown error")); } -void LuaLogMessage(LogPriority priority, std::string_view fmt, sol::variadic_args args) -{ - std::string formatted; - FMT_TRY - { - fmt::dynamic_format_arg_store store; - for (const sol::stack_proxy arg : args) { - switch (arg.get_type()) { - case sol::type::boolean: - store.push_back(arg.as()); - break; - case sol::type::number: - if (lua_isinteger(arg.lua_state(), arg.stack_index())) { - store.push_back(lua_tointeger(arg.lua_state(), arg.stack_index())); - } else { - store.push_back(lua_tonumber(arg.lua_state(), arg.stack_index())); - } - break; - case sol::type::string: - store.push_back(arg.as()); - break; - default: - store.push_back(sol::utility::to_string(sol::stack_object(arg))); - break; - } - } - formatted = fmt::vformat(fmt, store); - } - FMT_CATCH(const fmt::format_error &e) - { -#if FMT_EXCEPTIONS - // e.what() is undefined if exceptions are disabled, so we wrap the whole block - // with an `FMT_EXCEPTIONS` check. - std::string error = StrCat("Format error, fmt: ", fmt, " error: ", e.what()); - SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, "%s", error.c_str()); - return; -#endif - } - SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, static_cast(priority), "%s", formatted.c_str()); -} - -void LuaLog(std::string_view fmt, sol::variadic_args args) -{ - LuaLogMessage(LogPriority::Info, fmt, std::move(args)); -} -void LuaLogVerbose(std::string_view fmt, sol::variadic_args args) -{ - LuaLogMessage(LogPriority::Verbose, fmt, std::move(args)); -} -void LuaLogDebug(std::string_view fmt, sol::variadic_args args) -{ - LuaLogMessage(LogPriority::Debug, fmt, std::move(args)); -} -void LuaLogWarn(std::string_view fmt, sol::variadic_args args) -{ - LuaLogMessage(LogPriority::Warn, fmt, std::move(args)); -} -void LuaLogError(std::string_view fmt, sol::variadic_args args) -{ - LuaLogMessage(LogPriority::Error, fmt, std::move(args)); -} - } // namespace void Sol2DebugPrintStack(lua_State *state) @@ -174,13 +110,9 @@ void LuaInitialize() // Registering devilutionx object table lua.create_named_table( "devilutionx", - "message", [](std::string_view text) { EventPlrMsg(text, UiFlags::ColorRed); }, - "drawString", [](std::string_view text, int x, int y) { DrawString(GlobalBackBuffer(), text, { x, y }); }, - "log", LuaLog, - "logVerbose", LuaLogVerbose, - "logDebug", LuaLogDebug, - "logWarn", LuaLogWarn, - "logError", LuaLogError); + "log", LuaLogModule(lua), + "render", LuaRenderModule(lua), + "message", [](std::string_view text) { EventPlrMsg(text, UiFlags::ColorRed); }); RunScript("lua/init.lua"); RunScript("lua/user.lua"); diff --git a/Source/utils/lua.hpp b/Source/lua/lua.hpp similarity index 100% rename from Source/utils/lua.hpp rename to Source/lua/lua.hpp diff --git a/Source/lua/modules/log.cpp b/Source/lua/modules/log.cpp new file mode 100644 index 000000000..414db639e --- /dev/null +++ b/Source/lua/modules/log.cpp @@ -0,0 +1,93 @@ +#include "lua/modules/log.hpp" + +#include + +#include +#include +#include +#include + +#include "utils/log.hpp" + +#ifdef USE_SDL1 +#include "utils/sdl2_to_1_2_backports.h" +#endif + +namespace devilution { + +namespace { + +void LuaLogMessage(LogPriority priority, std::string_view fmt, sol::variadic_args args) +{ + std::string formatted; + FMT_TRY + { + fmt::dynamic_format_arg_store store; + for (const sol::stack_proxy arg : args) { + switch (arg.get_type()) { + case sol::type::boolean: + store.push_back(arg.as()); + break; + case sol::type::number: + if (lua_isinteger(arg.lua_state(), arg.stack_index())) { + store.push_back(lua_tointeger(arg.lua_state(), arg.stack_index())); + } else { + store.push_back(lua_tonumber(arg.lua_state(), arg.stack_index())); + } + break; + case sol::type::string: + store.push_back(arg.as()); + break; + default: + store.push_back(sol::utility::to_string(sol::stack_object(arg))); + break; + } + } + formatted = fmt::vformat(fmt, store); + } + FMT_CATCH(const fmt::format_error &e) + { +#if FMT_EXCEPTIONS + // e.what() is undefined if exceptions are disabled, so we wrap the whole block + // with an `FMT_EXCEPTIONS` check. + std::string error = StrCat("Format error, fmt: ", fmt, " error: ", e.what()); + SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, "%s", error.c_str()); + return; +#endif + } + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, static_cast(priority), "%s", formatted.c_str()); +} + +void LuaLogInfo(std::string_view fmt, sol::variadic_args args) +{ + LuaLogMessage(LogPriority::Info, fmt, std::move(args)); +} +void LuaLogVerbose(std::string_view fmt, sol::variadic_args args) +{ + LuaLogMessage(LogPriority::Verbose, fmt, std::move(args)); +} +void LuaLogDebug(std::string_view fmt, sol::variadic_args args) +{ + LuaLogMessage(LogPriority::Debug, fmt, std::move(args)); +} +void LuaLogWarn(std::string_view fmt, sol::variadic_args args) +{ + LuaLogMessage(LogPriority::Warn, fmt, std::move(args)); +} +void LuaLogError(std::string_view fmt, sol::variadic_args args) +{ + LuaLogMessage(LogPriority::Error, fmt, std::move(args)); +} +} // namespace + +sol::table LuaLogModule(sol::state_view &lua) +{ + return lua.create_table_with( + "info", LuaLogInfo, + "verbose", LuaLogVerbose, + "debug", LuaLogDebug, + "warn", LuaLogWarn, + "error", LuaLogError); +} + +} // namespace devilution diff --git a/Source/lua/modules/log.hpp b/Source/lua/modules/log.hpp new file mode 100644 index 000000000..5791e0ddc --- /dev/null +++ b/Source/lua/modules/log.hpp @@ -0,0 +1,9 @@ +#pragma once + +#include + +namespace devilution { + +sol::table LuaLogModule(sol::state_view &lua); + +} // namespace devilution diff --git a/Source/lua/modules/render.cpp b/Source/lua/modules/render.cpp new file mode 100644 index 000000000..ebe375357 --- /dev/null +++ b/Source/lua/modules/render.cpp @@ -0,0 +1,16 @@ +#include "lua/modules/render.hpp" + +#include + +#include "engine/dx.h" +#include "engine/render/text_render.hpp" + +namespace devilution { + +sol::table LuaRenderModule(sol::state_view &lua) +{ + return lua.create_table_with( + "string", [](std::string_view text, int x, int y) { DrawString(GlobalBackBuffer(), text, { x, y }); }); +} + +} // namespace devilution diff --git a/Source/lua/modules/render.hpp b/Source/lua/modules/render.hpp new file mode 100644 index 000000000..23a323a67 --- /dev/null +++ b/Source/lua/modules/render.hpp @@ -0,0 +1,9 @@ +#pragma once + +#include + +namespace devilution { + +sol::table LuaRenderModule(sol::state_view &lua); + +} // namespace devilution diff --git a/Source/utils/log.hpp b/Source/utils/log.hpp index 2931728ba..976424348 100644 --- a/Source/utils/log.hpp +++ b/Source/utils/log.hpp @@ -9,7 +9,7 @@ #include "utils/str_cat.hpp" #ifdef USE_SDL1 -#include "sdl2_to_1_2_backports.h" +#include "utils/sdl2_to_1_2_backports.h" #endif namespace devilution { diff --git a/test/timedemo_test.cpp b/test/timedemo_test.cpp index ca1ddfdd6..49084ee49 100644 --- a/test/timedemo_test.cpp +++ b/test/timedemo_test.cpp @@ -4,11 +4,11 @@ #include "diablo.h" #include "engine/demomode.h" +#include "lua/lua.hpp" #include "options.h" #include "pfile.h" #include "playerdat.hpp" #include "utils/display.h" -#include "utils/lua.hpp" #include "utils/paths.h" using namespace devilution;