… | |
… | |
44 | our @EVENT; |
44 | our @EVENT; |
45 | our $LIBDIR = datadir . "/ext"; |
45 | our $LIBDIR = datadir . "/ext"; |
46 | |
46 | |
47 | our $TICK = MAX_TIME * 1e-6; |
47 | our $TICK = MAX_TIME * 1e-6; |
48 | our $TICK_WATCHER; |
48 | our $TICK_WATCHER; |
|
|
49 | our $AIO_POLL_WATCHER; |
|
|
50 | our $WRITE_RUNTIME_WATCHER; |
49 | our $NEXT_TICK; |
51 | our $NEXT_TICK; |
50 | our $NOW; |
52 | our $NOW; |
51 | |
53 | |
52 | our %CFG; |
54 | our %CFG; |
53 | |
55 | |
… | |
… | |
257 | } |
259 | } |
258 | |
260 | |
259 | sub freeze_mainloop { |
261 | sub freeze_mainloop { |
260 | return unless $TICK_WATCHER->is_active; |
262 | return unless $TICK_WATCHER->is_active; |
261 | |
263 | |
262 | my $guard = Coro::guard { $TICK_WATCHER->start }; |
264 | my $guard = Coro::guard { |
|
|
265 | $TICK_WATCHER->start; |
|
|
266 | $WRITE_RUNTIME_WATCHER->start; |
|
|
267 | }; |
|
|
268 | $WRITE_RUNTIME_WATCHER->stop; |
263 | $TICK_WATCHER->stop; |
269 | $TICK_WATCHER->stop; |
264 | $guard |
270 | $guard |
265 | } |
271 | } |
266 | |
272 | |
267 | =item cf::async { BLOCK } |
273 | =item cf::async { BLOCK } |
… | |
… | |
338 | |
344 | |
339 | $coro |
345 | $coro |
340 | } |
346 | } |
341 | |
347 | |
342 | sub write_runtime { |
348 | sub write_runtime { |
343 | return unless $TICK_WATCHER->is_active; |
|
|
344 | |
|
|
345 | my $runtime = cf::localdir . "/runtime"; |
349 | my $runtime = cf::localdir . "/runtime"; |
346 | |
350 | |
347 | my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 |
351 | my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 |
348 | or return; |
352 | or return; |
349 | |
353 | |
… | |
… | |
900 | } or warn "$ext not loaded: $@"; |
904 | } or warn "$ext not loaded: $@"; |
901 | } |
905 | } |
902 | } |
906 | } |
903 | |
907 | |
904 | ############################################################################# |
908 | ############################################################################# |
905 | # load/save/clean perl data associated with a map |
|
|
906 | |
|
|
907 | *cf::mapsupport::on_clean = sub { |
|
|
908 | my ($map) = @_; |
|
|
909 | |
|
|
910 | my $path = $map->tmpname; |
|
|
911 | defined $path or return; |
|
|
912 | |
|
|
913 | unlink "$path.pst"; |
|
|
914 | }; |
|
|
915 | |
|
|
916 | cf::map->attach (prio => -10000, package => cf::mapsupport::); |
|
|
917 | |
|
|
918 | ############################################################################# |
|
|
919 | |
909 | |
920 | =head2 CORE EXTENSIONS |
910 | =head2 CORE EXTENSIONS |
921 | |
911 | |
922 | Functions and methods that extend core crossfire objects. |
912 | Functions and methods that extend core crossfire objects. |
923 | |
913 | |
… | |
… | |
1077 | for (@$files) { |
1067 | for (@$files) { |
1078 | utf8::decode $_; |
1068 | utf8::decode $_; |
1079 | next if /\.(?:pl|pst)$/; |
1069 | next if /\.(?:pl|pst)$/; |
1080 | next unless /^$PATH_SEP/o; |
1070 | next unless /^$PATH_SEP/o; |
1081 | |
1071 | |
|
|
1072 | s/\.map$//; |
1082 | push @paths, "~" . $pl->ob->name . "/" . $_; |
1073 | push @paths, "~" . $pl->ob->name . "/" . $_; |
1083 | } |
1074 | } |
1084 | |
1075 | |
1085 | \@paths |
1076 | \@paths |
1086 | } |
1077 | } |
… | |
… | |
1220 | # the displayed name, this is a one way mapping |
1211 | # the displayed name, this is a one way mapping |
1221 | sub visible_name { |
1212 | sub visible_name { |
1222 | &as_string |
1213 | &as_string |
1223 | } |
1214 | } |
1224 | |
1215 | |
1225 | # escape the /'s in the path |
|
|
1226 | sub _escaped_path { |
|
|
1227 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
|
|
1228 | |
|
|
1229 | $path |
|
|
1230 | } |
|
|
1231 | |
|
|
1232 | # the original (read-only) location |
1216 | # the original (read-only) location |
1233 | sub load_path { |
1217 | sub load_path { |
1234 | my ($self) = @_; |
1218 | my ($self) = @_; |
1235 | |
1219 | |
1236 | sprintf "%s/%s/%s", cf::datadir, cf::mapdir, $self->{path} |
1220 | sprintf "%s/%s/%s", cf::datadir, cf::mapdir, $self->{path} |
… | |
… | |
1238 | |
1222 | |
1239 | # the temporary/swap location |
1223 | # the temporary/swap location |
1240 | sub save_path { |
1224 | sub save_path { |
1241 | my ($self) = @_; |
1225 | my ($self) = @_; |
1242 | |
1226 | |
|
|
1227 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
1243 | sprintf "%s/%s/%s.map", cf::localdir, cf::tmpdir, $self->_escaped_path |
1228 | sprintf "%s/%s/%s", cf::localdir, cf::tmpdir, $path |
1244 | } |
1229 | } |
1245 | |
1230 | |
1246 | # the unique path, undef == no special unique path |
1231 | # the unique path, undef == no special unique path |
1247 | sub uniq_path { |
1232 | sub uniq_path { |
1248 | my ($self) = @_; |
1233 | my ($self) = @_; |
1249 | |
1234 | |
|
|
1235 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
1250 | sprintf "%s/%s/%s", cf::localdir, cf::uniquedir, $self->_escaped_path |
1236 | sprintf "%s/%s/%s", cf::localdir, cf::uniquedir, $path |
1251 | } |
1237 | } |
1252 | |
1238 | |
1253 | # and all this just because we cannot iterate over |
1239 | # and all this just because we cannot iterate over |
1254 | # all maps in C++... |
1240 | # all maps in C++... |
1255 | sub change_all_map_light { |
1241 | sub change_all_map_light { |
… | |
… | |
1544 | } |
1530 | } |
1545 | |
1531 | |
1546 | sub reset { |
1532 | sub reset { |
1547 | my ($self) = @_; |
1533 | my ($self) = @_; |
1548 | |
1534 | |
1549 | my $lock = cf::lock_acquire "map_data:" . $self->path; |
1535 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
1550 | |
1536 | |
1551 | return if $self->players; |
1537 | return if $self->players; |
1552 | return if $self->isa ("ext::map_per_player");#d# |
1538 | return if $self->isa ("ext::map_per_player");#d# |
1553 | |
1539 | |
1554 | warn "resetting map ", $self->path;#d# |
1540 | warn "resetting map ", $self->path;#d# |
1555 | |
1541 | |
1556 | delete $cf::MAP{$self->path}; |
1542 | delete $cf::MAP{$self->path}; |
|
|
1543 | |
|
|
1544 | $self->in_memory (cf::MAP_SWAPPED); |
|
|
1545 | $self->clear; |
1557 | |
1546 | |
1558 | $_->clear_links_to ($self) for values %cf::MAP; |
1547 | $_->clear_links_to ($self) for values %cf::MAP; |
1559 | |
1548 | |
1560 | $self->unlink_save; |
1549 | $self->unlink_save; |
1561 | $self->destroy; |
1550 | $self->destroy; |
… | |
… | |
1588 | for (@$files) { |
1577 | for (@$files) { |
1589 | utf8::decode $_; |
1578 | utf8::decode $_; |
1590 | next if /\.pst$/; |
1579 | next if /\.pst$/; |
1591 | next unless /^$PATH_SEP/o; |
1580 | next unless /^$PATH_SEP/o; |
1592 | |
1581 | |
|
|
1582 | s/\.map$//; |
1593 | push @paths, $_; |
1583 | push @paths, $_; |
1594 | } |
1584 | } |
1595 | |
1585 | |
1596 | \@paths |
1586 | \@paths |
1597 | } |
1587 | } |
… | |
… | |
2254 | warn "enter emergency perl save\n"; |
2244 | warn "enter emergency perl save\n"; |
2255 | |
2245 | |
2256 | cf::sync_job { |
2246 | cf::sync_job { |
2257 | # use a peculiar iteration method to avoid tripping on perl |
2247 | # use a peculiar iteration method to avoid tripping on perl |
2258 | # refcount bugs in for. also avoids problems with players |
2248 | # refcount bugs in for. also avoids problems with players |
2259 | # and maps saved/Destroyed asynchronously. |
2249 | # and maps saved/destroyed asynchronously. |
2260 | warn "begin emergency player save\n"; |
2250 | warn "begin emergency player save\n"; |
2261 | for my $login (keys %cf::PLAYER) { |
2251 | for my $login (keys %cf::PLAYER) { |
2262 | my $pl = $cf::PLAYER{$login} or next; |
2252 | my $pl = $cf::PLAYER{$login} or next; |
2263 | $pl->valid or next; |
2253 | $pl->valid or next; |
2264 | $pl->save; |
2254 | $pl->save; |
… | |
… | |
2416 | }, |
2406 | }, |
2417 | ); |
2407 | ); |
2418 | |
2408 | |
2419 | IO::AIO::max_poll_time $TICK * 0.2; |
2409 | IO::AIO::max_poll_time $TICK * 0.2; |
2420 | |
2410 | |
2421 | Event->io ( |
2411 | $AIO_POLL_WATCHER = Event->io ( |
2422 | fd => IO::AIO::poll_fileno, |
2412 | fd => IO::AIO::poll_fileno, |
2423 | poll => 'r', |
2413 | poll => 'r', |
2424 | prio => 5, |
2414 | prio => 5, |
2425 | data => WF_AUTOCANCEL, |
2415 | data => WF_AUTOCANCEL, |
2426 | cb => \&IO::AIO::poll_cb, |
2416 | cb => \&IO::AIO::poll_cb, |
2427 | ); |
2417 | ); |
2428 | |
2418 | |
2429 | Event->timer ( |
2419 | $WRITE_RUNTIME_WATCHER = Event->timer ( |
2430 | data => WF_AUTOCANCEL, |
2420 | data => WF_AUTOCANCEL, |
2431 | after => 0, |
2421 | after => 0, |
2432 | interval => 10, |
2422 | interval => 10, |
2433 | cb => sub { |
2423 | cb => sub { |
2434 | (Coro::unblock_sub { |
2424 | (Coro::unblock_sub { |