Delaying hooks in perpetuity, report delayed hooks on unload, re-run delayed hooks if a class name matches

This commit is contained in:
Aussiemon 2023-01-09 15:18:41 -07:00
parent 1134babf79
commit 5ec33a79bc
2 changed files with 25 additions and 3 deletions

View file

@ -80,6 +80,7 @@ end
function dmf_mod_object:on_unload()
print("DMF:ON_UNLOAD()")
dmf.report_delayed_hooks()
dmf.save_chat_history()
dmf.save_unsaved_settings_to_file()
dmf.destroy_command_gui()

View file

@ -18,6 +18,7 @@ local HOOK_TYPE_ORIGIN = 3
-- dont need to attach this to registry.
local _delayed = {}
local _delayed_obj_names = {}
local _delaying_enabled = true
-- This metatable will automatically create a table entry if one doesnt exist.
@ -285,6 +286,7 @@ local function generic_hook(mod, obj, method, handler, func_name)
table.insert(_delayed, function()
generic_hook(mod, obj, method, handler, func_name)
end)
_delayed_obj_names[obj] = true
return
else
mod:error("(%s): trying to hook object that doesn't exist: %s", func_name, obj)
@ -339,6 +341,7 @@ local function generic_hook_toggle(mod, obj, method, enabled_state)
table.insert(_delayed, function()
generic_hook_toggle(mod, obj, method, enabled_state)
end)
_delayed_obj_names[obj] = true
return
else
mod:error("(%s): trying to toggle hook on object that doesn't exist: %s", func_name, obj)
@ -464,9 +467,6 @@ dmf.hooks_unload = function()
end
dmf.apply_delayed_hooks = function(status, state)
if status == "enter" and state == "StateIngame" then
_delaying_enabled = false
end
if #_delayed > 0 then
dmf:info("Attempt to hook %s delayed hooks", #_delayed)
-- Go through the table in reverse so we don't get any issues removing entries inside the loop
@ -477,6 +477,13 @@ dmf.apply_delayed_hooks = function(status, state)
end
end
dmf.report_delayed_hooks = function()
if #_delayed > 0 then
dmf:info(tostring(#_delayed) .. " hooked function" ..
(#_delayed == 1 and " does" or "s do") .. " not exist.")
end
end
dmf.apply_hooks_to_file = function(require_store, filepath, store_index)
if _file_hooks_by_file[filepath] and require_store and require_store[store_index] then
for mod_name, hook_create_func in pairs(_file_hooks_by_file[filepath]) do
@ -486,3 +493,17 @@ dmf.apply_hooks_to_file = function(require_store, filepath, store_index)
end
end
end
-- ####################################################################################################################
-- ##### Hooks ########################################################################################################
-- ####################################################################################################################
-- If class() is called on an object we've delayed for, re-run delayed hooks
dmf:hook_safe(_G, "class", function(class_name)
if _delayed_obj_names[class_name] then
dmf:info("%s is now available for previously-delayed hooks.", class_name)
dmf.apply_delayed_hooks()
_delayed_obj_names[class_name] = nil
end
end)