… | |
… | |
21 | |
21 | |
22 | package cf; |
22 | package cf; |
23 | |
23 | |
24 | use 5.10.0; |
24 | use 5.10.0; |
25 | use utf8; |
25 | use utf8; |
26 | use strict "vars", "subs"; |
26 | use strict qw(vars subs); |
27 | |
27 | |
28 | use Symbol; |
28 | use Symbol; |
29 | use List::Util; |
29 | use List::Util; |
30 | use Socket; |
30 | use Socket; |
31 | use EV; |
31 | use EV; |
… | |
… | |
72 | |
72 | |
73 | # make sure c-lzf reinitialises itself |
73 | # make sure c-lzf reinitialises itself |
74 | Compress::LZF::set_serializer "Storable", "Storable::net_mstore", "Storable::mretrieve"; |
74 | Compress::LZF::set_serializer "Storable", "Storable::net_mstore", "Storable::mretrieve"; |
75 | Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later |
75 | Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later |
76 | |
76 | |
|
|
77 | # strictly for debugging |
|
|
78 | $SIG{QUIT} = sub { Carp::cluck "SIGQUIT" }; |
|
|
79 | |
77 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
80 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
78 | |
81 | |
79 | our %COMMAND = (); |
82 | our %COMMAND = (); |
80 | our %COMMAND_TIME = (); |
83 | our %COMMAND_TIME = (); |
81 | |
84 | |
… | |
… | |
105 | our %RESOURCE; |
108 | our %RESOURCE; |
106 | |
109 | |
107 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
110 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
108 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
111 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
109 | our $NEXT_TICK; |
112 | our $NEXT_TICK; |
110 | our $USE_FSYNC = 1; # use fsync to write maps - default off |
113 | our $USE_FSYNC = 1; # use fsync to write maps - default on |
111 | |
114 | |
112 | our $BDB_DEADLOCK_WATCHER; |
115 | our $BDB_DEADLOCK_WATCHER; |
113 | our $BDB_CHECKPOINT_WATCHER; |
116 | our $BDB_CHECKPOINT_WATCHER; |
114 | our $BDB_TRICKLE_WATCHER; |
117 | our $BDB_TRICKLE_WATCHER; |
115 | our $DB_ENV; |
118 | our $DB_ENV; |
116 | |
119 | |
|
|
120 | our @EXTRA_MODULES = qw(pod mapscript); |
|
|
121 | |
117 | our %CFG; |
122 | our %CFG; |
118 | |
123 | |
119 | our $UPTIME; $UPTIME ||= time; |
124 | our $UPTIME; $UPTIME ||= time; |
120 | our $RUNTIME; |
125 | our $RUNTIME; |
121 | our $NOW; |
126 | our $NOW; |
… | |
… | |
132 | our $JITTER; # average jitter |
137 | our $JITTER; # average jitter |
133 | our $TICK_START; # for load detecting purposes |
138 | our $TICK_START; # for load detecting purposes |
134 | |
139 | |
135 | our @POST_INIT; |
140 | our @POST_INIT; |
136 | |
141 | |
137 | our $REATTACH_ON_RELOAD; # ste to true to force object reattach on reload (slow) |
142 | our $REATTACH_ON_RELOAD; # set to true to force object reattach on reload (slow) |
|
|
143 | our $REALLY_UNLOOP; # never set to true, please :) |
138 | |
144 | |
139 | binmode STDOUT; |
145 | binmode STDOUT; |
140 | binmode STDERR; |
146 | binmode STDERR; |
141 | |
147 | |
142 | # read virtual server time, if available |
148 | # read virtual server time, if available |
143 | unless ($RUNTIME || !-e $RUNTIMEFILE) { |
149 | unless ($RUNTIME || !-e $RUNTIMEFILE) { |
144 | open my $fh, "<", $RUNTIMEFILE |
150 | open my $fh, "<", $RUNTIMEFILE |
145 | or die "unable to read $RUNTIMEFILE file: $!"; |
151 | or die "unable to read $RUNTIMEFILE file: $!"; |
146 | $RUNTIME = <$fh> + 0.; |
152 | $RUNTIME = <$fh> + 0.; |
147 | } |
153 | } |
|
|
154 | |
|
|
155 | eval "sub TICK() { $TICK } 1" or die; |
148 | |
156 | |
149 | mkdir $_ |
157 | mkdir $_ |
150 | for $LOCALDIR, $TMPDIR, $UNIQUEDIR, $PLAYERDIR, $RANDOMDIR, $BDBDIR; |
158 | for $LOCALDIR, $TMPDIR, $UNIQUEDIR, $PLAYERDIR, $RANDOMDIR, $BDBDIR; |
151 | |
159 | |
152 | our $EMERGENCY_POSITION; |
160 | our $EMERGENCY_POSITION; |
… | |
… | |
1147 | $decname, length $$rdata, scalar @$objs; |
1155 | $decname, length $$rdata, scalar @$objs; |
1148 | |
1156 | |
1149 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
1157 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
1150 | aio_chmod $fh, SAVE_MODE; |
1158 | aio_chmod $fh, SAVE_MODE; |
1151 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
1159 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
1152 | aio_fsync $fh if $cf::USE_FSYNC; |
1160 | if ($cf::USE_FSYNC) { |
|
|
1161 | aio_sync_file_range $fh, 0, 0, IO::AIO::SYNC_FILE_RANGE_WAIT_BEFORE | IO::AIO::SYNC_FILE_RANGE_WRITE | IO::AIO::SYNC_FILE_RANGE_WAIT_AFTER; |
|
|
1162 | aio_fsync $fh; |
|
|
1163 | } |
1153 | aio_close $fh; |
1164 | aio_close $fh; |
1154 | |
1165 | |
1155 | if (@$objs) { |
1166 | if (@$objs) { |
1156 | if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { |
1167 | if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { |
1157 | aio_chmod $fh, SAVE_MODE; |
1168 | aio_chmod $fh, SAVE_MODE; |
1158 | my $data = Coro::Storable::nfreeze { version => 1, objs => $objs }; |
1169 | my $data = Coro::Storable::nfreeze { version => 1, objs => $objs }; |
1159 | aio_write $fh, 0, (length $data), $data, 0; |
1170 | aio_write $fh, 0, (length $data), $data, 0; |
1160 | aio_fsync $fh if $cf::USE_FSYNC; |
1171 | if ($cf::USE_FSYNC) { |
|
|
1172 | aio_sync_file_range $fh, 0, 0, IO::AIO::SYNC_FILE_RANGE_WAIT_BEFORE | IO::AIO::SYNC_FILE_RANGE_WRITE | IO::AIO::SYNC_FILE_RANGE_WAIT_AFTER; |
|
|
1173 | aio_fsync $fh; |
|
|
1174 | } |
1161 | aio_close $fh; |
1175 | aio_close $fh; |
1162 | aio_rename "$filename.pst~", "$filename.pst"; |
1176 | aio_rename "$filename.pst~", "$filename.pst"; |
1163 | } |
1177 | } |
1164 | } else { |
1178 | } else { |
1165 | aio_unlink "$filename.pst"; |
1179 | aio_unlink "$filename.pst"; |
… | |
… | |
1316 | |
1330 | |
1317 | # "readahead" all extensions |
1331 | # "readahead" all extensions |
1318 | sub cache_extensions { |
1332 | sub cache_extensions { |
1319 | my $grp = IO::AIO::aio_group; |
1333 | my $grp = IO::AIO::aio_group; |
1320 | |
1334 | |
1321 | add $grp IO::AIO::aio_readdir $LIBDIR, sub { |
1335 | add $grp IO::AIO::aio_readdirx $LIBDIR, IO::AIO::READDIR_STAT_ORDER, sub { |
1322 | for (grep /\.ext$/, @{$_[0]}) { |
1336 | for (grep /\.ext$/, @{$_[0]}) { |
1323 | add $grp IO::AIO::aio_load "$LIBDIR/$_", my $data; |
1337 | add $grp IO::AIO::aio_load "$LIBDIR/$_", my $data; |
1324 | } |
1338 | } |
1325 | }; |
1339 | }; |
1326 | |
1340 | |
… | |
… | |
2384 | |
2398 | |
2385 | our $SAY_CHANNEL = { |
2399 | our $SAY_CHANNEL = { |
2386 | id => "say", |
2400 | id => "say", |
2387 | title => "Map", |
2401 | title => "Map", |
2388 | reply => "say ", |
2402 | reply => "say ", |
2389 | tooltip => "Things said to and replied from npcs near you and other players on the same map only.", |
2403 | tooltip => "Things said to and replied from NPCs near you and other players on the same map only.", |
2390 | }; |
2404 | }; |
2391 | |
2405 | |
2392 | our $CHAT_CHANNEL = { |
2406 | our $CHAT_CHANNEL = { |
2393 | id => "chat", |
2407 | id => "chat", |
2394 | title => "Chat", |
2408 | title => "Chat", |
… | |
… | |
2520 | |
2534 | |
2521 | $map->load; |
2535 | $map->load; |
2522 | $map->load_neighbours; |
2536 | $map->load_neighbours; |
2523 | |
2537 | |
2524 | return unless $self->contr->active; |
2538 | return unless $self->contr->active; |
2525 | $self->flag (cf::FLAG_DEBUG, 0);#d# temp |
|
|
2526 | $self->activate_recursive; |
|
|
2527 | |
2539 | |
2528 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
2540 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
2529 | $self->enter_map ($map, $x, $y); |
2541 | $self->enter_map ($map, $x, $y); |
|
|
2542 | |
|
|
2543 | # only activate afterwards, to support waiting in hooks |
|
|
2544 | $self->activate_recursive; |
2530 | } |
2545 | } |
2531 | |
2546 | |
2532 | =item $player_object->goto ($path, $x, $y[, $check->($map)[, $done->()]]) |
2547 | =item $player_object->goto ($path, $x, $y[, $check->($map)[, $done->()]]) |
2533 | |
2548 | |
2534 | Moves the player to the given map-path and coordinates by first freezing |
2549 | Moves the player to the given map-path and coordinates by first freezing |
… | |
… | |
2781 | id => "infobox", |
2796 | id => "infobox", |
2782 | title => "Skills", |
2797 | title => "Skills", |
2783 | reply => undef, |
2798 | reply => undef, |
2784 | tooltip => "Shows your experience per skill and item power", |
2799 | tooltip => "Shows your experience per skill and item power", |
2785 | }, |
2800 | }, |
|
|
2801 | "c/shopitems" => { |
|
|
2802 | id => "infobox", |
|
|
2803 | title => "Shop Items", |
|
|
2804 | reply => undef, |
|
|
2805 | tooltip => "Shows the items currently for sale in this shop", |
|
|
2806 | }, |
2786 | "c/resistances" => { |
2807 | "c/resistances" => { |
2787 | id => "infobox", |
2808 | id => "infobox", |
2788 | title => "Resistances", |
2809 | title => "Resistances", |
2789 | reply => undef, |
2810 | reply => undef, |
2790 | tooltip => "Shows your resistances", |
2811 | tooltip => "Shows your resistances", |
… | |
… | |
2792 | "c/pets" => { |
2813 | "c/pets" => { |
2793 | id => "infobox", |
2814 | id => "infobox", |
2794 | title => "Pets", |
2815 | title => "Pets", |
2795 | reply => undef, |
2816 | reply => undef, |
2796 | tooltip => "Shows information abotu your pets/a specific pet", |
2817 | tooltip => "Shows information abotu your pets/a specific pet", |
|
|
2818 | }, |
|
|
2819 | "c/perceiveself" => { |
|
|
2820 | id => "infobox", |
|
|
2821 | title => "Perceive Self", |
|
|
2822 | reply => undef, |
|
|
2823 | tooltip => "You gained detailed knowledge about yourself", |
2797 | }, |
2824 | }, |
2798 | "c/uptime" => { |
2825 | "c/uptime" => { |
2799 | id => "infobox", |
2826 | id => "infobox", |
2800 | title => "Uptime", |
2827 | title => "Uptime", |
2801 | reply => undef, |
2828 | reply => undef, |
… | |
… | |
3052 | The following functions and methods are available within a safe environment: |
3079 | The following functions and methods are available within a safe environment: |
3053 | |
3080 | |
3054 | cf::object |
3081 | cf::object |
3055 | contr pay_amount pay_player map x y force_find force_add destroy |
3082 | contr pay_amount pay_player map x y force_find force_add destroy |
3056 | insert remove name archname title slaying race decrease split |
3083 | insert remove name archname title slaying race decrease split |
|
|
3084 | value |
3057 | |
3085 | |
3058 | cf::object::player |
3086 | cf::object::player |
3059 | player |
3087 | player |
3060 | |
3088 | |
3061 | cf::player |
3089 | cf::player |
… | |
… | |
3067 | =cut |
3095 | =cut |
3068 | |
3096 | |
3069 | for ( |
3097 | for ( |
3070 | ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y |
3098 | ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y |
3071 | insert remove inv nrof name archname title slaying race |
3099 | insert remove inv nrof name archname title slaying race |
3072 | decrease split destroy change_exp)], |
3100 | decrease split destroy change_exp value msg lore send_msg)], |
3073 | ["cf::object::player" => qw(player)], |
3101 | ["cf::object::player" => qw(player)], |
3074 | ["cf::player" => qw(peaceful)], |
3102 | ["cf::player" => qw(peaceful send_msg)], |
3075 | ["cf::map" => qw(trigger)], |
3103 | ["cf::map" => qw(trigger)], |
3076 | ) { |
3104 | ) { |
3077 | no strict 'refs'; |
3105 | no strict 'refs'; |
3078 | my ($pkg, @funs) = @$_; |
3106 | my ($pkg, @funs) = @$_; |
3079 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
3107 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
… | |
… | |
3096 | my ($code, %vars) = @_; |
3124 | my ($code, %vars) = @_; |
3097 | |
3125 | |
3098 | my $qcode = $code; |
3126 | my $qcode = $code; |
3099 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
3127 | $qcode =~ s/"/‟/g; # not allowed in #line filenames |
3100 | $qcode =~ s/\n/\\n/g; |
3128 | $qcode =~ s/\n/\\n/g; |
|
|
3129 | |
|
|
3130 | %vars = (_dummy => 0) unless %vars; |
3101 | |
3131 | |
3102 | local $_; |
3132 | local $_; |
3103 | local @safe::cf::_safe_eval_args = values %vars; |
3133 | local @safe::cf::_safe_eval_args = values %vars; |
3104 | |
3134 | |
3105 | my $eval = |
3135 | my $eval = |
… | |
… | |
3373 | my $pid = <$fh>; |
3403 | my $pid = <$fh>; |
3374 | kill 9, $pid if $pid > 0; |
3404 | kill 9, $pid if $pid > 0; |
3375 | |
3405 | |
3376 | seek $fh, 0, 0; |
3406 | seek $fh, 0, 0; |
3377 | print $fh $$; |
3407 | print $fh $$; |
|
|
3408 | } |
|
|
3409 | |
|
|
3410 | sub main_loop { |
|
|
3411 | warn "EV::loop starting\n"; |
|
|
3412 | if (1) { |
|
|
3413 | EV::loop; |
|
|
3414 | } |
|
|
3415 | warn "EV::loop returned\n"; |
|
|
3416 | goto &main_loop unless $REALLY_UNLOOP; |
3378 | } |
3417 | } |
3379 | |
3418 | |
3380 | sub main { |
3419 | sub main { |
3381 | cf::init_globals; # initialise logging |
3420 | cf::init_globals; # initialise logging |
3382 | |
3421 | |
… | |
… | |
3422 | load_extensions; |
3461 | load_extensions; |
3423 | |
3462 | |
3424 | utime time, time, $RUNTIMEFILE; |
3463 | utime time, time, $RUNTIMEFILE; |
3425 | |
3464 | |
3426 | # no (long-running) fork's whatsoever before this point(!) |
3465 | # no (long-running) fork's whatsoever before this point(!) |
|
|
3466 | use POSIX (); |
3427 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3467 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3428 | |
3468 | |
3429 | (pop @POST_INIT)->(0) while @POST_INIT; |
3469 | (pop @POST_INIT)->(0) while @POST_INIT; |
3430 | }; |
3470 | }; |
3431 | |
3471 | |
3432 | EV::loop; |
3472 | main_loop; |
3433 | } |
3473 | } |
3434 | |
3474 | |
3435 | ############################################################################# |
3475 | ############################################################################# |
3436 | # initialisation and cleanup |
3476 | # initialisation and cleanup |
3437 | |
3477 | |
… | |
… | |
3693 | clear_package "safe::$_" |
3733 | clear_package "safe::$_" |
3694 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
3734 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
3695 | |
3735 | |
3696 | warn "unloading cf.pm \"a bit\""; |
3736 | warn "unloading cf.pm \"a bit\""; |
3697 | delete $INC{"cf.pm"}; |
3737 | delete $INC{"cf.pm"}; |
3698 | delete $INC{"cf/pod.pm"}; |
3738 | delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; |
3699 | |
3739 | |
3700 | # don't, removes xs symbols, too, |
3740 | # don't, removes xs symbols, too, |
3701 | # and global variables created in xs |
3741 | # and global variables created in xs |
3702 | #clear_package __PACKAGE__; |
3742 | #clear_package __PACKAGE__; |
3703 | |
3743 | |
… | |
… | |
3920 | LOG llevInfo, "[ABT] [suppressed]\n"; |
3960 | LOG llevInfo, "[ABT] [suppressed]\n"; |
3921 | } |
3961 | } |
3922 | } |
3962 | } |
3923 | |
3963 | |
3924 | # load additional modules |
3964 | # load additional modules |
3925 | use cf::pod; |
3965 | require "cf/$_.pm" for @EXTRA_MODULES; |
3926 | |
3966 | |
3927 | END { cf::emergency_save } |
3967 | END { cf::emergency_save } |
3928 | |
3968 | |
3929 | 1 |
3969 | 1 |
3930 | |
3970 | |