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.536 by root, Thu Apr 29 08:26:38 2010 UTC vs.
Revision 1.547 by root, Sun May 9 22:51:13 2010 UTC

20# The authors can be reached via e-mail to <support@deliantra.net> 20# The authors can be reached via e-mail to <support@deliantra.net>
21# 21#
22 22
23package cf; 23package cf;
24 24
25use 5.10.0; 25use common::sense;
26use utf8;
27use strict qw(vars subs);
28 26
29use Symbol; 27use Symbol;
30use List::Util; 28use List::Util;
31use Socket; 29use Socket;
32use EV; 30use EV;
78# strictly for debugging 76# strictly for debugging
79$SIG{QUIT} = sub { Carp::cluck "SIGQUIT" }; 77$SIG{QUIT} = sub { Carp::cluck "SIGQUIT" };
80 78
81sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload 79sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload
82 80
81our @ORIG_INC;
82
83our %COMMAND = (); 83our %COMMAND = ();
84our %COMMAND_TIME = (); 84our %COMMAND_TIME = ();
85 85
86our @EXTS = (); # list of extension package names 86our @EXTS = (); # list of extension package names
87our %EXTCMD = (); 87our %EXTCMD = ();
124our $BDB_DEADLOCK_WATCHER; 124our $BDB_DEADLOCK_WATCHER;
125our $BDB_CHECKPOINT_WATCHER; 125our $BDB_CHECKPOINT_WATCHER;
126our $BDB_TRICKLE_WATCHER; 126our $BDB_TRICKLE_WATCHER;
127our $DB_ENV; 127our $DB_ENV;
128 128
129our @EXTRA_MODULES = qw(pod match mapscript); 129our @EXTRA_MODULES = qw(pod match mapscript incloader);
130 130
131our %CFG; 131our %CFG;
132 132
133our $UPTIME; $UPTIME ||= time; 133our $UPTIME; $UPTIME ||= time;
134our $RUNTIME; 134our $RUNTIME;
233from wherever your confdir points to. 233from wherever your confdir points to.
234 234
235=item cf::wait_for_tick, cf::wait_for_tick_begin 235=item cf::wait_for_tick, cf::wait_for_tick_begin
236 236
237These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only 237These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only
238returns directly I<after> the tick processing (and consequently, can only wake one process 238returns directly I<after> the tick processing (and consequently, can only wake one thread
239per tick), while cf::wait_for_tick wakes up all waiters after tick processing. 239per tick), while cf::wait_for_tick wakes up all waiters after tick processing.
240
241=cut
242
243sub wait_for_tick();
244sub wait_for_tick_begin();
240 245
241=item @cf::INVOKE_RESULTS 246=item @cf::INVOKE_RESULTS
242 247
243This array contains the results of the last C<invoke ()> call. When 248This array contains the results of the last C<invoke ()> call. When
244C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of 249C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of
587Coro::Storable. May, of course, block. Note that the executed sub may 592Coro::Storable. May, of course, block. Note that the executed sub may
588never block itself or use any form of event handling. 593never block itself or use any form of event handling.
589 594
590=cut 595=cut
591 596
597sub post_fork {
598 reset_signals;
599}
600
592sub fork_call(&@) { 601sub fork_call(&@) {
593 my ($cb, @args) = @_; 602 my ($cb, @args) = @_;
594 603
595 # we seemingly have to make a local copy of the whole thing, 604 # we seemingly have to make a local copy of the whole thing,
596 # otherwise perl prematurely frees the stuff :/ 605 # otherwise perl prematurely frees the stuff :/
597 # TODO: investigate and fix (likely this will be rather laborious) 606 # TODO: investigate and fix (likely this will be rather laborious)
598 607
599 my @res = Coro::Util::fork_eval { 608 my @res = Coro::Util::fork_eval {
600 reset_signals; 609 cf::post_fork;
601 &$cb 610 &$cb
602 }, @args; 611 }, @args;
603 612
604 wantarray ? @res : $res[-1] 613 wantarray ? @res : $res[-1]
605} 614}
1423 1432
1424 $ext{meta} = { map { (split /=/, $_, 2)[0, 1] } split /\s+/, $1 } 1433 $ext{meta} = { map { (split /=/, $_, 2)[0, 1] } split /\s+/, $1 }
1425 if $source =~ /\A#!.*?perl.*?#\s*(.*)$/m; 1434 if $source =~ /\A#!.*?perl.*?#\s*(.*)$/m;
1426 1435
1427 $ext{source} = 1436 $ext{source} =
1428 "package $pkg; use 5.10.0; use strict 'vars', 'subs'; use utf8;\n" 1437 "package $pkg; use common::sense;\n"
1429 . "#line 1 \"$path\"\n{\n" 1438 . "#line 1 \"$path\"\n{\n"
1430 . $source 1439 . $source
1431 . "\n};\n1"; 1440 . "\n};\n1";
1432 1441
1433 $todo{$base} = \%ext; 1442 $todo{$base} = \%ext;
1643 $pl->{deny_save} = 1; 1652 $pl->{deny_save} = 1;
1644 $pl->password ("*"); # this should lock out the player until we have nuked the dir 1653 $pl->password ("*"); # this should lock out the player until we have nuked the dir
1645 1654
1646 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; 1655 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active;
1647 $pl->deactivate; 1656 $pl->deactivate;
1657
1648 my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy; 1658 my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy;
1659 ext::highscore::check ($pl->ob);
1660
1649 $pl->invoke (cf::EVENT_PLAYER_QUIT); 1661 $pl->invoke (cf::EVENT_PLAYER_QUIT);
1650 $pl->ns->destroy if $pl->ns; 1662 $pl->ns->destroy if $pl->ns;
1651 1663
1652 my $path = playerdir $pl; 1664 my $path = playerdir $pl;
1653 my $temp = "$path~$cf::RUNTIME~deleting~"; 1665 my $temp = "$path~$cf::RUNTIME~deleting~";
1848 1860
1849sub register { 1861sub register {
1850 my (undef, $regex, $prio) = @_; 1862 my (undef, $regex, $prio) = @_;
1851 my $pkg = caller; 1863 my $pkg = caller;
1852 1864
1853 no strict;
1854 push @{"$pkg\::ISA"}, __PACKAGE__; 1865 push @{"$pkg\::ISA"}, __PACKAGE__;
1855 1866
1856 $EXT_MAP{$pkg} = [$prio, qr<$regex>]; 1867 $EXT_MAP{$pkg} = [$prio, qr<$regex>];
1857} 1868}
1858 1869
1870} 1881}
1871 1882
1872sub normalise { 1883sub normalise {
1873 my ($path, $base) = @_; 1884 my ($path, $base) = @_;
1874 1885
1875 $path = "$path"; # make sure its a string 1886 $path = "$path"; # make sure it's a string
1876 1887
1877 $path =~ s/\.map$//; 1888 $path =~ s/\.map$//;
1878 1889
1879 # map plan: 1890 # map plan:
1880 # 1891 #
1919 $self->init; # pass $1 etc. 1930 $self->init; # pass $1 etc.
1920 return $self; 1931 return $self;
1921 } 1932 }
1922 } 1933 }
1923 1934
1924 Carp::cluck "unable to resolve path '$path' (base '$base')."; 1935 Carp::cluck "unable to resolve path '$path' (base '$base')";
1925 () 1936 ()
1926} 1937}
1927 1938
1928sub init { 1939sub init {
1929 my ($self) = @_; 1940 my ($self) = @_;
2051} 2062}
2052 2063
2053sub find; 2064sub find;
2054sub find { 2065sub find {
2055 my ($path, $origin) = @_; 2066 my ($path, $origin) = @_;
2067
2068 cf::cede_to_tick;
2056 2069
2057 $path = normalise $path, $origin && $origin->path; 2070 $path = normalise $path, $origin && $origin->path;
2058 2071
2059 my $guard1 = cf::lock_acquire "map_data:$path";#d#remove 2072 my $guard1 = cf::lock_acquire "map_data:$path";#d#remove
2060 my $guard2 = cf::lock_acquire "map_find:$path"; 2073 my $guard2 = cf::lock_acquire "map_find:$path";
3273 decrease split destroy change_exp value msg lore send_msg)], 3286 decrease split destroy change_exp value msg lore send_msg)],
3274 ["cf::object::player" => qw(player)], 3287 ["cf::object::player" => qw(player)],
3275 ["cf::player" => qw(peaceful send_msg)], 3288 ["cf::player" => qw(peaceful send_msg)],
3276 ["cf::map" => qw(trigger)], 3289 ["cf::map" => qw(trigger)],
3277) { 3290) {
3278 no strict 'refs';
3279 my ($pkg, @funs) = @$_; 3291 my ($pkg, @funs) = @$_;
3280 *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) 3292 *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"})
3281 for @funs; 3293 for @funs;
3282} 3294}
3283 3295
3370 3382
3371 my $enc = JSON::XS->new->utf8->canonical->relaxed; 3383 my $enc = JSON::XS->new->utf8->canonical->relaxed;
3372 3384
3373 trace "loading facedata from $path\n"; 3385 trace "loading facedata from $path\n";
3374 3386
3375 my $facedata;
3376 0 < aio_load $path, $facedata 3387 0 < aio_load $path, my $facedata
3377 or die "$path: $!"; 3388 or die "$path: $!";
3378 3389
3379 $facedata = Coro::Storable::thaw $facedata; 3390 $facedata = Coro::Storable::thaw $facedata;
3380 3391
3381 $facedata->{version} == 2 3392 $facedata->{version} == 2
3525} 3536}
3526 3537
3527sub reload_resources { 3538sub reload_resources {
3528 trace "reloading resource files...\n"; 3539 trace "reloading resource files...\n";
3529 3540
3541 reload_exp_table;
3542 reload_materials;
3530 reload_facedata; 3543 reload_facedata;
3531 reload_sound; 3544 reload_sound;
3532 reload_archetypes; 3545 reload_archetypes;
3533 reload_regions; 3546 reload_regions;
3534 reload_treasures; 3547 reload_treasures;
3537} 3550}
3538 3551
3539sub reload_config { 3552sub reload_config {
3540 trace "reloading config file...\n"; 3553 trace "reloading config file...\n";
3541 3554
3542 open my $fh, "<:utf8", "$CONFDIR/config" 3555 0 < aio_load "$CONFDIR/config", my $config
3543 or return; 3556 or die "$CONFDIR/config: $!";
3544 3557
3545 local $/; 3558 utf8::decode $config;
3559
3560 cf::get_slot 0.1, 10, "reload_config"; # yaml might be slow...
3546 *CFG = YAML::XS::Load scalar <$fh>; 3561 *CFG = YAML::XS::Load $config;
3547 3562
3548 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; 3563 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38];
3549 3564
3550 $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; 3565 $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset};
3551 $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; 3566 $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset};
3555 $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" 3570 $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()"
3556 and die "WARNING: m(un)lockall failed: $!\n"; 3571 and die "WARNING: m(un)lockall failed: $!\n";
3557 }; 3572 };
3558 warn $@ if $@; 3573 warn $@ if $@;
3559 } 3574 }
3560
3561 trace "finished reloading resource files\n";
3562} 3575}
3563 3576
3564sub pidfile() { 3577sub pidfile() {
3565 sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT 3578 sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT
3566 or die "$PIDFILE: $!"; 3579 or die "$PIDFILE: $!";
3591 3604
3592sub main { 3605sub main {
3593 cf::init_globals; # initialise logging 3606 cf::init_globals; # initialise logging
3594 3607
3595 LOG llevInfo, "Welcome to Deliantra, v" . VERSION; 3608 LOG llevInfo, "Welcome to Deliantra, v" . VERSION;
3596 LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; 3609 LOG llevInfo, "Copyright (C) 2005-2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team.";
3597 LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; 3610 LOG llevInfo, "Copyright (C) 1994 Mark Wedel.";
3598 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; 3611 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen.";
3599 3612
3600 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority 3613 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
3601 3614
3609 }; 3622 };
3610 3623
3611 evthread_start IO::AIO::poll_fileno; 3624 evthread_start IO::AIO::poll_fileno;
3612 3625
3613 cf::sync_job { 3626 cf::sync_job {
3614 cf::init_experience; 3627 cf::incloader::init ();
3628
3615 cf::init_anim; 3629 cf::init_anim;
3616 cf::init_attackmess; 3630 cf::init_attackmess;
3617 cf::init_dynamic; 3631 cf::init_dynamic;
3618 3632
3619 cf::load_settings; 3633 cf::load_settings;
3620 cf::load_materials;
3621 3634
3622 reload_resources; 3635 reload_resources;
3623 reload_config; 3636 reload_config;
3624 db_init; 3637 db_init;
3625 3638
3643 }; 3656 };
3644 3657
3645 cf::object::thawer::errors_are_fatal 0; 3658 cf::object::thawer::errors_are_fatal 0;
3646 info "parse errors in files are no longer fatal from this point on.\n"; 3659 info "parse errors in files are no longer fatal from this point on.\n";
3647 3660
3661 my $free_main; $free_main = EV::idle sub {
3662 undef $free_main;
3663 undef &main; # free gobs of memory :)
3664 };
3665
3648 main_loop; 3666 goto &main_loop;
3649} 3667}
3650 3668
3651############################################################################# 3669#############################################################################
3652# initialisation and cleanup 3670# initialisation and cleanup
3653 3671
3820sub post_cleanup { 3838sub post_cleanup {
3821 my ($make_core) = @_; 3839 my ($make_core) = @_;
3822 3840
3823 IO::AIO::flush; 3841 IO::AIO::flush;
3824 3842
3825 IO::AIO::flush;
3826
3827 error Carp::longmess "post_cleanup backtrace" 3843 error Carp::longmess "post_cleanup backtrace"
3828 if $make_core; 3844 if $make_core;
3829 3845
3830 my $fh = pidfile; 3846 my $fh = pidfile;
3831 unlink $PIDFILE if <$fh> == $$; 3847 unlink $PIDFILE if <$fh> == $$;
3856 } 3872 }
3857} 3873}
3858 3874
3859sub do_reload_perl() { 3875sub do_reload_perl() {
3860 # can/must only be called in main 3876 # can/must only be called in main
3861 if (in_main) { 3877 unless (in_main) {
3862 error "can only reload from main coroutine"; 3878 error "can only reload from main coroutine";
3863 return; 3879 return;
3864 } 3880 }
3865 3881
3866 return if $RELOAD++; 3882 return if $RELOAD++;
3867 3883
3868 my $t1 = AE::time; 3884 my $t1 = AE::time;
3869 3885
3870 while ($RELOAD) { 3886 while ($RELOAD) {
3887 cf::get_slot 0.1, -1, "reload_perl";
3871 info "reloading..."; 3888 info "reloading...";
3872 3889
3873 trace "entering sync_job"; 3890 trace "entering sync_job";
3874 3891
3875 cf::sync_job { 3892 cf::sync_job {
3876 cf::emergency_save; 3893 #cf::emergency_save;
3877 3894
3878 trace "cancelling all extension coros"; 3895 trace "cancelling all extension coros";
3879 $_->cancel for values %EXT_CORO; 3896 $_->cancel for values %EXT_CORO;
3880 %EXT_CORO = (); 3897 %EXT_CORO = ();
3881 3898
3996 reload_perl; 4013 reload_perl;
3997 }; 4014 };
3998 } 4015 }
3999}; 4016};
4000 4017
4001unshift @INC, $LIBDIR; 4018#############################################################################
4002 4019
4003my $bug_warning = 0; 4020my $bug_warning = 0;
4004 4021
4005our @WAIT_FOR_TICK; 4022our @WAIT_FOR_TICK;
4006our @WAIT_FOR_TICK_BEGIN; 4023our @WAIT_FOR_TICK_BEGIN;
4007 4024
4008sub wait_for_tick { 4025sub wait_for_tick() {
4009 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; 4026 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main;
4010 4027
4011 my $signal = new Coro::Signal; 4028 my $signal = new Coro::Signal;
4012 push @WAIT_FOR_TICK, $signal; 4029 push @WAIT_FOR_TICK, $signal;
4013 $signal->wait; 4030 $signal->wait;
4014} 4031}
4015 4032
4016sub wait_for_tick_begin { 4033sub wait_for_tick_begin() {
4017 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; 4034 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main;
4018 4035
4019 my $signal = new Coro::Signal; 4036 my $signal = new Coro::Signal;
4020 push @WAIT_FOR_TICK_BEGIN, $signal; 4037 push @WAIT_FOR_TICK_BEGIN, $signal;
4021 $signal->wait; 4038 $signal->wait;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines