… | |
… | |
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 | |
112 | =item $object->detach ($attachment) |
116 | =item $object->detach ($attachment) |
113 | |
117 | |
114 | Attach/detach a pre-registered attachment to an object. |
118 | Attach/detach a pre-registered attachment to an object. |
… | |
… | |
122 | =item $map->attach ($attachment, key => $value...) |
126 | =item $map->attach ($attachment, key => $value...) |
123 | |
127 | |
124 | =item $map->detach ($attachment) |
128 | =item $map->detach ($attachment) |
125 | |
129 | |
126 | Attach/detach 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. |
127 | |
139 | |
128 | =item cf::attach_global ... |
140 | =item cf::attach_global ... |
129 | |
141 | |
130 | Attach handlers for global events. |
142 | Attach handlers for global events. |
131 | |
143 | |
… | |
… | |
181 | Attach handlers to all maps. |
193 | Attach handlers to all maps. |
182 | |
194 | |
183 | =item cf:register_attachment $name, ... |
195 | =item cf:register_attachment $name, ... |
184 | |
196 | |
185 | Register an attachment by name through which objects can refer to this |
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 |
186 | attachment. |
203 | attachment. |
187 | |
204 | |
188 | =item cf:register_map_attachment $name, ... |
205 | =item cf:register_map_attachment $name, ... |
189 | |
206 | |
190 | Register an attachment by name through which maps can refer to this |
207 | Register an attachment by name through which maps can refer to this |
… | |
… | |
261 | } |
278 | } |
262 | |
279 | |
263 | sub _attach_attachment { |
280 | sub _attach_attachment { |
264 | my ($obj, $name, %arg) = @_; |
281 | my ($obj, $name, %arg) = @_; |
265 | |
282 | |
|
|
283 | return if exists $obj->{_attachment}{$name}; |
|
|
284 | |
266 | my $res; |
285 | my $res; |
267 | |
286 | |
268 | if (my $attach = $attachment{$name}) { |
287 | if (my $attach = $attachment{$name}) { |
269 | my $registry = $obj->registry; |
288 | my $registry = $obj->registry; |
270 | |
289 | |
… | |
… | |
288 | *cf::player::attach = |
307 | *cf::player::attach = |
289 | *cf::map::attach = sub { |
308 | *cf::map::attach = sub { |
290 | my ($obj, $name, %arg) = @_; |
309 | my ($obj, $name, %arg) = @_; |
291 | |
310 | |
292 | _attach_attachment $obj, $name, %arg; |
311 | _attach_attachment $obj, $name, %arg; |
293 | } |
312 | }; |
294 | |
313 | |
295 | # all those should be optimised |
314 | # all those should be optimised |
296 | *cf::object::detach = |
315 | *cf::object::detach = |
297 | *cf::player::detach = |
316 | *cf::player::detach = |
298 | *cf::map::detach = sub { |
317 | *cf::map::detach = sub { |
299 | my ($obj, $name) = @_; |
318 | my ($obj, $name) = @_; |
300 | |
319 | |
301 | delete $obj->{_attachment}{$name}; |
320 | delete $obj->{_attachment}{$name}; |
302 | reattach $obj; |
321 | reattach ($obj); |
|
|
322 | }; |
|
|
323 | |
|
|
324 | *cf::object::attached = |
|
|
325 | *cf::player::attached = |
|
|
326 | *cf::map::attached = sub { |
|
|
327 | my ($obj, $name) = @_; |
|
|
328 | |
|
|
329 | exists $obj->{_attachment}{$name} |
303 | }; |
330 | }; |
304 | |
331 | |
305 | sub attach_global { |
332 | sub attach_global { |
306 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
333 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
307 | } |
334 | } |
… | |
… | |
327 | |
354 | |
328 | sub register_attachment { |
355 | sub register_attachment { |
329 | my $name = shift; |
356 | my $name = shift; |
330 | |
357 | |
331 | $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, @_]]; |
332 | } |
365 | } |
333 | |
366 | |
334 | sub register_map_attachment { |
367 | sub register_map_attachment { |
335 | my $name = shift; |
368 | my $name = shift; |
336 | |
369 | |
… | |
… | |
365 | return 1 if $override; |
398 | return 1 if $override; |
366 | } |
399 | } |
367 | |
400 | |
368 | 0 |
401 | 0 |
369 | } |
402 | } |
|
|
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; |
370 | |
443 | |
371 | ############################################################################# |
444 | ############################################################################# |
372 | # object support |
445 | # object support |
373 | |
446 | |
374 | sub instantiate { |
447 | sub instantiate { |
… | |
… | |
403 | } |
476 | } |
404 | } |
477 | } |
405 | } |
478 | } |
406 | |
479 | |
407 | sub object_freezer_save { |
480 | sub object_freezer_save { |
408 | my ($filename, $objs) = @_; |
481 | my ($filename, $rdata, $objs) = @_; |
409 | |
482 | |
410 | if (@$objs) { |
483 | if (length $$rdata) { |
|
|
484 | warn sprintf "saving %s (%d,%d)\n", |
|
|
485 | $filename, length $$rdata, scalar @$objs; |
|
|
486 | |
411 | open my $fh, ">:raw", "$filename.pst~"; |
487 | if (open my $fh, ">:raw", "$filename~") { |
|
|
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; |
412 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
494 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
413 | close $fh; |
495 | close $fh; |
414 | chmod SAVE_MODE, "$filename.pst~"; |
|
|
415 | rename "$filename.pst~", "$filename.pst"; |
496 | rename "$filename.pst~", "$filename.pst"; |
|
|
497 | } else { |
|
|
498 | unlink "$filename.pst"; |
|
|
499 | } |
|
|
500 | |
|
|
501 | rename "$filename~", $filename; |
|
|
502 | } else { |
|
|
503 | warn "FATAL: $filename~: $!\n"; |
|
|
504 | } |
416 | } else { |
505 | } else { |
|
|
506 | unlink $filename; |
417 | unlink "$filename.pst"; |
507 | unlink "$filename.pst"; |
418 | } |
508 | } |
419 | |
|
|
420 | chmod SAVE_MODE, "$filename~"; |
|
|
421 | rename "$filename~", $filename; |
|
|
422 | } |
509 | } |
423 | |
510 | |
424 | sub object_thawer_load { |
511 | sub object_thawer_load { |
425 | my ($filename) = @_; |
512 | my ($filename) = @_; |
426 | |
513 | |
|
|
514 | local $/; |
|
|
515 | |
|
|
516 | my $av; |
|
|
517 | |
|
|
518 | #TODO: use sysread etc. |
|
|
519 | if (open my $data, "<:raw:perlio", $filename) { |
|
|
520 | $data = <$data>; |
427 | open my $fh, "<:raw:perlio", "$filename.pst" |
521 | if (open my $pst, "<:raw:perlio", "$filename.pst") { |
428 | or return; |
522 | $av = eval { (Storable::thaw <$pst>)->{objs} }; |
|
|
523 | } |
|
|
524 | return ($data, $av); |
|
|
525 | } |
429 | |
526 | |
430 | eval { local $/; (Storable::thaw <$fh>)->{objs} } |
527 | () |
431 | } |
528 | } |
432 | |
529 | |
433 | attach_to_objects |
530 | attach_to_objects |
434 | prio => -1000000, |
531 | prio => -1000000, |
435 | on_clone => sub { |
532 | on_clone => sub { |
… | |
… | |
631 | #Symbol::delete_package __PACKAGE__; |
728 | #Symbol::delete_package __PACKAGE__; |
632 | |
729 | |
633 | # 7. reload cf.pm |
730 | # 7. reload cf.pm |
634 | $msg->("reloading cf.pm"); |
731 | $msg->("reloading cf.pm"); |
635 | require cf; |
732 | require cf; |
|
|
733 | |
|
|
734 | $msg->("load extensions"); |
|
|
735 | cf::load_extensions; |
636 | }; |
736 | }; |
637 | $msg->($@) if $@; |
737 | $msg->($@) if $@; |
638 | |
738 | |
639 | $msg->("reloaded"); |
739 | $msg->("reloaded"); |
640 | }; |
740 | }; |
… | |
… | |
691 | defined $path or return; |
791 | defined $path or return; |
692 | |
792 | |
693 | unlink "$path.pst"; |
793 | unlink "$path.pst"; |
694 | }; |
794 | }; |
695 | |
795 | |
696 | # old style persistent data, TODO: remove #d# |
|
|
697 | *cf::mapsupport::on_swapin = |
|
|
698 | *cf::mapsupport::on_load = sub { |
|
|
699 | my ($map) = @_; |
|
|
700 | |
|
|
701 | my $path = $map->tmpname; |
|
|
702 | $path = $map->path unless defined $path; |
|
|
703 | |
|
|
704 | open my $fh, "<:raw", "$path.cfperl" |
|
|
705 | or return; # no perl data |
|
|
706 | |
|
|
707 | my $data = Storable::thaw do { local $/; <$fh> }; |
|
|
708 | |
|
|
709 | $data->{version} <= 1 |
|
|
710 | or return; # too new |
|
|
711 | |
|
|
712 | $map->_set_obs ($data->{obs}); |
|
|
713 | $map->invoke (EVENT_MAP_UPGRADE); |
|
|
714 | }; |
|
|
715 | |
|
|
716 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
796 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
717 | |
797 | |
718 | ############################################################################# |
798 | ############################################################################# |
719 | # load/save perl data associated with player->ob objects |
799 | # load/save perl data associated with player->ob objects |
720 | |
800 | |
721 | sub all_objects(@) { |
801 | sub all_objects(@) { |
722 | @_, map all_objects ($_->inv), @_ |
802 | @_, map all_objects ($_->inv), @_ |
723 | } |
803 | } |
724 | |
804 | |
|
|
805 | # TODO: compatibility cruft, remove when no longer needed |
725 | attach_to_players |
806 | attach_to_players |
726 | on_load => sub { |
807 | on_load => sub { |
727 | my ($pl, $path) = @_; |
808 | my ($pl, $path) = @_; |
728 | |
809 | |
729 | for my $o (all_objects $pl->ob) { |
810 | for my $o (all_objects $pl->ob) { |
… | |
… | |
850 | |
931 | |
851 | ############################################################################# |
932 | ############################################################################# |
852 | # the server's main() |
933 | # the server's main() |
853 | |
934 | |
854 | sub main { |
935 | sub main { |
|
|
936 | load_extensions; |
855 | Event::loop; |
937 | Event::loop; |
856 | } |
938 | } |
857 | |
939 | |
858 | ############################################################################# |
940 | ############################################################################# |
859 | # initialisation |
941 | # initialisation |
860 | |
942 | |
861 | register "<global>", __PACKAGE__; |
943 | register "<global>", __PACKAGE__; |
862 | |
944 | |
863 | unshift @INC, $LIBDIR; |
945 | unshift @INC, $LIBDIR; |
864 | |
|
|
865 | load_extensions; |
|
|
866 | |
946 | |
867 | $TICK_WATCHER = Event->timer ( |
947 | $TICK_WATCHER = Event->timer ( |
868 | prio => 1, |
948 | prio => 1, |
869 | at => $NEXT_TICK || 1, |
949 | at => $NEXT_TICK || 1, |
870 | cb => sub { |
950 | cb => sub { |