ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf.pm
(Generate patch)

Comparing deliantra/server/lib/cf.pm (file contents):
Revision 1.377 by root, Thu Oct 4 11:36:38 2007 UTC vs.
Revision 1.391 by root, Fri Oct 26 04:22:05 2007 UTC

4use strict; 4use strict;
5 5
6use Symbol; 6use Symbol;
7use List::Util; 7use List::Util;
8use Socket; 8use Socket;
9use Storable;
10use Event; 9use Event;
11use Opcode; 10use Opcode;
12use Safe; 11use Safe;
13use Safe::Hole; 12use Safe::Hole;
13use Storable ();
14 14
15use Coro 3.64 (); 15use Coro 4.1 ();
16use Coro::State; 16use Coro::State;
17use Coro::Handle; 17use Coro::Handle;
18use Coro::Event; 18use Coro::Event;
19use Coro::Timer; 19use Coro::Timer;
20use Coro::Signal; 20use Coro::Signal;
27use BDB (); 27use BDB ();
28use Data::Dumper; 28use Data::Dumper;
29use Digest::MD5; 29use Digest::MD5;
30use Fcntl; 30use Fcntl;
31use YAML::Syck (); 31use YAML::Syck ();
32use IO::AIO 2.32 (); 32use IO::AIO 2.51 ();
33use Time::HiRes; 33use Time::HiRes;
34use Compress::LZF; 34use Compress::LZF;
35use Digest::MD5 (); 35use 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;
442 warn $@ if $@; 444 warn $@ if $@;
443 undef $busy; 445 undef $busy;
444 })->prio (Coro::PRIO_MAX); 446 })->prio (Coro::PRIO_MAX);
445 447
446 while ($busy) { 448 while ($busy) {
449 if (Coro::nready) {
450 Coro::cede_notself;
451 } else {
447 Coro::cede or Event::one_event; 452 Event::one_event;
453 }
448 } 454 }
449 455
450 $time = Event::time - $time; 456 $time = Event::time - $time;
451 457
452 LOG llevError | logBacktrace, Carp::longmess "long sync job" 458 LOG llevError | logBacktrace, Carp::longmess "long sync job"
964 970
965=cut 971=cut
966 972
967############################################################################# 973#############################################################################
968# object support 974# object support
969#
970 975
976sub _object_equal($$);
977sub _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
1008our $SLOW_MERGES;#d#
971sub _can_merge { 1009sub _can_merge {
972 my ($ob1, $ob2) = @_; 1010 my ($ob1, $ob2) = @_;
973 1011
974 local $Storable::canonical = 1; 1012 ++$SLOW_MERGES;#d#
975 my $fob1 = Storable::freeze $ob1;
976 my $fob2 = Storable::freeze $ob2;
977 1013
978 $fob1 eq $fob2 1014 # we do the slow way here
1015 return _object_equal $ob1, $ob2
979} 1016}
980 1017
981sub reattach { 1018sub reattach {
982 # basically do the same as instantiate, without calling instantiate 1019 # basically do the same as instantiate, without calling instantiate
983 my ($obj) = @_; 1020 my ($obj) = @_;
1044 close $fh; 1081 close $fh;
1045 1082
1046 if (@$objs) { 1083 if (@$objs) {
1047 if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { 1084 if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) {
1048 chmod SAVE_MODE, $fh; 1085 chmod SAVE_MODE, $fh;
1049 my $data = Storable::nfreeze { version => 1, objs => $objs }; 1086 my $data = Coro::Storable::nfreeze { version => 1, objs => $objs };
1050 aio_write $fh, 0, (length $data), $data, 0; 1087 aio_write $fh, 0, (length $data), $data, 0;
1051 aio_fsync $fh if $cf::USE_FSYNC; 1088 aio_fsync $fh if $cf::USE_FSYNC;
1052 close $fh; 1089 close $fh;
1053 aio_rename "$filename.pst~", "$filename.pst"; 1090 aio_rename "$filename.pst~", "$filename.pst";
1054 } 1091 }
1085 1122
1086 unless (aio_stat "$filename.pst") { 1123 unless (aio_stat "$filename.pst") {
1087 (aio_load "$filename.pst", $av) >= 0 1124 (aio_load "$filename.pst", $av) >= 0
1088 or return; 1125 or return;
1089 1126
1090 $av = eval { (Storable::thaw $av)->{objs} }; 1127 my $st = eval { Coro::Storable::thaw $av };
1128 $av = $st->{objs};
1091 } 1129 }
1092 1130
1093 utf8::decode (my $decname = $filename); 1131 utf8::decode (my $decname = $filename);
1094 warn sprintf "loading %s (%d,%d)\n", 1132 warn sprintf "loading %s (%d,%d)\n",
1095 $decname, length $data, scalar @{$av || []}; 1133 $decname, length $data, scalar @{$av || []};
1510 1548
1511Expand crossfire pod fragments into protocol xml. 1549Expand crossfire pod fragments into protocol xml.
1512 1550
1513=cut 1551=cut
1514 1552
1553 use re 'eval';
1554
1555my $group;
1556my $interior; $interior = qr{
1557 (?:
1558 \ (.*?)\ (?{ $group = $^N })
1559 | < (??{$interior}) >
1560 )
1561}x;
1562
1515sub expand_cfpod { 1563sub expand_cfpod {
1516 ((my $self), (local $_)) = @_; 1564 my ($self, $pod) = @_;
1517 1565
1518 # escape & and < 1566 my $xml;
1519 s/&/&amp;/g;
1520 s/(?<![BIUGHT])</&lt;/g;
1521 1567
1522 # this is buggy, it needs to properly take care of nested <'s 1568 while () {
1569 if ($pod =~ /\G( (?: [^BCGHITU]+ | .(?=[^<]) )+ )/xgcs) {
1570 $group = $1;
1523 1571
1524 1 while 1572 $group =~ s/&/&amp;/g;
1525 # replace B<>, I<>, U<> etc. 1573 $group =~ s/</&lt;/g;
1526 s/B<([^\>]*)>/<b>$1<\/b>/ 1574
1527 || s/I<([^\>]*)>/<i>$1<\/i>/ 1575 $xml .= $group;
1528 || s/U<([^\>]*)>/<u>$1<\/u>/ 1576 } elsif ($pod =~ m%\G
1529 || s/T<([^\>]*)>/<big><b>$1<\/b><\/big>/ 1577 ([BCGHITU])
1530 # replace G<male|female> tags 1578 <
1531 || s{G<([^>|]*)\|([^>]*)>}{ 1579 (?:
1532 $self->gender ? $2 : $1 1580 ([^<>]*) (?{ $group = $^N })
1533 }ge 1581 | < $interior >
1534 # replace H<hint text> 1582 )
1535 || s{H<([^\>]*)>} 1583 >
1584 %gcsx
1536 { 1585 ) {
1586 my ($code, $data) = ($1, $group);
1587
1588 if ($code eq "B") {
1589 $xml .= "<b>" . expand_cfpod ($self, $data) . "</b>";
1590 } elsif ($code eq "I") {
1591 $xml .= "<i>" . expand_cfpod ($self, $data) . "</i>";
1592 } elsif ($code eq "U") {
1593 $xml .= "<u>" . expand_cfpod ($self, $data) . "</u>";
1594 } elsif ($code eq "C") {
1595 $xml .= "<tt>" . expand_cfpod ($self, $data) . "</tt>";
1596 } elsif ($code eq "T") {
1597 $xml .= "<big><b>" . expand_cfpod ($self, $data) . "</b></big>";
1598 } elsif ($code eq "G") {
1599 my ($male, $female) = split /\|/, $data;
1600 $data = $self->gender ? $female : $male;
1601 $xml .= expand_cfpod ($self, $data);
1602 } elsif ($code eq "H") {
1537 ("<fg name=\"lightblue\">[$1 (Use hintmode to suppress hints)]</fg>", 1603 $xml .= ("<fg name=\"lightblue\">[" . expand_cfpod ($self, $data) . " (Use hintmode to suppress hints)]</fg>",
1538 "<fg name=\"lightblue\">[Hint suppressed, see hintmode]</fg>", 1604 "<fg name=\"lightblue\">[Hint suppressed, see hintmode]</fg>",
1539 "") 1605 "")
1540 [$self->{hintmode}] 1606 [$self->{hintmode}];
1607 } else {
1608 $xml .= "error processing '$code($data)' directive";
1541 }ge; 1609 }
1610 } else {
1611 if ($pod =~ /\G(.+)/) {
1612 warn "parse error while expanding $pod (at $1)";
1613 }
1614 last;
1615 }
1616 }
1542 1617
1618 for ($xml) {
1543 # create single paragraphs (very hackish) 1619 # create single paragraphs (very hackish)
1544 s/(?<=\S)\n(?=\w)/ /g; 1620 s/(?<=\S)\n(?=\w)/ /g;
1545 1621
1546 # compress some whitespace 1622 # compress some whitespace
1547 s/\s+\n/\n/g; # ws line-ends 1623 s/\s+\n/\n/g; # ws line-ends
1548 s/\n\n+/\n/g; # double lines 1624 s/\n\n+/\n/g; # double lines
1549 s/^\n+//; # beginning lines 1625 s/^\n+//; # beginning lines
1550 s/\n+$//; # ending lines 1626 s/\n+$//; # ending lines
1627 }
1551 1628
1552 $_ 1629 $xml
1553} 1630}
1554 1631
1555sub hintmode { 1632sub hintmode {
1556 $_[0]{hintmode} = $_[1] if @_ > 1; 1633 $_[0]{hintmode} = $_[1] if @_ > 1;
1557 $_[0]{hintmode} 1634 $_[0]{hintmode}
2273=cut 2350=cut
2274 2351
2275sub deref { 2352sub deref {
2276 my ($ref) = @_; 2353 my ($ref) = @_;
2277 2354
2278 # temporary compatibility#TODO#remove
2279 $ref =~ s{^<}{player/<};
2280
2281 if ($ref =~ m{^player\/(<1\.[0-9a-f]+>)/(.*)$}) { 2355 if ($ref =~ m{^player\/(<1\.[0-9a-f]+>)/(.*)$}) {
2282 my ($uuid, $name) = ($1, $2); 2356 my ($uuid, $name) = ($1, $2);
2283 my $pl = $cf::PLAYER_LOADING{$name} || cf::player::find $name 2357 my $pl = $cf::PLAYER_LOADING{$name} || cf::player::find $name
2284 or return; 2358 or return;
2285 $pl->ob->uuid eq $uuid 2359 $pl->ob->uuid eq $uuid
2631the message, with C<log> being the default. If C<$color> is negative, suppress 2705the message, with C<log> being the default. If C<$color> is negative, suppress
2632the message unless the client supports the msg packet. 2706the message unless the client supports the msg packet.
2633 2707
2634=cut 2708=cut
2635 2709
2710# non-persistent channels (usually the info channel)
2636our %CHANNEL = ( 2711our %CHANNEL = (
2637 "c/identify" => { 2712 "c/identify" => {
2638 id => "infobox", 2713 id => "infobox",
2639 title => "Identify", 2714 title => "Identify",
2640 reply => undef, 2715 reply => undef,
2644 id => "infobox", 2719 id => "infobox",
2645 title => "Examine", 2720 title => "Examine",
2646 reply => undef, 2721 reply => undef,
2647 tooltip => "Signs and other items you examined", 2722 tooltip => "Signs and other items you examined",
2648 }, 2723 },
2724 "c/book" => {
2725 id => "infobox",
2726 title => "Book",
2727 reply => undef,
2728 tooltip => "The contents of a note or book",
2729 },
2649 "c/lookat" => { 2730 "c/lookat" => {
2650 id => "infobox", 2731 id => "infobox",
2651 title => "Look", 2732 title => "Look",
2652 reply => undef, 2733 reply => undef,
2653 tooltip => "What you saw there", 2734 tooltip => "What you saw there",
2735 },
2736 "c/who" => {
2737 id => "infobox",
2738 title => "Players",
2739 reply => undef,
2740 tooltip => "Shows players who are currently online",
2741 },
2742 "c/body" => {
2743 id => "infobox",
2744 title => "Body Parts",
2745 reply => undef,
2746 tooltip => "Shows which body parts you posess and are available",
2747 },
2748 "c/uptime" => {
2749 id => "infobox",
2750 title => "Uptime",
2751 reply => undef,
2752 tooltip => "How long the server has been running since last restart",
2753 },
2754 "c/mapinfo" => {
2755 id => "infobox",
2756 title => "Map Info",
2757 reply => undef,
2758 tooltip => "Information related to the maps",
2654 }, 2759 },
2655); 2760);
2656 2761
2657sub cf::client::send_msg { 2762sub cf::client::send_msg {
2658 my ($self, $channel, $msg, $color, @extra) = @_; 2763 my ($self, $channel, $msg, $color, @extra) = @_;
2895=pod 3000=pod
2896 3001
2897The following functions and methods are available within a safe environment: 3002The following functions and methods are available within a safe environment:
2898 3003
2899 cf::object 3004 cf::object
2900 contr pay_amount pay_player map x y force_find force_add 3005 contr pay_amount pay_player map x y force_find force_add destroy
2901 insert remove name archname title slaying race decrease_ob_nr 3006 insert remove name archname title slaying race decrease_ob_nr
2902 3007
2903 cf::object::player 3008 cf::object::player
2904 player 3009 player
2905 3010
2912=cut 3017=cut
2913 3018
2914for ( 3019for (
2915 ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y 3020 ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y
2916 insert remove inv name archname title slaying race 3021 insert remove inv name archname title slaying race
2917 decrease_ob_nr)], 3022 decrease_ob_nr destroy)],
2918 ["cf::object::player" => qw(player)], 3023 ["cf::object::player" => qw(player)],
2919 ["cf::player" => qw(peaceful)], 3024 ["cf::player" => qw(peaceful)],
2920 ["cf::map" => qw(trigger)], 3025 ["cf::map" => qw(trigger)],
2921) { 3026) {
2922 no strict 'refs'; 3027 no strict 'refs';
3279 # and maps saved/destroyed asynchronously. 3384 # and maps saved/destroyed asynchronously.
3280 warn "begin emergency player save\n"; 3385 warn "begin emergency player save\n";
3281 for my $login (keys %cf::PLAYER) { 3386 for my $login (keys %cf::PLAYER) {
3282 my $pl = $cf::PLAYER{$login} or next; 3387 my $pl = $cf::PLAYER{$login} or next;
3283 $pl->valid or next; 3388 $pl->valid or next;
3389 delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt
3284 $pl->save; 3390 $pl->save;
3285 } 3391 }
3286 warn "end emergency player save\n"; 3392 warn "end emergency player save\n";
3287 3393
3288 warn "begin emergency map save\n"; 3394 warn "begin emergency map save\n";
3336 3442
3337 warn "flushing outstanding aio requests"; 3443 warn "flushing outstanding aio requests";
3338 for (;;) { 3444 for (;;) {
3339 BDB::flush; 3445 BDB::flush;
3340 IO::AIO::flush; 3446 IO::AIO::flush;
3341 Coro::cede; 3447 Coro::cede_notself;
3342 last unless IO::AIO::nreqs || BDB::nreqs; 3448 last unless IO::AIO::nreqs || BDB::nreqs;
3343 warn "iterate..."; 3449 warn "iterate...";
3344 } 3450 }
3345 3451
3346 ++$RELOAD; 3452 ++$RELOAD;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines