… | |
… | |
11 | use Event; |
11 | use Event; |
12 | $Event::Eval = 1; # no idea why this is required, but it is |
12 | $Event::Eval = 1; # no idea why this is required, but it is |
13 | |
13 | |
14 | use strict; |
14 | use strict; |
15 | |
15 | |
16 | _reload_1; |
16 | _init_vars; |
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; |
… | |
… | |
33 | |
31 | |
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 | |
|
|
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 | |
36 | |
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. |
… | |
… | |
476 | } |
440 | } |
477 | } |
441 | } |
478 | } |
442 | } |
479 | |
443 | |
480 | sub object_freezer_save { |
444 | sub object_freezer_save { |
481 | my ($filename, $objs) = @_; |
445 | my ($filename, $rdata, $objs) = @_; |
482 | |
446 | |
483 | if (@$objs) { |
447 | if (length $$rdata) { |
|
|
448 | warn sprintf "saving %s (%d,%d)\n", |
|
|
449 | $filename, length $$rdata, scalar @$objs; |
|
|
450 | |
484 | open my $fh, ">:raw", "$filename.pst~"; |
451 | if (open my $fh, ">:raw", "$filename~") { |
|
|
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; |
485 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
458 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
486 | close $fh; |
459 | close $fh; |
487 | chmod SAVE_MODE, "$filename.pst~"; |
|
|
488 | rename "$filename.pst~", "$filename.pst"; |
460 | rename "$filename.pst~", "$filename.pst"; |
|
|
461 | } else { |
|
|
462 | unlink "$filename.pst"; |
|
|
463 | } |
|
|
464 | |
|
|
465 | rename "$filename~", $filename; |
|
|
466 | } else { |
|
|
467 | warn "FATAL: $filename~: $!\n"; |
|
|
468 | } |
489 | } else { |
469 | } else { |
|
|
470 | unlink $filename; |
490 | unlink "$filename.pst"; |
471 | unlink "$filename.pst"; |
491 | } |
472 | } |
492 | |
|
|
493 | chmod SAVE_MODE, "$filename~"; |
|
|
494 | rename "$filename~", $filename; |
|
|
495 | } |
473 | } |
496 | |
474 | |
497 | sub object_thawer_load { |
475 | sub object_thawer_load { |
498 | my ($filename) = @_; |
476 | my ($filename) = @_; |
499 | |
477 | |
|
|
478 | local $/; |
|
|
479 | |
|
|
480 | my $av; |
|
|
481 | |
|
|
482 | #TODO: use sysread etc. |
|
|
483 | if (open my $data, "<:raw:perlio", $filename) { |
|
|
484 | $data = <$data>; |
500 | open my $fh, "<:raw:perlio", "$filename.pst" |
485 | if (open my $pst, "<:raw:perlio", "$filename.pst") { |
501 | or return; |
486 | $av = eval { (Storable::thaw <$pst>)->{objs} }; |
|
|
487 | } |
|
|
488 | return ($data, $av); |
|
|
489 | } |
502 | |
490 | |
503 | eval { local $/; (Storable::thaw <$fh>)->{objs} } |
491 | () |
504 | } |
492 | } |
505 | |
493 | |
506 | attach_to_objects |
494 | attach_to_objects |
507 | prio => -1000000, |
495 | prio => -1000000, |
508 | on_clone => sub { |
496 | on_clone => sub { |
… | |
… | |
704 | #Symbol::delete_package __PACKAGE__; |
692 | #Symbol::delete_package __PACKAGE__; |
705 | |
693 | |
706 | # 7. reload cf.pm |
694 | # 7. reload cf.pm |
707 | $msg->("reloading cf.pm"); |
695 | $msg->("reloading cf.pm"); |
708 | require cf; |
696 | require cf; |
|
|
697 | |
|
|
698 | $msg->("load extensions"); |
|
|
699 | cf::load_extensions; |
|
|
700 | |
|
|
701 | $msg->("reattach"); |
|
|
702 | _global_reattach; |
709 | }; |
703 | }; |
710 | $msg->($@) if $@; |
704 | $msg->($@) if $@; |
711 | |
705 | |
712 | $msg->("reloaded"); |
706 | $msg->("reloaded"); |
713 | }; |
707 | }; |
… | |
… | |
764 | defined $path or return; |
758 | defined $path or return; |
765 | |
759 | |
766 | unlink "$path.pst"; |
760 | unlink "$path.pst"; |
767 | }; |
761 | }; |
768 | |
762 | |
769 | # old style persistent data, TODO: remove #d# |
|
|
770 | *cf::mapsupport::on_swapin = |
|
|
771 | *cf::mapsupport::on_load = sub { |
|
|
772 | my ($map) = @_; |
|
|
773 | |
|
|
774 | my $path = $map->tmpname; |
|
|
775 | $path = $map->path unless defined $path; |
|
|
776 | |
|
|
777 | open my $fh, "<:raw", "$path.cfperl" |
|
|
778 | or return; # no perl data |
|
|
779 | |
|
|
780 | my $data = Storable::thaw do { local $/; <$fh> }; |
|
|
781 | |
|
|
782 | $data->{version} <= 1 |
|
|
783 | or return; # too new |
|
|
784 | |
|
|
785 | $map->_set_obs ($data->{obs}); |
|
|
786 | $map->invoke (EVENT_MAP_UPGRADE); |
|
|
787 | }; |
|
|
788 | |
|
|
789 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
763 | attach_to_maps prio => -10000, package => cf::mapsupport::; |
790 | |
764 | |
791 | ############################################################################# |
765 | ############################################################################# |
792 | # load/save perl data associated with player->ob objects |
766 | # load/save perl data associated with player->ob objects |
793 | |
767 | |
794 | sub all_objects(@) { |
768 | sub all_objects(@) { |
795 | @_, map all_objects ($_->inv), @_ |
769 | @_, map all_objects ($_->inv), @_ |
796 | } |
770 | } |
797 | |
771 | |
|
|
772 | # TODO: compatibility cruft, remove when no longer needed |
798 | attach_to_players |
773 | attach_to_players |
799 | on_load => sub { |
774 | on_load => sub { |
800 | my ($pl, $path) = @_; |
775 | my ($pl, $path) = @_; |
801 | |
776 | |
802 | for my $o (all_objects $pl->ob) { |
777 | for my $o (all_objects $pl->ob) { |
… | |
… | |
923 | |
898 | |
924 | ############################################################################# |
899 | ############################################################################# |
925 | # the server's main() |
900 | # the server's main() |
926 | |
901 | |
927 | sub main { |
902 | sub main { |
|
|
903 | load_extensions; |
928 | Event::loop; |
904 | Event::loop; |
929 | } |
905 | } |
930 | |
906 | |
931 | ############################################################################# |
907 | ############################################################################# |
932 | # initialisation |
908 | # initialisation |
933 | |
909 | |
934 | register "<global>", __PACKAGE__; |
910 | register "<global>", __PACKAGE__; |
935 | |
911 | |
936 | unshift @INC, $LIBDIR; |
912 | unshift @INC, $LIBDIR; |
937 | |
|
|
938 | load_extensions; |
|
|
939 | |
913 | |
940 | $TICK_WATCHER = Event->timer ( |
914 | $TICK_WATCHER = Event->timer ( |
941 | prio => 1, |
915 | prio => 1, |
942 | at => $NEXT_TICK || 1, |
916 | at => $NEXT_TICK || 1, |
943 | cb => sub { |
917 | cb => sub { |
… | |
… | |
952 | $TICK_WATCHER->at ($NEXT_TICK); |
926 | $TICK_WATCHER->at ($NEXT_TICK); |
953 | $TICK_WATCHER->start; |
927 | $TICK_WATCHER->start; |
954 | }, |
928 | }, |
955 | ); |
929 | ); |
956 | |
930 | |
957 | _reload_2; |
|
|
958 | |
|
|
959 | 1 |
931 | 1 |
960 | |
932 | |