From c83ef8f91117c649dd12b3a9441f2acfb4c87137 Mon Sep 17 00:00:00 2001 From: Aussiemon Date: Wed, 29 Mar 2023 14:50:42 -0600 Subject: [PATCH] Fix mod:dump output to command window and auto-close --- dmf/localization/dmf.lua | 3 ++ .../mods/dmf/modules/debug/dev_console.lua | 28 ++++++++++++------- .../mods/dmf/modules/debug/table_dump.lua | 21 ++++++++------ 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/dmf/localization/dmf.lua b/dmf/localization/dmf.lua index a3d3391..87b84ad 100644 --- a/dmf/localization/dmf.lua +++ b/dmf/localization/dmf.lua @@ -329,6 +329,9 @@ return { ru = "Консоль разработчика закрыта.", ["zh-cn"] = "已关闭开发者控制台。", }, + dev_console_close_warning = { + en = "The developer console is disabled, but must be closed manually.", + }, -- MUTATORS diff --git a/dmf/scripts/mods/dmf/modules/debug/dev_console.lua b/dmf/scripts/mods/dmf/modules/debug/dev_console.lua index bcc49a2..1916438 100644 --- a/dmf/scripts/mods/dmf/modules/debug/dev_console.lua +++ b/dmf/scripts/mods/dmf/modules/debug/dev_console.lua @@ -49,16 +49,24 @@ local function close_dev_console() CommandWindow.close() - -- CommandWindow won't close by itself, so it have to be closed manually - dmf:pcall(function() - _ffi.cdef([[ - void* FindWindowA(const char* lpClassName, const char* lpWindowName); - int64_t SendMessageA(void* hWnd, unsigned int Msg, uint64_t wParam, int64_t lParam); - ]]) - local WM_CLOSE = 0x10; - local hwnd = _ffi.C.FindWindowA("ConsoleWindowClass", "Developer console") - _ffi.C.SendMessageA(hwnd, WM_CLOSE, 0, 0) - end) + -- CommandWindow won't close by itself, so it has to be closed through FFI + if _ffi then + dmf:pcall(function() + if _ffi then + _ffi.cdef([[ + void* FindWindowA(const char* lpClassName, const char* lpWindowName); + int64_t SendMessageA(void* hWnd, unsigned int Msg, uint64_t wParam, int64_t lParam); + ]]) + local WM_CLOSE = 0x10; + 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 end diff --git a/dmf/scripts/mods/dmf/modules/debug/table_dump.lua b/dmf/scripts/mods/dmf/modules/debug/table_dump.lua index bce18d3..608b765 100644 --- a/dmf/scripts/mods/dmf/modules/debug/table_dump.lua +++ b/dmf/scripts/mods/dmf/modules/debug/table_dump.lua @@ -7,7 +7,7 @@ if not _io.initialized then _io = dmf.deepcopy(Mods.lua.io) end --- Local backup of the io library +-- Local backup of the os library local _os = dmf:persistent_table("_os") _os.initialized = _os.initialized or false if not _os.initialized then @@ -17,6 +17,11 @@ end -- Global backup of original print() method 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) if max_depth < depth then return @@ -28,7 +33,7 @@ local function table_dump(key, value, depth, max_depth) if value_type == "table" then prefix = prefix .. ((key == nil and "") or " = ") - print(prefix .. "table") + log_and_console_print(prefix .. "table") for key_, value_ in pairs(value) do 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 type(meta) == "boolean" then - print(prefix .. "protected metatable") + log_and_console_print(prefix .. "protected metatable") else - print(prefix .. "metatable") + log_and_console_print(prefix .. "metatable") for key_, value_ in pairs(meta) do if key_ ~= "__index" and key_ ~= "super" then table_dump(key_, value_, depth + 1, max_depth) @@ -49,9 +54,9 @@ local function table_dump(key, value, depth, max_depth) end end 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 - print(prefix .. " = " .. tostring(value) .. " (" .. value_type .. ")") + log_and_console_print(prefix .. " = " .. tostring(value) .. " (" .. value_type .. ")") end end @@ -77,7 +82,7 @@ DMFMod.dump = function (self, dumped_object, dumped_object_name, max_depth) end if dumped_object_name then - print(string.format("<%s>", dumped_object_name)) + log_and_console_print(string.format("<%s>", dumped_object_name)) end if not max_depth then @@ -96,7 +101,7 @@ DMFMod.dump = function (self, dumped_object, dumped_object_name, max_depth) end if dumped_object_name then - print(string.format("", dumped_object_name)) + log_and_console_print(string.format("", dumped_object_name)) end end