… | |
… | |
96 | ############################################################################# |
96 | ############################################################################# |
97 | # "new" plug-in system |
97 | # "new" plug-in system |
98 | |
98 | |
99 | =item cf::object::attach ... # NYI |
99 | =item cf::object::attach ... # NYI |
100 | |
100 | |
101 | =item cf::attach_global ... # NYI |
101 | =item cf::attach_global ... |
102 | |
102 | |
103 | =item cf::attach_to_type ... # NYI |
103 | =item cf::attach_to_type ... |
104 | |
104 | |
105 | =item cf::attach_to_objects ... # NYI |
105 | =item cf::attach_to_objects ... |
106 | |
106 | |
107 | =item cf::attach_to_players ... # NYI |
107 | =item cf::attach_to_players ... |
108 | |
108 | |
109 | =item cf::attach_to_maps ... # NYI |
109 | =item cf::attach_to_maps ... |
110 | |
110 | |
111 | prio => $number, # higehr is earlier |
111 | prio => $number, # lower is earlier |
112 | on_xxx => \&cb, |
112 | on_xxx => \&cb, |
113 | package => package::, |
113 | package => package::, |
114 | |
114 | |
115 | =cut |
115 | =cut |
116 | |
116 | |
117 | our %CB_CLASS = (); # registry for class-based events |
117 | # the following variables are defined in .xs and must not be re-created |
118 | our @CB_GLOBAL = (); # registry for all global events |
118 | our @CB_GLOBAL = (); # registry for all global events |
|
|
119 | our @CB_OBJECT = (); |
|
|
120 | our @CB_PLAYER = (); |
119 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
121 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
|
|
122 | our @CB_MAP = (); |
120 | |
123 | |
121 | sub _attach_cb($\%$$$) { |
124 | sub _attach_cb($\%$$$) { |
122 | my ($registry, $undo, $event, $prio, $cb) = @_; |
125 | my ($registry, $undo, $event, $prio, $cb) = @_; |
123 | |
126 | |
124 | use sort 'stable'; |
127 | use sort 'stable'; |
… | |
… | |
183 | |
186 | |
184 | sub attach_global { |
187 | sub attach_global { |
185 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
188 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
186 | } |
189 | } |
187 | |
190 | |
188 | sub attach_type { |
191 | sub attach_to_type { |
189 | my $type = shift; |
192 | my $type = shift; |
190 | _attach @{$CB_TYPE[$type]}, KLASS_MAP, @_ |
193 | _attach @{$CB_TYPE[$type]}, KLASS_OBJECT, @_ |
191 | } |
194 | } |
192 | |
195 | |
193 | sub attach_to_objects { |
196 | sub attach_to_objects { |
194 | _attach @{$CB_CLASS{cf::object::wrap::}}, KLASS_OBJECT, @_ |
197 | _attach @CB_OBJECT, KLASS_OBJECT, @_ |
195 | } |
198 | } |
196 | |
199 | |
197 | sub attach_to_players { |
200 | sub attach_to_players { |
198 | _attach @{$CB_CLASS{cf::player::wrap::}}, KLASS_PLAYER, @_ |
201 | _attach @CB_PLAYER, KLASS_PLAYER, @_ |
199 | } |
202 | } |
200 | |
203 | |
201 | sub attach_to_maps { |
204 | sub attach_to_maps { |
202 | _attach @{$CB_CLASS{cf::map::wrap::}}, KLASS_MAP, @_ |
205 | _attach @CB_MAP, KLASS_MAP, @_ |
203 | } |
206 | } |
204 | |
207 | |
205 | our $override; |
208 | our $override; |
206 | |
209 | |
207 | sub override() { |
210 | sub override() { |
208 | $override = 1 |
211 | $override = 1 |
209 | } |
212 | } |
210 | |
213 | |
211 | sub invoke { |
214 | sub invoke { |
212 | my $event = shift; |
215 | my $event = shift; |
213 | |
216 | my $callbacks = shift; |
214 | my @cb; |
|
|
215 | |
|
|
216 | if (my $ref = ref $_[0]) { |
|
|
217 | # 1. object-specific (NYI) |
|
|
218 | # 2. class-specific |
|
|
219 | push @cb, @{$CB_CLASS{$ref}[$event] || []}; |
|
|
220 | } |
|
|
221 | |
|
|
222 | # global |
|
|
223 | push @cb, @{$CB_GLOBAL[$event] || []}; |
|
|
224 | |
|
|
225 | # warn "invoke id $EVENT[$event][0], args <@_> <=> @cb\n";#d# |
|
|
226 | |
217 | |
227 | local $override; |
218 | local $override; |
228 | |
219 | |
229 | for (@cb) { |
220 | for (@$callbacks) { |
230 | eval { &{$_->[1]} }; |
221 | eval { &{$_->[1]} }; |
231 | |
222 | |
232 | if ($@) { |
223 | if ($@) { |
233 | warn "$@"; |
224 | warn "$@"; |
234 | warn "... while processing $EVENT[$event][0] event, skipping processing altogether.\n"; |
225 | warn "... while processing $EVENT[$event][0] event, skipping processing altogether.\n"; |