Fix mod:dump output to command window and auto-close

This commit is contained in:
Aussiemon 2023-03-29 14:50:42 -06:00
parent 70b3492d35
commit c83ef8f911
3 changed files with 34 additions and 18 deletions

View file

@ -329,6 +329,9 @@ return {
ru = "Консоль разработчика закрыта.", ru = "Консоль разработчика закрыта.",
["zh-cn"] = "已关闭开发者控制台。", ["zh-cn"] = "已关闭开发者控制台。",
}, },
dev_console_close_warning = {
en = "The developer console is disabled, but must be closed manually.",
},
-- MUTATORS -- MUTATORS

View file

@ -49,16 +49,24 @@ local function close_dev_console()
CommandWindow.close() CommandWindow.close()
-- CommandWindow won't close by itself, so it have to be closed manually -- CommandWindow won't close by itself, so it has to be closed through FFI
dmf:pcall(function() if _ffi then
_ffi.cdef([[ dmf:pcall(function()
void* FindWindowA(const char* lpClassName, const char* lpWindowName); if _ffi then
int64_t SendMessageA(void* hWnd, unsigned int Msg, uint64_t wParam, int64_t lParam); _ffi.cdef([[
]]) void* FindWindowA(const char* lpClassName, const char* lpWindowName);
local WM_CLOSE = 0x10; int64_t SendMessageA(void* hWnd, unsigned int Msg, uint64_t wParam, int64_t lParam);
local hwnd = _ffi.C.FindWindowA("ConsoleWindowClass", "Developer console") ]])
_ffi.C.SendMessageA(hwnd, WM_CLOSE, 0, 0) local WM_CLOSE = 0x10;
end) local hwnd = _ffi.C.FindWindowA("ConsoleWindowClass", "Developer console")
_ffi.C.SendMessageA(hwnd, WM_CLOSE, 0, 0)
end
end)
-- Or manually closed by the user
else
dmf:warning(dmf:localize("dev_console_close_warning"))
end
_console_data.enabled = false _console_data.enabled = false
end end

View file

@ -7,7 +7,7 @@ if not _io.initialized then
_io = dmf.deepcopy(Mods.lua.io) _io = dmf.deepcopy(Mods.lua.io)
end end
-- Local backup of the io library -- Local backup of the os library
local _os = dmf:persistent_table("_os") local _os = dmf:persistent_table("_os")
_os.initialized = _os.initialized or false _os.initialized = _os.initialized or false
if not _os.initialized then if not _os.initialized then
@ -17,6 +17,11 @@ end
-- Global backup of original print() method -- Global backup of original print() method
local print = __print local print = __print
local function log_and_console_print(message, ...)
print(message, ...)
CommandWindow.print(message, ...)
end
local function table_dump(key, value, depth, max_depth) local function table_dump(key, value, depth, max_depth)
if max_depth < depth then if max_depth < depth then
return return
@ -28,7 +33,7 @@ local function table_dump(key, value, depth, max_depth)
if value_type == "table" then if value_type == "table" then
prefix = prefix .. ((key == nil and "") or " = ") prefix = prefix .. ((key == nil and "") or " = ")
print(prefix .. "table") log_and_console_print(prefix .. "table")
for key_, value_ in pairs(value) do for key_, value_ in pairs(value) do
table_dump(key_, value_, depth + 1, max_depth) table_dump(key_, value_, depth + 1, max_depth)
@ -38,9 +43,9 @@ local function table_dump(key, value, depth, max_depth)
if meta then if meta then
if type(meta) == "boolean" then if type(meta) == "boolean" then
print(prefix .. "protected metatable") log_and_console_print(prefix .. "protected metatable")
else else
print(prefix .. "metatable") log_and_console_print(prefix .. "metatable")
for key_, value_ in pairs(meta) do for key_, value_ in pairs(meta) do
if key_ ~= "__index" and key_ ~= "super" then if key_ ~= "__index" and key_ ~= "super" then
table_dump(key_, value_, depth + 1, max_depth) table_dump(key_, value_, depth + 1, max_depth)
@ -49,9 +54,9 @@ local function table_dump(key, value, depth, max_depth)
end end
end end
elseif value_type == "function" or value_type == "thread" or value_type == "userdata" or value == nil then elseif value_type == "function" or value_type == "thread" or value_type == "userdata" or value == nil then
print(prefix .. " = " .. tostring(value)) log_and_console_print(prefix .. " = " .. tostring(value))
else else
print(prefix .. " = " .. tostring(value) .. " (" .. value_type .. ")") log_and_console_print(prefix .. " = " .. tostring(value) .. " (" .. value_type .. ")")
end end
end end
@ -77,7 +82,7 @@ DMFMod.dump = function (self, dumped_object, dumped_object_name, max_depth)
end end
if dumped_object_name then if dumped_object_name then
print(string.format("<%s>", dumped_object_name)) log_and_console_print(string.format("<%s>", dumped_object_name))
end end
if not max_depth then if not max_depth then
@ -96,7 +101,7 @@ DMFMod.dump = function (self, dumped_object, dumped_object_name, max_depth)
end end
if dumped_object_name then if dumped_object_name then
print(string.format("</%s>", dumped_object_name)) log_and_console_print(string.format("</%s>", dumped_object_name))
end end
end end