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.548 by root, Mon May 10 21:40:22 2010 UTC vs.
Revision 1.559 by root, Sat Feb 26 12:50:27 2011 UTC

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";
381 383
382=cut 384=cut
383 385
384sub post_init(&) { 386sub post_init(&) {
385 push @POST_INIT, shift; 387 push @POST_INIT, shift;
388}
389
390sub _post_init {
391 trace "running post_init jobs";
392
393 # run them in parallel...
394
395 my @join;
396
397 while () {
398 push @join, map &Coro::async ($_, 0), @POST_INIT;
399 @POST_INIT = ();
400
401 @join or last;
402
403 (pop @join)->join;
404 }
386} 405}
387 406
388=item cf::lock_wait $string 407=item cf::lock_wait $string
389 408
390Wait until the given lock is available. See cf::lock_acquire. 409Wait until the given lock is available. See cf::lock_acquire.
1662 my $name = $pl->ob->name; 1681 my $name = $pl->ob->name;
1663 1682
1664 $pl->{deny_save} = 1; 1683 $pl->{deny_save} = 1;
1665 $pl->password ("*"); # this should lock out the player until we have nuked the dir 1684 $pl->password ("*"); # this should lock out the player until we have nuked the dir
1666 1685
1667 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; 1686 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->ns;
1668 $pl->deactivate; 1687 $pl->deactivate;
1669 1688
1670 my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy; 1689 my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy;
1690 $pl->invoke (cf::EVENT_PLAYER_QUIT) if $pl->ns;
1671 ext::highscore::check ($pl->ob); 1691 ext::highscore::check ($pl->ob);
1672 1692
1673 $pl->invoke (cf::EVENT_PLAYER_QUIT);
1674 $pl->ns->destroy if $pl->ns; 1693 $pl->ns->destroy if $pl->ns;
1675 1694
1676 my $path = playerdir $pl; 1695 my $path = playerdir $pl;
1677 my $temp = "$path~$cf::RUNTIME~deleting~"; 1696 my $temp = "$path~$cf::RUNTIME~deleting~";
1678 aio_rename $path, $temp; 1697 aio_rename $path, $temp;
1844sub generate_random_map { 1863sub generate_random_map {
1845 my ($self, $rmp) = @_; 1864 my ($self, $rmp) = @_;
1846 1865
1847 my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM 1866 my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM
1848 1867
1849 # mit "rum" bekleckern, nicht
1850 $self->_create_random_map ( 1868 $self->_create_random_map ($rmp);
1851 $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle},
1852 $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, $rmp->{miningstyle},
1853 $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map},
1854 $rmp->{exit_on_final_map},
1855 $rmp->{xsize}, $rmp->{ysize},
1856 $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3},
1857 $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase},
1858 $rmp->{dungeon_level}, $rmp->{dungeon_depth}, $rmp->{decoroptions}, $rmp->{orientation},
1859 $rmp->{origin_y}, $rmp->{origin_x}, $rmp->{random_seed}, $rmp->{total_map_hp},
1860 $rmp->{map_layout_style}, $rmp->{treasureoptions}, $rmp->{symmetry_used},
1861 (cf::region::find $rmp->{region}), $rmp->{custom}
1862 )
1863} 1869}
1864 1870
1865=item cf::map->register ($regex, $prio) 1871=item cf::map->register ($regex, $prio)
1866 1872
1867Register a handler for the map path matching the given regex at the 1873Register a handler for the map path matching the given regex at the
1918 $base =~ s{[^/]+/?$}{}; 1924 $base =~ s{[^/]+/?$}{};
1919 $path = "$base/$path"; 1925 $path = "$base/$path";
1920 } 1926 }
1921 1927
1922 for ($path) { 1928 for ($path) {
1923 redo if s{//}{/};
1924 redo if s{/\.?/}{/}; 1929 redo if s{/\.?/}{/};
1925 redo if s{/[^/]+/\.\./}{/}; 1930 redo if s{/[^/]+/\.\./}{/};
1926 } 1931 }
1927 1932
1928 $path 1933 $path
2170 2175
2171 $self->in_memory (cf::MAP_ACTIVE); 2176 $self->in_memory (cf::MAP_ACTIVE);
2172 } 2177 }
2173 2178
2174 $self->post_load; 2179 $self->post_load;
2180
2181 1
2175} 2182}
2176 2183
2177# customize the map for a given player, i.e. 2184# customize the map for a given player, i.e.
2178# return the _real_ map. used by e.g. per-player 2185# return the _real_ map. used by e.g. per-player
2179# maps to change the path to ~playername/mappath 2186# maps to change the path to ~playername/mappath
3421 3428
3422 cf::face::set_visibility $idx, $info->{visibility}; 3429 cf::face::set_visibility $idx, $info->{visibility};
3423 cf::face::set_magicmap $idx, $info->{magicmap}; 3430 cf::face::set_magicmap $idx, $info->{magicmap};
3424 cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32}; 3431 cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32};
3425 cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64}; 3432 cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64};
3433 cf::face::set_data $idx, 2, $info->{glyph} , $info->{glyph} ;
3426 3434
3427 cf::cede_to_tick; 3435 cf::cede_to_tick;
3428 } 3436 }
3429 3437
3430 while (my ($face, $info) = each %$faces) { 3438 while (my ($face, $info) = each %$faces) {
3660 3668
3661 # no (long-running) fork's whatsoever before this point(!) 3669 # no (long-running) fork's whatsoever before this point(!)
3662 use POSIX (); 3670 use POSIX ();
3663 POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; 3671 POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD};
3664 3672
3665 (pop @POST_INIT)->(0) while @POST_INIT; 3673 cf::_post_init 0;
3666 }; 3674 };
3667 3675
3668 cf::object::thawer::errors_are_fatal 0; 3676 cf::object::thawer::errors_are_fatal 0;
3669 info "parse errors in files are no longer fatal from this point on.\n"; 3677 info "parse errors in files are no longer fatal from this point on.\n";
3670 3678
3893 3901
3894 my $t1 = AE::time; 3902 my $t1 = AE::time;
3895 3903
3896 while ($RELOAD) { 3904 while ($RELOAD) {
3897 cf::get_slot 0.1, -1, "reload_perl"; 3905 cf::get_slot 0.1, -1, "reload_perl";
3898 info "reloading..."; 3906 info "perl_reload: reloading...";
3899 3907
3900 trace "entering sync_job"; 3908 trace "perl_reload: entering sync_job";
3901 3909
3902 cf::sync_job { 3910 cf::sync_job {
3903 #cf::emergency_save; 3911 #cf::emergency_save;
3904 3912
3905 trace "cancelling all extension coros"; 3913 trace "perl_reload: cancelling all extension coros";
3906 $_->cancel for values %EXT_CORO; 3914 $_->cancel for values %EXT_CORO;
3907 %EXT_CORO = (); 3915 %EXT_CORO = ();
3908 3916
3909 trace "removing commands"; 3917 trace "perl_reload: removing commands";
3910 %COMMAND = (); 3918 %COMMAND = ();
3911 3919
3912 trace "removing ext/exti commands"; 3920 trace "perl_reload: removing ext/exti commands";
3913 %EXTCMD = (); 3921 %EXTCMD = ();
3914 %EXTICMD = (); 3922 %EXTICMD = ();
3915 3923
3916 trace "unloading/nuking all extensions"; 3924 trace "perl_reload: unloading/nuking all extensions";
3917 for my $pkg (@EXTS) { 3925 for my $pkg (@EXTS) {
3918 trace "... unloading $pkg"; 3926 trace "... unloading $pkg";
3919 3927
3920 if (my $cb = $pkg->can ("unload")) { 3928 if (my $cb = $pkg->can ("unload")) {
3921 eval { 3929 eval {
3926 3934
3927 trace "... clearing $pkg"; 3935 trace "... clearing $pkg";
3928 clear_package $pkg; 3936 clear_package $pkg;
3929 } 3937 }
3930 3938
3931 trace "unloading all perl modules loaded from $LIBDIR"; 3939 trace "perl_reload: unloading all perl modules loaded from $LIBDIR";
3932 while (my ($k, $v) = each %INC) { 3940 while (my ($k, $v) = each %INC) {
3933 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; 3941 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
3934 3942
3935 trace "... unloading $k"; 3943 trace "... unloading $k";
3936 delete $INC{$k}; 3944 delete $INC{$k};
3943 } 3951 }
3944 3952
3945 clear_package $k; 3953 clear_package $k;
3946 } 3954 }
3947 3955
3948 trace "getting rid of safe::, as good as possible"; 3956 trace "perl_reload: getting rid of safe::, as good as possible";
3949 clear_package "safe::$_" 3957 clear_package "safe::$_"
3950 for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); 3958 for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region);
3951 3959
3952 trace "unloading cf.pm \"a bit\""; 3960 trace "perl_reload: unloading cf.pm \"a bit\"";
3953 delete $INC{"cf.pm"}; 3961 delete $INC{"cf.pm"};
3954 delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; 3962 delete $INC{"cf/$_.pm"} for @EXTRA_MODULES;
3955 3963
3956 # don't, removes xs symbols, too, 3964 # don't, removes xs symbols, too,
3957 # and global variables created in xs 3965 # and global variables created in xs
3958 #clear_package __PACKAGE__; 3966 #clear_package __PACKAGE__;
3959 3967
3960 info "unload completed, starting to reload now"; 3968 info "perl_reload: unload completed, starting to reload now";
3961 3969
3962 trace "reloading cf.pm"; 3970 trace "perl_reload: reloading cf.pm";
3963 require cf; 3971 require cf;
3964 cf::_connect_to_perl_1; 3972 cf::_connect_to_perl_1;
3965 3973
3966 trace "loading config and database again"; 3974 trace "perl_reload: loading config and database again";
3967 cf::reload_config; 3975 cf::reload_config;
3968 3976
3969 trace "loading extensions"; 3977 trace "perl_reload: loading extensions";
3970 cf::load_extensions; 3978 cf::load_extensions;
3971 3979
3972 if ($REATTACH_ON_RELOAD) { 3980 if ($REATTACH_ON_RELOAD) {
3973 trace "reattaching attachments to objects/players"; 3981 trace "perl_reload: reattaching attachments to objects/players";
3974 _global_reattach; # objects, sockets 3982 _global_reattach; # objects, sockets
3975 trace "reattaching attachments to maps"; 3983 trace "perl_reload: reattaching attachments to maps";
3976 reattach $_ for values %MAP; 3984 reattach $_ for values %MAP;
3977 trace "reattaching attachments to players"; 3985 trace "perl_reload: reattaching attachments to players";
3978 reattach $_ for values %PLAYER; 3986 reattach $_ for values %PLAYER;
3979 } 3987 }
3980 3988
3981 trace "running post_init jobs"; 3989 cf::_post_init 1;
3982 (pop @POST_INIT)->(1) while @POST_INIT;
3983 3990
3984 trace "leaving sync_job"; 3991 trace "perl_reload: leaving sync_job";
3985 3992
3986 1 3993 1
3987 } or do { 3994 } or do {
3988 error $@; 3995 error $@;
3989 cf::cleanup "error while reloading, exiting."; 3996 cf::cleanup "perl_reload: error, exiting.";
3990 }; 3997 };
3991 3998
3992 info "reloaded";
3993 --$RELOAD; 3999 --$RELOAD;
3994 } 4000 }
3995 4001
3996 $t1 = AE::time - $t1; 4002 $t1 = AE::time - $t1;
3997 info "reload completed in ${t1}s\n"; 4003 info "perl_reload: completed in ${t1}s\n";
3998}; 4004};
3999 4005
4000our $RELOAD_WATCHER; # used only during reload 4006our $RELOAD_WATCHER; # used only during reload
4001 4007
4002sub reload_perl() { 4008sub reload_perl() {
4135 IO::AIO::min_parallel 8; 4141 IO::AIO::min_parallel 8;
4136 IO::AIO::max_poll_time $TICK * 0.1; 4142 IO::AIO::max_poll_time $TICK * 0.1;
4137 undef $AnyEvent::AIO::WATCHER; 4143 undef $AnyEvent::AIO::WATCHER;
4138} 4144}
4139 4145
4140my $_log_backtrace; 4146our $_log_backtrace;
4147our $_log_backtrace_last;
4141 4148
4142sub _log_backtrace { 4149sub _log_backtrace {
4143 my ($msg, @addr) = @_; 4150 my ($msg, @addr) = @_;
4144 4151
4145 $msg =~ s/\n//; 4152 $msg =~ s/\n$//;
4146 4153
4154 if ($_log_backtrace_last eq $msg) {
4155 LOG llevInfo, "[ABT] $msg\n";
4156 LOG llevInfo, "[ABT] [duplicate, suppressed]\n";
4147 # limit the # of concurrent backtraces 4157 # limit the # of concurrent backtraces
4148 if ($_log_backtrace < 2) { 4158 } elsif ($_log_backtrace < 2) {
4159 $_log_backtrace_last = $msg;
4149 ++$_log_backtrace; 4160 ++$_log_backtrace;
4150 my $perl_bt = Carp::longmess $msg; 4161 my $perl_bt = Carp::longmess $msg;
4151 async { 4162 async {
4152 $Coro::current->{desc} = "abt $msg"; 4163 $Coro::current->{desc} = "abt $msg";
4153 4164
4173 LOG llevInfo, "[ABT] $_\n" for @bt; 4184 LOG llevInfo, "[ABT] $_\n" for @bt;
4174 --$_log_backtrace; 4185 --$_log_backtrace;
4175 }; 4186 };
4176 } else { 4187 } else {
4177 LOG llevInfo, "[ABT] $msg\n"; 4188 LOG llevInfo, "[ABT] $msg\n";
4178 LOG llevInfo, "[ABT] [suppressed]\n"; 4189 LOG llevInfo, "[ABT] [overload, suppressed]\n";
4179 } 4190 }
4180} 4191}
4181 4192
4182# load additional modules 4193# load additional modules
4183require "cf/$_.pm" for @EXTRA_MODULES; 4194require "cf/$_.pm" for @EXTRA_MODULES;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines