initial commit

This commit is contained in:
bryanthaboi
2026-07-17 20:30:02 -04:00
commit a5d2e77e7d
298 changed files with 100561 additions and 0 deletions
+47
View File
@@ -0,0 +1,47 @@
local Hooks = {}
Hooks.__index = Hooks
local unpack = table.unpack or unpack
function Hooks.new()
return setmetatable({ chains = {}, sealed = false }, Hooks)
end
function Hooks:wrap(name, callback, priority)
assert(not self.sealed, "mod hooks are sealed")
assert(type(name) == "string" and name ~= "", "hook name is required")
assert(type(callback) == "function", "hook callback must be a function")
local chain = self.chains[name] or {}
self.chains[name] = chain
local entry = { callback = callback, priority = priority or 0 }
chain[#chain + 1] = entry
table.sort(chain, function(a, b) return a.priority > b.priority end)
return function()
for i, candidate in ipairs(chain) do
if candidate == entry then table.remove(chain, i) break end
end
end
end
function Hooks:call(name, vanilla, ...)
local chain = self.chains[name] or {}
local args = { ... }
local function run(index, current)
if index > #chain then return current(unpack(args)) end
return chain[index].callback(function(...)
local nextArgs = { ... }
if #nextArgs == 0 then return run(index + 1, current) end
local old = args
args = nextArgs
local result = run(index + 1, current)
args = old
return result
end, unpack(args))
end
return run(1, vanilla)
end
function Hooks:seal()
self.sealed = true
end
return Hooks