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.

156 lines
3.9 KiB

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