… | |
… | |
63 | |
63 | |
64 | # configure various modules to our taste |
64 | # configure various modules to our taste |
65 | # |
65 | # |
66 | $Storable::canonical = 1; # reduce rsync transfers |
66 | $Storable::canonical = 1; # reduce rsync transfers |
67 | Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator |
67 | Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator |
|
|
68 | |
|
|
69 | $Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority |
|
|
70 | |
|
|
71 | # make sure c-lzf reinitialises itself |
|
|
72 | Compress::LZF::set_serializer "Storable", "Storable::net_mstore", "Storable::mretrieve"; |
68 | Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later |
73 | Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later |
69 | |
|
|
70 | $Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority |
|
|
71 | |
74 | |
72 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
75 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
73 | |
76 | |
74 | our %COMMAND = (); |
77 | our %COMMAND = (); |
75 | our %COMMAND_TIME = (); |
78 | our %COMMAND_TIME = (); |
… | |
… | |
78 | our %EXTCMD = (); |
81 | our %EXTCMD = (); |
79 | our %EXTICMD = (); |
82 | our %EXTICMD = (); |
80 | our %EXT_CORO = (); # coroutines bound to extensions |
83 | our %EXT_CORO = (); # coroutines bound to extensions |
81 | our %EXT_MAP = (); # pluggable maps |
84 | our %EXT_MAP = (); # pluggable maps |
82 | |
85 | |
83 | our $RELOAD; # number of reloads so far |
86 | our $RELOAD; # number of reloads so far, non-zero while in reload |
84 | our @EVENT; |
87 | our @EVENT; |
85 | |
88 | |
86 | our $CONFDIR = confdir; |
89 | our $CONFDIR = confdir; |
87 | our $DATADIR = datadir; |
90 | our $DATADIR = datadir; |
88 | our $LIBDIR = "$DATADIR/ext"; |
91 | our $LIBDIR = "$DATADIR/ext"; |
89 | our $PODDIR = "$DATADIR/pod"; |
92 | our $PODDIR = "$DATADIR/pod"; |
90 | our $MAPDIR = "$DATADIR/" . mapdir; |
93 | our $MAPDIR = "$DATADIR/" . mapdir; |
91 | our $LOCALDIR = localdir; |
94 | our $LOCALDIR = localdir; |
92 | our $TMPDIR = "$LOCALDIR/" . tmpdir; |
95 | our $TMPDIR = "$LOCALDIR/" . tmpdir; |
93 | our $UNIQUEDIR = "$LOCALDIR/" . uniquedir; |
96 | our $UNIQUEDIR = "$LOCALDIR/" . uniquedir; |
94 | our $PLAYERDIR = "$LOCALDIR/" . playerdir; |
97 | our $PLAYERDIR = "$LOCALDIR/" . playerdir; |
95 | our $RANDOMDIR = "$LOCALDIR/random"; |
98 | our $RANDOMDIR = "$LOCALDIR/random"; |
96 | our $BDBDIR = "$LOCALDIR/db"; |
99 | our $BDBDIR = "$LOCALDIR/db"; |
|
|
100 | our $PIDFILE = "$LOCALDIR/pid"; |
|
|
101 | our $RUNTIMEFILE = "$LOCALDIR/runtime"; |
|
|
102 | |
97 | our %RESOURCE; |
103 | our %RESOURCE; |
98 | |
104 | |
99 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
105 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
100 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
106 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
101 | our $NEXT_TICK; |
107 | our $NEXT_TICK; |
… | |
… | |
122 | our $LOAD; # a number between 0 (idle) and 1 (too many objects) |
128 | our $LOAD; # a number between 0 (idle) and 1 (too many objects) |
123 | our $LOADAVG; # same thing, but with alpha-smoothing |
129 | our $LOADAVG; # same thing, but with alpha-smoothing |
124 | our $JITTER; # average jitter |
130 | our $JITTER; # average jitter |
125 | our $TICK_START; # for load detecting purposes |
131 | our $TICK_START; # for load detecting purposes |
126 | |
132 | |
|
|
133 | our @POST_INIT; |
|
|
134 | |
|
|
135 | our $REATTACH_ON_RELOAD; # ste to true to force object reattach on reload (slow) |
|
|
136 | |
127 | binmode STDOUT; |
137 | binmode STDOUT; |
128 | binmode STDERR; |
138 | binmode STDERR; |
129 | |
139 | |
130 | # read virtual server time, if available |
140 | # read virtual server time, if available |
131 | unless ($RUNTIME || !-e "$LOCALDIR/runtime") { |
141 | unless ($RUNTIME || !-e $RUNTIMEFILE) { |
132 | open my $fh, "<", "$LOCALDIR/runtime" |
142 | open my $fh, "<", $RUNTIMEFILE |
133 | or die "unable to read runtime file: $!"; |
143 | or die "unable to read $RUNTIMEFILE file: $!"; |
134 | $RUNTIME = <$fh> + 0.; |
144 | $RUNTIME = <$fh> + 0.; |
135 | } |
145 | } |
136 | |
146 | |
137 | mkdir $_ |
147 | mkdir $_ |
138 | for $LOCALDIR, $TMPDIR, $UNIQUEDIR, $PLAYERDIR, $RANDOMDIR, $BDBDIR; |
148 | for $LOCALDIR, $TMPDIR, $UNIQUEDIR, $PLAYERDIR, $RANDOMDIR, $BDBDIR; |
… | |
… | |
299 | our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max |
309 | our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max |
300 | |
310 | |
301 | sub encode_json($) { $json_coder->encode ($_[0]) } |
311 | sub encode_json($) { $json_coder->encode ($_[0]) } |
302 | sub decode_json($) { $json_coder->decode ($_[0]) } |
312 | sub decode_json($) { $json_coder->decode ($_[0]) } |
303 | |
313 | |
|
|
314 | =item cf::post_init { BLOCK } |
|
|
315 | |
|
|
316 | Execute the given codeblock, I<after> all extensions have been (re-)loaded, |
|
|
317 | but I<before> the server starts ticking again. |
|
|
318 | |
|
|
319 | The cdoeblock will have a single boolean argument to indicate whether this |
|
|
320 | is a reload or not. |
|
|
321 | |
|
|
322 | =cut |
|
|
323 | |
|
|
324 | sub post_init(&) { |
|
|
325 | push @POST_INIT, shift; |
|
|
326 | } |
|
|
327 | |
304 | =item cf::lock_wait $string |
328 | =item cf::lock_wait $string |
305 | |
329 | |
306 | Wait until the given lock is available. See cf::lock_acquire. |
330 | Wait until the given lock is available. See cf::lock_acquire. |
307 | |
331 | |
308 | =item my $lock = cf::lock_acquire $string |
332 | =item my $lock = cf::lock_acquire $string |
… | |
… | |
335 | return;#d# |
359 | return;#d# |
336 | }#d# |
360 | }#d# |
337 | |
361 | |
338 | # wait for lock, if any |
362 | # wait for lock, if any |
339 | while ($LOCK{$key}) { |
363 | while ($LOCK{$key}) { |
|
|
364 | #local $Coro::current->{desc} = "$Coro::current->{desc} <waiting for lock $key>"; |
340 | push @{ $LOCK{$key} }, $Coro::current; |
365 | push @{ $LOCK{$key} }, $Coro::current; |
341 | Coro::schedule; |
366 | Coro::schedule; |
342 | } |
367 | } |
343 | } |
368 | } |
344 | |
369 | |
… | |
… | |
727 | |
752 | |
728 | ############################################################################# |
753 | ############################################################################# |
729 | |
754 | |
730 | =head2 ATTACHABLE OBJECTS |
755 | =head2 ATTACHABLE OBJECTS |
731 | |
756 | |
732 | Many objects in crossfire are so-called attachable objects. That means you can |
757 | Many objects in deliantra are so-called attachable objects. That means you can |
733 | attach callbacks/event handlers (a collection of which is called an "attachment") |
758 | attach callbacks/event handlers (a collection of which is called an "attachment") |
734 | to it. All such attachable objects support the following methods. |
759 | to it. All such attachable objects support the following methods. |
735 | |
760 | |
736 | In the following description, CLASS can be any of C<global>, C<object> |
761 | In the following description, CLASS can be any of C<global>, C<object> |
737 | C<player>, C<client> or C<map> (i.e. the attachable objects in |
762 | C<player>, C<client> or C<map> (i.e. the attachable objects in |
… | |
… | |
787 | =item cf::CLASS::attachment $name, ... |
812 | =item cf::CLASS::attachment $name, ... |
788 | |
813 | |
789 | Register an attachment by C<$name> through which attachable objects of the |
814 | Register an attachment by C<$name> through which attachable objects of the |
790 | given CLASS can refer to this attachment. |
815 | given CLASS can refer to this attachment. |
791 | |
816 | |
792 | Some classes such as crossfire maps and objects can specify attachments |
817 | Some classes such as deliantra maps and objects can specify attachments |
793 | that are attached at load/instantiate time, thus the need for a name. |
818 | that are attached at load/instantiate time, thus the need for a name. |
794 | |
819 | |
795 | These calls expect any number of the following handler/hook descriptions: |
820 | These calls expect any number of the following handler/hook descriptions: |
796 | |
821 | |
797 | =over 4 |
822 | =over 4 |
… | |
… | |
1164 | } else { |
1189 | } else { |
1165 | aio_unlink "$filename.pst"; |
1190 | aio_unlink "$filename.pst"; |
1166 | } |
1191 | } |
1167 | |
1192 | |
1168 | aio_rename "$filename~", $filename; |
1193 | aio_rename "$filename~", $filename; |
|
|
1194 | |
|
|
1195 | $filename =~ s%/[^/]+$%%; |
|
|
1196 | aio_pathsync $filename if $cf::USE_FSYNC; |
1169 | } else { |
1197 | } else { |
1170 | warn "FATAL: $filename~: $!\n"; |
1198 | warn "FATAL: $filename~: $!\n"; |
1171 | } |
1199 | } |
1172 | } else { |
1200 | } else { |
1173 | aio_unlink $filename; |
1201 | aio_unlink $filename; |
… | |
… | |
1266 | my ($name, $cb) = @_; |
1294 | my ($name, $cb) = @_; |
1267 | |
1295 | |
1268 | $EXTICMD{$name} = $cb; |
1296 | $EXTICMD{$name} = $cb; |
1269 | } |
1297 | } |
1270 | |
1298 | |
|
|
1299 | use File::Glob (); |
|
|
1300 | |
1271 | cf::player->attach ( |
1301 | cf::player->attach ( |
1272 | on_command => sub { |
1302 | on_command => sub { |
1273 | my ($pl, $name, $params) = @_; |
1303 | my ($pl, $name, $params) = @_; |
1274 | |
1304 | |
1275 | my $cb = $COMMAND{$name} |
1305 | my $cb = $COMMAND{$name} |
… | |
… | |
1306 | } |
1336 | } |
1307 | |
1337 | |
1308 | cf::override; |
1338 | cf::override; |
1309 | }, |
1339 | }, |
1310 | ); |
1340 | ); |
|
|
1341 | |
|
|
1342 | # "readahead" all extensions |
|
|
1343 | sub cache_extensions { |
|
|
1344 | my $grp = IO::AIO::aio_group; |
|
|
1345 | |
|
|
1346 | add $grp IO::AIO::aio_readdir $LIBDIR, sub { |
|
|
1347 | for (grep /\.ext$/, @{$_[0]}) { |
|
|
1348 | add $grp IO::AIO::aio_load "$LIBDIR/$_", my $data; |
|
|
1349 | } |
|
|
1350 | }; |
|
|
1351 | |
|
|
1352 | $grp |
|
|
1353 | } |
1311 | |
1354 | |
1312 | sub load_extensions { |
1355 | sub load_extensions { |
1313 | cf::sync_job { |
1356 | cf::sync_job { |
1314 | my %todo; |
1357 | my %todo; |
1315 | |
1358 | |
… | |
… | |
1385 | |
1428 | |
1386 | =back |
1429 | =back |
1387 | |
1430 | |
1388 | =head2 CORE EXTENSIONS |
1431 | =head2 CORE EXTENSIONS |
1389 | |
1432 | |
1390 | Functions and methods that extend core crossfire objects. |
1433 | Functions and methods that extend core deliantra objects. |
1391 | |
1434 | |
1392 | =cut |
1435 | =cut |
1393 | |
1436 | |
1394 | package cf::player; |
1437 | package cf::player; |
1395 | |
1438 | |
… | |
… | |
1438 | |
1481 | |
1439 | sub exists($) { |
1482 | sub exists($) { |
1440 | my ($login) = @_; |
1483 | my ($login) = @_; |
1441 | |
1484 | |
1442 | $cf::PLAYER{$login} |
1485 | $cf::PLAYER{$login} |
1443 | or cf::sync_job { !aio_stat path $login } |
1486 | or !aio_stat path $login |
1444 | } |
1487 | } |
1445 | |
1488 | |
1446 | sub find($) { |
1489 | sub find($) { |
1447 | return $cf::PLAYER{$_[0]} || do { |
1490 | return $cf::PLAYER{$_[0]} || do { |
1448 | my $login = $_[0]; |
1491 | my $login = $_[0]; |
… | |
… | |
1619 | } |
1662 | } |
1620 | |
1663 | |
1621 | \@paths |
1664 | \@paths |
1622 | } |
1665 | } |
1623 | |
1666 | |
1624 | =item $protocol_xml = $player->expand_cfpod ($crossfire_pod) |
1667 | =item $protocol_xml = $player->expand_cfpod ($cfpod) |
1625 | |
1668 | |
1626 | Expand crossfire pod fragments into protocol xml. |
1669 | Expand deliantra pod fragments into protocol xml. |
1627 | |
|
|
1628 | =cut |
|
|
1629 | |
|
|
1630 | sub expand_cfpod { |
|
|
1631 | my ($self, $pod) = @_; |
|
|
1632 | |
|
|
1633 | my @nest = [qr<\G$>, undef, ""]; |
|
|
1634 | my $xml; |
|
|
1635 | |
|
|
1636 | for ($pod) { |
|
|
1637 | while () { |
|
|
1638 | if (/\G( (?: [^BCEGHITUZ&>\n\ ]+ | [BCEGHITUZ](?!<) | \ (?!>) )+ )/xgcs) { |
|
|
1639 | $xml .= $1; |
|
|
1640 | } elsif (/\G\n(?=\S)/xgcs) { |
|
|
1641 | $xml .= " "; |
|
|
1642 | } elsif (/\G\n/xgcs) { |
|
|
1643 | $xml .= "\n"; |
|
|
1644 | } elsif (/\G ([BCEGHITUZ]) (< (?: <+\ | (?!<) ) )/xgcs) { |
|
|
1645 | my ($code, $delim) = ($1, scalar reverse $2); |
|
|
1646 | $delim =~ y/</>/; # delim now contains the stop sequence |
|
|
1647 | $delim = qr{\G\Q$delim}; |
|
|
1648 | |
|
|
1649 | my $cb; |
|
|
1650 | |
|
|
1651 | if ($code eq "B") { |
|
|
1652 | $cb = sub { "<b>$_[0]</b>" }; |
|
|
1653 | } elsif ($code eq "C") { |
|
|
1654 | $cb = sub { "<tt>$_[0]</tt>" }; |
|
|
1655 | } elsif ($code eq "E") { |
|
|
1656 | $cb = sub { warn "E<$_[0]>\n";"&$_[0];" }; |
|
|
1657 | } elsif ($code eq "G") { |
|
|
1658 | $cb = sub { |
|
|
1659 | my ($male, $female) = split /\|/, $_[0]; |
|
|
1660 | $self->gender ? $female : $male |
|
|
1661 | }; |
|
|
1662 | } elsif ($code eq "H") { |
|
|
1663 | $cb = sub { |
|
|
1664 | ( |
|
|
1665 | "<fg name=\"lightblue\">[$_[0] (Use hintmode to suppress hints)]</fg>", |
|
|
1666 | "<fg name=\"lightblue\">[Hint suppressed, see hintmode]</fg>", |
|
|
1667 | "", |
|
|
1668 | )[$self->{hintmode}]; |
|
|
1669 | }; |
|
|
1670 | } elsif ($code eq "I") { |
|
|
1671 | $cb = sub { "<i>$_[0]</i>" }; |
|
|
1672 | } elsif ($code eq "T") { |
|
|
1673 | $cb = sub { "<big><b>$_[0]</b></big>" }; |
|
|
1674 | } elsif ($code eq "U") { |
|
|
1675 | $cb = sub { "<u>$_[0]</u>" }; |
|
|
1676 | } elsif ($code eq "Z") { |
|
|
1677 | $cb = sub { }; |
|
|
1678 | } else { |
|
|
1679 | die "FATAL error in expand_cfpod"; |
|
|
1680 | } |
|
|
1681 | |
|
|
1682 | push @nest, [$delim, $cb, $xml]; |
|
|
1683 | undef $xml; |
|
|
1684 | |
|
|
1685 | } elsif ($_ =~ /$nest[-1][0]/gcs) { |
|
|
1686 | my $nest = pop @nest; |
|
|
1687 | |
|
|
1688 | if ($nest->[1]) { |
|
|
1689 | $xml = $nest->[2] . $nest->[1]->($xml); |
|
|
1690 | } else { |
|
|
1691 | last; |
|
|
1692 | } |
|
|
1693 | } elsif (/\G</xgcs) { |
|
|
1694 | $xml .= "<"; |
|
|
1695 | } elsif (/\G&/xgcs) { |
|
|
1696 | $xml .= "&"; |
|
|
1697 | } elsif (/\G>/xgcs) { |
|
|
1698 | $xml .= ">"; |
|
|
1699 | } else { |
|
|
1700 | if ($pod =~ /\G(.+)/xgcs) { |
|
|
1701 | warn "parse error (at $1)($nest[-1][0]) while expanding cfpod:\n$pod"; |
|
|
1702 | last; |
|
|
1703 | } else { |
|
|
1704 | warn "parse error (unclosed interior sequence at end of cfpod) while expanding cfpod:\n$pod"; |
|
|
1705 | return "<b>Sorry, the server encountered an internal error when formatting this message, please report this.</b>"; |
|
|
1706 | } |
|
|
1707 | } |
|
|
1708 | } |
|
|
1709 | } |
|
|
1710 | |
|
|
1711 | $xml |
|
|
1712 | } |
|
|
1713 | |
|
|
1714 | sub hintmode { |
|
|
1715 | $_[0]{hintmode} = $_[1] if @_ > 1; |
|
|
1716 | $_[0]{hintmode} |
|
|
1717 | } |
|
|
1718 | |
1670 | |
1719 | =item $player->ext_reply ($msgid, @msg) |
1671 | =item $player->ext_reply ($msgid, @msg) |
1720 | |
1672 | |
1721 | Sends an ext reply to the player. |
1673 | Sends an ext reply to the player. |
1722 | |
1674 | |
… | |
… | |
2536 | |
2488 | |
2537 | =item $player_object->enter_link |
2489 | =item $player_object->enter_link |
2538 | |
2490 | |
2539 | Freezes the player and moves him/her to a special map (C<{link}>). |
2491 | Freezes the player and moves him/her to a special map (C<{link}>). |
2540 | |
2492 | |
2541 | The player should be reasonably safe there for short amounts of time. You |
2493 | The player should be reasonably safe there for short amounts of time (e.g. |
2542 | I<MUST> call C<leave_link> as soon as possible, though. |
2494 | for loading a map). You I<MUST> call C<leave_link> as soon as possible, |
|
|
2495 | though, as the palyer cannot control the character while it is on the link |
|
|
2496 | map. |
2543 | |
2497 | |
2544 | Will never block. |
2498 | Will never block. |
2545 | |
2499 | |
2546 | =item $player_object->leave_link ($map, $x, $y) |
2500 | =item $player_object->leave_link ($map, $x, $y) |
2547 | |
2501 | |
… | |
… | |
2605 | |
2559 | |
2606 | $map->load; |
2560 | $map->load; |
2607 | $map->load_neighbours; |
2561 | $map->load_neighbours; |
2608 | |
2562 | |
2609 | return unless $self->contr->active; |
2563 | return unless $self->contr->active; |
|
|
2564 | $self->flag (cf::FLAG_DEBUG, 0);#d# temp |
2610 | $self->activate_recursive; |
2565 | $self->activate_recursive; |
2611 | |
2566 | |
2612 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
2567 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
2613 | $self->enter_map ($map, $x, $y); |
2568 | $self->enter_map ($map, $x, $y); |
2614 | } |
2569 | } |
… | |
… | |
2777 | $self->contr->savebed ($map, $x, $y) |
2732 | $self->contr->savebed ($map, $x, $y) |
2778 | if $exit->flag (cf::FLAG_DAMNED); |
2733 | if $exit->flag (cf::FLAG_DAMNED); |
2779 | |
2734 | |
2780 | 1 |
2735 | 1 |
2781 | }) { |
2736 | }) { |
2782 | $self->message ("Something went wrong deep within the crossfire server. " |
2737 | $self->message ("Something went wrong deep within the deliantra server. " |
2783 | . "I'll try to bring you back to the map you were before. " |
2738 | . "I'll try to bring you back to the map you were before. " |
2784 | . "Please report this to the dungeon master!", |
2739 | . "Please report this to the dungeon master!", |
2785 | cf::NDI_UNIQUE | cf::NDI_RED); |
2740 | cf::NDI_UNIQUE | cf::NDI_RED); |
2786 | |
2741 | |
2787 | warn "ERROR in enter_exit: $@"; |
2742 | warn "ERROR in enter_exit: $@"; |
… | |
… | |
2853 | id => "infobox", |
2808 | id => "infobox", |
2854 | title => "Body Parts", |
2809 | title => "Body Parts", |
2855 | reply => undef, |
2810 | reply => undef, |
2856 | tooltip => "Shows which body parts you posess and are available", |
2811 | tooltip => "Shows which body parts you posess and are available", |
2857 | }, |
2812 | }, |
|
|
2813 | "c/skills" => { |
|
|
2814 | id => "infobox", |
|
|
2815 | title => "Skills", |
|
|
2816 | reply => undef, |
|
|
2817 | tooltip => "Shows your experience per skill and item power", |
|
|
2818 | }, |
2858 | "c/uptime" => { |
2819 | "c/uptime" => { |
2859 | id => "infobox", |
2820 | id => "infobox", |
2860 | title => "Uptime", |
2821 | title => "Uptime", |
2861 | reply => undef, |
2822 | reply => undef, |
2862 | tooltip => "How long the server has been running since last restart", |
2823 | tooltip => "How long the server has been running since last restart", |
… | |
… | |
2876 | ); |
2837 | ); |
2877 | |
2838 | |
2878 | sub cf::client::send_msg { |
2839 | sub cf::client::send_msg { |
2879 | my ($self, $channel, $msg, $color, @extra) = @_; |
2840 | my ($self, $channel, $msg, $color, @extra) = @_; |
2880 | |
2841 | |
2881 | $msg = $self->pl->expand_cfpod ($msg); |
2842 | $msg = $self->pl->expand_cfpod ($msg) |
|
|
2843 | unless $color & cf::NDI_VERBATIM; |
2882 | |
2844 | |
2883 | $color &= cf::NDI_CLIENT_MASK; # just in case... |
2845 | $color &= cf::NDI_CLIENT_MASK; # just in case... |
2884 | |
2846 | |
2885 | # check predefined channels, for the benefit of C |
2847 | # check predefined channels, for the benefit of C |
2886 | if ($CHANNEL{$channel}) { |
2848 | if ($CHANNEL{$channel}) { |
… | |
… | |
3415 | reload_treasures; |
3377 | reload_treasures; |
3416 | |
3378 | |
3417 | warn "finished reloading resource files\n"; |
3379 | warn "finished reloading resource files\n"; |
3418 | } |
3380 | } |
3419 | |
3381 | |
3420 | sub init { |
|
|
3421 | my $guard = freeze_mainloop; |
|
|
3422 | |
|
|
3423 | evthread_start IO::AIO::poll_fileno; |
|
|
3424 | |
|
|
3425 | reload_resources; |
|
|
3426 | } |
|
|
3427 | |
|
|
3428 | sub reload_config { |
3382 | sub reload_config { |
3429 | open my $fh, "<:utf8", "$CONFDIR/config" |
3383 | open my $fh, "<:utf8", "$CONFDIR/config" |
3430 | or return; |
3384 | or return; |
3431 | |
3385 | |
3432 | local $/; |
3386 | local $/; |
… | |
… | |
3444 | }; |
3398 | }; |
3445 | warn $@ if $@; |
3399 | warn $@ if $@; |
3446 | } |
3400 | } |
3447 | } |
3401 | } |
3448 | |
3402 | |
|
|
3403 | sub pidfile() { |
|
|
3404 | sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT |
|
|
3405 | or die "$PIDFILE: $!"; |
|
|
3406 | flock $fh, &Fcntl::LOCK_EX |
|
|
3407 | or die "$PIDFILE: flock: $!"; |
|
|
3408 | $fh |
|
|
3409 | } |
|
|
3410 | |
|
|
3411 | # make sure only one server instance is running at any one time |
|
|
3412 | sub atomic { |
|
|
3413 | my $fh = pidfile; |
|
|
3414 | |
|
|
3415 | my $pid = <$fh>; |
|
|
3416 | kill 9, $pid if $pid > 0; |
|
|
3417 | |
|
|
3418 | seek $fh, 0, 0; |
|
|
3419 | print $fh $$; |
|
|
3420 | } |
|
|
3421 | |
3449 | sub main { |
3422 | sub main { |
|
|
3423 | cf::init_globals; # initialise logging |
|
|
3424 | |
|
|
3425 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
|
|
3426 | LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
|
|
3427 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
|
|
3428 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
|
|
3429 | |
|
|
3430 | cf::init_experience; |
|
|
3431 | cf::init_anim; |
|
|
3432 | cf::init_attackmess; |
|
|
3433 | cf::init_dynamic; |
|
|
3434 | cf::init_block; |
|
|
3435 | |
|
|
3436 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
|
|
3437 | |
3450 | # we must not ever block the main coroutine |
3438 | # we must not ever block the main coroutine |
3451 | local $Coro::idle = sub { |
3439 | local $Coro::idle = sub { |
3452 | Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# |
3440 | Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# |
3453 | (async { |
3441 | (async { |
3454 | $Coro::current->{desc} = "IDLE BUG HANDLER"; |
3442 | $Coro::current->{desc} = "IDLE BUG HANDLER"; |
3455 | EV::loop EV::LOOP_ONESHOT; |
3443 | EV::loop EV::LOOP_ONESHOT; |
3456 | })->prio (Coro::PRIO_MAX); |
3444 | })->prio (Coro::PRIO_MAX); |
3457 | }; |
3445 | }; |
3458 | |
3446 | |
3459 | { |
3447 | evthread_start IO::AIO::poll_fileno; |
3460 | my $guard = freeze_mainloop; |
3448 | |
|
|
3449 | cf::sync_job { |
|
|
3450 | reload_resources; |
3461 | reload_config; |
3451 | reload_config; |
3462 | db_init; |
3452 | db_init; |
|
|
3453 | |
|
|
3454 | cf::load_settings; |
|
|
3455 | cf::load_materials; |
|
|
3456 | cf::init_uuid; |
|
|
3457 | cf::init_signals; |
|
|
3458 | cf::init_commands; |
|
|
3459 | cf::init_skills; |
|
|
3460 | |
|
|
3461 | cf::init_beforeplay; |
|
|
3462 | |
|
|
3463 | atomic; |
|
|
3464 | |
3463 | load_extensions; |
3465 | load_extensions; |
3464 | |
3466 | |
3465 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3467 | utime time, time, $RUNTIMEFILE; |
|
|
3468 | |
|
|
3469 | # no (long-running) fork's whatsoever before this point(!) |
|
|
3470 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
|
|
3471 | |
|
|
3472 | (pop @POST_INIT)->(0) while @POST_INIT; |
3466 | } |
3473 | }; |
3467 | |
3474 | |
3468 | EV::loop; |
3475 | EV::loop; |
3469 | } |
3476 | } |
3470 | |
3477 | |
3471 | ############################################################################# |
3478 | ############################################################################# |
… | |
… | |
3480 | }; |
3487 | }; |
3481 | } |
3488 | } |
3482 | } |
3489 | } |
3483 | |
3490 | |
3484 | sub write_runtime_sync { |
3491 | sub write_runtime_sync { |
3485 | my $runtime = "$LOCALDIR/runtime"; |
|
|
3486 | |
|
|
3487 | # first touch the runtime file to show we are still running: |
3492 | # first touch the runtime file to show we are still running: |
3488 | # the fsync below can take a very very long time. |
3493 | # the fsync below can take a very very long time. |
3489 | |
3494 | |
3490 | IO::AIO::aio_utime $runtime, undef, undef; |
3495 | IO::AIO::aio_utime $RUNTIMEFILE, undef, undef; |
3491 | |
3496 | |
3492 | my $guard = cf::lock_acquire "write_runtime"; |
3497 | my $guard = cf::lock_acquire "write_runtime"; |
3493 | |
3498 | |
3494 | my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 |
3499 | my $fh = aio_open "$RUNTIMEFILE~", O_WRONLY | O_CREAT, 0644 |
3495 | or return; |
3500 | or return; |
3496 | |
3501 | |
3497 | my $value = $cf::RUNTIME + 90 + 10; |
3502 | my $value = $cf::RUNTIME + 90 + 10; |
3498 | # 10 is the runtime save interval, for a monotonic clock |
3503 | # 10 is the runtime save interval, for a monotonic clock |
3499 | # 60 allows for the watchdog to kill the server. |
3504 | # 60 allows for the watchdog to kill the server. |
… | |
… | |
3509 | aio_utime $fh, undef, undef; |
3514 | aio_utime $fh, undef, undef; |
3510 | |
3515 | |
3511 | close $fh |
3516 | close $fh |
3512 | or return; |
3517 | or return; |
3513 | |
3518 | |
3514 | aio_rename "$runtime~", $runtime |
3519 | aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE |
3515 | and return; |
3520 | and return; |
3516 | |
3521 | |
3517 | warn "runtime file written.\n"; |
3522 | warn "runtime file written.\n"; |
3518 | |
3523 | |
3519 | 1 |
3524 | 1 |
… | |
… | |
3531 | my $uuid = "$LOCALDIR/uuid"; |
3536 | my $uuid = "$LOCALDIR/uuid"; |
3532 | |
3537 | |
3533 | my $fh = aio_open "$uuid~", O_WRONLY | O_CREAT, 0644 |
3538 | my $fh = aio_open "$uuid~", O_WRONLY | O_CREAT, 0644 |
3534 | or return; |
3539 | or return; |
3535 | |
3540 | |
3536 | my $value = uuid_str $uuid_skip + uuid_seq uuid_cur; |
3541 | my $value = uuid_seq uuid_cur; |
|
|
3542 | |
|
|
3543 | unless ($value) { |
|
|
3544 | warn "cowardly refusing to write zero uuid value!\n"; |
|
|
3545 | return; |
|
|
3546 | } |
|
|
3547 | |
|
|
3548 | my $value = uuid_str $value + $uuid_skip; |
3537 | $uuid_skip = 0; |
3549 | $uuid_skip = 0; |
3538 | |
3550 | |
3539 | (aio_write $fh, 0, (length $value), $value, 0) <= 0 |
3551 | (aio_write $fh, 0, (length $value), $value, 0) <= 0 |
3540 | and return; |
3552 | and return; |
3541 | |
3553 | |
… | |
… | |
3563 | } |
3575 | } |
3564 | |
3576 | |
3565 | sub emergency_save() { |
3577 | sub emergency_save() { |
3566 | my $freeze_guard = cf::freeze_mainloop; |
3578 | my $freeze_guard = cf::freeze_mainloop; |
3567 | |
3579 | |
3568 | warn "enter emergency perl save\n"; |
3580 | warn "emergency_perl_save: enter\n"; |
3569 | |
3581 | |
3570 | cf::sync_job { |
3582 | cf::sync_job { |
|
|
3583 | # this is a trade-off: we want to be very quick here, so |
|
|
3584 | # save all maps without fsync, and later call a global sync |
|
|
3585 | # (which in turn might be very very slow) |
|
|
3586 | local $USE_FSYNC = 0; |
|
|
3587 | |
3571 | # use a peculiar iteration method to avoid tripping on perl |
3588 | # use a peculiar iteration method to avoid tripping on perl |
3572 | # refcount bugs in for. also avoids problems with players |
3589 | # refcount bugs in for. also avoids problems with players |
3573 | # and maps saved/destroyed asynchronously. |
3590 | # and maps saved/destroyed asynchronously. |
3574 | warn "begin emergency player save\n"; |
3591 | warn "emergency_perl_save: begin player save\n"; |
3575 | for my $login (keys %cf::PLAYER) { |
3592 | for my $login (keys %cf::PLAYER) { |
3576 | my $pl = $cf::PLAYER{$login} or next; |
3593 | my $pl = $cf::PLAYER{$login} or next; |
3577 | $pl->valid or next; |
3594 | $pl->valid or next; |
3578 | delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt |
3595 | delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt |
3579 | $pl->save; |
3596 | $pl->save; |
3580 | } |
3597 | } |
3581 | warn "end emergency player save\n"; |
3598 | warn "emergency_perl_save: end player save\n"; |
3582 | |
3599 | |
3583 | warn "begin emergency map save\n"; |
3600 | warn "emergency_perl_save: begin map save\n"; |
3584 | for my $path (keys %cf::MAP) { |
3601 | for my $path (keys %cf::MAP) { |
3585 | my $map = $cf::MAP{$path} or next; |
3602 | my $map = $cf::MAP{$path} or next; |
3586 | $map->valid or next; |
3603 | $map->valid or next; |
3587 | $map->save; |
3604 | $map->save; |
3588 | } |
3605 | } |
3589 | warn "end emergency map save\n"; |
3606 | warn "emergency_perl_save: end map save\n"; |
3590 | |
3607 | |
3591 | warn "begin emergency database checkpoint\n"; |
3608 | warn "emergency_perl_save: begin database checkpoint\n"; |
3592 | BDB::db_env_txn_checkpoint $DB_ENV; |
3609 | BDB::db_env_txn_checkpoint $DB_ENV; |
3593 | warn "end emergency database checkpoint\n"; |
3610 | warn "emergency_perl_save: end database checkpoint\n"; |
3594 | |
3611 | |
3595 | warn "begin write uuid\n"; |
3612 | warn "emergency_perl_save: begin write uuid\n"; |
3596 | write_uuid_sync 1; |
3613 | write_uuid_sync 1; |
3597 | warn "end write uuid\n"; |
3614 | warn "emergency_perl_save: end write uuid\n"; |
3598 | }; |
3615 | }; |
3599 | |
3616 | |
|
|
3617 | warn "emergency_perl_save: starting sync()\n"; |
|
|
3618 | IO::AIO::aio_sync sub { |
|
|
3619 | warn "emergency_perl_save: finished sync()\n"; |
|
|
3620 | }; |
|
|
3621 | |
3600 | warn "leave emergency perl save\n"; |
3622 | warn "emergency_perl_save: leave\n"; |
3601 | } |
3623 | } |
3602 | |
3624 | |
3603 | sub post_cleanup { |
3625 | sub post_cleanup { |
3604 | my ($make_core) = @_; |
3626 | my ($make_core) = @_; |
3605 | |
3627 | |
3606 | warn Carp::longmess "post_cleanup backtrace" |
3628 | warn Carp::longmess "post_cleanup backtrace" |
3607 | if $make_core; |
3629 | if $make_core; |
|
|
3630 | |
|
|
3631 | my $fh = pidfile; |
|
|
3632 | unlink $PIDFILE if <$fh> == $$; |
3608 | } |
3633 | } |
3609 | |
3634 | |
3610 | # a safer delete_package, copied from Symbol |
3635 | # a safer delete_package, copied from Symbol |
3611 | sub clear_package($) { |
3636 | sub clear_package($) { |
3612 | my $pkg = shift; |
3637 | my $pkg = shift; |
… | |
… | |
3628 | my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH}; |
3653 | my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH}; |
3629 | for my $name (keys %$leaf_symtab) { |
3654 | for my $name (keys %$leaf_symtab) { |
3630 | _gv_clear *{"$pkg$name"}; |
3655 | _gv_clear *{"$pkg$name"}; |
3631 | # use PApp::Util; PApp::Util::sv_dump *{"$pkg$name"}; |
3656 | # use PApp::Util; PApp::Util::sv_dump *{"$pkg$name"}; |
3632 | } |
3657 | } |
3633 | warn "cleared package #$pkg\n";#d# |
3658 | warn "cleared package $pkg\n";#d# |
3634 | } |
3659 | } |
3635 | |
|
|
3636 | our $RELOAD; # how many times to reload |
|
|
3637 | |
3660 | |
3638 | sub do_reload_perl() { |
3661 | sub do_reload_perl() { |
3639 | # can/must only be called in main |
3662 | # can/must only be called in main |
3640 | if ($Coro::current != $Coro::main) { |
3663 | if ($Coro::current != $Coro::main) { |
3641 | warn "can only reload from main coroutine"; |
3664 | warn "can only reload from main coroutine"; |
3642 | return; |
3665 | return; |
3643 | } |
3666 | } |
3644 | |
3667 | |
3645 | return if $RELOAD++; |
3668 | return if $RELOAD++; |
|
|
3669 | |
|
|
3670 | my $t1 = EV::time; |
3646 | |
3671 | |
3647 | while ($RELOAD) { |
3672 | while ($RELOAD) { |
3648 | warn "reloading..."; |
3673 | warn "reloading..."; |
3649 | |
3674 | |
3650 | warn "entering sync_job"; |
3675 | warn "entering sync_job"; |
… | |
… | |
3729 | cf::reload_config; |
3754 | cf::reload_config; |
3730 | |
3755 | |
3731 | warn "loading extensions"; |
3756 | warn "loading extensions"; |
3732 | cf::load_extensions; |
3757 | cf::load_extensions; |
3733 | |
3758 | |
|
|
3759 | if ($REATTACH_ON_RELOAD) { |
3734 | warn "reattaching attachments to objects/players"; |
3760 | warn "reattaching attachments to objects/players"; |
3735 | _global_reattach; # objects, sockets |
3761 | _global_reattach; # objects, sockets |
3736 | warn "reattaching attachments to maps"; |
3762 | warn "reattaching attachments to maps"; |
3737 | reattach $_ for values %MAP; |
3763 | reattach $_ for values %MAP; |
3738 | warn "reattaching attachments to players"; |
3764 | warn "reattaching attachments to players"; |
3739 | reattach $_ for values %PLAYER; |
3765 | reattach $_ for values %PLAYER; |
|
|
3766 | } |
|
|
3767 | |
|
|
3768 | warn "running post_init jobs"; |
|
|
3769 | (pop @POST_INIT)->(1) while @POST_INIT; |
3740 | |
3770 | |
3741 | warn "leaving sync_job"; |
3771 | warn "leaving sync_job"; |
3742 | |
3772 | |
3743 | 1 |
3773 | 1 |
3744 | } or do { |
3774 | } or do { |
… | |
… | |
3747 | }; |
3777 | }; |
3748 | |
3778 | |
3749 | warn "reloaded"; |
3779 | warn "reloaded"; |
3750 | --$RELOAD; |
3780 | --$RELOAD; |
3751 | } |
3781 | } |
|
|
3782 | |
|
|
3783 | $t1 = EV::time - $t1; |
|
|
3784 | warn "reload completed in ${t1}s\n"; |
3752 | }; |
3785 | }; |
3753 | |
3786 | |
3754 | our $RELOAD_WATCHER; # used only during reload |
3787 | our $RELOAD_WATCHER; # used only during reload |
3755 | |
3788 | |
3756 | sub reload_perl() { |
3789 | sub reload_perl() { |
3757 | # doing reload synchronously and two reloads happen back-to-back, |
3790 | # doing reload synchronously and two reloads happen back-to-back, |
3758 | # coro crashes during coro_state_free->destroy here. |
3791 | # coro crashes during coro_state_free->destroy here. |
3759 | |
3792 | |
|
|
3793 | $RELOAD_WATCHER ||= cf::async { |
|
|
3794 | Coro::AIO::aio_wait cache_extensions; |
|
|
3795 | |
3760 | $RELOAD_WATCHER ||= EV::timer 0, 0, sub { |
3796 | $RELOAD_WATCHER = EV::timer $TICK * 1.5, 0, sub { |
3761 | do_reload_perl; |
3797 | do_reload_perl; |
3762 | undef $RELOAD_WATCHER; |
3798 | undef $RELOAD_WATCHER; |
|
|
3799 | }; |
3763 | }; |
3800 | }; |
3764 | } |
3801 | } |
3765 | |
3802 | |
3766 | register_command "reload" => sub { |
3803 | register_command "reload" => sub { |
3767 | my ($who, $arg) = @_; |
3804 | my ($who, $arg) = @_; |
… | |
… | |
3893 | $msg =~ s/\n//; |
3930 | $msg =~ s/\n//; |
3894 | |
3931 | |
3895 | # limit the # of concurrent backtraces |
3932 | # limit the # of concurrent backtraces |
3896 | if ($_log_backtrace < 2) { |
3933 | if ($_log_backtrace < 2) { |
3897 | ++$_log_backtrace; |
3934 | ++$_log_backtrace; |
|
|
3935 | my $perl_bt = Carp::longmess $msg; |
3898 | async { |
3936 | async { |
3899 | $Coro::current->{desc} = "abt $msg"; |
3937 | $Coro::current->{desc} = "abt $msg"; |
3900 | |
3938 | |
3901 | my @bt = fork_call { |
3939 | my @bt = fork_call { |
3902 | @addr = map { sprintf "%x", $_ } @addr; |
3940 | @addr = map { sprintf "%x", $_ } @addr; |
… | |
… | |
3913 | } |
3951 | } |
3914 | |
3952 | |
3915 | @funcs |
3953 | @funcs |
3916 | }; |
3954 | }; |
3917 | |
3955 | |
3918 | LOG llevInfo, "[ABT] $msg\n"; |
3956 | LOG llevInfo, "[ABT] $perl_bt\n"; |
|
|
3957 | LOG llevInfo, "[ABT] --- C backtrace follows ---\n"; |
3919 | LOG llevInfo, "[ABT] $_\n" for @bt; |
3958 | LOG llevInfo, "[ABT] $_\n" for @bt; |
3920 | --$_log_backtrace; |
3959 | --$_log_backtrace; |
3921 | }; |
3960 | }; |
3922 | } else { |
3961 | } else { |
3923 | LOG llevInfo, "[ABT] $msg\n"; |
3962 | LOG llevInfo, "[ABT] $msg\n"; |