DevConsole: Moved globals inside a persistant table

This commit is contained in:
FireSiku 2018-06-06 21:59:03 -04:00
parent 036544e04a
commit d207c30720

View file

@ -1,7 +1,12 @@
local vmf = get_mod("VMF") local vmf = get_mod("VMF")
DEV_CONSOLE_ENABLED = DEV_CONSOLE_ENABLED or false -- Note(Siku): This file could definitely use the hooking system if we could figure out a way.
PRINT_ORIGINAL_FUNCTION = PRINT_ORIGINAL_FUNCTION or print -- It would requires hooks to be pushed higher in the loading order, but then we lose hooks printing to console
-- Unless we find a way to store our logging messages in memory before the console is loaded.
local _console_data = vmf:persistent_table("dev_console_data")
if not _console_data.enabled then _console_data.enabled = false end
if not _console_data.original_print then _console_data.original_print = print end
-- #################################################################################################################### -- ####################################################################################################################
-- ##### Local functions ############################################################################################## -- ##### Local functions ##############################################################################################
@ -9,10 +14,10 @@ PRINT_ORIGINAL_FUNCTION = PRINT_ORIGINAL_FUNCTION or print
local function open_dev_console() local function open_dev_console()
if not DEV_CONSOLE_ENABLED then if not _console_data.enabled then
local print_hook_function = function(func, ...) local print_hook_function = function(func, ...)
if DEV_CONSOLE_ENABLED then if _console_data.enabled then
CommandWindow.print(...) CommandWindow.print(...)
func(...) func(...)
else else
@ -21,19 +26,19 @@ local function open_dev_console()
end end
print = function(...) print = function(...)
print_hook_function(PRINT_ORIGINAL_FUNCTION, ...) print_hook_function(_console_data.original_print, ...)
end end
CommandWindow.open("Developer console") CommandWindow.open("Developer console")
DEV_CONSOLE_ENABLED = true _console_data.enabled = true
end end
end end
local function close_dev_console() local function close_dev_console()
if DEV_CONSOLE_ENABLED then if _console_data.enabled then
print = PRINT_ORIGINAL_FUNCTION print = _console_data.original_print
CommandWindow.close() CommandWindow.close()
@ -49,7 +54,7 @@ local function close_dev_console()
ffi.C.SendMessageA(hwnd, WM_CLOSE, 0, 0) ffi.C.SendMessageA(hwnd, WM_CLOSE, 0, 0)
end) end)
DEV_CONSOLE_ENABLED = false _console_data.enabled = false
end end
end end