OpenSDE Framework (without history before r20070)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
3.8 KiB

  1. -- --- T2-COPYRIGHT-NOTE-BEGIN ---
  2. -- This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  3. --
  4. -- T2 SDE: misc/lua/sde/hooks.lua
  5. -- Copyright (C) 2005 - 2006 The T2 SDE Project
  6. -- Copyright (C) 2005 - 2006 Juergen "George" Sawinski
  7. --
  8. -- More information can be found in the files COPYING and README.
  9. --
  10. -- This program is free software; you can redistribute it and/or modify
  11. -- it under the terms of the GNU General Public License as published by
  12. -- the Free Software Foundation; version 2 of the License. A copy of the
  13. -- GNU General Public License can be found in the file COPYING.
  14. -- --- T2-COPYRIGHT-NOTE-END ---
  15. -- TODO:
  16. -- - add "protected" hooks (hooks, that can't be overriden simply)
  17. -- DESCRIPTION:
  18. -- 1. Create a new hook
  19. -- h = hook() or h = hook.new()
  20. --
  21. -- 2. Using hooks
  22. --
  23. -- Access to the hooks with a given hook level (from 1 to 9):
  24. -- h[num]:add(function-or-string)
  25. -- Add a function to the hook with a hook level (num).
  26. --
  27. -- h[num]:set(function-or-string)
  28. -- Replace the contents of hook-order "num" with a new function
  29. --
  30. -- h[num]:clear()
  31. -- Clear all hooks.
  32. --
  33. -- h[num]:run()
  34. -- Run a specific hook level
  35. --
  36. -- Access without hook level:
  37. -- h:add(function-or-string)
  38. -- Equivalent to h[5]:add(function-or-string)
  39. --
  40. -- h:set(function-or-string)
  41. -- Equivalent to h[5]:set(function-or-string), however, clears
  42. -- all other levels
  43. --
  44. -- h:clear()
  45. -- Clear all hooks in all levels.
  46. --
  47. -- h:run()
  48. -- Execute the hooks in all levels, starting at hook level 1.
  49. -- INTERFACE -----------------------------------------------------------------
  50. hook = { level = {} }
  51. meta = {}
  52. function hook.new()
  53. local h = hook
  54. return setmetatable(h, meta)
  55. end
  56. function hook:add(data)
  57. self[5]:add(data)
  58. end
  59. function hook:set(data)
  60. self:clear()
  61. self[5]:set(data)
  62. end
  63. function hook:run()
  64. for _,l in pairs(self.level) do l:run() end
  65. end
  66. function hook:clear()
  67. for _,l in pairs(self.level) do l:clear() end
  68. end
  69. -- h = hook()
  70. setmetatable(hook, { __call = hook.new })
  71. -- INTERNAL HOOKS __hook -----------------------------------------------------
  72. local __hook = {}
  73. local __meta = { __index = {} }
  74. -- create a new __hook
  75. function __hook.new()
  76. local h = { hooks = {} }
  77. return setmetatable(h, __meta)
  78. end
  79. -- __hook.add(hook-table, function-or-string)
  80. -- add a function to the __hook
  81. function __hook.add(h, data)
  82. if type(data) == "table" then
  83. for _,f in pairs(data) do
  84. __hook.add(h, f)
  85. end
  86. return
  87. end
  88. -- insert hook
  89. if type(data) == "function" then
  90. table.insert(h.hooks, data)
  91. elseif type(data) == "string" then
  92. local f = loadstring(data)
  93. table.insert(h.hooks, f)
  94. else
  95. assert(type(data) == "function",
  96. "function or string expected in hook.add(table, pos, function-or-string)")
  97. end
  98. end
  99. -- __hook.set(hook-table, function-or-string-or-nil)
  100. -- add a function to the __hook
  101. function __hook.set(h, data)
  102. h.hooks = {}
  103. __hook.add(h, data)
  104. end
  105. -- __hook.run(hook-table)
  106. -- execute the hooks
  107. function __hook.run(h)
  108. for _,f in pairs(h.hooks) do
  109. if f then f() end
  110. end
  111. end
  112. -- __hook.clear(hook-table)
  113. -- clear all hooks
  114. function __hook.clear(h)
  115. h.hooks = {}
  116. end
  117. -- METATABLE -----------------------------------------------------------------
  118. function __meta.__index:add(data) __hook.add(self, data) end
  119. function __meta.__index:set(data) __hook.set(self, data) end
  120. function __meta.__index:clear() __hook.clear(self) end
  121. function __meta.__index:run() __hook.run(self) end
  122. function meta.__index(self, pos, data)
  123. -- clamp position
  124. if pos < 1 then pos = 1 end
  125. if pos > 9 then pos = 9 end
  126. -- create if it does not exist
  127. if not self.level[pos] then
  128. table.insert(self.level, pos, __hook.new())
  129. end
  130. return self.level[pos]
  131. end
  132. function meta.__newindex(self, pos, data)
  133. self[pos]:set(data)
  134. end