Skip to content

Commit dc27dd4

Browse files
committed
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.
1 parent b9cbef0 commit dc27dd4

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

‎lua-api/lib/src/ScriptContext.cpp‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,57 @@ int ScriptContext::setup_bindings() {
11471147
}
11481148
);
11491149

1150+
// Lua's stock print writes to stdout, which the game process does not have, so it needs
1151+
// a binding to reach the log at all. print and log.info go through ScriptContext::log,
1152+
// log.warn and log.error take the matching API levels. Arguments run through Lua's own
1153+
// tostring, so any type works and they are tab separated, same as stock print.
1154+
{
1155+
auto stringify = [](sol::this_state s, sol::variadic_args args) -> std::string {
1156+
sol::state_view lua{s};
1157+
sol::function tostring = lua["tostring"];
1158+
1159+
std::string out{};
1160+
1161+
for (auto arg : args) {
1162+
if (!out.empty()) {
1163+
out += "\t";
1164+
}
1165+
1166+
auto piece = tostring(arg);
1167+
1168+
if (piece.valid() && piece.get_type() == sol::type::string) {
1169+
out += piece.get<std::string>();
1170+
} else {
1171+
out += "<?>";
1172+
}
1173+
}
1174+
1175+
return out;
1176+
};
1177+
1178+
m_lua["print"] = [stringify](sol::this_state s, sol::variadic_args args) {
1179+
ScriptContext::log(stringify(s, args));
1180+
};
1181+
1182+
auto log_table = m_lua.create_table();
1183+
1184+
log_table["info"] = [stringify](sol::this_state s, sol::variadic_args args) {
1185+
ScriptContext::log(stringify(s, args));
1186+
};
1187+
1188+
log_table["warn"] = [stringify](sol::this_state s, sol::variadic_args args) {
1189+
const auto msg = stringify(s, args);
1190+
uevr::API::get()->log_warn("[LuaVR] %s", msg.c_str());
1191+
};
1192+
1193+
log_table["error"] = [stringify](sol::this_state s, sol::variadic_args args) {
1194+
const auto msg = stringify(s, args);
1195+
uevr::API::get()->log_error("[LuaVR] %s", msg.c_str());
1196+
};
1197+
1198+
m_lua["log"] = log_table;
1199+
}
1200+
11501201
setup_callback_bindings();
11511202

11521203
auto out = m_lua.create_table();

0 commit comments

Comments
 (0)