Skip to content

Commit

Permalink
Utilize the new Lua warning system to push deprecation messages to th…
Browse files Browse the repository at this point in the history
…e in-game console
  • Loading branch information
CelticMinstrel committed Feb 15, 2021
1 parent c52b71b commit b60164b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/scripting/lua_kernel_base.cpp
Expand Up @@ -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<lua_kernel_base>(reinterpret_cast<lua_State*>(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.
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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");

Expand Down
1 change: 1 addition & 0 deletions src/scripting/lua_kernel_base.hpp
Expand Up @@ -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:
Expand Down

0 comments on commit b60164b

Please sign in to comment.