… | |
… | |
8 | use Storable; |
8 | use Storable; |
9 | use Opcode; |
9 | use Opcode; |
10 | use Safe; |
10 | use Safe; |
11 | use Safe::Hole; |
11 | use Safe::Hole; |
12 | |
12 | |
13 | use Coro 3.3 (); |
13 | use Coro 3.4 (); |
14 | use Coro::Event; |
14 | use Coro::Event; |
15 | use Coro::Timer; |
15 | use Coro::Timer; |
16 | use Coro::Signal; |
16 | use Coro::Signal; |
17 | use Coro::Semaphore; |
17 | use Coro::Semaphore; |
18 | use Coro::AIO; |
18 | use Coro::AIO; |
… | |
… | |
503 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
503 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
504 | our @CB_MAP = (); |
504 | our @CB_MAP = (); |
505 | |
505 | |
506 | my %attachment; |
506 | my %attachment; |
507 | |
507 | |
|
|
508 | sub cf::attachable::thawer_merge { |
|
|
509 | # simply override everything except _meta |
|
|
510 | local $_[0]{_meta}; |
|
|
511 | %{$_[0]} = %{$_[1]}; |
|
|
512 | } |
|
|
513 | |
508 | sub _attach_cb($$$$) { |
514 | sub _attach_cb($$$$) { |
509 | my ($registry, $event, $prio, $cb) = @_; |
515 | my ($registry, $event, $prio, $cb) = @_; |
510 | |
516 | |
511 | use sort 'stable'; |
517 | use sort 'stable'; |
512 | |
518 | |
… | |
… | |
683 | # object support |
689 | # object support |
684 | |
690 | |
685 | sub reattach { |
691 | sub reattach { |
686 | # basically do the same as instantiate, without calling instantiate |
692 | # basically do the same as instantiate, without calling instantiate |
687 | my ($obj) = @_; |
693 | my ($obj) = @_; |
|
|
694 | |
|
|
695 | bless $obj, ref $obj; # re-bless in case extensions have been reloaded |
688 | |
696 | |
689 | my $registry = $obj->registry; |
697 | my $registry = $obj->registry; |
690 | |
698 | |
691 | @$registry = (); |
699 | @$registry = (); |
692 | |
700 | |
… | |
… | |
1105 | |
1113 | |
1106 | use Fcntl; |
1114 | use Fcntl; |
1107 | use Coro::AIO; |
1115 | use Coro::AIO; |
1108 | |
1116 | |
1109 | use overload |
1117 | use overload |
1110 | '""' => \&as_string; |
1118 | '""' => \&as_string, |
|
|
1119 | fallback => 1; |
1111 | |
1120 | |
1112 | our $MAX_RESET = 3600; |
1121 | our $MAX_RESET = 3600; |
1113 | our $DEFAULT_RESET = 3000; |
1122 | our $DEFAULT_RESET = 3000; |
1114 | |
1123 | |
1115 | sub generate_random_map { |
1124 | sub generate_random_map { |
… | |
… | |
1142 | } |
1151 | } |
1143 | |
1152 | |
1144 | # also paths starting with '/' |
1153 | # also paths starting with '/' |
1145 | $EXT_MAP{"cf::map"} = qr{^(?=/)}; |
1154 | $EXT_MAP{"cf::map"} = qr{^(?=/)}; |
1146 | |
1155 | |
|
|
1156 | sub thawer_merge { |
|
|
1157 | my ($self, $merge) = @_; |
|
|
1158 | |
|
|
1159 | # we have to keep some variables in memory intact |
|
|
1160 | local $self->{path}; |
|
|
1161 | local $self->{load_path}; |
|
|
1162 | local $self->{deny_save}; |
|
|
1163 | local $self->{deny_reset}; |
|
|
1164 | |
|
|
1165 | $self->SUPER::thawer_merge ($merge); |
|
|
1166 | } |
|
|
1167 | |
1147 | sub normalise { |
1168 | sub normalise { |
1148 | my ($path, $base) = @_; |
1169 | my ($path, $base) = @_; |
1149 | |
1170 | |
1150 | # map plan: |
1171 | # map plan: |
1151 | # |
1172 | # |
… | |
… | |
1247 | |
1268 | |
1248 | sub unlink_save { |
1269 | sub unlink_save { |
1249 | my ($self) = @_; |
1270 | my ($self) = @_; |
1250 | |
1271 | |
1251 | utf8::encode (my $save = $self->save_path); |
1272 | utf8::encode (my $save = $self->save_path); |
1252 | IO::AIO::aioreq_pri 4; IO::AIO::aio_unlink $save; |
1273 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink $save; |
1253 | IO::AIO::aioreq_pri 4; IO::AIO::aio_unlink "$save.pst"; |
1274 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink "$save.pst"; |
1254 | } |
1275 | } |
1255 | |
1276 | |
1256 | sub load_header_from($) { |
1277 | sub load_header_from($) { |
1257 | my ($self, $path) = @_; |
1278 | my ($self, $path) = @_; |
1258 | |
1279 | |
… | |
… | |
1319 | |
1340 | |
1320 | $map->load_header |
1341 | $map->load_header |
1321 | or return; |
1342 | or return; |
1322 | |
1343 | |
1323 | if ($map->should_reset) { |
1344 | if ($map->should_reset) { |
|
|
1345 | $cf::WAIT_FOR_TICK->wait; |
1324 | $map->reset; |
1346 | $map->reset; |
1325 | undef $guard; |
1347 | undef $guard; |
1326 | $map = find $path |
1348 | $map = find $path |
1327 | or return; |
1349 | or return; |
1328 | } |
1350 | } |
… | |
… | |
1498 | |
1520 | |
1499 | sub reset_at { |
1521 | sub reset_at { |
1500 | my ($self) = @_; |
1522 | my ($self) = @_; |
1501 | |
1523 | |
1502 | # TODO: safety, remove and allow resettable per-player maps |
1524 | # TODO: safety, remove and allow resettable per-player maps |
1503 | return 1e99 if $self->isa ("ext::map_per_player"); |
1525 | return 1e99 if $self->isa ("ext::map_per_player");#d# |
1504 | return 1e99 if $self->{deny_reset}; |
1526 | return 1e99 if $self->{deny_reset}; |
1505 | |
1527 | |
1506 | my $time = $self->fixed_resettime ? $self->{instantiate_time} : $self->last_access; |
1528 | my $time = $self->fixed_resettime ? $self->{instantiate_time} : $self->last_access; |
1507 | my $to = List::Util::min $MAX_RESET, $self->reset_timeout || $DEFAULT_RESET; |
1529 | my $to = List::Util::min $MAX_RESET, $self->reset_timeout || $DEFAULT_RESET; |
1508 | |
1530 | |
… | |
… | |
1513 | my ($self) = @_; |
1535 | my ($self) = @_; |
1514 | |
1536 | |
1515 | $self->reset_at <= $cf::RUNTIME |
1537 | $self->reset_at <= $cf::RUNTIME |
1516 | } |
1538 | } |
1517 | |
1539 | |
1518 | sub rename { |
|
|
1519 | my ($self, $new_path) = @_; |
|
|
1520 | |
|
|
1521 | normalise $new_path; |
|
|
1522 | |
|
|
1523 | $self->unlink_save; |
|
|
1524 | |
|
|
1525 | delete $cf::MAP{$self->path}; |
|
|
1526 | $self->{path} = $new_path; $self->path ($self->{path}); |
|
|
1527 | $cf::MAP{$self->path} = $self; |
|
|
1528 | |
|
|
1529 | $self->save; |
|
|
1530 | } |
|
|
1531 | |
|
|
1532 | sub reset { |
1540 | sub reset { |
1533 | my ($self) = @_; |
1541 | my ($self) = @_; |
1534 | |
1542 | |
1535 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
1543 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
1536 | |
1544 | |
… | |
… | |
1553 | my $nuke_counter = "aaaa"; |
1561 | my $nuke_counter = "aaaa"; |
1554 | |
1562 | |
1555 | sub nuke { |
1563 | sub nuke { |
1556 | my ($self) = @_; |
1564 | my ($self) = @_; |
1557 | |
1565 | |
|
|
1566 | delete $cf::MAP{$self->path}; |
|
|
1567 | |
|
|
1568 | $self->unlink_save; |
|
|
1569 | |
|
|
1570 | bless $self, "cf::map"; |
|
|
1571 | delete $self->{deny_reset}; |
1558 | $self->{deny_save} = 1; |
1572 | $self->{deny_save} = 1; |
1559 | $self->reset_timeout (1); |
1573 | $self->reset_timeout (1); |
1560 | $self->rename ("{nuke}/" . ($nuke_counter++)); |
1574 | $self->path ($self->{path} = "{nuke}/" . ($nuke_counter++)); |
|
|
1575 | |
|
|
1576 | $cf::MAP{$self->path} = $self; |
|
|
1577 | |
1561 | $self->reset; # polite request, might not happen |
1578 | $self->reset; # polite request, might not happen |
1562 | } |
1579 | } |
1563 | |
1580 | |
1564 | =item cf::map::unique_maps |
1581 | =item cf::map::unique_maps |
1565 | |
1582 | |
… | |
… | |
1829 | |
1846 | |
1830 | $rmp->{random_seed} ||= $exit->random_seed; |
1847 | $rmp->{random_seed} ||= $exit->random_seed; |
1831 | |
1848 | |
1832 | my $data = cf::to_json $rmp; |
1849 | my $data = cf::to_json $rmp; |
1833 | my $md5 = Digest::MD5::md5_hex $data; |
1850 | my $md5 = Digest::MD5::md5_hex $data; |
|
|
1851 | my $meta = "$cf::RANDOM_MAPS/$md5.meta"; |
1834 | |
1852 | |
1835 | if (my $fh = aio_open "$cf::RANDOM_MAPS/$md5.meta", O_WRONLY | O_CREAT, 0666) { |
1853 | if (my $fh = aio_open "$meta~", O_WRONLY | O_CREAT, 0666) { |
1836 | aio_write $fh, 0, (length $data), $data, 0; |
1854 | aio_write $fh, 0, (length $data), $data, 0; |
|
|
1855 | undef $fh; |
|
|
1856 | aio_rename "$meta~", $meta; |
1837 | |
1857 | |
1838 | $exit->slaying ("?random/$md5"); |
1858 | $exit->slaying ("?random/$md5"); |
1839 | $exit->msg (undef); |
1859 | $exit->msg (undef); |
1840 | } |
1860 | } |
1841 | } |
1861 | } |
… | |
… | |
2211 | |
2231 | |
2212 | sub main { |
2232 | sub main { |
2213 | # we must not ever block the main coroutine |
2233 | # we must not ever block the main coroutine |
2214 | local $Coro::idle = sub { |
2234 | local $Coro::idle = sub { |
2215 | Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# |
2235 | Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# |
|
|
2236 | (async { |
2216 | async { Event::one_event }; |
2237 | Event::one_event; |
|
|
2238 | })->prio (Coro::PRIO_MAX); |
2217 | }; |
2239 | }; |
2218 | |
2240 | |
2219 | cfg_load; |
2241 | cfg_load; |
2220 | db_load; |
2242 | db_load; |
2221 | load_extensions; |
2243 | load_extensions; |
… | |
… | |
2369 | } |
2391 | } |
2370 | |
2392 | |
2371 | warn "reloaded"; |
2393 | warn "reloaded"; |
2372 | }; |
2394 | }; |
2373 | |
2395 | |
|
|
2396 | our $RELOAD_WATCHER; # used only during reload |
|
|
2397 | |
2374 | register_command "reload" => sub { |
2398 | register_command "reload" => sub { |
2375 | my ($who, $arg) = @_; |
2399 | my ($who, $arg) = @_; |
2376 | |
2400 | |
2377 | if ($who->flag (FLAG_WIZ)) { |
2401 | if ($who->flag (FLAG_WIZ)) { |
2378 | $who->message ("start of reload."); |
2402 | $who->message ("reloading server."); |
|
|
2403 | |
|
|
2404 | # doing reload synchronously and two reloads happen back-to-back, |
|
|
2405 | # coro crashes during coro_state_free->destroy here. |
|
|
2406 | |
|
|
2407 | $RELOAD_WATCHER ||= Event->timer (after => 0, data => WF_AUTOCANCEL, cb => sub { |
2379 | reload; |
2408 | reload; |
2380 | $who->message ("end of reload."); |
2409 | undef $RELOAD_WATCHER; |
|
|
2410 | }); |
2381 | } |
2411 | } |
2382 | }; |
2412 | }; |
2383 | |
2413 | |
2384 | unshift @INC, $LIBDIR; |
2414 | unshift @INC, $LIBDIR; |
2385 | |
2415 | |