… | |
… | |
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 | |
… | |
… | |
69 | our $TMPDIR = "$LOCALDIR/" . tmpdir; |
69 | our $TMPDIR = "$LOCALDIR/" . tmpdir; |
70 | our $UNIQUEDIR = "$LOCALDIR/" . uniquedir; |
70 | our $UNIQUEDIR = "$LOCALDIR/" . uniquedir; |
71 | our $PLAYERDIR = "$LOCALDIR/" . playerdir; |
71 | our $PLAYERDIR = "$LOCALDIR/" . playerdir; |
72 | our $RANDOMDIR = "$LOCALDIR/random"; |
72 | our $RANDOMDIR = "$LOCALDIR/random"; |
73 | our $BDBDIR = "$LOCALDIR/db"; |
73 | our $BDBDIR = "$LOCALDIR/db"; |
|
|
74 | our %RESOURCE; |
74 | |
75 | |
75 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
76 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
76 | our $TICK_WATCHER; |
77 | our $TICK_WATCHER; |
77 | our $AIO_POLL_WATCHER; |
78 | our $AIO_POLL_WATCHER; |
78 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
79 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
… | |
… | |
192 | $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; |
193 | $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; |
193 | |
194 | |
194 | LOG llevError, $msg; |
195 | LOG llevError, $msg; |
195 | }; |
196 | }; |
196 | } |
197 | } |
|
|
198 | |
|
|
199 | $Coro::State::DIEHOOK = sub { |
|
|
200 | warn Carp::longmess $_[0]; |
|
|
201 | Coro::terminate; |
|
|
202 | }; |
197 | |
203 | |
198 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
204 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
199 | @safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; |
205 | @safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; |
200 | @safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable'; |
206 | @safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable'; |
201 | @safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable'; |
207 | @safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable'; |
… | |
… | |
968 | } |
974 | } |
969 | |
975 | |
970 | 0 |
976 | 0 |
971 | } |
977 | } |
972 | |
978 | |
973 | =item $bool = cf::global::invoke (EVENT_CLASS_XXX, ...) |
979 | =item $bool = cf::global->invoke (EVENT_CLASS_XXX, ...) |
974 | |
980 | |
975 | =item $bool = $attachable->invoke (EVENT_CLASS_XXX, ...) |
981 | =item $bool = $attachable->invoke (EVENT_CLASS_XXX, ...) |
976 | |
982 | |
977 | Generate an object-specific event with the given arguments. |
983 | Generate an object-specific event with the given arguments. |
978 | |
984 | |
… | |
… | |
3149 | { |
3155 | { |
3150 | my $faces = $facedata->{faceinfo}; |
3156 | my $faces = $facedata->{faceinfo}; |
3151 | |
3157 | |
3152 | while (my ($face, $info) = each %$faces) { |
3158 | while (my ($face, $info) = each %$faces) { |
3153 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
3159 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
|
|
3160 | |
3154 | cf::face::set_visibility $idx, $info->{visibility}; |
3161 | cf::face::set_visibility $idx, $info->{visibility}; |
3155 | cf::face::set_magicmap $idx, $info->{magicmap}; |
3162 | cf::face::set_magicmap $idx, $info->{magicmap}; |
3156 | cf::face::set_data $idx, 0, $info->{data32}, Digest::MD5::md5 $info->{data32}; |
3163 | cf::face::set_data $idx, 0, $info->{data32}, Digest::MD5::md5 $info->{data32}; |
3157 | cf::face::set_data $idx, 1, $info->{data64}, Digest::MD5::md5 $info->{data64}; |
3164 | cf::face::set_data $idx, 1, $info->{data64}, Digest::MD5::md5 $info->{data64}; |
3158 | |
3165 | |
3159 | cf::cede_to_tick; |
3166 | cf::cede_to_tick; |
3160 | } |
3167 | } |
3161 | |
3168 | |
3162 | while (my ($face, $info) = each %$faces) { |
3169 | while (my ($face, $info) = each %$faces) { |
3163 | next unless $info->{smooth}; |
3170 | next unless $info->{smooth}; |
|
|
3171 | |
3164 | my $idx = cf::face::find $face |
3172 | my $idx = cf::face::find $face |
3165 | or next; |
3173 | or next; |
|
|
3174 | |
3166 | if (my $smooth = cf::face::find $info->{smooth}) { |
3175 | if (my $smooth = cf::face::find $info->{smooth}) { |
3167 | cf::face::set_smooth $idx, $smooth; |
3176 | cf::face::set_smooth $idx, $smooth; |
3168 | cf::face::set_smoothlevel $idx, $info->{smoothlevel}; |
3177 | cf::face::set_smoothlevel $idx, $info->{smoothlevel}; |
3169 | } else { |
3178 | } else { |
3170 | warn "smooth face '$info->{smooth}' not found for face '$face'"; |
3179 | warn "smooth face '$info->{smooth}' not found for face '$face'"; |
… | |
… | |
3188 | { |
3197 | { |
3189 | # TODO: for gcfclient pleasure, we should give resources |
3198 | # TODO: for gcfclient pleasure, we should give resources |
3190 | # that gcfclient doesn't grok a >10000 face index. |
3199 | # that gcfclient doesn't grok a >10000 face index. |
3191 | my $res = $facedata->{resource}; |
3200 | my $res = $facedata->{resource}; |
3192 | |
3201 | |
3193 | my $soundconf = delete $res->{"res/sound.conf"}; |
|
|
3194 | |
|
|
3195 | while (my ($name, $info) = each %$res) { |
3202 | while (my ($name, $info) = each %$res) { |
|
|
3203 | if (defined $info->{type}) { |
3196 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3204 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3197 | my $data; |
3205 | my $data; |
3198 | |
3206 | |
3199 | if ($info->{type} & 1) { |
3207 | if ($info->{type} & 1) { |
3200 | # prepend meta info |
3208 | # prepend meta info |
3201 | |
3209 | |
3202 | my $meta = $enc->encode ({ |
3210 | my $meta = $enc->encode ({ |
3203 | name => $name, |
3211 | name => $name, |
3204 | %{ $info->{meta} || {} }, |
3212 | %{ $info->{meta} || {} }, |
3205 | }); |
3213 | }); |
3206 | |
3214 | |
3207 | $data = pack "(w/a*)*", $meta, $info->{data}; |
3215 | $data = pack "(w/a*)*", $meta, $info->{data}; |
|
|
3216 | } else { |
|
|
3217 | $data = $info->{data}; |
|
|
3218 | } |
|
|
3219 | |
|
|
3220 | cf::face::set_data $idx, 0, $data, Digest::MD5::md5 $data; |
|
|
3221 | cf::face::set_type $idx, $info->{type}; |
3208 | } else { |
3222 | } else { |
3209 | $data = $info->{data}; |
3223 | $RESOURCE{$name} = $info; |
3210 | } |
3224 | } |
3211 | |
3225 | |
3212 | cf::face::set_data $idx, 0, $data, Digest::MD5::md5 $data; |
|
|
3213 | cf::face::set_type $idx, $info->{type}; |
|
|
3214 | |
|
|
3215 | cf::cede_to_tick; |
3226 | cf::cede_to_tick; |
3216 | } |
3227 | } |
3217 | |
|
|
3218 | if ($soundconf) { |
|
|
3219 | $soundconf = $enc->decode (delete $soundconf->{data}); |
|
|
3220 | |
|
|
3221 | for (0 .. SOUND_CAST_SPELL_0 - 1) { |
|
|
3222 | my $sound = $soundconf->{compat}[$_] |
|
|
3223 | or next; |
|
|
3224 | |
|
|
3225 | my $face = cf::face::find "sound/$sound->[1]"; |
|
|
3226 | cf::sound::set $sound->[0] => $face; |
|
|
3227 | cf::sound::old_sound_index $_, $face; # gcfclient-compat |
|
|
3228 | } |
|
|
3229 | |
|
|
3230 | while (my ($k, $v) = each %{$soundconf->{event}}) { |
|
|
3231 | my $face = cf::face::find "sound/$v"; |
|
|
3232 | cf::sound::set $k => $face; |
|
|
3233 | } |
|
|
3234 | } |
|
|
3235 | } |
3228 | } |
|
|
3229 | |
|
|
3230 | cf::global->invoke (EVENT_GLOBAL_RESOURCE_UPDATE); |
3236 | |
3231 | |
3237 | 1 |
3232 | 1 |
3238 | } |
3233 | } |
|
|
3234 | |
|
|
3235 | cf::global->attach (on_resource_update => sub { |
|
|
3236 | if (my $soundconf = $RESOURCE{"res/sound.conf"}) { |
|
|
3237 | $soundconf = JSON::XS->new->utf8->relaxed->decode ($soundconf->{data}); |
|
|
3238 | |
|
|
3239 | for (0 .. SOUND_CAST_SPELL_0 - 1) { |
|
|
3240 | my $sound = $soundconf->{compat}[$_] |
|
|
3241 | or next; |
|
|
3242 | |
|
|
3243 | my $face = cf::face::find "sound/$sound->[1]"; |
|
|
3244 | cf::sound::set $sound->[0] => $face; |
|
|
3245 | cf::sound::old_sound_index $_, $face; # gcfclient-compat |
|
|
3246 | } |
|
|
3247 | |
|
|
3248 | while (my ($k, $v) = each %{$soundconf->{event}}) { |
|
|
3249 | my $face = cf::face::find "sound/$v"; |
|
|
3250 | cf::sound::set $k => $face; |
|
|
3251 | } |
|
|
3252 | } |
|
|
3253 | }); |
3239 | |
3254 | |
3240 | register_exticmd fx_want => sub { |
3255 | register_exticmd fx_want => sub { |
3241 | my ($ns, $want) = @_; |
3256 | my ($ns, $want) = @_; |
3242 | |
3257 | |
3243 | while (my ($k, $v) = each %$want) { |
3258 | while (my ($k, $v) = each %$want) { |
… | |
… | |
3603 | |
3618 | |
3604 | $NOW = $tick_start = EV::now; |
3619 | $NOW = $tick_start = EV::now; |
3605 | |
3620 | |
3606 | cf::server_tick; # one server iteration |
3621 | cf::server_tick; # one server iteration |
3607 | |
3622 | |
3608 | $RUNTIME += $TICK; |
3623 | $RUNTIME += $TICK; |
3609 | $NEXT_TICK += $TICK; |
3624 | $NEXT_TICK = $_[0]->at; |
3610 | |
3625 | |
3611 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3626 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3612 | $NEXT_RUNTIME_WRITE = $NOW + 10; |
3627 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
3613 | Coro::async_pool { |
3628 | Coro::async_pool { |
3614 | $Coro::current->{desc} = "runtime saver"; |
3629 | $Coro::current->{desc} = "runtime saver"; |
3615 | write_runtime |
3630 | write_runtime |
3616 | or warn "ERROR: unable to write runtime file: $!"; |
3631 | or warn "ERROR: unable to write runtime file: $!"; |
3617 | }; |
3632 | }; |
… | |
… | |
3630 | _post_tick; |
3645 | _post_tick; |
3631 | }; |
3646 | }; |
3632 | $TICK_WATCHER->priority (EV::MAXPRI); |
3647 | $TICK_WATCHER->priority (EV::MAXPRI); |
3633 | |
3648 | |
3634 | { |
3649 | { |
|
|
3650 | # configure BDB |
|
|
3651 | |
3635 | BDB::min_parallel 8; |
3652 | BDB::min_parallel 8; |
3636 | BDB::max_poll_reqs $TICK * 0.1; |
3653 | BDB::max_poll_reqs $TICK * 0.1; |
|
|
3654 | $Coro::BDB::WATCHER->priority (1); |
3637 | |
3655 | |
3638 | unless ($DB_ENV) { |
3656 | unless ($DB_ENV) { |
3639 | $DB_ENV = BDB::db_env_create; |
3657 | $DB_ENV = BDB::db_env_create; |
3640 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC |
3658 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC |
3641 | | BDB::LOG_AUTOREMOVE, 1); |
3659 | | BDB::LOG_AUTOREMOVE, 1); |
… | |
… | |
3668 | BDB::db_env_memp_trickle $DB_ENV, 20, 0, sub { }; |
3686 | BDB::db_env_memp_trickle $DB_ENV, 20, 0, sub { }; |
3669 | }; |
3687 | }; |
3670 | } |
3688 | } |
3671 | |
3689 | |
3672 | { |
3690 | { |
|
|
3691 | # configure IO::AIO |
|
|
3692 | |
3673 | IO::AIO::min_parallel 8; |
3693 | IO::AIO::min_parallel 8; |
3674 | |
|
|
3675 | undef $Coro::AIO::WATCHER; |
|
|
3676 | IO::AIO::max_poll_time $TICK * 0.1; |
3694 | IO::AIO::max_poll_time $TICK * 0.1; |
3677 | $AIO_POLL_WATCHER = EV::io IO::AIO::poll_fileno, EV::READ, \&IO::AIO::poll_cb; |
3695 | $Coro::AIO::WATCHER->priority (1); |
3678 | } |
3696 | } |
3679 | |
3697 | |
3680 | my $_log_backtrace; |
3698 | my $_log_backtrace; |
3681 | |
3699 | |
3682 | sub _log_backtrace { |
3700 | sub _log_backtrace { |