… | |
… | |
2329 | sub db_put($$$) { |
2329 | sub db_put($$$) { |
2330 | BDB::dbreq_pri 4; |
2330 | BDB::dbreq_pri 4; |
2331 | BDB::db_put $DB, undef, "$_[0]/$_[1]", Compress::LZF::sfreeze_cr $_[2], 0, sub { }; |
2331 | BDB::db_put $DB, undef, "$_[0]/$_[1]", Compress::LZF::sfreeze_cr $_[2], 0, sub { }; |
2332 | } |
2332 | } |
2333 | |
2333 | |
|
|
2334 | =item cf::cache $id => ... |
|
|
2335 | |
|
|
2336 | Generic caching function that returns the value of the resource $id, |
|
|
2337 | caching and regenerating as required. |
|
|
2338 | |
|
|
2339 | This function can block. |
|
|
2340 | |
|
|
2341 | source => filename returning the data (must be a scalar) |
|
|
2342 | expensive => true == try to cache harder |
|
|
2343 | filter => sub that processes the data into a scalar |
|
|
2344 | |
|
|
2345 | =cut |
|
|
2346 | |
|
|
2347 | sub cache { |
|
|
2348 | my ($id, %arg) = @_; |
|
|
2349 | |
|
|
2350 | aio_stat $arg{source} |
|
|
2351 | and Carp::croak "$arg{source}: $!"; |
|
|
2352 | |
|
|
2353 | my $meta = join ":", (stat _)[7,9]; |
|
|
2354 | my $md5; |
|
|
2355 | |
|
|
2356 | if ($arg{expensive}) { |
|
|
2357 | 0 <= aio_load $arg{source}, my $buf |
|
|
2358 | or Carp::croak "$arg{source}: $!"; |
|
|
2359 | |
|
|
2360 | $md5 = Digest::MD5::md5_hex $buf; |
|
|
2361 | } |
|
|
2362 | |
|
|
2363 | my $dbmeta = db_get "$id/meta"; |
|
|
2364 | if ($dbmeta ne $meta) { |
|
|
2365 | # changed, we need to process |
|
|
2366 | } else { |
|
|
2367 | # just fetch |
|
|
2368 | } |
|
|
2369 | |
|
|
2370 | |
|
|
2371 | } |
|
|
2372 | |
2334 | ############################################################################# |
2373 | ############################################################################# |
2335 | # the server's init and main functions |
2374 | # the server's init and main functions |
2336 | |
2375 | |
2337 | sub load_facedata { |
2376 | sub load_facedata($) { |
2338 | my $path = sprintf "%s/facedata", cf::datadir; |
2377 | my ($path) = @_; |
2339 | |
2378 | |
2340 | warn "loading facedata from $path\n"; |
2379 | warn "loading facedata from $path\n"; |
2341 | |
2380 | |
2342 | my $facedata; |
2381 | my $facedata; |
2343 | 0 < aio_load $path, $facedata |
2382 | 0 < aio_load $path, $facedata |
… | |
… | |
2384 | } |
2423 | } |
2385 | |
2424 | |
2386 | 1 |
2425 | 1 |
2387 | } |
2426 | } |
2388 | |
2427 | |
|
|
2428 | sub reload_facedata { |
|
|
2429 | load_facedata sprintf "%s/facedata", cf::datadir |
|
|
2430 | or die "unable to load facedata\n"; |
|
|
2431 | } |
|
|
2432 | |
|
|
2433 | sub reload_regions { |
|
|
2434 | load_resource_file sprintf "%s/%s/regions", cf::datadir, cf::mapdir |
|
|
2435 | or die "unable to load regions file\n"; |
|
|
2436 | } |
|
|
2437 | |
2389 | sub load_archetypes { |
2438 | sub reload_archetypes { |
2390 | load_archetype_file sprintf "%s/archetypes", cf::datadir; # remove when stable |
|
|
2391 | load_archetype_file sprintf "%s/archetypes", cf::datadir |
2439 | load_resource_file sprintf "%s/archetypes", cf::datadir |
|
|
2440 | or die "unable to load archetypes\n"; |
2392 | } |
2441 | } |
2393 | |
2442 | |
2394 | sub load_treasures { |
2443 | sub reload_treasures { |
2395 | load_treasure_file sprintf "%s/treasures", cf::datadir |
2444 | load_resource_file sprintf "%s/treasures", cf::datadir |
|
|
2445 | or die "unable to load treasurelists\n"; |
2396 | } |
2446 | } |
2397 | |
2447 | |
2398 | sub reload_resources { |
2448 | sub reload_resources { |
2399 | warn "reloading resource files...\n"; |
2449 | warn "reloading resource files...\n"; |
2400 | |
2450 | |
2401 | load_resource_file sprintf "%s/%s/regions", cf::datadir, cf::mapdir |
2451 | reload_regions; |
2402 | or die "unable to load regions file\n"; |
|
|
2403 | load_facedata |
2452 | reload_facedata; |
2404 | or die "unable to load facedata\n"; |
|
|
2405 | load_archetypes |
2453 | reload_archetypes; |
2406 | or die "unable to load archetypes\n"; |
|
|
2407 | load_treasures |
2454 | reload_treasures; |
2408 | or die "unable to load treasurelists\n"; |
|
|
2409 | |
2455 | |
2410 | warn "finished reloading resource files\n"; |
2456 | warn "finished reloading resource files\n"; |
2411 | } |
2457 | } |
2412 | |
2458 | |
2413 | sub init { |
2459 | sub init { |
… | |
… | |
2508 | |
2554 | |
2509 | warn Carp::longmess "post_cleanup backtrace" |
2555 | warn Carp::longmess "post_cleanup backtrace" |
2510 | if $make_core; |
2556 | if $make_core; |
2511 | } |
2557 | } |
2512 | |
2558 | |
2513 | sub reload() { |
2559 | sub do_reload_perl() { |
2514 | # can/must only be called in main |
2560 | # can/must only be called in main |
2515 | if ($Coro::current != $Coro::main) { |
2561 | if ($Coro::current != $Coro::main) { |
2516 | warn "can only reload from main coroutine"; |
2562 | warn "can only reload from main coroutine"; |
2517 | return; |
2563 | return; |
2518 | } |
2564 | } |
… | |
… | |
2617 | warn "reattaching attachments to maps"; |
2663 | warn "reattaching attachments to maps"; |
2618 | reattach $_ for values %MAP; |
2664 | reattach $_ for values %MAP; |
2619 | warn "reattaching attachments to players"; |
2665 | warn "reattaching attachments to players"; |
2620 | reattach $_ for values %PLAYER; |
2666 | reattach $_ for values %PLAYER; |
2621 | |
2667 | |
2622 | warn "loading reloadable resources"; |
|
|
2623 | reload_resources; |
|
|
2624 | |
|
|
2625 | warn "leaving sync_job"; |
2668 | warn "leaving sync_job"; |
2626 | |
2669 | |
2627 | 1 |
2670 | 1 |
2628 | } or do { |
2671 | } or do { |
2629 | warn $@; |
2672 | warn $@; |
… | |
… | |
2634 | warn "reloaded"; |
2677 | warn "reloaded"; |
2635 | }; |
2678 | }; |
2636 | |
2679 | |
2637 | our $RELOAD_WATCHER; # used only during reload |
2680 | our $RELOAD_WATCHER; # used only during reload |
2638 | |
2681 | |
|
|
2682 | sub reload_perl() { |
|
|
2683 | # doing reload synchronously and two reloads happen back-to-back, |
|
|
2684 | # coro crashes during coro_state_free->destroy here. |
|
|
2685 | |
|
|
2686 | $RELOAD_WATCHER ||= Event->timer ( |
|
|
2687 | reentrant => 0, |
|
|
2688 | after => 0, |
|
|
2689 | data => WF_AUTOCANCEL, |
|
|
2690 | cb => sub { |
|
|
2691 | do_reload_perl; |
|
|
2692 | undef $RELOAD_WATCHER; |
|
|
2693 | }, |
|
|
2694 | ); |
|
|
2695 | } |
|
|
2696 | |
2639 | register_command "reload" => sub { |
2697 | register_command "reload" => sub { |
2640 | my ($who, $arg) = @_; |
2698 | my ($who, $arg) = @_; |
2641 | |
2699 | |
2642 | if ($who->flag (FLAG_WIZ)) { |
2700 | if ($who->flag (FLAG_WIZ)) { |
2643 | $who->message ("reloading server."); |
2701 | $who->message ("reloading server."); |
2644 | |
2702 | async { reload_perl }; |
2645 | # doing reload synchronously and two reloads happen back-to-back, |
|
|
2646 | # coro crashes during coro_state_free->destroy here. |
|
|
2647 | |
|
|
2648 | $RELOAD_WATCHER ||= Event->timer ( |
|
|
2649 | reentrant => 0, |
|
|
2650 | after => 0, |
|
|
2651 | data => WF_AUTOCANCEL, |
|
|
2652 | cb => sub { |
|
|
2653 | reload; |
|
|
2654 | undef $RELOAD_WATCHER; |
|
|
2655 | }, |
|
|
2656 | ); |
|
|
2657 | } |
2703 | } |
2658 | }; |
2704 | }; |
2659 | |
2705 | |
2660 | unshift @INC, $LIBDIR; |
2706 | unshift @INC, $LIBDIR; |
2661 | |
2707 | |
… | |
… | |
2793 | prio => 6, |
2839 | prio => 6, |
2794 | cb => \&IO::AIO::poll_cb, |
2840 | cb => \&IO::AIO::poll_cb, |
2795 | ); |
2841 | ); |
2796 | } |
2842 | } |
2797 | |
2843 | |
|
|
2844 | # load additional modules |
|
|
2845 | use cf::pod; |
|
|
2846 | |
2798 | END { cf::emergency_save } |
2847 | END { cf::emergency_save } |
2799 | |
2848 | |
2800 | 1 |
2849 | 1 |
2801 | |
2850 | |