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.476 by root, Wed Sep 2 16:54:20 2009 UTC vs.
Revision 1.489 by root, Fri Oct 23 03:08:35 2009 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 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 4# Copyright (©) 2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5# 5#
6# Deliantra is free software: you can redistribute it and/or modify 6# Deliantra is free software: you can redistribute it and/or modify it under
7# it under the terms of the GNU General Public License as published by 7# the terms of the Affero GNU General Public License as published by the
8# the Free Software Foundation, either version 3 of the License, or 8# Free Software Foundation, either version 3 of the License, or (at your
9# (at your option) any later version. 9# option) any later version.
10# 10#
11# This program is distributed in the hope that it will be useful, 11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of 12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details. 14# GNU General Public License for more details.
15# 15#
16# You should have received a copy of the GNU General Public License 16# You should have received a copy of the Affero GNU General Public License
17# along with this program. If not, see <http://www.gnu.org/licenses/>. 17# and the GNU General Public License along with this program. If not, see
18# <http://www.gnu.org/licenses/>.
18# 19#
19# 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>
20# 21#
21 22
22package cf; 23package cf;
23 24
24use 5.10.0; 25use 5.10.0;
25use utf8; 26use utf8;
53use JSON::XS 2.01 (); 54use JSON::XS 2.01 ();
54use BDB (); 55use BDB ();
55use Data::Dumper; 56use Data::Dumper;
56use Digest::MD5; 57use Digest::MD5;
57use Fcntl; 58use Fcntl;
58use YAML (); 59use YAML::XS ();
59use IO::AIO (); 60use IO::AIO ();
60use Time::HiRes; 61use Time::HiRes;
61use Compress::LZF; 62use Compress::LZF;
62use Digest::MD5 (); 63use Digest::MD5 ();
63 64
88our %EXT_CORO = (); # coroutines bound to extensions 89our %EXT_CORO = (); # coroutines bound to extensions
89our %EXT_MAP = (); # pluggable maps 90our %EXT_MAP = (); # pluggable maps
90 91
91our $RELOAD; # number of reloads so far, non-zero while in reload 92our $RELOAD; # number of reloads so far, non-zero while in reload
92our @EVENT; 93our @EVENT;
94our @REFLECT; # set by XS
95our %REFLECT; # set by us
93 96
94our $CONFDIR = confdir; 97our $CONFDIR = confdir;
95our $DATADIR = datadir; 98our $DATADIR = datadir;
96our $LIBDIR = "$DATADIR/ext"; 99our $LIBDIR = "$DATADIR/ext";
97our $PODDIR = "$DATADIR/pod"; 100our $PODDIR = "$DATADIR/pod";
115our $BDB_DEADLOCK_WATCHER; 118our $BDB_DEADLOCK_WATCHER;
116our $BDB_CHECKPOINT_WATCHER; 119our $BDB_CHECKPOINT_WATCHER;
117our $BDB_TRICKLE_WATCHER; 120our $BDB_TRICKLE_WATCHER;
118our $DB_ENV; 121our $DB_ENV;
119 122
120our @EXTRA_MODULES = qw(pod mapscript); 123our @EXTRA_MODULES = qw(pod match mapscript);
121 124
122our %CFG; 125our %CFG;
123 126
124our $UPTIME; $UPTIME ||= time; 127our $UPTIME; $UPTIME ||= time;
125our $RUNTIME; 128our $RUNTIME;
161 164
162sub cf::map::normalise; 165sub cf::map::normalise;
163 166
164############################################################################# 167#############################################################################
165 168
169%REFLECT = ();
170for (@REFLECT) {
171 my $reflect = JSON::XS::decode_json $_;
172 $REFLECT{$reflect->{class}} = $reflect;
173}
174
175# this is decidedly evil
176$REFLECT{object}{flags} = { map +($_ => undef), grep $_, map /^FLAG_([A-Z0-9_]+)$/ && lc $1, keys %{"cf::"} };
177
178#############################################################################
179
166=head2 GLOBAL VARIABLES 180=head2 GLOBAL VARIABLES
167 181
168=over 4 182=over 4
169 183
170=item $cf::UPTIME 184=item $cf::UPTIME
214returns directly I<after> the tick processing (and consequently, can only wake one process 228returns directly I<after> the tick processing (and consequently, can only wake one process
215per tick), while cf::wait_for_tick wakes up all waiters after tick processing. 229per tick), while cf::wait_for_tick wakes up all waiters after tick processing.
216 230
217=item @cf::INVOKE_RESULTS 231=item @cf::INVOKE_RESULTS
218 232
219This array contains the results of the last C<invoke ()> call. When 233This array contains the results of the last C<invoke ()> call. When
220C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of 234C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of
221that call. 235that call.
222 236
237=item %cf::REFLECT
238
239Contains, for each (C++) class name, a hash reference with information
240about object members (methods, scalars, arrays and flags) and other
241metadata, which is useful for introspection.
242
223=back 243=back
224 244
225=cut 245=cut
226 246
227BEGIN { 247$Coro::State::WARNHOOK = sub {
228 *CORE::GLOBAL::warn = sub {
229 my $msg = join "", @_; 248 my $msg = join "", @_;
230 249
231 $msg .= "\n" 250 $msg .= "\n"
232 unless $msg =~ /\n$/; 251 unless $msg =~ /\n$/;
233 252
234 $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; 253 $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge;
235 254
236 LOG llevError, $msg; 255 LOG llevError, $msg;
237 }; 256};
238}
239 257
240$Coro::State::DIEHOOK = sub { 258$Coro::State::DIEHOOK = sub {
241 return unless $^S eq 0; # "eq", not "==" 259 return unless $^S eq 0; # "eq", not "=="
260
261 warn Carp::longmess $_[0];
242 262
243 if ($Coro::current == $Coro::main) {#d# 263 if ($Coro::current == $Coro::main) {#d#
244 warn "DIEHOOK called in main context, Coro bug?\n";#d# 264 warn "DIEHOOK called in main context, Coro bug?\n";#d#
245 return;#d# 265 return;#d#
246 }#d# 266 }#d#
247 267
248 # kill coroutine otherwise 268 # kill coroutine otherwise
249 warn Carp::longmess $_[0];
250 Coro::terminate 269 Coro::terminate
251}; 270};
252
253$SIG{__DIE__} = sub { }; #d#?
254 271
255@safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; 272@safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable';
256@safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; 273@safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable';
257@safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable'; 274@safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable';
258@safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable'; 275@safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable';
2223 2240
2224 my $lock = cf::lock_acquire "map_data:$self->{path}"; 2241 my $lock = cf::lock_acquire "map_data:$self->{path}";
2225 2242
2226 return if $self->players; 2243 return if $self->players;
2227 2244
2228 warn "resetting map ", $self->path; 2245 warn "resetting map ", $self->path, "\n";
2229 2246
2230 $self->in_memory (cf::MAP_SWAPPED); 2247 $self->in_memory (cf::MAP_SWAPPED);
2231 2248
2232 # need to save uniques path 2249 # need to save uniques path
2233 unless ($self->{deny_save}) { 2250 unless ($self->{deny_save}) {
2324 : normalise $_ 2341 : normalise $_
2325 } @{ aio_readdir $UNIQUEDIR or [] } 2342 } @{ aio_readdir $UNIQUEDIR or [] }
2326 ] 2343 ]
2327} 2344}
2328 2345
2346=item cf::map::static_maps
2347
2348Returns an arrayref if paths of all static maps (all preinstalled F<.map>
2349file in the shared directory). May block.
2350
2351=cut
2352
2353sub static_maps() {
2354 my @dirs = "";
2355 my @maps;
2356
2357 while (@dirs) {
2358 my $dir = shift @dirs;
2359
2360 my ($dirs, $files) = Coro::AIO::aio_scandir "$MAPDIR$dir", 2
2361 or return;
2362
2363 for (@$files) {
2364 s/\.map$// or next;
2365 utf8::decode $_;
2366 push @maps, "$dir/$_";
2367 }
2368
2369 push @dirs, map "$dir/$_", @$dirs;
2370 }
2371
2372 \@maps
2373}
2374
2329=back 2375=back
2330 2376
2331=head3 cf::object 2377=head3 cf::object
2332 2378
2333=cut 2379=cut
2748 2794
2749=cut 2795=cut
2750 2796
2751# non-persistent channels (usually the info channel) 2797# non-persistent channels (usually the info channel)
2752our %CHANNEL = ( 2798our %CHANNEL = (
2799 "c/motd" => {
2800 id => "infobox",
2801 title => "MOTD",
2802 reply => undef,
2803 tooltip => "The message of the day",
2804 },
2753 "c/identify" => { 2805 "c/identify" => {
2754 id => "infobox", 2806 id => "infobox",
2755 title => "Identify", 2807 title => "Identify",
2756 reply => undef, 2808 reply => undef,
2757 tooltip => "Items recently identified", 2809 tooltip => "Items recently identified",
2759 "c/examine" => { 2811 "c/examine" => {
2760 id => "infobox", 2812 id => "infobox",
2761 title => "Examine", 2813 title => "Examine",
2762 reply => undef, 2814 reply => undef,
2763 tooltip => "Signs and other items you examined", 2815 tooltip => "Signs and other items you examined",
2816 },
2817 "c/shopinfo" => {
2818 id => "infobox",
2819 title => "Shop Info",
2820 reply => undef,
2821 tooltip => "What your bargaining skill tells you about the shop",
2764 }, 2822 },
2765 "c/book" => { 2823 "c/book" => {
2766 id => "infobox", 2824 id => "infobox",
2767 title => "Book", 2825 title => "Book",
2768 reply => undef, 2826 reply => undef,
3366 3424
3367 warn "finished reloading resource files\n"; 3425 warn "finished reloading resource files\n";
3368} 3426}
3369 3427
3370sub reload_config { 3428sub reload_config {
3429 warn "reloading config file...\n";
3430
3371 open my $fh, "<:utf8", "$CONFDIR/config" 3431 open my $fh, "<:utf8", "$CONFDIR/config"
3372 or return; 3432 or return;
3373 3433
3374 local $/; 3434 local $/;
3375 *CFG = YAML::Load <$fh>; 3435 *CFG = YAML::XS::Load scalar <$fh>;
3376 3436
3377 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37]; 3437 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37];
3378 3438
3379 $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; 3439 $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset};
3380 $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; 3440 $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset};
3384 $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" 3444 $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()"
3385 and die "WARNING: m(un)lockall failed: $!\n"; 3445 and die "WARNING: m(un)lockall failed: $!\n";
3386 }; 3446 };
3387 warn $@ if $@; 3447 warn $@ if $@;
3388 } 3448 }
3449
3450 warn "finished reloading resource files\n";
3389} 3451}
3390 3452
3391sub pidfile() { 3453sub pidfile() {
3392 sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT 3454 sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT
3393 or die "$PIDFILE: $!"; 3455 or die "$PIDFILE: $!";
3743 3805
3744 warn "unload completed, starting to reload now"; 3806 warn "unload completed, starting to reload now";
3745 3807
3746 warn "reloading cf.pm"; 3808 warn "reloading cf.pm";
3747 require cf; 3809 require cf;
3748 cf::_connect_to_perl; # nominally unnecessary, but cannot hurt 3810 cf::_connect_to_perl_1;
3749 3811
3750 warn "loading config and database again"; 3812 warn "loading config and database again";
3751 cf::reload_config; 3813 cf::reload_config;
3752 3814
3753 warn "loading extensions"; 3815 warn "loading extensions";
3961 } 4023 }
3962} 4024}
3963 4025
3964# load additional modules 4026# load additional modules
3965require "cf/$_.pm" for @EXTRA_MODULES; 4027require "cf/$_.pm" for @EXTRA_MODULES;
4028cf::_connect_to_perl_2;
3966 4029
3967END { cf::emergency_save } 4030END { cf::emergency_save }
3968 4031
39691 40321
3970 4033

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines