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 (©) 2018 Marc Alexander Lehmann / the Deliantra team |
4 | # Copyright (©) 2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | # Copyright (©) 2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | # |
6 | # |
6 | # Deliantra is free software: you can redistribute it and/or modify it under |
7 | # 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 |
8 | # 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 |
9 | # Free Software Foundation, either version 3 of the License, or (at your |
… | |
… | |
57 | use JSON::XS 2.01 (); |
58 | use JSON::XS 2.01 (); |
58 | use BDB (); |
59 | use BDB (); |
59 | use Data::Dumper; |
60 | use Data::Dumper; |
60 | use Fcntl; |
61 | use Fcntl; |
61 | use YAML::XS (); |
62 | use YAML::XS (); |
|
|
63 | use CBOR::XS (); |
62 | use IO::AIO (); |
64 | use IO::AIO (); |
63 | use Time::HiRes; |
|
|
64 | use Compress::LZF; |
65 | use Compress::LZF; |
65 | use Digest::MD5 (); |
66 | use Digest::MD5 (); |
66 | |
67 | |
67 | AnyEvent::detect; |
68 | AnyEvent::detect; |
68 | |
69 | |
… | |
… | |
226 | =item $cf::RUNTIME |
227 | =item $cf::RUNTIME |
227 | |
228 | |
228 | The time this server has run, starts at 0 and is increased by $cf::TICK on |
229 | The time this server has run, starts at 0 and is increased by $cf::TICK on |
229 | every server tick. |
230 | every server tick. |
230 | |
231 | |
231 | =item $cf::CONFDIR $cf::DATADIR $cf::LIBDIR $cf::PODDIR |
232 | =item $cf::CONFDIR $cf::DATADIR $cf::LIBDIR $cf::PODDIR |
232 | $cf::MAPDIR $cf::LOCALDIR $cf::TMPDIR $cf::UNIQUEDIR |
233 | $cf::MAPDIR $cf::LOCALDIR $cf::TMPDIR $cf::UNIQUEDIR |
233 | $cf::PLAYERDIR $cf::RANDOMDIR $cf::BDBDIR |
234 | $cf::PLAYERDIR $cf::RANDOMDIR $cf::BDBDIR |
234 | |
235 | |
235 | Various directories - "/etc", read-only install directory, perl-library |
236 | Various directories - "/etc", read-only install directory, perl-library |
236 | directory, pod-directory, read-only maps directory, "/var", "/var/tmp", |
237 | directory, pod-directory, read-only maps directory, "/var", "/var/tmp", |
… | |
… | |
252 | Configuration for the server, loaded from C</etc/deliantra-server/config>, or |
253 | Configuration for the server, loaded from C</etc/deliantra-server/config>, or |
253 | from wherever your confdir points to. |
254 | from wherever your confdir points to. |
254 | |
255 | |
255 | =item cf::wait_for_tick, cf::wait_for_tick_begin |
256 | =item cf::wait_for_tick, cf::wait_for_tick_begin |
256 | |
257 | |
257 | These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only |
258 | These are functions that inhibit the current coroutine one tick. |
258 | returns directly I<after> the tick processing (and consequently, can only wake one thread |
259 | cf::wait_for_tick_begin only returns directly I<after> the tick |
|
|
260 | processing (and consequently, can only wake one thread per tick), while |
259 | per tick), while cf::wait_for_tick wakes up all waiters after tick processing. |
261 | cf::wait_for_tick wakes up all waiters after tick processing. |
260 | |
262 | |
261 | Note that cf::Wait_for_tick will immediately return when the server is not |
263 | Note that cf::wait_for_tick will immediately return when the server is not |
262 | ticking, making it suitable for small pauses in threads that need to run |
264 | ticking, making it suitable for small pauses in threads that need to run |
263 | when the server is paused. If that is not applicable (i.e. you I<really> |
265 | when the server is paused. If that is not applicable (i.e. you I<really> |
264 | want to wait, use C<$cf::WAIT_FOR_TICK>). |
266 | want to wait, use C<$cf::WAIT_FOR_TICK>). |
265 | |
267 | |
266 | =item $cf::WAIT_FOR_TICK |
268 | =item $cf::WAIT_FOR_TICK |
… | |
… | |
464 | |
466 | |
465 | sub decode_yaml($) { |
467 | sub decode_yaml($) { |
466 | fork_call { YAML::XS::Load $_[0] } @_ |
468 | fork_call { YAML::XS::Load $_[0] } @_ |
467 | } |
469 | } |
468 | |
470 | |
|
|
471 | =item $scalar = cf::decode_cbor $scalar |
|
|
472 | |
|
|
473 | Same as CBOR::XS::decode_cbor, but takes server ticks into account, so |
|
|
474 | blocks. For small amounts of data, C<CBOR::XS::decode_cbor> is the better |
|
|
475 | alternative. |
|
|
476 | |
|
|
477 | =cut |
|
|
478 | |
|
|
479 | sub decode_cbor($) { |
|
|
480 | # we assume 10mb/s minimum decoding speed (on a ~2ghz machine) |
|
|
481 | cf::get_slot +(length $_[0]) / 10_000_000, 0, "decode_cbor"; |
|
|
482 | CBOR::XS::decode_cbor $_[0] |
|
|
483 | } |
|
|
484 | |
469 | =item $scalar = cf::unlzf $scalar |
485 | =item $scalar = cf::unlzf $scalar |
470 | |
486 | |
471 | Same as Compress::LZF::compress, but takes server ticks into account, so |
487 | Same as Compress::LZF::compress, but takes server ticks into account, so |
472 | blocks. |
488 | blocks. |
473 | |
489 | |
… | |
… | |
510 | } |
526 | } |
511 | } |
527 | } |
512 | |
528 | |
513 | =item cf::lock_wait $string |
529 | =item cf::lock_wait $string |
514 | |
530 | |
515 | Wait until the given lock is available. See cf::lock_acquire. |
531 | Wait until the given lock is available. See cf::lock_acquire. |
516 | |
532 | |
517 | =item my $lock = cf::lock_acquire $string |
533 | =item my $lock = cf::lock_acquire $string |
518 | |
534 | |
519 | Wait until the given lock is available and then acquires it and returns |
535 | Wait until the given lock is available and then acquires it and returns |
520 | a L<Guard> object. If the guard object gets destroyed (goes out of scope, |
536 | a L<Guard> object. If the guard object gets destroyed (goes out of scope, |
… | |
… | |
573 | Allocate $time seconds of blocking CPU time at priority C<$priority> |
589 | Allocate $time seconds of blocking CPU time at priority C<$priority> |
574 | (default: 0): This call blocks and returns only when you have at least |
590 | (default: 0): This call blocks and returns only when you have at least |
575 | C<$time> seconds of cpu time till the next tick. The slot is only valid |
591 | C<$time> seconds of cpu time till the next tick. The slot is only valid |
576 | till the next cede. |
592 | till the next cede. |
577 | |
593 | |
578 | Background jobs should use a priority les than zero, interactive jobs |
594 | Background jobs should use a priority less than zero, interactive jobs |
579 | should use 100 or more. |
595 | should use 100 or more. |
580 | |
596 | |
581 | The optional C<$name> can be used to identify the job to run. It might be |
597 | The optional C<$name> can be used to identify the job to run. It might be |
582 | used for statistical purposes and should identify the same time-class. |
598 | used for statistical purposes and should identify the same time-class. |
583 | |
599 | |
… | |
… | |
596 | my $signal = new Coro::Signal; |
612 | my $signal = new Coro::Signal; |
597 | my $busy; |
613 | my $busy; |
598 | |
614 | |
599 | while () { |
615 | while () { |
600 | next_job: |
616 | next_job: |
|
|
617 | |
|
|
618 | Coro::cede; |
601 | |
619 | |
602 | my $avail = cf::till_tick; |
620 | my $avail = cf::till_tick; |
603 | |
621 | |
604 | for (0 .. $#SLOT_QUEUE) { |
622 | for (0 .. $#SLOT_QUEUE) { |
605 | if ($SLOT_QUEUE[$_][0] <= $avail) { |
623 | if ($SLOT_QUEUE[$_][0] <= $avail) { |
606 | $busy = 0; |
624 | $busy = 0; |
607 | my $job = splice @SLOT_QUEUE, $_, 1, (); |
625 | my $job = splice @SLOT_QUEUE, $_, 1, (); |
608 | $job->[2]->send; |
626 | $job->[2]->send; |
609 | Coro::cede; |
|
|
610 | goto next_job; |
627 | goto next_job; |
611 | } else { |
628 | } else { |
612 | $SLOT_QUEUE[$_][0] *= $SLOT_DECAY; |
629 | $SLOT_QUEUE[$_][0] *= $SLOT_DECAY; |
613 | } |
630 | } |
614 | } |
631 | } |
615 | |
632 | |
616 | if (@SLOT_QUEUE) { |
633 | if (@SLOT_QUEUE) { |
617 | # we do not use wait_for_tick() as it returns immediately when tick is inactive |
634 | wait_for_tick; |
618 | $WAIT_FOR_TICK->wait; |
|
|
619 | } else { |
635 | } else { |
620 | $busy = 0; |
636 | $busy = 0; |
621 | Coro::schedule; |
637 | Coro::schedule; |
622 | } |
638 | } |
623 | } |
639 | } |
… | |
… | |
649 | BEGIN { *async = \&Coro::async_pool } |
665 | BEGIN { *async = \&Coro::async_pool } |
650 | |
666 | |
651 | =item cf::sync_job { BLOCK } |
667 | =item cf::sync_job { BLOCK } |
652 | |
668 | |
653 | The design of Deliantra requires that the main coroutine ($Coro::main) |
669 | The design of Deliantra requires that the main coroutine ($Coro::main) |
654 | is always able to handle events or runnable, as Deliantra is only |
670 | is always able to handle events or is runnable, as Deliantra is only |
655 | partly reentrant. Thus "blocking" it by e.g. waiting for I/O is not |
671 | partly reentrant. Thus "blocking" it by e.g. waiting for I/O is not |
656 | acceptable. |
672 | acceptable. |
657 | |
673 | |
658 | If it must be done, put the blocking parts into C<sync_job>. This will run |
674 | If it must be done, put the blocking parts into C<sync_job>. This will run |
659 | the given BLOCK in another coroutine while waiting for the result. The |
675 | the given BLOCK in another coroutine while waiting for the result. The |
… | |
… | |
687 | |
703 | |
688 | while ($busy) { |
704 | while ($busy) { |
689 | if (Coro::nready) { |
705 | if (Coro::nready) { |
690 | Coro::cede_notself; |
706 | Coro::cede_notself; |
691 | } else { |
707 | } else { |
692 | EV::loop EV::LOOP_ONESHOT; |
708 | EV::run EV::RUN_ONCE; |
693 | } |
709 | } |
694 | } |
710 | } |
695 | |
711 | |
696 | my $time = AE::time - $time; |
712 | my $time = AE::time - $time; |
697 | |
713 | |
… | |
… | |
738 | reset_signals; |
754 | reset_signals; |
739 | } |
755 | } |
740 | |
756 | |
741 | sub fork_call(&@) { |
757 | sub fork_call(&@) { |
742 | my ($cb, @args) = @_; |
758 | my ($cb, @args) = @_; |
743 | |
|
|
744 | # we seemingly have to make a local copy of the whole thing, |
|
|
745 | # otherwise perl prematurely frees the stuff :/ |
|
|
746 | # TODO: investigate and fix (likely this will be rather laborious) |
|
|
747 | |
759 | |
748 | my @res = Coro::Util::fork_eval { |
760 | my @res = Coro::Util::fork_eval { |
749 | cf::post_fork; |
761 | cf::post_fork; |
750 | &$cb |
762 | &$cb |
751 | } @args; |
763 | } @args; |
… | |
… | |
901 | |
913 | |
902 | return db_get cache => "$id/data"; |
914 | return db_get cache => "$id/data"; |
903 | } |
915 | } |
904 | } |
916 | } |
905 | |
917 | |
906 | my $t1 = Time::HiRes::time; |
918 | my $t1 = EV::time; |
907 | my $data = $process->(\@data); |
919 | my $data = $process->(\@data); |
908 | my $t2 = Time::HiRes::time; |
920 | my $t2 = EV::time; |
909 | |
921 | |
910 | info "cache: '$id' processed in ", $t2 - $t1, "s\n"; |
922 | info "cache: '$id' processed in ", $t2 - $t1, "s\n"; |
911 | |
923 | |
912 | db_put cache => "$id/data", $data; |
924 | db_put cache => "$id/data", $data; |
913 | db_put cache => "$id/md5" , $md5; |
925 | db_put cache => "$id/md5" , $md5; |
… | |
… | |
2388 | |
2400 | |
2389 | $MAP_PREFETCHER ||= cf::async { |
2401 | $MAP_PREFETCHER ||= cf::async { |
2390 | $Coro::current->{desc} = "map prefetcher"; |
2402 | $Coro::current->{desc} = "map prefetcher"; |
2391 | |
2403 | |
2392 | while (%MAP_PREFETCH) { |
2404 | while (%MAP_PREFETCH) { |
2393 | while (my ($k, $v) = each %MAP_PREFETCH) { |
2405 | for my $k (keys %MAP_PREFETCH) { |
2394 | if (my $map = find $k) { |
2406 | if (my $map = find $k) { |
2395 | $map->load if $v; |
2407 | $map->load if $MAP_PREFETCH{$k}; |
2396 | } |
2408 | } |
2397 | |
2409 | |
2398 | delete $MAP_PREFETCH{$k}; |
2410 | delete $MAP_PREFETCH{$k}; |
2399 | } |
2411 | } |
2400 | } |
2412 | } |
… | |
… | |
2654 | |
2666 | |
2655 | Creates and returns a persistent reference to an object that can be stored as a string. |
2667 | Creates and returns a persistent reference to an object that can be stored as a string. |
2656 | |
2668 | |
2657 | =item $ob = cf::object::deref ($refstring) |
2669 | =item $ob = cf::object::deref ($refstring) |
2658 | |
2670 | |
2659 | returns the objetc referenced by refstring. may return undef when it cnanot find the object, |
2671 | returns the objetc referenced by refstring. may return undef when it cannot find the object, |
2660 | even if the object actually exists. May block. |
2672 | even if the object actually exists. May block. |
2661 | |
2673 | |
2662 | =cut |
2674 | =cut |
2663 | |
2675 | |
2664 | sub deref { |
2676 | sub deref { |
… | |
… | |
2746 | =item $player_object->may ("access") |
2758 | =item $player_object->may ("access") |
2747 | |
2759 | |
2748 | Returns wether the given player is authorized to access resource "access" |
2760 | Returns wether the given player is authorized to access resource "access" |
2749 | (e.g. "command_wizcast"). |
2761 | (e.g. "command_wizcast"). |
2750 | |
2762 | |
|
|
2763 | This is implemented by checking a config setting of C<may_access> where |
|
|
2764 | C<access> is replaced by the access string. The following alternatives are |
|
|
2765 | possible (and are tested in order): |
|
|
2766 | |
|
|
2767 | =over 4 |
|
|
2768 | |
|
|
2769 | =item * Player is DM |
|
|
2770 | |
|
|
2771 | The request will succeed. |
|
|
2772 | |
|
|
2773 | =item * may_access is an array reference |
|
|
2774 | |
|
|
2775 | If either the player nickname or UUID is in the array, the request will |
|
|
2776 | succeed, otherwise it will fail. |
|
|
2777 | |
|
|
2778 | =item * may_access is a true value |
|
|
2779 | |
|
|
2780 | The request will succeed. |
|
|
2781 | |
|
|
2782 | =item * may_access is missing or false |
|
|
2783 | |
|
|
2784 | The request will fail. |
|
|
2785 | |
|
|
2786 | =back |
|
|
2787 | |
2751 | =cut |
2788 | =cut |
2752 | |
2789 | |
2753 | sub cf::object::player::may { |
2790 | sub cf::object::player::may { |
2754 | my ($self, $access) = @_; |
2791 | my ($self, $access) = @_; |
2755 | |
2792 | |
2756 | $self->flag (cf::FLAG_WIZ) || |
2793 | $self->flag (cf::FLAG_WIZ) || |
2757 | (ref $cf::CFG{"may_$access"} |
2794 | (ref $cf::CFG{"may_$access"} |
2758 | ? scalar grep $self->name eq $_, @{$cf::CFG{"may_$access"}} |
2795 | ? scalar grep $self->name eq $_ || $self->uuid eq $_, @{$cf::CFG{"may_$access"}} |
2759 | : $cf::CFG{"may_$access"}) |
2796 | : $cf::CFG{"may_$access"}) |
2760 | } |
2797 | } |
2761 | |
2798 | |
2762 | =item $player_object->enter_link |
2799 | =item $player_object->enter_link |
2763 | |
2800 | |
… | |
… | |
2766 | The player should be reasonably safe there for short amounts of time (e.g. |
2803 | The player should be reasonably safe there for short amounts of time (e.g. |
2767 | for loading a map). You I<MUST> call C<leave_link> as soon as possible, |
2804 | for loading a map). You I<MUST> call C<leave_link> as soon as possible, |
2768 | though, as the player cannot control the character while it is on the link |
2805 | though, as the player cannot control the character while it is on the link |
2769 | map. |
2806 | map. |
2770 | |
2807 | |
2771 | Will never block. |
2808 | This method will never block, which is the whole reaosn for it's |
|
|
2809 | existance: you can I<always> put a player onto the link map, which is the |
|
|
2810 | only place to put objects that is guaranteed to exist. |
|
|
2811 | |
|
|
2812 | A typical usage pattern is to call C<enter_link> synchronously from the |
|
|
2813 | server, then start a new thread, do your blocking stuff there and then |
|
|
2814 | call C<leave_link> from that thread. |
2772 | |
2815 | |
2773 | =item $player_object->leave_link ($map, $x, $y) |
2816 | =item $player_object->leave_link ($map, $x, $y) |
2774 | |
2817 | |
2775 | Moves the player out of the special C<{link}> map onto the specified |
2818 | Moves the player out of the special C<{link}> map onto the specified |
2776 | map. If the map is not valid (or omitted), the player will be moved back |
2819 | map. If the map is not valid (or omitted), the player will be moved back |
… | |
… | |
3046 | |
3089 | |
3047 | =head3 cf::client |
3090 | =head3 cf::client |
3048 | |
3091 | |
3049 | =over 4 |
3092 | =over 4 |
3050 | |
3093 | |
3051 | =item $client->send_drawinfo ($text, $flags) |
|
|
3052 | |
|
|
3053 | Sends a drawinfo packet to the client. Circumvents output buffering so |
|
|
3054 | should not be used under normal circumstances. |
|
|
3055 | |
|
|
3056 | =cut |
|
|
3057 | |
|
|
3058 | sub cf::client::send_drawinfo { |
|
|
3059 | my ($self, $text, $flags) = @_; |
|
|
3060 | |
|
|
3061 | utf8::encode $text; |
|
|
3062 | $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text); |
|
|
3063 | } |
|
|
3064 | |
|
|
3065 | =item $client->send_big_packet ($pkt) |
3094 | =item $client->send_big_packet ($pkt) |
3066 | |
3095 | |
3067 | Like C<send_packet>, but tries to compress large packets, and fragments |
3096 | Like C<send_packet>, but tries to compress large packets, and fragments |
3068 | them as required. |
3097 | them as required. |
3069 | |
3098 | |
… | |
… | |
3087 | $self->send_packet ($pkt); |
3116 | $self->send_packet ($pkt); |
3088 | } |
3117 | } |
3089 | |
3118 | |
3090 | =item $client->send_msg ($channel, $msg, $color, [extra...]) |
3119 | =item $client->send_msg ($channel, $msg, $color, [extra...]) |
3091 | |
3120 | |
3092 | Send a drawinfo or msg packet to the client, formatting the msg for the |
3121 | Send a msg packet to the client, formatting the msg for the client if |
3093 | client if neccessary. C<$type> should be a string identifying the type of |
3122 | necessary. C<$type> should be a string identifying the type of the |
3094 | the message, with C<log> being the default. If C<$color> is negative, suppress |
3123 | message, with C<log> being the default. If C<$color> is negative, suppress |
3095 | the message unless the client supports the msg packet. |
3124 | the message unless the client supports the msg packet. |
3096 | |
3125 | |
3097 | =cut |
3126 | =cut |
3098 | |
3127 | |
3099 | # non-persistent channels (usually the info channel) |
3128 | # non-persistent channels (usually the info channel) |
… | |
… | |
3142 | }, |
3171 | }, |
3143 | "c/body" => { |
3172 | "c/body" => { |
3144 | id => "infobox", |
3173 | id => "infobox", |
3145 | title => "Body Parts", |
3174 | title => "Body Parts", |
3146 | reply => undef, |
3175 | reply => undef, |
3147 | tooltip => "Shows which body parts you posess and are available", |
3176 | tooltip => "Shows which body parts you possess and are available", |
3148 | }, |
3177 | }, |
3149 | "c/statistics" => { |
3178 | "c/statistics" => { |
3150 | id => "infobox", |
3179 | id => "infobox", |
3151 | title => "Statistics", |
3180 | title => "Statistics", |
3152 | reply => undef, |
3181 | reply => undef, |
… | |
… | |
3204 | id => "death", |
3233 | id => "death", |
3205 | title => "Death", |
3234 | title => "Death", |
3206 | reply => undef, |
3235 | reply => undef, |
3207 | tooltip => "Reason for and more info about your most recent death", |
3236 | tooltip => "Reason for and more info about your most recent death", |
3208 | }, |
3237 | }, |
|
|
3238 | "c/fatal" => { |
|
|
3239 | id => "fatal", |
|
|
3240 | title => "Fatal Error", |
|
|
3241 | reply => undef, |
|
|
3242 | tooltip => "Reason for the server disconnect", |
|
|
3243 | }, |
3209 | "c/say" => $SAY_CHANNEL, |
3244 | "c/say" => $SAY_CHANNEL, |
3210 | "c/chat" => $CHAT_CHANNEL, |
3245 | "c/chat" => $CHAT_CHANNEL, |
3211 | ); |
3246 | ); |
3212 | |
3247 | |
3213 | sub cf::client::send_msg { |
3248 | sub cf::client::send_msg { |
… | |
… | |
3286 | |
3321 | |
3287 | $self->send_packet ($self->{query_queue}[0][0]) |
3322 | $self->send_packet ($self->{query_queue}[0][0]) |
3288 | if @{ $self->{query_queue} } == 1; |
3323 | if @{ $self->{query_queue} } == 1; |
3289 | |
3324 | |
3290 | 1 |
3325 | 1 |
|
|
3326 | } |
|
|
3327 | |
|
|
3328 | =item $client->update_command_faces |
|
|
3329 | |
|
|
3330 | =cut |
|
|
3331 | |
|
|
3332 | our %COMMAND_FACE; |
|
|
3333 | |
|
|
3334 | sub cf::client::update_command_faces { |
|
|
3335 | my ($self) = @_; |
|
|
3336 | |
|
|
3337 | my @faces = grep $_, |
|
|
3338 | $COMMAND_FACE{preferred}, |
|
|
3339 | $COMMAND_FACE{standard}, |
|
|
3340 | $COMMAND_FACE{skill}, |
|
|
3341 | $self->pl->ob->flag (cf::FLAG_WIZ) ? $COMMAND_FACE{dm} : (), |
|
|
3342 | $COMMAND_FACE{emote}, |
|
|
3343 | ; |
|
|
3344 | |
|
|
3345 | $self->send_face ($_) |
|
|
3346 | for @faces; |
|
|
3347 | $self->flush_fx; |
|
|
3348 | |
|
|
3349 | $self->ext_msg (command_list => @faces); |
|
|
3350 | } |
|
|
3351 | |
|
|
3352 | =item cf::client::set_command_face $type, $commands |
|
|
3353 | |
|
|
3354 | =cut |
|
|
3355 | |
|
|
3356 | sub cf::client::set_command_face { |
|
|
3357 | my ($type, $list) = @_; |
|
|
3358 | |
|
|
3359 | my $idx = &cf::face::set ( #d# ugly forward reference |
|
|
3360 | "command_list/$type" => cf::FT_RSRC, |
|
|
3361 | JSON::XS->new->utf8->encode ([ sort @$list ]) |
|
|
3362 | ); |
|
|
3363 | |
|
|
3364 | $COMMAND_FACE{$type} = $idx; |
3291 | } |
3365 | } |
3292 | |
3366 | |
3293 | cf::client->attach ( |
3367 | cf::client->attach ( |
3294 | on_connect => sub { |
3368 | on_connect => sub { |
3295 | my ($ns) = @_; |
3369 | my ($ns) = @_; |
… | |
… | |
3519 | =cut |
3593 | =cut |
3520 | |
3594 | |
3521 | ############################################################################# |
3595 | ############################################################################# |
3522 | # the server's init and main functions |
3596 | # the server's init and main functions |
3523 | |
3597 | |
3524 | our %FACEHASH; # hash => idx, #d# HACK for http server |
3598 | { |
|
|
3599 | package cf::face; |
3525 | |
3600 | |
|
|
3601 | our %HASH; # hash => idx |
|
|
3602 | our @DATA; # dynamically-created facedata, only faceste 0 used |
|
|
3603 | our @FOFS; # file offset, if > 0 |
|
|
3604 | our @SIZE; # size of face, in octets |
|
|
3605 | our @META; # meta hash of face, if any |
|
|
3606 | our $DATAFH; # facedata filehandle |
|
|
3607 | |
3526 | # internal api, not fianlised |
3608 | # internal api, not finalised |
3527 | sub set_face { |
3609 | sub set { |
3528 | my ($name, $type, $data) = @_; |
3610 | my ($name, $type, $data) = @_; |
3529 | |
3611 | |
3530 | my $idx = cf::face::find $name; |
3612 | my $idx = cf::face::find $name; |
3531 | |
3613 | |
3532 | if ($idx) { |
3614 | if ($idx) { |
3533 | delete $FACEHASH{cf::face::get_chksum $idx}; |
3615 | delete $HASH{cf::face::get_csum $idx}; |
3534 | } else { |
3616 | } else { |
3535 | $idx = cf::face::alloc $name; |
3617 | $idx = cf::face::alloc $name; |
3536 | } |
3618 | } |
3537 | |
3619 | |
3538 | my $hash = cf::face::mangle_chksum Digest::MD5::md5 $data; |
3620 | my $hash = cf::face::mangle_csum Digest::MD5::md5 $data; |
3539 | |
3621 | |
3540 | cf::face::set_type $idx, $type; |
3622 | cf::face::set_type $idx, $type; |
3541 | cf::face::set_data $idx, 0, $data, $hash; |
3623 | cf::face::set_csum $idx, 0, $hash; |
3542 | cf::face::set_meta $idx, $type & 1 ? undef : undef; |
3624 | |
|
|
3625 | # we need to destroy the SV itself, not just modify it, as a running ix |
|
|
3626 | # might hold a reference to it: "delete" achieves that. |
|
|
3627 | delete $FOFS[0][$idx]; |
|
|
3628 | delete $DATA[0][$idx]; |
|
|
3629 | $DATA[0][$idx] = $data; |
|
|
3630 | $SIZE[0][$idx] = length $data; |
|
|
3631 | delete $META[$idx]; |
3543 | $FACEHASH{$hash} = $idx;#d# |
3632 | $HASH{$hash} = $idx;#d# |
3544 | |
3633 | |
3545 | $idx |
3634 | $idx |
|
|
3635 | } |
|
|
3636 | |
|
|
3637 | sub _get_data($$$) { |
|
|
3638 | my ($idx, $set, $cb) = @_; |
|
|
3639 | |
|
|
3640 | if (defined $DATA[$set][$idx]) { |
|
|
3641 | $cb->($DATA[$set][$idx]); |
|
|
3642 | } elsif (my $fofs = $FOFS[$set][$idx]) { |
|
|
3643 | my $size = $SIZE[$set][$idx]; |
|
|
3644 | my $buf; |
|
|
3645 | IO::AIO::aio_read $DATAFH, $fofs, $size, $buf, 0, sub { |
|
|
3646 | if ($_[0] == $size) { |
|
|
3647 | #cf::debug "read face $idx, $size from $fofs as ", length $buf;#d# |
|
|
3648 | $cb->($buf); |
|
|
3649 | } else { |
|
|
3650 | cf::error "INTERNAL ERROR: unable to read facedata for face $idx#$set ($size, $fofs), ignoring request."; |
|
|
3651 | } |
|
|
3652 | }; |
|
|
3653 | } else { |
|
|
3654 | cf::error "requested facedata for unknown face $idx#$set, ignoring."; |
|
|
3655 | } |
|
|
3656 | } |
|
|
3657 | |
|
|
3658 | # rather ineffient |
|
|
3659 | sub cf::face::get_data($;$) { |
|
|
3660 | my ($idx, $set) = @_; |
|
|
3661 | |
|
|
3662 | _get_data $idx, $set, Coro::rouse_cb; |
|
|
3663 | Coro::rouse_wait |
|
|
3664 | } |
|
|
3665 | |
|
|
3666 | sub cf::face::ix { |
|
|
3667 | my ($ns, $set, $idx, $pri) = @_; |
|
|
3668 | |
|
|
3669 | _get_data $idx, $set, sub { |
|
|
3670 | $ns->ix_send ($idx, $pri, $_[0]); |
|
|
3671 | }; |
|
|
3672 | } |
3546 | } |
3673 | } |
3547 | |
3674 | |
3548 | sub load_facedata($) { |
3675 | sub load_facedata($) { |
3549 | my ($path) = @_; |
3676 | my ($path) = @_; |
3550 | |
3677 | |
3551 | # HACK to clear player env face cache, we need some signal framework |
|
|
3552 | # for this (global event?) |
|
|
3553 | %ext::player_env::MUSIC_FACE_CACHE = (); |
|
|
3554 | |
|
|
3555 | my $enc = JSON::XS->new->utf8->canonical->relaxed; |
3678 | my $enc = JSON::XS->new->utf8->canonical->relaxed; |
3556 | |
3679 | |
3557 | trace "loading facedata from $path\n"; |
3680 | trace "loading facedata from $path\n"; |
3558 | |
3681 | |
3559 | my $facedata = decode_storable load_file $path; |
3682 | my $facedata = decode_storable load_file "$path/faceinfo"; |
3560 | |
3683 | |
3561 | $facedata->{version} == 2 |
3684 | $facedata->{version} == 2 |
3562 | or cf::cleanup "$path: version mismatch, cannot proceed."; |
3685 | or cf::cleanup "$path/faceinfo: version mismatch, cannot proceed."; |
3563 | |
3686 | |
3564 | cf::cede_to_tick; |
3687 | my $fh = aio_open "$DATADIR/facedata", IO::AIO::O_RDONLY, 0 |
|
|
3688 | or cf::cleanup "$path/facedata: $!, cannot proceed."; |
|
|
3689 | |
|
|
3690 | get_slot 1, -100, "load_facedata"; # make sure we get a very big slot |
|
|
3691 | |
|
|
3692 | # BEGIN ATOMIC |
|
|
3693 | # from here on, everything must be atomic - no thread switch allowed |
|
|
3694 | my $t1 = EV::time; |
3565 | |
3695 | |
3566 | { |
3696 | { |
3567 | my $faces = delete $facedata->{faceinfo}; |
3697 | my $faces = $facedata->{faceinfo}; |
3568 | |
3698 | |
3569 | for my $face (sort keys %$faces) { |
3699 | for my $face (sort keys %$faces) { |
3570 | my $info = $faces->{$face}; |
3700 | my $info = $faces->{$face}; |
3571 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
3701 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
3572 | |
3702 | |
3573 | cf::face::set_visibility $idx, $info->{visibility}; |
3703 | cf::face::set_visibility $idx, $info->{visibility}; |
3574 | cf::face::set_magicmap $idx, $info->{magicmap}; |
3704 | cf::face::set_magicmap $idx, $info->{magicmap}; |
3575 | cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32}; |
3705 | cf::face::set_csum $idx, 0, $info->{hash64}; $cf::face::SIZE[0][$idx] = $info->{size64}; $cf::face::FOFS[0][$idx] = $info->{fofs64}; |
3576 | cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64}; |
3706 | cf::face::set_csum $idx, 1, $info->{hash32}; $cf::face::SIZE[1][$idx] = $info->{size32}; $cf::face::FOFS[1][$idx] = $info->{fofs32}; |
3577 | cf::face::set_data $idx, 2, $info->{glyph} , $info->{glyph} ; |
3707 | cf::face::set_csum $idx, 2, $info->{glyph}; $cf::face::DATA[2][$idx] = $info->{glyph}; |
3578 | $FACEHASH{$info->{hash64}} = $idx;#d# |
3708 | $cf::face::HASH{$info->{hash64}} = $idx; |
3579 | |
3709 | delete $cf::face::META[$idx]; |
3580 | cf::cede_to_tick; |
|
|
3581 | } |
3710 | } |
3582 | |
3711 | |
3583 | while (my ($face, $info) = each %$faces) { |
3712 | while (my ($face, $info) = each %$faces) { |
3584 | next unless $info->{smooth}; |
3713 | next unless $info->{smooth}; |
3585 | |
3714 | |
… | |
… | |
3590 | cf::face::set_smooth $idx, $smooth; |
3719 | cf::face::set_smooth $idx, $smooth; |
3591 | cf::face::set_smoothlevel $idx, $info->{smoothlevel}; |
3720 | cf::face::set_smoothlevel $idx, $info->{smoothlevel}; |
3592 | } else { |
3721 | } else { |
3593 | error "smooth face '$info->{smooth}' not found for face '$face'"; |
3722 | error "smooth face '$info->{smooth}' not found for face '$face'"; |
3594 | } |
3723 | } |
3595 | |
|
|
3596 | cf::cede_to_tick; |
|
|
3597 | } |
3724 | } |
3598 | } |
3725 | } |
3599 | |
3726 | |
3600 | { |
3727 | { |
3601 | my $anims = delete $facedata->{animinfo}; |
3728 | my $anims = $facedata->{animinfo}; |
3602 | |
3729 | |
3603 | while (my ($anim, $info) = each %$anims) { |
3730 | while (my ($anim, $info) = each %$anims) { |
3604 | cf::anim::set $anim, $info->{frames}, $info->{facings}; |
3731 | cf::anim::set $anim, $info->{frames}, $info->{facings}; |
3605 | cf::cede_to_tick; |
|
|
3606 | } |
3732 | } |
3607 | |
3733 | |
3608 | cf::anim::invalidate_all; # d'oh |
3734 | cf::anim::invalidate_all; # d'oh |
3609 | } |
3735 | } |
3610 | |
3736 | |
3611 | { |
3737 | { |
3612 | my $res = delete $facedata->{resource}; |
3738 | my $res = $facedata->{resource}; |
3613 | |
3739 | |
3614 | while (my ($name, $info) = each %$res) { |
3740 | while (my ($name, $info) = each %$res) { |
3615 | if (defined (my $type = $info->{type})) { |
3741 | if (defined (my $type = $info->{type})) { |
3616 | # TODO: different hash - must free and use new index, or cache ixface data queue |
3742 | # TODO: different hash - must free and use new index, or cache ixface data queue |
3617 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3743 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3618 | |
3744 | |
3619 | cf::face::set_type $idx, $type; |
3745 | cf::face::set_type $idx, $type; |
3620 | cf::face::set_data $idx, 0, $info->{data}, $info->{hash}; |
3746 | cf::face::set_csum $idx, 0, $info->{hash}; |
|
|
3747 | $cf::face::SIZE[0][$idx] = $info->{size}; |
|
|
3748 | $cf::face::FOFS[0][$idx] = $info->{fofs}; |
3621 | cf::face::set_meta $idx, $type & 1 ? undef : $info->{meta}; # preserve meta unless prepended already |
3749 | $cf::face::META[$idx] = $type & 1 ? undef : $info->{meta}; # preserve meta unless prepended already |
3622 | $FACEHASH{$info->{hash}} = $idx;#d# |
3750 | $cf::face::HASH{$info->{hash}} = $idx; |
3623 | } else { |
3751 | } else { |
3624 | # $RESOURCE{$name} = $info; # unused |
3752 | # $RESOURCE{$name} = $info; # unused |
3625 | } |
3753 | } |
3626 | |
|
|
3627 | cf::cede_to_tick; |
|
|
3628 | } |
3754 | } |
3629 | } |
3755 | } |
|
|
3756 | |
|
|
3757 | ($fh, $cf::face::DATAFH) = ($cf::face::DATAFH, $fh); |
|
|
3758 | |
|
|
3759 | # HACK to clear player env face cache, we need some signal framework |
|
|
3760 | # for this (global event?) |
|
|
3761 | %ext::player_env::MUSIC_FACE_CACHE = (); |
|
|
3762 | |
|
|
3763 | # END ATOMIC |
|
|
3764 | |
|
|
3765 | cf::debug "facedata atomic update time ", EV::time - $t1; |
3630 | |
3766 | |
3631 | cf::global->invoke (EVENT_GLOBAL_RESOURCE_UPDATE); |
3767 | cf::global->invoke (EVENT_GLOBAL_RESOURCE_UPDATE); |
|
|
3768 | |
|
|
3769 | aio_close $fh if $fh; # close old facedata |
3632 | |
3770 | |
3633 | 1 |
3771 | 1 |
3634 | } |
3772 | } |
3635 | |
3773 | |
3636 | register_exticmd fx_want => sub { |
3774 | register_exticmd fx_want => sub { |
… | |
… | |
3652 | } |
3790 | } |
3653 | |
3791 | |
3654 | sub reload_exp_table { |
3792 | sub reload_exp_table { |
3655 | _reload_exp_table; |
3793 | _reload_exp_table; |
3656 | |
3794 | |
|
|
3795 | cf::face::set |
3657 | set_face "res/exp_table" => FT_RSRC, |
3796 | "res/exp_table" => FT_RSRC, |
3658 | JSON::XS->new->utf8->canonical->encode ( |
3797 | JSON::XS->new->utf8->canonical->encode ( |
3659 | [map cf::level_to_min_exp $_, 1 .. cf::settings->max_level] |
3798 | [map cf::level_to_min_exp $_, 1 .. cf::settings->max_level] |
3660 | ); |
3799 | ); |
3661 | } |
3800 | } |
3662 | |
3801 | |
3663 | sub reload_materials { |
3802 | sub reload_materials { |
3664 | _reload_materials; |
3803 | _reload_materials; |
3665 | } |
3804 | } |
… | |
… | |
3677 | if exists $_->{match}; |
3816 | if exists $_->{match}; |
3678 | } |
3817 | } |
3679 | } |
3818 | } |
3680 | |
3819 | |
3681 | sub reload_facedata { |
3820 | sub reload_facedata { |
3682 | load_facedata "$DATADIR/facedata" |
3821 | load_facedata $DATADIR |
3683 | or die "unable to load facedata\n"; |
3822 | or die "unable to load facedata\n"; |
3684 | } |
3823 | } |
3685 | |
3824 | |
3686 | sub reload_archetypes { |
3825 | sub reload_archetypes { |
3687 | load_resource_file "$DATADIR/archetypes" |
3826 | load_resource_file "$DATADIR/archetypes" |
3688 | or die "unable to load archetypes\n"; |
3827 | or die "unable to load archetypes\n"; |
3689 | |
3828 | |
|
|
3829 | cf::face::set |
3690 | set_face "res/skill_info" => FT_RSRC, |
3830 | "res/skill_info" => FT_RSRC, |
3691 | JSON::XS->new->utf8->canonical->encode ( |
3831 | JSON::XS->new->utf8->canonical->encode ( |
3692 | [map [cf::arch::skillvec ($_)->name], 0 .. cf::arch::skillvec_size - 1] |
3832 | [map [cf::arch::skillvec ($_)->name], 0 .. cf::arch::skillvec_size - 1] |
3693 | ); |
3833 | ); |
|
|
3834 | |
|
|
3835 | cf::face::set |
3694 | set_face "res/spell_paths" => FT_RSRC, |
3836 | "res/spell_paths" => FT_RSRC, |
3695 | JSON::XS->new->utf8->canonical->encode ( |
3837 | JSON::XS->new->utf8->canonical->encode ( |
3696 | [map [cf::spellpathnames ($_)], 0 .. NRSPELLPATHS - 1] |
3838 | [map [cf::spellpathnames ($_)], 0 .. NRSPELLPATHS - 1] |
3697 | ); |
3839 | ); |
|
|
3840 | |
|
|
3841 | # command completion |
|
|
3842 | my @commands; |
|
|
3843 | |
|
|
3844 | for (0..cf::arch::skillvec_size - 1) { |
|
|
3845 | my $skill = cf::arch::skillvec $_; |
|
|
3846 | my $name = $skill->name; |
|
|
3847 | my $flags = cf::skill_flags $skill->subtype; |
|
|
3848 | |
|
|
3849 | push @commands, "ready_skill $name" if $flags & (SF_COMBAT | SF_RANGED | SF_GRACE); |
|
|
3850 | push @commands, "use_skill $name" if $flags & (SF_USE | SF_AUTARK | SF_GRACE); |
|
|
3851 | } |
|
|
3852 | |
|
|
3853 | cf::client::set_command_face skill => \@commands; |
3698 | } |
3854 | } |
3699 | |
3855 | |
3700 | sub reload_treasures { |
3856 | sub reload_treasures { |
3701 | load_resource_file "$DATADIR/treasures" |
3857 | load_resource_file "$DATADIR/treasures" |
3702 | or die "unable to load treasurelists\n"; |
3858 | or die "unable to load treasurelists\n"; |
… | |
… | |
3775 | seek $fh, 0, 0; |
3931 | seek $fh, 0, 0; |
3776 | print $fh $$; |
3932 | print $fh $$; |
3777 | } |
3933 | } |
3778 | |
3934 | |
3779 | sub main_loop { |
3935 | sub main_loop { |
3780 | trace "EV::loop starting\n"; |
3936 | trace "EV::run starting\n"; |
3781 | if (1) { |
3937 | if (1) { |
3782 | EV::loop; |
3938 | EV::run; |
3783 | } |
3939 | } |
3784 | trace "EV::loop returned\n"; |
3940 | trace "EV::run returned\n"; |
3785 | goto &main_loop unless $REALLY_UNLOOP; |
3941 | goto &main_loop unless $REALLY_UNLOOP; |
3786 | } |
3942 | } |
3787 | |
3943 | |
3788 | sub main { |
3944 | sub main { |
3789 | cf::init_globals; # initialise logging |
3945 | cf::init_globals; # initialise logging |
3790 | |
3946 | |
3791 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3947 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
|
|
3948 | LOG llevInfo, "Copyright (C) 2017-2018 Marc Alexander Lehmann / the Deliantra team."; |
3792 | LOG llevInfo, "Copyright (C) 2005-2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3949 | LOG llevInfo, "Copyright (C) 2005-2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3793 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3950 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3794 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3951 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3795 | |
3952 | |
3796 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3953 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3797 | |
3954 | |
3798 | # we must not ever block the main coroutine |
3955 | # we must not ever block the main coroutine |
3799 | $Coro::idle = sub { |
3956 | $Coro::idle = sub { |
3800 | Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# |
3957 | Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# |
3801 | (async { |
3958 | (async { |
3802 | $Coro::current->{desc} = "IDLE BUG HANDLER"; |
3959 | $Coro::current->{desc} = "IDLE BUG HANDLER"; |
3803 | EV::loop EV::LOOP_ONESHOT; |
3960 | EV::run EV::RUN_ONCE; |
3804 | })->prio (Coro::PRIO_MAX); |
3961 | })->prio (Coro::PRIO_MAX); |
3805 | }; |
3962 | }; |
3806 | |
3963 | |
3807 | evthread_start IO::AIO::poll_fileno; |
3964 | evthread_start IO::AIO::poll_fileno; |
3808 | |
3965 | |