… | |
… | |
15 | |
15 | |
16 | _reload_1; |
16 | _reload_1; |
17 | |
17 | |
18 | our %COMMAND = (); |
18 | our %COMMAND = (); |
19 | our @EVENT; |
19 | our @EVENT; |
20 | our %PROP_TYPE; |
|
|
21 | our %PROP_IDX; |
|
|
22 | our $LIBDIR = maps_directory "perl"; |
20 | our $LIBDIR = maps_directory "perl"; |
23 | |
21 | |
24 | our $TICK = MAX_TIME * 1e-6; |
22 | our $TICK = MAX_TIME * 1e-6; |
25 | our $TICK_WATCHER; |
23 | our $TICK_WATCHER; |
26 | our $NEXT_TICK; |
24 | our $NEXT_TICK; |
… | |
… | |
34 | print STDERR "cfperl: $msg"; |
32 | print STDERR "cfperl: $msg"; |
35 | LOG llevError, "cfperl: $msg"; |
33 | LOG llevError, "cfperl: $msg"; |
36 | }; |
34 | }; |
37 | } |
35 | } |
38 | |
36 | |
39 | my %ignore_set = (MAP_PROP_PATH => 1); # I hate the plug-in api. Deeply! |
|
|
40 | |
|
|
41 | # generate property mutators |
|
|
42 | sub prop_gen { |
|
|
43 | my ($prefix, $class) = @_; |
|
|
44 | |
|
|
45 | no strict 'refs'; |
|
|
46 | |
|
|
47 | for my $prop (keys %PROP_TYPE) { |
|
|
48 | $prop =~ /^\Q$prefix\E_(.*$)/ or next; |
|
|
49 | my $sub = lc $1; |
|
|
50 | |
|
|
51 | my $type = $PROP_TYPE{$prop}; |
|
|
52 | my $idx = $PROP_IDX {$prop}; |
|
|
53 | |
|
|
54 | *{"$class\::get_$sub"} = *{"$class\::$sub"} = sub { |
|
|
55 | $_[0]->get_property ($type, $idx) |
|
|
56 | }; |
|
|
57 | |
|
|
58 | *{"$class\::set_$sub"} = sub { |
|
|
59 | $_[0]->set_property ($type, $idx, $_[1]); |
|
|
60 | } unless $ignore_set{$prop}; |
|
|
61 | } |
|
|
62 | } |
|
|
63 | |
|
|
64 | # auto-generate most of the API |
|
|
65 | |
|
|
66 | prop_gen OBJECT_PROP => "cf::object"; |
|
|
67 | # CFAPI_OBJECT_ANIMATION? |
|
|
68 | prop_gen PLAYER_PROP => "cf::object::player"; |
|
|
69 | |
|
|
70 | prop_gen MAP_PROP => "cf::map"; |
|
|
71 | prop_gen ARCH_PROP => "cf::arch"; |
|
|
72 | |
|
|
73 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
37 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
74 | |
38 | |
75 | # we bless all objects into (empty) derived classes to force a method lookup |
39 | # we bless all objects into (empty) derived classes to force a method lookup |
76 | # within the Safe compartment. |
40 | # within the Safe compartment. |
77 | for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch)) { |
41 | 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'; |
42 | no strict 'refs'; |
79 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
43 | @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; |
80 | } |
44 | } |
81 | |
45 | |
82 | $Event::DIED = sub { |
46 | $Event::DIED = sub { |
… | |
… | |
105 | } |
69 | } |
106 | |
70 | |
107 | ############################################################################# |
71 | ############################################################################# |
108 | # "new" plug-in system |
72 | # "new" plug-in system |
109 | |
73 | |
|
|
74 | =head3 EVENTS AND OBJECT ATTACHMENTS |
|
|
75 | |
|
|
76 | =over 4 |
|
|
77 | |
110 | =item $object->attach ($attachment, key => $value...) |
78 | =item $object->attach ($attachment, key => $value...) |
111 | |
79 | |
|
|
80 | =item $object->detach ($attachment) |
|
|
81 | |
112 | Attach a pre-registered attachment to an object. |
82 | Attach/detach a pre-registered attachment to an object. |
113 | |
83 | |
114 | =item $player->attach ($attachment, key => $value...) |
84 | =item $player->attach ($attachment, key => $value...) |
115 | |
85 | |
|
|
86 | =item $player->detach ($attachment) |
|
|
87 | |
116 | Attach a pre-registered attachment to a player. |
88 | Attach/detach a pre-registered attachment to a player. |
117 | |
89 | |
118 | =item $map->attach ($attachment, key => $value...) # not yet persistent |
90 | =item $map->attach ($attachment, key => $value...) |
119 | |
91 | |
|
|
92 | =item $map->detach ($attachment) |
|
|
93 | |
120 | Attach a pre-registered attachment to a map. |
94 | Attach/detach a pre-registered attachment to a map. |
|
|
95 | |
|
|
96 | =item $bool = $object->attached ($name) |
|
|
97 | |
|
|
98 | =item $bool = $player->attached ($name) |
|
|
99 | |
|
|
100 | =item $bool = $map->attached ($name) |
|
|
101 | |
|
|
102 | Checks wether the named attachment is currently attached to the object. |
121 | |
103 | |
122 | =item cf::attach_global ... |
104 | =item cf::attach_global ... |
123 | |
105 | |
124 | Attach handlers for global events. |
106 | Attach handlers for global events. |
125 | |
107 | |
… | |
… | |
173 | =item cf::attach_to_maps ... |
155 | =item cf::attach_to_maps ... |
174 | |
156 | |
175 | Attach handlers to all maps. |
157 | Attach handlers to all maps. |
176 | |
158 | |
177 | =item cf:register_attachment $name, ... |
159 | =item cf:register_attachment $name, ... |
|
|
160 | |
|
|
161 | Register an attachment by name through which objects can refer to this |
|
|
162 | attachment. |
|
|
163 | |
|
|
164 | =item cf:register_player_attachment $name, ... |
|
|
165 | |
|
|
166 | Register an attachment by name through which players can refer to this |
|
|
167 | attachment. |
|
|
168 | |
|
|
169 | =item cf:register_map_attachment $name, ... |
|
|
170 | |
|
|
171 | Register an attachment by name through which maps can refer to this |
|
|
172 | attachment. |
178 | |
173 | |
179 | =cut |
174 | =cut |
180 | |
175 | |
181 | # the following variables are defined in .xs and must not be re-created |
176 | # the following variables are defined in .xs and must not be re-created |
182 | our @CB_GLOBAL = (); # registry for all global events |
177 | our @CB_GLOBAL = (); # registry for all global events |
… | |
… | |
247 | } |
242 | } |
248 | |
243 | |
249 | sub _attach_attachment { |
244 | sub _attach_attachment { |
250 | my ($obj, $name, %arg) = @_; |
245 | my ($obj, $name, %arg) = @_; |
251 | |
246 | |
|
|
247 | return if exists $obj->{_attachment}{$name}; |
|
|
248 | |
252 | my $res; |
249 | my $res; |
253 | |
250 | |
254 | if (my $attach = $attachment{$name}) { |
251 | if (my $attach = $attachment{$name}) { |
255 | my $registry = $obj->registry; |
252 | my $registry = $obj->registry; |
256 | |
253 | |
… | |
… | |
262 | $obj->{$name} = \%arg; |
259 | $obj->{$name} = \%arg; |
263 | } else { |
260 | } else { |
264 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
261 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
265 | } |
262 | } |
266 | |
263 | |
267 | push @{$obj->{_attachment}}, $name; |
264 | $obj->{_attachment}{$name} = undef; |
268 | |
265 | |
269 | $res->{attachment} = $name; |
266 | $res->{attachment} = $name; |
270 | $res |
267 | $res |
271 | } |
268 | } |
272 | |
269 | |
273 | sub cf::object::attach { |
270 | *cf::object::attach = |
|
|
271 | *cf::player::attach = |
|
|
272 | *cf::map::attach = sub { |
274 | my ($obj, $name, %arg) = @_; |
273 | my ($obj, $name, %arg) = @_; |
275 | |
274 | |
276 | _attach_attachment $obj, $name, %arg; |
275 | _attach_attachment $obj, $name, %arg; |
277 | } |
276 | }; |
278 | |
277 | |
|
|
278 | # all those should be optimised |
|
|
279 | *cf::object::detach = |
279 | sub cf::player::attach { |
280 | *cf::player::detach = |
|
|
281 | *cf::map::detach = sub { |
280 | my ($obj, $name, %arg) = @_; |
282 | my ($obj, $name) = @_; |
281 | |
283 | |
282 | _attach_attachment KLASS_PLAYER, $obj, $name, %arg; |
284 | delete $obj->{_attachment}{$name}; |
283 | } |
285 | reattach ($obj); |
|
|
286 | }; |
284 | |
287 | |
285 | sub cf::map::attach { |
288 | *cf::object::attached = |
|
|
289 | *cf::player::attached = |
|
|
290 | *cf::map::attached = sub { |
286 | my ($obj, $name, %arg) = @_; |
291 | my ($obj, $name) = @_; |
287 | |
292 | |
288 | _attach_attachment KLASS_MAP, $obj, $name, %arg; |
293 | exists $obj->{_attachment}{$name} |
289 | } |
294 | }; |
290 | |
295 | |
291 | sub attach_global { |
296 | sub attach_global { |
292 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
297 | _attach @CB_GLOBAL, KLASS_GLOBAL, @_ |
293 | } |
298 | } |
294 | |
299 | |
… | |
… | |
313 | |
318 | |
314 | sub register_attachment { |
319 | sub register_attachment { |
315 | my $name = shift; |
320 | my $name = shift; |
316 | |
321 | |
317 | $attachment{$name} = [[KLASS_OBJECT, @_]]; |
322 | $attachment{$name} = [[KLASS_OBJECT, @_]]; |
|
|
323 | } |
|
|
324 | |
|
|
325 | sub register_player_attachment { |
|
|
326 | my $name = shift; |
|
|
327 | |
|
|
328 | $attachment{$name} = [[KLASS_PLAYER, @_]]; |
|
|
329 | } |
|
|
330 | |
|
|
331 | sub register_map_attachment { |
|
|
332 | my $name = shift; |
|
|
333 | |
|
|
334 | $attachment{$name} = [[KLASS_MAP, @_]]; |
318 | } |
335 | } |
319 | |
336 | |
320 | our $override; |
337 | our $override; |
321 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
338 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
322 | |
339 | |
… | |
… | |
346 | } |
363 | } |
347 | |
364 | |
348 | 0 |
365 | 0 |
349 | } |
366 | } |
350 | |
367 | |
|
|
368 | =item $bool = cf::invoke EVENT_GLOBAL_XXX, ... |
|
|
369 | |
|
|
370 | =item $bool = $object->invoke (EVENT_OBJECT_XXX, ...) |
|
|
371 | |
|
|
372 | =item $bool = $player->invoke (EVENT_PLAYER_XXX, ...) |
|
|
373 | |
|
|
374 | =item $bool = $map->invoke (EVENT_MAP_XXX, ...) |
|
|
375 | |
|
|
376 | Generate a global/object/player/map-specific event with the given arguments. |
|
|
377 | |
|
|
378 | This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be |
|
|
379 | removed in future versions), and there is no public API to access override |
|
|
380 | results (if you must, access C<@cf::invoke_results> directly). |
|
|
381 | |
|
|
382 | =back |
|
|
383 | |
|
|
384 | =head2 methods valid for all pointers |
|
|
385 | |
|
|
386 | =over 4 |
|
|
387 | |
|
|
388 | =item $object->valid |
|
|
389 | |
|
|
390 | =item $player->valid |
|
|
391 | |
|
|
392 | =item $map->valid |
|
|
393 | |
|
|
394 | Just because you have a perl object does not mean that the corresponding |
|
|
395 | C-level object still exists. If you try to access an object that has no |
|
|
396 | valid C counterpart anymore you get an exception at runtime. This method |
|
|
397 | can be used to test for existence of the C object part without causing an |
|
|
398 | exception. |
|
|
399 | |
|
|
400 | =back |
|
|
401 | |
|
|
402 | =cut |
|
|
403 | |
|
|
404 | *cf::object::valid = |
|
|
405 | *cf::player::valid = |
|
|
406 | *cf::map::valid = \&cf::_valid; |
|
|
407 | |
351 | ############################################################################# |
408 | ############################################################################# |
352 | # object support |
409 | # object support |
353 | |
410 | |
354 | sub instantiate { |
411 | sub instantiate { |
355 | my ($obj, $data) = @_; |
412 | my ($obj, $data) = @_; |
… | |
… | |
368 | my ($obj) = @_; |
425 | my ($obj) = @_; |
369 | my $registry = $obj->registry; |
426 | my $registry = $obj->registry; |
370 | |
427 | |
371 | @$registry = (); |
428 | @$registry = (); |
372 | |
429 | |
373 | delete $obj->{_attachment} unless @{ $obj->{_attachment} || [] }; |
430 | delete $obj->{_attachment} unless scalar keys %{ $obj->{_attachment} || {} }; |
374 | |
431 | |
375 | for my $name (@{ $obj->{_attachment} || [] }) { |
432 | for my $name (keys %{ $obj->{_attachment} || {} }) { |
376 | if (my $attach = $attachment{$name}) { |
433 | if (my $attach = $attachment{$name}) { |
377 | for (@$attach) { |
434 | for (@$attach) { |
378 | my ($klass, @attach) = @$_; |
435 | my ($klass, @attach) = @$_; |
379 | _attach @$registry, $klass, @attach; |
436 | _attach @$registry, $klass, @attach; |
380 | } |
437 | } |
… | |
… | |
383 | } |
440 | } |
384 | } |
441 | } |
385 | } |
442 | } |
386 | |
443 | |
387 | sub object_freezer_save { |
444 | sub object_freezer_save { |
388 | my ($filename, $objs) = @_; |
445 | my ($filename, $rdata, $objs) = @_; |
389 | |
446 | |
390 | $filename .= ".pst"; |
447 | if (length $$rdata) { |
|
|
448 | warn sprintf "saving %s (%d,%d)\n", |
|
|
449 | $filename, length $$rdata, scalar @$objs; |
391 | |
450 | |
392 | if (@$objs) { |
|
|
393 | open my $fh, ">:raw", "$filename~"; |
451 | if (open my $fh, ">:raw", "$filename~") { |
394 | chmod $fh, SAVE_MODE; |
452 | chmod SAVE_MODE, $fh; |
|
|
453 | syswrite $fh, $$rdata; |
|
|
454 | close $fh; |
|
|
455 | |
|
|
456 | if (@$objs && open my $fh, ">:raw", "$filename.pst~") { |
|
|
457 | chmod SAVE_MODE, $fh; |
395 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
458 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
396 | close $fh; |
459 | close $fh; |
|
|
460 | rename "$filename.pst~", "$filename.pst"; |
|
|
461 | } else { |
|
|
462 | unlink "$filename.pst"; |
|
|
463 | } |
|
|
464 | |
397 | rename "$filename~", $filename; |
465 | rename "$filename~", $filename; |
|
|
466 | } else { |
|
|
467 | warn "FATAL: $filename~: $!\n"; |
|
|
468 | } |
398 | } else { |
469 | } else { |
399 | unlink $filename; |
470 | unlink $filename; |
|
|
471 | unlink "$filename.pst"; |
400 | } |
472 | } |
401 | } |
473 | } |
402 | |
474 | |
403 | sub object_thawer_load { |
475 | sub object_thawer_load { |
404 | my ($filename) = @_; |
476 | my ($filename) = @_; |
405 | |
477 | |
|
|
478 | local $/; |
|
|
479 | |
|
|
480 | my $av; |
|
|
481 | |
|
|
482 | #TODO: use sysread etc. |
|
|
483 | if (open my $data, "<:raw:perlio", $filename) { |
|
|
484 | $data = <$data>; |
406 | open my $fh, "<:raw:perlio", "$filename.pst" |
485 | if (open my $pst, "<:raw:perlio", "$filename.pst") { |
407 | or return; |
486 | $av = eval { (Storable::thaw <$pst>)->{objs} }; |
|
|
487 | } |
|
|
488 | return ($data, $av); |
|
|
489 | } |
408 | |
490 | |
409 | eval { local $/; (Storable::thaw <$fh>)->{objs} } |
491 | () |
410 | } |
492 | } |
411 | |
493 | |
412 | attach_to_objects |
494 | attach_to_objects |
413 | prio => -1000000, |
495 | prio => -1000000, |
414 | on_clone => sub { |
496 | on_clone => sub { |
… | |
… | |
416 | |
498 | |
417 | @{$dst->registry} = @{$src->registry}; |
499 | @{$dst->registry} = @{$src->registry}; |
418 | |
500 | |
419 | %$dst = %$src; |
501 | %$dst = %$src; |
420 | |
502 | |
421 | $dst->{_attachment} = [@{ $src->{_attachment} }] |
503 | %{$dst->{_attachment}} = %{$src->{_attachment}} |
422 | if exists $src->{_attachment}; |
504 | if exists $src->{_attachment}; |
423 | }, |
505 | }, |
424 | ; |
506 | ; |
425 | |
507 | |
426 | ############################################################################# |
508 | ############################################################################# |
… | |
… | |
610 | #Symbol::delete_package __PACKAGE__; |
692 | #Symbol::delete_package __PACKAGE__; |
611 | |
693 | |
612 | # 7. reload cf.pm |
694 | # 7. reload cf.pm |
613 | $msg->("reloading cf.pm"); |
695 | $msg->("reloading cf.pm"); |
614 | require cf; |
696 | require cf; |
|
|
697 | |
|
|
698 | $msg->("load extensions"); |
|
|
699 | cf::load_extensions; |
615 | }; |
700 | }; |
616 | $msg->($@) if $@; |
701 | $msg->($@) if $@; |
617 | |
702 | |
618 | $msg->("reloaded"); |
703 | $msg->("reloaded"); |
619 | }; |
704 | }; |
… | |
… | |
667 | my ($map) = @_; |
752 | my ($map) = @_; |
668 | |
753 | |
669 | my $path = $map->tmpname; |
754 | my $path = $map->tmpname; |
670 | defined $path or return; |
755 | defined $path or return; |
671 | |
756 | |
672 | unlink "$path.cfperl"; |
|
|
673 | unlink "$path.pst"; |
757 | unlink "$path.pst"; |
674 | }; |
758 | }; |
675 | |
759 | |
676 | *cf::mapsupport::on_swapin = |
|
|
677 | *cf::mapsupport::on_load = sub { |
|
|
678 | my ($map) = @_; |
|
|
679 | |
|
|
680 | my $path = $map->tmpname; |
|
|
681 | $path = $map->path unless defined $path; |
|
|
682 | |
|
|
683 | open my $fh, "<:raw", "$path.cfperl" |
|
|
684 | or return; # no perl data |
|
|
685 | |
|
|
686 | my $data = Storable::thaw do { local $/; <$fh> }; |
|
|
687 | |
|
|
688 | $data->{version} <= 1 |
|
|
689 | or return; # too new |
|
|
690 | |
|
|
691 | $map->_set_obs ($data->{obs}); |
|
|
692 | }; |
|
|
693 | |
|
|
694 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
760 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
695 | |
761 | |
696 | ############################################################################# |
762 | ############################################################################# |
697 | # load/save perl data associated with player->ob objects |
763 | # load/save perl data associated with player->ob objects |
698 | |
764 | |
699 | sub all_objects(@) { |
765 | sub all_objects(@) { |
700 | @_, map all_objects ($_->inv), @_ |
766 | @_, map all_objects ($_->inv), @_ |
701 | } |
767 | } |
702 | |
768 | |
|
|
769 | # TODO: compatibility cruft, remove when no longer needed |
703 | attach_to_players |
770 | attach_to_players |
704 | on_load => sub { |
771 | on_load => sub { |
705 | my ($pl, $path) = @_; |
772 | my ($pl, $path) = @_; |
706 | |
773 | |
707 | for my $o (all_objects $pl->ob) { |
774 | for my $o (all_objects $pl->ob) { |
… | |
… | |
828 | |
895 | |
829 | ############################################################################# |
896 | ############################################################################# |
830 | # the server's main() |
897 | # the server's main() |
831 | |
898 | |
832 | sub main { |
899 | sub main { |
|
|
900 | load_extensions; |
833 | Event::loop; |
901 | Event::loop; |
834 | } |
902 | } |
835 | |
903 | |
836 | ############################################################################# |
904 | ############################################################################# |
837 | # initialisation |
905 | # initialisation |
838 | |
906 | |
839 | register "<global>", __PACKAGE__; |
907 | register "<global>", __PACKAGE__; |
840 | |
908 | |
841 | unshift @INC, $LIBDIR; |
909 | unshift @INC, $LIBDIR; |
842 | |
|
|
843 | load_extensions; |
|
|
844 | |
910 | |
845 | $TICK_WATCHER = Event->timer ( |
911 | $TICK_WATCHER = Event->timer ( |
846 | prio => 1, |
912 | prio => 1, |
847 | at => $NEXT_TICK || 1, |
913 | at => $NEXT_TICK || 1, |
848 | cb => sub { |
914 | cb => sub { |