… | |
… | |
4 | use strict; |
4 | use strict; |
5 | |
5 | |
6 | use Symbol; |
6 | use Symbol; |
7 | use List::Util; |
7 | use List::Util; |
8 | use Socket; |
8 | use Socket; |
9 | use EV; |
9 | use EV 1.86; |
10 | use Opcode; |
10 | use Opcode; |
11 | use Safe; |
11 | use Safe; |
12 | use Safe::Hole; |
12 | use Safe::Hole; |
13 | use Storable (); |
13 | use Storable (); |
14 | |
14 | |
15 | use Coro 4.1 (); |
15 | use Coro 4.32 (); |
16 | use Coro::State; |
16 | use Coro::State; |
17 | use Coro::Handle; |
17 | use Coro::Handle; |
18 | use Coro::EV; |
18 | use Coro::EV; |
19 | use Coro::Timer; |
19 | use Coro::Timer; |
20 | use Coro::Signal; |
20 | use Coro::Signal; |
21 | use Coro::Semaphore; |
21 | use Coro::Semaphore; |
22 | use Coro::AIO; |
22 | use Coro::AIO; |
|
|
23 | use Coro::BDB; |
23 | use Coro::Storable; |
24 | use Coro::Storable; |
24 | use Coro::Util (); |
25 | use Coro::Util (); |
25 | |
26 | |
26 | use JSON::XS 2.01 (); |
27 | use JSON::XS 2.01 (); |
27 | use BDB (); |
28 | use BDB (); |
28 | use Data::Dumper; |
29 | use Data::Dumper; |
29 | use Digest::MD5; |
30 | use Digest::MD5; |
30 | use Fcntl; |
31 | use Fcntl; |
31 | use YAML::Syck (); |
32 | use YAML (); |
32 | use IO::AIO 2.51 (); |
33 | use IO::AIO 2.51 (); |
33 | use Time::HiRes; |
34 | use Time::HiRes; |
34 | use Compress::LZF; |
35 | use Compress::LZF; |
35 | use Digest::MD5 (); |
36 | use Digest::MD5 (); |
36 | |
37 | |
37 | # configure various modules to our taste |
38 | # configure various modules to our taste |
38 | # |
39 | # |
39 | $Storable::canonical = 1; # reduce rsync transfers |
40 | $Storable::canonical = 1; # reduce rsync transfers |
40 | Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator |
41 | Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator |
41 | Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later |
42 | Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later |
42 | |
|
|
43 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
|
|
44 | $YAML::Syck::ImplicitUnicode = 1; |
|
|
45 | |
43 | |
46 | $Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority |
44 | $Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority |
47 | |
45 | |
48 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
46 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
49 | |
47 | |
… | |
… | |
68 | our $TMPDIR = "$LOCALDIR/" . tmpdir; |
66 | our $TMPDIR = "$LOCALDIR/" . tmpdir; |
69 | our $UNIQUEDIR = "$LOCALDIR/" . uniquedir; |
67 | our $UNIQUEDIR = "$LOCALDIR/" . uniquedir; |
70 | our $PLAYERDIR = "$LOCALDIR/" . playerdir; |
68 | our $PLAYERDIR = "$LOCALDIR/" . playerdir; |
71 | our $RANDOMDIR = "$LOCALDIR/random"; |
69 | our $RANDOMDIR = "$LOCALDIR/random"; |
72 | our $BDBDIR = "$LOCALDIR/db"; |
70 | our $BDBDIR = "$LOCALDIR/db"; |
|
|
71 | our %RESOURCE; |
73 | |
72 | |
74 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
73 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
75 | our $TICK_WATCHER; |
74 | our $TICK_WATCHER; |
76 | our $AIO_POLL_WATCHER; |
75 | our $AIO_POLL_WATCHER; |
77 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
76 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
… | |
… | |
191 | $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; |
190 | $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; |
192 | |
191 | |
193 | LOG llevError, $msg; |
192 | LOG llevError, $msg; |
194 | }; |
193 | }; |
195 | } |
194 | } |
|
|
195 | |
|
|
196 | $Coro::State::DIEHOOK = sub { |
|
|
197 | warn Carp::longmess $_[0]; |
|
|
198 | Coro::terminate; |
|
|
199 | }; |
196 | |
200 | |
197 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
201 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
198 | @safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; |
202 | @safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; |
199 | @safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable'; |
203 | @safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable'; |
200 | @safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable'; |
204 | @safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable'; |
… | |
… | |
967 | } |
971 | } |
968 | |
972 | |
969 | 0 |
973 | 0 |
970 | } |
974 | } |
971 | |
975 | |
972 | =item $bool = cf::global::invoke (EVENT_CLASS_XXX, ...) |
976 | =item $bool = cf::global->invoke (EVENT_CLASS_XXX, ...) |
973 | |
977 | |
974 | =item $bool = $attachable->invoke (EVENT_CLASS_XXX, ...) |
978 | =item $bool = $attachable->invoke (EVENT_CLASS_XXX, ...) |
975 | |
979 | |
976 | Generate an object-specific event with the given arguments. |
980 | Generate an object-specific event with the given arguments. |
977 | |
981 | |
… | |
… | |
3148 | { |
3152 | { |
3149 | my $faces = $facedata->{faceinfo}; |
3153 | my $faces = $facedata->{faceinfo}; |
3150 | |
3154 | |
3151 | while (my ($face, $info) = each %$faces) { |
3155 | while (my ($face, $info) = each %$faces) { |
3152 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
3156 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
|
|
3157 | |
3153 | cf::face::set_visibility $idx, $info->{visibility}; |
3158 | cf::face::set_visibility $idx, $info->{visibility}; |
3154 | cf::face::set_magicmap $idx, $info->{magicmap}; |
3159 | cf::face::set_magicmap $idx, $info->{magicmap}; |
3155 | cf::face::set_data $idx, 0, $info->{data32}, Digest::MD5::md5 $info->{data32}; |
3160 | cf::face::set_data $idx, 0, $info->{data32}, Digest::MD5::md5 $info->{data32}; |
3156 | cf::face::set_data $idx, 1, $info->{data64}, Digest::MD5::md5 $info->{data64}; |
3161 | cf::face::set_data $idx, 1, $info->{data64}, Digest::MD5::md5 $info->{data64}; |
3157 | |
3162 | |
3158 | cf::cede_to_tick; |
3163 | cf::cede_to_tick; |
3159 | } |
3164 | } |
3160 | |
3165 | |
3161 | while (my ($face, $info) = each %$faces) { |
3166 | while (my ($face, $info) = each %$faces) { |
3162 | next unless $info->{smooth}; |
3167 | next unless $info->{smooth}; |
|
|
3168 | |
3163 | my $idx = cf::face::find $face |
3169 | my $idx = cf::face::find $face |
3164 | or next; |
3170 | or next; |
|
|
3171 | |
3165 | if (my $smooth = cf::face::find $info->{smooth}) { |
3172 | if (my $smooth = cf::face::find $info->{smooth}) { |
3166 | cf::face::set_smooth $idx, $smooth; |
3173 | cf::face::set_smooth $idx, $smooth; |
3167 | cf::face::set_smoothlevel $idx, $info->{smoothlevel}; |
3174 | cf::face::set_smoothlevel $idx, $info->{smoothlevel}; |
3168 | } else { |
3175 | } else { |
3169 | warn "smooth face '$info->{smooth}' not found for face '$face'"; |
3176 | warn "smooth face '$info->{smooth}' not found for face '$face'"; |
… | |
… | |
3187 | { |
3194 | { |
3188 | # TODO: for gcfclient pleasure, we should give resources |
3195 | # TODO: for gcfclient pleasure, we should give resources |
3189 | # that gcfclient doesn't grok a >10000 face index. |
3196 | # that gcfclient doesn't grok a >10000 face index. |
3190 | my $res = $facedata->{resource}; |
3197 | my $res = $facedata->{resource}; |
3191 | |
3198 | |
3192 | my $soundconf = delete $res->{"res/sound.conf"}; |
|
|
3193 | |
|
|
3194 | while (my ($name, $info) = each %$res) { |
3199 | while (my ($name, $info) = each %$res) { |
|
|
3200 | if (defined $info->{type}) { |
3195 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3201 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3196 | my $data; |
3202 | my $data; |
3197 | |
3203 | |
3198 | if ($info->{type} & 1) { |
3204 | if ($info->{type} & 1) { |
3199 | # prepend meta info |
3205 | # prepend meta info |
3200 | |
3206 | |
3201 | my $meta = $enc->encode ({ |
3207 | my $meta = $enc->encode ({ |
3202 | name => $name, |
3208 | name => $name, |
3203 | %{ $info->{meta} || {} }, |
3209 | %{ $info->{meta} || {} }, |
3204 | }); |
3210 | }); |
3205 | |
3211 | |
3206 | $data = pack "(w/a*)*", $meta, $info->{data}; |
3212 | $data = pack "(w/a*)*", $meta, $info->{data}; |
|
|
3213 | } else { |
|
|
3214 | $data = $info->{data}; |
|
|
3215 | } |
|
|
3216 | |
|
|
3217 | cf::face::set_data $idx, 0, $data, Digest::MD5::md5 $data; |
|
|
3218 | cf::face::set_type $idx, $info->{type}; |
3207 | } else { |
3219 | } else { |
3208 | $data = $info->{data}; |
3220 | $RESOURCE{$name} = $info; |
3209 | } |
3221 | } |
3210 | |
3222 | |
3211 | cf::face::set_data $idx, 0, $data, Digest::MD5::md5 $data; |
|
|
3212 | cf::face::set_type $idx, $info->{type}; |
|
|
3213 | |
|
|
3214 | cf::cede_to_tick; |
3223 | cf::cede_to_tick; |
3215 | } |
3224 | } |
3216 | |
|
|
3217 | if ($soundconf) { |
|
|
3218 | $soundconf = $enc->decode (delete $soundconf->{data}); |
|
|
3219 | |
|
|
3220 | for (0 .. SOUND_CAST_SPELL_0 - 1) { |
|
|
3221 | my $sound = $soundconf->{compat}[$_] |
|
|
3222 | or next; |
|
|
3223 | |
|
|
3224 | my $face = cf::face::find "sound/$sound->[1]"; |
|
|
3225 | cf::sound::set $sound->[0] => $face; |
|
|
3226 | cf::sound::old_sound_index $_, $face; # gcfclient-compat |
|
|
3227 | } |
|
|
3228 | |
|
|
3229 | while (my ($k, $v) = each %{$soundconf->{event}}) { |
|
|
3230 | my $face = cf::face::find "sound/$v"; |
|
|
3231 | cf::sound::set $k => $face; |
|
|
3232 | } |
|
|
3233 | } |
|
|
3234 | } |
3225 | } |
|
|
3226 | |
|
|
3227 | cf::global->invoke (EVENT_GLOBAL_RESOURCE_UPDATE); |
3235 | |
3228 | |
3236 | 1 |
3229 | 1 |
3237 | } |
3230 | } |
|
|
3231 | |
|
|
3232 | cf::global->attach (on_resource_update => sub { |
|
|
3233 | if (my $soundconf = $RESOURCE{"res/sound.conf"}) { |
|
|
3234 | $soundconf = JSON::XS->new->utf8->relaxed->decode ($soundconf->{data}); |
|
|
3235 | |
|
|
3236 | for (0 .. SOUND_CAST_SPELL_0 - 1) { |
|
|
3237 | my $sound = $soundconf->{compat}[$_] |
|
|
3238 | or next; |
|
|
3239 | |
|
|
3240 | my $face = cf::face::find "sound/$sound->[1]"; |
|
|
3241 | cf::sound::set $sound->[0] => $face; |
|
|
3242 | cf::sound::old_sound_index $_, $face; # gcfclient-compat |
|
|
3243 | } |
|
|
3244 | |
|
|
3245 | while (my ($k, $v) = each %{$soundconf->{event}}) { |
|
|
3246 | my $face = cf::face::find "sound/$v"; |
|
|
3247 | cf::sound::set $k => $face; |
|
|
3248 | } |
|
|
3249 | } |
|
|
3250 | }); |
3238 | |
3251 | |
3239 | register_exticmd fx_want => sub { |
3252 | register_exticmd fx_want => sub { |
3240 | my ($ns, $want) = @_; |
3253 | my ($ns, $want) = @_; |
3241 | |
3254 | |
3242 | while (my ($k, $v) = each %$want) { |
3255 | while (my ($k, $v) = each %$want) { |
… | |
… | |
3297 | sub reload_config { |
3310 | sub reload_config { |
3298 | open my $fh, "<:utf8", "$CONFDIR/config" |
3311 | open my $fh, "<:utf8", "$CONFDIR/config" |
3299 | or return; |
3312 | or return; |
3300 | |
3313 | |
3301 | local $/; |
3314 | local $/; |
3302 | *CFG = YAML::Syck::Load <$fh>; |
3315 | *CFG = YAML::Load <$fh>; |
3303 | |
3316 | |
3304 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37]; |
3317 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37]; |
3305 | |
3318 | |
3306 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3319 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3307 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
3320 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
… | |
… | |
3602 | |
3615 | |
3603 | $NOW = $tick_start = EV::now; |
3616 | $NOW = $tick_start = EV::now; |
3604 | |
3617 | |
3605 | cf::server_tick; # one server iteration |
3618 | cf::server_tick; # one server iteration |
3606 | |
3619 | |
3607 | $RUNTIME += $TICK; |
3620 | $RUNTIME += $TICK; |
3608 | $NEXT_TICK += $TICK; |
3621 | $NEXT_TICK = $_[0]->at; |
3609 | |
3622 | |
3610 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3623 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3611 | $NEXT_RUNTIME_WRITE = $NOW + 10; |
3624 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
3612 | Coro::async_pool { |
3625 | Coro::async_pool { |
3613 | $Coro::current->{desc} = "runtime saver"; |
3626 | $Coro::current->{desc} = "runtime saver"; |
3614 | write_runtime |
3627 | write_runtime |
3615 | or warn "ERROR: unable to write runtime file: $!"; |
3628 | or warn "ERROR: unable to write runtime file: $!"; |
3616 | }; |
3629 | }; |
… | |
… | |
3629 | _post_tick; |
3642 | _post_tick; |
3630 | }; |
3643 | }; |
3631 | $TICK_WATCHER->priority (EV::MAXPRI); |
3644 | $TICK_WATCHER->priority (EV::MAXPRI); |
3632 | |
3645 | |
3633 | { |
3646 | { |
|
|
3647 | # configure BDB |
|
|
3648 | |
3634 | BDB::min_parallel 8; |
3649 | BDB::min_parallel 8; |
3635 | BDB::max_poll_time $TICK * 0.1; |
3650 | BDB::max_poll_reqs $TICK * 0.1; |
3636 | $BDB_POLL_WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb; |
3651 | $Coro::BDB::WATCHER->priority (1); |
3637 | |
|
|
3638 | BDB::set_sync_prepare { |
|
|
3639 | my $status; |
|
|
3640 | my $current = $Coro::current; |
|
|
3641 | ( |
|
|
3642 | sub { |
|
|
3643 | $status = $!; |
|
|
3644 | $current->ready; undef $current; |
|
|
3645 | }, |
|
|
3646 | sub { |
|
|
3647 | Coro::schedule while defined $current; |
|
|
3648 | $! = $status; |
|
|
3649 | }, |
|
|
3650 | ) |
|
|
3651 | }; |
|
|
3652 | |
3652 | |
3653 | unless ($DB_ENV) { |
3653 | unless ($DB_ENV) { |
3654 | $DB_ENV = BDB::db_env_create; |
3654 | $DB_ENV = BDB::db_env_create; |
3655 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC |
3655 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC |
3656 | | BDB::LOG_AUTOREMOVE, 1); |
3656 | | BDB::LOG_AUTOREMOVE, 1); |
… | |
… | |
3683 | BDB::db_env_memp_trickle $DB_ENV, 20, 0, sub { }; |
3683 | BDB::db_env_memp_trickle $DB_ENV, 20, 0, sub { }; |
3684 | }; |
3684 | }; |
3685 | } |
3685 | } |
3686 | |
3686 | |
3687 | { |
3687 | { |
|
|
3688 | # configure IO::AIO |
|
|
3689 | |
3688 | IO::AIO::min_parallel 8; |
3690 | IO::AIO::min_parallel 8; |
3689 | |
|
|
3690 | undef $Coro::AIO::WATCHER; |
|
|
3691 | IO::AIO::max_poll_time $TICK * 0.1; |
3691 | IO::AIO::max_poll_time $TICK * 0.1; |
3692 | $AIO_POLL_WATCHER = EV::io IO::AIO::poll_fileno, EV::READ, \&IO::AIO::poll_cb; |
3692 | $Coro::AIO::WATCHER->priority (1); |
3693 | } |
3693 | } |
3694 | |
3694 | |
3695 | my $_log_backtrace; |
3695 | my $_log_backtrace; |
3696 | |
3696 | |
3697 | sub _log_backtrace { |
3697 | sub _log_backtrace { |