Browse Source

Split up lua bindings a bit

```lua
local render = devilutionx.render

local function drawGreet ()
  render.string("Hello from " .. _VERSION, 10, 40)
end
Events.OnGameDrawComplete.Add(drawGreet)
```
pull/6748/head
Gleb Mazovetskiy 2 years ago
parent
commit
de6eac137b
  1. 5
      Source/CMakeLists.txt
  2. 2
      Source/debug.cpp
  3. 2
      Source/diablo.cpp
  4. 2
      Source/engine/render/scrollrt.cpp
  5. 80
      Source/lua/lua.cpp
  6. 0
      Source/lua/lua.hpp
  7. 93
      Source/lua/modules/log.cpp
  8. 9
      Source/lua/modules/log.hpp
  9. 16
      Source/lua/modules/render.cpp
  10. 9
      Source/lua/modules/render.hpp
  11. 2
      Source/utils/log.hpp
  12. 2
      test/timedemo_test.cpp

5
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

2
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"

2
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"

2
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

80
Source/utils/lua.cpp → Source/lua/lua.cpp

@ -1,16 +1,14 @@
#include "utils/lua.hpp"
#include "lua/lua.hpp"
#include <optional>
#include <string_view>
#include <fmt/args.h>
#include <fmt/format.h>
#include <sol/sol.hpp>
#include <sol/utility/to_string.hpp>
#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<std::string> 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<fmt::format_context> store;
for (const sol::stack_proxy arg : args) {
switch (arg.get_type()) {
case sol::type::boolean:
store.push_back(arg.as<bool>());
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<std::string>());
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<SDL_LogPriority>(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");

0
Source/utils/lua.hpp → Source/lua/lua.hpp

93
Source/lua/modules/log.cpp

@ -0,0 +1,93 @@
#include "lua/modules/log.hpp"
#include <SDL.h>
#include <fmt/args.h>
#include <fmt/format.h>
#include <sol/sol.hpp>
#include <sol/utility/to_string.hpp>
#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<fmt::format_context> store;
for (const sol::stack_proxy arg : args) {
switch (arg.get_type()) {
case sol::type::boolean:
store.push_back(arg.as<bool>());
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<std::string>());
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<SDL_LogPriority>(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

9
Source/lua/modules/log.hpp

@ -0,0 +1,9 @@
#pragma once
#include <sol/sol.hpp>
namespace devilution {
sol::table LuaLogModule(sol::state_view &lua);
} // namespace devilution

16
Source/lua/modules/render.cpp

@ -0,0 +1,16 @@
#include "lua/modules/render.hpp"
#include <sol/sol.hpp>
#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

9
Source/lua/modules/render.hpp

@ -0,0 +1,9 @@
#pragma once
#include <sol/sol.hpp>
namespace devilution {
sol::table LuaRenderModule(sol::state_view &lua);
} // namespace devilution

2
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 {

2
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;

Loading…
Cancel
Save