From b60164b8c1e25ceb77e072f06836e791c1952e63 Mon Sep 17 00:00:00 2001 From: Celtic Minstrel Date: Mon, 15 Feb 2021 15:19:23 -0500 Subject: [PATCH] Utilize the new Lua warning system to push deprecation messages to the in-game console --- src/scripting/lua_kernel_base.cpp | 19 +++++++++++++++++++ src/scripting/lua_kernel_base.hpp | 1 + 2 files changed, 20 insertions(+) diff --git a/src/scripting/lua_kernel_base.cpp b/src/scripting/lua_kernel_base.cpp index d377708ee263..0c88042302c5 100644 --- a/src/scripting/lua_kernel_base.cpp +++ b/src/scripting/lua_kernel_base.cpp @@ -129,6 +129,23 @@ int lua_kernel_base::intf_print(lua_State* L) return 0; } +static void impl_warn(void* p, const char* msg, int tocont) { + static const char*const prefix = "Warning:\n "; + static std::ostringstream warning(prefix); + warning.seekp(0, std::ios::end); + warning << msg << ' '; + if(!tocont) { + auto& lk = lua_kernel_base::get_lua_kernel(reinterpret_cast(p)); + lk.add_log_to_console(warning.str()); + warning.str(prefix); + } +} + +void lua_kernel_base::add_log_to_console(const std::string& msg) { + cmd_log_ << msg << "\n"; + DBG_LUA << "'" << msg << "'\n"; +} + /** * Replacement load function. Mostly the same as regular load, but disallows loading binary chunks * due to CVE-2018-1999023. @@ -328,6 +345,7 @@ static int intf_deprecated_message(lua_State* L) { lua_push(L, msg); return lua_error(L); } + lua_warning(L, msg.c_str(), false); return 0; } @@ -485,6 +503,7 @@ lua_kernel_base::lua_kernel_base() lua_setglobal(L, "std_print"); //storing original impl as 'std_print' lua_settop(L, 0); //clear stack, just to be sure + lua_setwarnf(L, &::impl_warn, L); lua_pushcfunction(L, &dispatch<&lua_kernel_base::intf_print>); lua_setglobal(L, "print"); diff --git a/src/scripting/lua_kernel_base.hpp b/src/scripting/lua_kernel_base.hpp index 904a6dd86d0a..60adef50554f 100644 --- a/src/scripting/lua_kernel_base.hpp +++ b/src/scripting/lua_kernel_base.hpp @@ -115,6 +115,7 @@ class lua_kernel_base { // Print text to the command log for this lua kernel. Used as a replacement impl for lua print. int intf_print(lua_State * L); public: + void add_log_to_console(const std::string& msg); // Show the interactive lua console (for debugging purposes) int intf_show_lua_console(lua_State * L); protected: