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.569 by root, Wed May 4 07:36:40 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
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
1954sub init { 2000sub init {
1955 my ($self) = @_; 2001 my ($self) = @_;
1956 2002
1957 $self 2003 $self
1958} 2004}
2023 $self->{load_path} = $path; 2069 $self->{load_path} = $path;
2024 2070
2025 1 2071 1
2026} 2072}
2027 2073
2074# used to laod the header of an original map
2028sub load_header_orig { 2075sub load_header_orig {
2029 my ($self) = @_; 2076 my ($self) = @_;
2030 2077
2031 $self->load_header_from ($self->load_path) 2078 $self->load_header_from ($self->load_path)
2032} 2079}
2033 2080
2081# used to laod the header of an instantiated map
2034sub load_header_temp { 2082sub load_header_temp {
2035 my ($self) = @_; 2083 my ($self) = @_;
2036 2084
2037 $self->load_header_from ($self->save_path) 2085 $self->load_header_from ($self->save_path)
2038} 2086}
2039 2087
2088# called after loading the header from an instantiated map
2040sub prepare_temp { 2089sub prepare_temp {
2041 my ($self) = @_; 2090 my ($self) = @_;
2042 2091
2043 $self->last_access ((delete $self->{last_access}) 2092 $self->last_access ((delete $self->{last_access})
2044 || $cf::RUNTIME); #d# 2093 || $cf::RUNTIME); #d#
2045 # safety 2094 # safety
2046 $self->{instantiate_time} = $cf::RUNTIME 2095 $self->{instantiate_time} = $cf::RUNTIME
2047 if $self->{instantiate_time} > $cf::RUNTIME; 2096 if $self->{instantiate_time} > $cf::RUNTIME;
2048} 2097}
2049 2098
2099# called after loading the header from an original map
2050sub prepare_orig { 2100sub prepare_orig {
2051 my ($self) = @_; 2101 my ($self) = @_;
2052 2102
2053 $self->{load_original} = 1; 2103 $self->{load_original} = 1;
2054 $self->{instantiate_time} = $cf::RUNTIME; 2104 $self->{instantiate_time} = $cf::RUNTIME;
2080sub find { 2130sub find {
2081 my ($path, $origin) = @_; 2131 my ($path, $origin) = @_;
2082 2132
2083 cf::cede_to_tick; 2133 cf::cede_to_tick;
2084 2134
2085 $path = normalise $path, $origin && $origin->path; 2135 $path = normalise $path, $origin;
2086 2136
2087 my $guard1 = cf::lock_acquire "map_data:$path";#d#remove 2137 my $guard1 = cf::lock_acquire "map_data:$path";#d#remove
2088 my $guard2 = cf::lock_acquire "map_find:$path"; 2138 my $guard2 = cf::lock_acquire "map_find:$path";
2089 2139
2090 $cf::MAP{$path} || do { 2140 $cf::MAP{$path} || do {
2123 my $guard = cf::lock_acquire "map_data:$path"; 2173 my $guard = cf::lock_acquire "map_data:$path";
2124 2174
2125 return unless $self->valid; 2175 return unless $self->valid;
2126 return unless $self->in_memory == cf::MAP_SWAPPED; 2176 return unless $self->in_memory == cf::MAP_SWAPPED;
2127 2177
2128 $self->in_memory (cf::MAP_LOADING);
2129
2130 $self->alloc; 2178 $self->alloc;
2131 2179
2132 $self->pre_load; 2180 $self->pre_load;
2133 cf::cede_to_tick; 2181 cf::cede_to_tick;
2134 2182
2183 if (exists $self->{load_path}) {
2135 my $f = new_from_file cf::object::thawer $self->{load_path}; 2184 my $f = new_from_file cf::object::thawer $self->{load_path};
2136 $f->skip_block; 2185 $f->skip_block;
2137 $self->_load_objects ($f) 2186 $self->_load_objects ($f)
2138 or return; 2187 or return;
2139 2188
2140 $self->post_load_original 2189 $self->post_load_original
2141 if delete $self->{load_original}; 2190 if delete $self->{load_original};
2142 2191
2143 if (my $uniq = $self->uniq_path) { 2192 if (my $uniq = $self->uniq_path) {
2144 utf8::encode $uniq; 2193 utf8::encode $uniq;
2145 unless (aio_stat $uniq) { 2194 unless (aio_stat $uniq) {
2146 if (my $f = new_from_file cf::object::thawer $uniq) { 2195 if (my $f = new_from_file cf::object::thawer $uniq) {
2147 $self->clear_unique_items; 2196 $self->clear_unique_items;
2148 $self->_load_objects ($f); 2197 $self->_load_objects ($f);
2149 $f->resolve_delayed_derefs; 2198 $f->resolve_delayed_derefs;
2199 }
2150 } 2200 }
2151 } 2201 }
2152 }
2153 2202
2154 $f->resolve_delayed_derefs; 2203 $f->resolve_delayed_derefs;
2204 } else {
2205 $self->post_load_original
2206 if delete $self->{load_original};
2207 }
2208
2209 $self->in_memory (cf::MAP_INACTIVE);
2155 2210
2156 cf::cede_to_tick; 2211 cf::cede_to_tick;
2157 # now do the right thing for maps 2212 # now do the right thing for maps
2158 $self->link_multipart_objects; 2213 $self->link_multipart_objects;
2159 $self->difficulty ($self->estimate_difficulty) 2214 $self->difficulty ($self->estimate_difficulty)
2163 unless ($self->{deny_activate}) { 2218 unless ($self->{deny_activate}) {
2164 $self->decay_objects; 2219 $self->decay_objects;
2165 $self->fix_auto_apply; 2220 $self->fix_auto_apply;
2166 $self->update_buttons; 2221 $self->update_buttons;
2167 cf::cede_to_tick; 2222 cf::cede_to_tick;
2168 $self->activate; 2223 #$self->activate; # no longer activate maps automatically
2169 } 2224 }
2170 2225
2171 $self->{last_save} = $cf::RUNTIME; 2226 $self->{last_save} = $cf::RUNTIME;
2172 $self->last_access ($cf::RUNTIME); 2227 $self->last_access ($cf::RUNTIME);
2173
2174 $self->in_memory (cf::MAP_ACTIVE);
2175 } 2228 }
2176 2229
2177 $self->post_load; 2230 $self->post_load;
2178 2231
2179 1 2232 1
2222} 2275}
2223 2276
2224sub find_sync { 2277sub find_sync {
2225 my ($path, $origin) = @_; 2278 my ($path, $origin) = @_;
2226 2279
2280 # it's a bug to call this from the main context
2227 return cf::LOG cf::llevError | cf::logBacktrace, "do_find_sync" 2281 return cf::LOG cf::llevError | cf::logBacktrace, "do_find_sync"
2228 if $Coro::current == $Coro::main; 2282 if $Coro::current == $Coro::main;
2229 2283
2230 find $path, $origin 2284 find $path, $origin
2231} 2285}
2232 2286
2233sub do_load_sync { 2287sub do_load_sync {
2234 my ($map) = @_; 2288 my ($map) = @_;
2235 2289
2290 # it's a bug to call this from the main context
2236 return cf::LOG cf::llevError | cf::logBacktrace, "do_load_sync" 2291 return cf::LOG cf::llevError | cf::logBacktrace, "do_load_sync"
2237 if $Coro::current == $Coro::main; 2292 if $Coro::current == $Coro::main;
2238 2293
2239 $map->load; 2294 $map->load;
2240} 2295}
2243our $MAP_PREFETCHER = undef; 2298our $MAP_PREFETCHER = undef;
2244 2299
2245sub find_async { 2300sub find_async {
2246 my ($path, $origin, $load) = @_; 2301 my ($path, $origin, $load) = @_;
2247 2302
2248 $path = normalise $path, $origin && $origin->{path}; 2303 $path = normalise $path, $origin;
2249 2304
2250 if (my $map = $cf::MAP{$path}) { 2305 if (my $map = $cf::MAP{$path}) {
2251 return $map if !$load || $map->in_memory == cf::MAP_ACTIVE; 2306 return $map if !$load || $map->linkable;
2252 } 2307 }
2253 2308
2254 $MAP_PREFETCH{$path} |= $load; 2309 $MAP_PREFETCH{$path} |= $load;
2255 2310
2256 $MAP_PREFETCHER ||= cf::async { 2311 $MAP_PREFETCHER ||= cf::async {
2315sub swap_out { 2370sub swap_out {
2316 my ($self) = @_; 2371 my ($self) = @_;
2317 2372
2318 my $lock = cf::lock_acquire "map_data:$self->{path}"; 2373 my $lock = cf::lock_acquire "map_data:$self->{path}";
2319 2374
2320 return if $self->in_memory != cf::MAP_ACTIVE; 2375 return if !$self->linkable;
2321 return if $self->{deny_save}; 2376 return if $self->{deny_save};
2322 return if $self->players; 2377 return if $self->players;
2323 2378
2324 # first deactivate the map and "unlink" it from the core 2379 # first deactivate the map and "unlink" it from the core
2325 $self->deactivate; 2380 $self->deactivate;
2764 ($path, $x, $y) = (undef, undef, undef); 2819 ($path, $x, $y) = (undef, undef, undef);
2765 } 2820 }
2766 } 2821 }
2767 2822
2768 my $map = eval { 2823 my $map = eval {
2769 my $map = defined $path ? cf::map::find $path : undef; 2824 my $map = defined $path ? cf::map::find $path, $self->map : undef;
2770 2825
2771 if ($map) { 2826 if ($map) {
2772 $map = $map->customise_for ($self); 2827 $map = $map->customise_for ($self);
2773 $map = $check->($map, $x, $y, $self) if $check && $map; 2828 $map = $check->($map, $x, $y, $self) if $check && $map;
2774 } else { 2829 } else {
2864 $Coro::current->{desc} = "enter_exit"; 2919 $Coro::current->{desc} = "enter_exit";
2865 2920
2866 unless (eval { 2921 unless (eval {
2867 $self->deactivate_recursive; # just to be sure 2922 $self->deactivate_recursive; # just to be sure
2868 2923
2869 # random map handling
2870 {
2871 my $guard = cf::lock_acquire "exit_prepare:$exit";
2872
2873 prepare_random_map $exit
2874 if $exit->slaying eq "/!";
2875 }
2876
2877 my $map = cf::map::normalise $exit->slaying, $exit->map && $exit->map->path; 2924 my $map = cf::map::normalise $exit->slaying, $exit->map;
2878 my $x = $exit->stats->hp; 2925 my $x = $exit->stats->hp;
2879 my $y = $exit->stats->sp; 2926 my $y = $exit->stats->sp;
2927
2928 # special map handling
2929 my $slaying = $exit->slaying;
2930
2931 # special map handling
2932 if ($slaying eq "/!") {
2933 my $guard = cf::lock_acquire "exit_prepare:$exit";
2934
2935 prepare_random_map $exit
2936 if $exit->slaying eq "/!"; # need to re-check after getting the lock
2937
2938 $map = $exit->slaying;
2939
2940 } elsif ($slaying eq '!up') {
2941 $map = $exit->map->tile_path (cf::TILE_UP);
2942 $x = $exit->x;
2943 $y = $exit->y;
2944
2945 } elsif ($slaying eq '!down') {
2946 $map = $exit->map->tile_path (cf::TILE_DOWN);
2947 $x = $exit->x;
2948 $y = $exit->y;
2949 }
2880 2950
2881 $self->goto ($map, $x, $y); 2951 $self->goto ($map, $x, $y);
2882 2952
2883 # if exit is damned, update players death & WoR home-position 2953 # if exit is damned, update players death & WoR home-position
2884 $self->contr->savebed ($map, $x, $y) 2954 $self->contr->savebed ($map, $x, $y)
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
3426 3493
3427 cf::face::set_visibility $idx, $info->{visibility}; 3494 cf::face::set_visibility $idx, $info->{visibility};
3428 cf::face::set_magicmap $idx, $info->{magicmap}; 3495 cf::face::set_magicmap $idx, $info->{magicmap};
3429 cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32}; 3496 cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32};
3430 cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64}; 3497 cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64};
3498 cf::face::set_data $idx, 2, $info->{glyph} , $info->{glyph} ;
3431 3499
3432 cf::cede_to_tick; 3500 cf::cede_to_tick;
3433 } 3501 }
3434 3502
3435 while (my ($face, $info) = each %$faces) { 3503 while (my ($face, $info) = each %$faces) {
3530} 3598}
3531 3599
3532sub reload_sound { 3600sub reload_sound {
3533 trace "loading sound config from $DATADIR/sound\n"; 3601 trace "loading sound config from $DATADIR/sound\n";
3534 3602
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); 3603 my $soundconf = JSON::XS->new->utf8->relaxed->decode (load_file "$DATADIR/sound");
3539 3604
3540 for (0 .. SOUND_CAST_SPELL_0 - 1) { 3605 for (0 .. SOUND_CAST_SPELL_0 - 1) {
3541 my $sound = $soundconf->{compat}[$_] 3606 my $sound = $soundconf->{compat}[$_]
3542 or next; 3607 or next;
3543 3608
3567} 3632}
3568 3633
3569sub reload_config { 3634sub reload_config {
3570 trace "reloading config file...\n"; 3635 trace "reloading config file...\n";
3571 3636
3572 0 < aio_load "$CONFDIR/config", my $config 3637 my $config = load_file "$CONFDIR/config";
3573 or die "$CONFDIR/config: $!";
3574
3575 utf8::decode $config; 3638 utf8::decode $config;
3576 *CFG = yaml_load $config; 3639 *CFG = decode_yaml $config;
3577 3640
3578 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; 3641 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38];
3579 3642
3580 $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};
3581 $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};
3619 3682
3620sub main { 3683sub main {
3621 cf::init_globals; # initialise logging 3684 cf::init_globals; # initialise logging
3622 3685
3623 LOG llevInfo, "Welcome to Deliantra, v" . VERSION; 3686 LOG llevInfo, "Welcome to Deliantra, v" . VERSION;
3624 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.";
3625 LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; 3688 LOG llevInfo, "Copyright (C) 1994 Mark Wedel.";
3626 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; 3689 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen.";
3627 3690
3628 $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
3629 3692

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines