… | |
… | |
5 | use Storable; |
5 | use Storable; |
6 | use Opcode; |
6 | use Opcode; |
7 | use Safe; |
7 | use Safe; |
8 | use Safe::Hole; |
8 | use Safe::Hole; |
9 | |
9 | |
|
|
10 | use IO::AIO (); |
|
|
11 | use YAML::Syck (); |
10 | use Time::HiRes; |
12 | use Time::HiRes; |
11 | use Event; |
13 | use Event; |
12 | $Event::Eval = 1; # no idea why this is required, but it is |
14 | $Event::Eval = 1; # no idea why this is required, but it is |
13 | |
15 | |
|
|
16 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
|
|
17 | $YAML::Syck::ImplicitUnicode = 1; |
|
|
18 | |
14 | use strict; |
19 | use strict; |
15 | |
20 | |
16 | _reload_1; |
21 | _init_vars; |
17 | |
22 | |
18 | our %COMMAND = (); |
23 | our %COMMAND = (); |
19 | our @EVENT; |
24 | our @EVENT; |
20 | our %PROP_TYPE; |
|
|
21 | our %PROP_IDX; |
|
|
22 | our $LIBDIR = maps_directory "perl"; |
25 | our $LIBDIR = maps_directory "perl"; |
23 | |
26 | |
24 | our $TICK = MAX_TIME * 1e-6; |
27 | our $TICK = MAX_TIME * 1e-6; |
25 | our $TICK_WATCHER; |
28 | our $TICK_WATCHER; |
26 | our $NEXT_TICK; |
29 | our $NEXT_TICK; |
|
|
30 | |
|
|
31 | our %CFG; |
|
|
32 | |
|
|
33 | our $uptime; |
|
|
34 | |
|
|
35 | $uptime ||= time; |
|
|
36 | |
|
|
37 | ############################################################################# |
|
|
38 | |
|
|
39 | =head2 GLOBAL VARIABLES |
|
|
40 | |
|
|
41 | =over 4 |
|
|
42 | |
|
|
43 | =item $cf::LIBDIR |
|
|
44 | |
|
|
45 | The perl library directory, where extensions and cf-specific modules can |
|
|
46 | be found. It will be added to C<@INC> automatically. |
|
|
47 | |
|
|
48 | =item $cf::TICK |
|
|
49 | |
|
|
50 | The interval between server ticks, in seconds. |
|
|
51 | |
|
|
52 | =item %cf::CFG |
|
|
53 | |
|
|
54 | Configuration for the server, loaded from C</etc/crossfire/config>, or |
|
|
55 | from wherever your confdir points to. |
|
|
56 | |
|
|
57 | =back |
|
|
58 | |
|
|
59 | =cut |
27 | |
60 | |
28 | BEGIN { |
61 | BEGIN { |
29 | *CORE::GLOBAL::warn = sub { |
62 | *CORE::GLOBAL::warn = sub { |
30 | my $msg = join "", @_; |
63 | my $msg = join "", @_; |
31 | $msg .= "\n" |
64 | $msg .= "\n" |
… | |
… | |
34 | print STDERR "cfperl: $msg"; |
67 | print STDERR "cfperl: $msg"; |
35 | LOG llevError, "cfperl: $msg"; |
68 | LOG llevError, "cfperl: $msg"; |
36 | }; |
69 | }; |
37 | } |
70 | } |
38 | |
71 | |
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'; |
72 | @safe::cf::object::player::ISA = @cf::object::player::ISA = 'cf::object'; |
74 | |
73 | |
75 | # we bless all objects into (empty) derived classes to force a method lookup |
74 | # we bless all objects into (empty) derived classes to force a method lookup |
76 | # within the Safe compartment. |
75 | # within the Safe compartment. |
77 | for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch cf::living)) { |
76 | for my $pkg (qw(cf::object cf::object::player cf::player cf::map cf::party cf::region cf::arch cf::living)) { |
… | |
… | |
87 | my @exts; |
86 | my @exts; |
88 | my @hook; |
87 | my @hook; |
89 | my %command; |
88 | my %command; |
90 | my %extcmd; |
89 | my %extcmd; |
91 | |
90 | |
92 | ############################################################################# |
91 | =head2 UTILITY FUNCTIONS |
93 | # utility functions |
92 | |
|
|
93 | =over 4 |
|
|
94 | |
|
|
95 | =cut |
94 | |
96 | |
95 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
97 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
|
|
98 | |
|
|
99 | =item $ref = cf::from_json $json |
|
|
100 | |
|
|
101 | Converts a JSON string into the corresponding perl data structure. |
|
|
102 | |
|
|
103 | =cut |
96 | |
104 | |
97 | sub from_json($) { |
105 | sub from_json($) { |
98 | $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs |
106 | $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs |
99 | JSON::Syck::Load $_[0] |
107 | JSON::Syck::Load $_[0] |
100 | } |
108 | } |
101 | |
109 | |
|
|
110 | =item $json = cf::to_json $ref |
|
|
111 | |
|
|
112 | Converts a perl data structure into its JSON representation. |
|
|
113 | |
|
|
114 | =cut |
|
|
115 | |
102 | sub to_json($) { |
116 | sub to_json($) { |
103 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
117 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
104 | JSON::Syck::Dump $_[0] |
118 | JSON::Syck::Dump $_[0] |
105 | } |
119 | } |
106 | |
120 | |
107 | ############################################################################# |
121 | =back |
108 | # "new" plug-in system |
|
|
109 | |
122 | |
|
|
123 | =cut |
|
|
124 | |
|
|
125 | ############################################################################# |
|
|
126 | |
110 | =head3 EVENTS AND OBJECT ATTACHMENTS |
127 | =head2 EVENTS AND OBJECT ATTACHMENTS |
111 | |
128 | |
112 | =over 4 |
129 | =over 4 |
113 | |
130 | |
114 | =item $object->attach ($attachment, key => $value...) |
131 | =item $object->attach ($attachment, key => $value...) |
115 | |
132 | |
… | |
… | |
389 | for (@$callbacks) { |
406 | for (@$callbacks) { |
390 | eval { &{$_->[1]} }; |
407 | eval { &{$_->[1]} }; |
391 | |
408 | |
392 | if ($@) { |
409 | if ($@) { |
393 | warn "$@"; |
410 | warn "$@"; |
394 | warn "... while processing $EVENT[$event][0] event, skipping processing altogether.\n"; |
411 | warn "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n"; |
395 | override; |
412 | override; |
396 | } |
413 | } |
397 | |
414 | |
398 | return 1 if $override; |
415 | return 1 if $override; |
399 | } |
416 | } |
… | |
… | |
415 | removed in future versions), and there is no public API to access override |
432 | removed in future versions), and there is no public API to access override |
416 | results (if you must, access C<@cf::invoke_results> directly). |
433 | results (if you must, access C<@cf::invoke_results> directly). |
417 | |
434 | |
418 | =back |
435 | =back |
419 | |
436 | |
420 | =head2 methods valid for all pointers |
437 | =cut |
|
|
438 | |
|
|
439 | ############################################################################# |
|
|
440 | |
|
|
441 | =head2 METHODS VALID FOR ALL CORE OBJECTS |
421 | |
442 | |
422 | =over 4 |
443 | =over 4 |
423 | |
444 | |
424 | =item $object->valid |
445 | =item $object->valid, $player->valid, $map->valid |
425 | |
|
|
426 | =item $player->valid |
|
|
427 | |
|
|
428 | =item $map->valid |
|
|
429 | |
446 | |
430 | Just because you have a perl object does not mean that the corresponding |
447 | 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 |
448 | 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 |
449 | 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 |
450 | can be used to test for existence of the C object part without causing an |
… | |
… | |
478 | } |
495 | } |
479 | |
496 | |
480 | sub object_freezer_save { |
497 | sub object_freezer_save { |
481 | my ($filename, $rdata, $objs) = @_; |
498 | my ($filename, $rdata, $objs) = @_; |
482 | |
499 | |
|
|
500 | if (length $$rdata) { |
|
|
501 | warn sprintf "saving %s (%d,%d)\n", |
|
|
502 | $filename, length $$rdata, scalar @$objs; |
|
|
503 | |
483 | if (open my $fh, ">:raw", "$filename~") { |
504 | if (open my $fh, ">:raw", "$filename~") { |
484 | chmod SAVE_MODE, $fh; |
|
|
485 | syswrite $fh, $$rdata; |
|
|
486 | close $fh; |
|
|
487 | |
|
|
488 | if (@$objs && open my $fh, ">:raw", "$filename.pst~") { |
|
|
489 | chmod SAVE_MODE, $fh; |
505 | chmod SAVE_MODE, $fh; |
490 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
506 | syswrite $fh, $$rdata; |
491 | close $fh; |
507 | close $fh; |
|
|
508 | |
|
|
509 | if (@$objs && open my $fh, ">:raw", "$filename.pst~") { |
|
|
510 | chmod SAVE_MODE, $fh; |
|
|
511 | syswrite $fh, Storable::nfreeze { version => 1, objs => $objs }; |
|
|
512 | close $fh; |
492 | rename "$filename.pst~", "$filename.pst"; |
513 | rename "$filename.pst~", "$filename.pst"; |
|
|
514 | } else { |
|
|
515 | unlink "$filename.pst"; |
|
|
516 | } |
|
|
517 | |
|
|
518 | rename "$filename~", $filename; |
493 | } else { |
519 | } else { |
494 | unlink "$filename.pst"; |
520 | warn "FATAL: $filename~: $!\n"; |
495 | } |
521 | } |
496 | |
|
|
497 | rename "$filename~", $filename; |
|
|
498 | } else { |
522 | } else { |
499 | warn "FATAL: $filename~: $!\n"; |
523 | unlink $filename; |
|
|
524 | unlink "$filename.pst"; |
500 | } |
525 | } |
501 | } |
526 | } |
502 | |
527 | |
503 | sub object_thawer_load { |
528 | sub object_thawer_load { |
504 | my ($filename) = @_; |
529 | my ($filename) = @_; |
505 | |
530 | |
|
|
531 | local $/; |
|
|
532 | |
|
|
533 | my $av; |
|
|
534 | |
|
|
535 | #TODO: use sysread etc. |
|
|
536 | if (open my $data, "<:raw:perlio", $filename) { |
|
|
537 | $data = <$data>; |
506 | open my $fh, "<:raw:perlio", "$filename.pst" |
538 | if (open my $pst, "<:raw:perlio", "$filename.pst") { |
507 | or return; |
539 | $av = eval { (Storable::thaw <$pst>)->{objs} }; |
|
|
540 | } |
|
|
541 | return ($data, $av); |
|
|
542 | } |
508 | |
543 | |
509 | eval { local $/; (Storable::thaw <$fh>)->{objs} } |
544 | () |
510 | } |
545 | } |
511 | |
546 | |
512 | attach_to_objects |
547 | attach_to_objects |
513 | prio => -1000000, |
548 | prio => -1000000, |
514 | on_clone => sub { |
549 | on_clone => sub { |
… | |
… | |
660 | load_extension $ext; |
695 | load_extension $ext; |
661 | 1 |
696 | 1 |
662 | } or warn "$ext not loaded: $@"; |
697 | } or warn "$ext not loaded: $@"; |
663 | } |
698 | } |
664 | } |
699 | } |
665 | |
|
|
666 | sub _perl_reload(&) { |
|
|
667 | my ($msg) = @_; |
|
|
668 | |
|
|
669 | $msg->("reloading..."); |
|
|
670 | |
|
|
671 | eval { |
|
|
672 | # 1. cancel all watchers |
|
|
673 | $_->cancel for Event::all_watchers; |
|
|
674 | |
|
|
675 | # 2. unload all extensions |
|
|
676 | for (@exts) { |
|
|
677 | $msg->("unloading <$_>"); |
|
|
678 | unload_extension $_; |
|
|
679 | } |
|
|
680 | |
|
|
681 | # 3. unload all modules loaded from $LIBDIR |
|
|
682 | while (my ($k, $v) = each %INC) { |
|
|
683 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
|
|
684 | |
|
|
685 | $msg->("removing <$k>"); |
|
|
686 | delete $INC{$k}; |
|
|
687 | |
|
|
688 | $k =~ s/\.pm$//; |
|
|
689 | $k =~ s/\//::/g; |
|
|
690 | |
|
|
691 | if (my $cb = $k->can ("unload_module")) { |
|
|
692 | $cb->(); |
|
|
693 | } |
|
|
694 | |
|
|
695 | Symbol::delete_package $k; |
|
|
696 | } |
|
|
697 | |
|
|
698 | # 4. get rid of safe::, as good as possible |
|
|
699 | Symbol::delete_package "safe::$_" |
|
|
700 | for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region); |
|
|
701 | |
|
|
702 | # 5. remove register_script_function callbacks |
|
|
703 | # TODO |
|
|
704 | |
|
|
705 | # 6. unload cf.pm "a bit" |
|
|
706 | delete $INC{"cf.pm"}; |
|
|
707 | |
|
|
708 | # don't, removes xs symbols, too, |
|
|
709 | # and global variables created in xs |
|
|
710 | #Symbol::delete_package __PACKAGE__; |
|
|
711 | |
|
|
712 | # 7. reload cf.pm |
|
|
713 | $msg->("reloading cf.pm"); |
|
|
714 | require cf; |
|
|
715 | }; |
|
|
716 | $msg->($@) if $@; |
|
|
717 | |
|
|
718 | $msg->("reloaded"); |
|
|
719 | }; |
|
|
720 | |
|
|
721 | sub perl_reload() { |
|
|
722 | _perl_reload { |
|
|
723 | warn $_[0]; |
|
|
724 | print "$_[0]\n"; |
|
|
725 | }; |
|
|
726 | } |
|
|
727 | |
|
|
728 | register_command "perl-reload", 0, sub { |
|
|
729 | my ($who, $arg) = @_; |
|
|
730 | |
|
|
731 | if ($who->flag (FLAG_WIZ)) { |
|
|
732 | _perl_reload { |
|
|
733 | warn $_[0]; |
|
|
734 | $who->message ($_[0]); |
|
|
735 | }; |
|
|
736 | } |
|
|
737 | }; |
|
|
738 | |
700 | |
739 | ############################################################################# |
701 | ############################################################################# |
740 | # extcmd framework, basically convert ext <msg> |
702 | # extcmd framework, basically convert ext <msg> |
741 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
703 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
742 | |
704 | |
… | |
… | |
779 | |
741 | |
780 | sub all_objects(@) { |
742 | sub all_objects(@) { |
781 | @_, map all_objects ($_->inv), @_ |
743 | @_, map all_objects ($_->inv), @_ |
782 | } |
744 | } |
783 | |
745 | |
|
|
746 | # TODO: compatibility cruft, remove when no longer needed |
784 | attach_to_players |
747 | attach_to_players |
785 | on_load => sub { |
748 | on_load => sub { |
786 | my ($pl, $path) = @_; |
749 | my ($pl, $path) = @_; |
787 | |
750 | |
788 | for my $o (all_objects $pl->ob) { |
751 | for my $o (all_objects $pl->ob) { |
… | |
… | |
794 | } |
757 | } |
795 | }, |
758 | }, |
796 | ; |
759 | ; |
797 | |
760 | |
798 | ############################################################################# |
761 | ############################################################################# |
799 | # core extensions - in perl |
762 | |
|
|
763 | =head2 CORE EXTENSIONS |
|
|
764 | |
|
|
765 | Functions and methods that extend core crossfire objects. |
|
|
766 | |
|
|
767 | =over 4 |
800 | |
768 | |
801 | =item cf::player::exists $login |
769 | =item cf::player::exists $login |
802 | |
770 | |
803 | Returns true when the given account exists. |
771 | Returns true when the given account exists. |
804 | |
772 | |
… | |
… | |
807 | sub cf::player::exists($) { |
775 | sub cf::player::exists($) { |
808 | cf::player::find $_[0] |
776 | cf::player::find $_[0] |
809 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
777 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
810 | } |
778 | } |
811 | |
779 | |
812 | =item $player->reply ($npc, $msg[, $flags]) |
780 | =item $player_object->reply ($npc, $msg[, $flags]) |
813 | |
781 | |
814 | Sends a message to the player, as if the npc C<$npc> replied. C<$npc> |
782 | Sends a message to the player, as if the npc C<$npc> replied. C<$npc> |
815 | can be C<undef>. Does the right thing when the player is currently in a |
783 | can be C<undef>. Does the right thing when the player is currently in a |
816 | dialogue with the given NPC character. |
784 | dialogue with the given NPC character. |
817 | |
785 | |
… | |
… | |
844 | $msg{msgid} = $id; |
812 | $msg{msgid} = $id; |
845 | |
813 | |
846 | $self->send ("ext " . to_json \%msg); |
814 | $self->send ("ext " . to_json \%msg); |
847 | } |
815 | } |
848 | |
816 | |
|
|
817 | =item $player_object->may ("access") |
|
|
818 | |
|
|
819 | Returns wether the given player is authorized to access resource "access" |
|
|
820 | (e.g. "command_wizcast"). |
|
|
821 | |
|
|
822 | =cut |
|
|
823 | |
|
|
824 | sub cf::object::player::may { |
|
|
825 | my ($self, $access) = @_; |
|
|
826 | |
|
|
827 | $self->flag (cf::FLAG_WIZ) || |
|
|
828 | (ref $cf::CFG{"may_$access"} |
|
|
829 | ? scalar grep $self->name eq $_, @{$cf::CFG{"may_$access"}} |
|
|
830 | : $cf::CFG{"may_$access"}) |
|
|
831 | } |
|
|
832 | |
|
|
833 | =cut |
|
|
834 | |
849 | ############################################################################# |
835 | ############################################################################# |
850 | # map scripting support |
836 | |
|
|
837 | =head2 SAFE SCRIPTING |
|
|
838 | |
|
|
839 | Functions that provide a safe environment to compile and execute |
|
|
840 | snippets of perl code without them endangering the safety of the server |
|
|
841 | itself. Looping constructs, I/O operators and other built-in functionality |
|
|
842 | is not available in the safe scripting environment, and the number of |
|
|
843 | functions and methods that can be called is greatly reduced. |
|
|
844 | |
|
|
845 | =cut |
851 | |
846 | |
852 | our $safe = new Safe "safe"; |
847 | our $safe = new Safe "safe"; |
853 | our $safe_hole = new Safe::Hole; |
848 | our $safe_hole = new Safe::Hole; |
854 | |
849 | |
855 | $SIG{FPE} = 'IGNORE'; |
850 | $SIG{FPE} = 'IGNORE'; |
856 | |
851 | |
857 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
852 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
858 | |
853 | |
859 | # here we export the classes and methods available to script code |
854 | # here we export the classes and methods available to script code |
|
|
855 | |
|
|
856 | =pod |
|
|
857 | |
|
|
858 | The following fucntions and emthods are available within a safe environment: |
|
|
859 | |
|
|
860 | cf::object contr pay_amount pay_player |
|
|
861 | cf::object::player player |
|
|
862 | cf::player peaceful |
|
|
863 | |
|
|
864 | =cut |
860 | |
865 | |
861 | for ( |
866 | for ( |
862 | ["cf::object" => qw(contr pay_amount pay_player)], |
867 | ["cf::object" => qw(contr pay_amount pay_player)], |
863 | ["cf::object::player" => qw(player)], |
868 | ["cf::object::player" => qw(player)], |
864 | ["cf::player" => qw(peaceful)], |
869 | ["cf::player" => qw(peaceful)], |
… | |
… | |
867 | my ($pkg, @funs) = @$_; |
872 | my ($pkg, @funs) = @$_; |
868 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
873 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
869 | for @funs; |
874 | for @funs; |
870 | } |
875 | } |
871 | |
876 | |
|
|
877 | =over 4 |
|
|
878 | |
|
|
879 | =item @retval = safe_eval $code, [var => value, ...] |
|
|
880 | |
|
|
881 | Compiled and executes the given perl code snippet. additional var/value |
|
|
882 | pairs result in temporary local (my) scalar variables of the given name |
|
|
883 | that are available in the code snippet. Example: |
|
|
884 | |
|
|
885 | my $five = safe_eval '$first + $second', first => 1, second => 4; |
|
|
886 | |
|
|
887 | =cut |
|
|
888 | |
872 | sub safe_eval($;@) { |
889 | sub safe_eval($;@) { |
873 | my ($code, %vars) = @_; |
890 | my ($code, %vars) = @_; |
874 | |
891 | |
875 | my $qcode = $code; |
892 | my $qcode = $code; |
876 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
893 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
… | |
… | |
898 | } |
915 | } |
899 | |
916 | |
900 | wantarray ? @res : $res[0] |
917 | wantarray ? @res : $res[0] |
901 | } |
918 | } |
902 | |
919 | |
|
|
920 | =item cf::register_script_function $function => $cb |
|
|
921 | |
|
|
922 | Register a function that can be called from within map/npc scripts. The |
|
|
923 | function should be reasonably secure and should be put into a package name |
|
|
924 | like the extension. |
|
|
925 | |
|
|
926 | Example: register a function that gets called whenever a map script calls |
|
|
927 | C<rent::overview>, as used by the C<rent> extension. |
|
|
928 | |
|
|
929 | cf::register_script_function "rent::overview" => sub { |
|
|
930 | ... |
|
|
931 | }; |
|
|
932 | |
|
|
933 | =cut |
|
|
934 | |
903 | sub register_script_function { |
935 | sub register_script_function { |
904 | my ($fun, $cb) = @_; |
936 | my ($fun, $cb) = @_; |
905 | |
937 | |
906 | no strict 'refs'; |
938 | no strict 'refs'; |
907 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
939 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
908 | } |
940 | } |
909 | |
941 | |
|
|
942 | =back |
|
|
943 | |
|
|
944 | =cut |
|
|
945 | |
|
|
946 | ############################################################################# |
|
|
947 | |
|
|
948 | =head2 EXTENSION DATABASE SUPPORT |
|
|
949 | |
|
|
950 | Crossfire maintains a very simple database for extension use. It can |
|
|
951 | currently store anything that can be serialised using Storable, which |
|
|
952 | excludes objects. |
|
|
953 | |
|
|
954 | The parameter C<$family> should best start with the name of the extension |
|
|
955 | using it, it should be unique. |
|
|
956 | |
|
|
957 | =over 4 |
|
|
958 | |
|
|
959 | =item $hashref = cf::db_get $family |
|
|
960 | |
|
|
961 | Return a hashref for use by the extension C<$family>, which can be |
|
|
962 | modified. After modifications, you have to call C<cf::db_dirty> or |
|
|
963 | C<cf::db_sync>. |
|
|
964 | |
|
|
965 | =item $value = cf::db_get $family => $key |
|
|
966 | |
|
|
967 | Returns a single value from the database |
|
|
968 | |
|
|
969 | =item cf::db_put $family => $hashref |
|
|
970 | |
|
|
971 | Stores the given family hashref into the database. Updates are delayed, if |
|
|
972 | you want the data to be synced to disk immediately, use C<cf::db_sync>. |
|
|
973 | |
|
|
974 | =item cf::db_put $family => $key => $value |
|
|
975 | |
|
|
976 | Stores the given C<$value> in the family hash. Updates are delayed, if you |
|
|
977 | want the data to be synced to disk immediately, use C<cf::db_sync>. |
|
|
978 | |
|
|
979 | =item cf::db_dirty |
|
|
980 | |
|
|
981 | Marks the database as dirty, to be updated at a later time. |
|
|
982 | |
|
|
983 | =item cf::db_sync |
|
|
984 | |
|
|
985 | Immediately write the database to disk I<if it is dirty>. |
|
|
986 | |
|
|
987 | =cut |
|
|
988 | |
|
|
989 | our $DB; |
|
|
990 | |
|
|
991 | { |
|
|
992 | my $path = cf::localdir . "/database.pst"; |
|
|
993 | |
|
|
994 | sub db_load() { |
|
|
995 | warn "loading database $path\n";#d# remove later |
|
|
996 | $DB = stat $path ? Storable::retrieve $path : { }; |
|
|
997 | } |
|
|
998 | |
|
|
999 | my $pid; |
|
|
1000 | |
|
|
1001 | sub db_save() { |
|
|
1002 | warn "saving database $path\n";#d# remove later |
|
|
1003 | waitpid $pid, 0 if $pid; |
|
|
1004 | if (0 == ($pid = fork)) { |
|
|
1005 | $DB->{_meta}{version} = 1; |
|
|
1006 | Storable::nstore $DB, "$path~"; |
|
|
1007 | rename "$path~", $path; |
|
|
1008 | cf::_exit 0 if defined $pid; |
|
|
1009 | } |
|
|
1010 | } |
|
|
1011 | |
|
|
1012 | my $dirty; |
|
|
1013 | |
|
|
1014 | sub db_sync() { |
|
|
1015 | db_save if $dirty; |
|
|
1016 | undef $dirty; |
|
|
1017 | } |
|
|
1018 | |
|
|
1019 | my $idle = Event->idle (min => $TICK * 2.8, max => 10, repeat => 0, cb => sub { |
|
|
1020 | db_sync; |
|
|
1021 | }); |
|
|
1022 | |
|
|
1023 | sub db_dirty() { |
|
|
1024 | $dirty = 1; |
|
|
1025 | $idle->start; |
|
|
1026 | } |
|
|
1027 | |
|
|
1028 | sub db_get($;$) { |
|
|
1029 | @_ >= 2 |
|
|
1030 | ? $DB->{$_[0]}{$_[1]} |
|
|
1031 | : ($DB->{$_[0]} ||= { }) |
|
|
1032 | } |
|
|
1033 | |
|
|
1034 | sub db_put($$;$) { |
|
|
1035 | if (@_ >= 3) { |
|
|
1036 | $DB->{$_[0]}{$_[1]} = $_[2]; |
|
|
1037 | } else { |
|
|
1038 | $DB->{$_[0]} = $_[1]; |
|
|
1039 | } |
|
|
1040 | db_dirty; |
|
|
1041 | } |
|
|
1042 | |
|
|
1043 | attach_global |
|
|
1044 | prio => 10000, |
|
|
1045 | on_cleanup => sub { |
|
|
1046 | db_sync; |
|
|
1047 | }, |
|
|
1048 | ; |
|
|
1049 | } |
|
|
1050 | |
910 | ############################################################################# |
1051 | ############################################################################# |
911 | # the server's main() |
1052 | # the server's main() |
912 | |
1053 | |
|
|
1054 | sub cfg_load { |
|
|
1055 | open my $fh, "<:utf8", cf::confdir . "/config" |
|
|
1056 | or return; |
|
|
1057 | |
|
|
1058 | local $/; |
|
|
1059 | *CFG = YAML::Syck::Load <$fh>; |
|
|
1060 | } |
|
|
1061 | |
913 | sub main { |
1062 | sub main { |
|
|
1063 | cfg_load; |
|
|
1064 | db_load; |
|
|
1065 | load_extensions; |
914 | Event::loop; |
1066 | Event::loop; |
915 | } |
1067 | } |
916 | |
1068 | |
917 | ############################################################################# |
1069 | ############################################################################# |
918 | # initialisation |
1070 | # initialisation |
919 | |
1071 | |
|
|
1072 | sub _perl_reload(&) { |
|
|
1073 | my ($msg) = @_; |
|
|
1074 | |
|
|
1075 | $msg->("reloading..."); |
|
|
1076 | |
|
|
1077 | eval { |
|
|
1078 | # cancel all watchers |
|
|
1079 | $_->cancel for Event::all_watchers; |
|
|
1080 | |
|
|
1081 | # unload all extensions |
|
|
1082 | for (@exts) { |
|
|
1083 | $msg->("unloading <$_>"); |
|
|
1084 | unload_extension $_; |
|
|
1085 | } |
|
|
1086 | |
|
|
1087 | # unload all modules loaded from $LIBDIR |
|
|
1088 | while (my ($k, $v) = each %INC) { |
|
|
1089 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
|
|
1090 | |
|
|
1091 | $msg->("removing <$k>"); |
|
|
1092 | delete $INC{$k}; |
|
|
1093 | |
|
|
1094 | $k =~ s/\.pm$//; |
|
|
1095 | $k =~ s/\//::/g; |
|
|
1096 | |
|
|
1097 | if (my $cb = $k->can ("unload_module")) { |
|
|
1098 | $cb->(); |
|
|
1099 | } |
|
|
1100 | |
|
|
1101 | Symbol::delete_package $k; |
|
|
1102 | } |
|
|
1103 | |
|
|
1104 | # sync database to disk |
|
|
1105 | cf::db_sync; |
|
|
1106 | |
|
|
1107 | # get rid of safe::, as good as possible |
|
|
1108 | Symbol::delete_package "safe::$_" |
|
|
1109 | for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region); |
|
|
1110 | |
|
|
1111 | # remove register_script_function callbacks |
|
|
1112 | # TODO |
|
|
1113 | |
|
|
1114 | # unload cf.pm "a bit" |
|
|
1115 | delete $INC{"cf.pm"}; |
|
|
1116 | |
|
|
1117 | # don't, removes xs symbols, too, |
|
|
1118 | # and global variables created in xs |
|
|
1119 | #Symbol::delete_package __PACKAGE__; |
|
|
1120 | |
|
|
1121 | # reload cf.pm |
|
|
1122 | $msg->("reloading cf.pm"); |
|
|
1123 | require cf; |
|
|
1124 | |
|
|
1125 | # load config and database again |
|
|
1126 | cf::cfg_load; |
|
|
1127 | cf::db_load; |
|
|
1128 | |
|
|
1129 | # load extensions |
|
|
1130 | $msg->("load extensions"); |
|
|
1131 | cf::load_extensions; |
|
|
1132 | |
|
|
1133 | # reattach attachments to objects |
|
|
1134 | $msg->("reattach"); |
|
|
1135 | _global_reattach; |
|
|
1136 | }; |
|
|
1137 | $msg->($@) if $@; |
|
|
1138 | |
|
|
1139 | $msg->("reloaded"); |
|
|
1140 | }; |
|
|
1141 | |
|
|
1142 | sub perl_reload() { |
|
|
1143 | _perl_reload { |
|
|
1144 | warn $_[0]; |
|
|
1145 | print "$_[0]\n"; |
|
|
1146 | }; |
|
|
1147 | } |
|
|
1148 | |
|
|
1149 | register_command "perl-reload", 0, sub { |
|
|
1150 | my ($who, $arg) = @_; |
|
|
1151 | |
|
|
1152 | if ($who->flag (FLAG_WIZ)) { |
|
|
1153 | _perl_reload { |
|
|
1154 | warn $_[0]; |
|
|
1155 | $who->message ($_[0]); |
|
|
1156 | }; |
|
|
1157 | } |
|
|
1158 | }; |
|
|
1159 | |
920 | register "<global>", __PACKAGE__; |
1160 | register "<global>", __PACKAGE__; |
921 | |
1161 | |
922 | unshift @INC, $LIBDIR; |
1162 | unshift @INC, $LIBDIR; |
923 | |
1163 | |
924 | load_extensions; |
|
|
925 | |
|
|
926 | $TICK_WATCHER = Event->timer ( |
1164 | $TICK_WATCHER = Event->timer ( |
927 | prio => 1, |
1165 | prio => 1, |
|
|
1166 | async => 1, |
928 | at => $NEXT_TICK || 1, |
1167 | at => $NEXT_TICK || 1, |
929 | cb => sub { |
1168 | cb => sub { |
930 | cf::server_tick; # one server iteration |
1169 | cf::server_tick; # one server iteration |
931 | |
1170 | |
932 | my $NOW = Event::time; |
1171 | my $NOW = Event::time; |
933 | $NEXT_TICK += $TICK; |
1172 | $NEXT_TICK += $TICK; |
934 | |
1173 | |
935 | # if we are delayed by four ticks, skip them all |
1174 | # if we are delayed by four ticks or more, skip them all |
936 | $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; |
1175 | $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; |
937 | |
1176 | |
938 | $TICK_WATCHER->at ($NEXT_TICK); |
1177 | $TICK_WATCHER->at ($NEXT_TICK); |
939 | $TICK_WATCHER->start; |
1178 | $TICK_WATCHER->start; |
940 | }, |
1179 | }, |
941 | ); |
1180 | ); |
942 | |
1181 | |
943 | _reload_2; |
1182 | eval { IO::AIO::max_poll_time $TICK * 0.2 }; #d# remove eval after restart |
|
|
1183 | |
|
|
1184 | Event->io (fd => IO::AIO::poll_fileno, |
|
|
1185 | poll => 'r', |
|
|
1186 | prio => 5, |
|
|
1187 | cb => \&IO::AIO::poll_cb); |
944 | |
1188 | |
945 | 1 |
1189 | 1 |
946 | |
1190 | |