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.513 by root, Mon Apr 12 05:22:38 2010 UTC vs.
Revision 1.526 by root, Wed Apr 21 05:48:35 2010 UTC

165 165
166our $EMERGENCY_POSITION; 166our $EMERGENCY_POSITION;
167 167
168sub cf::map::normalise; 168sub cf::map::normalise;
169 169
170sub in_main() {
171 $Coro::current == $Coro::main
172}
173
170############################################################################# 174#############################################################################
171 175
172%REFLECT = (); 176%REFLECT = ();
173for (@REFLECT) { 177for (@REFLECT) {
174 my $reflect = JSON::XS::decode_json $_; 178 my $reflect = JSON::XS::decode_json $_;
261$Coro::State::DIEHOOK = sub { 265$Coro::State::DIEHOOK = sub {
262 return unless $^S eq 0; # "eq", not "==" 266 return unless $^S eq 0; # "eq", not "=="
263 267
264 warn Carp::longmess $_[0]; 268 warn Carp::longmess $_[0];
265 269
266 if ($Coro::current == $Coro::main) {#d# 270 if (in_main) {#d#
267 warn "DIEHOOK called in main context, Coro bug?\n";#d# 271 warn "DIEHOOK called in main context, Coro bug?\n";#d#
268 return;#d# 272 return;#d#
269 }#d# 273 }#d#
270 274
271 # kill coroutine otherwise 275 # kill coroutine otherwise
399} 403}
400 404
401=item cf::periodic $interval, $cb 405=item cf::periodic $interval, $cb
402 406
403Like EV::periodic, but randomly selects a starting point so that the actions 407Like EV::periodic, but randomly selects a starting point so that the actions
404get spread over timer. 408get spread over time.
405 409
406=cut 410=cut
407 411
408sub periodic($$) { 412sub periodic($$) {
409 my ($interval, $cb) = @_; 413 my ($interval, $cb) = @_;
587 reset_signals; 591 reset_signals;
588 &$cb 592 &$cb
589 }, @args; 593 }, @args;
590 594
591 wantarray ? @res : $res[-1] 595 wantarray ? @res : $res[-1]
596}
597
598sub objinfo {
599 (
600 "counter value" => cf::object::object_count,
601 "objects created" => cf::object::create_count,
602 "objects destroyed" => cf::object::destroy_count,
603 "freelist size" => cf::object::free_count,
604 "allocated objects" => cf::object::objects_size,
605 "active objects" => cf::object::actives_size,
606 )
592} 607}
593 608
594=item $coin = coin_from_name $name 609=item $coin = coin_from_name $name
595 610
596=cut 611=cut
1425 if (length $@) { 1440 if (length $@) {
1426 warn "$v->{path}: $@\n"; 1441 warn "$v->{path}: $@\n";
1427 1442
1428 cf::cleanup "mandatory extension '$k' failed to load, exiting." 1443 cf::cleanup "mandatory extension '$k' failed to load, exiting."
1429 if exists $v->{meta}{mandatory}; 1444 if exists $v->{meta}{mandatory};
1445
1446 warn "$v->{base}: optional extension cannot be loaded, skipping.\n";
1447 delete $todo{$k};
1430 } else { 1448 } else {
1431 $done{$k} = delete $todo{$k}; 1449 $done{$k} = delete $todo{$k};
1432 push @EXTS, $v->{pkg}; 1450 push @EXTS, $v->{pkg};
1433 $progress = 1; 1451 $progress = 1;
1434 1452
1677 \@logins 1695 \@logins
1678} 1696}
1679 1697
1680=item $player->maps 1698=item $player->maps
1681 1699
1700=item cf::player::maps $login
1701
1682Returns an arrayref of map paths that are private for this 1702Returns an arrayref of map paths that are private for this
1683player. May block. 1703player. May block.
1684 1704
1685=cut 1705=cut
1686 1706
1747 1767
1748=cut 1768=cut
1749 1769
1750sub find_by_path($) { 1770sub find_by_path($) {
1751 my ($path) = @_; 1771 my ($path) = @_;
1772
1773 $path =~ s/^~[^\/]*//; # skip ~login
1752 1774
1753 my ($match, $specificity); 1775 my ($match, $specificity);
1754 1776
1755 for my $region (list) { 1777 for my $region (list) {
1756 if ($region->{match} && $path =~ $region->{match}) { 1778 if ($region->{match} && $path =~ $region->{match}) {
1820 1842
1821 $EXT_MAP{$pkg} = [$prio, qr<$regex>]; 1843 $EXT_MAP{$pkg} = [$prio, qr<$regex>];
1822} 1844}
1823 1845
1824# also paths starting with '/' 1846# also paths starting with '/'
1825$EXT_MAP{"cf::map"} = [0, qr{^(?=/)}]; 1847$EXT_MAP{"cf::map::wrap"} = [0, qr{^(?=/)}];
1826 1848
1827sub thawer_merge { 1849sub thawer_merge {
1828 my ($self, $merge) = @_; 1850 my ($self, $merge) = @_;
1829 1851
1830 # we have to keep some variables in memory intact 1852 # we have to keep some variables in memory intact
2201 $MAP_PREFETCHER->prio (6); 2223 $MAP_PREFETCHER->prio (6);
2202 2224
2203 () 2225 ()
2204} 2226}
2205 2227
2228# common code, used by both ->save and ->swapout
2206sub save { 2229sub _save {
2207 my ($self) = @_; 2230 my ($self) = @_;
2208
2209 my $lock = cf::lock_acquire "map_data:$self->{path}";
2210 2231
2211 $self->{last_save} = $cf::RUNTIME; 2232 $self->{last_save} = $cf::RUNTIME;
2212 2233
2213 return unless $self->dirty; 2234 return unless $self->dirty;
2214 2235
2234 } else { 2255 } else {
2235 $self->_save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES); 2256 $self->_save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES);
2236 } 2257 }
2237} 2258}
2238 2259
2260sub save {
2261 my ($self) = @_;
2262
2263 my $lock = cf::lock_acquire "map_data:$self->{path}";
2264
2265 $self->_save;
2266}
2267
2239sub swap_out { 2268sub swap_out {
2240 my ($self) = @_; 2269 my ($self) = @_;
2241 2270
2242 # save first because save cedes
2243 $self->save;
2244
2245 my $lock = cf::lock_acquire "map_data:$self->{path}"; 2271 my $lock = cf::lock_acquire "map_data:$self->{path}";
2246 2272
2247 return if $self->players;
2248 return if $self->in_memory != cf::MAP_ACTIVE; 2273 return if $self->in_memory != cf::MAP_ACTIVE;
2249 return if $self->{deny_save}; 2274 return if $self->{deny_save};
2275 return if $self->players;
2250 2276
2277 # first deactivate the map and "unlink" it from the core
2278 $self->deactivate;
2279 $_->clear_links_to ($self) for values %cf::MAP;
2251 $self->in_memory (cf::MAP_SWAPPED); 2280 $self->in_memory (cf::MAP_SWAPPED);
2281
2282 # then atomically save
2283 $self->_save;
2284
2285 # then free the map
2286 $self->clear;
2287}
2288
2289sub reset_at {
2290 my ($self) = @_;
2291
2292 # TODO: safety, remove and allow resettable per-player maps
2293 return 1e99 if $self->{deny_reset};
2294
2295 my $time = $self->fixed_resettime ? $self->{instantiate_time} : $self->last_access;
2296 my $to = List::Util::min $MAX_RESET, $self->reset_timeout || $DEFAULT_RESET;
2297
2298 $time + $to
2299}
2300
2301sub should_reset {
2302 my ($self) = @_;
2303
2304 $self->reset_at <= $cf::RUNTIME
2305}
2306
2307sub reset {
2308 my ($self) = @_;
2309
2310 my $lock = cf::lock_acquire "map_data:$self->{path}";
2311
2312 return if $self->players;
2313
2314 warn "resetting map ", $self->path, "\n";
2315
2316 $self->in_memory (cf::MAP_SWAPPED);
2317
2318 # need to save uniques path
2319 unless ($self->{deny_save}) {
2320 my $uniq = $self->uniq_path; utf8::encode $uniq;
2321
2322 $self->_save_objects ($uniq, cf::IO_UNIQUES)
2323 if $uniq;
2324 }
2325
2326 delete $cf::MAP{$self->path};
2252 2327
2253 $self->deactivate; 2328 $self->deactivate;
2254 $_->clear_links_to ($self) for values %cf::MAP; 2329 $_->clear_links_to ($self) for values %cf::MAP;
2255 $self->clear; 2330 $self->clear;
2256}
2257
2258sub reset_at {
2259 my ($self) = @_;
2260
2261 # TODO: safety, remove and allow resettable per-player maps
2262 return 1e99 if $self->{deny_reset};
2263
2264 my $time = $self->fixed_resettime ? $self->{instantiate_time} : $self->last_access;
2265 my $to = List::Util::min $MAX_RESET, $self->reset_timeout || $DEFAULT_RESET;
2266
2267 $time + $to
2268}
2269
2270sub should_reset {
2271 my ($self) = @_;
2272
2273 $self->reset_at <= $cf::RUNTIME
2274}
2275
2276sub reset {
2277 my ($self) = @_;
2278
2279 my $lock = cf::lock_acquire "map_data:$self->{path}";
2280
2281 return if $self->players;
2282
2283 warn "resetting map ", $self->path, "\n";
2284
2285 $self->in_memory (cf::MAP_SWAPPED);
2286
2287 # need to save uniques path
2288 unless ($self->{deny_save}) {
2289 my $uniq = $self->uniq_path; utf8::encode $uniq;
2290
2291 $self->_save_objects ($uniq, cf::IO_UNIQUES)
2292 if $uniq;
2293 }
2294
2295 delete $cf::MAP{$self->path};
2296
2297 $self->deactivate;
2298 $_->clear_links_to ($self) for values %cf::MAP;
2299 $self->clear;
2300 2331
2301 $self->unlink_save; 2332 $self->unlink_save;
2302 $self->destroy; 2333 $self->destroy;
2303} 2334}
2304 2335
2312 2343
2313 delete $cf::MAP{$self->path}; 2344 delete $cf::MAP{$self->path};
2314 2345
2315 $self->unlink_save; 2346 $self->unlink_save;
2316 2347
2317 bless $self, "cf::map"; 2348 bless $self, "cf::map::wrap";
2318 delete $self->{deny_reset}; 2349 delete $self->{deny_reset};
2319 $self->{deny_save} = 1; 2350 $self->{deny_save} = 1;
2320 $self->reset_timeout (1); 2351 $self->reset_timeout (1);
2321 $self->path ($self->{path} = "{nuke}/" . ($nuke_counter++)); 2352 $self->path ($self->{path} = "{nuke}/" . ($nuke_counter++));
2322 2353
2588 return if UNIVERSAL::isa $self->map, "ext::map_link"; 2619 return if UNIVERSAL::isa $self->map, "ext::map_link";
2589 2620
2590 $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y] 2621 $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y]
2591 if $self->map && $self->map->{path} ne "{link}"; 2622 if $self->map && $self->map->{path} ne "{link}";
2592 2623
2593 $self->enter_map ($LINK_MAP || link_map, 10, 10); 2624 $self->enter_map ($LINK_MAP || link_map, 3, 3);
2594} 2625}
2595 2626
2596sub cf::object::player::leave_link { 2627sub cf::object::player::leave_link {
2597 my ($self, $map, $x, $y) = @_; 2628 my ($self, $map, $x, $y) = @_;
2598 2629
2627 local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext 2658 local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext
2628 $self->enter_map ($map, $x, $y); 2659 $self->enter_map ($map, $x, $y);
2629 2660
2630 # only activate afterwards, to support waiting in hooks 2661 # only activate afterwards, to support waiting in hooks
2631 $self->activate_recursive; 2662 $self->activate_recursive;
2663# unless exists $self->{_link_pos}; # maybe we re in-flight again
2632} 2664}
2633 2665
2634=item $player_object->goto ($path, $x, $y[, $check->($map)[, $done->()]]) 2666=item $player_object->goto ($path, $x, $y[, $check->($map)[, $done->()]])
2635 2667
2636Moves the player to the given map-path and coordinates by first freezing 2668Moves the player to the given map-path and coordinates by first freezing
3532 LOG llevInfo, "Welcome to Deliantra, v" . VERSION; 3564 LOG llevInfo, "Welcome to Deliantra, v" . VERSION;
3533 LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; 3565 LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team.";
3534 LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; 3566 LOG llevInfo, "Copyright (C) 1994 Mark Wedel.";
3535 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; 3567 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen.";
3536 3568
3537 cf::init_experience;
3538 cf::init_anim;
3539 cf::init_attackmess;
3540 cf::init_dynamic;
3541
3542 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority 3569 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
3543 3570
3544 # we must not ever block the main coroutine 3571 # we must not ever block the main coroutine
3545 local $Coro::idle = sub { 3572 local $Coro::idle = sub {
3546 Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# 3573 Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d#
3551 }; 3578 };
3552 3579
3553 evthread_start IO::AIO::poll_fileno; 3580 evthread_start IO::AIO::poll_fileno;
3554 3581
3555 cf::sync_job { 3582 cf::sync_job {
3583 cf::init_experience;
3584 cf::init_anim;
3585 cf::init_attackmess;
3586 cf::init_dynamic;
3587
3556 cf::load_settings; 3588 cf::load_settings;
3557 cf::load_materials; 3589 cf::load_materials;
3558 3590
3559 reload_resources; 3591 reload_resources;
3560 reload_config; 3592 reload_config;
3576 use POSIX (); 3608 use POSIX ();
3577 POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; 3609 POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD};
3578 3610
3579 (pop @POST_INIT)->(0) while @POST_INIT; 3611 (pop @POST_INIT)->(0) while @POST_INIT;
3580 }; 3612 };
3613
3614 cf::object::thawer::errors_are_fatal 0;
3615 warn "parse errors in files are no longer fatal from this point on.\n";
3581 3616
3582 main_loop; 3617 main_loop;
3583} 3618}
3584 3619
3585############################################################################# 3620#############################################################################
3762 my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH}; 3797 my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH};
3763 for my $name (keys %$leaf_symtab) { 3798 for my $name (keys %$leaf_symtab) {
3764 _gv_clear *{"$pkg$name"}; 3799 _gv_clear *{"$pkg$name"};
3765# use PApp::Util; PApp::Util::sv_dump *{"$pkg$name"}; 3800# use PApp::Util; PApp::Util::sv_dump *{"$pkg$name"};
3766 } 3801 }
3767 warn "cleared package $pkg\n";#d#
3768} 3802}
3769 3803
3770sub do_reload_perl() { 3804sub do_reload_perl() {
3771 # can/must only be called in main 3805 # can/must only be called in main
3772 if ($Coro::current != $Coro::main) { 3806 if (in_main) {
3773 warn "can only reload from main coroutine"; 3807 warn "can only reload from main coroutine";
3774 return; 3808 return;
3775 } 3809 }
3776 3810
3777 return if $RELOAD++; 3811 return if $RELOAD++;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines