1 | package cf; |
1 | package cf; |
|
|
2 | |
|
|
3 | use utf8; |
|
|
4 | use strict; |
2 | |
5 | |
3 | use Symbol; |
6 | use Symbol; |
4 | use List::Util; |
7 | use List::Util; |
5 | use Storable; |
8 | use Storable; |
6 | use Opcode; |
9 | use Opcode; |
7 | use Safe; |
10 | use Safe; |
8 | use Safe::Hole; |
11 | use Safe::Hole; |
9 | |
12 | |
|
|
13 | use Coro; |
|
|
14 | use Coro::Event; |
|
|
15 | use Coro::Timer; |
|
|
16 | use Coro::Signal; |
|
|
17 | use Coro::Semaphore; |
|
|
18 | |
10 | use IO::AIO (); |
19 | use IO::AIO; |
11 | use YAML::Syck (); |
20 | use YAML::Syck (); |
12 | use Time::HiRes; |
21 | use Time::HiRes; |
13 | use Event; |
22 | |
14 | $Event::Eval = 1; # no idea why this is required, but it is |
23 | use Event; $Event::Eval = 1; # no idea why this is required, but it is |
15 | |
24 | |
16 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
25 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
17 | $YAML::Syck::ImplicitUnicode = 1; |
26 | $YAML::Syck::ImplicitUnicode = 1; |
18 | |
27 | |
19 | use strict; |
28 | $Coro::main->prio (Coro::PRIO_MIN); |
20 | |
29 | |
21 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
30 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
22 | |
31 | |
23 | our %COMMAND = (); |
32 | our %COMMAND = (); |
24 | our %COMMAND_TIME = (); |
33 | our %COMMAND_TIME = (); |
… | |
… | |
74 | print STDERR "cfperl: $msg"; |
83 | print STDERR "cfperl: $msg"; |
75 | LOG llevError, "cfperl: $msg"; |
84 | LOG llevError, "cfperl: $msg"; |
76 | }; |
85 | }; |
77 | } |
86 | } |
78 | |
87 | |
|
|
88 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
|
|
89 | @safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; |
|
|
90 | @safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable'; |
|
|
91 | @safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable'; |
|
|
92 | @safe::cf::map::ISA = @cf::map::ISA = 'cf::attachable'; |
79 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
93 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
80 | |
94 | |
81 | # we bless all objects into (empty) derived classes to force a method lookup |
95 | # we bless all objects into (empty) derived classes to force a method lookup |
82 | # within the Safe compartment. |
96 | # within the Safe compartment. |
83 | for my $pkg (qw( |
97 | for my $pkg (qw( |
|
|
98 | cf::global |
84 | cf::object cf::object::player |
99 | cf::object cf::object::player |
85 | cf::client cf::player |
100 | cf::client cf::player |
86 | cf::arch cf::living |
101 | cf::arch cf::living |
87 | cf::map cf::party cf::region |
102 | cf::map cf::party cf::region |
88 | )) { |
103 | )) { |
… | |
… | |
132 | |
147 | |
133 | =cut |
148 | =cut |
134 | |
149 | |
135 | ############################################################################# |
150 | ############################################################################# |
136 | |
151 | |
137 | =head2 EVENTS AND OBJECT ATTACHMENTS |
152 | =head2 ATTACHABLE OBJECTS |
|
|
153 | |
|
|
154 | Many objects in crossfire are so-called attachable objects. That means you can |
|
|
155 | attach callbacks/event handlers (a collection of which is called an "attachment") |
|
|
156 | to it. All such attachable objects support the following methods. |
|
|
157 | |
|
|
158 | In the following description, CLASS can be any of C<global>, C<object> |
|
|
159 | C<player>, C<client> or C<map> (i.e. the attachable objects in |
|
|
160 | crossfire+). |
138 | |
161 | |
139 | =over 4 |
162 | =over 4 |
140 | |
163 | |
141 | =item $object->attach ($attachment, key => $value...) |
|
|
142 | |
|
|
143 | =item $object->detach ($attachment) |
|
|
144 | |
|
|
145 | Attach/detach a pre-registered attachment to an object. |
|
|
146 | |
|
|
147 | =item $player->attach ($attachment, key => $value...) |
|
|
148 | |
|
|
149 | =item $player->detach ($attachment) |
|
|
150 | |
|
|
151 | Attach/detach a pre-registered attachment to a player. |
|
|
152 | |
|
|
153 | =item $map->attach ($attachment, key => $value...) |
164 | =item $attachable->attach ($attachment, key => $value...) |
154 | |
165 | |
155 | =item $map->detach ($attachment) |
166 | =item $attachable->detach ($attachment) |
156 | |
167 | |
157 | Attach/detach a pre-registered attachment to a map. |
168 | Attach/detach a pre-registered attachment to a specific object and give it |
|
|
169 | the specified key/value pairs as arguments. |
158 | |
170 | |
159 | =item $bool = $object->attached ($name) |
171 | Example, attach a minesweeper attachment to the given object, making it a |
|
|
172 | 10x10 minesweeper game: |
160 | |
173 | |
161 | =item $bool = $player->attached ($name) |
174 | $obj->attach (minesweeper => width => 10, height => 10); |
162 | |
175 | |
163 | =item $bool = $map->attached ($name) |
176 | =item $bool = $attachable->attached ($name) |
164 | |
177 | |
165 | Checks wether the named attachment is currently attached to the object. |
178 | Checks wether the named attachment is currently attached to the object. |
166 | |
179 | |
167 | =item cf::attach_global ... |
180 | =item cf::CLASS->attach ... |
168 | |
181 | |
169 | Attach handlers for global events. |
182 | =item cf::CLASS->detach ... |
170 | |
183 | |
171 | This and all following C<attach_*>-functions expect any number of the |
184 | Define an anonymous attachment and attach it to all objects of the given |
172 | following handler/hook descriptions: |
185 | CLASS. See the next function for an explanation of its arguments. |
|
|
186 | |
|
|
187 | You can attach to global events by using the C<cf::global> class. |
|
|
188 | |
|
|
189 | Example, log all player logins: |
|
|
190 | |
|
|
191 | cf::player->attach ( |
|
|
192 | on_login => sub { |
|
|
193 | my ($pl) = @_; |
|
|
194 | ... |
|
|
195 | }, |
|
|
196 | ); |
|
|
197 | |
|
|
198 | Example, attach to the jeweler skill: |
|
|
199 | |
|
|
200 | cf::object->attach ( |
|
|
201 | type => cf::SKILL, |
|
|
202 | subtype => cf::SK_JEWELER, |
|
|
203 | on_use_skill => sub { |
|
|
204 | my ($sk, $ob, $part, $dir, $msg) = @_; |
|
|
205 | ... |
|
|
206 | }, |
|
|
207 | ); |
|
|
208 | |
|
|
209 | =item cf::CLASS::attachment $name, ... |
|
|
210 | |
|
|
211 | Register an attachment by C<$name> through which attachable objects of the |
|
|
212 | given CLASS can refer to this attachment. |
|
|
213 | |
|
|
214 | Some classes such as crossfire maps and objects can specify attachments |
|
|
215 | that are attached at load/instantiate time, thus the need for a name. |
|
|
216 | |
|
|
217 | These calls expect any number of the following handler/hook descriptions: |
173 | |
218 | |
174 | =over 4 |
219 | =over 4 |
175 | |
220 | |
176 | =item prio => $number |
221 | =item prio => $number |
177 | |
222 | |
… | |
… | |
179 | by another C<prio> setting). Lower priority handlers get executed |
224 | by another C<prio> setting). Lower priority handlers get executed |
180 | earlier. The default priority is C<0>, and many built-in handlers are |
225 | earlier. The default priority is C<0>, and many built-in handlers are |
181 | registered at priority C<-1000>, so lower priorities should not be used |
226 | registered at priority C<-1000>, so lower priorities should not be used |
182 | unless you know what you are doing. |
227 | unless you know what you are doing. |
183 | |
228 | |
|
|
229 | =item type => $type |
|
|
230 | |
|
|
231 | (Only for C<< cf::object->attach >> calls), limits the attachment to the |
|
|
232 | given type of objects only (the additional parameter C<subtype> can be |
|
|
233 | used to further limit to the given subtype). |
|
|
234 | |
184 | =item on_I<event> => \&cb |
235 | =item on_I<event> => \&cb |
185 | |
236 | |
186 | Call the given code reference whenever the named event happens (event is |
237 | Call the given code reference whenever the named event happens (event is |
187 | something like C<instantiate>, C<apply>, C<use_skill> and so on, and which |
238 | something like C<instantiate>, C<apply>, C<use_skill> and so on, and which |
188 | handlers are recognised generally depends on the type of object these |
239 | handlers are recognised generally depends on the type of object these |
… | |
… | |
197 | package and register them. Only handlers for eevents supported by the |
248 | package and register them. Only handlers for eevents supported by the |
198 | object/class are recognised. |
249 | object/class are recognised. |
199 | |
250 | |
200 | =back |
251 | =back |
201 | |
252 | |
202 | =item cf::attach_to_type $object_type, $subtype, ... |
253 | Example, define an attachment called "sockpuppet" that calls the given |
|
|
254 | event handler when a monster attacks: |
203 | |
255 | |
204 | Attach handlers for a specific object type (e.g. TRANSPORT) and |
256 | cf::object::attachment sockpuppet => |
205 | subtype. If C<$subtype> is zero or undef, matches all objects of the given |
257 | on_skill_attack => sub { |
206 | type. |
258 | my ($self, $victim) = @_; |
207 | |
259 | ... |
208 | =item cf::attach_to_objects ... |
|
|
209 | |
|
|
210 | Attach handlers to all objects. Do not use this except for debugging or |
|
|
211 | very rare events, as handlers are (obviously) called for I<all> objects in |
|
|
212 | the game. |
|
|
213 | |
|
|
214 | =item cf::attach_to_players ... |
|
|
215 | |
|
|
216 | Attach handlers to all players. |
|
|
217 | |
|
|
218 | =item cf::attach_to_maps ... |
|
|
219 | |
|
|
220 | Attach handlers to all maps. |
|
|
221 | |
|
|
222 | =item cf:register_attachment $name, ... |
|
|
223 | |
|
|
224 | Register an attachment by name through which objects can refer to this |
|
|
225 | attachment. |
|
|
226 | |
|
|
227 | =item cf:register_player_attachment $name, ... |
|
|
228 | |
|
|
229 | Register an attachment by name through which players can refer to this |
|
|
230 | attachment. |
|
|
231 | |
|
|
232 | =item cf:register_map_attachment $name, ... |
|
|
233 | |
|
|
234 | Register an attachment by name through which maps can refer to this |
|
|
235 | attachment. |
|
|
236 | |
|
|
237 | =cut |
|
|
238 | |
|
|
239 | # the following variables are defined in .xs and must not be re-created |
|
|
240 | our @CB_GLOBAL = (); # registry for all global events |
|
|
241 | our @CB_OBJECT = (); # all objects (should not be used except in emergency) |
|
|
242 | our @CB_PLAYER = (); |
|
|
243 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
|
|
244 | our @CB_MAP = (); |
|
|
245 | |
|
|
246 | my %attachment; |
|
|
247 | |
|
|
248 | sub _attach_cb($\%$$$) { |
|
|
249 | my ($registry, $undo, $event, $prio, $cb) = @_; |
|
|
250 | |
|
|
251 | use sort 'stable'; |
|
|
252 | |
|
|
253 | $cb = [$prio, $cb]; |
|
|
254 | |
|
|
255 | @{$registry->[$event]} = sort |
|
|
256 | { $a->[0] cmp $b->[0] } |
|
|
257 | @{$registry->[$event] || []}, $cb; |
|
|
258 | |
|
|
259 | push @{$undo->{cb}}, [$event, $cb]; |
|
|
260 | } |
|
|
261 | |
|
|
262 | # attach handles attaching event callbacks |
|
|
263 | # the only thing the caller has to do is pass the correct |
|
|
264 | # registry (== where the callback attaches to). |
|
|
265 | sub _attach(\@$@) { |
|
|
266 | my ($registry, $klass, @arg) = @_; |
|
|
267 | |
|
|
268 | my $prio = 0; |
|
|
269 | |
|
|
270 | my %undo = ( |
|
|
271 | registry => $registry, |
|
|
272 | cb => [], |
|
|
273 | ); |
|
|
274 | |
|
|
275 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
|
|
276 | |
|
|
277 | while (@arg) { |
|
|
278 | my $type = shift @arg; |
|
|
279 | |
|
|
280 | if ($type eq "prio") { |
|
|
281 | $prio = shift @arg; |
|
|
282 | |
|
|
283 | } elsif ($type eq "package") { |
|
|
284 | my $pkg = shift @arg; |
|
|
285 | |
|
|
286 | while (my ($name, $id) = each %cb_id) { |
|
|
287 | if (my $cb = $pkg->can ($name)) { |
|
|
288 | _attach_cb $registry, %undo, $id, $prio, $cb; |
|
|
289 | } |
|
|
290 | } |
260 | } |
291 | |
|
|
292 | } elsif (exists $cb_id{$type}) { |
|
|
293 | _attach_cb $registry, %undo, $cb_id{$type}, $prio, shift @arg; |
|
|
294 | |
|
|
295 | } elsif (ref $type) { |
|
|
296 | warn "attaching objects not supported, ignoring.\n"; |
|
|
297 | |
|
|
298 | } else { |
|
|
299 | shift @arg; |
|
|
300 | warn "attach argument '$type' not supported, ignoring.\n"; |
|
|
301 | } |
|
|
302 | } |
|
|
303 | |
|
|
304 | \%undo |
|
|
305 | } |
|
|
306 | |
|
|
307 | sub _attach_attachment { |
|
|
308 | my ($obj, $name, %arg) = @_; |
|
|
309 | |
|
|
310 | return if exists $obj->{_attachment}{$name}; |
|
|
311 | |
|
|
312 | my $res; |
|
|
313 | |
|
|
314 | if (my $attach = $attachment{$name}) { |
|
|
315 | my $registry = $obj->registry; |
|
|
316 | |
|
|
317 | for (@$attach) { |
|
|
318 | my ($klass, @attach) = @$_; |
|
|
319 | $res = _attach @$registry, $klass, @attach; |
|
|
320 | } |
|
|
321 | |
|
|
322 | $obj->{$name} = \%arg; |
|
|
323 | } else { |
|
|
324 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
|
|
325 | } |
|
|
326 | |
|
|
327 | $obj->{_attachment}{$name} = undef; |
|
|
328 | |
|
|
329 | $res->{attachment} = $name; |
|
|
330 | $res |
|
|
331 | } |
|
|
332 | |
|
|
333 | *cf::object::attach = |
|
|
334 | *cf::player::attach = |
|
|
335 | *cf::map::attach = sub { |
|
|
336 | my ($obj, $name, %arg) = @_; |
|
|
337 | |
|
|
338 | _attach_attachment $obj, $name, %arg; |
|
|
339 | }; |
|
|
340 | |
|
|
341 | # all those should be optimised |
|
|
342 | *cf::object::detach = |
|
|
343 | *cf::player::detach = |
|
|
344 | *cf::map::detach = sub { |
|
|
345 | my ($obj, $name) = @_; |
|
|
346 | |
|
|
347 | delete $obj->{_attachment}{$name}; |
|
|
348 | reattach ($obj); |
|
|
349 | }; |
|
|
350 | |
|
|
351 | *cf::object::attached = |
|
|
352 | *cf::player::attached = |
|
|
353 | *cf::map::attached = sub { |
|
|
354 | my ($obj, $name) = @_; |
|
|
355 | |
|
|
356 | exists $obj->{_attachment}{$name} |
|
|
357 | }; |
|
|
358 | |
|
|
359 | sub attach_global { |
|
|
360 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
|
|
361 | } |
|
|
362 | |
|
|
363 | sub attach_to_type { |
|
|
364 | my $type = shift; |
|
|
365 | my $subtype = shift; |
|
|
366 | |
|
|
367 | _attach @{$CB_TYPE[$type + $subtype * NUM_SUBTYPES]}, KLASS_OBJECT, @_ |
|
|
368 | } |
|
|
369 | |
|
|
370 | sub attach_to_objects { |
|
|
371 | _attach @CB_OBJECT, KLASS_OBJECT, @_ |
|
|
372 | } |
|
|
373 | |
|
|
374 | sub attach_to_players { |
|
|
375 | _attach @CB_PLAYER, KLASS_PLAYER, @_ |
|
|
376 | } |
|
|
377 | |
|
|
378 | sub attach_to_maps { |
|
|
379 | _attach @CB_MAP, KLASS_MAP, @_ |
|
|
380 | } |
|
|
381 | |
|
|
382 | sub register_attachment { |
|
|
383 | my $name = shift; |
|
|
384 | |
|
|
385 | $attachment{$name} = [[KLASS_OBJECT, @_]]; |
|
|
386 | } |
|
|
387 | |
|
|
388 | sub register_player_attachment { |
|
|
389 | my $name = shift; |
|
|
390 | |
|
|
391 | $attachment{$name} = [[KLASS_PLAYER, @_]]; |
|
|
392 | } |
|
|
393 | |
|
|
394 | sub register_map_attachment { |
|
|
395 | my $name = shift; |
|
|
396 | |
|
|
397 | $attachment{$name} = [[KLASS_MAP, @_]]; |
|
|
398 | } |
|
|
399 | |
|
|
400 | our $override; |
|
|
401 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
|
|
402 | |
|
|
403 | sub override { |
|
|
404 | $override = 1; |
|
|
405 | @invoke_results = (); |
|
|
406 | } |
|
|
407 | |
|
|
408 | sub do_invoke { |
|
|
409 | my $event = shift; |
|
|
410 | my $callbacks = shift; |
|
|
411 | |
|
|
412 | @invoke_results = (); |
|
|
413 | |
|
|
414 | local $override; |
|
|
415 | |
|
|
416 | for (@$callbacks) { |
|
|
417 | eval { &{$_->[1]} }; |
|
|
418 | |
|
|
419 | if ($@) { |
|
|
420 | warn "$@"; |
|
|
421 | warn "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n"; |
|
|
422 | override; |
|
|
423 | } |
|
|
424 | |
|
|
425 | return 1 if $override; |
|
|
426 | } |
|
|
427 | |
|
|
428 | 0 |
261 | } |
429 | } |
|
|
430 | |
262 | |
431 | =item $bool = cf::invoke EVENT_GLOBAL_XXX, ... |
263 | =item $attachable->valid |
432 | |
|
|
433 | =item $bool = $object->invoke (EVENT_OBJECT_XXX, ...) |
|
|
434 | |
|
|
435 | =item $bool = $player->invoke (EVENT_PLAYER_XXX, ...) |
|
|
436 | |
|
|
437 | =item $bool = $map->invoke (EVENT_MAP_XXX, ...) |
|
|
438 | |
|
|
439 | Generate a global/object/player/map-specific event with the given arguments. |
|
|
440 | |
|
|
441 | This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be |
|
|
442 | removed in future versions), and there is no public API to access override |
|
|
443 | results (if you must, access C<@cf::invoke_results> directly). |
|
|
444 | |
|
|
445 | =back |
|
|
446 | |
|
|
447 | =cut |
|
|
448 | |
|
|
449 | ############################################################################# |
|
|
450 | |
|
|
451 | =head2 METHODS VALID FOR ALL CORE OBJECTS |
|
|
452 | |
|
|
453 | =over 4 |
|
|
454 | |
|
|
455 | =item $object->valid, $player->valid, $map->valid |
|
|
456 | |
264 | |
457 | Just because you have a perl object does not mean that the corresponding |
265 | Just because you have a perl object does not mean that the corresponding |
458 | C-level object still exists. If you try to access an object that has no |
266 | C-level object still exists. If you try to access an object that has no |
459 | valid C counterpart anymore you get an exception at runtime. This method |
267 | valid C counterpart anymore you get an exception at runtime. This method |
460 | can be used to test for existence of the C object part without causing an |
268 | can be used to test for existence of the C object part without causing an |
461 | exception. |
269 | exception. |
462 | |
270 | |
|
|
271 | =cut |
|
|
272 | |
|
|
273 | # the following variables are defined in .xs and must not be re-created |
|
|
274 | our @CB_GLOBAL = (); # registry for all global events |
|
|
275 | our @CB_OBJECT = (); # all objects (should not be used except in emergency) |
|
|
276 | our @CB_PLAYER = (); |
|
|
277 | our @CB_CLIENT = (); |
|
|
278 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
|
|
279 | our @CB_MAP = (); |
|
|
280 | |
|
|
281 | my %attachment; |
|
|
282 | |
|
|
283 | sub _attach_cb($$$$) { |
|
|
284 | my ($registry, $event, $prio, $cb) = @_; |
|
|
285 | |
|
|
286 | use sort 'stable'; |
|
|
287 | |
|
|
288 | $cb = [$prio, $cb]; |
|
|
289 | |
|
|
290 | @{$registry->[$event]} = sort |
|
|
291 | { $a->[0] cmp $b->[0] } |
|
|
292 | @{$registry->[$event] || []}, $cb; |
|
|
293 | } |
|
|
294 | |
|
|
295 | # attach handles attaching event callbacks |
|
|
296 | # the only thing the caller has to do is pass the correct |
|
|
297 | # registry (== where the callback attaches to). |
|
|
298 | sub _attach { |
|
|
299 | my ($registry, $klass, @arg) = @_; |
|
|
300 | |
|
|
301 | my $object_type; |
|
|
302 | my $prio = 0; |
|
|
303 | my %cb_id = map +("on_" . lc $EVENT[$_][0], $_) , grep $EVENT[$_][1] == $klass, 0 .. $#EVENT; |
|
|
304 | |
|
|
305 | while (@arg) { |
|
|
306 | my $type = shift @arg; |
|
|
307 | |
|
|
308 | if ($type eq "prio") { |
|
|
309 | $prio = shift @arg; |
|
|
310 | |
|
|
311 | } elsif ($type eq "type") { |
|
|
312 | $object_type = shift @arg; |
|
|
313 | $registry = $CB_TYPE[$object_type] ||= []; |
|
|
314 | |
|
|
315 | } elsif ($type eq "subtype") { |
|
|
316 | defined $object_type or Carp::croak "subtype specified without type"; |
|
|
317 | my $object_subtype = shift @arg; |
|
|
318 | $registry = $CB_TYPE[$object_type + $object_subtype * NUM_SUBTYPES] ||= []; |
|
|
319 | |
|
|
320 | } elsif ($type eq "package") { |
|
|
321 | my $pkg = shift @arg; |
|
|
322 | |
|
|
323 | while (my ($name, $id) = each %cb_id) { |
|
|
324 | if (my $cb = $pkg->can ($name)) { |
|
|
325 | _attach_cb $registry, $id, $prio, $cb; |
|
|
326 | } |
|
|
327 | } |
|
|
328 | |
|
|
329 | } elsif (exists $cb_id{$type}) { |
|
|
330 | _attach_cb $registry, $cb_id{$type}, $prio, shift @arg; |
|
|
331 | |
|
|
332 | } elsif (ref $type) { |
|
|
333 | warn "attaching objects not supported, ignoring.\n"; |
|
|
334 | |
|
|
335 | } else { |
|
|
336 | shift @arg; |
|
|
337 | warn "attach argument '$type' not supported, ignoring.\n"; |
|
|
338 | } |
|
|
339 | } |
|
|
340 | } |
|
|
341 | |
|
|
342 | sub _object_attach { |
|
|
343 | my ($obj, $name, %arg) = @_; |
|
|
344 | |
|
|
345 | return if exists $obj->{_attachment}{$name}; |
|
|
346 | |
|
|
347 | if (my $attach = $attachment{$name}) { |
|
|
348 | my $registry = $obj->registry; |
|
|
349 | |
|
|
350 | for (@$attach) { |
|
|
351 | my ($klass, @attach) = @$_; |
|
|
352 | _attach $registry, $klass, @attach; |
|
|
353 | } |
|
|
354 | |
|
|
355 | $obj->{$name} = \%arg; |
|
|
356 | } else { |
|
|
357 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
|
|
358 | } |
|
|
359 | |
|
|
360 | $obj->{_attachment}{$name} = undef; |
|
|
361 | } |
|
|
362 | |
|
|
363 | sub cf::attachable::attach { |
|
|
364 | if (ref $_[0]) { |
|
|
365 | _object_attach @_; |
|
|
366 | } else { |
|
|
367 | _attach shift->_attach_registry, @_; |
|
|
368 | } |
|
|
369 | }; |
|
|
370 | |
|
|
371 | # all those should be optimised |
|
|
372 | sub cf::attachable::detach { |
|
|
373 | my ($obj, $name) = @_; |
|
|
374 | |
|
|
375 | if (ref $obj) { |
|
|
376 | delete $obj->{_attachment}{$name}; |
|
|
377 | reattach ($obj); |
|
|
378 | } else { |
|
|
379 | Carp::croak "cannot, currently, detach class attachments"; |
|
|
380 | } |
|
|
381 | }; |
|
|
382 | |
|
|
383 | sub cf::attachable::attached { |
|
|
384 | my ($obj, $name) = @_; |
|
|
385 | |
|
|
386 | exists $obj->{_attachment}{$name} |
|
|
387 | } |
|
|
388 | |
|
|
389 | for my $klass (qw(GLOBAL OBJECT PLAYER CLIENT MAP)) { |
|
|
390 | eval "#line " . __LINE__ . " 'cf.pm' |
|
|
391 | sub cf::\L$klass\E::_attach_registry { |
|
|
392 | (\\\@CB_$klass, KLASS_$klass) |
|
|
393 | } |
|
|
394 | |
|
|
395 | sub cf::\L$klass\E::attachment { |
|
|
396 | my \$name = shift; |
|
|
397 | |
|
|
398 | \$attachment{\$name} = [[KLASS_$klass, \@_]]; |
|
|
399 | } |
|
|
400 | "; |
|
|
401 | die if $@; |
|
|
402 | } |
|
|
403 | |
|
|
404 | our $override; |
|
|
405 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
|
|
406 | |
|
|
407 | sub override { |
|
|
408 | $override = 1; |
|
|
409 | @invoke_results = (); |
|
|
410 | } |
|
|
411 | |
|
|
412 | sub do_invoke { |
|
|
413 | my $event = shift; |
|
|
414 | my $callbacks = shift; |
|
|
415 | |
|
|
416 | @invoke_results = (); |
|
|
417 | |
|
|
418 | local $override; |
|
|
419 | |
|
|
420 | for (@$callbacks) { |
|
|
421 | eval { &{$_->[1]} }; |
|
|
422 | |
|
|
423 | if ($@) { |
|
|
424 | warn "$@"; |
|
|
425 | warn "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n"; |
|
|
426 | override; |
|
|
427 | } |
|
|
428 | |
|
|
429 | return 1 if $override; |
|
|
430 | } |
|
|
431 | |
|
|
432 | 0 |
|
|
433 | } |
|
|
434 | |
|
|
435 | =item $bool = cf::global::invoke (EVENT_CLASS_XXX, ...) |
|
|
436 | |
|
|
437 | =item $bool = $attachable->invoke (EVENT_CLASS_XXX, ...) |
|
|
438 | |
|
|
439 | Generate an object-specific event with the given arguments. |
|
|
440 | |
|
|
441 | This API is preliminary (most likely, the EVENT_CLASS_xxx prefix will be |
|
|
442 | removed in future versions), and there is no public API to access override |
|
|
443 | results (if you must, access C<@cf::invoke_results> directly). |
|
|
444 | |
463 | =back |
445 | =back |
464 | |
446 | |
465 | =cut |
447 | =cut |
466 | |
|
|
467 | *cf::object::valid = |
|
|
468 | *cf::player::valid = |
|
|
469 | *cf::map::valid = \&cf::_valid; |
|
|
470 | |
448 | |
471 | ############################################################################# |
449 | ############################################################################# |
472 | # object support |
450 | # object support |
473 | |
451 | |
474 | sub instantiate { |
452 | sub instantiate { |
… | |
… | |
494 | |
472 | |
495 | for my $name (keys %{ $obj->{_attachment} || {} }) { |
473 | for my $name (keys %{ $obj->{_attachment} || {} }) { |
496 | if (my $attach = $attachment{$name}) { |
474 | if (my $attach = $attachment{$name}) { |
497 | for (@$attach) { |
475 | for (@$attach) { |
498 | my ($klass, @attach) = @$_; |
476 | my ($klass, @attach) = @$_; |
499 | _attach @$registry, $klass, @attach; |
477 | _attach $registry, $klass, @attach; |
500 | } |
478 | } |
501 | } else { |
479 | } else { |
502 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
480 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
503 | } |
481 | } |
504 | } |
482 | } |
… | |
… | |
560 | } |
538 | } |
561 | |
539 | |
562 | () |
540 | () |
563 | } |
541 | } |
564 | |
542 | |
565 | attach_to_objects |
543 | cf::object->attach ( |
566 | prio => -1000000, |
544 | prio => -1000000, |
567 | on_clone => sub { |
545 | on_clone => sub { |
568 | my ($src, $dst) = @_; |
546 | my ($src, $dst) = @_; |
569 | |
547 | |
570 | @{$dst->registry} = @{$src->registry}; |
548 | @{$dst->registry} = @{$src->registry}; |
… | |
… | |
572 | %$dst = %$src; |
550 | %$dst = %$src; |
573 | |
551 | |
574 | %{$dst->{_attachment}} = %{$src->{_attachment}} |
552 | %{$dst->{_attachment}} = %{$src->{_attachment}} |
575 | if exists $src->{_attachment}; |
553 | if exists $src->{_attachment}; |
576 | }, |
554 | }, |
577 | ; |
555 | ); |
578 | |
556 | |
579 | ############################################################################# |
557 | ############################################################################# |
580 | # command handling &c |
558 | # command handling &c |
581 | |
559 | |
582 | =item cf::register_command $name => \&callback($ob,$args); |
560 | =item cf::register_command $name => \&callback($ob,$args); |
… | |
… | |
611 | #warn "registering extcmd '$name' to '$caller'"; |
589 | #warn "registering extcmd '$name' to '$caller'"; |
612 | |
590 | |
613 | $EXTCMD{$name} = [$cb, $caller]; |
591 | $EXTCMD{$name} = [$cb, $caller]; |
614 | } |
592 | } |
615 | |
593 | |
616 | attach_to_players |
594 | cf::player->attach ( |
617 | on_command => sub { |
595 | on_command => sub { |
618 | my ($pl, $name, $params) = @_; |
596 | my ($pl, $name, $params) = @_; |
619 | |
597 | |
620 | my $cb = $COMMAND{$name} |
598 | my $cb = $COMMAND{$name} |
621 | or return; |
599 | or return; |
… | |
… | |
641 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
619 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
642 | } |
620 | } |
643 | |
621 | |
644 | cf::override; |
622 | cf::override; |
645 | }, |
623 | }, |
646 | ; |
624 | ); |
647 | |
625 | |
648 | sub register { |
626 | sub register { |
649 | my ($base, $pkg) = @_; |
627 | my ($base, $pkg) = @_; |
650 | |
628 | |
651 | #TODO |
629 | #TODO |
… | |
… | |
741 | defined $path or return; |
719 | defined $path or return; |
742 | |
720 | |
743 | unlink "$path.pst"; |
721 | unlink "$path.pst"; |
744 | }; |
722 | }; |
745 | |
723 | |
746 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
724 | cf::map->attach (prio => -10000, package => cf::mapsupport::); |
747 | |
725 | |
748 | ############################################################################# |
726 | ############################################################################# |
749 | # load/save perl data associated with player->ob objects |
727 | # load/save perl data associated with player->ob objects |
750 | |
728 | |
751 | sub all_objects(@) { |
729 | sub all_objects(@) { |
752 | @_, map all_objects ($_->inv), @_ |
730 | @_, map all_objects ($_->inv), @_ |
753 | } |
731 | } |
754 | |
732 | |
755 | # TODO: compatibility cruft, remove when no longer needed |
733 | # TODO: compatibility cruft, remove when no longer needed |
756 | attach_to_players |
734 | cf::player->attach ( |
757 | on_load => sub { |
735 | on_load => sub { |
758 | my ($pl, $path) = @_; |
736 | my ($pl, $path) = @_; |
759 | |
737 | |
760 | for my $o (all_objects $pl->ob) { |
738 | for my $o (all_objects $pl->ob) { |
761 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
739 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
… | |
… | |
763 | |
741 | |
764 | %$o = %{ Storable::thaw pack "H*", $value }; |
742 | %$o = %{ Storable::thaw pack "H*", $value }; |
765 | } |
743 | } |
766 | } |
744 | } |
767 | }, |
745 | }, |
768 | ; |
746 | ); |
769 | |
747 | |
770 | ############################################################################# |
748 | ############################################################################# |
771 | |
749 | |
772 | =head2 CORE EXTENSIONS |
750 | =head2 CORE EXTENSIONS |
773 | |
751 | |
774 | Functions and methods that extend core crossfire objects. |
752 | Functions and methods that extend core crossfire objects. |
|
|
753 | |
|
|
754 | =head3 cf::player |
775 | |
755 | |
776 | =over 4 |
756 | =over 4 |
777 | |
757 | |
778 | =item cf::player::exists $login |
758 | =item cf::player::exists $login |
779 | |
759 | |
… | |
… | |
784 | sub cf::player::exists($) { |
764 | sub cf::player::exists($) { |
785 | cf::player::find $_[0] |
765 | cf::player::find $_[0] |
786 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
766 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
787 | } |
767 | } |
788 | |
768 | |
|
|
769 | =item $player->ext_reply ($msgid, $msgtype, %msg) |
|
|
770 | |
|
|
771 | Sends an ext reply to the player. |
|
|
772 | |
|
|
773 | =cut |
|
|
774 | |
|
|
775 | sub cf::player::ext_reply($$$%) { |
|
|
776 | my ($self, $id, %msg) = @_; |
|
|
777 | |
|
|
778 | $msg{msgid} = $id; |
|
|
779 | |
|
|
780 | $self->send ("ext " . to_json \%msg); |
|
|
781 | } |
|
|
782 | |
|
|
783 | =back |
|
|
784 | |
|
|
785 | =head3 cf::object::player |
|
|
786 | |
|
|
787 | =over 4 |
|
|
788 | |
789 | =item $player_object->reply ($npc, $msg[, $flags]) |
789 | =item $player_object->reply ($npc, $msg[, $flags]) |
790 | |
790 | |
791 | Sends a message to the player, as if the npc C<$npc> replied. C<$npc> |
791 | Sends a message to the player, as if the npc C<$npc> replied. C<$npc> |
792 | can be C<undef>. Does the right thing when the player is currently in a |
792 | can be C<undef>. Does the right thing when the player is currently in a |
793 | dialogue with the given NPC character. |
793 | dialogue with the given NPC character. |
794 | |
794 | |
795 | =cut |
795 | =cut |
796 | |
796 | |
797 | # rough implementation of a future "reply" method that works |
797 | # rough implementation of a future "reply" method that works |
798 | # with dialog boxes. |
798 | # with dialog boxes. |
|
|
799 | #TODO: the first argument must go, split into a $npc->reply_to ( method |
799 | sub cf::object::player::reply($$$;$) { |
800 | sub cf::object::player::reply($$$;$) { |
800 | my ($self, $npc, $msg, $flags) = @_; |
801 | my ($self, $npc, $msg, $flags) = @_; |
801 | |
802 | |
802 | $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4; |
803 | $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4; |
803 | |
804 | |
… | |
… | |
807 | $msg = $npc->name . " says: $msg" if $npc; |
808 | $msg = $npc->name . " says: $msg" if $npc; |
808 | $self->message ($msg, $flags); |
809 | $self->message ($msg, $flags); |
809 | } |
810 | } |
810 | } |
811 | } |
811 | |
812 | |
812 | =item $player->ext_reply ($msgid, $msgtype, %msg) |
|
|
813 | |
|
|
814 | Sends an ext reply to the player. |
|
|
815 | |
|
|
816 | =cut |
|
|
817 | |
|
|
818 | sub cf::player::ext_reply($$$%) { |
|
|
819 | my ($self, $id, %msg) = @_; |
|
|
820 | |
|
|
821 | $msg{msgid} = $id; |
|
|
822 | |
|
|
823 | $self->send ("ext " . to_json \%msg); |
|
|
824 | } |
|
|
825 | |
|
|
826 | =item $player_object->may ("access") |
813 | =item $player_object->may ("access") |
827 | |
814 | |
828 | Returns wether the given player is authorized to access resource "access" |
815 | Returns wether the given player is authorized to access resource "access" |
829 | (e.g. "command_wizcast"). |
816 | (e.g. "command_wizcast"). |
830 | |
817 | |
… | |
… | |
837 | (ref $cf::CFG{"may_$access"} |
824 | (ref $cf::CFG{"may_$access"} |
838 | ? scalar grep $self->name eq $_, @{$cf::CFG{"may_$access"}} |
825 | ? scalar grep $self->name eq $_, @{$cf::CFG{"may_$access"}} |
839 | : $cf::CFG{"may_$access"}) |
826 | : $cf::CFG{"may_$access"}) |
840 | } |
827 | } |
841 | |
828 | |
842 | =cut |
829 | =head3 cf::client |
843 | |
830 | |
844 | ############################################################################# |
831 | =over 4 |
|
|
832 | |
|
|
833 | =item $client->send_drawinfo ($text, $flags) |
|
|
834 | |
|
|
835 | Sends a drawinfo packet to the client. Circumvents output buffering so |
|
|
836 | should not be used under normal circumstances. |
|
|
837 | |
|
|
838 | =cut |
|
|
839 | |
|
|
840 | sub cf::client::send_drawinfo { |
|
|
841 | my ($self, $text, $flags) = @_; |
|
|
842 | |
|
|
843 | utf8::encode $text; |
|
|
844 | $self->send_packet (sprintf "drawinfo %d %s", $flags, $text); |
|
|
845 | } |
|
|
846 | |
|
|
847 | |
|
|
848 | =item $success = $client->query ($flags, "text", \&cb) |
|
|
849 | |
|
|
850 | Queues a query to the client, calling the given callback with |
|
|
851 | the reply text on a reply. flags can be C<cf::CS_QUERY_YESNO>, |
|
|
852 | C<cf::CS_QUERY_SINGLECHAR> or C<cf::CS_QUERY_HIDEINPUT> or C<0>. |
|
|
853 | |
|
|
854 | Queries can fail, so check the return code. Or don't, as queries will become |
|
|
855 | reliable at some point in the future. |
|
|
856 | |
|
|
857 | =cut |
|
|
858 | |
|
|
859 | sub cf::client::query { |
|
|
860 | my ($self, $flags, $text, $cb) = @_; |
|
|
861 | |
|
|
862 | return unless $self->state == ST_PLAYING |
|
|
863 | || $self->state == ST_SETUP |
|
|
864 | || $self->state == ST_CUSTOM; |
|
|
865 | |
|
|
866 | $self->state (ST_CUSTOM); |
|
|
867 | |
|
|
868 | utf8::encode $text; |
|
|
869 | push @{ $self->{query_queue} }, [(sprintf "query %d %s", $flags, $text), $cb]; |
|
|
870 | |
|
|
871 | $self->send_packet ($self->{query_queue}[0][0]) |
|
|
872 | if @{ $self->{query_queue} } == 1; |
|
|
873 | } |
|
|
874 | |
|
|
875 | cf::client->attach ( |
|
|
876 | on_reply => sub { |
|
|
877 | my ($ns, $msg) = @_; |
|
|
878 | |
|
|
879 | # this weird shuffling is so that direct followup queries |
|
|
880 | # get handled first |
|
|
881 | my $queue = delete $ns->{query_queue}; |
|
|
882 | |
|
|
883 | (shift @$queue)->[1]->($msg); |
|
|
884 | |
|
|
885 | push @{ $ns->{query_queue} }, @$queue; |
|
|
886 | |
|
|
887 | if (@{ $ns->{query_queue} } == @$queue) { |
|
|
888 | if (@$queue) { |
|
|
889 | $ns->send_packet ($ns->{query_queue}[0][0]); |
|
|
890 | } else { |
|
|
891 | $ns->state (ST_PLAYING); |
|
|
892 | } |
|
|
893 | } |
|
|
894 | }, |
|
|
895 | ); |
|
|
896 | |
|
|
897 | =item $client->coro (\&cb) |
|
|
898 | |
|
|
899 | Create a new coroutine, running the specified callback. The coroutine will |
|
|
900 | be automatically cancelled when the client gets destroyed (e.g. on logout, |
|
|
901 | or loss of connection). |
|
|
902 | |
|
|
903 | =cut |
|
|
904 | |
|
|
905 | sub cf::client::coro { |
|
|
906 | my ($self, $cb) = @_; |
|
|
907 | |
|
|
908 | my $coro; $coro = async { |
|
|
909 | eval { |
|
|
910 | $cb->(); |
|
|
911 | }; |
|
|
912 | warn $@ if $@; |
|
|
913 | delete $self->{_coro}{$coro+0}; |
|
|
914 | }; |
|
|
915 | |
|
|
916 | $self->{_coro}{$coro+0} = $coro; |
|
|
917 | } |
|
|
918 | |
|
|
919 | cf::client->attach ( |
|
|
920 | on_destroy => sub { |
|
|
921 | my ($ns) = @_; |
|
|
922 | |
|
|
923 | $_->cancel for values %{ (delete $ns->{_coro}) || {} }; |
|
|
924 | }, |
|
|
925 | ); |
|
|
926 | |
|
|
927 | =back |
|
|
928 | |
845 | |
929 | |
846 | =head2 SAFE SCRIPTING |
930 | =head2 SAFE SCRIPTING |
847 | |
931 | |
848 | Functions that provide a safe environment to compile and execute |
932 | Functions that provide a safe environment to compile and execute |
849 | snippets of perl code without them endangering the safety of the server |
933 | snippets of perl code without them endangering the safety of the server |
… | |
… | |
1049 | $DB->{$_[0]} = $_[1]; |
1133 | $DB->{$_[0]} = $_[1]; |
1050 | } |
1134 | } |
1051 | db_dirty; |
1135 | db_dirty; |
1052 | } |
1136 | } |
1053 | |
1137 | |
1054 | attach_global |
1138 | cf::global->attach ( |
1055 | prio => 10000, |
1139 | prio => 10000, |
1056 | on_cleanup => sub { |
1140 | on_cleanup => sub { |
1057 | db_sync; |
1141 | db_sync; |
1058 | }, |
1142 | }, |
1059 | ; |
1143 | ); |
1060 | } |
1144 | } |
1061 | |
1145 | |
1062 | ############################################################################# |
1146 | ############################################################################# |
1063 | # the server's main() |
1147 | # the server's main() |
1064 | |
1148 | |