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.494 by root, Mon Oct 26 05:18:00 2009 UTC vs.
Revision 1.510 by root, Tue Apr 6 22:59:28 2010 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,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5# 5#
6# Deliantra is free software: you can redistribute it and/or modify it under 6# Deliantra is free software: you can redistribute it and/or modify it under
7# the terms of the Affero GNU General Public License as published by the 7# the terms of the Affero GNU General Public License as published by the
8# Free Software Foundation, either version 3 of the License, or (at your 8# Free Software Foundation, either version 3 of the License, or (at your
9# option) any later version. 9# option) any later version.
53use Coro::Util (); 53use Coro::Util ();
54 54
55use JSON::XS 2.01 (); 55use JSON::XS 2.01 ();
56use BDB (); 56use BDB ();
57use Data::Dumper; 57use Data::Dumper;
58use Digest::MD5;
59use Fcntl; 58use Fcntl;
60use YAML::XS (); 59use YAML::XS ();
61use IO::AIO (); 60use IO::AIO ();
62use Time::HiRes; 61use Time::HiRes;
63use Compress::LZF; 62use Compress::LZF;
424 423
425=cut 424=cut
426 425
427our @SLOT_QUEUE; 426our @SLOT_QUEUE;
428our $SLOT_QUEUE; 427our $SLOT_QUEUE;
428our $SLOT_DECAY = 0.9;
429 429
430$SLOT_QUEUE->cancel if $SLOT_QUEUE; 430$SLOT_QUEUE->cancel if $SLOT_QUEUE;
431$SLOT_QUEUE = Coro::async { 431$SLOT_QUEUE = Coro::async {
432 $Coro::current->desc ("timeslot manager"); 432 $Coro::current->desc ("timeslot manager");
433 433
434 my $signal = new Coro::Signal; 434 my $signal = new Coro::Signal;
435 my $busy;
435 436
436 while () { 437 while () {
437 next_job: 438 next_job:
439
438 my $avail = cf::till_tick; 440 my $avail = cf::till_tick;
439 if ($avail > 0.01) { 441
440 for (0 .. $#SLOT_QUEUE) { 442 for (0 .. $#SLOT_QUEUE) {
441 if ($SLOT_QUEUE[$_][0] < $avail) { 443 if ($SLOT_QUEUE[$_][0] <= $avail) {
444 $busy = 0;
442 my $job = splice @SLOT_QUEUE, $_, 1, (); 445 my $job = splice @SLOT_QUEUE, $_, 1, ();
443 $job->[2]->send; 446 $job->[2]->send;
444 Coro::cede; 447 Coro::cede;
445 goto next_job; 448 goto next_job;
446 } 449 } else {
450 $SLOT_QUEUE[$_][0] *= $SLOT_DECAY;
447 } 451 }
448 } 452 }
449 453
450 if (@SLOT_QUEUE) { 454 if (@SLOT_QUEUE) {
451 # we do not use wait_for_tick() as it returns immediately when tick is inactive 455 # we do not use wait_for_tick() as it returns immediately when tick is inactive
452 push @cf::WAIT_FOR_TICK, $signal; 456 push @cf::WAIT_FOR_TICK, $signal;
453 $signal->wait; 457 $signal->wait;
454 } else { 458 } else {
459 $busy = 0;
455 Coro::schedule; 460 Coro::schedule;
456 } 461 }
457 } 462 }
458}; 463};
459 464
460sub get_slot($;$$) { 465sub get_slot($;$$) {
461 return if tick_inhibit || $Coro::current == $Coro::main; 466 return if tick_inhibit || $Coro::current == $Coro::main;
462 467
463 my ($time, $pri, $name) = @_; 468 my ($time, $pri, $name) = @_;
464 469
465 $time = $TICK * .6 if $time > $TICK * .6; 470 $time = clamp $time, 0.01, $TICK * .6;
471
466 my $sig = new Coro::Signal; 472 my $sig = new Coro::Signal;
467 473
468 push @SLOT_QUEUE, [$time, $pri, $sig, $name]; 474 push @SLOT_QUEUE, [$time, $pri, $sig, $name];
469 @SLOT_QUEUE = sort { $b->[1] <=> $a->[1] } @SLOT_QUEUE; 475 @SLOT_QUEUE = sort { $b->[1] <=> $a->[1] } @SLOT_QUEUE;
470 $SLOT_QUEUE->ready; 476 $SLOT_QUEUE->ready;
1304} 1310}
1305 1311
1306use File::Glob (); 1312use File::Glob ();
1307 1313
1308cf::player->attach ( 1314cf::player->attach (
1309 on_command => sub { 1315 on_unknown_command => sub {
1310 my ($pl, $name, $params) = @_; 1316 my ($pl, $name, $params) = @_;
1311 1317
1312 my $cb = $COMMAND{$name} 1318 my $cb = $COMMAND{$name}
1313 or return; 1319 or return;
1314 1320
1393 . "\n};\n1"; 1399 . "\n};\n1";
1394 1400
1395 $todo{$base} = \%ext; 1401 $todo{$base} = \%ext;
1396 } 1402 }
1397 1403
1404 my $pass = 0;
1398 my %done; 1405 my %done;
1399 while (%todo) { 1406 while (%todo) {
1400 my $progress; 1407 my $progress;
1401 1408
1409 ++$pass;
1410
1411 ext:
1402 while (my ($k, $v) = each %todo) { 1412 while (my ($k, $v) = each %todo) {
1403 for (split /,\s*/, $v->{meta}{depends}) { 1413 for (split /,\s*/, $v->{meta}{depends}) {
1404 goto skip 1414 next ext
1405 unless exists $done{$_}; 1415 unless exists $done{$_};
1406 } 1416 }
1407 1417
1408 warn "... loading '$k' into '$v->{pkg}'\n"; 1418 warn "... pass $pass, loading '$k' into '$v->{pkg}'\n";
1409 1419
1410 unless (eval $v->{source}) { 1420 my $active = eval $v->{source};
1421
1422 if (length $@) {
1411 my $msg = $@ ? "$v->{path}: $@\n" 1423 warn "$v->{path}: $@\n";
1412 : "$v->{base}: extension inactive.\n";
1413 1424
1414 if (exists $v->{meta}{mandatory}) {
1415 warn $msg;
1416 cf::cleanup "mandatory extension failed to load, exiting."; 1425 cf::cleanup "mandatory extension '$k' failed to load, exiting."
1417 } 1426 if exists $v->{meta}{mandatory};
1418 1427 } else {
1419 warn $msg; 1428 $done{$k} = delete $todo{$k};
1429 push @EXTS, $v->{pkg};
1430 $progress = 1;
1431
1432 warn "$v->{base}: extension inactive.\n"
1433 unless $active;
1420 } 1434 }
1421
1422 $done{$k} = delete $todo{$k};
1423 push @EXTS, $v->{pkg};
1424 $progress = 1;
1425 } 1435 }
1426 1436
1427 skip: 1437 unless ($progress) {
1428 die "cannot load " . (join ", ", keys %todo) . ": unable to resolve dependencies\n" 1438 warn "cannot load " . (join ", ", keys %todo) . ": unable to resolve dependencies\n";
1429 unless $progress; 1439
1440 while (my ($k, $v) = each %todo) {
1441 cf::cleanup "mandatory extension '$k' has unresolved dependencies, exiting."
1442 if exists $v->{meta}{mandatory};
1443 }
1444 }
1430 } 1445 }
1431 }; 1446 };
1432} 1447}
1433 1448
1434############################################################################# 1449#############################################################################
1754 my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM 1769 my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM
1755 1770
1756 # mit "rum" bekleckern, nicht 1771 # mit "rum" bekleckern, nicht
1757 $self->_create_random_map ( 1772 $self->_create_random_map (
1758 $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, 1773 $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle},
1759 $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, 1774 $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, $rmp->{miningstyle},
1760 $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, 1775 $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map},
1761 $rmp->{exit_on_final_map}, 1776 $rmp->{exit_on_final_map},
1762 $rmp->{xsize}, $rmp->{ysize}, 1777 $rmp->{xsize}, $rmp->{ysize},
1763 $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3}, 1778 $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3},
1764 $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase}, 1779 $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase},
2010 2025
2011 $cf::MAP{$path} = $map 2026 $cf::MAP{$path} = $map
2012 } 2027 }
2013} 2028}
2014 2029
2015sub pre_load { } 2030sub pre_load { }
2016sub post_load { } 2031#sub post_load { } # XS
2017 2032
2018sub load { 2033sub load {
2019 my ($self) = @_; 2034 my ($self) = @_;
2020 2035
2021 local $self->{deny_reset} = 1; # loading can take a long time 2036 local $self->{deny_reset} = 1; # loading can take a long time
2078 } 2093 }
2079 2094
2080 $self->post_load; 2095 $self->post_load;
2081} 2096}
2082 2097
2098# customize the map for a given player, i.e.
2099# return the _real_ map. used by e.g. per-player
2100# maps to change the path to ~playername/mappath
2083sub customise_for { 2101sub customise_for {
2084 my ($self, $ob) = @_; 2102 my ($self, $ob) = @_;
2085 2103
2086 return find "~" . $ob->name . "/" . $self->{path} 2104 return find "~" . $ob->name . "/" . $self->{path}
2087 if $self->per_player; 2105 if $self->per_player;
3116 3134
3117 $coro 3135 $coro
3118} 3136}
3119 3137
3120cf::client->attach ( 3138cf::client->attach (
3121 on_destroy => sub { 3139 on_client_destroy => sub {
3122 my ($ns) = @_; 3140 my ($ns) = @_;
3123 3141
3124 $_->cancel for values %{ (delete $ns->{_coro}) || {} }; 3142 $_->cancel for values %{ (delete $ns->{_coro}) || {} };
3125 }, 3143 },
3126); 3144);
3142our $safe_hole = new Safe::Hole; 3160our $safe_hole = new Safe::Hole;
3143 3161
3144$SIG{FPE} = 'IGNORE'; 3162$SIG{FPE} = 'IGNORE';
3145 3163
3146$safe->permit_only (Opcode::opset qw( 3164$safe->permit_only (Opcode::opset qw(
3147 :base_core :base_mem :base_orig :base_math 3165 :base_core :base_mem :base_orig :base_math :base_loop
3148 grepstart grepwhile mapstart mapwhile 3166 grepstart grepwhile mapstart mapwhile
3149 sort time 3167 sort time
3150)); 3168));
3151 3169
3152# here we export the classes and methods available to script code 3170# here we export the classes and methods available to script code
3204 $qcode =~ s/"/‟/g; # not allowed in #line filenames 3222 $qcode =~ s/"/‟/g; # not allowed in #line filenames
3205 $qcode =~ s/\n/\\n/g; 3223 $qcode =~ s/\n/\\n/g;
3206 3224
3207 %vars = (_dummy => 0) unless %vars; 3225 %vars = (_dummy => 0) unless %vars;
3208 3226
3227 my @res;
3209 local $_; 3228 local $_;
3210 local @safe::cf::_safe_eval_args = values %vars;
3211 3229
3212 my $eval = 3230 my $eval =
3213 "do {\n" 3231 "do {\n"
3214 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" 3232 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n"
3215 . "#line 0 \"{$qcode}\"\n" 3233 . "#line 0 \"{$qcode}\"\n"
3216 . $code 3234 . $code
3217 . "\n}" 3235 . "\n}"
3218 ; 3236 ;
3219 3237
3238 if ($CFG{safe_eval}) {
3220 sub_generation_inc; 3239 sub_generation_inc;
3240 local @safe::cf::_safe_eval_args = values %vars;
3221 my @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval); 3241 @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval);
3222 sub_generation_inc; 3242 sub_generation_inc;
3243 } else {
3244 local @cf::_safe_eval_args = values %vars;
3245 @res = wantarray ? eval eval : scalar eval $eval;
3246 }
3223 3247
3224 if ($@) { 3248 if ($@) {
3225 warn "$@"; 3249 warn "$@";
3226 warn "while executing safe code '$code'\n"; 3250 warn "while executing safe code '$code'\n";
3227 warn "with arguments " . (join " ", %vars) . "\n"; 3251 warn "with arguments " . (join " ", %vars) . "\n";
3246=cut 3270=cut
3247 3271
3248sub register_script_function { 3272sub register_script_function {
3249 my ($fun, $cb) = @_; 3273 my ($fun, $cb) = @_;
3250 3274
3251 no strict 'refs'; 3275 $fun = "safe::$fun" if $CFG{safe_eval};
3252 *{"safe::$fun"} = $safe_hole->wrap ($cb); 3276 *$fun = $safe_hole->wrap ($cb);
3253} 3277}
3254 3278
3255=back 3279=back
3256 3280
3257=cut 3281=cut
3278 3302
3279 $facedata->{version} == 2 3303 $facedata->{version} == 2
3280 or cf::cleanup "$path: version mismatch, cannot proceed."; 3304 or cf::cleanup "$path: version mismatch, cannot proceed.";
3281 3305
3282 # patch in the exptable 3306 # patch in the exptable
3307 my $exp_table = $enc->encode ([map cf::level_to_min_exp $_, 1 .. cf::settings->max_level]);
3283 $facedata->{resource}{"res/exp_table"} = { 3308 $facedata->{resource}{"res/exp_table"} = {
3284 type => FT_RSRC, 3309 type => FT_RSRC,
3285 data => $enc->encode ([map cf::level_to_min_exp $_, 1 .. cf::settings->max_level]), 3310 data => $exp_table,
3311 hash => (Digest::MD5::md5 $exp_table),
3286 }; 3312 };
3287 cf::cede_to_tick; 3313 cf::cede_to_tick;
3288 3314
3289 { 3315 {
3290 my $faces = $facedata->{faceinfo}; 3316 my $faces = $facedata->{faceinfo};
3292 while (my ($face, $info) = each %$faces) { 3318 while (my ($face, $info) = each %$faces) {
3293 my $idx = (cf::face::find $face) || cf::face::alloc $face; 3319 my $idx = (cf::face::find $face) || cf::face::alloc $face;
3294 3320
3295 cf::face::set_visibility $idx, $info->{visibility}; 3321 cf::face::set_visibility $idx, $info->{visibility};
3296 cf::face::set_magicmap $idx, $info->{magicmap}; 3322 cf::face::set_magicmap $idx, $info->{magicmap};
3297 cf::face::set_data $idx, 0, $info->{data32}, Digest::MD5::md5 $info->{data32}; 3323 cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32};
3298 cf::face::set_data $idx, 1, $info->{data64}, Digest::MD5::md5 $info->{data64}; 3324 cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64};
3299 3325
3300 cf::cede_to_tick; 3326 cf::cede_to_tick;
3301 } 3327 }
3302 3328
3303 while (my ($face, $info) = each %$faces) { 3329 while (my ($face, $info) = each %$faces) {
3327 3353
3328 cf::anim::invalidate_all; # d'oh 3354 cf::anim::invalidate_all; # d'oh
3329 } 3355 }
3330 3356
3331 { 3357 {
3332 # TODO: for gcfclient pleasure, we should give resources
3333 # that gcfclient doesn't grok a >10000 face index.
3334 my $res = $facedata->{resource}; 3358 my $res = $facedata->{resource};
3335 3359
3336 while (my ($name, $info) = each %$res) { 3360 while (my ($name, $info) = each %$res) {
3337 if (defined $info->{type}) { 3361 if (defined $info->{type}) {
3338 my $idx = (cf::face::find $name) || cf::face::alloc $name; 3362 my $idx = (cf::face::find $name) || cf::face::alloc $name;
3339 my $data;
3340 3363
3341 if ($info->{type} & 1) { 3364 cf::face::set_data $idx, 0, $info->{data}, $info->{hash};
3342 # prepend meta info
3343
3344 my $meta = $enc->encode ({
3345 name => $name,
3346 %{ $info->{meta} || {} },
3347 });
3348
3349 $data = pack "(w/a*)*", $meta, $info->{data};
3350 } else {
3351 $data = $info->{data};
3352 }
3353
3354 cf::face::set_data $idx, 0, $data, Digest::MD5::md5 $data;
3355 cf::face::set_type $idx, $info->{type}; 3365 cf::face::set_type $idx, $info->{type};
3356 } else { 3366 } else {
3357 $RESOURCE{$name} = $info; 3367 $RESOURCE{$name} = $info;
3358 } 3368 }
3359 3369
3522 }; 3532 };
3523 3533
3524 evthread_start IO::AIO::poll_fileno; 3534 evthread_start IO::AIO::poll_fileno;
3525 3535
3526 cf::sync_job { 3536 cf::sync_job {
3537 cf::load_settings;
3538 cf::load_materials;
3539
3527 reload_resources; 3540 reload_resources;
3528 reload_config; 3541 reload_config;
3529 db_init; 3542 db_init;
3530 3543
3531 cf::load_settings;
3532 cf::load_materials;
3533 cf::init_uuid; 3544 cf::init_uuid;
3534 cf::init_signals; 3545 cf::init_signals;
3535 cf::init_commands;
3536 cf::init_skills; 3546 cf::init_skills;
3537 3547
3538 cf::init_beforeplay; 3548 cf::init_beforeplay;
3539 3549
3540 atomic; 3550 atomic;
3565 }; 3575 };
3566 } 3576 }
3567} 3577}
3568 3578
3569sub write_runtime_sync { 3579sub write_runtime_sync {
3580 my $t0 = EV::time;
3581
3570 # first touch the runtime file to show we are still running: 3582 # first touch the runtime file to show we are still running:
3571 # the fsync below can take a very very long time. 3583 # the fsync below can take a very very long time.
3572 3584
3573 IO::AIO::aio_utime $RUNTIMEFILE, undef, undef; 3585 IO::AIO::aio_utime $RUNTIMEFILE, undef, undef;
3574 3586
3575 my $guard = cf::lock_acquire "write_runtime"; 3587 my $guard = cf::lock_acquire "write_runtime";
3576 3588
3577 my $fh = aio_open "$RUNTIMEFILE~", O_WRONLY | O_CREAT, 0644 3589 my $fh = aio_open "$RUNTIMEFILE~", O_WRONLY | O_CREAT | O_TRUNC, 0644
3578 or return; 3590 or return;
3579 3591
3580 my $value = $cf::RUNTIME + 90 + 10; 3592 my $value = $cf::RUNTIME + 90 + 10;
3581 # 10 is the runtime save interval, for a monotonic clock 3593 # 10 is the runtime save interval, for a monotonic clock
3582 # 60 allows for the watchdog to kill the server. 3594 # 60 allows for the watchdog to kill the server.
3595 or return; 3607 or return;
3596 3608
3597 aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE 3609 aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE
3598 and return; 3610 and return;
3599 3611
3600 warn "runtime file written.\n"; 3612 warn sprintf "runtime file written (%gs).\n", EV::time - $t0;
3601 3613
3602 1 3614 1
3603} 3615}
3604 3616
3605our $uuid_lock; 3617our $uuid_lock;
3896 3908
3897our @WAIT_FOR_TICK; 3909our @WAIT_FOR_TICK;
3898our @WAIT_FOR_TICK_BEGIN; 3910our @WAIT_FOR_TICK_BEGIN;
3899 3911
3900sub wait_for_tick { 3912sub wait_for_tick {
3901 return if tick_inhibit || $Coro::current == $Coro::main; 3913 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main;
3902 3914
3903 my $signal = new Coro::Signal; 3915 my $signal = new Coro::Signal;
3904 push @WAIT_FOR_TICK, $signal; 3916 push @WAIT_FOR_TICK, $signal;
3905 $signal->wait; 3917 $signal->wait;
3906} 3918}
3907 3919
3908sub wait_for_tick_begin { 3920sub wait_for_tick_begin {
3909 return if tick_inhibit || $Coro::current == $Coro::main; 3921 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main;
3910 3922
3911 my $signal = new Coro::Signal; 3923 my $signal = new Coro::Signal;
3912 push @WAIT_FOR_TICK_BEGIN, $signal; 3924 push @WAIT_FOR_TICK_BEGIN, $signal;
3913 $signal->wait; 3925 $signal->wait;
3914} 3926}
3919 unless ++$bug_warning > 10; 3931 unless ++$bug_warning > 10;
3920 return; 3932 return;
3921 } 3933 }
3922 3934
3923 cf::server_tick; # one server iteration 3935 cf::server_tick; # one server iteration
3936
3937 #for(1..3e6){} EV::now_update; $NOW=EV::now; # generate load #d#
3924 3938
3925 if ($NOW >= $NEXT_RUNTIME_WRITE) { 3939 if ($NOW >= $NEXT_RUNTIME_WRITE) {
3926 $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; 3940 $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.;
3927 Coro::async_pool { 3941 Coro::async_pool {
3928 $Coro::current->{desc} = "runtime saver"; 3942 $Coro::current->{desc} = "runtime saver";
3951} 3965}
3952 3966
3953{ 3967{
3954 # configure BDB 3968 # configure BDB
3955 3969
3956 BDB::min_parallel 8; 3970 BDB::min_parallel 16;
3957 BDB::max_poll_reqs $TICK * 0.1; 3971 BDB::max_poll_reqs $TICK * 0.1;
3958 $AnyEvent::BDB::WATCHER->priority (1); 3972 $AnyEvent::BDB::WATCHER->priority (1);
3959 3973
3960 unless ($DB_ENV) { 3974 unless ($DB_ENV) {
3961 $DB_ENV = BDB::db_env_create; 3975 $DB_ENV = BDB::db_env_create;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines