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.555 by root, Wed Jun 30 01:32:56 2010 UTC vs.
Revision 1.561 by root, Fri Apr 22 06:10:33 2011 UTC

93our @EVENT; 93our @EVENT;
94our @REFLECT; # set by XS 94our @REFLECT; # set by XS
95our %REFLECT; # set by us 95our %REFLECT; # set by us
96 96
97our $CONFDIR = confdir; 97our $CONFDIR = confdir;
98
98our $DATADIR = datadir; 99our $DATADIR = datadir;
99our $LIBDIR = "$DATADIR/ext"; 100our $LIBDIR = "$DATADIR/ext";
100our $PODDIR = "$DATADIR/pod"; 101our $PODDIR = "$DATADIR/pod";
101our $MAPDIR = "$DATADIR/" . mapdir; 102our $MAPDIR = "$DATADIR/" . mapdir;
103
102our $LOCALDIR = localdir; 104our $LOCALDIR = localdir;
103our $TMPDIR = "$LOCALDIR/" . tmpdir; 105our $TMPDIR = "$LOCALDIR/" . tmpdir;
104our $UNIQUEDIR = "$LOCALDIR/" . uniquedir; 106our $UNIQUEDIR = "$LOCALDIR/" . uniquedir;
105our $PLAYERDIR = "$LOCALDIR/" . playerdir; 107our $PLAYERDIR = "$LOCALDIR/" . playerdir;
106our $RANDOMDIR = "$LOCALDIR/random"; 108our $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
322sub fork_call(&@);
323sub 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
356Loads the given file from path and returns its contents. Croaks on error
357and can block.
358
359=cut
360
361sub 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
349Converts a JSON string into the corresponding perl data structure. 370Converts 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
357our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max 378our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max
358 379
359sub encode_json($) { $json_coder->encode ($_[0]) } 380sub encode_json($) { $json_coder->encode ($_[0]) }
360sub decode_json($) { $json_coder->decode ($_[0]) } 381sub decode_json($) { $json_coder->decode ($_[0]) }
361 382
383=item $ref = cf::decode_storable $scalar
384
385Same as Coro::Storable::thaw, so blocks.
386
387=cut
388
389BEGIN { *decode_storable = \&Coro::Storable::thaw }
390
362=item $ref = cf::yaml_load $scalar 391=item $ref = cf::decode_yaml $scalar
363 392
364Same as YAML::XS::Load, but doesn't leak, because it forks (and thus blocks). 393Same as YAML::XS::Load, but doesn't leak, because it forks (and thus blocks).
365 394
366=cut 395=cut
367 396
368sub fork_call(&@); 397sub decode_yaml($) {
369
370sub 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
403Same as Compress::LZF::compress, but takes server ticks into account, so
404blocks.
405
406=cut
407
408sub 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
376Execute the given codeblock, I<after> all extensions have been (re-)loaded, 416Execute 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
465Allocate $time seconds of blocking CPU time at priority C<$priority>: 505Allocate $time seconds of blocking CPU time at priority C<$priority>
466This 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
467of cpu time till the next tick. The slot is only valid till the next cede. 507C<$time> seconds of cpu time till the next tick. The slot is only valid
508till the next cede.
509
510Background jobs should use a priority les than zero, interactive jobs
511should use 100 or more.
468 512
469The optional C<$name> can be used to identify the job to run. It might be 513The optional C<$name> can be used to identify the job to run. It might be
470used for statistical purposes and should identify the same time-class. 514used for statistical purposes and should identify the same time-class.
471 515
472Useful for short background jobs. 516Useful 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
1861sub generate_random_map { 1906sub generate_random_map {
1862 my ($self, $rmp) = @_; 1907 my ($self, $rmp) = @_;
1863 1908
1864 my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM 1909 my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM
1865 1910
1866 # mit "rum" bekleckern, nicht
1867 $self->_create_random_map ( 1911 $self->_create_random_map ($rmp);
1868 $rmp, $rmp->{total_map_hp},
1869 (cf::region::find $rmp->{region})
1870 )
1871} 1912}
1872 1913
1873=item cf::map->register ($regex, $prio) 1914=item cf::map->register ($regex, $prio)
1874 1915
1875Register a handler for the map path matching the given regex at the 1916Register a handler for the map path matching the given regex at the
1953 1994
1954 Carp::cluck "unable to resolve path '$path' (base '$base')"; 1995 Carp::cluck "unable to resolve path '$path' (base '$base')";
1955 () 1996 ()
1956} 1997}
1957 1998
1999# may re-bless or do other evil things
1958sub init { 2000sub init {
1959 my ($self) = @_; 2001 my ($self) = @_;
1960 2002
1961 $self 2003 $self
1962} 2004}
3403 3445
3404 my $enc = JSON::XS->new->utf8->canonical->relaxed; 3446 my $enc = JSON::XS->new->utf8->canonical->relaxed;
3405 3447
3406 trace "loading facedata from $path\n"; 3448 trace "loading facedata from $path\n";
3407 3449
3408 0 < aio_load $path, my $facedata 3450 my $facedata = decode_storable load_file $path;
3409 or die "$path: $!";
3410
3411 $facedata = Coro::Storable::thaw $facedata;
3412 3451
3413 $facedata->{version} == 2 3452 $facedata->{version} == 2
3414 or cf::cleanup "$path: version mismatch, cannot proceed."; 3453 or cf::cleanup "$path: version mismatch, cannot proceed.";
3415 3454
3416 # patch in the exptable 3455 # patch in the exptable
3430 3469
3431 cf::face::set_visibility $idx, $info->{visibility}; 3470 cf::face::set_visibility $idx, $info->{visibility};
3432 cf::face::set_magicmap $idx, $info->{magicmap}; 3471 cf::face::set_magicmap $idx, $info->{magicmap};
3433 cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32}; 3472 cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32};
3434 cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64}; 3473 cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64};
3474 cf::face::set_data $idx, 2, $info->{glyph} , $info->{glyph} ;
3435 3475
3436 cf::cede_to_tick; 3476 cf::cede_to_tick;
3437 } 3477 }
3438 3478
3439 while (my ($face, $info) = each %$faces) { 3479 while (my ($face, $info) = each %$faces) {
3534} 3574}
3535 3575
3536sub reload_sound { 3576sub reload_sound {
3537 trace "loading sound config from $DATADIR/sound\n"; 3577 trace "loading sound config from $DATADIR/sound\n";
3538 3578
3539 0 < Coro::AIO::aio_load "$DATADIR/sound", my $data
3540 or die "$DATADIR/sound $!";
3541
3542 my $soundconf = JSON::XS->new->utf8->relaxed->decode ($data); 3579 my $soundconf = JSON::XS->new->utf8->relaxed->decode (load_file "$DATADIR/sound");
3543 3580
3544 for (0 .. SOUND_CAST_SPELL_0 - 1) { 3581 for (0 .. SOUND_CAST_SPELL_0 - 1) {
3545 my $sound = $soundconf->{compat}[$_] 3582 my $sound = $soundconf->{compat}[$_]
3546 or next; 3583 or next;
3547 3584
3571} 3608}
3572 3609
3573sub reload_config { 3610sub reload_config {
3574 trace "reloading config file...\n"; 3611 trace "reloading config file...\n";
3575 3612
3576 0 < aio_load "$CONFDIR/config", my $config 3613 my $config = load_file "$CONFDIR/config";
3577 or die "$CONFDIR/config: $!";
3578
3579 utf8::decode $config; 3614 utf8::decode $config;
3580 *CFG = yaml_load $config; 3615 *CFG = decode_yaml $config;
3581 3616
3582 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; 3617 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38];
3583 3618
3584 $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; 3619 $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset};
3585 $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; 3620 $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset};
3623 3658
3624sub main { 3659sub main {
3625 cf::init_globals; # initialise logging 3660 cf::init_globals; # initialise logging
3626 3661
3627 LOG llevInfo, "Welcome to Deliantra, v" . VERSION; 3662 LOG llevInfo, "Welcome to Deliantra, v" . VERSION;
3628 LOG llevInfo, "Copyright (C) 2005-2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; 3663 LOG llevInfo, "Copyright (C) 2005-2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team.";
3629 LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; 3664 LOG llevInfo, "Copyright (C) 1994 Mark Wedel.";
3630 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; 3665 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen.";
3631 3666
3632 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority 3667 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
3633 3668

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines