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 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | # Copyright (©) 2006,2007,2008,2009,2010,2011 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. |
… | |
… | |
93 | our @EVENT; |
93 | our @EVENT; |
94 | our @REFLECT; # set by XS |
94 | our @REFLECT; # set by XS |
95 | our %REFLECT; # set by us |
95 | our %REFLECT; # set by us |
96 | |
96 | |
97 | our $CONFDIR = confdir; |
97 | our $CONFDIR = confdir; |
|
|
98 | |
98 | our $DATADIR = datadir; |
99 | our $DATADIR = datadir; |
99 | our $LIBDIR = "$DATADIR/ext"; |
100 | our $LIBDIR = "$DATADIR/ext"; |
100 | our $PODDIR = "$DATADIR/pod"; |
101 | our $PODDIR = "$DATADIR/pod"; |
101 | our $MAPDIR = "$DATADIR/" . mapdir; |
102 | our $MAPDIR = "$DATADIR/" . mapdir; |
|
|
103 | |
102 | our $LOCALDIR = localdir; |
104 | our $LOCALDIR = localdir; |
103 | our $TMPDIR = "$LOCALDIR/" . tmpdir; |
105 | our $TMPDIR = "$LOCALDIR/" . tmpdir; |
104 | our $UNIQUEDIR = "$LOCALDIR/" . uniquedir; |
106 | our $UNIQUEDIR = "$LOCALDIR/" . uniquedir; |
105 | our $PLAYERDIR = "$LOCALDIR/" . playerdir; |
107 | our $PLAYERDIR = "$LOCALDIR/" . playerdir; |
106 | our $RANDOMDIR = "$LOCALDIR/random"; |
108 | our $RANDOMDIR = "$LOCALDIR/random"; |
… | |
… | |
312 | } |
314 | } |
313 | |
315 | |
314 | $EV::DIED = sub { |
316 | $EV::DIED = sub { |
315 | Carp::cluck "error in event callback: @_"; |
317 | Carp::cluck "error in event callback: @_"; |
316 | }; |
318 | }; |
|
|
319 | |
|
|
320 | ############################################################################# |
|
|
321 | |
|
|
322 | sub fork_call(&@); |
|
|
323 | sub get_slot($;$$); |
317 | |
324 | |
318 | ############################################################################# |
325 | ############################################################################# |
319 | |
326 | |
320 | =head2 UTILITY FUNCTIONS |
327 | =head2 UTILITY FUNCTIONS |
321 | |
328 | |
… | |
… | |
342 | $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; |
349 | $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; |
343 | $d |
350 | $d |
344 | } || "[unable to dump $_[0]: '$@']"; |
351 | } || "[unable to dump $_[0]: '$@']"; |
345 | } |
352 | } |
346 | |
353 | |
|
|
354 | =item $scalar = load_file $path |
|
|
355 | |
|
|
356 | Loads the given file from path and returns its contents. Croaks on error |
|
|
357 | and can block. |
|
|
358 | |
|
|
359 | =cut |
|
|
360 | |
|
|
361 | sub load_file($) { |
|
|
362 | 0 <= aio_load $_[0], my $data |
|
|
363 | or Carp::croak "$_[0]: $!"; |
|
|
364 | |
|
|
365 | $data |
|
|
366 | } |
|
|
367 | |
347 | =item $ref = cf::decode_json $json |
368 | =item $ref = cf::decode_json $json |
348 | |
369 | |
349 | Converts a JSON string into the corresponding perl data structure. |
370 | Converts a JSON string into the corresponding perl data structure. |
350 | |
371 | |
351 | =item $json = cf::encode_json $ref |
372 | =item $json = cf::encode_json $ref |
… | |
… | |
357 | our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max |
378 | our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max |
358 | |
379 | |
359 | sub encode_json($) { $json_coder->encode ($_[0]) } |
380 | sub encode_json($) { $json_coder->encode ($_[0]) } |
360 | sub decode_json($) { $json_coder->decode ($_[0]) } |
381 | sub decode_json($) { $json_coder->decode ($_[0]) } |
361 | |
382 | |
|
|
383 | =item $ref = cf::decode_storable $scalar |
|
|
384 | |
|
|
385 | Same as Coro::Storable::thaw, so blocks. |
|
|
386 | |
|
|
387 | =cut |
|
|
388 | |
|
|
389 | BEGIN { *decode_storable = \&Coro::Storable::thaw } |
|
|
390 | |
362 | =item $ref = cf::yaml_load $scalar |
391 | =item $ref = cf::decode_yaml $scalar |
363 | |
392 | |
364 | Same as YAML::XS::Load, but doesn't leak, because it forks (and thus blocks). |
393 | Same as YAML::XS::Load, but doesn't leak, because it forks (and thus blocks). |
365 | |
394 | |
366 | =cut |
395 | =cut |
367 | |
396 | |
368 | sub fork_call(&@); |
397 | sub decode_yaml($) { |
369 | |
|
|
370 | sub yaml_load($) { |
|
|
371 | fork_call { YAML::XS::Load $_[0] } @_ |
398 | fork_call { YAML::XS::Load $_[0] } @_ |
|
|
399 | } |
|
|
400 | |
|
|
401 | =item $scalar = cf::unlzf $scalar |
|
|
402 | |
|
|
403 | Same as Compress::LZF::compress, but takes server ticks into account, so |
|
|
404 | blocks. |
|
|
405 | |
|
|
406 | =cut |
|
|
407 | |
|
|
408 | sub unlzf($) { |
|
|
409 | # we assume 100mb/s minimum decompression speed (noncompressible data on a ~2ghz machine) |
|
|
410 | cf::get_slot +(length $_[0]) / 100_000_000, 0, "unlzf"; |
|
|
411 | Compress::LZF::decompress $_[0] |
372 | } |
412 | } |
373 | |
413 | |
374 | =item cf::post_init { BLOCK } |
414 | =item cf::post_init { BLOCK } |
375 | |
415 | |
376 | Execute the given codeblock, I<after> all extensions have been (re-)loaded, |
416 | Execute the given codeblock, I<after> all extensions have been (re-)loaded, |
… | |
… | |
460 | EV::periodic $start, $interval, 0, $cb |
500 | EV::periodic $start, $interval, 0, $cb |
461 | } |
501 | } |
462 | |
502 | |
463 | =item cf::get_slot $time[, $priority[, $name]] |
503 | =item cf::get_slot $time[, $priority[, $name]] |
464 | |
504 | |
465 | Allocate $time seconds of blocking CPU time at priority C<$priority>: |
505 | Allocate $time seconds of blocking CPU time at priority C<$priority> |
466 | This call blocks and returns only when you have at least C<$time> seconds |
506 | (default: 0): This call blocks and returns only when you have at least |
467 | of cpu time till the next tick. The slot is only valid till the next cede. |
507 | C<$time> seconds of cpu time till the next tick. The slot is only valid |
|
|
508 | till the next cede. |
|
|
509 | |
|
|
510 | Background jobs should use a priority les than zero, interactive jobs |
|
|
511 | should use 100 or more. |
468 | |
512 | |
469 | The optional C<$name> can be used to identify the job to run. It might be |
513 | The optional C<$name> can be used to identify the job to run. It might be |
470 | used for statistical purposes and should identify the same time-class. |
514 | used for statistical purposes and should identify the same time-class. |
471 | |
515 | |
472 | Useful for short background jobs. |
516 | Useful for short background jobs. |
… | |
… | |
767 | |
811 | |
768 | my @data; |
812 | my @data; |
769 | my $md5; |
813 | my $md5; |
770 | |
814 | |
771 | for (0 .. $#$src) { |
815 | for (0 .. $#$src) { |
772 | 0 <= aio_load $src->[$_], $data[$_] |
816 | $data[$_] = load_file $src->[$_]; |
773 | or Carp::croak "$src->[$_]: $!"; |
|
|
774 | } |
817 | } |
775 | |
818 | |
776 | # if processing is expensive, check |
819 | # if processing is expensive, check |
777 | # checksum first |
820 | # checksum first |
778 | if (1) { |
821 | if (1) { |
… | |
… | |
1512 | |
1555 | |
1513 | while (my ($k, $v) = each %todo) { |
1556 | while (my ($k, $v) = each %todo) { |
1514 | cf::cleanup "mandatory extension '$k' has unresolved dependencies, exiting." |
1557 | cf::cleanup "mandatory extension '$k' has unresolved dependencies, exiting." |
1515 | if exists $v->{meta}{mandatory}; |
1558 | if exists $v->{meta}{mandatory}; |
1516 | } |
1559 | } |
|
|
1560 | |
|
|
1561 | last; |
1517 | } |
1562 | } |
1518 | } |
1563 | } |
1519 | }; |
1564 | }; |
1520 | } |
1565 | } |
1521 | |
1566 | |
… | |
… | |
1949 | |
1994 | |
1950 | Carp::cluck "unable to resolve path '$path' (base '$base')"; |
1995 | Carp::cluck "unable to resolve path '$path' (base '$base')"; |
1951 | () |
1996 | () |
1952 | } |
1997 | } |
1953 | |
1998 | |
|
|
1999 | # may re-bless or do other evil things |
1954 | sub init { |
2000 | sub init { |
1955 | my ($self) = @_; |
2001 | my ($self) = @_; |
1956 | |
2002 | |
1957 | $self |
2003 | $self |
1958 | } |
2004 | } |
… | |
… | |
2130 | $self->alloc; |
2176 | $self->alloc; |
2131 | |
2177 | |
2132 | $self->pre_load; |
2178 | $self->pre_load; |
2133 | cf::cede_to_tick; |
2179 | cf::cede_to_tick; |
2134 | |
2180 | |
|
|
2181 | if (exists $self->{load_path}) { |
2135 | my $f = new_from_file cf::object::thawer $self->{load_path}; |
2182 | my $f = new_from_file cf::object::thawer $self->{load_path}; |
2136 | $f->skip_block; |
2183 | $f->skip_block; |
2137 | $self->_load_objects ($f) |
2184 | $self->_load_objects ($f) |
2138 | or return; |
2185 | or return; |
2139 | |
2186 | |
2140 | $self->post_load_original |
2187 | $self->post_load_original |
2141 | if delete $self->{load_original}; |
2188 | if delete $self->{load_original}; |
2142 | |
2189 | |
2143 | if (my $uniq = $self->uniq_path) { |
2190 | if (my $uniq = $self->uniq_path) { |
2144 | utf8::encode $uniq; |
2191 | utf8::encode $uniq; |
2145 | unless (aio_stat $uniq) { |
2192 | unless (aio_stat $uniq) { |
2146 | if (my $f = new_from_file cf::object::thawer $uniq) { |
2193 | if (my $f = new_from_file cf::object::thawer $uniq) { |
2147 | $self->clear_unique_items; |
2194 | $self->clear_unique_items; |
2148 | $self->_load_objects ($f); |
2195 | $self->_load_objects ($f); |
2149 | $f->resolve_delayed_derefs; |
2196 | $f->resolve_delayed_derefs; |
|
|
2197 | } |
2150 | } |
2198 | } |
2151 | } |
2199 | } |
2152 | } |
|
|
2153 | |
2200 | |
2154 | $f->resolve_delayed_derefs; |
2201 | $f->resolve_delayed_derefs; |
|
|
2202 | } |
2155 | |
2203 | |
2156 | cf::cede_to_tick; |
2204 | cf::cede_to_tick; |
2157 | # now do the right thing for maps |
2205 | # now do the right thing for maps |
2158 | $self->link_multipart_objects; |
2206 | $self->link_multipart_objects; |
2159 | $self->difficulty ($self->estimate_difficulty) |
2207 | $self->difficulty ($self->estimate_difficulty) |
… | |
… | |
3399 | |
3447 | |
3400 | my $enc = JSON::XS->new->utf8->canonical->relaxed; |
3448 | my $enc = JSON::XS->new->utf8->canonical->relaxed; |
3401 | |
3449 | |
3402 | trace "loading facedata from $path\n"; |
3450 | trace "loading facedata from $path\n"; |
3403 | |
3451 | |
3404 | 0 < aio_load $path, my $facedata |
3452 | my $facedata = decode_storable load_file $path; |
3405 | or die "$path: $!"; |
|
|
3406 | |
|
|
3407 | $facedata = Coro::Storable::thaw $facedata; |
|
|
3408 | |
3453 | |
3409 | $facedata->{version} == 2 |
3454 | $facedata->{version} == 2 |
3410 | or cf::cleanup "$path: version mismatch, cannot proceed."; |
3455 | or cf::cleanup "$path: version mismatch, cannot proceed."; |
3411 | |
3456 | |
3412 | # patch in the exptable |
3457 | # patch in the exptable |
… | |
… | |
3531 | } |
3576 | } |
3532 | |
3577 | |
3533 | sub reload_sound { |
3578 | sub reload_sound { |
3534 | trace "loading sound config from $DATADIR/sound\n"; |
3579 | trace "loading sound config from $DATADIR/sound\n"; |
3535 | |
3580 | |
3536 | 0 < Coro::AIO::aio_load "$DATADIR/sound", my $data |
|
|
3537 | or die "$DATADIR/sound $!"; |
|
|
3538 | |
|
|
3539 | my $soundconf = JSON::XS->new->utf8->relaxed->decode ($data); |
3581 | my $soundconf = JSON::XS->new->utf8->relaxed->decode (load_file "$DATADIR/sound"); |
3540 | |
3582 | |
3541 | for (0 .. SOUND_CAST_SPELL_0 - 1) { |
3583 | for (0 .. SOUND_CAST_SPELL_0 - 1) { |
3542 | my $sound = $soundconf->{compat}[$_] |
3584 | my $sound = $soundconf->{compat}[$_] |
3543 | or next; |
3585 | or next; |
3544 | |
3586 | |
… | |
… | |
3568 | } |
3610 | } |
3569 | |
3611 | |
3570 | sub reload_config { |
3612 | sub reload_config { |
3571 | trace "reloading config file...\n"; |
3613 | trace "reloading config file...\n"; |
3572 | |
3614 | |
3573 | 0 < aio_load "$CONFDIR/config", my $config |
3615 | my $config = load_file "$CONFDIR/config"; |
3574 | or die "$CONFDIR/config: $!"; |
|
|
3575 | |
|
|
3576 | utf8::decode $config; |
3616 | utf8::decode $config; |
3577 | *CFG = yaml_load $config; |
3617 | *CFG = decode_yaml $config; |
3578 | |
3618 | |
3579 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; |
3619 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; |
3580 | |
3620 | |
3581 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3621 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3582 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
3622 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
… | |
… | |
3620 | |
3660 | |
3621 | sub main { |
3661 | sub main { |
3622 | cf::init_globals; # initialise logging |
3662 | cf::init_globals; # initialise logging |
3623 | |
3663 | |
3624 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3664 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3625 | LOG llevInfo, "Copyright (C) 2005-2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3665 | LOG llevInfo, "Copyright (C) 2005-2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3626 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3666 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3627 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3667 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3628 | |
3668 | |
3629 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3669 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3630 | |
3670 | |