From b9cbef03bafc24b6ecd326c88e374710030eb414 Mon Sep 17 00:00:00 2001 From: remleo Date: Sun, 30 Aug 2026 16:39:29 +0200 Subject: [PATCH 1/2] lua-api: don't bind the variadic log_* fields directly log_error, log_warn and log_info are declared in API.h as C variadic functions. Bound straight through, calling one from Lua with a colon passes self as the first argument, so the struct pointer lands in the format parameter and printf starts reading it as a format string. That is an access violation, not a Lua error, so pcall does not catch it. On a live game uevr.params.functions:log_info("...") killed the process while the script was still loading. The wrappers now take a ready string and pass it as an argument to a %s format, so percent signs in the text are harmless and the colon syntax keeps working. --- lua-api/lib/src/ScriptContext.cpp | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/lua-api/lib/src/ScriptContext.cpp b/lua-api/lib/src/ScriptContext.cpp index eea1ed764..0e32a92b9 100644 --- a/lua-api/lib/src/ScriptContext.cpp +++ b/lua-api/lib/src/ScriptContext.cpp @@ -380,9 +380,25 @@ int ScriptContext::setup_bindings() { ); m_lua.new_usertype("UEVR_PluginFunctions", - "log_error", &UEVR_PluginFunctions::log_error, - "log_warn", &UEVR_PluginFunctions::log_warn, - "log_info", &UEVR_PluginFunctions::log_info, + // Wrapped rather than bound directly, because these are C variadics in API.h: a ':' + // call would leave self in the format parameter and printf would read the struct + // pointer as a format string. Passing a ready string as an argument to "%s" also + // keeps percent signs in the message harmless. + "log_error", [](UEVR_PluginFunctions* self, const char* message) { + if (self != nullptr && message != nullptr) { + self->log_error("%s", message); + } + }, + "log_warn", [](UEVR_PluginFunctions* self, const char* message) { + if (self != nullptr && message != nullptr) { + self->log_warn("%s", message); + } + }, + "log_info", [](UEVR_PluginFunctions* self, const char* message) { + if (self != nullptr && message != nullptr) { + self->log_info("%s", message); + } + }, "is_drawing_ui", &UEVR_PluginFunctions::is_drawing_ui, "get_commit_hash", &UEVR_PluginFunctions::get_commit_hash, From dc27dd4422f43103bee73fcf42dbdd3471cd986c Mon Sep 17 00:00:00 2001 From: remleo Date: Sun, 30 Aug 2026 16:39:59 +0200 Subject: [PATCH 2/2] lua-api: bind print, and add a log table print was never bound, so the stock implementation ran and wrote to a stdout the game does not have. No script output ever reached log.txt, and there was no error either, which is easy to mistake for the script not running at all. ScriptContext::log already writes to OutputDebugStringA, stderr and API::log_info with a safe %s format, it was only used internally. print goes through it now. Arguments are stringified by Lua's own tostring, so any type works and arguments are tab separated, same as stock print. Also adds a global log table with info, warn and error, since the warn and error levels had no safe route from a script. --- lua-api/lib/src/ScriptContext.cpp | 51 +++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/lua-api/lib/src/ScriptContext.cpp b/lua-api/lib/src/ScriptContext.cpp index 0e32a92b9..0294165ea 100644 --- a/lua-api/lib/src/ScriptContext.cpp +++ b/lua-api/lib/src/ScriptContext.cpp @@ -1147,6 +1147,57 @@ int ScriptContext::setup_bindings() { } ); + // Lua's stock print writes to stdout, which the game process does not have, so it needs + // a binding to reach the log at all. print and log.info go through ScriptContext::log, + // log.warn and log.error take the matching API levels. Arguments run through Lua's own + // tostring, so any type works and they are tab separated, same as stock print. + { + auto stringify = [](sol::this_state s, sol::variadic_args args) -> std::string { + sol::state_view lua{s}; + sol::function tostring = lua["tostring"]; + + std::string out{}; + + for (auto arg : args) { + if (!out.empty()) { + out += "\t"; + } + + auto piece = tostring(arg); + + if (piece.valid() && piece.get_type() == sol::type::string) { + out += piece.get(); + } else { + out += ""; + } + } + + return out; + }; + + m_lua["print"] = [stringify](sol::this_state s, sol::variadic_args args) { + ScriptContext::log(stringify(s, args)); + }; + + auto log_table = m_lua.create_table(); + + log_table["info"] = [stringify](sol::this_state s, sol::variadic_args args) { + ScriptContext::log(stringify(s, args)); + }; + + log_table["warn"] = [stringify](sol::this_state s, sol::variadic_args args) { + const auto msg = stringify(s, args); + uevr::API::get()->log_warn("[LuaVR] %s", msg.c_str()); + }; + + log_table["error"] = [stringify](sol::this_state s, sol::variadic_args args) { + const auto msg = stringify(s, args); + uevr::API::get()->log_error("[LuaVR] %s", msg.c_str()); + }; + + m_lua["log"] = log_table; + } + setup_callback_bindings(); auto out = m_lua.create_table();