… | |
… | |
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 | |
… | |
… | |
27 | use JSON::XS 2.01 (); |
27 | use JSON::XS 2.01 (); |
28 | use BDB (); |
28 | use BDB (); |
29 | use Data::Dumper; |
29 | use Data::Dumper; |
30 | use Digest::MD5; |
30 | use Digest::MD5; |
31 | use Fcntl; |
31 | use Fcntl; |
32 | use YAML::Syck (); |
32 | use YAML (); |
33 | use IO::AIO 2.51 (); |
33 | use IO::AIO 2.51 (); |
34 | use Time::HiRes; |
34 | use Time::HiRes; |
35 | use Compress::LZF; |
35 | use Compress::LZF; |
36 | use Digest::MD5 (); |
36 | use Digest::MD5 (); |
37 | |
37 | |
38 | # configure various modules to our taste |
38 | # configure various modules to our taste |
39 | # |
39 | # |
40 | $Storable::canonical = 1; # reduce rsync transfers |
40 | $Storable::canonical = 1; # reduce rsync transfers |
41 | 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 |
42 | 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 |
43 | |
|
|
44 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
|
|
45 | $YAML::Syck::ImplicitUnicode = 1; |
|
|
46 | |
43 | |
47 | $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 |
48 | |
45 | |
49 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
46 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
50 | |
47 | |
… | |
… | |
69 | our $TMPDIR = "$LOCALDIR/" . tmpdir; |
66 | our $TMPDIR = "$LOCALDIR/" . tmpdir; |
70 | our $UNIQUEDIR = "$LOCALDIR/" . uniquedir; |
67 | our $UNIQUEDIR = "$LOCALDIR/" . uniquedir; |
71 | our $PLAYERDIR = "$LOCALDIR/" . playerdir; |
68 | our $PLAYERDIR = "$LOCALDIR/" . playerdir; |
72 | our $RANDOMDIR = "$LOCALDIR/random"; |
69 | our $RANDOMDIR = "$LOCALDIR/random"; |
73 | our $BDBDIR = "$LOCALDIR/db"; |
70 | our $BDBDIR = "$LOCALDIR/db"; |
|
|
71 | our %RESOURCE; |
74 | |
72 | |
75 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
73 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
76 | our $TICK_WATCHER; |
74 | our $TICK_WATCHER; |
77 | our $AIO_POLL_WATCHER; |
75 | our $AIO_POLL_WATCHER; |
78 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
76 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
… | |
… | |
192 | $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; |
193 | |
191 | |
194 | LOG llevError, $msg; |
192 | LOG llevError, $msg; |
195 | }; |
193 | }; |
196 | } |
194 | } |
|
|
195 | |
|
|
196 | $Coro::State::DIEHOOK = sub { |
|
|
197 | return unless $^S eq 0; # "eq", not "==" |
|
|
198 | |
|
|
199 | if ($Coro::current == $Coro::main) {#d# |
|
|
200 | warn "DIEHOOK called in main context, Coro bug?\n";#d# |
|
|
201 | return;#d# |
|
|
202 | }#d# |
|
|
203 | |
|
|
204 | # kill coroutine otherwise |
|
|
205 | warn Carp::longmess $_[0]; |
|
|
206 | Coro::terminate |
|
|
207 | }; |
|
|
208 | |
|
|
209 | $SIG{__DIE__} = sub { }; #d#? |
197 | |
210 | |
198 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
211 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
199 | @safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; |
212 | @safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; |
200 | @safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable'; |
213 | @safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable'; |
201 | @safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable'; |
214 | @safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable'; |
… | |
… | |
968 | } |
981 | } |
969 | |
982 | |
970 | 0 |
983 | 0 |
971 | } |
984 | } |
972 | |
985 | |
973 | =item $bool = cf::global::invoke (EVENT_CLASS_XXX, ...) |
986 | =item $bool = cf::global->invoke (EVENT_CLASS_XXX, ...) |
974 | |
987 | |
975 | =item $bool = $attachable->invoke (EVENT_CLASS_XXX, ...) |
988 | =item $bool = $attachable->invoke (EVENT_CLASS_XXX, ...) |
976 | |
989 | |
977 | Generate an object-specific event with the given arguments. |
990 | Generate an object-specific event with the given arguments. |
978 | |
991 | |
… | |
… | |
3149 | { |
3162 | { |
3150 | my $faces = $facedata->{faceinfo}; |
3163 | my $faces = $facedata->{faceinfo}; |
3151 | |
3164 | |
3152 | while (my ($face, $info) = each %$faces) { |
3165 | while (my ($face, $info) = each %$faces) { |
3153 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
3166 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
|
|
3167 | |
3154 | cf::face::set_visibility $idx, $info->{visibility}; |
3168 | cf::face::set_visibility $idx, $info->{visibility}; |
3155 | cf::face::set_magicmap $idx, $info->{magicmap}; |
3169 | cf::face::set_magicmap $idx, $info->{magicmap}; |
3156 | cf::face::set_data $idx, 0, $info->{data32}, Digest::MD5::md5 $info->{data32}; |
3170 | 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}; |
3171 | cf::face::set_data $idx, 1, $info->{data64}, Digest::MD5::md5 $info->{data64}; |
3158 | |
3172 | |
3159 | cf::cede_to_tick; |
3173 | cf::cede_to_tick; |
3160 | } |
3174 | } |
3161 | |
3175 | |
3162 | while (my ($face, $info) = each %$faces) { |
3176 | while (my ($face, $info) = each %$faces) { |
3163 | next unless $info->{smooth}; |
3177 | next unless $info->{smooth}; |
|
|
3178 | |
3164 | my $idx = cf::face::find $face |
3179 | my $idx = cf::face::find $face |
3165 | or next; |
3180 | or next; |
|
|
3181 | |
3166 | if (my $smooth = cf::face::find $info->{smooth}) { |
3182 | if (my $smooth = cf::face::find $info->{smooth}) { |
3167 | cf::face::set_smooth $idx, $smooth; |
3183 | cf::face::set_smooth $idx, $smooth; |
3168 | cf::face::set_smoothlevel $idx, $info->{smoothlevel}; |
3184 | cf::face::set_smoothlevel $idx, $info->{smoothlevel}; |
3169 | } else { |
3185 | } else { |
3170 | warn "smooth face '$info->{smooth}' not found for face '$face'"; |
3186 | warn "smooth face '$info->{smooth}' not found for face '$face'"; |
… | |
… | |
3188 | { |
3204 | { |
3189 | # TODO: for gcfclient pleasure, we should give resources |
3205 | # TODO: for gcfclient pleasure, we should give resources |
3190 | # that gcfclient doesn't grok a >10000 face index. |
3206 | # that gcfclient doesn't grok a >10000 face index. |
3191 | my $res = $facedata->{resource}; |
3207 | my $res = $facedata->{resource}; |
3192 | |
3208 | |
3193 | my $soundconf = delete $res->{"res/sound.conf"}; |
|
|
3194 | |
|
|
3195 | while (my ($name, $info) = each %$res) { |
3209 | while (my ($name, $info) = each %$res) { |
|
|
3210 | if (defined $info->{type}) { |
3196 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3211 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3197 | my $data; |
3212 | my $data; |
3198 | |
3213 | |
3199 | if ($info->{type} & 1) { |
3214 | if ($info->{type} & 1) { |
3200 | # prepend meta info |
3215 | # prepend meta info |
3201 | |
3216 | |
3202 | my $meta = $enc->encode ({ |
3217 | my $meta = $enc->encode ({ |
3203 | name => $name, |
3218 | name => $name, |
3204 | %{ $info->{meta} || {} }, |
3219 | %{ $info->{meta} || {} }, |
3205 | }); |
3220 | }); |
3206 | |
3221 | |
3207 | $data = pack "(w/a*)*", $meta, $info->{data}; |
3222 | $data = pack "(w/a*)*", $meta, $info->{data}; |
|
|
3223 | } else { |
|
|
3224 | $data = $info->{data}; |
|
|
3225 | } |
|
|
3226 | |
|
|
3227 | cf::face::set_data $idx, 0, $data, Digest::MD5::md5 $data; |
|
|
3228 | cf::face::set_type $idx, $info->{type}; |
3208 | } else { |
3229 | } else { |
3209 | $data = $info->{data}; |
3230 | $RESOURCE{$name} = $info; |
3210 | } |
3231 | } |
3211 | |
3232 | |
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; |
3233 | cf::cede_to_tick; |
3216 | } |
3234 | } |
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 | } |
3235 | } |
|
|
3236 | |
|
|
3237 | cf::global->invoke (EVENT_GLOBAL_RESOURCE_UPDATE); |
3236 | |
3238 | |
3237 | 1 |
3239 | 1 |
3238 | } |
3240 | } |
|
|
3241 | |
|
|
3242 | cf::global->attach (on_resource_update => sub { |
|
|
3243 | if (my $soundconf = $RESOURCE{"res/sound.conf"}) { |
|
|
3244 | $soundconf = JSON::XS->new->utf8->relaxed->decode ($soundconf->{data}); |
|
|
3245 | |
|
|
3246 | for (0 .. SOUND_CAST_SPELL_0 - 1) { |
|
|
3247 | my $sound = $soundconf->{compat}[$_] |
|
|
3248 | or next; |
|
|
3249 | |
|
|
3250 | my $face = cf::face::find "sound/$sound->[1]"; |
|
|
3251 | cf::sound::set $sound->[0] => $face; |
|
|
3252 | cf::sound::old_sound_index $_, $face; # gcfclient-compat |
|
|
3253 | } |
|
|
3254 | |
|
|
3255 | while (my ($k, $v) = each %{$soundconf->{event}}) { |
|
|
3256 | my $face = cf::face::find "sound/$v"; |
|
|
3257 | cf::sound::set $k => $face; |
|
|
3258 | } |
|
|
3259 | } |
|
|
3260 | }); |
3239 | |
3261 | |
3240 | register_exticmd fx_want => sub { |
3262 | register_exticmd fx_want => sub { |
3241 | my ($ns, $want) = @_; |
3263 | my ($ns, $want) = @_; |
3242 | |
3264 | |
3243 | while (my ($k, $v) = each %$want) { |
3265 | while (my ($k, $v) = each %$want) { |
… | |
… | |
3298 | sub reload_config { |
3320 | sub reload_config { |
3299 | open my $fh, "<:utf8", "$CONFDIR/config" |
3321 | open my $fh, "<:utf8", "$CONFDIR/config" |
3300 | or return; |
3322 | or return; |
3301 | |
3323 | |
3302 | local $/; |
3324 | local $/; |
3303 | *CFG = YAML::Syck::Load <$fh>; |
3325 | *CFG = YAML::Load <$fh>; |
3304 | |
3326 | |
3305 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37]; |
3327 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37]; |
3306 | |
3328 | |
3307 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3329 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3308 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
3330 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
… | |
… | |
3550 | sub reload_perl() { |
3572 | sub reload_perl() { |
3551 | # doing reload synchronously and two reloads happen back-to-back, |
3573 | # doing reload synchronously and two reloads happen back-to-back, |
3552 | # coro crashes during coro_state_free->destroy here. |
3574 | # coro crashes during coro_state_free->destroy here. |
3553 | |
3575 | |
3554 | $RELOAD_WATCHER ||= EV::timer 0, 0, sub { |
3576 | $RELOAD_WATCHER ||= EV::timer 0, 0, sub { |
|
|
3577 | do_reload_perl; |
3555 | undef $RELOAD_WATCHER; |
3578 | undef $RELOAD_WATCHER; |
3556 | do_reload_perl; |
|
|
3557 | }; |
3579 | }; |
3558 | } |
3580 | } |
3559 | |
3581 | |
3560 | register_command "reload" => sub { |
3582 | register_command "reload" => sub { |
3561 | my ($who, $arg) = @_; |
3583 | my ($who, $arg) = @_; |
… | |
… | |
3603 | |
3625 | |
3604 | $NOW = $tick_start = EV::now; |
3626 | $NOW = $tick_start = EV::now; |
3605 | |
3627 | |
3606 | cf::server_tick; # one server iteration |
3628 | cf::server_tick; # one server iteration |
3607 | |
3629 | |
3608 | $RUNTIME += $TICK; |
3630 | $RUNTIME += $TICK; |
3609 | $NEXT_TICK += $TICK; |
3631 | $NEXT_TICK = $_[0]->at; |
3610 | |
3632 | |
3611 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3633 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3612 | $NEXT_RUNTIME_WRITE = $NOW + 10; |
3634 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
3613 | Coro::async_pool { |
3635 | Coro::async_pool { |
3614 | $Coro::current->{desc} = "runtime saver"; |
3636 | $Coro::current->{desc} = "runtime saver"; |
3615 | write_runtime |
3637 | write_runtime |
3616 | or warn "ERROR: unable to write runtime file: $!"; |
3638 | or warn "ERROR: unable to write runtime file: $!"; |
3617 | }; |
3639 | }; |
… | |
… | |
3634 | { |
3656 | { |
3635 | # configure BDB |
3657 | # configure BDB |
3636 | |
3658 | |
3637 | BDB::min_parallel 8; |
3659 | BDB::min_parallel 8; |
3638 | BDB::max_poll_reqs $TICK * 0.1; |
3660 | BDB::max_poll_reqs $TICK * 0.1; |
|
|
3661 | $Coro::BDB::WATCHER->priority (1); |
3639 | |
3662 | |
3640 | unless ($DB_ENV) { |
3663 | unless ($DB_ENV) { |
3641 | $DB_ENV = BDB::db_env_create; |
3664 | $DB_ENV = BDB::db_env_create; |
3642 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC |
3665 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC |
3643 | | BDB::LOG_AUTOREMOVE, 1); |
3666 | | BDB::LOG_AUTOREMOVE, 1); |
… | |
… | |
3674 | { |
3697 | { |
3675 | # configure IO::AIO |
3698 | # configure IO::AIO |
3676 | |
3699 | |
3677 | IO::AIO::min_parallel 8; |
3700 | IO::AIO::min_parallel 8; |
3678 | IO::AIO::max_poll_time $TICK * 0.1; |
3701 | IO::AIO::max_poll_time $TICK * 0.1; |
|
|
3702 | $Coro::AIO::WATCHER->priority (1); |
3679 | } |
3703 | } |
3680 | |
3704 | |
3681 | my $_log_backtrace; |
3705 | my $_log_backtrace; |
3682 | |
3706 | |
3683 | sub _log_backtrace { |
3707 | sub _log_backtrace { |