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.556 by root, Sat Jul 3 01:49:18 2010 UTC vs.
Revision 1.564 by elmex, Thu Apr 28 14:58:08 2011 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 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.
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
1771 for (@$files) { 1816 for (@$files) {
1772 utf8::decode $_; 1817 utf8::decode $_;
1773 next if /\.(?:pl|pst)$/; 1818 next if /\.(?:pl|pst)$/;
1774 next unless /^$PATH_SEP/o; 1819 next unless /^$PATH_SEP/o;
1775 1820
1776 push @paths, cf::map::normalise "~$pl/$_"; 1821 push @paths, cf::map::normalise "~$pl/$_", $pl->map;
1777 } 1822 }
1778 1823
1779 \@paths 1824 \@paths
1780} 1825}
1781 1826
1909 # {... are special paths that are not being touched 1954 # {... are special paths that are not being touched
1910 # ?xxx/... are special absolute paths 1955 # ?xxx/... are special absolute paths
1911 # ?random/... random maps 1956 # ?random/... random maps
1912 # /... normal maps 1957 # /... normal maps
1913 # ~user/... per-player map of a specific user 1958 # ~user/... per-player map of a specific user
1959 # !up !down for quad maps, or other maps with up/down layers
1914 1960
1915 $path =~ s/$PATH_SEP/\//go; 1961 $path =~ s/$PATH_SEP/\//go;
1916 1962
1917 # treat it as relative path if it starts with 1963 # treat it as relative path if it starts with
1918 # something that looks reasonable 1964 # something that looks reasonable
1919 if ($path =~ m{^(?:\./|\.\./|\w)}) { 1965 if ($path =~ m{^(?:\./|\.\./|\w)}) {
1920 $base or Carp::carp "normalise called with relative path and no base: '$path'"; 1966 $base or Carp::carp "normalise called with relative path and no base: '$path'";
1921 1967
1922 $base =~ s{[^/]+/?$}{}; 1968 $base =~ s{[^/]+/?$}{};
1923 $path = "$base/$path"; 1969 $path = "$base/$path";
1970
1971 } elsif ($path eq '!up') {
1972 $base && ref $base
1973 or Carp::carp "normalise called with relative tile path and no base: '$path'";
1974
1975 my $uppth = $base->tile_path (cf::TILE_UP);
1976 $path = $uppth if $uppth;
1977
1978 } elsif ($path eq '!down') {
1979 $base && ref $base
1980 or Carp::carp "normalise called with relative tile path and no base: '$path'";
1981
1982 my $dpth = $base->tile_path (cf::TILE_DOWN);
1983 $path = $dpth if $dpth;
1924 } 1984 }
1925 1985
1926 for ($path) { 1986 for ($path) {
1927 redo if s{/\.?/}{/}; 1987 redo if s{/\.?/}{/};
1928 redo if s{/[^/]+/\.\./}{/}; 1988 redo if s{/[^/]+/\.\./}{/};
1949 2009
1950 Carp::cluck "unable to resolve path '$path' (base '$base')"; 2010 Carp::cluck "unable to resolve path '$path' (base '$base')";
1951 () 2011 ()
1952} 2012}
1953 2013
2014# may re-bless or do other evil things
1954sub init { 2015sub init {
1955 my ($self) = @_; 2016 my ($self) = @_;
1956 2017
1957 $self 2018 $self
1958} 2019}
2080sub find { 2141sub find {
2081 my ($path, $origin) = @_; 2142 my ($path, $origin) = @_;
2082 2143
2083 cf::cede_to_tick; 2144 cf::cede_to_tick;
2084 2145
2085 $path = normalise $path, $origin && $origin->path; 2146 $path = normalise $path, $origin;
2086 2147
2087 my $guard1 = cf::lock_acquire "map_data:$path";#d#remove 2148 my $guard1 = cf::lock_acquire "map_data:$path";#d#remove
2088 my $guard2 = cf::lock_acquire "map_find:$path"; 2149 my $guard2 = cf::lock_acquire "map_find:$path";
2089 2150
2090 $cf::MAP{$path} || do { 2151 $cf::MAP{$path} || do {
2130 $self->alloc; 2191 $self->alloc;
2131 2192
2132 $self->pre_load; 2193 $self->pre_load;
2133 cf::cede_to_tick; 2194 cf::cede_to_tick;
2134 2195
2196 if (exists $self->{load_path}) {
2135 my $f = new_from_file cf::object::thawer $self->{load_path}; 2197 my $f = new_from_file cf::object::thawer $self->{load_path};
2136 $f->skip_block; 2198 $f->skip_block;
2137 $self->_load_objects ($f) 2199 $self->_load_objects ($f)
2138 or return; 2200 or return;
2139 2201
2140 $self->post_load_original 2202 $self->post_load_original
2141 if delete $self->{load_original}; 2203 if delete $self->{load_original};
2142 2204
2143 if (my $uniq = $self->uniq_path) { 2205 if (my $uniq = $self->uniq_path) {
2144 utf8::encode $uniq; 2206 utf8::encode $uniq;
2145 unless (aio_stat $uniq) { 2207 unless (aio_stat $uniq) {
2146 if (my $f = new_from_file cf::object::thawer $uniq) { 2208 if (my $f = new_from_file cf::object::thawer $uniq) {
2147 $self->clear_unique_items; 2209 $self->clear_unique_items;
2148 $self->_load_objects ($f); 2210 $self->_load_objects ($f);
2149 $f->resolve_delayed_derefs; 2211 $f->resolve_delayed_derefs;
2212 }
2150 } 2213 }
2151 } 2214 }
2152 }
2153 2215
2154 $f->resolve_delayed_derefs; 2216 $f->resolve_delayed_derefs;
2217 }
2155 2218
2156 cf::cede_to_tick; 2219 cf::cede_to_tick;
2157 # now do the right thing for maps 2220 # now do the right thing for maps
2158 $self->link_multipart_objects; 2221 $self->link_multipart_objects;
2159 $self->difficulty ($self->estimate_difficulty) 2222 $self->difficulty ($self->estimate_difficulty)
2243our $MAP_PREFETCHER = undef; 2306our $MAP_PREFETCHER = undef;
2244 2307
2245sub find_async { 2308sub find_async {
2246 my ($path, $origin, $load) = @_; 2309 my ($path, $origin, $load) = @_;
2247 2310
2248 $path = normalise $path, $origin && $origin->{path}; 2311 $path = normalise $path, $origin;
2249 2312
2250 if (my $map = $cf::MAP{$path}) { 2313 if (my $map = $cf::MAP{$path}) {
2251 return $map if !$load || $map->in_memory == cf::MAP_ACTIVE; 2314 return $map if !$load || $map->in_memory == cf::MAP_ACTIVE;
2252 } 2315 }
2253 2316
2764 ($path, $x, $y) = (undef, undef, undef); 2827 ($path, $x, $y) = (undef, undef, undef);
2765 } 2828 }
2766 } 2829 }
2767 2830
2768 my $map = eval { 2831 my $map = eval {
2769 my $map = defined $path ? cf::map::find $path : undef; 2832 my $map = defined $path ? cf::map::find $path, $self->map : undef;
2770 2833
2771 if ($map) { 2834 if ($map) {
2772 $map = $map->customise_for ($self); 2835 $map = $map->customise_for ($self);
2773 $map = $check->($map, $x, $y, $self) if $check && $map; 2836 $map = $check->($map, $x, $y, $self) if $check && $map;
2774 } else { 2837 } else {
2872 2935
2873 prepare_random_map $exit 2936 prepare_random_map $exit
2874 if $exit->slaying eq "/!"; 2937 if $exit->slaying eq "/!";
2875 } 2938 }
2876 2939
2877 my $map = cf::map::normalise $exit->slaying, $exit->map && $exit->map->path; 2940 my $map = cf::map::normalise $exit->slaying, $exit->map;
2878 my $x = $exit->stats->hp; 2941 my $x = $exit->stats->hp;
2879 my $y = $exit->stats->sp; 2942 my $y = $exit->stats->sp;
2880 2943
2881 $self->goto ($map, $x, $y); 2944 $self->goto ($map, $x, $y);
2882 2945
3399 3462
3400 my $enc = JSON::XS->new->utf8->canonical->relaxed; 3463 my $enc = JSON::XS->new->utf8->canonical->relaxed;
3401 3464
3402 trace "loading facedata from $path\n"; 3465 trace "loading facedata from $path\n";
3403 3466
3404 0 < aio_load $path, my $facedata 3467 my $facedata = decode_storable load_file $path;
3405 or die "$path: $!";
3406
3407 $facedata = Coro::Storable::thaw $facedata;
3408 3468
3409 $facedata->{version} == 2 3469 $facedata->{version} == 2
3410 or cf::cleanup "$path: version mismatch, cannot proceed."; 3470 or cf::cleanup "$path: version mismatch, cannot proceed.";
3411 3471
3412 # patch in the exptable 3472 # patch in the exptable
3426 3486
3427 cf::face::set_visibility $idx, $info->{visibility}; 3487 cf::face::set_visibility $idx, $info->{visibility};
3428 cf::face::set_magicmap $idx, $info->{magicmap}; 3488 cf::face::set_magicmap $idx, $info->{magicmap};
3429 cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32}; 3489 cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32};
3430 cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64}; 3490 cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64};
3491 cf::face::set_data $idx, 2, $info->{glyph} , $info->{glyph} ;
3431 3492
3432 cf::cede_to_tick; 3493 cf::cede_to_tick;
3433 } 3494 }
3434 3495
3435 while (my ($face, $info) = each %$faces) { 3496 while (my ($face, $info) = each %$faces) {
3530} 3591}
3531 3592
3532sub reload_sound { 3593sub reload_sound {
3533 trace "loading sound config from $DATADIR/sound\n"; 3594 trace "loading sound config from $DATADIR/sound\n";
3534 3595
3535 0 < Coro::AIO::aio_load "$DATADIR/sound", my $data
3536 or die "$DATADIR/sound $!";
3537
3538 my $soundconf = JSON::XS->new->utf8->relaxed->decode ($data); 3596 my $soundconf = JSON::XS->new->utf8->relaxed->decode (load_file "$DATADIR/sound");
3539 3597
3540 for (0 .. SOUND_CAST_SPELL_0 - 1) { 3598 for (0 .. SOUND_CAST_SPELL_0 - 1) {
3541 my $sound = $soundconf->{compat}[$_] 3599 my $sound = $soundconf->{compat}[$_]
3542 or next; 3600 or next;
3543 3601
3567} 3625}
3568 3626
3569sub reload_config { 3627sub reload_config {
3570 trace "reloading config file...\n"; 3628 trace "reloading config file...\n";
3571 3629
3572 0 < aio_load "$CONFDIR/config", my $config 3630 my $config = load_file "$CONFDIR/config";
3573 or die "$CONFDIR/config: $!";
3574
3575 utf8::decode $config; 3631 utf8::decode $config;
3576 *CFG = yaml_load $config; 3632 *CFG = decode_yaml $config;
3577 3633
3578 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; 3634 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38];
3579 3635
3580 $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; 3636 $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset};
3581 $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; 3637 $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset};
3619 3675
3620sub main { 3676sub main {
3621 cf::init_globals; # initialise logging 3677 cf::init_globals; # initialise logging
3622 3678
3623 LOG llevInfo, "Welcome to Deliantra, v" . VERSION; 3679 LOG llevInfo, "Welcome to Deliantra, v" . VERSION;
3624 LOG llevInfo, "Copyright (C) 2005-2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; 3680 LOG llevInfo, "Copyright (C) 2005-2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team.";
3625 LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; 3681 LOG llevInfo, "Copyright (C) 1994 Mark Wedel.";
3626 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; 3682 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen.";
3627 3683
3628 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority 3684 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
3629 3685

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines