… | |
… | |
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 3.64 (); |
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; |
19 | use Coro::Timer; |
19 | use Coro::Timer; |
20 | use Coro::Signal; |
20 | use Coro::Signal; |
… | |
… | |
27 | use BDB (); |
27 | use BDB (); |
28 | use Data::Dumper; |
28 | use Data::Dumper; |
29 | use Digest::MD5; |
29 | use Digest::MD5; |
30 | use Fcntl; |
30 | use Fcntl; |
31 | use YAML::Syck (); |
31 | use YAML::Syck (); |
32 | use IO::AIO 2.32 (); |
32 | use IO::AIO 2.51 (); |
33 | use Time::HiRes; |
33 | use Time::HiRes; |
34 | use Compress::LZF; |
34 | use Compress::LZF; |
35 | use Digest::MD5 (); |
35 | use Digest::MD5 (); |
36 | |
36 | |
37 | # configure various modules to our taste |
37 | # configure various modules to our taste |
… | |
… | |
373 | } |
373 | } |
374 | } |
374 | } |
375 | } |
375 | } |
376 | |
376 | |
377 | if (@SLOT_QUEUE) { |
377 | if (@SLOT_QUEUE) { |
378 | # we do not use wait_For_tick() as it returns immediately when tick is inactive |
378 | # we do not use wait_for_tick() as it returns immediately when tick is inactive |
379 | push @cf::WAIT_FOR_TICK, $signal; |
379 | push @cf::WAIT_FOR_TICK, $signal; |
380 | $signal->wait; |
380 | $signal->wait; |
381 | } else { |
381 | } else { |
382 | Coro::schedule; |
382 | Coro::schedule; |
383 | } |
383 | } |
… | |
… | |
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, Carp::longmess "sync job";#d# |
|
|
433 | |
432 | # TODO: use suspend/resume instead |
434 | # TODO: use suspend/resume instead |
433 | # (but this is cancel-safe) |
435 | # (but this is cancel-safe) |
434 | my $freeze_guard = freeze_mainloop; |
436 | my $freeze_guard = freeze_mainloop; |
435 | |
437 | |
436 | my $busy = 1; |
438 | my $busy = 1; |
… | |
… | |
964 | |
966 | |
965 | =cut |
967 | =cut |
966 | |
968 | |
967 | ############################################################################# |
969 | ############################################################################# |
968 | # object support |
970 | # object support |
969 | # |
|
|
970 | |
971 | |
|
|
972 | sub _object_equal($$); |
|
|
973 | sub _object_equal($$) { |
|
|
974 | my ($a, $b) = @_; |
|
|
975 | |
|
|
976 | return 0 unless (ref $a) eq (ref $b); |
|
|
977 | |
|
|
978 | if ("HASH" eq ref $a) { |
|
|
979 | my @ka = keys %$a; |
|
|
980 | my @kb = keys %$b; |
|
|
981 | |
|
|
982 | return 0 if @ka != @kb; |
|
|
983 | |
|
|
984 | for (0 .. $#ka) { |
|
|
985 | return 0 unless $ka[$_] eq $kb[$_]; |
|
|
986 | return 0 unless _object_equal $a->{$ka[$_]}, $b->{$kb[$_]}; |
|
|
987 | } |
|
|
988 | |
|
|
989 | } elsif ("ARRAY" eq ref $a) { |
|
|
990 | |
|
|
991 | return 0 if @$a != @$b; |
|
|
992 | |
|
|
993 | for (0 .. $#$a) { |
|
|
994 | return 0 unless _object_equal $a->[$_], $b->[$_]; |
|
|
995 | } |
|
|
996 | |
|
|
997 | } elsif ($a ne $b) { |
|
|
998 | return 0; |
|
|
999 | } |
|
|
1000 | |
|
|
1001 | 1 |
|
|
1002 | } |
|
|
1003 | |
|
|
1004 | our $SLOW_MERGES;#d# |
971 | sub _can_merge { |
1005 | sub _can_merge { |
972 | my ($ob1, $ob2) = @_; |
1006 | my ($ob1, $ob2) = @_; |
973 | |
1007 | |
974 | local $Storable::canonical = 1; |
1008 | ++$SLOW_MERGES;#d# |
975 | my $fob1 = Storable::freeze $ob1; |
|
|
976 | my $fob2 = Storable::freeze $ob2; |
|
|
977 | |
1009 | |
978 | $fob1 eq $fob2 |
1010 | # we do the slow way here |
|
|
1011 | return _object_equal $ob1, $ob2 |
979 | } |
1012 | } |
980 | |
1013 | |
981 | sub reattach { |
1014 | sub reattach { |
982 | # basically do the same as instantiate, without calling instantiate |
1015 | # basically do the same as instantiate, without calling instantiate |
983 | my ($obj) = @_; |
1016 | my ($obj) = @_; |
… | |
… | |
1044 | close $fh; |
1077 | close $fh; |
1045 | |
1078 | |
1046 | if (@$objs) { |
1079 | if (@$objs) { |
1047 | if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { |
1080 | if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { |
1048 | chmod SAVE_MODE, $fh; |
1081 | chmod SAVE_MODE, $fh; |
1049 | my $data = Storable::nfreeze { version => 1, objs => $objs }; |
1082 | my $data = Coro::Storable::blocking_nfreeze { version => 1, objs => $objs }; |
1050 | aio_write $fh, 0, (length $data), $data, 0; |
1083 | aio_write $fh, 0, (length $data), $data, 0; |
1051 | aio_fsync $fh if $cf::USE_FSYNC; |
1084 | aio_fsync $fh if $cf::USE_FSYNC; |
1052 | close $fh; |
1085 | close $fh; |
1053 | aio_rename "$filename.pst~", "$filename.pst"; |
1086 | aio_rename "$filename.pst~", "$filename.pst"; |
1054 | } |
1087 | } |
… | |
… | |
1085 | |
1118 | |
1086 | unless (aio_stat "$filename.pst") { |
1119 | unless (aio_stat "$filename.pst") { |
1087 | (aio_load "$filename.pst", $av) >= 0 |
1120 | (aio_load "$filename.pst", $av) >= 0 |
1088 | or return; |
1121 | or return; |
1089 | |
1122 | |
1090 | $av = eval { (Storable::thaw $av)->{objs} }; |
1123 | my $st = eval { Coro::Storable::thaw $av } |
|
|
1124 | || eval { my $guard = Coro::Storable::guard; Storable::thaw $av }; #d# compatibility, remove |
|
|
1125 | $av = $st->{objs}; |
1091 | } |
1126 | } |
1092 | |
1127 | |
1093 | utf8::decode (my $decname = $filename); |
1128 | utf8::decode (my $decname = $filename); |
1094 | warn sprintf "loading %s (%d,%d)\n", |
1129 | warn sprintf "loading %s (%d,%d)\n", |
1095 | $decname, length $data, scalar @{$av || []}; |
1130 | $decname, length $data, scalar @{$av || []}; |
… | |
… | |
2273 | =cut |
2308 | =cut |
2274 | |
2309 | |
2275 | sub deref { |
2310 | sub deref { |
2276 | my ($ref) = @_; |
2311 | my ($ref) = @_; |
2277 | |
2312 | |
2278 | # temporary compatibility#TODO#remove |
|
|
2279 | $ref =~ s{^<}{player/<}; |
|
|
2280 | |
|
|
2281 | if ($ref =~ m{^player\/(<1\.\d+>)/(.*)$}) { |
2313 | if ($ref =~ m{^player\/(<1\.[0-9a-f]+>)/(.*)$}) { |
2282 | my ($uuid, $name) = ($1, $2); |
2314 | my ($uuid, $name) = ($1, $2); |
2283 | my $pl = $cf::PLAYER_LOADING{$name} || cf::player::find $name |
2315 | my $pl = $cf::PLAYER_LOADING{$name} || cf::player::find $name |
2284 | or return; |
2316 | or return; |
2285 | $pl->ob->uuid eq $uuid |
2317 | $pl->ob->uuid eq $uuid |
2286 | or return; |
2318 | or return; |
… | |
… | |
2895 | =pod |
2927 | =pod |
2896 | |
2928 | |
2897 | The following functions and methods are available within a safe environment: |
2929 | The following functions and methods are available within a safe environment: |
2898 | |
2930 | |
2899 | cf::object |
2931 | cf::object |
2900 | contr pay_amount pay_player map x y force_find force_add |
2932 | contr pay_amount pay_player map x y force_find force_add destroy |
2901 | insert remove name archname title slaying race decrease_ob_nr |
2933 | insert remove name archname title slaying race decrease_ob_nr |
2902 | |
2934 | |
2903 | cf::object::player |
2935 | cf::object::player |
2904 | player |
2936 | player |
2905 | |
2937 | |
… | |
… | |
2912 | =cut |
2944 | =cut |
2913 | |
2945 | |
2914 | for ( |
2946 | for ( |
2915 | ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y |
2947 | ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y |
2916 | insert remove inv name archname title slaying race |
2948 | insert remove inv name archname title slaying race |
2917 | decrease_ob_nr)], |
2949 | decrease_ob_nr destroy)], |
2918 | ["cf::object::player" => qw(player)], |
2950 | ["cf::object::player" => qw(player)], |
2919 | ["cf::player" => qw(peaceful)], |
2951 | ["cf::player" => qw(peaceful)], |
2920 | ["cf::map" => qw(trigger)], |
2952 | ["cf::map" => qw(trigger)], |
2921 | ) { |
2953 | ) { |
2922 | no strict 'refs'; |
2954 | no strict 'refs'; |
… | |
… | |
3279 | # and maps saved/destroyed asynchronously. |
3311 | # and maps saved/destroyed asynchronously. |
3280 | warn "begin emergency player save\n"; |
3312 | warn "begin emergency player save\n"; |
3281 | for my $login (keys %cf::PLAYER) { |
3313 | for my $login (keys %cf::PLAYER) { |
3282 | my $pl = $cf::PLAYER{$login} or next; |
3314 | my $pl = $cf::PLAYER{$login} or next; |
3283 | $pl->valid or next; |
3315 | $pl->valid or next; |
|
|
3316 | delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt |
3284 | $pl->save; |
3317 | $pl->save; |
3285 | } |
3318 | } |
3286 | warn "end emergency player save\n"; |
3319 | warn "end emergency player save\n"; |
3287 | |
3320 | |
3288 | warn "begin emergency map save\n"; |
3321 | warn "begin emergency map save\n"; |