… | |
… | |
317 | Carp::cluck "error in event callback: @_"; |
317 | Carp::cluck "error in event callback: @_"; |
318 | }; |
318 | }; |
319 | |
319 | |
320 | ############################################################################# |
320 | ############################################################################# |
321 | |
321 | |
|
|
322 | sub fork_call(&@); |
|
|
323 | sub get_slot($;$$); |
|
|
324 | |
|
|
325 | ############################################################################# |
|
|
326 | |
322 | =head2 UTILITY FUNCTIONS |
327 | =head2 UTILITY FUNCTIONS |
323 | |
328 | |
324 | =over 4 |
329 | =over 4 |
325 | |
330 | |
326 | =item dumpval $ref |
331 | =item dumpval $ref |
… | |
… | |
344 | $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; |
349 | $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; |
345 | $d |
350 | $d |
346 | } || "[unable to dump $_[0]: '$@']"; |
351 | } || "[unable to dump $_[0]: '$@']"; |
347 | } |
352 | } |
348 | |
353 | |
|
|
354 | =item $scalar = load_file $path |
|
|
355 | |
|
|
356 | Loads the given file from path and returns its contents. Croaks on error |
|
|
357 | and can block. |
|
|
358 | |
|
|
359 | =cut |
|
|
360 | |
|
|
361 | sub load_file($) { |
|
|
362 | 0 <= aio_load $_[0], my $data |
|
|
363 | or Carp::croak "$_[0]: $!"; |
|
|
364 | |
|
|
365 | $data |
|
|
366 | } |
|
|
367 | |
349 | =item $ref = cf::decode_json $json |
368 | =item $ref = cf::decode_json $json |
350 | |
369 | |
351 | Converts a JSON string into the corresponding perl data structure. |
370 | Converts a JSON string into the corresponding perl data structure. |
352 | |
371 | |
353 | =item $json = cf::encode_json $ref |
372 | =item $json = cf::encode_json $ref |
… | |
… | |
359 | our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max |
378 | our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max |
360 | |
379 | |
361 | sub encode_json($) { $json_coder->encode ($_[0]) } |
380 | sub encode_json($) { $json_coder->encode ($_[0]) } |
362 | sub decode_json($) { $json_coder->decode ($_[0]) } |
381 | sub decode_json($) { $json_coder->decode ($_[0]) } |
363 | |
382 | |
|
|
383 | =item $ref = cf::decode_storable $scalar |
|
|
384 | |
|
|
385 | Same as Coro::Storable::thaw, so blocks. |
|
|
386 | |
|
|
387 | =cut |
|
|
388 | |
|
|
389 | BEGIN { *decode_storable = \&Coro::Storable::thaw } |
|
|
390 | |
364 | =item $ref = cf::yaml_load $scalar |
391 | =item $ref = cf::decode_yaml $scalar |
365 | |
392 | |
366 | Same as YAML::XS::Load, but doesn't leak, because it forks (and thus blocks). |
393 | Same as YAML::XS::Load, but doesn't leak, because it forks (and thus blocks). |
367 | |
394 | |
368 | =cut |
395 | =cut |
369 | |
396 | |
370 | sub fork_call(&@); |
397 | sub decode_yaml($) { |
371 | |
|
|
372 | sub yaml_load($) { |
|
|
373 | fork_call { YAML::XS::Load $_[0] } @_ |
398 | fork_call { YAML::XS::Load $_[0] } @_ |
|
|
399 | } |
|
|
400 | |
|
|
401 | =item $scalar = cf::unlzf $scalar |
|
|
402 | |
|
|
403 | Same as Compress::LZF::compress, but takes server ticks into account, so |
|
|
404 | blocks. |
|
|
405 | |
|
|
406 | =cut |
|
|
407 | |
|
|
408 | sub unlzf($) { |
|
|
409 | # we assume 100mb/s minimum decompression speed (noncompressible data on a ~2ghz machine) |
|
|
410 | cf::get_slot +(length $_[0]) / 100_000_000, 0, "unlzf"; |
|
|
411 | Compress::LZF::decompress $_[0] |
374 | } |
412 | } |
375 | |
413 | |
376 | =item cf::post_init { BLOCK } |
414 | =item cf::post_init { BLOCK } |
377 | |
415 | |
378 | Execute the given codeblock, I<after> all extensions have been (re-)loaded, |
416 | Execute the given codeblock, I<after> all extensions have been (re-)loaded, |
… | |
… | |
462 | EV::periodic $start, $interval, 0, $cb |
500 | EV::periodic $start, $interval, 0, $cb |
463 | } |
501 | } |
464 | |
502 | |
465 | =item cf::get_slot $time[, $priority[, $name]] |
503 | =item cf::get_slot $time[, $priority[, $name]] |
466 | |
504 | |
467 | Allocate $time seconds of blocking CPU time at priority C<$priority>: |
505 | Allocate $time seconds of blocking CPU time at priority C<$priority> |
468 | This call blocks and returns only when you have at least C<$time> seconds |
506 | (default: 0): This call blocks and returns only when you have at least |
469 | of cpu time till the next tick. The slot is only valid till the next cede. |
507 | C<$time> seconds of cpu time till the next tick. The slot is only valid |
|
|
508 | till the next cede. |
|
|
509 | |
|
|
510 | Background jobs should use a priority les than zero, interactive jobs |
|
|
511 | should use 100 or more. |
470 | |
512 | |
471 | The optional C<$name> can be used to identify the job to run. It might be |
513 | The optional C<$name> can be used to identify the job to run. It might be |
472 | used for statistical purposes and should identify the same time-class. |
514 | used for statistical purposes and should identify the same time-class. |
473 | |
515 | |
474 | Useful for short background jobs. |
516 | Useful for short background jobs. |
… | |
… | |
769 | |
811 | |
770 | my @data; |
812 | my @data; |
771 | my $md5; |
813 | my $md5; |
772 | |
814 | |
773 | for (0 .. $#$src) { |
815 | for (0 .. $#$src) { |
774 | 0 <= aio_load $src->[$_], $data[$_] |
816 | $data[$_] = load_file $src->[$_]; |
775 | or Carp::croak "$src->[$_]: $!"; |
|
|
776 | } |
817 | } |
777 | |
818 | |
778 | # if processing is expensive, check |
819 | # if processing is expensive, check |
779 | # checksum first |
820 | # checksum first |
780 | if (1) { |
821 | if (1) { |
… | |
… | |
3401 | |
3442 | |
3402 | my $enc = JSON::XS->new->utf8->canonical->relaxed; |
3443 | my $enc = JSON::XS->new->utf8->canonical->relaxed; |
3403 | |
3444 | |
3404 | trace "loading facedata from $path\n"; |
3445 | trace "loading facedata from $path\n"; |
3405 | |
3446 | |
3406 | 0 < aio_load $path, my $facedata |
3447 | my $facedata = decode_storable load_file $path; |
3407 | or die "$path: $!"; |
|
|
3408 | |
|
|
3409 | $facedata = Coro::Storable::thaw $facedata; |
|
|
3410 | |
3448 | |
3411 | $facedata->{version} == 2 |
3449 | $facedata->{version} == 2 |
3412 | or cf::cleanup "$path: version mismatch, cannot proceed."; |
3450 | or cf::cleanup "$path: version mismatch, cannot proceed."; |
3413 | |
3451 | |
3414 | # patch in the exptable |
3452 | # patch in the exptable |
… | |
… | |
3533 | } |
3571 | } |
3534 | |
3572 | |
3535 | sub reload_sound { |
3573 | sub reload_sound { |
3536 | trace "loading sound config from $DATADIR/sound\n"; |
3574 | trace "loading sound config from $DATADIR/sound\n"; |
3537 | |
3575 | |
3538 | 0 < Coro::AIO::aio_load "$DATADIR/sound", my $data |
|
|
3539 | or die "$DATADIR/sound $!"; |
|
|
3540 | |
|
|
3541 | my $soundconf = JSON::XS->new->utf8->relaxed->decode ($data); |
3576 | my $soundconf = JSON::XS->new->utf8->relaxed->decode (load_file "$DATADIR/sound"); |
3542 | |
3577 | |
3543 | for (0 .. SOUND_CAST_SPELL_0 - 1) { |
3578 | for (0 .. SOUND_CAST_SPELL_0 - 1) { |
3544 | my $sound = $soundconf->{compat}[$_] |
3579 | my $sound = $soundconf->{compat}[$_] |
3545 | or next; |
3580 | or next; |
3546 | |
3581 | |
… | |
… | |
3570 | } |
3605 | } |
3571 | |
3606 | |
3572 | sub reload_config { |
3607 | sub reload_config { |
3573 | trace "reloading config file...\n"; |
3608 | trace "reloading config file...\n"; |
3574 | |
3609 | |
3575 | 0 < aio_load "$CONFDIR/config", my $config |
3610 | my $config = load_file "$CONFDIR/config"; |
3576 | or die "$CONFDIR/config: $!"; |
|
|
3577 | |
|
|
3578 | utf8::decode $config; |
3611 | utf8::decode $config; |
3579 | *CFG = yaml_load $config; |
3612 | *CFG = decode_yaml $config; |
3580 | |
3613 | |
3581 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; |
3614 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; |
3582 | |
3615 | |
3583 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3616 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3584 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
3617 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |