Steal hooks to solve addon conflicts

This commit is contained in:
weltensturm 2018-01-06 09:55:23 +01:00
parent 00c3263934
commit cc3d38fdab
3 changed files with 66 additions and 6 deletions

View File

@ -14,3 +14,9 @@ Installation
------------
Download the .zip file and put the folders inside into your garrysmod/garrysmod/addons folder.
Discord
-------
Wanna talk? Join our discord: https://discord.gg/bzZQJEe

View File

@ -66,6 +66,7 @@ else
local view = {}
local lastCalcView = 0
wac.calcView = function(player, pos, ang, fov)
if not wac.hooksOverride["CalcView"] then return end
if lastCalcView == CurTime() then return end
player.wac = player.wac or {}
lastCalcView = CurTime()
@ -75,7 +76,7 @@ else
view.vm_angles = nil
view.vm_origin = nil
local send = false
for name, hook in pairs(wac.hooksCalcView) do
for name, hook in pairs(wac.hooksOverride["CalcView"]) do
local t = hook.f(player, view.origin, view.angles, view.fov)
if t then
view.origin = t.origin or view.origin
@ -89,7 +90,6 @@ else
player.wac.lastView = table.Copy(view)
if send then return view end
end
hook.Add("CalcView", "wac_cl_calcview", wac.calcView)
wac.hooks["wac_cl_calcview"] = {f = wac.calcView, g = "CalcView"}
end
@ -100,3 +100,56 @@ concommand.Add("wac_reloadhooks" .. (CLIENT and "_cl" or ""), function(player, c
hook.Add(t.g, name, t.f)
end
end)
hook.Add("Think", "wac_stealhooks", function()
for gmname, hooks in pairs(hook.GetTable()) do
if WAC_STEAL_HOOKS[gmname] then
for name, cb in pairs(hooks) do
if not wac.hooks[name] and (not wac.hooksOverride[gmname] or not wac.hooksOverride[gmname][name]) then
print("WAC is stealing hook "..gmname.." "..tostring(name))
wac.stolenHooks[gmname] = wac.stolenHooks[gmname] or {}
wac.stolenHooks[gmname][name] = cb
hook.Remove(gmname, name)
end
end
end
end
end)
hook.Add("CalcView", "wac_calcview", function(pl, pos, ang, fov)
local r = wac.calcView(pl, pos, ang, fov)
if r then
return r
end
if wac.stolenHooks["CalcView"] then
for _, f in pairs(wac.stolenHooks["CalcView"]) do
r = f(pl, pos, ang, fov)
if r then
return r
end
end
end
end)
wac.hooks["wac_calcview"] = true
hook.Add("CreateMove", "wac_createmove", function(move)
local result = nil
if wac.hooksOverride["CreateMove"] then
for _, hook in pairs(wac.hooksOverride["CreateMove"]) do
local r = hook.f(move)
if r then
result = r
end
end
end
if wac.stolenHooks["CreateMove"] then
for _, f in pairs(wac.stolenHooks["CreateMove"]) do
local r = f(move)
if r then
result = r
end
end
end
return result
end)
wac.hooks["wac_createmove"] = true

View File

@ -69,12 +69,13 @@ wac = wac or {
return begin
end,
hooks = {},
hooksCalcView = {},
hooksOverride = {},
stolenHooks = {},
hook = function(gmhook, name, func, unload)
if gmhook == "CalcView" then
wac.hooksCalcView[name] = {f = func, u = unload, g = gmhook}
if WAC_STEAL_HOOKS[gmhook] then
wac.hooksOverride[gmhook] = wac.hooksOverride[gmhook] or {}
wac.hooksOverride[gmhook][name] = {f = func, u = unload, g = gmhook}
else
wac.hooks[name] = {f = func, u = unload, g = gmhook}
hook.Add(gmhook, name, func)