… | |
… | |
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 | |
|
|
21 | our %COMMAND = (); |
|
|
22 | our %COMMAND_TIME = (); |
|
|
23 | our %EXTCMD = (); |
|
|
24 | |
16 | _init_vars; |
25 | _init_vars; |
17 | |
26 | |
18 | our %COMMAND = (); |
|
|
19 | our @EVENT; |
27 | our @EVENT; |
20 | our $LIBDIR = maps_directory "perl"; |
28 | our $LIBDIR = maps_directory "perl"; |
21 | |
29 | |
22 | our $TICK = MAX_TIME * 1e-6; |
30 | our $TICK = MAX_TIME * 1e-6; |
23 | our $TICK_WATCHER; |
31 | our $TICK_WATCHER; |
24 | our $NEXT_TICK; |
32 | our $NEXT_TICK; |
|
|
33 | |
|
|
34 | our %CFG; |
|
|
35 | |
|
|
36 | our $UPTIME; $UPTIME ||= time; |
|
|
37 | |
|
|
38 | ############################################################################# |
|
|
39 | |
|
|
40 | =head2 GLOBAL VARIABLES |
|
|
41 | |
|
|
42 | =over 4 |
|
|
43 | |
|
|
44 | =item $cf::UPTIME |
|
|
45 | |
|
|
46 | The timestamp of the server start (so not actually an uptime). |
|
|
47 | |
|
|
48 | =item $cf::LIBDIR |
|
|
49 | |
|
|
50 | The perl library directory, where extensions and cf-specific modules can |
|
|
51 | be found. It will be added to C<@INC> automatically. |
|
|
52 | |
|
|
53 | =item $cf::TICK |
|
|
54 | |
|
|
55 | The interval between server ticks, in seconds. |
|
|
56 | |
|
|
57 | =item %cf::CFG |
|
|
58 | |
|
|
59 | Configuration for the server, loaded from C</etc/crossfire/config>, or |
|
|
60 | from wherever your confdir points to. |
|
|
61 | |
|
|
62 | =back |
|
|
63 | |
|
|
64 | =cut |
25 | |
65 | |
26 | BEGIN { |
66 | BEGIN { |
27 | *CORE::GLOBAL::warn = sub { |
67 | *CORE::GLOBAL::warn = sub { |
28 | my $msg = join "", @_; |
68 | my $msg = join "", @_; |
29 | $msg .= "\n" |
69 | $msg .= "\n" |
… | |
… | |
48 | }; |
88 | }; |
49 | |
89 | |
50 | my %ext_pkg; |
90 | my %ext_pkg; |
51 | my @exts; |
91 | my @exts; |
52 | my @hook; |
92 | my @hook; |
53 | my %command; |
|
|
54 | my %extcmd; |
|
|
55 | |
93 | |
56 | ############################################################################# |
94 | =head2 UTILITY FUNCTIONS |
57 | # utility functions |
95 | |
|
|
96 | =over 4 |
|
|
97 | |
|
|
98 | =cut |
58 | |
99 | |
59 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
100 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
|
|
101 | |
|
|
102 | =item $ref = cf::from_json $json |
|
|
103 | |
|
|
104 | Converts a JSON string into the corresponding perl data structure. |
|
|
105 | |
|
|
106 | =cut |
60 | |
107 | |
61 | sub from_json($) { |
108 | sub from_json($) { |
62 | $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs |
109 | $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs |
63 | JSON::Syck::Load $_[0] |
110 | JSON::Syck::Load $_[0] |
64 | } |
111 | } |
65 | |
112 | |
|
|
113 | =item $json = cf::to_json $ref |
|
|
114 | |
|
|
115 | Converts a perl data structure into its JSON representation. |
|
|
116 | |
|
|
117 | =cut |
|
|
118 | |
66 | sub to_json($) { |
119 | sub to_json($) { |
67 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
120 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
68 | JSON::Syck::Dump $_[0] |
121 | JSON::Syck::Dump $_[0] |
69 | } |
122 | } |
70 | |
123 | |
|
|
124 | =back |
|
|
125 | |
|
|
126 | =cut |
|
|
127 | |
71 | ############################################################################# |
128 | ############################################################################# |
72 | # "new" plug-in system |
|
|
73 | |
129 | |
74 | =head3 EVENTS AND OBJECT ATTACHMENTS |
130 | =head2 EVENTS AND OBJECT ATTACHMENTS |
75 | |
131 | |
76 | =over 4 |
132 | =over 4 |
77 | |
133 | |
78 | =item $object->attach ($attachment, key => $value...) |
134 | =item $object->attach ($attachment, key => $value...) |
79 | |
135 | |
… | |
… | |
379 | removed in future versions), and there is no public API to access override |
435 | removed in future versions), and there is no public API to access override |
380 | results (if you must, access C<@cf::invoke_results> directly). |
436 | results (if you must, access C<@cf::invoke_results> directly). |
381 | |
437 | |
382 | =back |
438 | =back |
383 | |
439 | |
384 | =head2 methods valid for all pointers |
440 | =cut |
|
|
441 | |
|
|
442 | ############################################################################# |
|
|
443 | |
|
|
444 | =head2 METHODS VALID FOR ALL CORE OBJECTS |
385 | |
445 | |
386 | =over 4 |
446 | =over 4 |
387 | |
447 | |
388 | =item $object->valid |
448 | =item $object->valid, $player->valid, $map->valid |
389 | |
|
|
390 | =item $player->valid |
|
|
391 | |
|
|
392 | =item $map->valid |
|
|
393 | |
449 | |
394 | Just because you have a perl object does not mean that the corresponding |
450 | 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 |
451 | 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 |
452 | 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 |
453 | can be used to test for existence of the C object part without causing an |
… | |
… | |
470 | unlink $filename; |
526 | unlink $filename; |
471 | unlink "$filename.pst"; |
527 | unlink "$filename.pst"; |
472 | } |
528 | } |
473 | } |
529 | } |
474 | |
530 | |
|
|
531 | sub object_freezer_as_string { |
|
|
532 | my ($rdata, $objs) = @_; |
|
|
533 | |
|
|
534 | use Data::Dumper; |
|
|
535 | |
|
|
536 | $$rdata . Dumper $objs |
|
|
537 | } |
|
|
538 | |
475 | sub object_thawer_load { |
539 | sub object_thawer_load { |
476 | my ($filename) = @_; |
540 | my ($filename) = @_; |
477 | |
541 | |
478 | local $/; |
542 | local $/; |
479 | |
543 | |
… | |
… | |
504 | if exists $src->{_attachment}; |
568 | if exists $src->{_attachment}; |
505 | }, |
569 | }, |
506 | ; |
570 | ; |
507 | |
571 | |
508 | ############################################################################# |
572 | ############################################################################# |
509 | # old plug-in events |
573 | # command handling &c |
510 | |
574 | |
511 | sub inject_event { |
575 | =item cf::register_command $name => \&callback($ob,$args); |
512 | my $extension = shift; |
|
|
513 | my $event_code = shift; |
|
|
514 | |
576 | |
515 | my $cb = $hook[$event_code]{$extension} |
577 | Register a callback for execution when the client sends the user command |
516 | or return; |
578 | $name. |
517 | |
579 | |
518 | &$cb |
580 | =cut |
519 | } |
|
|
520 | |
|
|
521 | sub inject_global_event { |
|
|
522 | my $event = shift; |
|
|
523 | |
|
|
524 | my $cb = $hook[$event] |
|
|
525 | or return; |
|
|
526 | |
|
|
527 | List::Util::max map &$_, values %$cb |
|
|
528 | } |
|
|
529 | |
|
|
530 | sub inject_command { |
|
|
531 | my ($name, $obj, $params) = @_; |
|
|
532 | |
|
|
533 | for my $cmd (@{ $command{$name} }) { |
|
|
534 | $cmd->[1]->($obj, $params); |
|
|
535 | } |
|
|
536 | |
|
|
537 | -1 |
|
|
538 | } |
|
|
539 | |
581 | |
540 | sub register_command { |
582 | sub register_command { |
541 | my ($name, $time, $cb) = @_; |
583 | my ($name, $cb) = @_; |
542 | |
584 | |
543 | my $caller = caller; |
585 | my $caller = caller; |
544 | #warn "registering command '$name/$time' to '$caller'"; |
586 | #warn "registering command '$name/$time' to '$caller'"; |
545 | |
587 | |
546 | push @{ $command{$name} }, [$time, $cb, $caller]; |
588 | push @{ $COMMAND{$name} }, [$caller, $cb]; |
547 | $COMMAND{"$name\000"} = List::Util::max map $_->[0], @{ $command{$name} }; |
|
|
548 | } |
589 | } |
|
|
590 | |
|
|
591 | =item cf::register_extcmd $name => \&callback($pl,$packet); |
|
|
592 | |
|
|
593 | Register a callbackf ro execution when the client sends an extcmd packet. |
|
|
594 | |
|
|
595 | If the callback returns something, it is sent back as if reply was being |
|
|
596 | called. |
|
|
597 | |
|
|
598 | =cut |
549 | |
599 | |
550 | sub register_extcmd { |
600 | sub register_extcmd { |
551 | my ($name, $cb) = @_; |
601 | my ($name, $cb) = @_; |
552 | |
602 | |
553 | my $caller = caller; |
603 | my $caller = caller; |
554 | #warn "registering extcmd '$name' to '$caller'"; |
604 | #warn "registering extcmd '$name' to '$caller'"; |
555 | |
605 | |
556 | $extcmd{$name} = [$cb, $caller]; |
606 | $EXTCMD{$name} = [$cb, $caller]; |
557 | } |
607 | } |
|
|
608 | |
|
|
609 | attach_to_players |
|
|
610 | on_command => sub { |
|
|
611 | my ($pl, $name, $params) = @_; |
|
|
612 | |
|
|
613 | my $cb = $COMMAND{$name} |
|
|
614 | or return; |
|
|
615 | |
|
|
616 | for my $cmd (@$cb) { |
|
|
617 | $cmd->[1]->($pl->ob, $params); |
|
|
618 | } |
|
|
619 | |
|
|
620 | cf::override; |
|
|
621 | }, |
|
|
622 | on_extcmd => sub { |
|
|
623 | my ($pl, $buf) = @_; |
|
|
624 | |
|
|
625 | my $msg = eval { from_json $buf }; |
|
|
626 | |
|
|
627 | if (ref $msg) { |
|
|
628 | if (my $cb = $EXTCMD{$msg->{msgtype}}) { |
|
|
629 | if (my %reply = $cb->[0]->($pl, $msg)) { |
|
|
630 | $pl->ext_reply ($msg->{msgid}, %reply); |
|
|
631 | } |
|
|
632 | } |
|
|
633 | } else { |
|
|
634 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
|
|
635 | } |
|
|
636 | |
|
|
637 | cf::override; |
|
|
638 | }, |
|
|
639 | ; |
558 | |
640 | |
559 | sub register { |
641 | sub register { |
560 | my ($base, $pkg) = @_; |
642 | my ($base, $pkg) = @_; |
561 | |
643 | |
562 | #TODO |
644 | #TODO |
… | |
… | |
581 | . "#line 1 \"$path\"\n{\n" |
663 | . "#line 1 \"$path\"\n{\n" |
582 | . (do { local $/; <$fh> }) |
664 | . (do { local $/; <$fh> }) |
583 | . "\n};\n1"; |
665 | . "\n};\n1"; |
584 | |
666 | |
585 | eval $source |
667 | eval $source |
586 | or die "$path: $@"; |
668 | or die $@ ? "$path: $@\n" |
|
|
669 | : "extension disabled.\n"; |
587 | |
670 | |
588 | push @exts, $pkg; |
671 | push @exts, $pkg; |
589 | $ext_pkg{$base} = $pkg; |
672 | $ext_pkg{$base} = $pkg; |
590 | |
673 | |
591 | # no strict 'refs'; |
674 | # no strict 'refs'; |
… | |
… | |
604 | # for my $idx (0 .. $#PLUGIN_EVENT) { |
687 | # for my $idx (0 .. $#PLUGIN_EVENT) { |
605 | # delete $hook[$idx]{$pkg}; |
688 | # delete $hook[$idx]{$pkg}; |
606 | # } |
689 | # } |
607 | |
690 | |
608 | # remove commands |
691 | # remove commands |
609 | for my $name (keys %command) { |
692 | for my $name (keys %COMMAND) { |
610 | my @cb = grep $_->[2] ne $pkg, @{ $command{$name} }; |
693 | my @cb = grep $_->[0] ne $pkg, @{ $COMMAND{$name} }; |
611 | |
694 | |
612 | if (@cb) { |
695 | if (@cb) { |
613 | $command{$name} = \@cb; |
696 | $COMMAND{$name} = \@cb; |
614 | $COMMAND{"$name\000"} = List::Util::max map $_->[0], @cb; |
|
|
615 | } else { |
697 | } else { |
616 | delete $command{$name}; |
|
|
617 | delete $COMMAND{"$name\000"}; |
698 | delete $COMMAND{$name}; |
618 | } |
699 | } |
619 | } |
700 | } |
620 | |
701 | |
621 | # remove extcmds |
702 | # remove extcmds |
622 | for my $name (grep $extcmd{$_}[1] eq $pkg, keys %extcmd) { |
703 | for my $name (grep $EXTCMD{$_}[1] eq $pkg, keys %EXTCMD) { |
623 | delete $extcmd{$name}; |
704 | delete $EXTCMD{$name}; |
624 | } |
705 | } |
625 | |
706 | |
626 | if (my $cb = $pkg->can ("unload")) { |
707 | if (my $cb = $pkg->can ("unload")) { |
627 | eval { |
708 | eval { |
628 | $cb->($pkg); |
709 | $cb->($pkg); |
… | |
… | |
644 | } or warn "$ext not loaded: $@"; |
725 | } or warn "$ext not loaded: $@"; |
645 | } |
726 | } |
646 | } |
727 | } |
647 | |
728 | |
648 | ############################################################################# |
729 | ############################################################################# |
649 | # extcmd framework, basically convert ext <msg> |
|
|
650 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
|
|
651 | |
|
|
652 | attach_to_players |
|
|
653 | on_extcmd => sub { |
|
|
654 | my ($pl, $buf) = @_; |
|
|
655 | |
|
|
656 | my $msg = eval { from_json $buf }; |
|
|
657 | |
|
|
658 | if (ref $msg) { |
|
|
659 | if (my $cb = $extcmd{$msg->{msgtype}}) { |
|
|
660 | if (my %reply = $cb->[0]->($pl, $msg)) { |
|
|
661 | $pl->ext_reply ($msg->{msgid}, %reply); |
|
|
662 | } |
|
|
663 | } |
|
|
664 | } else { |
|
|
665 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
|
|
666 | } |
|
|
667 | |
|
|
668 | cf::override; |
|
|
669 | }, |
|
|
670 | ; |
|
|
671 | |
|
|
672 | ############################################################################# |
|
|
673 | # load/save/clean perl data associated with a map |
730 | # load/save/clean perl data associated with a map |
674 | |
731 | |
675 | *cf::mapsupport::on_clean = sub { |
732 | *cf::mapsupport::on_clean = sub { |
676 | my ($map) = @_; |
733 | my ($map) = @_; |
677 | |
734 | |
… | |
… | |
704 | } |
761 | } |
705 | }, |
762 | }, |
706 | ; |
763 | ; |
707 | |
764 | |
708 | ############################################################################# |
765 | ############################################################################# |
709 | # core extensions - in perl |
766 | |
|
|
767 | =head2 CORE EXTENSIONS |
|
|
768 | |
|
|
769 | Functions and methods that extend core crossfire objects. |
|
|
770 | |
|
|
771 | =over 4 |
710 | |
772 | |
711 | =item cf::player::exists $login |
773 | =item cf::player::exists $login |
712 | |
774 | |
713 | Returns true when the given account exists. |
775 | Returns true when the given account exists. |
714 | |
776 | |
… | |
… | |
717 | sub cf::player::exists($) { |
779 | sub cf::player::exists($) { |
718 | cf::player::find $_[0] |
780 | cf::player::find $_[0] |
719 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
781 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
720 | } |
782 | } |
721 | |
783 | |
722 | =item $player->reply ($npc, $msg[, $flags]) |
784 | =item $player_object->reply ($npc, $msg[, $flags]) |
723 | |
785 | |
724 | Sends a message to the player, as if the npc C<$npc> replied. C<$npc> |
786 | Sends a message to the player, as if the npc C<$npc> replied. C<$npc> |
725 | can be C<undef>. Does the right thing when the player is currently in a |
787 | can be C<undef>. Does the right thing when the player is currently in a |
726 | dialogue with the given NPC character. |
788 | dialogue with the given NPC character. |
727 | |
789 | |
… | |
… | |
754 | $msg{msgid} = $id; |
816 | $msg{msgid} = $id; |
755 | |
817 | |
756 | $self->send ("ext " . to_json \%msg); |
818 | $self->send ("ext " . to_json \%msg); |
757 | } |
819 | } |
758 | |
820 | |
|
|
821 | =item $player_object->may ("access") |
|
|
822 | |
|
|
823 | Returns wether the given player is authorized to access resource "access" |
|
|
824 | (e.g. "command_wizcast"). |
|
|
825 | |
|
|
826 | =cut |
|
|
827 | |
|
|
828 | sub cf::object::player::may { |
|
|
829 | my ($self, $access) = @_; |
|
|
830 | |
|
|
831 | $self->flag (cf::FLAG_WIZ) || |
|
|
832 | (ref $cf::CFG{"may_$access"} |
|
|
833 | ? scalar grep $self->name eq $_, @{$cf::CFG{"may_$access"}} |
|
|
834 | : $cf::CFG{"may_$access"}) |
|
|
835 | } |
|
|
836 | |
|
|
837 | =cut |
|
|
838 | |
759 | ############################################################################# |
839 | ############################################################################# |
760 | # map scripting support |
840 | |
|
|
841 | =head2 SAFE SCRIPTING |
|
|
842 | |
|
|
843 | Functions that provide a safe environment to compile and execute |
|
|
844 | snippets of perl code without them endangering the safety of the server |
|
|
845 | itself. Looping constructs, I/O operators and other built-in functionality |
|
|
846 | is not available in the safe scripting environment, and the number of |
|
|
847 | functions and methods that can be called is greatly reduced. |
|
|
848 | |
|
|
849 | =cut |
761 | |
850 | |
762 | our $safe = new Safe "safe"; |
851 | our $safe = new Safe "safe"; |
763 | our $safe_hole = new Safe::Hole; |
852 | our $safe_hole = new Safe::Hole; |
764 | |
853 | |
765 | $SIG{FPE} = 'IGNORE'; |
854 | $SIG{FPE} = 'IGNORE'; |
766 | |
855 | |
767 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
856 | $safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); |
768 | |
857 | |
769 | # here we export the classes and methods available to script code |
858 | # here we export the classes and methods available to script code |
|
|
859 | |
|
|
860 | =pod |
|
|
861 | |
|
|
862 | The following fucntions and emthods are available within a safe environment: |
|
|
863 | |
|
|
864 | cf::object contr pay_amount pay_player |
|
|
865 | cf::object::player player |
|
|
866 | cf::player peaceful |
|
|
867 | |
|
|
868 | =cut |
770 | |
869 | |
771 | for ( |
870 | for ( |
772 | ["cf::object" => qw(contr pay_amount pay_player)], |
871 | ["cf::object" => qw(contr pay_amount pay_player)], |
773 | ["cf::object::player" => qw(player)], |
872 | ["cf::object::player" => qw(player)], |
774 | ["cf::player" => qw(peaceful)], |
873 | ["cf::player" => qw(peaceful)], |
… | |
… | |
777 | my ($pkg, @funs) = @$_; |
876 | my ($pkg, @funs) = @$_; |
778 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
877 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
779 | for @funs; |
878 | for @funs; |
780 | } |
879 | } |
781 | |
880 | |
|
|
881 | =over 4 |
|
|
882 | |
|
|
883 | =item @retval = safe_eval $code, [var => value, ...] |
|
|
884 | |
|
|
885 | Compiled and executes the given perl code snippet. additional var/value |
|
|
886 | pairs result in temporary local (my) scalar variables of the given name |
|
|
887 | that are available in the code snippet. Example: |
|
|
888 | |
|
|
889 | my $five = safe_eval '$first + $second', first => 1, second => 4; |
|
|
890 | |
|
|
891 | =cut |
|
|
892 | |
782 | sub safe_eval($;@) { |
893 | sub safe_eval($;@) { |
783 | my ($code, %vars) = @_; |
894 | my ($code, %vars) = @_; |
784 | |
895 | |
785 | my $qcode = $code; |
896 | my $qcode = $code; |
786 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
897 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
… | |
… | |
808 | } |
919 | } |
809 | |
920 | |
810 | wantarray ? @res : $res[0] |
921 | wantarray ? @res : $res[0] |
811 | } |
922 | } |
812 | |
923 | |
|
|
924 | =item cf::register_script_function $function => $cb |
|
|
925 | |
|
|
926 | Register a function that can be called from within map/npc scripts. The |
|
|
927 | function should be reasonably secure and should be put into a package name |
|
|
928 | like the extension. |
|
|
929 | |
|
|
930 | Example: register a function that gets called whenever a map script calls |
|
|
931 | C<rent::overview>, as used by the C<rent> extension. |
|
|
932 | |
|
|
933 | cf::register_script_function "rent::overview" => sub { |
|
|
934 | ... |
|
|
935 | }; |
|
|
936 | |
|
|
937 | =cut |
|
|
938 | |
813 | sub register_script_function { |
939 | sub register_script_function { |
814 | my ($fun, $cb) = @_; |
940 | my ($fun, $cb) = @_; |
815 | |
941 | |
816 | no strict 'refs'; |
942 | no strict 'refs'; |
817 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
943 | *{"safe::$fun"} = $safe_hole->wrap ($cb); |
818 | } |
944 | } |
|
|
945 | |
|
|
946 | =back |
|
|
947 | |
|
|
948 | =cut |
819 | |
949 | |
820 | ############################################################################# |
950 | ############################################################################# |
821 | |
951 | |
822 | =head2 EXTENSION DATABASE SUPPORT |
952 | =head2 EXTENSION DATABASE SUPPORT |
823 | |
953 | |
… | |
… | |
858 | |
988 | |
859 | Immediately write the database to disk I<if it is dirty>. |
989 | Immediately write the database to disk I<if it is dirty>. |
860 | |
990 | |
861 | =cut |
991 | =cut |
862 | |
992 | |
|
|
993 | our $DB; |
|
|
994 | |
863 | { |
995 | { |
864 | my $db; |
|
|
865 | my $path = cf::localdir . "/database.pst"; |
996 | my $path = cf::localdir . "/database.pst"; |
866 | |
997 | |
867 | sub db_load() { |
998 | sub db_load() { |
868 | warn "loading database $path\n";#d# remove later |
999 | warn "loading database $path\n";#d# remove later |
869 | $db = stat $path ? Storable::retrieve $path : { }; |
1000 | $DB = stat $path ? Storable::retrieve $path : { }; |
870 | } |
1001 | } |
871 | |
1002 | |
872 | my $pid; |
1003 | my $pid; |
873 | |
1004 | |
874 | sub db_save() { |
1005 | sub db_save() { |
875 | warn "saving database $path\n";#d# remove later |
1006 | warn "saving database $path\n";#d# remove later |
876 | waitpid $pid, 0 if $pid; |
1007 | waitpid $pid, 0 if $pid; |
877 | unless ($pid = fork) { |
1008 | if (0 == ($pid = fork)) { |
878 | $db->{_meta}{version} = 1; |
1009 | $DB->{_meta}{version} = 1; |
879 | Storable::nstore $db, "$path~"; |
1010 | Storable::nstore $DB, "$path~"; |
880 | rename "$path~", $path; |
1011 | rename "$path~", $path; |
881 | cf::_exit 0 if defined $pid; |
1012 | cf::_exit 0 if defined $pid; |
882 | } |
1013 | } |
883 | } |
1014 | } |
884 | |
1015 | |
… | |
… | |
898 | $idle->start; |
1029 | $idle->start; |
899 | } |
1030 | } |
900 | |
1031 | |
901 | sub db_get($;$) { |
1032 | sub db_get($;$) { |
902 | @_ >= 2 |
1033 | @_ >= 2 |
903 | ? $db->{$_[0]}{$_[1]} |
1034 | ? $DB->{$_[0]}{$_[1]} |
904 | : ($db->{$_[0]} ||= { }) |
1035 | : ($DB->{$_[0]} ||= { }) |
905 | } |
1036 | } |
906 | |
1037 | |
907 | sub db_put($$;$) { |
1038 | sub db_put($$;$) { |
908 | if (@_ >= 3) { |
1039 | if (@_ >= 3) { |
909 | $db->{$_[0]}{$_[1]} = $_[2]; |
1040 | $DB->{$_[0]}{$_[1]} = $_[2]; |
910 | } else { |
1041 | } else { |
911 | $db->{$_[0]} = $_[1]; |
1042 | $DB->{$_[0]} = $_[1]; |
912 | } |
1043 | } |
913 | db_dirty; |
1044 | db_dirty; |
914 | } |
1045 | } |
|
|
1046 | |
|
|
1047 | attach_global |
|
|
1048 | prio => 10000, |
|
|
1049 | on_cleanup => sub { |
|
|
1050 | db_sync; |
|
|
1051 | }, |
|
|
1052 | ; |
915 | } |
1053 | } |
916 | |
1054 | |
917 | ############################################################################# |
1055 | ############################################################################# |
918 | # the server's main() |
1056 | # the server's main() |
919 | |
1057 | |
|
|
1058 | sub cfg_load { |
|
|
1059 | open my $fh, "<:utf8", cf::confdir . "/config" |
|
|
1060 | or return; |
|
|
1061 | |
|
|
1062 | local $/; |
|
|
1063 | *CFG = YAML::Syck::Load <$fh>; |
|
|
1064 | } |
|
|
1065 | |
920 | sub main { |
1066 | sub main { |
|
|
1067 | cfg_load; |
921 | db_load; |
1068 | db_load; |
922 | load_extensions; |
1069 | load_extensions; |
923 | Event::loop; |
1070 | Event::loop; |
924 | } |
1071 | } |
925 | |
1072 | |
… | |
… | |
977 | |
1124 | |
978 | # reload cf.pm |
1125 | # reload cf.pm |
979 | $msg->("reloading cf.pm"); |
1126 | $msg->("reloading cf.pm"); |
980 | require cf; |
1127 | require cf; |
981 | |
1128 | |
982 | # load database again |
1129 | # load config and database again |
|
|
1130 | cf::cfg_load; |
983 | cf::db_load; |
1131 | cf::db_load; |
984 | |
1132 | |
985 | # load extensions |
1133 | # load extensions |
986 | $msg->("load extensions"); |
1134 | $msg->("load extensions"); |
987 | cf::load_extensions; |
1135 | cf::load_extensions; |
… | |
… | |
1000 | warn $_[0]; |
1148 | warn $_[0]; |
1001 | print "$_[0]\n"; |
1149 | print "$_[0]\n"; |
1002 | }; |
1150 | }; |
1003 | } |
1151 | } |
1004 | |
1152 | |
|
|
1153 | register "<global>", __PACKAGE__; |
|
|
1154 | |
1005 | register_command "perl-reload", 0, sub { |
1155 | register_command "perl-reload" => sub { |
1006 | my ($who, $arg) = @_; |
1156 | my ($who, $arg) = @_; |
1007 | |
1157 | |
1008 | if ($who->flag (FLAG_WIZ)) { |
1158 | if ($who->flag (FLAG_WIZ)) { |
1009 | _perl_reload { |
1159 | _perl_reload { |
1010 | warn $_[0]; |
1160 | warn $_[0]; |
1011 | $who->message ($_[0]); |
1161 | $who->message ($_[0]); |
1012 | }; |
1162 | }; |
1013 | } |
1163 | } |
1014 | }; |
1164 | }; |
1015 | |
1165 | |
1016 | register "<global>", __PACKAGE__; |
|
|
1017 | |
|
|
1018 | unshift @INC, $LIBDIR; |
1166 | unshift @INC, $LIBDIR; |
1019 | |
1167 | |
1020 | $TICK_WATCHER = Event->timer ( |
1168 | $TICK_WATCHER = Event->timer ( |
1021 | prio => 1, |
1169 | prio => 1, |
|
|
1170 | async => 1, |
1022 | at => $NEXT_TICK || 1, |
1171 | at => $NEXT_TICK || 1, |
1023 | cb => sub { |
1172 | cb => sub { |
1024 | cf::server_tick; # one server iteration |
1173 | cf::server_tick; # one server iteration |
1025 | |
1174 | |
1026 | my $NOW = Event::time; |
1175 | my $NOW = Event::time; |
1027 | $NEXT_TICK += $TICK; |
1176 | $NEXT_TICK += $TICK; |
1028 | |
1177 | |
1029 | # if we are delayed by four ticks, skip them all |
1178 | # if we are delayed by four ticks or more, skip them all |
1030 | $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; |
1179 | $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; |
1031 | |
1180 | |
1032 | $TICK_WATCHER->at ($NEXT_TICK); |
1181 | $TICK_WATCHER->at ($NEXT_TICK); |
1033 | $TICK_WATCHER->start; |
1182 | $TICK_WATCHER->start; |
1034 | }, |
1183 | }, |
1035 | ); |
1184 | ); |
1036 | |
1185 | |
|
|
1186 | IO::AIO::max_poll_time $TICK * 0.2; |
|
|
1187 | |
|
|
1188 | Event->io (fd => IO::AIO::poll_fileno, |
|
|
1189 | poll => 'r', |
|
|
1190 | prio => 5, |
|
|
1191 | cb => \&IO::AIO::poll_cb); |
|
|
1192 | |
1037 | 1 |
1193 | 1 |
1038 | |
1194 | |