… | |
… | |
4 | use strict; |
4 | use strict; |
5 | |
5 | |
6 | use Symbol; |
6 | use Symbol; |
7 | use List::Util; |
7 | use List::Util; |
8 | use Socket; |
8 | use Socket; |
9 | use Storable; |
|
|
10 | use Event; |
9 | use Event; |
11 | use Opcode; |
10 | use Opcode; |
12 | use Safe; |
11 | use Safe; |
13 | use Safe::Hole; |
12 | use Safe::Hole; |
|
|
13 | use Storable (); |
14 | |
14 | |
15 | use Coro 4.1 (); |
15 | use Coro 4.1 (); |
16 | use Coro::State; |
16 | use Coro::State; |
17 | use Coro::Handle; |
17 | use Coro::Handle; |
18 | use Coro::Event; |
18 | use Coro::Event; |
… | |
… | |
427 | my $time = Event::time; |
427 | my $time = Event::time; |
428 | |
428 | |
429 | # this is the main coro, too bad, we have to block |
429 | # this is the main coro, too bad, we have to block |
430 | # till the operation succeeds, freezing the server :/ |
430 | # till the operation succeeds, freezing the server :/ |
431 | |
431 | |
432 | LOG llevError | logBacktrace, Carp::longmess "sync job";#d# |
432 | LOG llevError, Carp::longmess "sync job";#d# |
433 | |
433 | |
434 | # TODO: use suspend/resume instead |
434 | # TODO: use suspend/resume instead |
435 | # (but this is cancel-safe) |
435 | # (but this is cancel-safe) |
436 | my $freeze_guard = freeze_mainloop; |
436 | my $freeze_guard = freeze_mainloop; |
437 | |
437 | |
… | |
… | |
444 | warn $@ if $@; |
444 | warn $@ if $@; |
445 | undef $busy; |
445 | undef $busy; |
446 | })->prio (Coro::PRIO_MAX); |
446 | })->prio (Coro::PRIO_MAX); |
447 | |
447 | |
448 | while ($busy) { |
448 | while ($busy) { |
|
|
449 | if (Coro::nready) { |
|
|
450 | Coro::cede_notself; |
|
|
451 | } else { |
449 | Coro::cede or Event::one_event; |
452 | Event::one_event; |
|
|
453 | } |
450 | } |
454 | } |
451 | |
455 | |
452 | $time = Event::time - $time; |
456 | $time = Event::time - $time; |
453 | |
457 | |
454 | LOG llevError | logBacktrace, Carp::longmess "long sync job" |
458 | LOG llevError | logBacktrace, Carp::longmess "long sync job" |
… | |
… | |
966 | |
970 | |
967 | =cut |
971 | =cut |
968 | |
972 | |
969 | ############################################################################# |
973 | ############################################################################# |
970 | # object support |
974 | # object support |
971 | # |
|
|
972 | |
975 | |
|
|
976 | sub _object_equal($$); |
|
|
977 | sub _object_equal($$) { |
|
|
978 | my ($a, $b) = @_; |
|
|
979 | |
|
|
980 | return 0 unless (ref $a) eq (ref $b); |
|
|
981 | |
|
|
982 | if ("HASH" eq ref $a) { |
|
|
983 | my @ka = keys %$a; |
|
|
984 | my @kb = keys %$b; |
|
|
985 | |
|
|
986 | return 0 if @ka != @kb; |
|
|
987 | |
|
|
988 | for (0 .. $#ka) { |
|
|
989 | return 0 unless $ka[$_] eq $kb[$_]; |
|
|
990 | return 0 unless _object_equal $a->{$ka[$_]}, $b->{$kb[$_]}; |
|
|
991 | } |
|
|
992 | |
|
|
993 | } elsif ("ARRAY" eq ref $a) { |
|
|
994 | |
|
|
995 | return 0 if @$a != @$b; |
|
|
996 | |
|
|
997 | for (0 .. $#$a) { |
|
|
998 | return 0 unless _object_equal $a->[$_], $b->[$_]; |
|
|
999 | } |
|
|
1000 | |
|
|
1001 | } elsif ($a ne $b) { |
|
|
1002 | return 0; |
|
|
1003 | } |
|
|
1004 | |
|
|
1005 | 1 |
|
|
1006 | } |
|
|
1007 | |
|
|
1008 | our $SLOW_MERGES;#d# |
973 | sub _can_merge { |
1009 | sub _can_merge { |
974 | my ($ob1, $ob2) = @_; |
1010 | my ($ob1, $ob2) = @_; |
975 | |
1011 | |
976 | local $Storable::canonical = 1; |
1012 | ++$SLOW_MERGES;#d# |
977 | my $fob1 = Storable::freeze $ob1; |
|
|
978 | my $fob2 = Storable::freeze $ob2; |
|
|
979 | |
1013 | |
980 | $fob1 eq $fob2 |
1014 | # we do the slow way here |
|
|
1015 | return _object_equal $ob1, $ob2 |
981 | } |
1016 | } |
982 | |
1017 | |
983 | sub reattach { |
1018 | sub reattach { |
984 | # basically do the same as instantiate, without calling instantiate |
1019 | # basically do the same as instantiate, without calling instantiate |
985 | my ($obj) = @_; |
1020 | my ($obj) = @_; |
… | |
… | |
1087 | |
1122 | |
1088 | unless (aio_stat "$filename.pst") { |
1123 | unless (aio_stat "$filename.pst") { |
1089 | (aio_load "$filename.pst", $av) >= 0 |
1124 | (aio_load "$filename.pst", $av) >= 0 |
1090 | or return; |
1125 | or return; |
1091 | |
1126 | |
1092 | my $st = eval { Coro::Storable::thaw $av } |
1127 | my $st = eval { Coro::Storable::thaw $av }; |
1093 | || eval { my $guard = Coro::Storable::guard; Storable::thaw $av }; #d# compatibility, remove |
|
|
1094 | $av = $st->{objs}; |
1128 | $av = $st->{objs}; |
1095 | } |
1129 | } |
1096 | |
1130 | |
1097 | utf8::decode (my $decname = $filename); |
1131 | utf8::decode (my $decname = $filename); |
1098 | warn sprintf "loading %s (%d,%d)\n", |
1132 | warn sprintf "loading %s (%d,%d)\n", |
… | |
… | |
2645 | id => "infobox", |
2679 | id => "infobox", |
2646 | title => "Examine", |
2680 | title => "Examine", |
2647 | reply => undef, |
2681 | reply => undef, |
2648 | tooltip => "Signs and other items you examined", |
2682 | tooltip => "Signs and other items you examined", |
2649 | }, |
2683 | }, |
|
|
2684 | "c/book" => { |
|
|
2685 | id => "infobox", |
|
|
2686 | title => "Book", |
|
|
2687 | reply => undef, |
|
|
2688 | tooltip => "The contents of a note or book", |
|
|
2689 | }, |
2650 | "c/lookat" => { |
2690 | "c/lookat" => { |
2651 | id => "infobox", |
2691 | id => "infobox", |
2652 | title => "Look", |
2692 | title => "Look", |
2653 | reply => undef, |
2693 | reply => undef, |
2654 | tooltip => "What you saw there", |
2694 | tooltip => "What you saw there", |
|
|
2695 | }, |
|
|
2696 | "c/who" => { |
|
|
2697 | id => "infobox", |
|
|
2698 | title => "Players", |
|
|
2699 | reply => undef, |
|
|
2700 | tooltip => "Shows players who are currently online", |
|
|
2701 | }, |
|
|
2702 | "c/body" => { |
|
|
2703 | id => "infobox", |
|
|
2704 | title => "Body Parts", |
|
|
2705 | reply => undef, |
|
|
2706 | tooltip => "Shows which body parts you posess and are available", |
|
|
2707 | }, |
|
|
2708 | "c/uptime" => { |
|
|
2709 | id => "infobox", |
|
|
2710 | title => "Uptime", |
|
|
2711 | reply => undef, |
|
|
2712 | tooltip => "How long the server has been running sicne last restart", |
|
|
2713 | }, |
|
|
2714 | "c/mapinfo" => { |
|
|
2715 | id => "infobox", |
|
|
2716 | title => "Map Info", |
|
|
2717 | reply => undef, |
|
|
2718 | tooltip => "Information related to the maps", |
2655 | }, |
2719 | }, |
2656 | ); |
2720 | ); |
2657 | |
2721 | |
2658 | sub cf::client::send_msg { |
2722 | sub cf::client::send_msg { |
2659 | my ($self, $channel, $msg, $color, @extra) = @_; |
2723 | my ($self, $channel, $msg, $color, @extra) = @_; |
… | |
… | |
2896 | =pod |
2960 | =pod |
2897 | |
2961 | |
2898 | The following functions and methods are available within a safe environment: |
2962 | The following functions and methods are available within a safe environment: |
2899 | |
2963 | |
2900 | cf::object |
2964 | cf::object |
2901 | contr pay_amount pay_player map x y force_find force_add |
2965 | contr pay_amount pay_player map x y force_find force_add destroy |
2902 | insert remove name archname title slaying race decrease_ob_nr |
2966 | insert remove name archname title slaying race decrease_ob_nr |
2903 | |
2967 | |
2904 | cf::object::player |
2968 | cf::object::player |
2905 | player |
2969 | player |
2906 | |
2970 | |
… | |
… | |
2913 | =cut |
2977 | =cut |
2914 | |
2978 | |
2915 | for ( |
2979 | for ( |
2916 | ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y |
2980 | ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y |
2917 | insert remove inv name archname title slaying race |
2981 | insert remove inv name archname title slaying race |
2918 | decrease_ob_nr)], |
2982 | decrease_ob_nr destroy)], |
2919 | ["cf::object::player" => qw(player)], |
2983 | ["cf::object::player" => qw(player)], |
2920 | ["cf::player" => qw(peaceful)], |
2984 | ["cf::player" => qw(peaceful)], |
2921 | ["cf::map" => qw(trigger)], |
2985 | ["cf::map" => qw(trigger)], |
2922 | ) { |
2986 | ) { |
2923 | no strict 'refs'; |
2987 | no strict 'refs'; |
… | |
… | |
3280 | # and maps saved/destroyed asynchronously. |
3344 | # and maps saved/destroyed asynchronously. |
3281 | warn "begin emergency player save\n"; |
3345 | warn "begin emergency player save\n"; |
3282 | for my $login (keys %cf::PLAYER) { |
3346 | for my $login (keys %cf::PLAYER) { |
3283 | my $pl = $cf::PLAYER{$login} or next; |
3347 | my $pl = $cf::PLAYER{$login} or next; |
3284 | $pl->valid or next; |
3348 | $pl->valid or next; |
3285 | delete $pl->{unclean_save}; |
3349 | delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt |
3286 | $pl->save; |
3350 | $pl->save; |
3287 | } |
3351 | } |
3288 | warn "end emergency player save\n"; |
3352 | warn "end emergency player save\n"; |
3289 | |
3353 | |
3290 | warn "begin emergency map save\n"; |
3354 | warn "begin emergency map save\n"; |
… | |
… | |
3338 | |
3402 | |
3339 | warn "flushing outstanding aio requests"; |
3403 | warn "flushing outstanding aio requests"; |
3340 | for (;;) { |
3404 | for (;;) { |
3341 | BDB::flush; |
3405 | BDB::flush; |
3342 | IO::AIO::flush; |
3406 | IO::AIO::flush; |
3343 | Coro::cede; |
3407 | Coro::cede_notself; |
3344 | last unless IO::AIO::nreqs || BDB::nreqs; |
3408 | last unless IO::AIO::nreqs || BDB::nreqs; |
3345 | warn "iterate..."; |
3409 | warn "iterate..."; |
3346 | } |
3410 | } |
3347 | |
3411 | |
3348 | ++$RELOAD; |
3412 | ++$RELOAD; |