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.559 by root, Sat Feb 26 12:50:27 2011 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.
317 Carp::cluck "error in event callback: @_"; 317 Carp::cluck "error in event callback: @_";
318}; 318};
319 319
320############################################################################# 320#############################################################################
321 321
322sub fork_call(&@);
323sub get_slot($;$$);
324
325#############################################################################
326
322=head2 UTILITY FUNCTIONS 327=head2 UTILITY FUNCTIONS
323 328
324=over 4 329=over 4
325 330
326=item dumpval $ref 331=item dumpval $ref
344 $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;
345 $d 350 $d
346 } || "[unable to dump $_[0]: '$@']"; 351 } || "[unable to dump $_[0]: '$@']";
347} 352}
348 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
349=item $ref = cf::decode_json $json 368=item $ref = cf::decode_json $json
350 369
351Converts a JSON string into the corresponding perl data structure. 370Converts a JSON string into the corresponding perl data structure.
352 371
353=item $json = cf::encode_json $ref 372=item $json = cf::encode_json $ref
359our $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
360 379
361sub encode_json($) { $json_coder->encode ($_[0]) } 380sub encode_json($) { $json_coder->encode ($_[0]) }
362sub decode_json($) { $json_coder->decode ($_[0]) } 381sub decode_json($) { $json_coder->decode ($_[0]) }
363 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
364=item $ref = cf::yaml_load $scalar 391=item $ref = cf::decode_yaml $scalar
365 392
366Same 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).
367 394
368=cut 395=cut
369 396
370sub fork_call(&@); 397sub decode_yaml($) {
371
372sub yaml_load($) {
373 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]
374} 412}
375 413
376=item cf::post_init { BLOCK } 414=item cf::post_init { BLOCK }
377 415
378Execute the given codeblock, I<after> all extensions have been (re-)loaded, 416Execute the given codeblock, I<after> all extensions have been (re-)loaded,
462 EV::periodic $start, $interval, 0, $cb 500 EV::periodic $start, $interval, 0, $cb
463} 501}
464 502
465=item cf::get_slot $time[, $priority[, $name]] 503=item cf::get_slot $time[, $priority[, $name]]
466 504
467Allocate $time seconds of blocking CPU time at priority C<$priority>: 505Allocate $time seconds of blocking CPU time at priority C<$priority>
468This 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
469of 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.
470 512
471The 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
472used for statistical purposes and should identify the same time-class. 514used for statistical purposes and should identify the same time-class.
473 515
474Useful for short background jobs. 516Useful for short background jobs.
769 811
770 my @data; 812 my @data;
771 my $md5; 813 my $md5;
772 814
773 for (0 .. $#$src) { 815 for (0 .. $#$src) {
774 0 <= aio_load $src->[$_], $data[$_] 816 $data[$_] = load_file $src->[$_];
775 or Carp::croak "$src->[$_]: $!";
776 } 817 }
777 818
778 # if processing is expensive, check 819 # if processing is expensive, check
779 # checksum first 820 # checksum first
780 if (1) { 821 if (1) {
1514 1555
1515 while (my ($k, $v) = each %todo) { 1556 while (my ($k, $v) = each %todo) {
1516 cf::cleanup "mandatory extension '$k' has unresolved dependencies, exiting." 1557 cf::cleanup "mandatory extension '$k' has unresolved dependencies, exiting."
1517 if exists $v->{meta}{mandatory}; 1558 if exists $v->{meta}{mandatory};
1518 } 1559 }
1560
1561 last;
1519 } 1562 }
1520 } 1563 }
1521 }; 1564 };
1522} 1565}
1523 1566
1911 # {... are special paths that are not being touched 1954 # {... are special paths that are not being touched
1912 # ?xxx/... are special absolute paths 1955 # ?xxx/... are special absolute paths
1913 # ?random/... random maps 1956 # ?random/... random maps
1914 # /... normal maps 1957 # /... normal maps
1915 # ~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
1916 1960
1917 $path =~ s/$PATH_SEP/\//go; 1961 $path =~ s/$PATH_SEP/\//go;
1918 1962
1919 # treat it as relative path if it starts with 1963 # treat it as relative path if it starts with
1920 # something that looks reasonable 1964 # something that looks reasonable
1921 if ($path =~ m{^(?:\./|\.\./|\w)}) { 1965 if ($path =~ m{^(?:\./|\.\./|\w)}) {
1922 $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'";
1923 1967
1924 $base =~ s{[^/]+/?$}{}; 1968 $base =~ s{[^/]+/?$}{};
1925 $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;
1926 } 1984 }
1927 1985
1928 for ($path) { 1986 for ($path) {
1929 redo if s{/\.?/}{/}; 1987 redo if s{/\.?/}{/};
1930 redo if s{/[^/]+/\.\./}{/}; 1988 redo if s{/[^/]+/\.\./}{/};
1951 2009
1952 Carp::cluck "unable to resolve path '$path' (base '$base')"; 2010 Carp::cluck "unable to resolve path '$path' (base '$base')";
1953 () 2011 ()
1954} 2012}
1955 2013
2014# may re-bless or do other evil things
1956sub init { 2015sub init {
1957 my ($self) = @_; 2016 my ($self) = @_;
1958 2017
1959 $self 2018 $self
1960} 2019}
2025 $self->{load_path} = $path; 2084 $self->{load_path} = $path;
2026 2085
2027 1 2086 1
2028} 2087}
2029 2088
2089# used to laod the header of an original map
2030sub load_header_orig { 2090sub load_header_orig {
2031 my ($self) = @_; 2091 my ($self) = @_;
2032 2092
2033 $self->load_header_from ($self->load_path) 2093 $self->load_header_from ($self->load_path)
2034} 2094}
2035 2095
2096# used to laod the header of an instantiated map
2036sub load_header_temp { 2097sub load_header_temp {
2037 my ($self) = @_; 2098 my ($self) = @_;
2038 2099
2039 $self->load_header_from ($self->save_path) 2100 $self->load_header_from ($self->save_path)
2040} 2101}
2041 2102
2103# called after loading the header from an instantiated map
2042sub prepare_temp { 2104sub prepare_temp {
2043 my ($self) = @_; 2105 my ($self) = @_;
2044 2106
2045 $self->last_access ((delete $self->{last_access}) 2107 $self->last_access ((delete $self->{last_access})
2046 || $cf::RUNTIME); #d# 2108 || $cf::RUNTIME); #d#
2047 # safety 2109 # safety
2048 $self->{instantiate_time} = $cf::RUNTIME 2110 $self->{instantiate_time} = $cf::RUNTIME
2049 if $self->{instantiate_time} > $cf::RUNTIME; 2111 if $self->{instantiate_time} > $cf::RUNTIME;
2050} 2112}
2051 2113
2114# called after loading the header from an original map
2052sub prepare_orig { 2115sub prepare_orig {
2053 my ($self) = @_; 2116 my ($self) = @_;
2054 2117
2055 $self->{load_original} = 1; 2118 $self->{load_original} = 1;
2056 $self->{instantiate_time} = $cf::RUNTIME; 2119 $self->{instantiate_time} = $cf::RUNTIME;
2082sub find { 2145sub find {
2083 my ($path, $origin) = @_; 2146 my ($path, $origin) = @_;
2084 2147
2085 cf::cede_to_tick; 2148 cf::cede_to_tick;
2086 2149
2087 $path = normalise $path, $origin && $origin->path; 2150 $path = normalise $path, $origin;
2088 2151
2089 my $guard1 = cf::lock_acquire "map_data:$path";#d#remove 2152 my $guard1 = cf::lock_acquire "map_data:$path";#d#remove
2090 my $guard2 = cf::lock_acquire "map_find:$path"; 2153 my $guard2 = cf::lock_acquire "map_find:$path";
2091 2154
2092 $cf::MAP{$path} || do { 2155 $cf::MAP{$path} || do {
2132 $self->alloc; 2195 $self->alloc;
2133 2196
2134 $self->pre_load; 2197 $self->pre_load;
2135 cf::cede_to_tick; 2198 cf::cede_to_tick;
2136 2199
2200 if (exists $self->{load_path}) {
2137 my $f = new_from_file cf::object::thawer $self->{load_path}; 2201 my $f = new_from_file cf::object::thawer $self->{load_path};
2138 $f->skip_block; 2202 $f->skip_block;
2139 $self->_load_objects ($f) 2203 $self->_load_objects ($f)
2140 or return; 2204 or return;
2141 2205
2142 $self->post_load_original 2206 $self->post_load_original
2143 if delete $self->{load_original}; 2207 if delete $self->{load_original};
2144 2208
2145 if (my $uniq = $self->uniq_path) { 2209 if (my $uniq = $self->uniq_path) {
2146 utf8::encode $uniq; 2210 utf8::encode $uniq;
2147 unless (aio_stat $uniq) { 2211 unless (aio_stat $uniq) {
2148 if (my $f = new_from_file cf::object::thawer $uniq) { 2212 if (my $f = new_from_file cf::object::thawer $uniq) {
2149 $self->clear_unique_items; 2213 $self->clear_unique_items;
2150 $self->_load_objects ($f); 2214 $self->_load_objects ($f);
2151 $f->resolve_delayed_derefs; 2215 $f->resolve_delayed_derefs;
2216 }
2152 } 2217 }
2153 } 2218 }
2154 }
2155 2219
2156 $f->resolve_delayed_derefs; 2220 $f->resolve_delayed_derefs;
2221 } else {
2222 $self->post_load_original
2223 if delete $self->{load_original};
2224 }
2157 2225
2158 cf::cede_to_tick; 2226 cf::cede_to_tick;
2159 # now do the right thing for maps 2227 # now do the right thing for maps
2160 $self->link_multipart_objects; 2228 $self->link_multipart_objects;
2161 $self->difficulty ($self->estimate_difficulty) 2229 $self->difficulty ($self->estimate_difficulty)
2245our $MAP_PREFETCHER = undef; 2313our $MAP_PREFETCHER = undef;
2246 2314
2247sub find_async { 2315sub find_async {
2248 my ($path, $origin, $load) = @_; 2316 my ($path, $origin, $load) = @_;
2249 2317
2250 $path = normalise $path, $origin && $origin->{path}; 2318 $path = normalise $path, $origin;
2251 2319
2252 if (my $map = $cf::MAP{$path}) { 2320 if (my $map = $cf::MAP{$path}) {
2253 return $map if !$load || $map->in_memory == cf::MAP_ACTIVE; 2321 return $map if !$load || $map->in_memory == cf::MAP_ACTIVE;
2254 } 2322 }
2255 2323
2766 ($path, $x, $y) = (undef, undef, undef); 2834 ($path, $x, $y) = (undef, undef, undef);
2767 } 2835 }
2768 } 2836 }
2769 2837
2770 my $map = eval { 2838 my $map = eval {
2771 my $map = defined $path ? cf::map::find $path : undef; 2839 my $map = defined $path ? cf::map::find $path, $self->map : undef;
2772 2840
2773 if ($map) { 2841 if ($map) {
2774 $map = $map->customise_for ($self); 2842 $map = $map->customise_for ($self);
2775 $map = $check->($map, $x, $y, $self) if $check && $map; 2843 $map = $check->($map, $x, $y, $self) if $check && $map;
2776 } else { 2844 } else {
2874 2942
2875 prepare_random_map $exit 2943 prepare_random_map $exit
2876 if $exit->slaying eq "/!"; 2944 if $exit->slaying eq "/!";
2877 } 2945 }
2878 2946
2879 my $map = cf::map::normalise $exit->slaying, $exit->map && $exit->map->path; 2947 my $map = cf::map::normalise $exit->slaying, $exit->map;
2880 my $x = $exit->stats->hp; 2948 my $x = $exit->stats->hp;
2881 my $y = $exit->stats->sp; 2949 my $y = $exit->stats->sp;
2882 2950
2883 $self->goto ($map, $x, $y); 2951 $self->goto ($map, $x, $y);
2884 2952
3401 3469
3402 my $enc = JSON::XS->new->utf8->canonical->relaxed; 3470 my $enc = JSON::XS->new->utf8->canonical->relaxed;
3403 3471
3404 trace "loading facedata from $path\n"; 3472 trace "loading facedata from $path\n";
3405 3473
3406 0 < aio_load $path, my $facedata 3474 my $facedata = decode_storable load_file $path;
3407 or die "$path: $!";
3408
3409 $facedata = Coro::Storable::thaw $facedata;
3410 3475
3411 $facedata->{version} == 2 3476 $facedata->{version} == 2
3412 or cf::cleanup "$path: version mismatch, cannot proceed."; 3477 or cf::cleanup "$path: version mismatch, cannot proceed.";
3413 3478
3414 # patch in the exptable 3479 # patch in the exptable
3533} 3598}
3534 3599
3535sub reload_sound { 3600sub reload_sound {
3536 trace "loading sound config from $DATADIR/sound\n"; 3601 trace "loading sound config from $DATADIR/sound\n";
3537 3602
3538 0 < Coro::AIO::aio_load "$DATADIR/sound", my $data
3539 or die "$DATADIR/sound $!";
3540
3541 my $soundconf = JSON::XS->new->utf8->relaxed->decode ($data); 3603 my $soundconf = JSON::XS->new->utf8->relaxed->decode (load_file "$DATADIR/sound");
3542 3604
3543 for (0 .. SOUND_CAST_SPELL_0 - 1) { 3605 for (0 .. SOUND_CAST_SPELL_0 - 1) {
3544 my $sound = $soundconf->{compat}[$_] 3606 my $sound = $soundconf->{compat}[$_]
3545 or next; 3607 or next;
3546 3608
3570} 3632}
3571 3633
3572sub reload_config { 3634sub reload_config {
3573 trace "reloading config file...\n"; 3635 trace "reloading config file...\n";
3574 3636
3575 0 < aio_load "$CONFDIR/config", my $config 3637 my $config = load_file "$CONFDIR/config";
3576 or die "$CONFDIR/config: $!";
3577
3578 utf8::decode $config; 3638 utf8::decode $config;
3579 *CFG = yaml_load $config; 3639 *CFG = decode_yaml $config;
3580 3640
3581 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; 3641 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38];
3582 3642
3583 $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};
3584 $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};
3622 3682
3623sub main { 3683sub main {
3624 cf::init_globals; # initialise logging 3684 cf::init_globals; # initialise logging
3625 3685
3626 LOG llevInfo, "Welcome to Deliantra, v" . VERSION; 3686 LOG llevInfo, "Welcome to Deliantra, v" . VERSION;
3627 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.";
3628 LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; 3688 LOG llevInfo, "Copyright (C) 1994 Mark Wedel.";
3629 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; 3689 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen.";
3630 3690
3631 $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
3632 3692

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines