Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. local f = {} -- f is for hook functions.
  2. local e = {} -- e is for broken hook functions.
  3.  
  4. local gmod = gmod
  5. local pmv = SortedPairsByMemberValue
  6.  
  7. local unpack = unpack
  8. local isstring = isstring
  9. local isfunction = isfunction
  10. local IsValid = IsValid
  11.  
  12. local lhook = hook
  13. local hook = {}
  14.  
  15. _G.lhook = lhook
  16. _G.hook = hook
  17.  
  18. function hook.GetTable()
  19. return f
  20. end
  21.  
  22. function hook.GetErrors()
  23. return e
  24. end
  25.  
  26. function hook.Add(event, id, callback, priority)
  27. f[event] = f[event] or {}
  28. f[event][id] = {priority = priority, callback}
  29. end
  30.  
  31. function hook.Inject(event, func, after)
  32. local old = func
  33.  
  34. if after then
  35. func = function(self, ...)
  36. local re = old(self, ...)
  37. hook.Run(event, re, ...)
  38. return re
  39. end
  40. else
  41. func = function(self, ...)
  42. hook.Run(event, ...)
  43. return old(self, ...)
  44. end
  45. end
  46.  
  47. return func
  48. end
  49.  
  50. function hook.Remove(event, id)
  51. if f[event][id] then
  52. f[event][id] = nil
  53. end
  54. end
  55.  
  56. local function catch(event,id,info)
  57. local stack = debug.traceback(coroutine.running(), info, 2)
  58. local object = f[event] and f[event][id] or nil
  59.  
  60. if object then
  61. object.errors = object.errors or {}
  62. table.insert(object.errors, stack)
  63.  
  64. if #f[event][id].errors > 5 then
  65. e[event] = e[event] or {}
  66. e[event][id] = f[event][id]
  67. e[event][id].errors = stack
  68.  
  69. f[event][id] = nil
  70. end
  71. end
  72.  
  73. error(stack)
  74. end
  75.  
  76. local function run(event, gm, ignore_priority, ...)
  77. local re
  78.  
  79. if f[event] then
  80. if isstring(event) or IsValid(event) then
  81. if ignore_priority then
  82. for id, data in next, f[event] do
  83. re = {xpcall(data.callback,
  84. function(info) catch(event,id,info) end,
  85. ...
  86. )}
  87. if re ~= nil then
  88. return unpack(re)
  89. end
  90. end
  91. else
  92. for id, data in pmv(f[event], 'priority') do
  93. re = {xpcall(data.callback,
  94. function(info) catch(event,id,info) end,
  95. ...
  96. )}
  97. if re ~= nil then
  98. return unpack(re)
  99. end
  100. end
  101. end
  102. else
  103. f[event][id] = nil
  104. end
  105. end
  106.  
  107. local GM = GM or GAMEMODE
  108.  
  109. if GM then
  110. local GamemodeFunction = GM[event]
  111. if isfunction(GamemodeFunction) then
  112. return GamemodeFunction(GM, ...)
  113. end
  114. end
  115. end
  116.  
  117. function hook.Call(event, gm, ...)
  118. local re = {run(event, gm, false, ...)}
  119. return unpack(re)
  120. end
  121.  
  122. function hook.Run(event, ...)
  123. local re = {run(event, gmod and gmod.GetGamemode() or nil, false, ...)}
  124. return unpack(re)
  125. end
  126.  
  127. function hook.RunEx(event, ignore_priority, delay, ...)
  128. local re
  129. local delay = CurTime() + delay
  130.  
  131. if ( CurTime() > delay ) then
  132. re = {run(event, gmod and gmod.GetGamemode() or nil, ignore_priority, ...)}
  133. return unpack(re)
  134. end
  135. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement