… | |
… | |
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 | |
… | |
… | |
85 | our %EXT_CORO = (); # coroutines bound to extensions |
88 | our %EXT_CORO = (); # coroutines bound to extensions |
86 | our %EXT_MAP = (); # pluggable maps |
89 | our %EXT_MAP = (); # pluggable maps |
87 | |
90 | |
88 | our $RELOAD; # number of reloads so far, non-zero while in reload |
91 | our $RELOAD; # number of reloads so far, non-zero while in reload |
89 | our @EVENT; |
92 | our @EVENT; |
|
|
93 | our @REFLECT; # set by XS |
|
|
94 | our %REFLECT; # set by us |
90 | |
95 | |
91 | our $CONFDIR = confdir; |
96 | our $CONFDIR = confdir; |
92 | our $DATADIR = datadir; |
97 | our $DATADIR = datadir; |
93 | our $LIBDIR = "$DATADIR/ext"; |
98 | our $LIBDIR = "$DATADIR/ext"; |
94 | our $PODDIR = "$DATADIR/pod"; |
99 | our $PODDIR = "$DATADIR/pod"; |
… | |
… | |
105 | our %RESOURCE; |
110 | our %RESOURCE; |
106 | |
111 | |
107 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
112 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
108 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
113 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
109 | our $NEXT_TICK; |
114 | our $NEXT_TICK; |
110 | our $USE_FSYNC = 1; # use fsync to write maps - default off |
115 | our $USE_FSYNC = 1; # use fsync to write maps - default on |
111 | |
116 | |
112 | our $BDB_DEADLOCK_WATCHER; |
117 | our $BDB_DEADLOCK_WATCHER; |
113 | our $BDB_CHECKPOINT_WATCHER; |
118 | our $BDB_CHECKPOINT_WATCHER; |
114 | our $BDB_TRICKLE_WATCHER; |
119 | our $BDB_TRICKLE_WATCHER; |
115 | our $DB_ENV; |
120 | our $DB_ENV; |
… | |
… | |
134 | our $JITTER; # average jitter |
139 | our $JITTER; # average jitter |
135 | our $TICK_START; # for load detecting purposes |
140 | our $TICK_START; # for load detecting purposes |
136 | |
141 | |
137 | our @POST_INIT; |
142 | our @POST_INIT; |
138 | |
143 | |
139 | our $REATTACH_ON_RELOAD; # ste to true to force object reattach on reload (slow) |
144 | our $REATTACH_ON_RELOAD; # set to true to force object reattach on reload (slow) |
|
|
145 | our $REALLY_UNLOOP; # never set to true, please :) |
140 | |
146 | |
141 | binmode STDOUT; |
147 | binmode STDOUT; |
142 | binmode STDERR; |
148 | binmode STDERR; |
143 | |
149 | |
144 | # read virtual server time, if available |
150 | # read virtual server time, if available |
… | |
… | |
157 | |
163 | |
158 | sub cf::map::normalise; |
164 | sub cf::map::normalise; |
159 | |
165 | |
160 | ############################################################################# |
166 | ############################################################################# |
161 | |
167 | |
|
|
168 | %REFLECT = (); |
|
|
169 | for (@REFLECT) { |
|
|
170 | my $reflect = JSON::XS::decode_json $_; |
|
|
171 | $REFLECT{$reflect->{class}} = $reflect; |
|
|
172 | } |
|
|
173 | |
|
|
174 | # this is decidedly evil |
|
|
175 | $REFLECT{object}{flags} = [grep $_, map /^FLAG_([A-Z0-9_]+)$/ && lc $1, keys %{"cf::"}]; |
|
|
176 | |
|
|
177 | ############################################################################# |
|
|
178 | |
162 | =head2 GLOBAL VARIABLES |
179 | =head2 GLOBAL VARIABLES |
163 | |
180 | |
164 | =over 4 |
181 | =over 4 |
165 | |
182 | |
166 | =item $cf::UPTIME |
183 | =item $cf::UPTIME |
… | |
… | |
214 | |
231 | |
215 | This array contains the results of the last C<invoke ()> call. When |
232 | This array contains the results of the last C<invoke ()> call. When |
216 | C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of |
233 | C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of |
217 | that call. |
234 | that call. |
218 | |
235 | |
|
|
236 | =item %cf::REFLECT |
|
|
237 | |
|
|
238 | Contains, for each (C++) class name, a hash reference with information |
|
|
239 | about object members (methods, scalars, arrays and flags) and other |
|
|
240 | metadata, which is useful for introspection. |
|
|
241 | |
219 | =back |
242 | =back |
220 | |
243 | |
221 | =cut |
244 | =cut |
222 | |
245 | |
223 | BEGIN { |
246 | $Coro::State::WARNHOOK = sub { |
224 | *CORE::GLOBAL::warn = sub { |
|
|
225 | my $msg = join "", @_; |
247 | my $msg = join "", @_; |
226 | |
248 | |
227 | $msg .= "\n" |
249 | $msg .= "\n" |
228 | unless $msg =~ /\n$/; |
250 | unless $msg =~ /\n$/; |
229 | |
251 | |
230 | $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; |
252 | $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; |
231 | |
253 | |
232 | LOG llevError, $msg; |
254 | LOG llevError, $msg; |
233 | }; |
255 | }; |
234 | } |
|
|
235 | |
256 | |
236 | $Coro::State::DIEHOOK = sub { |
257 | $Coro::State::DIEHOOK = sub { |
237 | return unless $^S eq 0; # "eq", not "==" |
258 | return unless $^S eq 0; # "eq", not "==" |
|
|
259 | |
|
|
260 | warn Carp::longmess $_[0]; |
238 | |
261 | |
239 | if ($Coro::current == $Coro::main) {#d# |
262 | if ($Coro::current == $Coro::main) {#d# |
240 | warn "DIEHOOK called in main context, Coro bug?\n";#d# |
263 | warn "DIEHOOK called in main context, Coro bug?\n";#d# |
241 | return;#d# |
264 | return;#d# |
242 | }#d# |
265 | }#d# |
243 | |
266 | |
244 | # kill coroutine otherwise |
267 | # kill coroutine otherwise |
245 | warn Carp::longmess $_[0]; |
|
|
246 | Coro::terminate |
268 | Coro::terminate |
247 | }; |
269 | }; |
248 | |
|
|
249 | $SIG{__DIE__} = sub { }; #d#? |
|
|
250 | |
270 | |
251 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
271 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
252 | @safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; |
272 | @safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; |
253 | @safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable'; |
273 | @safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable'; |
254 | @safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable'; |
274 | @safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable'; |
… | |
… | |
1151 | $decname, length $$rdata, scalar @$objs; |
1171 | $decname, length $$rdata, scalar @$objs; |
1152 | |
1172 | |
1153 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
1173 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
1154 | aio_chmod $fh, SAVE_MODE; |
1174 | aio_chmod $fh, SAVE_MODE; |
1155 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
1175 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
1156 | aio_fsync $fh if $cf::USE_FSYNC; |
1176 | if ($cf::USE_FSYNC) { |
|
|
1177 | 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; |
|
|
1178 | aio_fsync $fh; |
|
|
1179 | } |
1157 | aio_close $fh; |
1180 | aio_close $fh; |
1158 | |
1181 | |
1159 | if (@$objs) { |
1182 | if (@$objs) { |
1160 | if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { |
1183 | if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { |
1161 | aio_chmod $fh, SAVE_MODE; |
1184 | aio_chmod $fh, SAVE_MODE; |
1162 | my $data = Coro::Storable::nfreeze { version => 1, objs => $objs }; |
1185 | my $data = Coro::Storable::nfreeze { version => 1, objs => $objs }; |
1163 | aio_write $fh, 0, (length $data), $data, 0; |
1186 | aio_write $fh, 0, (length $data), $data, 0; |
1164 | aio_fsync $fh if $cf::USE_FSYNC; |
1187 | if ($cf::USE_FSYNC) { |
|
|
1188 | 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; |
|
|
1189 | aio_fsync $fh; |
|
|
1190 | } |
1165 | aio_close $fh; |
1191 | aio_close $fh; |
1166 | aio_rename "$filename.pst~", "$filename.pst"; |
1192 | aio_rename "$filename.pst~", "$filename.pst"; |
1167 | } |
1193 | } |
1168 | } else { |
1194 | } else { |
1169 | aio_unlink "$filename.pst"; |
1195 | aio_unlink "$filename.pst"; |
… | |
… | |
1320 | |
1346 | |
1321 | # "readahead" all extensions |
1347 | # "readahead" all extensions |
1322 | sub cache_extensions { |
1348 | sub cache_extensions { |
1323 | my $grp = IO::AIO::aio_group; |
1349 | my $grp = IO::AIO::aio_group; |
1324 | |
1350 | |
1325 | add $grp IO::AIO::aio_readdir $LIBDIR, sub { |
1351 | add $grp IO::AIO::aio_readdirx $LIBDIR, IO::AIO::READDIR_STAT_ORDER, sub { |
1326 | for (grep /\.ext$/, @{$_[0]}) { |
1352 | for (grep /\.ext$/, @{$_[0]}) { |
1327 | add $grp IO::AIO::aio_load "$LIBDIR/$_", my $data; |
1353 | add $grp IO::AIO::aio_load "$LIBDIR/$_", my $data; |
1328 | } |
1354 | } |
1329 | }; |
1355 | }; |
1330 | |
1356 | |
… | |
… | |
2213 | |
2239 | |
2214 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
2240 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
2215 | |
2241 | |
2216 | return if $self->players; |
2242 | return if $self->players; |
2217 | |
2243 | |
2218 | warn "resetting map ", $self->path; |
2244 | warn "resetting map ", $self->path, "\n"; |
2219 | |
2245 | |
2220 | $self->in_memory (cf::MAP_SWAPPED); |
2246 | $self->in_memory (cf::MAP_SWAPPED); |
2221 | |
2247 | |
2222 | # need to save uniques path |
2248 | # need to save uniques path |
2223 | unless ($self->{deny_save}) { |
2249 | unless ($self->{deny_save}) { |
… | |
… | |
2524 | |
2550 | |
2525 | $map->load; |
2551 | $map->load; |
2526 | $map->load_neighbours; |
2552 | $map->load_neighbours; |
2527 | |
2553 | |
2528 | return unless $self->contr->active; |
2554 | return unless $self->contr->active; |
2529 | $self->flag (cf::FLAG_DEBUG, 0);#d# temp |
|
|
2530 | $self->activate_recursive; |
|
|
2531 | |
2555 | |
2532 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
2556 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
2533 | $self->enter_map ($map, $x, $y); |
2557 | $self->enter_map ($map, $x, $y); |
|
|
2558 | |
|
|
2559 | # only activate afterwards, to support waiting in hooks |
|
|
2560 | $self->activate_recursive; |
2534 | } |
2561 | } |
2535 | |
2562 | |
2536 | =item $player_object->goto ($path, $x, $y[, $check->($map)[, $done->()]]) |
2563 | =item $player_object->goto ($path, $x, $y[, $check->($map)[, $done->()]]) |
2537 | |
2564 | |
2538 | Moves the player to the given map-path and coordinates by first freezing |
2565 | Moves the player to the given map-path and coordinates by first freezing |
… | |
… | |
2785 | id => "infobox", |
2812 | id => "infobox", |
2786 | title => "Skills", |
2813 | title => "Skills", |
2787 | reply => undef, |
2814 | reply => undef, |
2788 | tooltip => "Shows your experience per skill and item power", |
2815 | tooltip => "Shows your experience per skill and item power", |
2789 | }, |
2816 | }, |
|
|
2817 | "c/shopitems" => { |
|
|
2818 | id => "infobox", |
|
|
2819 | title => "Shop Items", |
|
|
2820 | reply => undef, |
|
|
2821 | tooltip => "Shows the items currently for sale in this shop", |
|
|
2822 | }, |
2790 | "c/resistances" => { |
2823 | "c/resistances" => { |
2791 | id => "infobox", |
2824 | id => "infobox", |
2792 | title => "Resistances", |
2825 | title => "Resistances", |
2793 | reply => undef, |
2826 | reply => undef, |
2794 | tooltip => "Shows your resistances", |
2827 | tooltip => "Shows your resistances", |
… | |
… | |
2796 | "c/pets" => { |
2829 | "c/pets" => { |
2797 | id => "infobox", |
2830 | id => "infobox", |
2798 | title => "Pets", |
2831 | title => "Pets", |
2799 | reply => undef, |
2832 | reply => undef, |
2800 | tooltip => "Shows information abotu your pets/a specific pet", |
2833 | tooltip => "Shows information abotu your pets/a specific pet", |
|
|
2834 | }, |
|
|
2835 | "c/perceiveself" => { |
|
|
2836 | id => "infobox", |
|
|
2837 | title => "Perceive Self", |
|
|
2838 | reply => undef, |
|
|
2839 | tooltip => "You gained detailed knowledge about yourself", |
2801 | }, |
2840 | }, |
2802 | "c/uptime" => { |
2841 | "c/uptime" => { |
2803 | id => "infobox", |
2842 | id => "infobox", |
2804 | title => "Uptime", |
2843 | title => "Uptime", |
2805 | reply => undef, |
2844 | reply => undef, |
… | |
… | |
3382 | |
3421 | |
3383 | seek $fh, 0, 0; |
3422 | seek $fh, 0, 0; |
3384 | print $fh $$; |
3423 | print $fh $$; |
3385 | } |
3424 | } |
3386 | |
3425 | |
|
|
3426 | sub main_loop { |
|
|
3427 | warn "EV::loop starting\n"; |
|
|
3428 | if (1) { |
|
|
3429 | EV::loop; |
|
|
3430 | } |
|
|
3431 | warn "EV::loop returned\n"; |
|
|
3432 | goto &main_loop unless $REALLY_UNLOOP; |
|
|
3433 | } |
|
|
3434 | |
3387 | sub main { |
3435 | sub main { |
3388 | cf::init_globals; # initialise logging |
3436 | cf::init_globals; # initialise logging |
3389 | |
3437 | |
3390 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3438 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3391 | LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3439 | LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
… | |
… | |
3429 | load_extensions; |
3477 | load_extensions; |
3430 | |
3478 | |
3431 | utime time, time, $RUNTIMEFILE; |
3479 | utime time, time, $RUNTIMEFILE; |
3432 | |
3480 | |
3433 | # no (long-running) fork's whatsoever before this point(!) |
3481 | # no (long-running) fork's whatsoever before this point(!) |
|
|
3482 | use POSIX (); |
3434 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3483 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3435 | |
3484 | |
3436 | (pop @POST_INIT)->(0) while @POST_INIT; |
3485 | (pop @POST_INIT)->(0) while @POST_INIT; |
3437 | }; |
3486 | }; |
3438 | |
3487 | |
3439 | EV::loop; |
3488 | main_loop; |
3440 | } |
3489 | } |
3441 | |
3490 | |
3442 | ############################################################################# |
3491 | ############################################################################# |
3443 | # initialisation and cleanup |
3492 | # initialisation and cleanup |
3444 | |
3493 | |