1 -- --- T2-COPYRIGHT-NOTE-BEGIN ---
2 -- This copyright note is auto-generated by ./scripts/Create-CopyPatch.
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
8 -- More information can be found in the files COPYING and README.
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 ---
17 -- - add "protected" hooks (hooks, that can't be overriden simply)
20 -- 1. Create a new hook
21 -- h = hook() or h = hook.new()
25 -- Access to the hooks with a given hook level (from 1 to 9):
26 -- h[num]:add(function-or-string)
27 -- Add a function to the hook with a hook level (num).
29 -- h[num]:set(function-or-string)
30 -- Replace the contents of hook-order "num" with a new function
36 -- Run a specific hook level
38 -- Access without hook level:
39 -- h:add(function-or-string)
40 -- Equivalent to h[5]:add(function-or-string)
42 -- h:set(function-or-string)
43 -- Equivalent to h[5]:set(function-or-string), however, clears
47 -- Clear all hooks in all levels.
50 -- Execute the hooks in all levels, starting at hook level 1.
52 -- INTERFACE -----------------------------------------------------------------
58 return setmetatable(h
, meta
)
61 function hook
:add(data
)
65 function hook
:set(data
)
71 for _
,l
in pairs(self
.level
) do l
:run() end
75 for _
,l
in pairs(self
.level
) do l
:clear() end
79 setmetatable(hook
, { __call
= hook
.new
})
81 -- INTERNAL HOOKS __hook -----------------------------------------------------
83 local __meta
= { __index
= {} }
85 -- create a new __hook
87 local h
= { hooks
= {} }
88 return setmetatable(h
, __meta
)
91 -- __hook.add(hook-table, function-or-string)
92 -- add a function to the __hook
93 function __hook
.add(h
, data
)
94 if type(data
) == "table" then
95 for _
,f
in pairs(data
) do
102 if type(data
) == "function" then
103 table.insert(h
.hooks
, data
)
104 elseif type(data
) == "string" then
105 local f
= loadstring(data
)
106 table.insert(h
.hooks
, f
)
108 assert(type(data
) == "function",
109 "function or string expected in hook.add(table, pos, function-or-string)")
113 -- __hook.set(hook-table, function-or-string-or-nil)
114 -- add a function to the __hook
115 function __hook
.set(h
, data
)
120 -- __hook.run(hook-table)
122 function __hook
.run(h
)
123 for _
,f
in pairs(h
.hooks
) do
128 -- __hook.clear(hook-table)
130 function __hook
.clear(h
)
134 -- METATABLE -----------------------------------------------------------------
135 function __meta
.__index
:add(data
) __hook
.add(self
, data
) end
136 function __meta
.__index
:set(data
) __hook
.set(self
, data
) end
137 function __meta
.__index
:clear() __hook
.clear(self
) end
138 function __meta
.__index
:run() __hook
.run(self
) end
140 function meta
.__index(self
, pos
, data
)
142 if pos
< 1 then pos
= 1 end
143 if pos
> 9 then pos
= 9 end
145 -- create if it does not exist
146 if not self
.level
[pos
] then
147 table.insert(self
.level
, pos
, __hook
.new())
150 return self
.level
[pos
]
153 function meta
.__newindex(self
, pos
, data
)