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.403 by root, Mon Dec 17 07:09:18 2007 UTC vs.
Revision 1.410 by root, Tue Jan 15 12:02:24 2008 UTC

4use strict; 4use strict;
5 5
6use Symbol; 6use Symbol;
7use List::Util; 7use List::Util;
8use Socket; 8use Socket;
9use EV; 9use EV 1.86;
10use Opcode; 10use Opcode;
11use Safe; 11use Safe;
12use Safe::Hole; 12use Safe::Hole;
13use Storable (); 13use Storable ();
14 14
27use JSON::XS 2.01 (); 27use JSON::XS 2.01 ();
28use BDB (); 28use BDB ();
29use Data::Dumper; 29use Data::Dumper;
30use Digest::MD5; 30use Digest::MD5;
31use Fcntl; 31use Fcntl;
32use YAML::Syck (); 32use YAML ();
33use IO::AIO 2.51 (); 33use IO::AIO 2.51 ();
34use Time::HiRes; 34use Time::HiRes;
35use Compress::LZF; 35use Compress::LZF;
36use Digest::MD5 (); 36use Digest::MD5 ();
37 37
38# configure various modules to our taste 38# configure various modules to our taste
39# 39#
40$Storable::canonical = 1; # reduce rsync transfers 40$Storable::canonical = 1; # reduce rsync transfers
41Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator 41Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator
42Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later 42Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later
43
44# work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode?
45$YAML::Syck::ImplicitUnicode = 1;
46 43
47$Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority 44$Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority
48 45
49sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload 46sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload
50 47
69our $TMPDIR = "$LOCALDIR/" . tmpdir; 66our $TMPDIR = "$LOCALDIR/" . tmpdir;
70our $UNIQUEDIR = "$LOCALDIR/" . uniquedir; 67our $UNIQUEDIR = "$LOCALDIR/" . uniquedir;
71our $PLAYERDIR = "$LOCALDIR/" . playerdir; 68our $PLAYERDIR = "$LOCALDIR/" . playerdir;
72our $RANDOMDIR = "$LOCALDIR/random"; 69our $RANDOMDIR = "$LOCALDIR/random";
73our $BDBDIR = "$LOCALDIR/db"; 70our $BDBDIR = "$LOCALDIR/db";
71our %RESOURCE;
74 72
75our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) 73our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!)
76our $TICK_WATCHER; 74our $TICK_WATCHER;
77our $AIO_POLL_WATCHER; 75our $AIO_POLL_WATCHER;
78our $NEXT_RUNTIME_WRITE; # when should the runtime file be written 76our $NEXT_RUNTIME_WRITE; # when should the runtime file be written
192 $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; 190 $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge;
193 191
194 LOG llevError, $msg; 192 LOG llevError, $msg;
195 }; 193 };
196} 194}
195
196$Coro::State::DIEHOOK = sub {
197 return unless $^S eq 0; # "eq", not "=="
198
199 if ($Coro::current == $Coro::main) {#d#
200 warn "DIEHOOK called in main context, Coro bug?\n";#d#
201 return;#d#
202 }#d#
203
204 # kill coroutine otherwise
205 warn Carp::longmess $_[0];
206 Coro::terminate
207};
208
209$SIG{__DIE__} = sub { }; #d#?
197 210
198@safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; 211@safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable';
199@safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; 212@safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable';
200@safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable'; 213@safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable';
201@safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable'; 214@safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable';
968 } 981 }
969 982
970 0 983 0
971} 984}
972 985
973=item $bool = cf::global::invoke (EVENT_CLASS_XXX, ...) 986=item $bool = cf::global->invoke (EVENT_CLASS_XXX, ...)
974 987
975=item $bool = $attachable->invoke (EVENT_CLASS_XXX, ...) 988=item $bool = $attachable->invoke (EVENT_CLASS_XXX, ...)
976 989
977Generate an object-specific event with the given arguments. 990Generate an object-specific event with the given arguments.
978 991
3149 { 3162 {
3150 my $faces = $facedata->{faceinfo}; 3163 my $faces = $facedata->{faceinfo};
3151 3164
3152 while (my ($face, $info) = each %$faces) { 3165 while (my ($face, $info) = each %$faces) {
3153 my $idx = (cf::face::find $face) || cf::face::alloc $face; 3166 my $idx = (cf::face::find $face) || cf::face::alloc $face;
3167
3154 cf::face::set_visibility $idx, $info->{visibility}; 3168 cf::face::set_visibility $idx, $info->{visibility};
3155 cf::face::set_magicmap $idx, $info->{magicmap}; 3169 cf::face::set_magicmap $idx, $info->{magicmap};
3156 cf::face::set_data $idx, 0, $info->{data32}, Digest::MD5::md5 $info->{data32}; 3170 cf::face::set_data $idx, 0, $info->{data32}, Digest::MD5::md5 $info->{data32};
3157 cf::face::set_data $idx, 1, $info->{data64}, Digest::MD5::md5 $info->{data64}; 3171 cf::face::set_data $idx, 1, $info->{data64}, Digest::MD5::md5 $info->{data64};
3158 3172
3159 cf::cede_to_tick; 3173 cf::cede_to_tick;
3160 } 3174 }
3161 3175
3162 while (my ($face, $info) = each %$faces) { 3176 while (my ($face, $info) = each %$faces) {
3163 next unless $info->{smooth}; 3177 next unless $info->{smooth};
3178
3164 my $idx = cf::face::find $face 3179 my $idx = cf::face::find $face
3165 or next; 3180 or next;
3181
3166 if (my $smooth = cf::face::find $info->{smooth}) { 3182 if (my $smooth = cf::face::find $info->{smooth}) {
3167 cf::face::set_smooth $idx, $smooth; 3183 cf::face::set_smooth $idx, $smooth;
3168 cf::face::set_smoothlevel $idx, $info->{smoothlevel}; 3184 cf::face::set_smoothlevel $idx, $info->{smoothlevel};
3169 } else { 3185 } else {
3170 warn "smooth face '$info->{smooth}' not found for face '$face'"; 3186 warn "smooth face '$info->{smooth}' not found for face '$face'";
3188 { 3204 {
3189 # TODO: for gcfclient pleasure, we should give resources 3205 # TODO: for gcfclient pleasure, we should give resources
3190 # that gcfclient doesn't grok a >10000 face index. 3206 # that gcfclient doesn't grok a >10000 face index.
3191 my $res = $facedata->{resource}; 3207 my $res = $facedata->{resource};
3192 3208
3193 my $soundconf = delete $res->{"res/sound.conf"};
3194
3195 while (my ($name, $info) = each %$res) { 3209 while (my ($name, $info) = each %$res) {
3210 if (defined $info->{type}) {
3196 my $idx = (cf::face::find $name) || cf::face::alloc $name; 3211 my $idx = (cf::face::find $name) || cf::face::alloc $name;
3197 my $data; 3212 my $data;
3198 3213
3199 if ($info->{type} & 1) { 3214 if ($info->{type} & 1) {
3200 # prepend meta info 3215 # prepend meta info
3201 3216
3202 my $meta = $enc->encode ({ 3217 my $meta = $enc->encode ({
3203 name => $name, 3218 name => $name,
3204 %{ $info->{meta} || {} }, 3219 %{ $info->{meta} || {} },
3205 }); 3220 });
3206 3221
3207 $data = pack "(w/a*)*", $meta, $info->{data}; 3222 $data = pack "(w/a*)*", $meta, $info->{data};
3223 } else {
3224 $data = $info->{data};
3225 }
3226
3227 cf::face::set_data $idx, 0, $data, Digest::MD5::md5 $data;
3228 cf::face::set_type $idx, $info->{type};
3208 } else { 3229 } else {
3209 $data = $info->{data}; 3230 $RESOURCE{$name} = $info;
3210 } 3231 }
3211 3232
3212 cf::face::set_data $idx, 0, $data, Digest::MD5::md5 $data;
3213 cf::face::set_type $idx, $info->{type};
3214
3215 cf::cede_to_tick; 3233 cf::cede_to_tick;
3216 } 3234 }
3217
3218 if ($soundconf) {
3219 $soundconf = $enc->decode (delete $soundconf->{data});
3220
3221 for (0 .. SOUND_CAST_SPELL_0 - 1) {
3222 my $sound = $soundconf->{compat}[$_]
3223 or next;
3224
3225 my $face = cf::face::find "sound/$sound->[1]";
3226 cf::sound::set $sound->[0] => $face;
3227 cf::sound::old_sound_index $_, $face; # gcfclient-compat
3228 }
3229
3230 while (my ($k, $v) = each %{$soundconf->{event}}) {
3231 my $face = cf::face::find "sound/$v";
3232 cf::sound::set $k => $face;
3233 }
3234 }
3235 } 3235 }
3236
3237 cf::global->invoke (EVENT_GLOBAL_RESOURCE_UPDATE);
3236 3238
3237 1 3239 1
3238} 3240}
3241
3242cf::global->attach (on_resource_update => sub {
3243 if (my $soundconf = $RESOURCE{"res/sound.conf"}) {
3244 $soundconf = JSON::XS->new->utf8->relaxed->decode ($soundconf->{data});
3245
3246 for (0 .. SOUND_CAST_SPELL_0 - 1) {
3247 my $sound = $soundconf->{compat}[$_]
3248 or next;
3249
3250 my $face = cf::face::find "sound/$sound->[1]";
3251 cf::sound::set $sound->[0] => $face;
3252 cf::sound::old_sound_index $_, $face; # gcfclient-compat
3253 }
3254
3255 while (my ($k, $v) = each %{$soundconf->{event}}) {
3256 my $face = cf::face::find "sound/$v";
3257 cf::sound::set $k => $face;
3258 }
3259 }
3260});
3239 3261
3240register_exticmd fx_want => sub { 3262register_exticmd fx_want => sub {
3241 my ($ns, $want) = @_; 3263 my ($ns, $want) = @_;
3242 3264
3243 while (my ($k, $v) = each %$want) { 3265 while (my ($k, $v) = each %$want) {
3298sub reload_config { 3320sub reload_config {
3299 open my $fh, "<:utf8", "$CONFDIR/config" 3321 open my $fh, "<:utf8", "$CONFDIR/config"
3300 or return; 3322 or return;
3301 3323
3302 local $/; 3324 local $/;
3303 *CFG = YAML::Syck::Load <$fh>; 3325 *CFG = YAML::Load <$fh>;
3304 3326
3305 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37]; 3327 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37];
3306 3328
3307 $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; 3329 $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset};
3308 $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; 3330 $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset};
3550sub reload_perl() { 3572sub reload_perl() {
3551 # doing reload synchronously and two reloads happen back-to-back, 3573 # doing reload synchronously and two reloads happen back-to-back,
3552 # coro crashes during coro_state_free->destroy here. 3574 # coro crashes during coro_state_free->destroy here.
3553 3575
3554 $RELOAD_WATCHER ||= EV::timer 0, 0, sub { 3576 $RELOAD_WATCHER ||= EV::timer 0, 0, sub {
3577 do_reload_perl;
3555 undef $RELOAD_WATCHER; 3578 undef $RELOAD_WATCHER;
3556 do_reload_perl;
3557 }; 3579 };
3558} 3580}
3559 3581
3560register_command "reload" => sub { 3582register_command "reload" => sub {
3561 my ($who, $arg) = @_; 3583 my ($who, $arg) = @_;
3603 3625
3604 $NOW = $tick_start = EV::now; 3626 $NOW = $tick_start = EV::now;
3605 3627
3606 cf::server_tick; # one server iteration 3628 cf::server_tick; # one server iteration
3607 3629
3608 $RUNTIME += $TICK; 3630 $RUNTIME += $TICK;
3609 $NEXT_TICK += $TICK; 3631 $NEXT_TICK = $_[0]->at;
3610 3632
3611 if ($NOW >= $NEXT_RUNTIME_WRITE) { 3633 if ($NOW >= $NEXT_RUNTIME_WRITE) {
3612 $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; 3634 $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.;
3613 Coro::async_pool { 3635 Coro::async_pool {
3614 $Coro::current->{desc} = "runtime saver"; 3636 $Coro::current->{desc} = "runtime saver";

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines