… | |
… | |
16 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
16 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
17 | $YAML::Syck::ImplicitUnicode = 1; |
17 | $YAML::Syck::ImplicitUnicode = 1; |
18 | |
18 | |
19 | use strict; |
19 | use strict; |
20 | |
20 | |
|
|
21 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
|
|
22 | |
|
|
23 | our %COMMAND = (); |
|
|
24 | our %COMMAND_TIME = (); |
|
|
25 | our %EXTCMD = (); |
|
|
26 | |
21 | _init_vars; |
27 | _init_vars; |
22 | |
28 | |
23 | our %COMMAND = (); |
|
|
24 | our @EVENT; |
29 | our @EVENT; |
25 | our $LIBDIR = maps_directory "perl"; |
30 | our $LIBDIR = datadir . "/ext"; |
26 | |
31 | |
27 | our $TICK = MAX_TIME * 1e-6; |
32 | our $TICK = MAX_TIME * 1e-6; |
28 | our $TICK_WATCHER; |
33 | our $TICK_WATCHER; |
29 | our $NEXT_TICK; |
34 | our $NEXT_TICK; |
30 | |
35 | |
31 | our %CFG; |
36 | our %CFG; |
32 | |
37 | |
33 | our $uptime;#d# |
38 | our $UPTIME; $UPTIME ||= time; |
34 | our $UPTIME; |
|
|
35 | $UPTIME ||= $uptime;#d# |
|
|
36 | $UPTIME ||= time; |
|
|
37 | |
39 | |
38 | ############################################################################# |
40 | ############################################################################# |
39 | |
41 | |
40 | =head2 GLOBAL VARIABLES |
42 | =head2 GLOBAL VARIABLES |
41 | |
43 | |
… | |
… | |
72 | print STDERR "cfperl: $msg"; |
74 | print STDERR "cfperl: $msg"; |
73 | LOG llevError, "cfperl: $msg"; |
75 | LOG llevError, "cfperl: $msg"; |
74 | }; |
76 | }; |
75 | } |
77 | } |
76 | |
78 | |
|
|
79 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
|
|
80 | @safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; |
|
|
81 | @safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable'; |
|
|
82 | @safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable'; |
|
|
83 | @safe::cf::map::ISA = @cf::map::ISA = 'cf::attachable'; |
77 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
84 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
78 | |
85 | |
79 | # we bless all objects into (empty) derived classes to force a method lookup |
86 | # we bless all objects into (empty) derived classes to force a method lookup |
80 | # within the Safe compartment. |
87 | # within the Safe compartment. |
81 | for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch cf::living)) { |
88 | for my $pkg (qw( |
|
|
89 | cf::global |
|
|
90 | cf::object cf::object::player |
|
|
91 | cf::client cf::player |
|
|
92 | cf::arch cf::living |
|
|
93 | cf::map cf::party cf::region |
|
|
94 | )) { |
82 | no strict 'refs'; |
95 | no strict 'refs'; |
83 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
96 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
84 | } |
97 | } |
85 | |
98 | |
86 | $Event::DIED = sub { |
99 | $Event::DIED = sub { |
… | |
… | |
88 | }; |
101 | }; |
89 | |
102 | |
90 | my %ext_pkg; |
103 | my %ext_pkg; |
91 | my @exts; |
104 | my @exts; |
92 | my @hook; |
105 | my @hook; |
93 | my %command; |
|
|
94 | my %extcmd; |
|
|
95 | |
106 | |
96 | =head2 UTILITY FUNCTIONS |
107 | =head2 UTILITY FUNCTIONS |
97 | |
108 | |
98 | =over 4 |
109 | =over 4 |
99 | |
110 | |
… | |
… | |
127 | |
138 | |
128 | =cut |
139 | =cut |
129 | |
140 | |
130 | ############################################################################# |
141 | ############################################################################# |
131 | |
142 | |
132 | =head2 EVENTS AND OBJECT ATTACHMENTS |
143 | =head2 ATTACHABLE OBJECTS |
|
|
144 | |
|
|
145 | You can define and attach attachments to each "attachable" object in |
|
|
146 | crossfire+ (objects, players, clients, maps and the special "global" |
|
|
147 | class). In the following description, CLASS can be any of C<global>, |
|
|
148 | C<object> C<player>, C<client> or C<map>. |
133 | |
149 | |
134 | =over 4 |
150 | =over 4 |
135 | |
151 | |
136 | =item $object->attach ($attachment, key => $value...) |
152 | =item cf::CLASS::attachment $name, ... |
137 | |
153 | |
138 | =item $object->detach ($attachment) |
154 | Register an attachment by name through which attachable objects can refer |
|
|
155 | to this attachment. |
139 | |
156 | |
140 | Attach/detach a pre-registered attachment to an object. |
|
|
141 | |
|
|
142 | =item $player->attach ($attachment, key => $value...) |
|
|
143 | |
|
|
144 | =item $player->detach ($attachment) |
|
|
145 | |
|
|
146 | Attach/detach a pre-registered attachment to a player. |
|
|
147 | |
|
|
148 | =item $map->attach ($attachment, key => $value...) |
|
|
149 | |
|
|
150 | =item $map->detach ($attachment) |
|
|
151 | |
|
|
152 | Attach/detach a pre-registered attachment to a map. |
|
|
153 | |
|
|
154 | =item $bool = $object->attached ($name) |
|
|
155 | |
|
|
156 | =item $bool = $player->attached ($name) |
|
|
157 | |
|
|
158 | =item $bool = $map->attached ($name) |
157 | =item $bool = $attachable->attached ($name) |
159 | |
158 | |
160 | Checks wether the named attachment is currently attached to the object. |
159 | Checks wether the named attachment is currently attached to the object. |
161 | |
160 | |
162 | =item cf::attach_global ... |
161 | =item $attachable->attach ($attachment, key => $value...) |
163 | |
162 | |
164 | Attach handlers for global events. |
163 | =item $attachable->detach ($attachment) |
165 | |
164 | |
166 | This and all following C<attach_*>-functions expect any number of the |
165 | Attach/detach a pre-registered attachment either to a specific object |
167 | following handler/hook descriptions: |
166 | (C<$attachable>) or all objects of the given class (if C<$attachable> is a |
|
|
167 | class in a static method call). |
|
|
168 | |
|
|
169 | You can attach to global events by using the C<cf::global> class. |
|
|
170 | |
|
|
171 | These method calls expect any number of the following handler/hook |
|
|
172 | descriptions: |
168 | |
173 | |
169 | =over 4 |
174 | =over 4 |
170 | |
175 | |
171 | =item prio => $number |
176 | =item prio => $number |
172 | |
177 | |
… | |
… | |
174 | by another C<prio> setting). Lower priority handlers get executed |
179 | by another C<prio> setting). Lower priority handlers get executed |
175 | earlier. The default priority is C<0>, and many built-in handlers are |
180 | earlier. The default priority is C<0>, and many built-in handlers are |
176 | registered at priority C<-1000>, so lower priorities should not be used |
181 | registered at priority C<-1000>, so lower priorities should not be used |
177 | unless you know what you are doing. |
182 | unless you know what you are doing. |
178 | |
183 | |
|
|
184 | =item type => $type |
|
|
185 | |
|
|
186 | (Only for C<< cf::object->attach >> calls), limits the attachment to the |
|
|
187 | given type of objects only (the additional parameter C<subtype> can be |
|
|
188 | used to further limit to the given subtype). |
|
|
189 | |
179 | =item on_I<event> => \&cb |
190 | =item on_I<event> => \&cb |
180 | |
191 | |
181 | Call the given code reference whenever the named event happens (event is |
192 | Call the given code reference whenever the named event happens (event is |
182 | something like C<instantiate>, C<apply>, C<use_skill> and so on, and which |
193 | something like C<instantiate>, C<apply>, C<use_skill> and so on, and which |
183 | handlers are recognised generally depends on the type of object these |
194 | handlers are recognised generally depends on the type of object these |
… | |
… | |
192 | package and register them. Only handlers for eevents supported by the |
203 | package and register them. Only handlers for eevents supported by the |
193 | object/class are recognised. |
204 | object/class are recognised. |
194 | |
205 | |
195 | =back |
206 | =back |
196 | |
207 | |
197 | =item cf::attach_to_type $object_type, $subtype, ... |
|
|
198 | |
|
|
199 | Attach handlers for a specific object type (e.g. TRANSPORT) and |
|
|
200 | subtype. If C<$subtype> is zero or undef, matches all objects of the given |
|
|
201 | type. |
|
|
202 | |
|
|
203 | =item cf::attach_to_objects ... |
|
|
204 | |
|
|
205 | Attach handlers to all objects. Do not use this except for debugging or |
|
|
206 | very rare events, as handlers are (obviously) called for I<all> objects in |
|
|
207 | the game. |
|
|
208 | |
|
|
209 | =item cf::attach_to_players ... |
|
|
210 | |
|
|
211 | Attach handlers to all players. |
|
|
212 | |
|
|
213 | =item cf::attach_to_maps ... |
|
|
214 | |
|
|
215 | Attach handlers to all maps. |
|
|
216 | |
|
|
217 | =item cf:register_attachment $name, ... |
|
|
218 | |
|
|
219 | Register an attachment by name through which objects can refer to this |
|
|
220 | attachment. |
|
|
221 | |
|
|
222 | =item cf:register_player_attachment $name, ... |
|
|
223 | |
|
|
224 | Register an attachment by name through which players can refer to this |
|
|
225 | attachment. |
|
|
226 | |
|
|
227 | =item cf:register_map_attachment $name, ... |
|
|
228 | |
|
|
229 | Register an attachment by name through which maps can refer to this |
|
|
230 | attachment. |
|
|
231 | |
|
|
232 | =cut |
208 | =cut |
233 | |
209 | |
234 | # the following variables are defined in .xs and must not be re-created |
210 | # the following variables are defined in .xs and must not be re-created |
235 | our @CB_GLOBAL = (); # registry for all global events |
211 | our @CB_GLOBAL = (); # registry for all global events |
236 | our @CB_OBJECT = (); # all objects (should not be used except in emergency) |
212 | our @CB_OBJECT = (); # all objects (should not be used except in emergency) |
237 | our @CB_PLAYER = (); |
213 | our @CB_PLAYER = (); |
|
|
214 | our @CB_CLIENT = (); |
238 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
215 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
239 | our @CB_MAP = (); |
216 | our @CB_MAP = (); |
240 | |
217 | |
241 | my %attachment; |
218 | my %attachment; |
242 | |
219 | |
243 | sub _attach_cb($\%$$$) { |
220 | sub _attach_cb($$$$) { |
244 | my ($registry, $undo, $event, $prio, $cb) = @_; |
221 | my ($registry, $event, $prio, $cb) = @_; |
245 | |
222 | |
246 | use sort 'stable'; |
223 | use sort 'stable'; |
247 | |
224 | |
248 | $cb = [$prio, $cb]; |
225 | $cb = [$prio, $cb]; |
249 | |
226 | |
250 | @{$registry->[$event]} = sort |
227 | @{$registry->[$event]} = sort |
251 | { $a->[0] cmp $b->[0] } |
228 | { $a->[0] cmp $b->[0] } |
252 | @{$registry->[$event] || []}, $cb; |
229 | @{$registry->[$event] || []}, $cb; |
253 | |
|
|
254 | push @{$undo->{cb}}, [$event, $cb]; |
|
|
255 | } |
230 | } |
256 | |
231 | |
257 | # attach handles attaching event callbacks |
232 | # attach handles attaching event callbacks |
258 | # the only thing the caller has to do is pass the correct |
233 | # the only thing the caller has to do is pass the correct |
259 | # registry (== where the callback attaches to). |
234 | # registry (== where the callback attaches to). |
260 | sub _attach(\@$@) { |
235 | sub _attach { |
261 | my ($registry, $klass, @arg) = @_; |
236 | my ($registry, $klass, @arg) = @_; |
262 | |
237 | |
|
|
238 | my $object_type; |
263 | my $prio = 0; |
239 | my $prio = 0; |
264 | |
|
|
265 | my %undo = ( |
|
|
266 | registry => $registry, |
|
|
267 | cb => [], |
|
|
268 | ); |
|
|
269 | |
|
|
270 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
240 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
271 | |
241 | |
272 | while (@arg) { |
242 | while (@arg) { |
273 | my $type = shift @arg; |
243 | my $type = shift @arg; |
274 | |
244 | |
275 | if ($type eq "prio") { |
245 | if ($type eq "prio") { |
276 | $prio = shift @arg; |
246 | $prio = shift @arg; |
277 | |
247 | |
|
|
248 | } elsif ($type eq "type") { |
|
|
249 | $object_type = shift @arg; |
|
|
250 | $registry = $CB_TYPE[$object_type] ||= []; |
|
|
251 | |
|
|
252 | } elsif ($type eq "subtype") { |
|
|
253 | defined $object_type or Carp::croak "subtype specified without type"; |
|
|
254 | my $object_subtype = shift @arg; |
|
|
255 | $registry = $CB_TYPE[$object_type + $object_subtype * NUM_SUBTYPES] ||= []; |
|
|
256 | |
278 | } elsif ($type eq "package") { |
257 | } elsif ($type eq "package") { |
279 | my $pkg = shift @arg; |
258 | my $pkg = shift @arg; |
280 | |
259 | |
281 | while (my ($name, $id) = each %cb_id) { |
260 | while (my ($name, $id) = each %cb_id) { |
282 | if (my $cb = $pkg->can ($name)) { |
261 | if (my $cb = $pkg->can ($name)) { |
283 | _attach_cb $registry, %undo, $id, $prio, $cb; |
262 | _attach_cb $registry, $id, $prio, $cb; |
284 | } |
263 | } |
285 | } |
264 | } |
286 | |
265 | |
287 | } elsif (exists $cb_id{$type}) { |
266 | } elsif (exists $cb_id{$type}) { |
288 | _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @arg; |
267 | _attach_cb $registry, $cb_id{$type}, $prio, shift @arg; |
289 | |
268 | |
290 | } elsif (ref $type) { |
269 | } elsif (ref $type) { |
291 | warn "attaching objects not supported, ignoring.\n"; |
270 | warn "attaching objects not supported, ignoring.\n"; |
292 | |
271 | |
293 | } else { |
272 | } else { |
294 | shift @arg; |
273 | shift @arg; |
295 | warn "attach argument '$type' not supported, ignoring.\n"; |
274 | warn "attach argument '$type' not supported, ignoring.\n"; |
296 | } |
275 | } |
297 | } |
276 | } |
298 | |
|
|
299 | \%undo |
|
|
300 | } |
277 | } |
301 | |
278 | |
302 | sub _attach_attachment { |
279 | sub _object_attach { |
303 | my ($obj, $name, %arg) = @_; |
280 | my ($obj, $name, %arg) = @_; |
304 | |
281 | |
305 | return if exists $obj->{_attachment}{$name}; |
282 | return if exists $obj->{_attachment}{$name}; |
306 | |
|
|
307 | my $res; |
|
|
308 | |
283 | |
309 | if (my $attach = $attachment{$name}) { |
284 | if (my $attach = $attachment{$name}) { |
310 | my $registry = $obj->registry; |
285 | my $registry = $obj->registry; |
311 | |
286 | |
312 | for (@$attach) { |
287 | for (@$attach) { |
313 | my ($klass, @attach) = @$_; |
288 | my ($klass, @attach) = @$_; |
314 | $res = _attach @$registry, $klass, @attach; |
289 | _attach $registry, $klass, @attach; |
315 | } |
290 | } |
316 | |
291 | |
317 | $obj->{$name} = \%arg; |
292 | $obj->{$name} = \%arg; |
318 | } else { |
293 | } else { |
319 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
294 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
320 | } |
295 | } |
321 | |
296 | |
322 | $obj->{_attachment}{$name} = undef; |
297 | $obj->{_attachment}{$name} = undef; |
323 | |
|
|
324 | $res->{attachment} = $name; |
|
|
325 | $res |
|
|
326 | } |
298 | } |
327 | |
299 | |
328 | *cf::object::attach = |
300 | sub cf::attachable::attach { |
329 | *cf::player::attach = |
301 | if (ref $_[0]) { |
330 | *cf::map::attach = sub { |
302 | _object_attach @_; |
331 | my ($obj, $name, %arg) = @_; |
303 | } else { |
332 | |
304 | _attach shift->_attach_registry, @_; |
333 | _attach_attachment $obj, $name, %arg; |
305 | } |
334 | }; |
306 | }; |
335 | |
307 | |
336 | # all those should be optimised |
308 | # all those should be optimised |
337 | *cf::object::detach = |
309 | sub cf::attachable::detach { |
338 | *cf::player::detach = |
|
|
339 | *cf::map::detach = sub { |
|
|
340 | my ($obj, $name) = @_; |
310 | my ($obj, $name) = @_; |
341 | |
311 | |
|
|
312 | if (ref $obj) { |
342 | delete $obj->{_attachment}{$name}; |
313 | delete $obj->{_attachment}{$name}; |
343 | reattach ($obj); |
314 | reattach ($obj); |
|
|
315 | } else { |
|
|
316 | Carp::croak "cannot, currently, detach class attachments"; |
|
|
317 | } |
344 | }; |
318 | }; |
345 | |
319 | |
346 | *cf::object::attached = |
320 | sub cf::attachable::attached { |
347 | *cf::player::attached = |
|
|
348 | *cf::map::attached = sub { |
|
|
349 | my ($obj, $name) = @_; |
321 | my ($obj, $name) = @_; |
350 | |
322 | |
351 | exists $obj->{_attachment}{$name} |
323 | exists $obj->{_attachment}{$name} |
352 | }; |
|
|
353 | |
|
|
354 | sub attach_global { |
|
|
355 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
|
|
356 | } |
324 | } |
357 | |
325 | |
358 | sub attach_to_type { |
326 | for my $klass (qw(GLOBAL OBJECT PLAYER CLIENT MAP)) { |
359 | my $type = shift; |
327 | eval "#line " . __LINE__ . " 'cf.pm' |
360 | my $subtype = shift; |
328 | sub cf::\L$klass\E::_attach_registry { |
|
|
329 | (\\\@CB_$klass, KLASS_$klass) |
|
|
330 | } |
361 | |
331 | |
362 | _attach @{$CB_TYPE[$type + $subtype * NUM_SUBTYPES]}, KLASS_OBJECT, @_ |
332 | sub cf::\L$klass\E::attachment { |
363 | } |
|
|
364 | |
|
|
365 | sub attach_to_objects { |
|
|
366 | _attach @CB_OBJECT, KLASS_OBJECT, @_ |
|
|
367 | } |
|
|
368 | |
|
|
369 | sub attach_to_players { |
|
|
370 | _attach @CB_PLAYER, KLASS_PLAYER, @_ |
|
|
371 | } |
|
|
372 | |
|
|
373 | sub attach_to_maps { |
|
|
374 | _attach @CB_MAP, KLASS_MAP, @_ |
|
|
375 | } |
|
|
376 | |
|
|
377 | sub register_attachment { |
|
|
378 | my $name = shift; |
333 | my \$name = shift; |
379 | |
334 | |
380 | $attachment{$name} = [[KLASS_OBJECT, @_]]; |
|
|
381 | } |
|
|
382 | |
|
|
383 | sub register_player_attachment { |
|
|
384 | my $name = shift; |
|
|
385 | |
|
|
386 | $attachment{$name} = [[KLASS_PLAYER, @_]]; |
|
|
387 | } |
|
|
388 | |
|
|
389 | sub register_map_attachment { |
|
|
390 | my $name = shift; |
|
|
391 | |
|
|
392 | $attachment{$name} = [[KLASS_MAP, @_]]; |
335 | \$attachment{\$name} = [[KLASS_$klass, \@_]]; |
|
|
336 | } |
|
|
337 | "; |
|
|
338 | die if $@; |
393 | } |
339 | } |
394 | |
340 | |
395 | our $override; |
341 | our $override; |
396 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
342 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
397 | |
343 | |
… | |
… | |
427 | |
373 | |
428 | =item $bool = $object->invoke (EVENT_OBJECT_XXX, ...) |
374 | =item $bool = $object->invoke (EVENT_OBJECT_XXX, ...) |
429 | |
375 | |
430 | =item $bool = $player->invoke (EVENT_PLAYER_XXX, ...) |
376 | =item $bool = $player->invoke (EVENT_PLAYER_XXX, ...) |
431 | |
377 | |
|
|
378 | =item $bool = $client->invoke (EVENT_CLIENT_XXX, ...) |
|
|
379 | |
432 | =item $bool = $map->invoke (EVENT_MAP_XXX, ...) |
380 | =item $bool = $map->invoke (EVENT_MAP_XXX, ...) |
433 | |
381 | |
434 | Generate a global/object/player/map-specific event with the given arguments. |
382 | Generate a global/object/player/map-specific event with the given arguments. |
435 | |
383 | |
436 | This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be |
384 | This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be |
… | |
… | |
441 | |
389 | |
442 | =cut |
390 | =cut |
443 | |
391 | |
444 | ############################################################################# |
392 | ############################################################################# |
445 | |
393 | |
446 | =head2 METHODS VALID FOR ALL CORE OBJECTS |
394 | =head2 METHODS VALID FOR ALL ATTACHABLE OBJECTS |
|
|
395 | |
|
|
396 | Attachable objects includes objects, players, clients and maps. |
447 | |
397 | |
448 | =over 4 |
398 | =over 4 |
449 | |
399 | |
450 | =item $object->valid, $player->valid, $map->valid |
400 | =item $object->valid |
451 | |
401 | |
452 | Just because you have a perl object does not mean that the corresponding |
402 | Just because you have a perl object does not mean that the corresponding |
453 | C-level object still exists. If you try to access an object that has no |
403 | C-level object still exists. If you try to access an object that has no |
454 | valid C counterpart anymore you get an exception at runtime. This method |
404 | valid C counterpart anymore you get an exception at runtime. This method |
455 | can be used to test for existence of the C object part without causing an |
405 | can be used to test for existence of the C object part without causing an |
… | |
… | |
457 | |
407 | |
458 | =back |
408 | =back |
459 | |
409 | |
460 | =cut |
410 | =cut |
461 | |
411 | |
462 | *cf::object::valid = |
|
|
463 | *cf::player::valid = |
|
|
464 | *cf::map::valid = \&cf::_valid; |
|
|
465 | |
|
|
466 | ############################################################################# |
412 | ############################################################################# |
467 | # object support |
413 | # object support |
468 | |
414 | |
469 | sub instantiate { |
415 | sub instantiate { |
470 | my ($obj, $data) = @_; |
416 | my ($obj, $data) = @_; |
… | |
… | |
489 | |
435 | |
490 | for my $name (keys %{ $obj->{_attachment} || {} }) { |
436 | for my $name (keys %{ $obj->{_attachment} || {} }) { |
491 | if (my $attach = $attachment{$name}) { |
437 | if (my $attach = $attachment{$name}) { |
492 | for (@$attach) { |
438 | for (@$attach) { |
493 | my ($klass, @attach) = @$_; |
439 | my ($klass, @attach) = @$_; |
494 | _attach @$registry, $klass, @attach; |
440 | _attach $registry, $klass, @attach; |
495 | } |
441 | } |
496 | } else { |
442 | } else { |
497 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
443 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
498 | } |
444 | } |
499 | } |
445 | } |
… | |
… | |
555 | } |
501 | } |
556 | |
502 | |
557 | () |
503 | () |
558 | } |
504 | } |
559 | |
505 | |
560 | attach_to_objects |
506 | cf::object->attach ( |
561 | prio => -1000000, |
507 | prio => -1000000, |
562 | on_clone => sub { |
508 | on_clone => sub { |
563 | my ($src, $dst) = @_; |
509 | my ($src, $dst) = @_; |
564 | |
510 | |
565 | @{$dst->registry} = @{$src->registry}; |
511 | @{$dst->registry} = @{$src->registry}; |
… | |
… | |
567 | %$dst = %$src; |
513 | %$dst = %$src; |
568 | |
514 | |
569 | %{$dst->{_attachment}} = %{$src->{_attachment}} |
515 | %{$dst->{_attachment}} = %{$src->{_attachment}} |
570 | if exists $src->{_attachment}; |
516 | if exists $src->{_attachment}; |
571 | }, |
517 | }, |
572 | ; |
518 | ); |
573 | |
519 | |
574 | ############################################################################# |
520 | ############################################################################# |
575 | # old plug-in events |
521 | # command handling &c |
576 | |
522 | |
577 | sub inject_event { |
523 | =item cf::register_command $name => \&callback($ob,$args); |
578 | my $extension = shift; |
|
|
579 | my $event_code = shift; |
|
|
580 | |
524 | |
581 | my $cb = $hook[$event_code]{$extension} |
525 | Register a callback for execution when the client sends the user command |
582 | or return; |
526 | $name. |
583 | |
527 | |
584 | &$cb |
528 | =cut |
585 | } |
|
|
586 | |
|
|
587 | sub inject_global_event { |
|
|
588 | my $event = shift; |
|
|
589 | |
|
|
590 | my $cb = $hook[$event] |
|
|
591 | or return; |
|
|
592 | |
|
|
593 | List::Util::max map &$_, values %$cb |
|
|
594 | } |
|
|
595 | |
|
|
596 | sub inject_command { |
|
|
597 | my ($name, $obj, $params) = @_; |
|
|
598 | |
|
|
599 | for my $cmd (@{ $command{$name} }) { |
|
|
600 | $cmd->[1]->($obj, $params); |
|
|
601 | } |
|
|
602 | |
|
|
603 | -1 |
|
|
604 | } |
|
|
605 | |
529 | |
606 | sub register_command { |
530 | sub register_command { |
607 | my ($name, $time, $cb) = @_; |
531 | my ($name, $cb) = @_; |
608 | |
532 | |
609 | my $caller = caller; |
533 | my $caller = caller; |
610 | #warn "registering command '$name/$time' to '$caller'"; |
534 | #warn "registering command '$name/$time' to '$caller'"; |
611 | |
535 | |
612 | push @{ $command{$name} }, [$time, $cb, $caller]; |
536 | push @{ $COMMAND{$name} }, [$caller, $cb]; |
613 | $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} }; |
|
|
614 | } |
537 | } |
|
|
538 | |
|
|
539 | =item cf::register_extcmd $name => \&callback($pl,$packet); |
|
|
540 | |
|
|
541 | Register a callbackf ro execution when the client sends an extcmd packet. |
|
|
542 | |
|
|
543 | If the callback returns something, it is sent back as if reply was being |
|
|
544 | called. |
|
|
545 | |
|
|
546 | =cut |
615 | |
547 | |
616 | sub register_extcmd { |
548 | sub register_extcmd { |
617 | my ($name, $cb) = @_; |
549 | my ($name, $cb) = @_; |
618 | |
550 | |
619 | my $caller = caller; |
551 | my $caller = caller; |
620 | #warn "registering extcmd '$name' to '$caller'"; |
552 | #warn "registering extcmd '$name' to '$caller'"; |
621 | |
553 | |
622 | $extcmd{$name} = [$cb, $caller]; |
554 | $EXTCMD{$name} = [$cb, $caller]; |
623 | } |
555 | } |
|
|
556 | |
|
|
557 | cf::player->attach ( |
|
|
558 | on_command => sub { |
|
|
559 | my ($pl, $name, $params) = @_; |
|
|
560 | |
|
|
561 | my $cb = $COMMAND{$name} |
|
|
562 | or return; |
|
|
563 | |
|
|
564 | for my $cmd (@$cb) { |
|
|
565 | $cmd->[1]->($pl->ob, $params); |
|
|
566 | } |
|
|
567 | |
|
|
568 | cf::override; |
|
|
569 | }, |
|
|
570 | on_extcmd => sub { |
|
|
571 | my ($pl, $buf) = @_; |
|
|
572 | |
|
|
573 | my $msg = eval { from_json $buf }; |
|
|
574 | |
|
|
575 | if (ref $msg) { |
|
|
576 | if (my $cb = $EXTCMD{$msg->{msgtype}}) { |
|
|
577 | if (my %reply = $cb->[0]->($pl, $msg)) { |
|
|
578 | $pl->ext_reply ($msg->{msgid}, %reply); |
|
|
579 | } |
|
|
580 | } |
|
|
581 | } else { |
|
|
582 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
|
|
583 | } |
|
|
584 | |
|
|
585 | cf::override; |
|
|
586 | }, |
|
|
587 | ); |
624 | |
588 | |
625 | sub register { |
589 | sub register { |
626 | my ($base, $pkg) = @_; |
590 | my ($base, $pkg) = @_; |
627 | |
591 | |
628 | #TODO |
592 | #TODO |
… | |
… | |
671 | # for my $idx (0 .. $#PLUGIN_EVENT) { |
635 | # for my $idx (0 .. $#PLUGIN_EVENT) { |
672 | # delete $hook[$idx]{$pkg}; |
636 | # delete $hook[$idx]{$pkg}; |
673 | # } |
637 | # } |
674 | |
638 | |
675 | # remove commands |
639 | # remove commands |
676 | for my $name (keys %command) { |
640 | for my $name (keys %COMMAND) { |
677 | my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; |
641 | my @cb = grep $_->[0] ne $pkg, @{ $COMMAND{$name} }; |
678 | |
642 | |
679 | if (@cb) { |
643 | if (@cb) { |
680 | $command{$name} = \@cb; |
644 | $COMMAND{$name} = \@cb; |
681 | $COMMAND{"$name\000"} = List::Util::max map $_->[0], @cb; |
|
|
682 | } else { |
645 | } else { |
683 | delete $command{$name}; |
|
|
684 | delete $COMMAND{"$name\000"}; |
646 | delete $COMMAND{$name}; |
685 | } |
647 | } |
686 | } |
648 | } |
687 | |
649 | |
688 | # remove extcmds |
650 | # remove extcmds |
689 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
651 | for my $name (grep $EXTCMD{$_}[1] eq $pkg, keys %EXTCMD) { |
690 | delete $extcmd{$name}; |
652 | delete $EXTCMD{$name}; |
691 | } |
653 | } |
692 | |
654 | |
693 | if (my $cb = $pkg->can ("unload")) { |
655 | if (my $cb = $pkg->can ("unload")) { |
694 | eval { |
656 | eval { |
695 | $cb->($pkg); |
657 | $cb->($pkg); |
… | |
… | |
699 | |
661 | |
700 | Symbol::delete_package $pkg; |
662 | Symbol::delete_package $pkg; |
701 | } |
663 | } |
702 | |
664 | |
703 | sub load_extensions { |
665 | sub load_extensions { |
704 | my $LIBDIR = maps_directory "perl"; |
|
|
705 | |
|
|
706 | for my $ext (<$LIBDIR/*.ext>) { |
666 | for my $ext (<$LIBDIR/*.ext>) { |
707 | next unless -r $ext; |
667 | next unless -r $ext; |
708 | eval { |
668 | eval { |
709 | load_extension $ext; |
669 | load_extension $ext; |
710 | 1 |
670 | 1 |
711 | } or warn "$ext not loaded: $@"; |
671 | } or warn "$ext not loaded: $@"; |
712 | } |
672 | } |
713 | } |
673 | } |
714 | |
674 | |
715 | ############################################################################# |
675 | ############################################################################# |
716 | # extcmd framework, basically convert ext <msg> |
|
|
717 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
|
|
718 | |
|
|
719 | attach_to_players |
|
|
720 | on_extcmd => sub { |
|
|
721 | my ($pl, $buf) = @_; |
|
|
722 | |
|
|
723 | my $msg = eval { from_json $buf }; |
|
|
724 | |
|
|
725 | if (ref $msg) { |
|
|
726 | if (my $cb = $extcmd{$msg->{msgtype}}) { |
|
|
727 | if (my %reply = $cb->[0]->($pl, $msg)) { |
|
|
728 | $pl->ext_reply ($msg->{msgid}, %reply); |
|
|
729 | } |
|
|
730 | } |
|
|
731 | } else { |
|
|
732 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
|
|
733 | } |
|
|
734 | |
|
|
735 | cf::override; |
|
|
736 | }, |
|
|
737 | ; |
|
|
738 | |
|
|
739 | ############################################################################# |
|
|
740 | # load/save/clean perl data associated with a map |
676 | # load/save/clean perl data associated with a map |
741 | |
677 | |
742 | *cf::mapsupport::on_clean = sub { |
678 | *cf::mapsupport::on_clean = sub { |
743 | my ($map) = @_; |
679 | my ($map) = @_; |
744 | |
680 | |
… | |
… | |
746 | defined $path or return; |
682 | defined $path or return; |
747 | |
683 | |
748 | unlink "$path.pst"; |
684 | unlink "$path.pst"; |
749 | }; |
685 | }; |
750 | |
686 | |
751 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
687 | cf::map->attach (prio => -10000, package => cf::mapsupport::); |
752 | |
688 | |
753 | ############################################################################# |
689 | ############################################################################# |
754 | # load/save perl data associated with player->ob objects |
690 | # load/save perl data associated with player->ob objects |
755 | |
691 | |
756 | sub all_objects(@) { |
692 | sub all_objects(@) { |
757 | @_, map all_objects ($_->inv), @_ |
693 | @_, map all_objects ($_->inv), @_ |
758 | } |
694 | } |
759 | |
695 | |
760 | # TODO: compatibility cruft, remove when no longer needed |
696 | # TODO: compatibility cruft, remove when no longer needed |
761 | attach_to_players |
697 | cf::player->attach ( |
762 | on_load => sub { |
698 | on_load => sub { |
763 | my ($pl, $path) = @_; |
699 | my ($pl, $path) = @_; |
764 | |
700 | |
765 | for my $o (all_objects $pl->ob) { |
701 | for my $o (all_objects $pl->ob) { |
766 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
702 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
… | |
… | |
768 | |
704 | |
769 | %$o = %{ Storable::thaw pack "H*", $value }; |
705 | %$o = %{ Storable::thaw pack "H*", $value }; |
770 | } |
706 | } |
771 | } |
707 | } |
772 | }, |
708 | }, |
773 | ; |
709 | ); |
774 | |
710 | |
775 | ############################################################################# |
711 | ############################################################################# |
776 | |
712 | |
777 | =head2 CORE EXTENSIONS |
713 | =head2 CORE EXTENSIONS |
778 | |
714 | |
… | |
… | |
869 | |
805 | |
870 | =pod |
806 | =pod |
871 | |
807 | |
872 | The following fucntions and emthods are available within a safe environment: |
808 | The following fucntions and emthods are available within a safe environment: |
873 | |
809 | |
874 | cf::object contr pay_amount pay_player |
810 | cf::object contr pay_amount pay_player map |
875 | cf::object::player player |
811 | cf::object::player player |
876 | cf::player peaceful |
812 | cf::player peaceful |
|
|
813 | cf::map trigger |
877 | |
814 | |
878 | =cut |
815 | =cut |
879 | |
816 | |
880 | for ( |
817 | for ( |
881 | ["cf::object" => qw(contr pay_amount pay_player)], |
818 | ["cf::object" => qw(contr pay_amount pay_player map)], |
882 | ["cf::object::player" => qw(player)], |
819 | ["cf::object::player" => qw(player)], |
883 | ["cf::player" => qw(peaceful)], |
820 | ["cf::player" => qw(peaceful)], |
|
|
821 | ["cf::map" => qw(trigger)], |
884 | ) { |
822 | ) { |
885 | no strict 'refs'; |
823 | no strict 'refs'; |
886 | my ($pkg, @funs) = @$_; |
824 | my ($pkg, @funs) = @$_; |
887 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
825 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
888 | for @funs; |
826 | for @funs; |
… | |
… | |
1028 | sub db_sync() { |
966 | sub db_sync() { |
1029 | db_save if $dirty; |
967 | db_save if $dirty; |
1030 | undef $dirty; |
968 | undef $dirty; |
1031 | } |
969 | } |
1032 | |
970 | |
1033 | my $idle = Event->idle (min => $TICK * 2.8, max => 10, repeat => 0, cb => sub { |
971 | my $idle = Event->idle (min => $TICK * 2.8, max => 10, repeat => 0, data => WF_AUTOCANCEL, cb => sub { |
1034 | db_sync; |
972 | db_sync; |
1035 | }); |
973 | }); |
1036 | |
974 | |
1037 | sub db_dirty() { |
975 | sub db_dirty() { |
1038 | $dirty = 1; |
976 | $dirty = 1; |
… | |
… | |
1052 | $DB->{$_[0]} = $_[1]; |
990 | $DB->{$_[0]} = $_[1]; |
1053 | } |
991 | } |
1054 | db_dirty; |
992 | db_dirty; |
1055 | } |
993 | } |
1056 | |
994 | |
1057 | attach_global |
995 | cf::global->attach ( |
1058 | prio => 10000, |
996 | prio => 10000, |
1059 | on_cleanup => sub { |
997 | on_cleanup => sub { |
1060 | db_sync; |
998 | db_sync; |
1061 | }, |
999 | }, |
1062 | ; |
1000 | ); |
1063 | } |
1001 | } |
1064 | |
1002 | |
1065 | ############################################################################# |
1003 | ############################################################################# |
1066 | # the server's main() |
1004 | # the server's main() |
1067 | |
1005 | |
… | |
… | |
1088 | |
1026 | |
1089 | $msg->("reloading..."); |
1027 | $msg->("reloading..."); |
1090 | |
1028 | |
1091 | eval { |
1029 | eval { |
1092 | # cancel all watchers |
1030 | # cancel all watchers |
1093 | $_->cancel for Event::all_watchers; |
1031 | for (Event::all_watchers) { |
|
|
1032 | $_->cancel if $_->data & WF_AUTOCANCEL; |
|
|
1033 | } |
1094 | |
1034 | |
1095 | # unload all extensions |
1035 | # unload all extensions |
1096 | for (@exts) { |
1036 | for (@exts) { |
1097 | $msg->("unloading <$_>"); |
1037 | $msg->("unloading <$_>"); |
1098 | unload_extension $_; |
1038 | unload_extension $_; |
… | |
… | |
1158 | warn $_[0]; |
1098 | warn $_[0]; |
1159 | print "$_[0]\n"; |
1099 | print "$_[0]\n"; |
1160 | }; |
1100 | }; |
1161 | } |
1101 | } |
1162 | |
1102 | |
|
|
1103 | register "<global>", __PACKAGE__; |
|
|
1104 | |
1163 | register_command "perl-reload", 0, sub { |
1105 | register_command "perl-reload" => sub { |
1164 | my ($who, $arg) = @_; |
1106 | my ($who, $arg) = @_; |
1165 | |
1107 | |
1166 | if ($who->flag (FLAG_WIZ)) { |
1108 | if ($who->flag (FLAG_WIZ)) { |
1167 | _perl_reload { |
1109 | _perl_reload { |
1168 | warn $_[0]; |
1110 | warn $_[0]; |
1169 | $who->message ($_[0]); |
1111 | $who->message ($_[0]); |
1170 | }; |
1112 | }; |
1171 | } |
1113 | } |
1172 | }; |
1114 | }; |
1173 | |
1115 | |
1174 | register "<global>", __PACKAGE__; |
|
|
1175 | |
|
|
1176 | unshift @INC, $LIBDIR; |
1116 | unshift @INC, $LIBDIR; |
1177 | |
1117 | |
1178 | $TICK_WATCHER = Event->timer ( |
1118 | $TICK_WATCHER = Event->timer ( |
1179 | prio => 1, |
1119 | prio => 0, |
1180 | async => 1, |
|
|
1181 | at => $NEXT_TICK || 1, |
1120 | at => $NEXT_TICK || 1, |
|
|
1121 | data => WF_AUTOCANCEL, |
1182 | cb => sub { |
1122 | cb => sub { |
1183 | cf::server_tick; # one server iteration |
1123 | cf::server_tick; # one server iteration |
1184 | |
1124 | |
1185 | my $NOW = Event::time; |
1125 | my $NOW = Event::time; |
1186 | $NEXT_TICK += $TICK; |
1126 | $NEXT_TICK += $TICK; |
… | |
… | |
1196 | IO::AIO::max_poll_time $TICK * 0.2; |
1136 | IO::AIO::max_poll_time $TICK * 0.2; |
1197 | |
1137 | |
1198 | Event->io (fd => IO::AIO::poll_fileno, |
1138 | Event->io (fd => IO::AIO::poll_fileno, |
1199 | poll => 'r', |
1139 | poll => 'r', |
1200 | prio => 5, |
1140 | prio => 5, |
|
|
1141 | data => WF_AUTOCANCEL, |
1201 | cb => \&IO::AIO::poll_cb); |
1142 | cb => \&IO::AIO::poll_cb); |
1202 | |
1143 | |
1203 | 1 |
1144 | 1 |
1204 | |
1145 | |