… | |
… | |
547 | reset_signals; |
547 | reset_signals; |
548 | &$cb |
548 | &$cb |
549 | }, @args; |
549 | }, @args; |
550 | |
550 | |
551 | wantarray ? @res : $res[-1] |
551 | wantarray ? @res : $res[-1] |
|
|
552 | } |
|
|
553 | |
|
|
554 | =item $coin = coin_from_name $name |
|
|
555 | |
|
|
556 | =cut |
|
|
557 | |
|
|
558 | our %coin_alias = ( |
|
|
559 | "silver" => "silvercoin", |
|
|
560 | "silvercoin" => "silvercoin", |
|
|
561 | "silvercoins" => "silvercoin", |
|
|
562 | "gold" => "goldcoin", |
|
|
563 | "goldcoin" => "goldcoin", |
|
|
564 | "goldcoins" => "goldcoin", |
|
|
565 | "platinum" => "platinacoin", |
|
|
566 | "platinumcoin" => "platinacoin", |
|
|
567 | "platinumcoins" => "platinacoin", |
|
|
568 | "platina" => "platinacoin", |
|
|
569 | "platinacoin" => "platinacoin", |
|
|
570 | "platinacoins" => "platinacoin", |
|
|
571 | "royalty" => "royalty", |
|
|
572 | "royalties" => "royalty", |
|
|
573 | ); |
|
|
574 | |
|
|
575 | sub coin_from_name($) { |
|
|
576 | $coin_alias{$_[0]} |
|
|
577 | ? cf::arch::find $coin_alias{$_[0]} |
|
|
578 | : undef |
552 | } |
579 | } |
553 | |
580 | |
554 | =item $value = cf::db_get $family => $key |
581 | =item $value = cf::db_get $family => $key |
555 | |
582 | |
556 | Returns a single value from the environment database. |
583 | Returns a single value from the environment database. |
… | |
… | |
1748 | our $MAX_RESET = 3600; |
1775 | our $MAX_RESET = 3600; |
1749 | our $DEFAULT_RESET = 3000; |
1776 | our $DEFAULT_RESET = 3000; |
1750 | |
1777 | |
1751 | sub generate_random_map { |
1778 | sub generate_random_map { |
1752 | my ($self, $rmp) = @_; |
1779 | my ($self, $rmp) = @_; |
|
|
1780 | |
|
|
1781 | my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM |
|
|
1782 | |
1753 | # mit "rum" bekleckern, nicht |
1783 | # mit "rum" bekleckern, nicht |
1754 | $self->_create_random_map ( |
1784 | $self->_create_random_map ( |
1755 | $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, |
1785 | $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, |
1756 | $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, |
1786 | $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, |
1757 | $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, |
1787 | $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, |
… | |
… | |
1879 | |
1909 | |
1880 | # the temporary/swap location |
1910 | # the temporary/swap location |
1881 | sub save_path { |
1911 | sub save_path { |
1882 | my ($self) = @_; |
1912 | my ($self) = @_; |
1883 | |
1913 | |
1884 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
1914 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/go; |
1885 | "$TMPDIR/$path.map" |
1915 | "$TMPDIR/$path.map" |
1886 | } |
1916 | } |
1887 | |
1917 | |
1888 | # the unique path, undef == no special unique path |
1918 | # the unique path, undef == no special unique path |
1889 | sub uniq_path { |
1919 | sub uniq_path { |
1890 | my ($self) = @_; |
1920 | my ($self) = @_; |
1891 | |
1921 | |
1892 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
1922 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/go; |
1893 | "$UNIQUEDIR/$path" |
1923 | "$UNIQUEDIR/$path" |
1894 | } |
1924 | } |
1895 | |
1925 | |
1896 | # and all this just because we cannot iterate over |
1926 | # and all this just because we cannot iterate over |
1897 | # all maps in C++... |
1927 | # all maps in C++... |
… | |
… | |
2343 | |
2373 | |
2344 | sub unique_maps() { |
2374 | sub unique_maps() { |
2345 | [ |
2375 | [ |
2346 | map { |
2376 | map { |
2347 | utf8::decode $_; |
2377 | utf8::decode $_; |
2348 | /\.map$/ |
2378 | s/\.map$//; # TODO future compatibility hack |
|
|
2379 | /\.pst$/ || !/^$PATH_SEP/o # TODO unique maps apparebntly lack the .map suffix :/ |
|
|
2380 | ? () |
2349 | ? normalise $_ |
2381 | : normalise $_ |
2350 | : () |
|
|
2351 | } @{ aio_readdir $UNIQUEDIR or [] } |
2382 | } @{ aio_readdir $UNIQUEDIR or [] } |
2352 | ] |
2383 | ] |
2353 | } |
2384 | } |
2354 | |
2385 | |
2355 | package cf; |
|
|
2356 | |
|
|
2357 | =back |
2386 | =back |
2358 | |
2387 | |
2359 | =head3 cf::object |
2388 | =head3 cf::object |
2360 | |
2389 | |
2361 | =cut |
2390 | =cut |
… | |
… | |
2364 | |
2393 | |
2365 | =over 4 |
2394 | =over 4 |
2366 | |
2395 | |
2367 | =item $ob->inv_recursive |
2396 | =item $ob->inv_recursive |
2368 | |
2397 | |
2369 | Returns the inventory of the object _and_ their inventories, recursively. |
2398 | Returns the inventory of the object I<and> their inventories, recursively, |
|
|
2399 | but I<not> the object itself. |
2370 | |
2400 | |
2371 | =cut |
2401 | =cut |
2372 | |
2402 | |
2373 | sub inv_recursive_; |
2403 | sub inv_recursive_; |
2374 | sub inv_recursive_ { |
2404 | sub inv_recursive_ { |
… | |
… | |
2379 | inv_recursive_ inv $_[0] |
2409 | inv_recursive_ inv $_[0] |
2380 | } |
2410 | } |
2381 | |
2411 | |
2382 | =item $ref = $ob->ref |
2412 | =item $ref = $ob->ref |
2383 | |
2413 | |
2384 | creates and returns a persistent reference to an objetc that can be stored as a string. |
2414 | Creates and returns a persistent reference to an object that can be stored as a string. |
2385 | |
2415 | |
2386 | =item $ob = cf::object::deref ($refstring) |
2416 | =item $ob = cf::object::deref ($refstring) |
2387 | |
2417 | |
2388 | returns the objetc referenced by refstring. may return undef when it cnanot find the object, |
2418 | returns the objetc referenced by refstring. may return undef when it cnanot find the object, |
2389 | even if the object actually exists. May block. |
2419 | even if the object actually exists. May block. |
… | |
… | |
3379 | cf::cleanup "SIG$signal"; |
3409 | cf::cleanup "SIG$signal"; |
3380 | }; |
3410 | }; |
3381 | } |
3411 | } |
3382 | } |
3412 | } |
3383 | |
3413 | |
3384 | sub write_runtime { |
3414 | sub write_runtime_sync { |
3385 | my $runtime = "$LOCALDIR/runtime"; |
3415 | my $runtime = "$LOCALDIR/runtime"; |
3386 | |
3416 | |
3387 | # first touch the runtime file to show we are still running: |
3417 | # first touch the runtime file to show we are still running: |
3388 | # the fsync below can take a very very long time. |
3418 | # the fsync below can take a very very long time. |
3389 | |
3419 | |
… | |
… | |
3415 | and return; |
3445 | and return; |
3416 | |
3446 | |
3417 | warn "runtime file written.\n"; |
3447 | warn "runtime file written.\n"; |
3418 | |
3448 | |
3419 | 1 |
3449 | 1 |
|
|
3450 | } |
|
|
3451 | |
|
|
3452 | our $uuid_lock; |
|
|
3453 | our $uuid_skip; |
|
|
3454 | |
|
|
3455 | sub write_uuid_sync($) { |
|
|
3456 | $uuid_skip ||= $_[0]; |
|
|
3457 | |
|
|
3458 | return if $uuid_lock; |
|
|
3459 | local $uuid_lock = 1; |
|
|
3460 | |
|
|
3461 | my $uuid = "$LOCALDIR/uuid"; |
|
|
3462 | |
|
|
3463 | my $fh = aio_open "$uuid~", O_WRONLY | O_CREAT, 0644 |
|
|
3464 | or return; |
|
|
3465 | |
|
|
3466 | my $value = uuid_str $uuid_skip + uuid_seq uuid_cur; |
|
|
3467 | $uuid_skip = 0; |
|
|
3468 | |
|
|
3469 | (aio_write $fh, 0, (length $value), $value, 0) <= 0 |
|
|
3470 | and return; |
|
|
3471 | |
|
|
3472 | # always fsync - this file is important |
|
|
3473 | aio_fsync $fh |
|
|
3474 | and return; |
|
|
3475 | |
|
|
3476 | close $fh |
|
|
3477 | or return; |
|
|
3478 | |
|
|
3479 | aio_rename "$uuid~", $uuid |
|
|
3480 | and return; |
|
|
3481 | |
|
|
3482 | warn "uuid file written ($value).\n"; |
|
|
3483 | |
|
|
3484 | 1 |
|
|
3485 | |
|
|
3486 | } |
|
|
3487 | |
|
|
3488 | sub write_uuid($$) { |
|
|
3489 | my ($skip, $sync) = @_; |
|
|
3490 | |
|
|
3491 | $sync ? write_uuid_sync $skip |
|
|
3492 | : async { write_uuid_sync $skip }; |
3420 | } |
3493 | } |
3421 | |
3494 | |
3422 | sub emergency_save() { |
3495 | sub emergency_save() { |
3423 | my $freeze_guard = cf::freeze_mainloop; |
3496 | my $freeze_guard = cf::freeze_mainloop; |
3424 | |
3497 | |
… | |
… | |
3446 | warn "end emergency map save\n"; |
3519 | warn "end emergency map save\n"; |
3447 | |
3520 | |
3448 | warn "begin emergency database checkpoint\n"; |
3521 | warn "begin emergency database checkpoint\n"; |
3449 | BDB::db_env_txn_checkpoint $DB_ENV; |
3522 | BDB::db_env_txn_checkpoint $DB_ENV; |
3450 | warn "end emergency database checkpoint\n"; |
3523 | warn "end emergency database checkpoint\n"; |
|
|
3524 | |
|
|
3525 | warn "begin write uuid\n"; |
|
|
3526 | write_uuid_sync 1; |
|
|
3527 | warn "end write uuid\n"; |
3451 | }; |
3528 | }; |
3452 | |
3529 | |
3453 | warn "leave emergency perl save\n"; |
3530 | warn "leave emergency perl save\n"; |
3454 | } |
3531 | } |
3455 | |
3532 | |
… | |
… | |
3470 | warn "reloading..."; |
3547 | warn "reloading..."; |
3471 | |
3548 | |
3472 | warn "entering sync_job"; |
3549 | warn "entering sync_job"; |
3473 | |
3550 | |
3474 | cf::sync_job { |
3551 | cf::sync_job { |
3475 | cf::write_runtime; # external watchdog should not bark |
3552 | cf::write_runtime_sync; # external watchdog should not bark |
3476 | cf::emergency_save; |
3553 | cf::emergency_save; |
3477 | cf::write_runtime; # external watchdog should not bark |
3554 | cf::write_runtime_sync; # external watchdog should not bark |
3478 | |
3555 | |
3479 | warn "syncing database to disk"; |
3556 | warn "syncing database to disk"; |
3480 | BDB::db_env_txn_checkpoint $DB_ENV; |
3557 | BDB::db_env_txn_checkpoint $DB_ENV; |
3481 | |
3558 | |
3482 | # if anything goes wrong in here, we should simply crash as we already saved |
3559 | # if anything goes wrong in here, we should simply crash as we already saved |
… | |
… | |
3637 | |
3714 | |
3638 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3715 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3639 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
3716 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
3640 | Coro::async_pool { |
3717 | Coro::async_pool { |
3641 | $Coro::current->{desc} = "runtime saver"; |
3718 | $Coro::current->{desc} = "runtime saver"; |
3642 | write_runtime |
3719 | write_runtime_sync |
3643 | or warn "ERROR: unable to write runtime file: $!"; |
3720 | or warn "ERROR: unable to write runtime file: $!"; |
3644 | }; |
3721 | }; |
3645 | } |
3722 | } |
3646 | |
3723 | |
3647 | if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { |
3724 | if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { |