… | |
… | |
72 | |
72 | |
73 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
73 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
74 | |
74 | |
75 | # we bless all objects into (empty) derived classes to force a method lookup |
75 | # we bless all objects into (empty) derived classes to force a method lookup |
76 | # within the Safe compartment. |
76 | # within the Safe compartment. |
77 | for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch)) { |
77 | for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch cf::living)) { |
78 | no strict 'refs'; |
78 | no strict 'refs'; |
79 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
79 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
80 | } |
80 | } |
81 | |
81 | |
82 | $Event::DIED = sub { |
82 | $Event::DIED = sub { |
… | |
… | |
105 | } |
105 | } |
106 | |
106 | |
107 | ############################################################################# |
107 | ############################################################################# |
108 | # "new" plug-in system |
108 | # "new" plug-in system |
109 | |
109 | |
|
|
110 | =head3 EVENTS AND OBJECT ATTACHMENTS |
|
|
111 | |
|
|
112 | =over 4 |
|
|
113 | |
110 | =item $object->attach ($attachment, key => $value...) |
114 | =item $object->attach ($attachment, key => $value...) |
111 | |
115 | |
|
|
116 | =item $object->detach ($attachment) |
|
|
117 | |
112 | Attach a pre-registered attachment to an object. |
118 | Attach/detach a pre-registered attachment to an object. |
113 | |
119 | |
114 | =item $player->attach ($attachment, key => $value...) |
120 | =item $player->attach ($attachment, key => $value...) |
115 | |
121 | |
|
|
122 | =item $player->detach ($attachment) |
|
|
123 | |
116 | Attach a pre-registered attachment to a player. |
124 | Attach/detach a pre-registered attachment to a player. |
117 | |
125 | |
118 | =item $map->attach ($attachment, key => $value...) # not yet persistent |
126 | =item $map->attach ($attachment, key => $value...) |
119 | |
127 | |
|
|
128 | =item $map->detach ($attachment) |
|
|
129 | |
120 | Attach a pre-registered attachment to a map. |
130 | Attach/detach a pre-registered attachment to a map. |
|
|
131 | |
|
|
132 | =item $bool = $object->attached ($name) |
|
|
133 | |
|
|
134 | =item $bool = $player->attached ($name) |
|
|
135 | |
|
|
136 | =item $bool = $map->attached ($name) |
|
|
137 | |
|
|
138 | Checks wether the named attachment is currently attached to the object. |
121 | |
139 | |
122 | =item cf::attach_global ... |
140 | =item cf::attach_global ... |
123 | |
141 | |
124 | Attach handlers for global events. |
142 | Attach handlers for global events. |
125 | |
143 | |
… | |
… | |
173 | =item cf::attach_to_maps ... |
191 | =item cf::attach_to_maps ... |
174 | |
192 | |
175 | Attach handlers to all maps. |
193 | Attach handlers to all maps. |
176 | |
194 | |
177 | =item cf:register_attachment $name, ... |
195 | =item cf:register_attachment $name, ... |
|
|
196 | |
|
|
197 | Register an attachment by name through which objects can refer to this |
|
|
198 | attachment. |
|
|
199 | |
|
|
200 | =item cf:register_player_attachment $name, ... |
|
|
201 | |
|
|
202 | Register an attachment by name through which players can refer to this |
|
|
203 | attachment. |
|
|
204 | |
|
|
205 | =item cf:register_map_attachment $name, ... |
|
|
206 | |
|
|
207 | Register an attachment by name through which maps can refer to this |
|
|
208 | attachment. |
178 | |
209 | |
179 | =cut |
210 | =cut |
180 | |
211 | |
181 | # the following variables are defined in .xs and must not be re-created |
212 | # the following variables are defined in .xs and must not be re-created |
182 | our @CB_GLOBAL = (); # registry for all global events |
213 | our @CB_GLOBAL = (); # registry for all global events |
… | |
… | |
247 | } |
278 | } |
248 | |
279 | |
249 | sub _attach_attachment { |
280 | sub _attach_attachment { |
250 | my ($obj, $name, %arg) = @_; |
281 | my ($obj, $name, %arg) = @_; |
251 | |
282 | |
|
|
283 | return if exists $obj->{_attachment}{$name}; |
|
|
284 | |
252 | my $res; |
285 | my $res; |
253 | |
286 | |
254 | if (my $attach = $attachment{$name}) { |
287 | if (my $attach = $attachment{$name}) { |
255 | my $registry = $obj->registry; |
288 | my $registry = $obj->registry; |
256 | |
289 | |
… | |
… | |
262 | $obj->{$name} = \%arg; |
295 | $obj->{$name} = \%arg; |
263 | } else { |
296 | } else { |
264 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
297 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
265 | } |
298 | } |
266 | |
299 | |
267 | push @{$obj->{_attachment}}, $name; |
300 | $obj->{_attachment}{$name} = undef; |
268 | |
301 | |
269 | $res->{attachment} = $name; |
302 | $res->{attachment} = $name; |
270 | $res |
303 | $res |
271 | } |
304 | } |
272 | |
305 | |
273 | sub cf::object::attach { |
306 | *cf::object::attach = |
|
|
307 | *cf::player::attach = |
|
|
308 | *cf::map::attach = sub { |
274 | my ($obj, $name, %arg) = @_; |
309 | my ($obj, $name, %arg) = @_; |
275 | |
310 | |
276 | _attach_attachment $obj, $name, %arg; |
311 | _attach_attachment $obj, $name, %arg; |
277 | } |
312 | }; |
278 | |
313 | |
|
|
314 | # all those should be optimised |
|
|
315 | *cf::object::detach = |
279 | sub cf::player::attach { |
316 | *cf::player::detach = |
|
|
317 | *cf::map::detach = sub { |
280 | my ($obj, $name, %arg) = @_; |
318 | my ($obj, $name) = @_; |
281 | |
319 | |
282 | _attach_attachment KLASS_PLAYER, $obj, $name, %arg; |
320 | delete $obj->{_attachment}{$name}; |
283 | } |
321 | reattach ($obj); |
|
|
322 | }; |
284 | |
323 | |
285 | sub cf::map::attach { |
324 | *cf::object::attached = |
|
|
325 | *cf::player::attached = |
|
|
326 | *cf::map::attached = sub { |
286 | my ($obj, $name, %arg) = @_; |
327 | my ($obj, $name) = @_; |
287 | |
328 | |
288 | _attach_attachment KLASS_MAP, $obj, $name, %arg; |
329 | exists $obj->{_attachment}{$name} |
289 | } |
330 | }; |
290 | |
331 | |
291 | sub attach_global { |
332 | sub attach_global { |
292 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
333 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
293 | } |
334 | } |
294 | |
335 | |
… | |
… | |
313 | |
354 | |
314 | sub register_attachment { |
355 | sub register_attachment { |
315 | my $name = shift; |
356 | my $name = shift; |
316 | |
357 | |
317 | $attachment{$name} = [[KLASS_OBJECT, @_]]; |
358 | $attachment{$name} = [[KLASS_OBJECT, @_]]; |
|
|
359 | } |
|
|
360 | |
|
|
361 | sub register_player_attachment { |
|
|
362 | my $name = shift; |
|
|
363 | |
|
|
364 | $attachment{$name} = [[KLASS_PLAYER, @_]]; |
|
|
365 | } |
|
|
366 | |
|
|
367 | sub register_map_attachment { |
|
|
368 | my $name = shift; |
|
|
369 | |
|
|
370 | $attachment{$name} = [[KLASS_MAP, @_]]; |
318 | } |
371 | } |
319 | |
372 | |
320 | our $override; |
373 | our $override; |
321 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
374 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
322 | |
375 | |
… | |
… | |
346 | } |
399 | } |
347 | |
400 | |
348 | 0 |
401 | 0 |
349 | } |
402 | } |
350 | |
403 | |
|
|
404 | =item $bool = cf::invoke EVENT_GLOBAL_XXX, ... |
|
|
405 | |
|
|
406 | =item $bool = $object->invoke (EVENT_OBJECT_XXX, ...) |
|
|
407 | |
|
|
408 | =item $bool = $player->invoke (EVENT_PLAYER_XXX, ...) |
|
|
409 | |
|
|
410 | =item $bool = $map->invoke (EVENT_MAP_XXX, ...) |
|
|
411 | |
|
|
412 | Generate a global/object/player/map-specific event with the given arguments. |
|
|
413 | |
|
|
414 | This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be |
|
|
415 | removed in future versions), and there is no public API to access override |
|
|
416 | results (if you must, access C<@cf::invoke_results> directly). |
|
|
417 | |
|
|
418 | =back |
|
|
419 | |
|
|
420 | =head2 methods valid for all pointers |
|
|
421 | |
|
|
422 | =over 4 |
|
|
423 | |
|
|
424 | =item $object->valid |
|
|
425 | |
|
|
426 | =item $player->valid |
|
|
427 | |
|
|
428 | =item $map->valid |
|
|
429 | |
|
|
430 | Just because you have a perl object does not mean that the corresponding |
|
|
431 | C-level object still exists. If you try to access an object that has no |
|
|
432 | valid C counterpart anymore you get an exception at runtime. This method |
|
|
433 | can be used to test for existence of the C object part without causing an |
|
|
434 | exception. |
|
|
435 | |
|
|
436 | =back |
|
|
437 | |
|
|
438 | =cut |
|
|
439 | |
|
|
440 | *cf::object::valid = |
|
|
441 | *cf::player::valid = |
|
|
442 | *cf::map::valid = \&cf::_valid; |
|
|
443 | |
351 | ############################################################################# |
444 | ############################################################################# |
352 | # object support |
445 | # object support |
353 | |
446 | |
354 | sub instantiate { |
447 | sub instantiate { |
355 | my ($obj, $data) = @_; |
448 | my ($obj, $data) = @_; |
356 | |
449 | |
357 | $data = from_json $data; |
450 | $data = from_json $data; |
358 | |
451 | |
359 | for (@$data) { |
452 | for (@$data) { |
360 | my ($name, $args) = @$_; |
453 | my ($name, $args) = @$_; |
|
|
454 | |
361 | attach $obj, $name, %{$args || {} }; |
455 | $obj->attach ($name, %{$args || {} }); |
362 | } |
456 | } |
363 | } |
457 | } |
364 | |
458 | |
365 | # basically do the same as instantiate, without calling instantiate |
459 | # basically do the same as instantiate, without calling instantiate |
366 | sub reattach { |
460 | sub reattach { |
367 | my ($obj) = @_; |
461 | my ($obj) = @_; |
368 | my $registry = $obj->registry; |
462 | my $registry = $obj->registry; |
369 | |
463 | |
370 | @$registry = (); |
464 | @$registry = (); |
371 | |
465 | |
|
|
466 | delete $obj->{_attachment} unless scalar keys %{ $obj->{_attachment} || {} }; |
|
|
467 | |
372 | for my $name (@{ $obj->{_attachment} }) { |
468 | for my $name (keys %{ $obj->{_attachment} || {} }) { |
373 | if (my $attach = $attachment{$name}) { |
469 | if (my $attach = $attachment{$name}) { |
374 | for (@$attach) { |
470 | for (@$attach) { |
375 | my ($klass, @attach) = @$_; |
471 | my ($klass, @attach) = @$_; |
376 | _attach @$registry, $klass, @attach; |
472 | _attach @$registry, $klass, @attach; |
377 | } |
473 | } |
… | |
… | |
380 | } |
476 | } |
381 | } |
477 | } |
382 | } |
478 | } |
383 | |
479 | |
384 | sub object_freezer_save { |
480 | sub object_freezer_save { |
385 | my ($filename, $objs) = @_; |
481 | my ($filename, $rdata, $objs) = @_; |
386 | |
482 | |
387 | $filename .= ".pst"; |
483 | if (length $$rdata) { |
|
|
484 | warn sprintf "saving %s (%d,%d)\n", |
|
|
485 | $filename, length $$rdata, scalar @$objs; |
388 | |
486 | |
389 | if (@$objs) { |
|
|
390 | open my $fh, ">:raw", "$filename~"; |
487 | if (open my $fh, ">:raw", "$filename~") { |
391 | chmod $fh, SAVE_MODE; |
488 | chmod SAVE_MODE, $fh; |
|
|
489 | syswrite $fh, $$rdata; |
|
|
490 | close $fh; |
|
|
491 | |
|
|
492 | if (@$objs && open my $fh, ">:raw", "$filename.pst~") { |
|
|
493 | chmod SAVE_MODE, $fh; |
392 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
494 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
393 | close $fh; |
495 | close $fh; |
|
|
496 | rename "$filename.pst~", "$filename.pst"; |
|
|
497 | } else { |
|
|
498 | unlink "$filename.pst"; |
|
|
499 | } |
|
|
500 | |
394 | rename "$filename~", $filename; |
501 | rename "$filename~", $filename; |
|
|
502 | } else { |
|
|
503 | warn "FATAL: $filename~: $!\n"; |
|
|
504 | } |
395 | } else { |
505 | } else { |
396 | unlink $filename; |
506 | unlink $filename; |
|
|
507 | unlink "$filename.pst"; |
397 | } |
508 | } |
398 | } |
509 | } |
399 | |
510 | |
400 | sub object_thawer_load { |
511 | sub object_thawer_load { |
401 | my ($filename) = @_; |
512 | my ($filename) = @_; |
402 | |
513 | |
|
|
514 | local $/; |
|
|
515 | |
|
|
516 | my $av; |
|
|
517 | |
|
|
518 | #TODO: use sysread etc. |
|
|
519 | if (open my $data, "<:raw:perlio", $filename) { |
|
|
520 | $data = <$data>; |
403 | open my $fh, "<:raw:perlio", "$filename.pst" |
521 | if (open my $pst, "<:raw:perlio", "$filename.pst") { |
404 | or return; |
522 | $av = eval { (Storable::thaw <$pst>)->{objs} }; |
|
|
523 | } |
|
|
524 | return ($data, $av); |
|
|
525 | } |
405 | |
526 | |
406 | eval { local $/; (Storable::thaw <$fh>)->{objs} } |
527 | () |
407 | } |
528 | } |
408 | |
529 | |
409 | attach_to_objects |
530 | attach_to_objects |
410 | prio => -1000000, |
531 | prio => -1000000, |
411 | on_clone => sub { |
532 | on_clone => sub { |
… | |
… | |
413 | |
534 | |
414 | @{$dst->registry} = @{$src->registry}; |
535 | @{$dst->registry} = @{$src->registry}; |
415 | |
536 | |
416 | %$dst = %$src; |
537 | %$dst = %$src; |
417 | |
538 | |
418 | $dst->{_attachment} = [@{ $src->{_attachment} }] |
539 | %{$dst->{_attachment}} = %{$src->{_attachment}} |
419 | if exists $src->{_attachment}; |
540 | if exists $src->{_attachment}; |
420 | }, |
541 | }, |
421 | ; |
542 | ; |
422 | |
543 | |
423 | ############################################################################# |
544 | ############################################################################# |
… | |
… | |
607 | #Symbol::delete_package __PACKAGE__; |
728 | #Symbol::delete_package __PACKAGE__; |
608 | |
729 | |
609 | # 7. reload cf.pm |
730 | # 7. reload cf.pm |
610 | $msg->("reloading cf.pm"); |
731 | $msg->("reloading cf.pm"); |
611 | require cf; |
732 | require cf; |
|
|
733 | |
|
|
734 | $msg->("load extensions"); |
|
|
735 | cf::load_extensions; |
612 | }; |
736 | }; |
613 | $msg->($@) if $@; |
737 | $msg->($@) if $@; |
614 | |
738 | |
615 | $msg->("reloaded"); |
739 | $msg->("reloaded"); |
616 | }; |
740 | }; |
… | |
… | |
664 | my ($map) = @_; |
788 | my ($map) = @_; |
665 | |
789 | |
666 | my $path = $map->tmpname; |
790 | my $path = $map->tmpname; |
667 | defined $path or return; |
791 | defined $path or return; |
668 | |
792 | |
669 | unlink "$path.cfperl"; |
|
|
670 | unlink "$path.pst"; |
793 | unlink "$path.pst"; |
671 | }; |
794 | }; |
672 | |
795 | |
673 | *cf::mapsupport::on_swapin = |
|
|
674 | *cf::mapsupport::on_load = sub { |
|
|
675 | my ($map) = @_; |
|
|
676 | |
|
|
677 | my $path = $map->tmpname; |
|
|
678 | $path = $map->path unless defined $path; |
|
|
679 | |
|
|
680 | open my $fh, "<:raw", "$path.cfperl" |
|
|
681 | or return; # no perl data |
|
|
682 | |
|
|
683 | my $data = Storable::thaw do { local $/; <$fh> }; |
|
|
684 | |
|
|
685 | $data->{version} <= 1 |
|
|
686 | or return; # too new |
|
|
687 | |
|
|
688 | $map->_set_obs ($data->{obs}); |
|
|
689 | }; |
|
|
690 | |
|
|
691 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
796 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
692 | |
797 | |
693 | ############################################################################# |
798 | ############################################################################# |
694 | # load/save perl data associated with player->ob objects |
799 | # load/save perl data associated with player->ob objects |
695 | |
800 | |
696 | sub all_objects(@) { |
801 | sub all_objects(@) { |
697 | @_, map all_objects ($_->inv), @_ |
802 | @_, map all_objects ($_->inv), @_ |
698 | } |
803 | } |
699 | |
804 | |
|
|
805 | # TODO: compatibility cruft, remove when no longer needed |
700 | attach_to_players |
806 | attach_to_players |
701 | on_load => sub { |
807 | on_load => sub { |
702 | my ($pl, $path) = @_; |
808 | my ($pl, $path) = @_; |
703 | |
809 | |
704 | for my $o (all_objects $pl->ob) { |
810 | for my $o (all_objects $pl->ob) { |
… | |
… | |
825 | |
931 | |
826 | ############################################################################# |
932 | ############################################################################# |
827 | # the server's main() |
933 | # the server's main() |
828 | |
934 | |
829 | sub main { |
935 | sub main { |
|
|
936 | load_extensions; |
830 | Event::loop; |
937 | Event::loop; |
831 | } |
938 | } |
832 | |
939 | |
833 | ############################################################################# |
940 | ############################################################################# |
834 | # initialisation |
941 | # initialisation |
835 | |
942 | |
836 | register "<global>", __PACKAGE__; |
943 | register "<global>", __PACKAGE__; |
837 | |
944 | |
838 | unshift @INC, $LIBDIR; |
945 | unshift @INC, $LIBDIR; |
839 | |
|
|
840 | load_extensions; |
|
|
841 | |
946 | |
842 | $TICK_WATCHER = Event->timer ( |
947 | $TICK_WATCHER = Event->timer ( |
843 | prio => 1, |
948 | prio => 1, |
844 | at => $NEXT_TICK || 1, |
949 | at => $NEXT_TICK || 1, |
845 | cb => sub { |
950 | cb => sub { |