diff --git a/vmf/scripts/mods/vmf/modules/core/newhooks.lua b/vmf/scripts/mods/vmf/modules/core/newhooks.lua index f7e9c40..9142057 100644 --- a/vmf/scripts/mods/vmf/modules/core/newhooks.lua +++ b/vmf/scripts/mods/vmf/modules/core/newhooks.lua @@ -1,13 +1,15 @@ local vmf = get_mod("VMF") --- Private members -local HookMixin = {} +-- #################################################################################################################### +-- ##### Locals and Variables ######################################################################################### +-- #################################################################################################################### -local HOOK_TYPE_BEFORE = 1 -local HOOK_TYPE_AFTER = 2 -local HOOK_TYPE_NORMAL = 3 +-- Constants for hook_type +local HOOK_TYPE_NORMAL = 1 +local HOOK_TYPE_BEFORE = 2 +local HOOK_TYPE_AFTER = 3 local HOOK_TYPE_RAW = 4 -local HOOK_ERR_NAME = { "before-hook", "after-hook", "hook", "rawhook", } +local HOOK_ERR_NAME = { "hook", "before", "after", "rawhook", } --[[ Planned registry structure: _registry[self][hook_type] = { @@ -154,7 +156,7 @@ local function create_hook(self, orig, obj, method, handler, hook_type) end if type(obj) == "string" then - if not _G[obj] then + if not rawget(_G, obj) then return end obj = _G[obj] @@ -197,11 +199,11 @@ end local function get_orig_function(self, obj, method) -- Validate types if obj and not (type(obj) == "table" or type(obj) == "string") then - self:error("(hook): 'object' - table or string expected, got %s", type(obj)) + self:error("(hook): 'object' - table or string expected, got %s [args: %s, %s", type(obj), obj or "nil", method or "nil") return end if type(method) ~= "string" then - self:error("(hook): 'method' - string expected, got %s", type(method)) + self:error("(hook): 'method' - string expected, got %s [args: %s, %s]", type(method), obj or "nil", method or "nil") return end @@ -209,7 +211,7 @@ local function get_orig_function(self, obj, method) -- obj can be a string. We'll need to grab the actual object first. -- if we can't find object, we don't need to go any further. if type(obj) == "string" then - if not _G[obj] then return end + if not rawget(_G, obj) then return end obj = _G[obj] end if is_orig_hooked(obj, method) then @@ -240,7 +242,7 @@ end -- This type of hook is typically used if you need to know a function was called, but dont want to modify it. -- These will always be executed before the hook chain. -- Due to discussion, handler may not receive any arguments, but will see what the use cases are with them first. -function HookMixin:new_before(obj, method, handler) +function VMFMod:before(obj, method, handler) if type(method) == "function" then method, handler, obj = obj, method, nil end @@ -253,7 +255,7 @@ end -- original function, nor can you change its return values. -- These will always be executed after the hook chain. -- This is similar to :front() functionality in V1 modding. -function HookMixin:new_after(obj, method, handler) +function VMFMod:after(obj, method, handler) if type(method) == "function" then method, handler, obj = obj, method, nil end @@ -266,7 +268,7 @@ end -- and control it's execution. All hooks on the same function will be part of a chain, with the -- original function at the end. Your handler has to call the next function in the chain manually. -- The chain of event is determined by mod load order. -function HookMixin:new_hook(obj, method, handler) +function VMFMod:hook(obj, method, handler) if type(method) == "function" then method, handler, obj = obj, method, nil end @@ -280,7 +282,7 @@ end -- This is similar to :back functionality that was sparsely used in old V1 mods. -- This there is a limit of a single rawhook for any given function. -- This should only be used as a last resort due to its limitation and its potential to break the game if not careful. -function HookMixin:new_rawhook(obj, method, handler) +function VMFMod:rawhook(obj, method, handler) if type(method) == "function" then method, handler, obj = obj, method, nil end @@ -289,7 +291,7 @@ function HookMixin:new_rawhook(obj, method, handler) create_hook(self, orig, obj, method, handler, HOOK_TYPE_RAW) end -function HookMixin:enable_all_hooks() +function VMFMod:enable_all_hooks() -- Using pairs because the self table may contain nils, and order isnt important. for _, hooks in pairs(_registry[self]) do for orig, _ in pairs(hooks.active) do @@ -298,7 +300,7 @@ function HookMixin:enable_all_hooks() end end -function HookMixin:disable_all_hooks() +function VMFMod:disable_all_hooks() -- Using pairs because the self table may contain nils, and order isnt important. for _, hooks in pairs(_registry[self]) do for orig, _ in pairs(hooks.active) do @@ -313,11 +315,4 @@ end -- -- removes all hooks when VMF is about to be reloaded -- vmf.hooks_unload = function() --- end - --- put the hook mixin inside VMFMod -do - for k, v in pairs(HookMixin) do - VMFMod[k] = v - end -end \ No newline at end of file +-- end \ No newline at end of file diff --git a/vmf/scripts/mods/vmf/modules/gui/custom_textures.lua b/vmf/scripts/mods/vmf/modules/gui/custom_textures.lua index d72cf49..edf70b7 100644 --- a/vmf/scripts/mods/vmf/modules/gui/custom_textures.lua +++ b/vmf/scripts/mods/vmf/modules/gui/custom_textures.lua @@ -172,6 +172,7 @@ end -- ##### Hooks ######################################################################################################## -- #################################################################################################################### +local LUA_SCRIPT_CALLER_POSITION = 4 vmf:hook("UIRenderer.create", function(func, world, ...) local is_modified = false @@ -182,11 +183,11 @@ vmf:hook("UIRenderer.create", function(func, world, ...) local callstack = debug.traceback() -- get the name of lua script which called 'UIRenderer.create' - -- it's always the 3rd string of the 'debug.traceback()' output + -- it's the 4th string of the 'debug.traceback()' output local i = 0 for s in callstack:gmatch("(.-)\n") do i = i + 1 - if i == 3 then + if i == LUA_SCRIPT_CALLER_POSITION then ui_renderer_creator = s:match("([^%/]+)%.lua:") break end diff --git a/vmf/scripts/mods/vmf/vmf_loader.lua b/vmf/scripts/mods/vmf/vmf_loader.lua index 49fd2b5..e5a12f3 100644 --- a/vmf/scripts/mods/vmf/vmf_loader.lua +++ b/vmf/scripts/mods/vmf/vmf_loader.lua @@ -18,7 +18,8 @@ function vmf_mod_object:init() dofile("scripts/mods/vmf/modules/core/persistent_tables") dofile("scripts/mods/vmf/modules/debug/dev_console") dofile("scripts/mods/vmf/modules/debug/table_dump") - dofile("scripts/mods/vmf/modules/core/hooks") + --dofile("scripts/mods/vmf/modules/core/hooks") + dofile("scripts/mods/vmf/modules/core/newhooks") dofile("scripts/mods/vmf/modules/core/toggling") dofile("scripts/mods/vmf/modules/core/keybindings") dofile("scripts/mods/vmf/modules/core/delayed_chat_messages")