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.571 by root, Wed May 4 19:04:45 2011 UTC vs.
Revision 1.593 by root, Fri Nov 9 01:59:33 2012 UTC

1# 1#
2# This file is part of Deliantra, the Roguelike Realtime MMORPG. 2# This file is part of Deliantra, the Roguelike Realtime MMORPG.
3# 3#
4# Copyright (©) 2006,2007,2008,2009,2010,2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 4# Copyright (©) 2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5# 5#
6# Deliantra is free software: you can redistribute it and/or modify it under 6# Deliantra is free software: you can redistribute it and/or modify it under
7# the terms of the Affero GNU General Public License as published by the 7# the terms of the Affero GNU General Public License as published by the
8# Free Software Foundation, either version 3 of the License, or (at your 8# Free Software Foundation, either version 3 of the License, or (at your
9# option) any later version. 9# option) any later version.
10# 10#
11# This program is distributed in the hope that it will be useful, 11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of 12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details. 14# GNU General Public License for more details.
15# 15#
16# You should have received a copy of the Affero GNU General Public License 16# You should have received a copy of the Affero GNU General Public License
17# and the GNU General Public License along with this program. If not, see 17# and the GNU General Public License along with this program. If not, see
18# <http://www.gnu.org/licenses/>. 18# <http://www.gnu.org/licenses/>.
19# 19#
20# The authors can be reached via e-mail to <support@deliantra.net> 20# The authors can be reached via e-mail to <support@deliantra.net>
21# 21#
22 22
23package cf; 23package cf;
24 24
32use Safe; 32use Safe;
33use Safe::Hole; 33use Safe::Hole;
34use Storable (); 34use Storable ();
35use Carp (); 35use Carp ();
36 36
37use Guard (); 37use AnyEvent ();
38use AnyEvent::IO ();
39use AnyEvent::DNS ();
40
38use Coro (); 41use Coro ();
39use Coro::State; 42use Coro::State;
40use Coro::Handle; 43use Coro::Handle;
41use Coro::EV; 44use Coro::EV;
42use Coro::AnyEvent; 45use Coro::AnyEvent;
48use Coro::AIO; 51use Coro::AIO;
49use Coro::BDB 1.6; 52use Coro::BDB 1.6;
50use Coro::Storable; 53use Coro::Storable;
51use Coro::Util (); 54use Coro::Util ();
52 55
56use Guard ();
53use JSON::XS 2.01 (); 57use JSON::XS 2.01 ();
54use BDB (); 58use BDB ();
55use Data::Dumper; 59use Data::Dumper;
56use Fcntl; 60use Fcntl;
57use YAML::XS (); 61use YAML::XS ();
108our $RANDOMDIR = "$LOCALDIR/random"; 112our $RANDOMDIR = "$LOCALDIR/random";
109our $BDBDIR = "$LOCALDIR/db"; 113our $BDBDIR = "$LOCALDIR/db";
110our $PIDFILE = "$LOCALDIR/pid"; 114our $PIDFILE = "$LOCALDIR/pid";
111our $RUNTIMEFILE = "$LOCALDIR/runtime"; 115our $RUNTIMEFILE = "$LOCALDIR/runtime";
112 116
113our %RESOURCE; # unused 117#our %RESOURCE; # unused
114 118
115our $OUTPUT_RATE_MIN = 3000; 119our $OUTPUT_RATE_MIN = 3000;
116our $OUTPUT_RATE_MAX = 1000000; 120our $OUTPUT_RATE_MAX = 1000000;
117 121
118our $MAX_LINKS = 32; # how many chained exits to follow 122our $MAX_LINKS = 32; # how many chained exits to follow
129our $DB_ENV; 133our $DB_ENV;
130 134
131our @EXTRA_MODULES = qw(pod match mapscript incloader); 135our @EXTRA_MODULES = qw(pod match mapscript incloader);
132 136
133our %CFG; 137our %CFG;
138our %EXT_CFG; # cfgkeyname => [var-ref, defaultvalue]
134 139
135our $UPTIME; $UPTIME ||= time; 140our $UPTIME; $UPTIME ||= time;
136our $RUNTIME = 0; 141our $RUNTIME = 0;
137our $SERVER_TICK = 0; 142our $SERVER_TICK = 0;
138our $NOW; 143our $NOW;
219=item $cf::RUNTIME 224=item $cf::RUNTIME
220 225
221The time this server has run, starts at 0 and is increased by $cf::TICK on 226The time this server has run, starts at 0 and is increased by $cf::TICK on
222every server tick. 227every server tick.
223 228
224=item $cf::CONFDIR $cf::DATADIR $cf::LIBDIR $cf::PODDIR 229=item $cf::CONFDIR $cf::DATADIR $cf::LIBDIR $cf::PODDIR
225$cf::MAPDIR $cf::LOCALDIR $cf::TMPDIR $cf::UNIQUEDIR 230$cf::MAPDIR $cf::LOCALDIR $cf::TMPDIR $cf::UNIQUEDIR
226$cf::PLAYERDIR $cf::RANDOMDIR $cf::BDBDIR 231$cf::PLAYERDIR $cf::RANDOMDIR $cf::BDBDIR
227 232
228Various directories - "/etc", read-only install directory, perl-library 233Various directories - "/etc", read-only install directory, perl-library
229directory, pod-directory, read-only maps directory, "/var", "/var/tmp", 234directory, pod-directory, read-only maps directory, "/var", "/var/tmp",
230unique-items directory, player file directory, random maps directory and 235unique-items directory, player file directory, random maps directory and
231database environment. 236database environment.
335)) { 340)) {
336 @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; 341 @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg;
337} 342}
338 343
339$EV::DIED = sub { 344$EV::DIED = sub {
340 Carp::cluck "error in event callback: @_"; 345 warn "error in event callback: $@";
341}; 346};
342 347
343############################################################################# 348#############################################################################
344 349
345sub fork_call(&@); 350sub fork_call(&@);
372 $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; 377 $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge;
373 $d 378 $d
374 } || "[unable to dump $_[0]: '$@']"; 379 } || "[unable to dump $_[0]: '$@']";
375} 380}
376 381
377=item $scalar = load_file $path 382=item $scalar = cf::load_file $path
378 383
379Loads the given file from path and returns its contents. Croaks on error 384Loads the given file from path and returns its contents. Croaks on error
380and can block. 385and can block.
381 386
382=cut 387=cut
384sub load_file($) { 389sub load_file($) {
385 0 <= aio_load $_[0], my $data 390 0 <= aio_load $_[0], my $data
386 or Carp::croak "$_[0]: $!"; 391 or Carp::croak "$_[0]: $!";
387 392
388 $data 393 $data
394}
395
396=item $success = cf::replace_file $path, $data, $sync
397
398Atomically replaces the file at the given $path with new $data, and
399optionally $sync the data to disk before replacing the file.
400
401=cut
402
403sub replace_file($$;$) {
404 my ($path, $data, $sync) = @_;
405
406 my $lock = cf::lock_acquire ("replace_file:$path");
407
408 my $fh = aio_open "$path~", Fcntl::O_WRONLY | Fcntl::O_CREAT | Fcntl::O_TRUNC, 0644
409 or return;
410
411 $data = $data->() if ref $data;
412
413 length $data == aio_write $fh, 0, (length $data), $data, 0
414 or return;
415
416 !$sync
417 or !aio_fsync $fh
418 or return;
419
420 aio_close $fh
421 and return;
422
423 aio_rename "$path~", $path
424 and return;
425
426 if ($sync) {
427 $path =~ s%/[^/]*$%%;
428 aio_pathsync $path;
429 }
430
431 1
389} 432}
390 433
391=item $ref = cf::decode_json $json 434=item $ref = cf::decode_json $json
392 435
393Converts a JSON string into the corresponding perl data structure. 436Converts a JSON string into the corresponding perl data structure.
1388=cut 1431=cut
1389 1432
1390############################################################################# 1433#############################################################################
1391# command handling &c 1434# command handling &c
1392 1435
1393=item cf::register_command $name => \&callback($ob,$args); 1436=item cf::register_command $name => \&callback($ob,$args)
1394 1437
1395Register a callback for execution when the client sends the user command 1438Register a callback for execution when the client sends the user command
1396$name. 1439$name.
1397 1440
1398=cut 1441=cut
1404 #warn "registering command '$name/$time' to '$caller'"; 1447 #warn "registering command '$name/$time' to '$caller'";
1405 1448
1406 push @{ $COMMAND{$name} }, [$caller, $cb]; 1449 push @{ $COMMAND{$name} }, [$caller, $cb];
1407} 1450}
1408 1451
1409=item cf::register_extcmd $name => \&callback($pl,$packet); 1452=item cf::register_extcmd $name => \&callback($pl,$packet)
1410 1453
1411Register a callback for execution when the client sends an (synchronous) 1454Register a callback for execution when the client sends an (synchronous)
1412extcmd packet. Ext commands will be processed in the order they are 1455extcmd packet. Ext commands will be processed in the order they are
1413received by the server, like other user commands. The first argument is 1456received by the server, like other user commands. The first argument is
1414the logged-in player. Ext commands can only be processed after a player 1457the logged-in player. Ext commands can only be processed after a player
1415has logged in successfully. 1458has logged in successfully.
1416 1459
1417If the callback returns something, it is sent back as if reply was being 1460If the callback returns something, it is sent back as if reply was being
1418called. 1461called.
1419 1462
1420=item cf::register_exticmd $name => \&callback($ns,$packet); 1463=item cf::register_exticmd $name => \&callback($ns,$packet)
1421 1464
1422Register a callback for execution when the client sends an (asynchronous) 1465Register a callback for execution when the client sends an (asynchronous)
1423exticmd packet. Exti commands are processed by the server as soon as they 1466exticmd packet. Exti commands are processed by the server as soon as they
1424are received, i.e. out of order w.r.t. other commands. The first argument 1467are received, i.e. out of order w.r.t. other commands. The first argument
1425is a client socket. Exti commands can be received anytime, even before 1468is a client socket. Exti commands can be received anytime, even before
1461 my ($pl, $buf) = @_; 1504 my ($pl, $buf) = @_;
1462 1505
1463 my $msg = eval { $pl->ns->{json_coder}->decode ($buf) }; 1506 my $msg = eval { $pl->ns->{json_coder}->decode ($buf) };
1464 1507
1465 if (ref $msg) { 1508 if (ref $msg) {
1466 my ($type, $reply, @payload) = 1509 my ($type, $reply, @payload) = @$msg; # version 1 used %type, $id, %$hash
1467 "ARRAY" eq ref $msg
1468 ? @$msg
1469 : ($msg->{msgtype}, $msg->{msgid}, %$msg); # TODO: version 1, remove
1470 1510
1471 my @reply; 1511 my @reply;
1472 1512
1473 if (my $cb = $EXTCMD{$type}) { 1513 if (my $cb = $EXTCMD{$type}) {
1474 @reply = $cb->($pl, @payload); 1514 @reply = $cb->($pl, @payload);
1475 } 1515 }
1476 1516
1477 $pl->ext_reply ($reply, @reply) 1517 $pl->ext_msg ("reply-$reply", @reply)
1478 if $reply; 1518 if $reply;
1479 1519
1480 } else { 1520 } else {
1481 error "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; 1521 error "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n";
1482 } 1522 }
1496 }; 1536 };
1497 1537
1498 $grp 1538 $grp
1499} 1539}
1500 1540
1541sub _ext_cfg_reg($$$$) {
1542 my ($rvar, $varname, $cfgname, $default) = @_;
1543
1544 $cfgname = lc $varname
1545 unless length $cfgname;
1546
1547 $EXT_CFG{$cfgname} = [$rvar, $default];
1548
1549 $$rvar = exists $CFG{$cfgname} ? $CFG{$cfgname} : $default;
1550}
1551
1501sub load_extensions { 1552sub load_extensions {
1502 info "loading extensions..."; 1553 info "loading extensions...";
1554
1555 %EXT_CFG = ();
1503 1556
1504 cf::sync_job { 1557 cf::sync_job {
1505 my %todo; 1558 my %todo;
1506 1559
1507 for my $path (<$LIBDIR/*.ext>) { 1560 for my $path (<$LIBDIR/*.ext>) {
1550 unless exists $done{$_}; 1603 unless exists $done{$_};
1551 } 1604 }
1552 1605
1553 trace "... pass $pass, loading '$k' into '$v->{pkg}'\n"; 1606 trace "... pass $pass, loading '$k' into '$v->{pkg}'\n";
1554 1607
1608 my $source = $v->{source};
1609
1610 # support "CONF varname :confname = default" pseudo-statements
1611 $source =~ s{
1612 ^ CONF \s+ ([^\s:=]+) \s* (?:: \s* ([^\s:=]+) \s* )? = ([^\n#]+)
1613 }{
1614 "our \$$1; BEGIN { cf::_ext_cfg_reg \\\$$1, q\x00$1\x00, q\x00$2\x00, $3 }";
1615 }gmxe;
1616
1555 my $active = eval $v->{source}; 1617 my $active = eval $source;
1556 1618
1557 if (length $@) { 1619 if (length $@) {
1558 error "$v->{path}: $@\n"; 1620 error "$v->{path}: $@\n";
1559 1621
1560 cf::cleanup "mandatory extension '$k' failed to load, exiting." 1622 cf::cleanup "mandatory extension '$k' failed to load, exiting."
1847} 1909}
1848 1910
1849=item $protocol_xml = $player->expand_cfpod ($cfpod) 1911=item $protocol_xml = $player->expand_cfpod ($cfpod)
1850 1912
1851Expand deliantra pod fragments into protocol xml. 1913Expand deliantra pod fragments into protocol xml.
1852
1853=item $player->ext_reply ($msgid, @msg)
1854
1855Sends an ext reply to the player.
1856
1857=cut
1858
1859sub ext_reply($$@) {
1860 my ($self, $id, @msg) = @_;
1861
1862 $self->ns->ext_reply ($id, @msg)
1863}
1864 1914
1865=item $player->ext_msg ($type, @msg) 1915=item $player->ext_msg ($type, @msg)
1866 1916
1867Sends an ext event to the client. 1917Sends an ext event to the client.
1868 1918
2239 2289
2240 unless ($self->{deny_activate}) { 2290 unless ($self->{deny_activate}) {
2241 $self->decay_objects; 2291 $self->decay_objects;
2242 $self->fix_auto_apply; 2292 $self->fix_auto_apply;
2243 $self->update_buttons; 2293 $self->update_buttons;
2244 $self->post_load_physics;
2245 cf::cede_to_tick; 2294 cf::cede_to_tick;
2246 #$self->activate; # no longer activate maps automatically 2295 #$self->activate; # no longer activate maps automatically
2247 } 2296 }
2248 2297
2249 $self->{last_save} = $cf::RUNTIME; 2298 $self->{last_save} = $cf::RUNTIME;
2268# if $self->per_party; 2317# if $self->per_party;
2269 2318
2270 $self 2319 $self
2271} 2320}
2272 2321
2273# find and load all maps in the 3x3 area around a map
2274sub load_neighbours {
2275 my ($map) = @_;
2276
2277 my @neigh; # diagonal neighbours
2278
2279 for (0 .. 3) {
2280 my $neigh = $map->tile_path ($_)
2281 or next;
2282 $neigh = find $neigh, $map
2283 or next;
2284 $neigh->load;
2285
2286 # now find the diagonal neighbours
2287 push @neigh,
2288 [$neigh->tile_path (($_ + 3) % 4), $neigh],
2289 [$neigh->tile_path (($_ + 1) % 4), $neigh];
2290 }
2291
2292 for (grep defined $_->[0], @neigh) {
2293 my ($path, $origin) = @$_;
2294 my $neigh = find $path, $origin
2295 or next;
2296 $neigh->load;
2297 }
2298}
2299
2300sub find_sync { 2322sub find_sync {
2301 my ($path, $origin) = @_; 2323 my ($path, $origin) = @_;
2302 2324
2303 # it's a bug to call this from the main context 2325 # it's a bug to call this from the main context
2304 return cf::LOG cf::llevError | cf::logBacktrace, "do_find_sync" 2326 return cf::LOG cf::llevError | cf::logBacktrace, "do_find_sync"
2322 2344
2323sub find_async { 2345sub find_async {
2324 my ($path, $origin, $load) = @_; 2346 my ($path, $origin, $load) = @_;
2325 2347
2326 $path = normalise $path, $origin; 2348 $path = normalise $path, $origin;
2327 2349
2328 if (my $map = $cf::MAP{$path}) { 2350 if (my $map = $cf::MAP{$path}) {
2329 return $map if !$load || $map->linkable; 2351 return $map if !$load || $map->linkable;
2330 } 2352 }
2331 2353
2332 $MAP_PREFETCH{$path} |= $load; 2354 $MAP_PREFETCH{$path} |= $load;
2776 # use -1 or undef as default coordinates, not 0, 0 2798 # use -1 or undef as default coordinates, not 0, 0
2777 ($x, $y) = ($map->enter_x, $map->enter_y) 2799 ($x, $y) = ($map->enter_x, $map->enter_y)
2778 if $x <= 0 && $y <= 0; 2800 if $x <= 0 && $y <= 0;
2779 2801
2780 $map->load; 2802 $map->load;
2781 $map->load_neighbours;
2782 2803
2783 return unless $self->contr->active; 2804 return unless $self->contr->active;
2784 2805
2785 local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext 2806 local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext
2786 if ($self->enter_map ($map, $x, $y)) { 2807 if ($self->enter_map ($map, $x, $y)) {
3202=cut 3223=cut
3203 3224
3204sub cf::client::ext_msg($$@) { 3225sub cf::client::ext_msg($$@) {
3205 my ($self, $type, @msg) = @_; 3226 my ($self, $type, @msg) = @_;
3206 3227
3207 if ($self->extcmd == 2) {
3208 $self->send_big_packet ("ext " . $self->{json_coder}->encode ([$type, @msg])); 3228 $self->send_big_packet ("ext " . $self->{json_coder}->encode ([$type, @msg]));
3209 } elsif ($self->extcmd == 1) { # TODO: remove
3210 push @msg, msgtype => "event_$type";
3211 $self->send_big_packet ("ext " . $self->{json_coder}->encode ({@msg}));
3212 }
3213}
3214
3215=item $client->ext_reply ($msgid, @msg)
3216
3217Sends an ext reply to the client.
3218
3219=cut
3220
3221sub cf::client::ext_reply($$@) {
3222 my ($self, $id, @msg) = @_;
3223
3224 if ($self->extcmd == 2) {
3225 $self->send_big_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg]));
3226 } elsif ($self->extcmd == 1) {
3227 #TODO: version 1, remove
3228 unshift @msg, msgtype => "reply", msgid => $id;
3229 $self->send_big_packet ("ext " . $self->{json_coder}->encode ({@msg}));
3230 }
3231} 3229}
3232 3230
3233=item $success = $client->query ($flags, "text", \&cb) 3231=item $success = $client->query ($flags, "text", \&cb)
3234 3232
3235Queues a query to the client, calling the given callback with 3233Queues a query to the client, calling the given callback with
3290 my ($ns, $buf) = @_; 3288 my ($ns, $buf) = @_;
3291 3289
3292 my $msg = eval { $ns->{json_coder}->decode ($buf) }; 3290 my $msg = eval { $ns->{json_coder}->decode ($buf) };
3293 3291
3294 if (ref $msg) { 3292 if (ref $msg) {
3295 my ($type, $reply, @payload) = 3293 my ($type, $reply, @payload) = @$msg; # version 1 used %type, $id, %$hash
3296 "ARRAY" eq ref $msg
3297 ? @$msg
3298 : ($msg->{msgtype}, $msg->{msgid}, %$msg); # TODO: version 1, remove
3299 3294
3300 my @reply; 3295 my @reply;
3301 3296
3302 if (my $cb = $EXTICMD{$type}) { 3297 if (my $cb = $EXTICMD{$type}) {
3303 @reply = $cb->($ns, @payload); 3298 @reply = $cb->($ns, @payload);
3304 } 3299 }
3305 3300
3306 $ns->ext_reply ($reply, @reply) 3301 $ns->ext_msg ("reply-$reply", @reply)
3307 if $reply; 3302 if $reply;
3308 3303
3309 } else { 3304 } else {
3310 error "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; 3305 error "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n";
3311 } 3306 }
3481=cut 3476=cut
3482 3477
3483############################################################################# 3478#############################################################################
3484# the server's init and main functions 3479# the server's init and main functions
3485 3480
3481our %FACEHASH; # hash => idx, #d# HACK for http server
3482
3483# internal api, not fianlised
3484sub add_face {
3485 my ($name, $type, $data) = @_;
3486
3487 my $idx = cf::face::find $name;
3488
3489 if ($idx) {
3490 delete $FACEHASH{cf::face::get_chksum $idx};
3491 } else {
3492 $idx = cf::face::alloc $name;
3493 }
3494
3495 my $hash = cf::face::mangle_chksum Digest::MD5::md5 $data;
3496
3497 cf::face::set_type $idx, $type;
3498 cf::face::set_data $idx, 0, $data, $hash;
3499 cf::face::set_meta $idx, $type & 1 ? undef : undef;
3500 $FACEHASH{$hash} = $idx;#d#
3501
3502 $idx
3503}
3504
3486sub load_facedata($) { 3505sub load_facedata($) {
3487 my ($path) = @_; 3506 my ($path) = @_;
3488 3507
3489 # HACK to clear player env face cache, we need some signal framework 3508 # HACK to clear player env face cache, we need some signal framework
3490 # for this (global event?) 3509 # for this (global event?)
3497 my $facedata = decode_storable load_file $path; 3516 my $facedata = decode_storable load_file $path;
3498 3517
3499 $facedata->{version} == 2 3518 $facedata->{version} == 2
3500 or cf::cleanup "$path: version mismatch, cannot proceed."; 3519 or cf::cleanup "$path: version mismatch, cannot proceed.";
3501 3520
3502 # patch in the exptable
3503 my $exp_table = $enc->encode ([map cf::level_to_min_exp $_, 1 .. cf::settings->max_level]);
3504 $facedata->{resource}{"res/exp_table"} = {
3505 type => FT_RSRC,
3506 data => $exp_table,
3507 hash => (Digest::MD5::md5 $exp_table),
3508 };
3509 cf::cede_to_tick; 3521 cf::cede_to_tick;
3510 3522
3511 { 3523 {
3512 my $faces = $facedata->{faceinfo}; 3524 my $faces = $facedata->{faceinfo};
3513 3525
3514 while (my ($face, $info) = each %$faces) { 3526 for my $face (sort keys %$faces) {
3527 my $info = $faces->{$face};
3515 my $idx = (cf::face::find $face) || cf::face::alloc $face; 3528 my $idx = (cf::face::find $face) || cf::face::alloc $face;
3516 3529
3517 cf::face::set_visibility $idx, $info->{visibility}; 3530 cf::face::set_visibility $idx, $info->{visibility};
3518 cf::face::set_magicmap $idx, $info->{magicmap}; 3531 cf::face::set_magicmap $idx, $info->{magicmap};
3519 cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32}; 3532 cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32};
3520 cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64}; 3533 cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64};
3521 cf::face::set_data $idx, 2, $info->{glyph} , $info->{glyph} ; 3534 cf::face::set_data $idx, 2, $info->{glyph} , $info->{glyph} ;
3535 $FACEHASH{$info->{hash64}} = $idx;#d#
3522 3536
3523 cf::cede_to_tick; 3537 cf::cede_to_tick;
3524 } 3538 }
3525 3539
3526 while (my ($face, $info) = each %$faces) { 3540 while (my ($face, $info) = each %$faces) {
3553 3567
3554 { 3568 {
3555 my $res = $facedata->{resource}; 3569 my $res = $facedata->{resource};
3556 3570
3557 while (my ($name, $info) = each %$res) { 3571 while (my ($name, $info) = each %$res) {
3558 if (defined $info->{type}) { 3572 if (defined (my $type = $info->{type})) {
3573 # TODO: different hash - must free and use new index, or cache ixface data queue
3559 my $idx = (cf::face::find $name) || cf::face::alloc $name; 3574 my $idx = (cf::face::find $name) || cf::face::alloc $name;
3560 3575
3576 cf::face::set_type $idx, $type;
3561 cf::face::set_data $idx, 0, $info->{data}, $info->{hash}; 3577 cf::face::set_data $idx, 0, $info->{data}, $info->{hash};
3562 cf::face::set_type $idx, $info->{type}; 3578 cf::face::set_meta $idx, $type & 1 ? undef : $info->{meta}; # preserve meta unless prepended already
3579 $FACEHASH{$info->{hash}} = $idx;#d#
3563 } else { 3580 } else {
3564 $RESOURCE{$name} = $info; # unused 3581# $RESOURCE{$name} = $info; # unused
3565 } 3582 }
3566 3583
3567 cf::cede_to_tick; 3584 cf::cede_to_tick;
3568 } 3585 }
3569 } 3586 }
3587 my $status = load_resource_file_ $_[0]; 3604 my $status = load_resource_file_ $_[0];
3588 get_slot 0.1, 100; 3605 get_slot 0.1, 100;
3589 cf::arch::commit_load; 3606 cf::arch::commit_load;
3590 3607
3591 $status 3608 $status
3609}
3610
3611sub reload_exp_table {
3612 _reload_exp_table;
3613
3614 add_face "res/exp_table" => FT_RSRC,
3615 JSON::XS->new->utf8->canonical->encode (
3616 [map cf::level_to_min_exp $_, 1 .. cf::settings->max_level]
3617 );
3618}
3619
3620sub reload_materials {
3621 _reload_materials;
3592} 3622}
3593 3623
3594sub reload_regions { 3624sub reload_regions {
3595 # HACK to clear player env face cache, we need some signal framework 3625 # HACK to clear player env face cache, we need some signal framework
3596 # for this (global event?) 3626 # for this (global event?)
3611} 3641}
3612 3642
3613sub reload_archetypes { 3643sub reload_archetypes {
3614 load_resource_file "$DATADIR/archetypes" 3644 load_resource_file "$DATADIR/archetypes"
3615 or die "unable to load archetypes\n"; 3645 or die "unable to load archetypes\n";
3646
3647 add_face "res/skill_info" => FT_RSRC,
3648 JSON::XS->new->utf8->canonical->encode (
3649 [map [cf::arch::skillvec ($_)->name], 0 .. cf::arch::skillvec_size - 1]
3650 );
3651 add_face "res/spell_paths" => FT_RSRC,
3652 JSON::XS->new->utf8->canonical->encode (
3653 [map [cf::spellpathnames ($_)], 0 .. NRSPELLPATHS - 1]
3654 );
3616} 3655}
3617 3656
3618sub reload_treasures { 3657sub reload_treasures {
3619 load_resource_file "$DATADIR/treasures" 3658 load_resource_file "$DATADIR/treasures"
3620 or die "unable to load treasurelists\n"; 3659 or die "unable to load treasurelists\n";
3638 my $face = cf::face::find "sound/$v"; 3677 my $face = cf::face::find "sound/$v";
3639 cf::sound::set $k => $face; 3678 cf::sound::set $k => $face;
3640 } 3679 }
3641} 3680}
3642 3681
3682#d# move docstuff to help or so
3683our %DOCSTRING;
3684
3685sub reload_pod {
3686 trace "loading pods $PODDIR\n";
3687
3688 %DOCSTRING = ();
3689 my @command_list;
3690
3691 for (
3692 [0, "command_help"],
3693 [1, "emote_help"],
3694 [2, "dmcommand_help"],
3695 ) {
3696 my ($type, $path) = @$_;
3697
3698 my $paragraphs = &cf::pod::load_pod ("$PODDIR/$path.pod")
3699 or die "unable to load $path";
3700
3701 my $level = 1e9;
3702 my $rpar;
3703
3704 for my $par (@$paragraphs) {
3705 if ($par->{type} eq "head2") {
3706 # this code taken almost verbatim from DC/Protocol.pm
3707
3708 if ($par->{markup} =~ /^(\S+) (?:\s+ \( ([^\)]*) \) )?/x) {
3709 my $cmd = $1;
3710 my @args = split /\|/, $2;
3711 @args = (".*") unless @args;
3712
3713 $_ = $_ eq ".*" ? "" : " $_"
3714 for @args;
3715
3716 my @variants = map "$cmd$_", sort { (length $a) <=> (length $b) } @args;
3717
3718 $rpar = \($DOCSTRING{"command/$cmd"} = &cf::pod::as_cfpod ([$par]));
3719
3720 push @command_list, [$type, \@variants];
3721 $level = $par->{level};
3722 } else {
3723 error "$par->{markup}: unparsable command heading";
3724 }
3725 } elsif ($par->{level} > $level) {
3726 $$rpar .= &cf::pod::as_cfpod ([$par]);
3727 }
3728
3729 cf::cede_to_tick;
3730 }
3731 }
3732
3733 @command_list = sort {
3734 $a->[0] <=> $b->[0]
3735 or $a->[1] cmp $b->[1]
3736 } @command_list;
3737
3738 cf::cede_to_tick;
3739
3740 add_face "res/command_list" => FT_RSRC,
3741 JSON::XS->new->utf8->encode (\@command_list);
3742}
3743
3643sub reload_resources { 3744sub reload_resources {
3644 trace "reloading resource files...\n"; 3745 trace "reloading resource files...\n";
3645 3746
3646 reload_exp_table;
3647 reload_materials; 3747 reload_materials;
3648 reload_facedata; 3748 reload_facedata;
3749 reload_exp_table;
3649 reload_sound; 3750 reload_sound;
3650 reload_archetypes; 3751 reload_archetypes;
3651 reload_regions; 3752 reload_regions;
3652 reload_treasures; 3753 reload_treasures;
3754 reload_pod;
3653 3755
3654 trace "finished reloading resource files\n"; 3756 trace "finished reloading resource files\n";
3655} 3757}
3656 3758
3657sub reload_config { 3759sub reload_config {
3705 3807
3706sub main { 3808sub main {
3707 cf::init_globals; # initialise logging 3809 cf::init_globals; # initialise logging
3708 3810
3709 LOG llevInfo, "Welcome to Deliantra, v" . VERSION; 3811 LOG llevInfo, "Welcome to Deliantra, v" . VERSION;
3710 LOG llevInfo, "Copyright (C) 2005-2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; 3812 LOG llevInfo, "Copyright (C) 2005-2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team.";
3711 LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; 3813 LOG llevInfo, "Copyright (C) 1994 Mark Wedel.";
3712 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; 3814 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen.";
3713 3815
3714 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority 3816 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
3715 3817
3716 # we must not ever block the main coroutine 3818 # we must not ever block the main coroutine
3717 local $Coro::idle = sub { 3819 $Coro::idle = sub {
3718 Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# 3820 Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d#
3719 (async { 3821 (async {
3720 $Coro::current->{desc} = "IDLE BUG HANDLER"; 3822 $Coro::current->{desc} = "IDLE BUG HANDLER";
3721 EV::loop EV::LOOP_ONESHOT; 3823 EV::loop EV::LOOP_ONESHOT;
3722 })->prio (Coro::PRIO_MAX); 3824 })->prio (Coro::PRIO_MAX);
3725 evthread_start IO::AIO::poll_fileno; 3827 evthread_start IO::AIO::poll_fileno;
3726 3828
3727 cf::sync_job { 3829 cf::sync_job {
3728 cf::incloader::init (); 3830 cf::incloader::init ();
3729 3831
3832 db_init;
3833
3730 cf::init_anim; 3834 cf::init_anim;
3731 cf::init_attackmess; 3835 cf::init_attackmess;
3732 cf::init_dynamic; 3836 cf::init_dynamic;
3733 3837
3734 cf::load_settings; 3838 cf::load_settings;
3735 3839
3736 reload_resources; 3840 reload_resources;
3737 reload_config; 3841 reload_config;
3738 db_init;
3739 3842
3740 cf::init_uuid; 3843 cf::init_uuid;
3741 cf::init_signals; 3844 cf::init_signals;
3742 cf::init_skills; 3845 cf::init_skills;
3743 3846
3757 }; 3860 };
3758 3861
3759 cf::object::thawer::errors_are_fatal 0; 3862 cf::object::thawer::errors_are_fatal 0;
3760 info "parse errors in files are no longer fatal from this point on.\n"; 3863 info "parse errors in files are no longer fatal from this point on.\n";
3761 3864
3762 my $free_main; $free_main = EV::idle sub { 3865 AE::postpone {
3763 undef $free_main;
3764 undef &main; # free gobs of memory :) 3866 undef &main; # free gobs of memory :)
3765 }; 3867 };
3766 3868
3767 goto &main_loop; 3869 goto &main_loop;
3768} 3870}
3925 4027
3926 cf::write_runtime_sync; # external watchdog should not bark 4028 cf::write_runtime_sync; # external watchdog should not bark
3927 4029
3928 trace "emergency_perl_save: flushing outstanding aio requests"; 4030 trace "emergency_perl_save: flushing outstanding aio requests";
3929 while (IO::AIO::nreqs || BDB::nreqs) { 4031 while (IO::AIO::nreqs || BDB::nreqs) {
3930 Coro::EV::timer_once 0.01; # let the sync_job do it's thing 4032 Coro::AnyEvent::sleep 0.01; # let the sync_job do it's thing
3931 } 4033 }
3932 4034
3933 cf::write_runtime_sync; # external watchdog should not bark 4035 cf::write_runtime_sync; # external watchdog should not bark
3934 }; 4036 };
3935 4037
4169 } 4271 }
4170} 4272}
4171 4273
4172{ 4274{
4173 # configure BDB 4275 # configure BDB
4276 info "initialising database";
4174 4277
4175 BDB::min_parallel 16; 4278 BDB::min_parallel 16;
4176 BDB::max_poll_reqs $TICK * 0.1; 4279 BDB::max_poll_reqs $TICK * 0.1;
4177 $AnyEvent::BDB::WATCHER->priority (1); 4280 #$AnyEvent::BDB::WATCHER->priority (1);
4178 4281
4179 unless ($DB_ENV) { 4282 unless ($DB_ENV) {
4180 $DB_ENV = BDB::db_env_create; 4283 $DB_ENV = BDB::db_env_create;
4181 $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT); 4284 $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT);
4182 $DB_ENV->set_flags (&BDB::LOG_AUTOREMOVE ) if BDB::VERSION v0, v4.7; 4285 $DB_ENV->set_flags (&BDB::LOG_AUTOREMOVE ) if BDB::VERSION v0, v4.7;
4207 BDB::db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; 4310 BDB::db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { };
4208 }; 4311 };
4209 $BDB_TRICKLE_WATCHER = EV::periodic 0, 10, 0, sub { 4312 $BDB_TRICKLE_WATCHER = EV::periodic 0, 10, 0, sub {
4210 BDB::db_env_memp_trickle $DB_ENV, 20, 0, sub { }; 4313 BDB::db_env_memp_trickle $DB_ENV, 20, 0, sub { };
4211 }; 4314 };
4315
4316 info "database initialised";
4212} 4317}
4213 4318
4214{ 4319{
4215 # configure IO::AIO 4320 # configure IO::AIO
4216 4321
4322 info "initialising aio";
4217 IO::AIO::min_parallel 8; 4323 IO::AIO::min_parallel 8;
4218 IO::AIO::max_poll_time $TICK * 0.1; 4324 IO::AIO::max_poll_time $TICK * 0.1;
4219 undef $AnyEvent::AIO::WATCHER; 4325 undef $AnyEvent::AIO::WATCHER;
4326 info "aio initialised";
4220} 4327}
4221 4328
4222our $_log_backtrace; 4329our $_log_backtrace;
4223our $_log_backtrace_last; 4330our $_log_backtrace_last;
4224 4331

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines