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.401 by root, Mon Dec 17 06:45:53 2007 UTC vs.
Revision 1.408 by root, Mon Jan 14 10:12:12 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 warn Carp::longmess $_[0];
198 Coro::terminate;
199};
197 200
198@safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; 201@safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable';
199@safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; 202@safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable';
200@safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable'; 203@safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable';
201@safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable'; 204@safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable';
968 } 971 }
969 972
970 0 973 0
971} 974}
972 975
973=item $bool = cf::global::invoke (EVENT_CLASS_XXX, ...) 976=item $bool = cf::global->invoke (EVENT_CLASS_XXX, ...)
974 977
975=item $bool = $attachable->invoke (EVENT_CLASS_XXX, ...) 978=item $bool = $attachable->invoke (EVENT_CLASS_XXX, ...)
976 979
977Generate an object-specific event with the given arguments. 980Generate an object-specific event with the given arguments.
978 981
3149 { 3152 {
3150 my $faces = $facedata->{faceinfo}; 3153 my $faces = $facedata->{faceinfo};
3151 3154
3152 while (my ($face, $info) = each %$faces) { 3155 while (my ($face, $info) = each %$faces) {
3153 my $idx = (cf::face::find $face) || cf::face::alloc $face; 3156 my $idx = (cf::face::find $face) || cf::face::alloc $face;
3157
3154 cf::face::set_visibility $idx, $info->{visibility}; 3158 cf::face::set_visibility $idx, $info->{visibility};
3155 cf::face::set_magicmap $idx, $info->{magicmap}; 3159 cf::face::set_magicmap $idx, $info->{magicmap};
3156 cf::face::set_data $idx, 0, $info->{data32}, Digest::MD5::md5 $info->{data32}; 3160 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}; 3161 cf::face::set_data $idx, 1, $info->{data64}, Digest::MD5::md5 $info->{data64};
3158 3162
3159 cf::cede_to_tick; 3163 cf::cede_to_tick;
3160 } 3164 }
3161 3165
3162 while (my ($face, $info) = each %$faces) { 3166 while (my ($face, $info) = each %$faces) {
3163 next unless $info->{smooth}; 3167 next unless $info->{smooth};
3168
3164 my $idx = cf::face::find $face 3169 my $idx = cf::face::find $face
3165 or next; 3170 or next;
3171
3166 if (my $smooth = cf::face::find $info->{smooth}) { 3172 if (my $smooth = cf::face::find $info->{smooth}) {
3167 cf::face::set_smooth $idx, $smooth; 3173 cf::face::set_smooth $idx, $smooth;
3168 cf::face::set_smoothlevel $idx, $info->{smoothlevel}; 3174 cf::face::set_smoothlevel $idx, $info->{smoothlevel};
3169 } else { 3175 } else {
3170 warn "smooth face '$info->{smooth}' not found for face '$face'"; 3176 warn "smooth face '$info->{smooth}' not found for face '$face'";
3188 { 3194 {
3189 # TODO: for gcfclient pleasure, we should give resources 3195 # TODO: for gcfclient pleasure, we should give resources
3190 # that gcfclient doesn't grok a >10000 face index. 3196 # that gcfclient doesn't grok a >10000 face index.
3191 my $res = $facedata->{resource}; 3197 my $res = $facedata->{resource};
3192 3198
3193 my $soundconf = delete $res->{"res/sound.conf"};
3194
3195 while (my ($name, $info) = each %$res) { 3199 while (my ($name, $info) = each %$res) {
3200 if (defined $info->{type}) {
3196 my $idx = (cf::face::find $name) || cf::face::alloc $name; 3201 my $idx = (cf::face::find $name) || cf::face::alloc $name;
3197 my $data; 3202 my $data;
3198 3203
3199 if ($info->{type} & 1) { 3204 if ($info->{type} & 1) {
3200 # prepend meta info 3205 # prepend meta info
3201 3206
3202 my $meta = $enc->encode ({ 3207 my $meta = $enc->encode ({
3203 name => $name, 3208 name => $name,
3204 %{ $info->{meta} || {} }, 3209 %{ $info->{meta} || {} },
3205 }); 3210 });
3206 3211
3207 $data = pack "(w/a*)*", $meta, $info->{data}; 3212 $data = pack "(w/a*)*", $meta, $info->{data};
3213 } else {
3214 $data = $info->{data};
3215 }
3216
3217 cf::face::set_data $idx, 0, $data, Digest::MD5::md5 $data;
3218 cf::face::set_type $idx, $info->{type};
3208 } else { 3219 } else {
3209 $data = $info->{data}; 3220 $RESOURCE{$name} = $info;
3210 } 3221 }
3211 3222
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; 3223 cf::cede_to_tick;
3216 } 3224 }
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 } 3225 }
3226
3227 cf::global->invoke (EVENT_GLOBAL_RESOURCE_UPDATE);
3236 3228
3237 1 3229 1
3238} 3230}
3231
3232cf::global->attach (on_resource_update => sub {
3233 if (my $soundconf = $RESOURCE{"res/sound.conf"}) {
3234 $soundconf = JSON::XS->new->utf8->relaxed->decode ($soundconf->{data});
3235
3236 for (0 .. SOUND_CAST_SPELL_0 - 1) {
3237 my $sound = $soundconf->{compat}[$_]
3238 or next;
3239
3240 my $face = cf::face::find "sound/$sound->[1]";
3241 cf::sound::set $sound->[0] => $face;
3242 cf::sound::old_sound_index $_, $face; # gcfclient-compat
3243 }
3244
3245 while (my ($k, $v) = each %{$soundconf->{event}}) {
3246 my $face = cf::face::find "sound/$v";
3247 cf::sound::set $k => $face;
3248 }
3249 }
3250});
3239 3251
3240register_exticmd fx_want => sub { 3252register_exticmd fx_want => sub {
3241 my ($ns, $want) = @_; 3253 my ($ns, $want) = @_;
3242 3254
3243 while (my ($k, $v) = each %$want) { 3255 while (my ($k, $v) = each %$want) {
3298sub reload_config { 3310sub reload_config {
3299 open my $fh, "<:utf8", "$CONFDIR/config" 3311 open my $fh, "<:utf8", "$CONFDIR/config"
3300 or return; 3312 or return;
3301 3313
3302 local $/; 3314 local $/;
3303 *CFG = YAML::Syck::Load <$fh>; 3315 *CFG = YAML::Load <$fh>;
3304 3316
3305 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37]; 3317 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37];
3306 3318
3307 $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; 3319 $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}; 3320 $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset};
3603 3615
3604 $NOW = $tick_start = EV::now; 3616 $NOW = $tick_start = EV::now;
3605 3617
3606 cf::server_tick; # one server iteration 3618 cf::server_tick; # one server iteration
3607 3619
3608 $RUNTIME += $TICK; 3620 $RUNTIME += $TICK;
3609 $NEXT_TICK += $TICK; 3621 $NEXT_TICK = $_[0]->at;
3610 3622
3611 if ($NOW >= $NEXT_RUNTIME_WRITE) { 3623 if ($NOW >= $NEXT_RUNTIME_WRITE) {
3612 $NEXT_RUNTIME_WRITE = $NOW + 10; 3624 $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.;
3613 Coro::async_pool { 3625 Coro::async_pool {
3614 $Coro::current->{desc} = "runtime saver"; 3626 $Coro::current->{desc} = "runtime saver";
3615 write_runtime 3627 write_runtime
3616 or warn "ERROR: unable to write runtime file: $!"; 3628 or warn "ERROR: unable to write runtime file: $!";
3617 }; 3629 };
3634{ 3646{
3635 # configure BDB 3647 # configure BDB
3636 3648
3637 BDB::min_parallel 8; 3649 BDB::min_parallel 8;
3638 BDB::max_poll_reqs $TICK * 0.1; 3650 BDB::max_poll_reqs $TICK * 0.1;
3651 $Coro::BDB::WATCHER->priority (1);
3639 3652
3640 unless ($DB_ENV) { 3653 unless ($DB_ENV) {
3641 $DB_ENV = BDB::db_env_create; 3654 $DB_ENV = BDB::db_env_create;
3642 $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC 3655 $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC
3643 | BDB::LOG_AUTOREMOVE, 1); 3656 | BDB::LOG_AUTOREMOVE, 1);
3674{ 3687{
3675 # configure IO::AIO 3688 # configure IO::AIO
3676 3689
3677 IO::AIO::min_parallel 8; 3690 IO::AIO::min_parallel 8;
3678 IO::AIO::max_poll_time $TICK * 0.1; 3691 IO::AIO::max_poll_time $TICK * 0.1;
3692 $Coro::AIO::WATCHER->priority (1);
3679} 3693}
3680 3694
3681my $_log_backtrace; 3695my $_log_backtrace;
3682 3696
3683sub _log_backtrace { 3697sub _log_backtrace {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines