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.558 by root, Tue Oct 5 22:35:30 2010 UTC vs.
Revision 1.566 by root, Sun May 1 16:58:16 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
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}
2023 $self->{load_path} = $path; 2084 $self->{load_path} = $path;
2024 2085
2025 1 2086 1
2026} 2087}
2027 2088
2089# used to laod the header of an original map
2028sub load_header_orig { 2090sub load_header_orig {
2029 my ($self) = @_; 2091 my ($self) = @_;
2030 2092
2031 $self->load_header_from ($self->load_path) 2093 $self->load_header_from ($self->load_path)
2032} 2094}
2033 2095
2096# used to laod the header of an instantiated map
2034sub load_header_temp { 2097sub load_header_temp {
2035 my ($self) = @_; 2098 my ($self) = @_;
2036 2099
2037 $self->load_header_from ($self->save_path) 2100 $self->load_header_from ($self->save_path)
2038} 2101}
2039 2102
2103# called after loading the header from an instantiated map
2040sub prepare_temp { 2104sub prepare_temp {
2041 my ($self) = @_; 2105 my ($self) = @_;
2042 2106
2043 $self->last_access ((delete $self->{last_access}) 2107 $self->last_access ((delete $self->{last_access})
2044 || $cf::RUNTIME); #d# 2108 || $cf::RUNTIME); #d#
2045 # safety 2109 # safety
2046 $self->{instantiate_time} = $cf::RUNTIME 2110 $self->{instantiate_time} = $cf::RUNTIME
2047 if $self->{instantiate_time} > $cf::RUNTIME; 2111 if $self->{instantiate_time} > $cf::RUNTIME;
2048} 2112}
2049 2113
2114# called after loading the header from an original map
2050sub prepare_orig { 2115sub prepare_orig {
2051 my ($self) = @_; 2116 my ($self) = @_;
2052 2117
2053 $self->{load_original} = 1; 2118 $self->{load_original} = 1;
2054 $self->{instantiate_time} = $cf::RUNTIME; 2119 $self->{instantiate_time} = $cf::RUNTIME;
2080sub find { 2145sub find {
2081 my ($path, $origin) = @_; 2146 my ($path, $origin) = @_;
2082 2147
2083 cf::cede_to_tick; 2148 cf::cede_to_tick;
2084 2149
2085 $path = normalise $path, $origin && $origin->path; 2150 $path = normalise $path, $origin;
2086 2151
2087 my $guard1 = cf::lock_acquire "map_data:$path";#d#remove 2152 my $guard1 = cf::lock_acquire "map_data:$path";#d#remove
2088 my $guard2 = cf::lock_acquire "map_find:$path"; 2153 my $guard2 = cf::lock_acquire "map_find:$path";
2089 2154
2090 $cf::MAP{$path} || do { 2155 $cf::MAP{$path} || do {
2130 $self->alloc; 2195 $self->alloc;
2131 2196
2132 $self->pre_load; 2197 $self->pre_load;
2133 cf::cede_to_tick; 2198 cf::cede_to_tick;
2134 2199
2200 if (exists $self->{load_path}) {
2135 my $f = new_from_file cf::object::thawer $self->{load_path}; 2201 my $f = new_from_file cf::object::thawer $self->{load_path};
2136 $f->skip_block; 2202 $f->skip_block;
2137 $self->_load_objects ($f) 2203 $self->_load_objects ($f)
2138 or return; 2204 or return;
2139 2205
2140 $self->post_load_original 2206 $self->post_load_original
2141 if delete $self->{load_original}; 2207 if delete $self->{load_original};
2142 2208
2143 if (my $uniq = $self->uniq_path) { 2209 if (my $uniq = $self->uniq_path) {
2144 utf8::encode $uniq; 2210 utf8::encode $uniq;
2145 unless (aio_stat $uniq) { 2211 unless (aio_stat $uniq) {
2146 if (my $f = new_from_file cf::object::thawer $uniq) { 2212 if (my $f = new_from_file cf::object::thawer $uniq) {
2147 $self->clear_unique_items; 2213 $self->clear_unique_items;
2148 $self->_load_objects ($f); 2214 $self->_load_objects ($f);
2149 $f->resolve_delayed_derefs; 2215 $f->resolve_delayed_derefs;
2216 }
2150 } 2217 }
2151 } 2218 }
2152 }
2153 2219
2154 $f->resolve_delayed_derefs; 2220 $f->resolve_delayed_derefs;
2221 } else {
2222 $self->post_load_original
2223 if delete $self->{load_original};
2224 }
2155 2225
2156 cf::cede_to_tick; 2226 cf::cede_to_tick;
2157 # now do the right thing for maps 2227 # now do the right thing for maps
2158 $self->link_multipart_objects; 2228 $self->link_multipart_objects;
2159 $self->difficulty ($self->estimate_difficulty) 2229 $self->difficulty ($self->estimate_difficulty)
2243our $MAP_PREFETCHER = undef; 2313our $MAP_PREFETCHER = undef;
2244 2314
2245sub find_async { 2315sub find_async {
2246 my ($path, $origin, $load) = @_; 2316 my ($path, $origin, $load) = @_;
2247 2317
2248 $path = normalise $path, $origin && $origin->{path}; 2318 $path = normalise $path, $origin;
2249 2319
2250 if (my $map = $cf::MAP{$path}) { 2320 if (my $map = $cf::MAP{$path}) {
2251 return $map if !$load || $map->in_memory == cf::MAP_ACTIVE; 2321 return $map if !$load || $map->in_memory == cf::MAP_ACTIVE;
2252 } 2322 }
2253 2323
2764 ($path, $x, $y) = (undef, undef, undef); 2834 ($path, $x, $y) = (undef, undef, undef);
2765 } 2835 }
2766 } 2836 }
2767 2837
2768 my $map = eval { 2838 my $map = eval {
2769 my $map = defined $path ? cf::map::find $path : undef; 2839 my $map = defined $path ? cf::map::find $path, $self->map : undef;
2770 2840
2771 if ($map) { 2841 if ($map) {
2772 $map = $map->customise_for ($self); 2842 $map = $map->customise_for ($self);
2773 $map = $check->($map, $x, $y, $self) if $check && $map; 2843 $map = $check->($map, $x, $y, $self) if $check && $map;
2774 } else { 2844 } else {
2872 2942
2873 prepare_random_map $exit 2943 prepare_random_map $exit
2874 if $exit->slaying eq "/!"; 2944 if $exit->slaying eq "/!";
2875 } 2945 }
2876 2946
2877 my $map = cf::map::normalise $exit->slaying, $exit->map && $exit->map->path; 2947 my $map = cf::map::normalise $exit->slaying, $exit->map;
2878 my $x = $exit->stats->hp; 2948 my $x = $exit->stats->hp;
2879 my $y = $exit->stats->sp; 2949 my $y = $exit->stats->sp;
2880 2950
2881 $self->goto ($map, $x, $y); 2951 $self->goto ($map, $x, $y);
2882 2952
3399 3469
3400 my $enc = JSON::XS->new->utf8->canonical->relaxed; 3470 my $enc = JSON::XS->new->utf8->canonical->relaxed;
3401 3471
3402 trace "loading facedata from $path\n"; 3472 trace "loading facedata from $path\n";
3403 3473
3404 0 < aio_load $path, my $facedata 3474 my $facedata = decode_storable load_file $path;
3405 or die "$path: $!";
3406
3407 $facedata = Coro::Storable::thaw $facedata;
3408 3475
3409 $facedata->{version} == 2 3476 $facedata->{version} == 2
3410 or cf::cleanup "$path: version mismatch, cannot proceed."; 3477 or cf::cleanup "$path: version mismatch, cannot proceed.";
3411 3478
3412 # patch in the exptable 3479 # patch in the exptable
3531} 3598}
3532 3599
3533sub reload_sound { 3600sub reload_sound {
3534 trace "loading sound config from $DATADIR/sound\n"; 3601 trace "loading sound config from $DATADIR/sound\n";
3535 3602
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); 3603 my $soundconf = JSON::XS->new->utf8->relaxed->decode (load_file "$DATADIR/sound");
3540 3604
3541 for (0 .. SOUND_CAST_SPELL_0 - 1) { 3605 for (0 .. SOUND_CAST_SPELL_0 - 1) {
3542 my $sound = $soundconf->{compat}[$_] 3606 my $sound = $soundconf->{compat}[$_]
3543 or next; 3607 or next;
3544 3608
3568} 3632}
3569 3633
3570sub reload_config { 3634sub reload_config {
3571 trace "reloading config file...\n"; 3635 trace "reloading config file...\n";
3572 3636
3573 0 < aio_load "$CONFDIR/config", my $config 3637 my $config = load_file "$CONFDIR/config";
3574 or die "$CONFDIR/config: $!";
3575
3576 utf8::decode $config; 3638 utf8::decode $config;
3577 *CFG = yaml_load $config; 3639 *CFG = decode_yaml $config;
3578 3640
3579 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; 3641 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38];
3580 3642
3581 $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; 3643 $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}; 3644 $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset};
3620 3682
3621sub main { 3683sub main {
3622 cf::init_globals; # initialise logging 3684 cf::init_globals; # initialise logging
3623 3685
3624 LOG llevInfo, "Welcome to Deliantra, v" . VERSION; 3686 LOG llevInfo, "Welcome to Deliantra, v" . VERSION;
3625 LOG llevInfo, "Copyright (C) 2005-2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; 3687 LOG llevInfo, "Copyright (C) 2005-2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team.";
3626 LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; 3688 LOG llevInfo, "Copyright (C) 1994 Mark Wedel.";
3627 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; 3689 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen.";
3628 3690
3629 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority 3691 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
3630 3692

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines