… | |
… | |
106 | our $RANDOMDIR = "$LOCALDIR/random"; |
106 | our $RANDOMDIR = "$LOCALDIR/random"; |
107 | our $BDBDIR = "$LOCALDIR/db"; |
107 | our $BDBDIR = "$LOCALDIR/db"; |
108 | our $PIDFILE = "$LOCALDIR/pid"; |
108 | our $PIDFILE = "$LOCALDIR/pid"; |
109 | our $RUNTIMEFILE = "$LOCALDIR/runtime"; |
109 | our $RUNTIMEFILE = "$LOCALDIR/runtime"; |
110 | |
110 | |
111 | our %RESOURCE; |
111 | our %RESOURCE; # unused |
112 | |
112 | |
113 | our $OUTPUT_RATE_MIN = 4000; |
113 | our $OUTPUT_RATE_MIN = 3000; |
114 | our $OUTPUT_RATE_MAX = 100000; |
114 | our $OUTPUT_RATE_MAX = 1000000; |
|
|
115 | |
|
|
116 | our $MAX_LINKS = 32; # how many chained exits to follow |
|
|
117 | our $VERBOSE_IO = 1; |
115 | |
118 | |
116 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
119 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
117 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
120 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
118 | our $NEXT_TICK; |
121 | our $NEXT_TICK; |
119 | our $USE_FSYNC = 1; # use fsync to write maps - default on |
122 | our $USE_FSYNC = 1; # use fsync to write maps - default on |
… | |
… | |
165 | |
168 | |
166 | our $EMERGENCY_POSITION; |
169 | our $EMERGENCY_POSITION; |
167 | |
170 | |
168 | sub cf::map::normalise; |
171 | sub cf::map::normalise; |
169 | |
172 | |
|
|
173 | sub in_main() { |
|
|
174 | $Coro::current == $Coro::main |
|
|
175 | } |
|
|
176 | |
170 | ############################################################################# |
177 | ############################################################################# |
171 | |
178 | |
172 | %REFLECT = (); |
179 | %REFLECT = (); |
173 | for (@REFLECT) { |
180 | for (@REFLECT) { |
174 | my $reflect = JSON::XS::decode_json $_; |
181 | my $reflect = JSON::XS::decode_json $_; |
… | |
… | |
245 | |
252 | |
246 | =back |
253 | =back |
247 | |
254 | |
248 | =cut |
255 | =cut |
249 | |
256 | |
|
|
257 | sub error(@) { LOG llevError, join "", @_ } |
|
|
258 | sub warn (@) { LOG llevWarn , join "", @_ } |
|
|
259 | sub info (@) { LOG llevInfo , join "", @_ } |
|
|
260 | sub debug(@) { LOG llevDebug, join "", @_ } |
|
|
261 | sub trace(@) { LOG llevTrace, join "", @_ } |
|
|
262 | |
250 | $Coro::State::WARNHOOK = sub { |
263 | $Coro::State::WARNHOOK = sub { |
251 | my $msg = join "", @_; |
264 | my $msg = join "", @_; |
252 | |
265 | |
253 | $msg .= "\n" |
266 | $msg .= "\n" |
254 | unless $msg =~ /\n$/; |
267 | unless $msg =~ /\n$/; |
255 | |
268 | |
256 | $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; |
269 | $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; |
257 | |
270 | |
258 | LOG llevError, $msg; |
271 | LOG llevWarn, $msg; |
259 | }; |
272 | }; |
260 | |
273 | |
261 | $Coro::State::DIEHOOK = sub { |
274 | $Coro::State::DIEHOOK = sub { |
262 | return unless $^S eq 0; # "eq", not "==" |
275 | return unless $^S eq 0; # "eq", not "==" |
263 | |
276 | |
264 | warn Carp::longmess $_[0]; |
277 | error Carp::longmess $_[0]; |
265 | |
278 | |
266 | if ($Coro::current == $Coro::main) {#d# |
279 | if (in_main) {#d# |
267 | warn "DIEHOOK called in main context, Coro bug?\n";#d# |
280 | error "DIEHOOK called in main context, Coro bug?\n";#d# |
268 | return;#d# |
281 | return;#d# |
269 | }#d# |
282 | }#d# |
270 | |
283 | |
271 | # kill coroutine otherwise |
284 | # kill coroutine otherwise |
272 | Coro::terminate |
285 | Coro::terminate |
… | |
… | |
521 | my @res; |
534 | my @res; |
522 | |
535 | |
523 | (async { |
536 | (async { |
524 | $Coro::current->desc ("sync job coro"); |
537 | $Coro::current->desc ("sync job coro"); |
525 | @res = eval { $job->() }; |
538 | @res = eval { $job->() }; |
526 | warn $@ if $@; |
539 | error $@ if $@; |
527 | undef $busy; |
540 | undef $busy; |
528 | })->prio (Coro::PRIO_MAX); |
541 | })->prio (Coro::PRIO_MAX); |
529 | |
542 | |
530 | while ($busy) { |
543 | while ($busy) { |
531 | if (Coro::nready) { |
544 | if (Coro::nready) { |
… | |
… | |
742 | |
755 | |
743 | my $t1 = Time::HiRes::time; |
756 | my $t1 = Time::HiRes::time; |
744 | my $data = $process->(\@data); |
757 | my $data = $process->(\@data); |
745 | my $t2 = Time::HiRes::time; |
758 | my $t2 = Time::HiRes::time; |
746 | |
759 | |
747 | warn "cache: '$id' processed in ", $t2 - $t1, "s\n"; |
760 | info "cache: '$id' processed in ", $t2 - $t1, "s\n"; |
748 | |
761 | |
749 | db_put cache => "$id/data", $data; |
762 | db_put cache => "$id/data", $data; |
750 | db_put cache => "$id/md5" , $md5; |
763 | db_put cache => "$id/md5" , $md5; |
751 | db_put cache => "$id/meta", $meta; |
764 | db_put cache => "$id/meta", $meta; |
752 | |
765 | |
… | |
… | |
762 | |
775 | |
763 | =cut |
776 | =cut |
764 | |
777 | |
765 | sub datalog($@) { |
778 | sub datalog($@) { |
766 | my ($type, %kv) = @_; |
779 | my ($type, %kv) = @_; |
767 | warn "DATALOG ", JSON::XS->new->ascii->encode ({ %kv, type => $type }); |
780 | info "DATALOG ", JSON::XS->new->ascii->encode ({ %kv, type => $type }); |
768 | } |
781 | } |
769 | |
782 | |
770 | =back |
783 | =back |
771 | |
784 | |
772 | =cut |
785 | =cut |
… | |
… | |
967 | |
980 | |
968 | } elsif (exists $cb_id{$type}) { |
981 | } elsif (exists $cb_id{$type}) { |
969 | _attach_cb $registry, $cb_id{$type}, $prio, shift @arg; |
982 | _attach_cb $registry, $cb_id{$type}, $prio, shift @arg; |
970 | |
983 | |
971 | } elsif (ref $type) { |
984 | } elsif (ref $type) { |
972 | warn "attaching objects not supported, ignoring.\n"; |
985 | error "attaching objects not supported, ignoring.\n"; |
973 | |
986 | |
974 | } else { |
987 | } else { |
975 | shift @arg; |
988 | shift @arg; |
976 | warn "attach argument '$type' not supported, ignoring.\n"; |
989 | error "attach argument '$type' not supported, ignoring.\n"; |
977 | } |
990 | } |
978 | } |
991 | } |
979 | } |
992 | } |
980 | |
993 | |
981 | sub _object_attach { |
994 | sub _object_attach { |
… | |
… | |
991 | _attach $registry, $klass, @attach; |
1004 | _attach $registry, $klass, @attach; |
992 | } |
1005 | } |
993 | |
1006 | |
994 | $obj->{$name} = \%arg; |
1007 | $obj->{$name} = \%arg; |
995 | } else { |
1008 | } else { |
996 | warn "object uses attachment '$name' which is not available, postponing.\n"; |
1009 | info "object uses attachment '$name' which is not available, postponing.\n"; |
997 | } |
1010 | } |
998 | |
1011 | |
999 | $obj->{_attachment}{$name} = undef; |
1012 | $obj->{_attachment}{$name} = undef; |
1000 | } |
1013 | } |
1001 | |
1014 | |
… | |
… | |
1060 | |
1073 | |
1061 | for (@$callbacks) { |
1074 | for (@$callbacks) { |
1062 | eval { &{$_->[1]} }; |
1075 | eval { &{$_->[1]} }; |
1063 | |
1076 | |
1064 | if ($@) { |
1077 | if ($@) { |
1065 | warn "$@"; |
|
|
1066 | warn "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n"; |
1078 | error "$@", "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n"; |
1067 | override; |
1079 | override; |
1068 | } |
1080 | } |
1069 | |
1081 | |
1070 | return 1 if $override; |
1082 | return 1 if $override; |
1071 | } |
1083 | } |
… | |
… | |
1150 | for (@$attach) { |
1162 | for (@$attach) { |
1151 | my ($klass, @attach) = @$_; |
1163 | my ($klass, @attach) = @$_; |
1152 | _attach $registry, $klass, @attach; |
1164 | _attach $registry, $klass, @attach; |
1153 | } |
1165 | } |
1154 | } else { |
1166 | } else { |
1155 | warn "object uses attachment '$name' that is not available, postponing.\n"; |
1167 | info "object uses attachment '$name' that is not available, postponing.\n"; |
1156 | } |
1168 | } |
1157 | } |
1169 | } |
1158 | } |
1170 | } |
1159 | |
1171 | |
1160 | cf::attachable->attach ( |
1172 | cf::attachable->attach ( |
… | |
… | |
1187 | my ($filename, $rdata, $objs) = @_; |
1199 | my ($filename, $rdata, $objs) = @_; |
1188 | |
1200 | |
1189 | sync_job { |
1201 | sync_job { |
1190 | if (length $$rdata) { |
1202 | if (length $$rdata) { |
1191 | utf8::decode (my $decname = $filename); |
1203 | utf8::decode (my $decname = $filename); |
1192 | warn sprintf "saving %s (%d,%d)\n", |
1204 | trace sprintf "saving %s (%d,%d)\n", |
1193 | $decname, length $$rdata, scalar @$objs; |
1205 | $decname, length $$rdata, scalar @$objs |
|
|
1206 | if $VERBOSE_IO; |
1194 | |
1207 | |
1195 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
1208 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
1196 | aio_chmod $fh, SAVE_MODE; |
1209 | aio_chmod $fh, SAVE_MODE; |
1197 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
1210 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
1198 | if ($cf::USE_FSYNC) { |
1211 | if ($cf::USE_FSYNC) { |
… | |
… | |
1220 | aio_rename "$filename~", $filename; |
1233 | aio_rename "$filename~", $filename; |
1221 | |
1234 | |
1222 | $filename =~ s%/[^/]+$%%; |
1235 | $filename =~ s%/[^/]+$%%; |
1223 | aio_pathsync $filename if $cf::USE_FSYNC; |
1236 | aio_pathsync $filename if $cf::USE_FSYNC; |
1224 | } else { |
1237 | } else { |
1225 | warn "unable to save objects: $filename~: $!\n"; |
1238 | error "unable to save objects: $filename~: $!\n"; |
1226 | } |
1239 | } |
1227 | } else { |
1240 | } else { |
1228 | aio_unlink $filename; |
1241 | aio_unlink $filename; |
1229 | aio_unlink "$filename.pst"; |
1242 | aio_unlink "$filename.pst"; |
1230 | } |
1243 | } |
… | |
… | |
1254 | my $st = eval { Coro::Storable::thaw $av }; |
1267 | my $st = eval { Coro::Storable::thaw $av }; |
1255 | $av = $st->{objs}; |
1268 | $av = $st->{objs}; |
1256 | } |
1269 | } |
1257 | |
1270 | |
1258 | utf8::decode (my $decname = $filename); |
1271 | utf8::decode (my $decname = $filename); |
1259 | warn sprintf "loading %s (%d,%d)\n", |
1272 | trace sprintf "loading %s (%d,%d)\n", |
1260 | $decname, length $data, scalar @{$av || []}; |
1273 | $decname, length $data, scalar @{$av || []} |
|
|
1274 | if $VERBOSE_IO; |
1261 | |
1275 | |
1262 | ($data, $av) |
1276 | ($data, $av) |
1263 | } |
1277 | } |
1264 | |
1278 | |
1265 | =head2 COMMAND CALLBACKS |
1279 | =head2 COMMAND CALLBACKS |
… | |
… | |
1357 | |
1371 | |
1358 | $pl->ext_reply ($reply, @reply) |
1372 | $pl->ext_reply ($reply, @reply) |
1359 | if $reply; |
1373 | if $reply; |
1360 | |
1374 | |
1361 | } else { |
1375 | } else { |
1362 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
1376 | error "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
1363 | } |
1377 | } |
1364 | |
1378 | |
1365 | cf::override; |
1379 | cf::override; |
1366 | }, |
1380 | }, |
1367 | ); |
1381 | ); |
… | |
… | |
1378 | |
1392 | |
1379 | $grp |
1393 | $grp |
1380 | } |
1394 | } |
1381 | |
1395 | |
1382 | sub load_extensions { |
1396 | sub load_extensions { |
|
|
1397 | info "loading extensions..."; |
|
|
1398 | |
1383 | cf::sync_job { |
1399 | cf::sync_job { |
1384 | my %todo; |
1400 | my %todo; |
1385 | |
1401 | |
1386 | for my $path (<$LIBDIR/*.ext>) { |
1402 | for my $path (<$LIBDIR/*.ext>) { |
1387 | next unless -r $path; |
1403 | next unless -r $path; |
… | |
… | |
1427 | for (split /,\s*/, $v->{meta}{depends}) { |
1443 | for (split /,\s*/, $v->{meta}{depends}) { |
1428 | next ext |
1444 | next ext |
1429 | unless exists $done{$_}; |
1445 | unless exists $done{$_}; |
1430 | } |
1446 | } |
1431 | |
1447 | |
1432 | warn "... pass $pass, loading '$k' into '$v->{pkg}'\n"; |
1448 | trace "... pass $pass, loading '$k' into '$v->{pkg}'\n"; |
1433 | |
1449 | |
1434 | my $active = eval $v->{source}; |
1450 | my $active = eval $v->{source}; |
1435 | |
1451 | |
1436 | if (length $@) { |
1452 | if (length $@) { |
1437 | warn "$v->{path}: $@\n"; |
1453 | error "$v->{path}: $@\n"; |
1438 | |
1454 | |
1439 | cf::cleanup "mandatory extension '$k' failed to load, exiting." |
1455 | cf::cleanup "mandatory extension '$k' failed to load, exiting." |
1440 | if exists $v->{meta}{mandatory}; |
1456 | if exists $v->{meta}{mandatory}; |
|
|
1457 | |
|
|
1458 | warn "$v->{base}: optional extension cannot be loaded, skipping.\n"; |
|
|
1459 | delete $todo{$k}; |
1441 | } else { |
1460 | } else { |
1442 | $done{$k} = delete $todo{$k}; |
1461 | $done{$k} = delete $todo{$k}; |
1443 | push @EXTS, $v->{pkg}; |
1462 | push @EXTS, $v->{pkg}; |
1444 | $progress = 1; |
1463 | $progress = 1; |
1445 | |
1464 | |
1446 | warn "$v->{base}: extension inactive.\n" |
1465 | info "$v->{base}: extension inactive.\n" |
1447 | unless $active; |
1466 | unless $active; |
1448 | } |
1467 | } |
1449 | } |
1468 | } |
1450 | |
1469 | |
1451 | unless ($progress) { |
1470 | unless ($progress) { |
… | |
… | |
1623 | $pl->password ("*"); # this should lock out the player until we have nuked the dir |
1642 | $pl->password ("*"); # this should lock out the player until we have nuked the dir |
1624 | |
1643 | |
1625 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; |
1644 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; |
1626 | $pl->deactivate; |
1645 | $pl->deactivate; |
1627 | my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy; |
1646 | my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy; |
1628 | $pl->ob->check_score; |
|
|
1629 | $pl->invoke (cf::EVENT_PLAYER_QUIT); |
1647 | $pl->invoke (cf::EVENT_PLAYER_QUIT); |
1630 | $pl->ns->destroy if $pl->ns; |
1648 | $pl->ns->destroy if $pl->ns; |
1631 | |
1649 | |
1632 | my $path = playerdir $pl; |
1650 | my $path = playerdir $pl; |
1633 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
1651 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
… | |
… | |
1688 | \@logins |
1706 | \@logins |
1689 | } |
1707 | } |
1690 | |
1708 | |
1691 | =item $player->maps |
1709 | =item $player->maps |
1692 | |
1710 | |
|
|
1711 | =item cf::player::maps $login |
|
|
1712 | |
1693 | Returns an arrayref of map paths that are private for this |
1713 | Returns an arrayref of map paths that are private for this |
1694 | player. May block. |
1714 | player. May block. |
1695 | |
1715 | |
1696 | =cut |
1716 | =cut |
1697 | |
1717 | |
… | |
… | |
1758 | |
1778 | |
1759 | =cut |
1779 | =cut |
1760 | |
1780 | |
1761 | sub find_by_path($) { |
1781 | sub find_by_path($) { |
1762 | my ($path) = @_; |
1782 | my ($path) = @_; |
|
|
1783 | |
|
|
1784 | $path =~ s/^~[^\/]*//; # skip ~login |
1763 | |
1785 | |
1764 | my ($match, $specificity); |
1786 | my ($match, $specificity); |
1765 | |
1787 | |
1766 | for my $region (list) { |
1788 | for my $region (list) { |
1767 | if ($region->{match} && $path =~ $region->{match}) { |
1789 | if ($region->{match} && $path =~ $region->{match}) { |
… | |
… | |
1831 | |
1853 | |
1832 | $EXT_MAP{$pkg} = [$prio, qr<$regex>]; |
1854 | $EXT_MAP{$pkg} = [$prio, qr<$regex>]; |
1833 | } |
1855 | } |
1834 | |
1856 | |
1835 | # also paths starting with '/' |
1857 | # also paths starting with '/' |
1836 | $EXT_MAP{"cf::map"} = [0, qr{^(?=/)}]; |
1858 | $EXT_MAP{"cf::map::wrap"} = [0, qr{^(?=/)}]; |
1837 | |
1859 | |
1838 | sub thawer_merge { |
1860 | sub thawer_merge { |
1839 | my ($self, $merge) = @_; |
1861 | my ($self, $merge) = @_; |
1840 | |
1862 | |
1841 | # we have to keep some variables in memory intact |
1863 | # we have to keep some variables in memory intact |
… | |
… | |
2151 | or next; |
2173 | or next; |
2152 | $neigh = find $neigh, $map |
2174 | $neigh = find $neigh, $map |
2153 | or next; |
2175 | or next; |
2154 | $neigh->load; |
2176 | $neigh->load; |
2155 | |
2177 | |
|
|
2178 | # now find the diagonal neighbours |
2156 | push @neigh, |
2179 | push @neigh, |
2157 | [$neigh->tile_path (($_ + 3) % 4), $neigh], |
2180 | [$neigh->tile_path (($_ + 3) % 4), $neigh], |
2158 | [$neigh->tile_path (($_ + 1) % 4), $neigh]; |
2181 | [$neigh->tile_path (($_ + 1) % 4), $neigh]; |
2159 | } |
2182 | } |
2160 | |
2183 | |
… | |
… | |
2212 | $MAP_PREFETCHER->prio (6); |
2235 | $MAP_PREFETCHER->prio (6); |
2213 | |
2236 | |
2214 | () |
2237 | () |
2215 | } |
2238 | } |
2216 | |
2239 | |
|
|
2240 | # common code, used by both ->save and ->swapout |
2217 | sub save { |
2241 | sub _save { |
2218 | my ($self) = @_; |
2242 | my ($self) = @_; |
2219 | |
|
|
2220 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
|
|
2221 | |
2243 | |
2222 | $self->{last_save} = $cf::RUNTIME; |
2244 | $self->{last_save} = $cf::RUNTIME; |
2223 | |
2245 | |
2224 | return unless $self->dirty; |
2246 | return unless $self->dirty; |
2225 | |
2247 | |
… | |
… | |
2245 | } else { |
2267 | } else { |
2246 | $self->_save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES); |
2268 | $self->_save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES); |
2247 | } |
2269 | } |
2248 | } |
2270 | } |
2249 | |
2271 | |
|
|
2272 | sub save { |
|
|
2273 | my ($self) = @_; |
|
|
2274 | |
|
|
2275 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
|
|
2276 | |
|
|
2277 | $self->_save; |
|
|
2278 | } |
|
|
2279 | |
2250 | sub swap_out { |
2280 | sub swap_out { |
2251 | my ($self) = @_; |
2281 | my ($self) = @_; |
2252 | |
2282 | |
2253 | # save first because save cedes |
|
|
2254 | $self->save; |
|
|
2255 | |
|
|
2256 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
2283 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
2257 | |
2284 | |
2258 | return if $self->players; |
|
|
2259 | return if $self->in_memory != cf::MAP_ACTIVE; |
2285 | return if $self->in_memory != cf::MAP_ACTIVE; |
2260 | return if $self->{deny_save}; |
2286 | return if $self->{deny_save}; |
|
|
2287 | return if $self->players; |
2261 | |
2288 | |
|
|
2289 | # first deactivate the map and "unlink" it from the core |
|
|
2290 | $self->deactivate; |
|
|
2291 | $_->clear_links_to ($self) for values %cf::MAP; |
2262 | $self->in_memory (cf::MAP_SWAPPED); |
2292 | $self->in_memory (cf::MAP_SWAPPED); |
|
|
2293 | |
|
|
2294 | # then atomically save |
|
|
2295 | $self->_save; |
|
|
2296 | |
|
|
2297 | # then free the map |
|
|
2298 | $self->clear; |
|
|
2299 | } |
|
|
2300 | |
|
|
2301 | sub reset_at { |
|
|
2302 | my ($self) = @_; |
|
|
2303 | |
|
|
2304 | # TODO: safety, remove and allow resettable per-player maps |
|
|
2305 | return 1e99 if $self->{deny_reset}; |
|
|
2306 | |
|
|
2307 | my $time = $self->fixed_resettime ? $self->{instantiate_time} : $self->last_access; |
|
|
2308 | my $to = List::Util::min $MAX_RESET, $self->reset_timeout || $DEFAULT_RESET; |
|
|
2309 | |
|
|
2310 | $time + $to |
|
|
2311 | } |
|
|
2312 | |
|
|
2313 | sub should_reset { |
|
|
2314 | my ($self) = @_; |
|
|
2315 | |
|
|
2316 | $self->reset_at <= $cf::RUNTIME |
|
|
2317 | } |
|
|
2318 | |
|
|
2319 | sub reset { |
|
|
2320 | my ($self) = @_; |
|
|
2321 | |
|
|
2322 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
|
|
2323 | |
|
|
2324 | return if $self->players; |
|
|
2325 | |
|
|
2326 | cf::trace "resetting map ", $self->path, "\n"; |
|
|
2327 | |
|
|
2328 | $self->in_memory (cf::MAP_SWAPPED); |
|
|
2329 | |
|
|
2330 | # need to save uniques path |
|
|
2331 | unless ($self->{deny_save}) { |
|
|
2332 | my $uniq = $self->uniq_path; utf8::encode $uniq; |
|
|
2333 | |
|
|
2334 | $self->_save_objects ($uniq, cf::IO_UNIQUES) |
|
|
2335 | if $uniq; |
|
|
2336 | } |
|
|
2337 | |
|
|
2338 | delete $cf::MAP{$self->path}; |
2263 | |
2339 | |
2264 | $self->deactivate; |
2340 | $self->deactivate; |
2265 | $_->clear_links_to ($self) for values %cf::MAP; |
2341 | $_->clear_links_to ($self) for values %cf::MAP; |
2266 | $self->clear; |
2342 | $self->clear; |
2267 | } |
|
|
2268 | |
|
|
2269 | sub reset_at { |
|
|
2270 | my ($self) = @_; |
|
|
2271 | |
|
|
2272 | # TODO: safety, remove and allow resettable per-player maps |
|
|
2273 | return 1e99 if $self->{deny_reset}; |
|
|
2274 | |
|
|
2275 | my $time = $self->fixed_resettime ? $self->{instantiate_time} : $self->last_access; |
|
|
2276 | my $to = List::Util::min $MAX_RESET, $self->reset_timeout || $DEFAULT_RESET; |
|
|
2277 | |
|
|
2278 | $time + $to |
|
|
2279 | } |
|
|
2280 | |
|
|
2281 | sub should_reset { |
|
|
2282 | my ($self) = @_; |
|
|
2283 | |
|
|
2284 | $self->reset_at <= $cf::RUNTIME |
|
|
2285 | } |
|
|
2286 | |
|
|
2287 | sub reset { |
|
|
2288 | my ($self) = @_; |
|
|
2289 | |
|
|
2290 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
|
|
2291 | |
|
|
2292 | return if $self->players; |
|
|
2293 | |
|
|
2294 | warn "resetting map ", $self->path, "\n"; |
|
|
2295 | |
|
|
2296 | $self->in_memory (cf::MAP_SWAPPED); |
|
|
2297 | |
|
|
2298 | # need to save uniques path |
|
|
2299 | unless ($self->{deny_save}) { |
|
|
2300 | my $uniq = $self->uniq_path; utf8::encode $uniq; |
|
|
2301 | |
|
|
2302 | $self->_save_objects ($uniq, cf::IO_UNIQUES) |
|
|
2303 | if $uniq; |
|
|
2304 | } |
|
|
2305 | |
|
|
2306 | delete $cf::MAP{$self->path}; |
|
|
2307 | |
|
|
2308 | $self->deactivate; |
|
|
2309 | $_->clear_links_to ($self) for values %cf::MAP; |
|
|
2310 | $self->clear; |
|
|
2311 | |
2343 | |
2312 | $self->unlink_save; |
2344 | $self->unlink_save; |
2313 | $self->destroy; |
2345 | $self->destroy; |
2314 | } |
2346 | } |
2315 | |
2347 | |
… | |
… | |
2323 | |
2355 | |
2324 | delete $cf::MAP{$self->path}; |
2356 | delete $cf::MAP{$self->path}; |
2325 | |
2357 | |
2326 | $self->unlink_save; |
2358 | $self->unlink_save; |
2327 | |
2359 | |
2328 | bless $self, "cf::map"; |
2360 | bless $self, "cf::map::wrap"; |
2329 | delete $self->{deny_reset}; |
2361 | delete $self->{deny_reset}; |
2330 | $self->{deny_save} = 1; |
2362 | $self->{deny_save} = 1; |
2331 | $self->reset_timeout (1); |
2363 | $self->reset_timeout (1); |
2332 | $self->path ($self->{path} = "{nuke}/" . ($nuke_counter++)); |
2364 | $self->path ($self->{path} = "{nuke}/" . ($nuke_counter++)); |
2333 | |
2365 | |
… | |
… | |
2563 | |
2595 | |
2564 | Freezes the player and moves him/her to a special map (C<{link}>). |
2596 | Freezes the player and moves him/her to a special map (C<{link}>). |
2565 | |
2597 | |
2566 | The player should be reasonably safe there for short amounts of time (e.g. |
2598 | The player should be reasonably safe there for short amounts of time (e.g. |
2567 | for loading a map). You I<MUST> call C<leave_link> as soon as possible, |
2599 | for loading a map). You I<MUST> call C<leave_link> as soon as possible, |
2568 | though, as the palyer cannot control the character while it is on the link |
2600 | though, as the player cannot control the character while it is on the link |
2569 | map. |
2601 | map. |
2570 | |
2602 | |
2571 | Will never block. |
2603 | Will never block. |
2572 | |
2604 | |
2573 | =item $player_object->leave_link ($map, $x, $y) |
2605 | =item $player_object->leave_link ($map, $x, $y) |
… | |
… | |
2594 | sub cf::object::player::enter_link { |
2626 | sub cf::object::player::enter_link { |
2595 | my ($self) = @_; |
2627 | my ($self) = @_; |
2596 | |
2628 | |
2597 | $self->deactivate_recursive; |
2629 | $self->deactivate_recursive; |
2598 | |
2630 | |
|
|
2631 | ++$self->{_link_recursion}; |
|
|
2632 | |
2599 | return if UNIVERSAL::isa $self->map, "ext::map_link"; |
2633 | return if UNIVERSAL::isa $self->map, "ext::map_link"; |
2600 | |
2634 | |
2601 | $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y] |
2635 | $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y] |
2602 | if $self->map && $self->map->{path} ne "{link}"; |
2636 | if $self->map && $self->map->{path} ne "{link}"; |
2603 | |
2637 | |
2604 | $self->enter_map ($LINK_MAP || link_map, 10, 10); |
2638 | $self->enter_map ($LINK_MAP || link_map, 3, 3); |
2605 | } |
2639 | } |
2606 | |
2640 | |
2607 | sub cf::object::player::leave_link { |
2641 | sub cf::object::player::leave_link { |
2608 | my ($self, $map, $x, $y) = @_; |
2642 | my ($self, $map, $x, $y) = @_; |
2609 | |
2643 | |
… | |
… | |
2634 | $map->load_neighbours; |
2668 | $map->load_neighbours; |
2635 | |
2669 | |
2636 | return unless $self->contr->active; |
2670 | return unless $self->contr->active; |
2637 | |
2671 | |
2638 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
2672 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
2639 | $self->enter_map ($map, $x, $y); |
2673 | if ($self->enter_map ($map, $x, $y)) { |
2640 | |
2674 | # entering was successful |
|
|
2675 | delete $self->{_link_recursion}; |
2641 | # only activate afterwards, to support waiting in hooks |
2676 | # only activate afterwards, to support waiting in hooks |
2642 | $self->activate_recursive; |
2677 | $self->activate_recursive; |
2643 | } |
2678 | } |
2644 | |
2679 | |
|
|
2680 | } |
|
|
2681 | |
2645 | =item $player_object->goto ($path, $x, $y[, $check->($map)[, $done->()]]) |
2682 | =item $player_object->goto ($path, $x, $y[, $check->($map, $x, $y, $player)[, $done->($player)]]) |
2646 | |
2683 | |
2647 | Moves the player to the given map-path and coordinates by first freezing |
2684 | Moves the player to the given map-path and coordinates by first freezing |
2648 | her, loading and preparing them map, calling the provided $check callback |
2685 | her, loading and preparing them map, calling the provided $check callback |
2649 | that has to return the map if sucecssful, and then unfreezes the player on |
2686 | that has to return the map if sucecssful, and then unfreezes the player on |
2650 | the new (success) or old (failed) map position. In either case, $done will |
2687 | the new (success) or old (failed) map position. In either case, $done will |
… | |
… | |
2657 | |
2694 | |
2658 | our $GOTOGEN; |
2695 | our $GOTOGEN; |
2659 | |
2696 | |
2660 | sub cf::object::player::goto { |
2697 | sub cf::object::player::goto { |
2661 | my ($self, $path, $x, $y, $check, $done) = @_; |
2698 | my ($self, $path, $x, $y, $check, $done) = @_; |
|
|
2699 | |
|
|
2700 | if ($self->{_link_recursion} >= $MAX_LINKS) { |
|
|
2701 | error "FATAL: link recursion exceeded, ", $self->name, " goto $path $x $y, redirecting."; |
|
|
2702 | $self->failmsg ("Something went wrong inside the server - please contact an administrator!"); |
|
|
2703 | ($path, $x, $y) = @$EMERGENCY_POSITION; |
|
|
2704 | } |
2662 | |
2705 | |
2663 | # do generation counting so two concurrent goto's will be executed in-order |
2706 | # do generation counting so two concurrent goto's will be executed in-order |
2664 | my $gen = $self->{_goto_generation} = ++$GOTOGEN; |
2707 | my $gen = $self->{_goto_generation} = ++$GOTOGEN; |
2665 | |
2708 | |
2666 | $self->enter_link; |
2709 | $self->enter_link; |
… | |
… | |
2690 | my $map = eval { |
2733 | my $map = eval { |
2691 | my $map = defined $path ? cf::map::find $path : undef; |
2734 | my $map = defined $path ? cf::map::find $path : undef; |
2692 | |
2735 | |
2693 | if ($map) { |
2736 | if ($map) { |
2694 | $map = $map->customise_for ($self); |
2737 | $map = $map->customise_for ($self); |
2695 | $map = $check->($map) if $check && $map; |
2738 | $map = $check->($map, $x, $y, $self) if $check && $map; |
2696 | } else { |
2739 | } else { |
2697 | $self->message ("The exit to '$path' is closed.", cf::NDI_UNIQUE | cf::NDI_RED); |
2740 | $self->message ("The exit to '$path' is closed.", cf::NDI_UNIQUE | cf::NDI_RED); |
2698 | } |
2741 | } |
2699 | |
2742 | |
2700 | $map |
2743 | $map |
… | |
… | |
2708 | if ($gen == $self->{_goto_generation}) { |
2751 | if ($gen == $self->{_goto_generation}) { |
2709 | delete $self->{_goto_generation}; |
2752 | delete $self->{_goto_generation}; |
2710 | $self->leave_link ($map, $x, $y); |
2753 | $self->leave_link ($map, $x, $y); |
2711 | } |
2754 | } |
2712 | |
2755 | |
2713 | $done->() if $done; |
2756 | $done->($self) if $done; |
2714 | })->prio (1); |
2757 | })->prio (1); |
2715 | } |
2758 | } |
2716 | |
2759 | |
2717 | =item $player_object->enter_exit ($exit_object) |
2760 | =item $player_object->enter_exit ($exit_object) |
2718 | |
2761 | |
… | |
… | |
2811 | $self->message ("Something went wrong deep within the deliantra server. " |
2854 | $self->message ("Something went wrong deep within the deliantra server. " |
2812 | . "I'll try to bring you back to the map you were before. " |
2855 | . "I'll try to bring you back to the map you were before. " |
2813 | . "Please report this to the dungeon master!", |
2856 | . "Please report this to the dungeon master!", |
2814 | cf::NDI_UNIQUE | cf::NDI_RED); |
2857 | cf::NDI_UNIQUE | cf::NDI_RED); |
2815 | |
2858 | |
2816 | warn "ERROR in enter_exit: $@"; |
2859 | error "ERROR in enter_exit: $@"; |
2817 | $self->leave_link; |
2860 | $self->leave_link; |
2818 | } |
2861 | } |
2819 | })->prio (1); |
2862 | })->prio (1); |
2820 | } |
2863 | } |
2821 | |
2864 | |
… | |
… | |
3134 | |
3177 | |
3135 | $ns->ext_reply ($reply, @reply) |
3178 | $ns->ext_reply ($reply, @reply) |
3136 | if $reply; |
3179 | if $reply; |
3137 | |
3180 | |
3138 | } else { |
3181 | } else { |
3139 | warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; |
3182 | error "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; |
3140 | } |
3183 | } |
3141 | |
3184 | |
3142 | cf::override; |
3185 | cf::override; |
3143 | }, |
3186 | }, |
3144 | ); |
3187 | ); |
… | |
… | |
3274 | local @cf::_safe_eval_args = values %vars; |
3317 | local @cf::_safe_eval_args = values %vars; |
3275 | @res = wantarray ? eval eval : scalar eval $eval; |
3318 | @res = wantarray ? eval eval : scalar eval $eval; |
3276 | } |
3319 | } |
3277 | |
3320 | |
3278 | if ($@) { |
3321 | if ($@) { |
3279 | warn "$@"; |
3322 | warn "$@", |
3280 | warn "while executing safe code '$code'\n"; |
3323 | "while executing safe code '$code'\n", |
3281 | warn "with arguments " . (join " ", %vars) . "\n"; |
3324 | "with arguments " . (join " ", %vars) . "\n"; |
3282 | } |
3325 | } |
3283 | |
3326 | |
3284 | wantarray ? @res : $res[0] |
3327 | wantarray ? @res : $res[0] |
3285 | } |
3328 | } |
3286 | |
3329 | |
… | |
… | |
3320 | # for this (global event?) |
3363 | # for this (global event?) |
3321 | %ext::player_env::MUSIC_FACE_CACHE = (); |
3364 | %ext::player_env::MUSIC_FACE_CACHE = (); |
3322 | |
3365 | |
3323 | my $enc = JSON::XS->new->utf8->canonical->relaxed; |
3366 | my $enc = JSON::XS->new->utf8->canonical->relaxed; |
3324 | |
3367 | |
3325 | warn "loading facedata from $path\n"; |
3368 | trace "loading facedata from $path\n"; |
3326 | |
3369 | |
3327 | my $facedata; |
3370 | my $facedata; |
3328 | 0 < aio_load $path, $facedata |
3371 | 0 < aio_load $path, $facedata |
3329 | or die "$path: $!"; |
3372 | or die "$path: $!"; |
3330 | |
3373 | |
… | |
… | |
3364 | |
3407 | |
3365 | if (my $smooth = cf::face::find $info->{smooth}) { |
3408 | if (my $smooth = cf::face::find $info->{smooth}) { |
3366 | cf::face::set_smooth $idx, $smooth; |
3409 | cf::face::set_smooth $idx, $smooth; |
3367 | cf::face::set_smoothlevel $idx, $info->{smoothlevel}; |
3410 | cf::face::set_smoothlevel $idx, $info->{smoothlevel}; |
3368 | } else { |
3411 | } else { |
3369 | warn "smooth face '$info->{smooth}' not found for face '$face'"; |
3412 | error "smooth face '$info->{smooth}' not found for face '$face'"; |
3370 | } |
3413 | } |
3371 | |
3414 | |
3372 | cf::cede_to_tick; |
3415 | cf::cede_to_tick; |
3373 | } |
3416 | } |
3374 | } |
3417 | } |
… | |
… | |
3392 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3435 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3393 | |
3436 | |
3394 | cf::face::set_data $idx, 0, $info->{data}, $info->{hash}; |
3437 | cf::face::set_data $idx, 0, $info->{data}, $info->{hash}; |
3395 | cf::face::set_type $idx, $info->{type}; |
3438 | cf::face::set_type $idx, $info->{type}; |
3396 | } else { |
3439 | } else { |
3397 | $RESOURCE{$name} = $info; |
3440 | $RESOURCE{$name} = $info; # unused |
3398 | } |
3441 | } |
3399 | |
3442 | |
3400 | cf::cede_to_tick; |
3443 | cf::cede_to_tick; |
3401 | } |
3444 | } |
3402 | } |
3445 | } |
3403 | |
3446 | |
3404 | cf::global->invoke (EVENT_GLOBAL_RESOURCE_UPDATE); |
3447 | cf::global->invoke (EVENT_GLOBAL_RESOURCE_UPDATE); |
3405 | |
3448 | |
3406 | 1 |
3449 | 1 |
3407 | } |
3450 | } |
3408 | |
|
|
3409 | cf::global->attach (on_resource_update => sub { |
|
|
3410 | if (my $soundconf = $RESOURCE{"res/sound.conf"}) { |
|
|
3411 | $soundconf = JSON::XS->new->utf8->relaxed->decode ($soundconf->{data}); |
|
|
3412 | |
|
|
3413 | for (0 .. SOUND_CAST_SPELL_0 - 1) { |
|
|
3414 | my $sound = $soundconf->{compat}[$_] |
|
|
3415 | or next; |
|
|
3416 | |
|
|
3417 | my $face = cf::face::find "sound/$sound->[1]"; |
|
|
3418 | cf::sound::set $sound->[0] => $face; |
|
|
3419 | cf::sound::old_sound_index $_, $face; # gcfclient-compat |
|
|
3420 | } |
|
|
3421 | |
|
|
3422 | while (my ($k, $v) = each %{$soundconf->{event}}) { |
|
|
3423 | my $face = cf::face::find "sound/$v"; |
|
|
3424 | cf::sound::set $k => $face; |
|
|
3425 | } |
|
|
3426 | } |
|
|
3427 | }); |
|
|
3428 | |
3451 | |
3429 | register_exticmd fx_want => sub { |
3452 | register_exticmd fx_want => sub { |
3430 | my ($ns, $want) = @_; |
3453 | my ($ns, $want) = @_; |
3431 | |
3454 | |
3432 | while (my ($k, $v) = each %$want) { |
3455 | while (my ($k, $v) = each %$want) { |
… | |
… | |
3471 | sub reload_treasures { |
3494 | sub reload_treasures { |
3472 | load_resource_file "$DATADIR/treasures" |
3495 | load_resource_file "$DATADIR/treasures" |
3473 | or die "unable to load treasurelists\n"; |
3496 | or die "unable to load treasurelists\n"; |
3474 | } |
3497 | } |
3475 | |
3498 | |
|
|
3499 | sub reload_sound { |
|
|
3500 | trace "loading sound config from $DATADIR/sound\n"; |
|
|
3501 | |
|
|
3502 | 0 < Coro::AIO::aio_load "$DATADIR/sound", my $data |
|
|
3503 | or die "$DATADIR/sound $!"; |
|
|
3504 | |
|
|
3505 | my $soundconf = JSON::XS->new->utf8->relaxed->decode ($data); |
|
|
3506 | |
|
|
3507 | for (0 .. SOUND_CAST_SPELL_0 - 1) { |
|
|
3508 | my $sound = $soundconf->{compat}[$_] |
|
|
3509 | or next; |
|
|
3510 | |
|
|
3511 | my $face = cf::face::find "sound/$sound->[1]"; |
|
|
3512 | cf::sound::set $sound->[0] => $face; |
|
|
3513 | cf::sound::old_sound_index $_, $face; # gcfclient-compat |
|
|
3514 | } |
|
|
3515 | |
|
|
3516 | while (my ($k, $v) = each %{$soundconf->{event}}) { |
|
|
3517 | my $face = cf::face::find "sound/$v"; |
|
|
3518 | cf::sound::set $k => $face; |
|
|
3519 | } |
|
|
3520 | } |
|
|
3521 | |
3476 | sub reload_resources { |
3522 | sub reload_resources { |
3477 | warn "reloading resource files...\n"; |
3523 | trace "reloading resource files...\n"; |
3478 | |
3524 | |
3479 | reload_facedata; |
3525 | reload_facedata; |
|
|
3526 | reload_sound; |
3480 | reload_archetypes; |
3527 | reload_archetypes; |
3481 | reload_regions; |
3528 | reload_regions; |
3482 | reload_treasures; |
3529 | reload_treasures; |
3483 | |
3530 | |
3484 | warn "finished reloading resource files\n"; |
3531 | trace "finished reloading resource files\n"; |
3485 | } |
3532 | } |
3486 | |
3533 | |
3487 | sub reload_config { |
3534 | sub reload_config { |
3488 | warn "reloading config file...\n"; |
3535 | trace "reloading config file...\n"; |
3489 | |
3536 | |
3490 | open my $fh, "<:utf8", "$CONFDIR/config" |
3537 | open my $fh, "<:utf8", "$CONFDIR/config" |
3491 | or return; |
3538 | or return; |
3492 | |
3539 | |
3493 | local $/; |
3540 | local $/; |
3494 | *CFG = YAML::XS::Load scalar <$fh>; |
3541 | *CFG = YAML::XS::Load scalar <$fh>; |
3495 | |
3542 | |
3496 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37]; |
3543 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; |
3497 | |
3544 | |
3498 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3545 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3499 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
3546 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
3500 | |
3547 | |
3501 | if (exists $CFG{mlockall}) { |
3548 | if (exists $CFG{mlockall}) { |
… | |
… | |
3504 | and die "WARNING: m(un)lockall failed: $!\n"; |
3551 | and die "WARNING: m(un)lockall failed: $!\n"; |
3505 | }; |
3552 | }; |
3506 | warn $@ if $@; |
3553 | warn $@ if $@; |
3507 | } |
3554 | } |
3508 | |
3555 | |
3509 | warn "finished reloading resource files\n"; |
3556 | trace "finished reloading resource files\n"; |
3510 | } |
3557 | } |
3511 | |
3558 | |
3512 | sub pidfile() { |
3559 | sub pidfile() { |
3513 | sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT |
3560 | sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT |
3514 | or die "$PIDFILE: $!"; |
3561 | or die "$PIDFILE: $!"; |
… | |
… | |
3527 | seek $fh, 0, 0; |
3574 | seek $fh, 0, 0; |
3528 | print $fh $$; |
3575 | print $fh $$; |
3529 | } |
3576 | } |
3530 | |
3577 | |
3531 | sub main_loop { |
3578 | sub main_loop { |
3532 | warn "EV::loop starting\n"; |
3579 | trace "EV::loop starting\n"; |
3533 | if (1) { |
3580 | if (1) { |
3534 | EV::loop; |
3581 | EV::loop; |
3535 | } |
3582 | } |
3536 | warn "EV::loop returned\n"; |
3583 | trace "EV::loop returned\n"; |
3537 | goto &main_loop unless $REALLY_UNLOOP; |
3584 | goto &main_loop unless $REALLY_UNLOOP; |
3538 | } |
3585 | } |
3539 | |
3586 | |
3540 | sub main { |
3587 | sub main { |
3541 | cf::init_globals; # initialise logging |
3588 | cf::init_globals; # initialise logging |
3542 | |
3589 | |
3543 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3590 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3544 | LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3591 | LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3545 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3592 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3546 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3593 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3547 | |
|
|
3548 | cf::init_experience; |
|
|
3549 | cf::init_anim; |
|
|
3550 | cf::init_attackmess; |
|
|
3551 | cf::init_dynamic; |
|
|
3552 | |
3594 | |
3553 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3595 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3554 | |
3596 | |
3555 | # we must not ever block the main coroutine |
3597 | # we must not ever block the main coroutine |
3556 | local $Coro::idle = sub { |
3598 | local $Coro::idle = sub { |
… | |
… | |
3562 | }; |
3604 | }; |
3563 | |
3605 | |
3564 | evthread_start IO::AIO::poll_fileno; |
3606 | evthread_start IO::AIO::poll_fileno; |
3565 | |
3607 | |
3566 | cf::sync_job { |
3608 | cf::sync_job { |
|
|
3609 | cf::init_experience; |
|
|
3610 | cf::init_anim; |
|
|
3611 | cf::init_attackmess; |
|
|
3612 | cf::init_dynamic; |
|
|
3613 | |
3567 | cf::load_settings; |
3614 | cf::load_settings; |
3568 | cf::load_materials; |
3615 | cf::load_materials; |
3569 | |
3616 | |
3570 | reload_resources; |
3617 | reload_resources; |
3571 | reload_config; |
3618 | reload_config; |
… | |
… | |
3587 | use POSIX (); |
3634 | use POSIX (); |
3588 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3635 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
3589 | |
3636 | |
3590 | (pop @POST_INIT)->(0) while @POST_INIT; |
3637 | (pop @POST_INIT)->(0) while @POST_INIT; |
3591 | }; |
3638 | }; |
|
|
3639 | |
|
|
3640 | cf::object::thawer::errors_are_fatal 0; |
|
|
3641 | info "parse errors in files are no longer fatal from this point on.\n"; |
3592 | |
3642 | |
3593 | main_loop; |
3643 | main_loop; |
3594 | } |
3644 | } |
3595 | |
3645 | |
3596 | ############################################################################# |
3646 | ############################################################################# |
… | |
… | |
3637 | or return; |
3687 | or return; |
3638 | |
3688 | |
3639 | aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE |
3689 | aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE |
3640 | and return; |
3690 | and return; |
3641 | |
3691 | |
3642 | warn sprintf "runtime file written (%gs).\n", AE::time - $t0; |
3692 | trace sprintf "runtime file written (%gs).\n", AE::time - $t0; |
3643 | |
3693 | |
3644 | 1 |
3694 | 1 |
3645 | } |
3695 | } |
3646 | |
3696 | |
3647 | our $uuid_lock; |
3697 | our $uuid_lock; |
… | |
… | |
3659 | or return; |
3709 | or return; |
3660 | |
3710 | |
3661 | my $value = uuid_seq uuid_cur; |
3711 | my $value = uuid_seq uuid_cur; |
3662 | |
3712 | |
3663 | unless ($value) { |
3713 | unless ($value) { |
3664 | warn "cowardly refusing to write zero uuid value!\n"; |
3714 | info "cowardly refusing to write zero uuid value!\n"; |
3665 | return; |
3715 | return; |
3666 | } |
3716 | } |
3667 | |
3717 | |
3668 | my $value = uuid_str $value + $uuid_skip; |
3718 | my $value = uuid_str $value + $uuid_skip; |
3669 | $uuid_skip = 0; |
3719 | $uuid_skip = 0; |
… | |
… | |
3679 | or return; |
3729 | or return; |
3680 | |
3730 | |
3681 | aio_rename "$uuid~", $uuid |
3731 | aio_rename "$uuid~", $uuid |
3682 | and return; |
3732 | and return; |
3683 | |
3733 | |
3684 | warn "uuid file written ($value).\n"; |
3734 | trace "uuid file written ($value).\n"; |
3685 | |
3735 | |
3686 | 1 |
3736 | 1 |
3687 | |
3737 | |
3688 | } |
3738 | } |
3689 | |
3739 | |
… | |
… | |
3695 | } |
3745 | } |
3696 | |
3746 | |
3697 | sub emergency_save() { |
3747 | sub emergency_save() { |
3698 | my $freeze_guard = cf::freeze_mainloop; |
3748 | my $freeze_guard = cf::freeze_mainloop; |
3699 | |
3749 | |
3700 | warn "emergency_perl_save: enter\n"; |
3750 | info "emergency_perl_save: enter\n"; |
3701 | |
3751 | |
3702 | cf::sync_job { |
3752 | cf::sync_job { |
3703 | # this is a trade-off: we want to be very quick here, so |
3753 | # this is a trade-off: we want to be very quick here, so |
3704 | # save all maps without fsync, and later call a global sync |
3754 | # save all maps without fsync, and later call a global sync |
3705 | # (which in turn might be very very slow) |
3755 | # (which in turn might be very very slow) |
3706 | local $USE_FSYNC = 0; |
3756 | local $USE_FSYNC = 0; |
3707 | |
3757 | |
3708 | # use a peculiar iteration method to avoid tripping on perl |
3758 | # use a peculiar iteration method to avoid tripping on perl |
3709 | # refcount bugs in for. also avoids problems with players |
3759 | # refcount bugs in for. also avoids problems with players |
3710 | # and maps saved/destroyed asynchronously. |
3760 | # and maps saved/destroyed asynchronously. |
3711 | warn "emergency_perl_save: begin player save\n"; |
3761 | info "emergency_perl_save: begin player save\n"; |
3712 | for my $login (keys %cf::PLAYER) { |
3762 | for my $login (keys %cf::PLAYER) { |
3713 | my $pl = $cf::PLAYER{$login} or next; |
3763 | my $pl = $cf::PLAYER{$login} or next; |
3714 | $pl->valid or next; |
3764 | $pl->valid or next; |
3715 | delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt |
3765 | delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt |
3716 | $pl->save; |
3766 | $pl->save; |
3717 | } |
3767 | } |
3718 | warn "emergency_perl_save: end player save\n"; |
3768 | info "emergency_perl_save: end player save\n"; |
3719 | |
3769 | |
3720 | warn "emergency_perl_save: begin map save\n"; |
3770 | info "emergency_perl_save: begin map save\n"; |
3721 | for my $path (keys %cf::MAP) { |
3771 | for my $path (keys %cf::MAP) { |
3722 | my $map = $cf::MAP{$path} or next; |
3772 | my $map = $cf::MAP{$path} or next; |
3723 | $map->valid or next; |
3773 | $map->valid or next; |
3724 | $map->save; |
3774 | $map->save; |
3725 | } |
3775 | } |
3726 | warn "emergency_perl_save: end map save\n"; |
3776 | info "emergency_perl_save: end map save\n"; |
3727 | |
3777 | |
3728 | warn "emergency_perl_save: begin database checkpoint\n"; |
3778 | info "emergency_perl_save: begin database checkpoint\n"; |
3729 | BDB::db_env_txn_checkpoint $DB_ENV; |
3779 | BDB::db_env_txn_checkpoint $DB_ENV; |
3730 | warn "emergency_perl_save: end database checkpoint\n"; |
3780 | info "emergency_perl_save: end database checkpoint\n"; |
3731 | |
3781 | |
3732 | warn "emergency_perl_save: begin write uuid\n"; |
3782 | info "emergency_perl_save: begin write uuid\n"; |
3733 | write_uuid_sync 1; |
3783 | write_uuid_sync 1; |
3734 | warn "emergency_perl_save: end write uuid\n"; |
3784 | info "emergency_perl_save: end write uuid\n"; |
3735 | }; |
3785 | }; |
3736 | |
3786 | |
3737 | warn "emergency_perl_save: starting sync()\n"; |
3787 | info "emergency_perl_save: starting sync()\n"; |
3738 | IO::AIO::aio_sync sub { |
3788 | IO::AIO::aio_sync sub { |
3739 | warn "emergency_perl_save: finished sync()\n"; |
3789 | info "emergency_perl_save: finished sync()\n"; |
3740 | }; |
3790 | }; |
3741 | |
3791 | |
3742 | warn "emergency_perl_save: leave\n"; |
3792 | info "emergency_perl_save: leave\n"; |
3743 | } |
3793 | } |
3744 | |
3794 | |
3745 | sub post_cleanup { |
3795 | sub post_cleanup { |
3746 | my ($make_core) = @_; |
3796 | my ($make_core) = @_; |
3747 | |
3797 | |
3748 | warn Carp::longmess "post_cleanup backtrace" |
3798 | error Carp::longmess "post_cleanup backtrace" |
3749 | if $make_core; |
3799 | if $make_core; |
3750 | |
3800 | |
3751 | my $fh = pidfile; |
3801 | my $fh = pidfile; |
3752 | unlink $PIDFILE if <$fh> == $$; |
3802 | unlink $PIDFILE if <$fh> == $$; |
3753 | } |
3803 | } |
… | |
… | |
3773 | my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH}; |
3823 | my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH}; |
3774 | for my $name (keys %$leaf_symtab) { |
3824 | for my $name (keys %$leaf_symtab) { |
3775 | _gv_clear *{"$pkg$name"}; |
3825 | _gv_clear *{"$pkg$name"}; |
3776 | # use PApp::Util; PApp::Util::sv_dump *{"$pkg$name"}; |
3826 | # use PApp::Util; PApp::Util::sv_dump *{"$pkg$name"}; |
3777 | } |
3827 | } |
3778 | warn "cleared package $pkg\n";#d# |
|
|
3779 | } |
3828 | } |
3780 | |
3829 | |
3781 | sub do_reload_perl() { |
3830 | sub do_reload_perl() { |
3782 | # can/must only be called in main |
3831 | # can/must only be called in main |
3783 | if ($Coro::current != $Coro::main) { |
3832 | if (in_main) { |
3784 | warn "can only reload from main coroutine"; |
3833 | error "can only reload from main coroutine"; |
3785 | return; |
3834 | return; |
3786 | } |
3835 | } |
3787 | |
3836 | |
3788 | return if $RELOAD++; |
3837 | return if $RELOAD++; |
3789 | |
3838 | |
3790 | my $t1 = AE::time; |
3839 | my $t1 = AE::time; |
3791 | |
3840 | |
3792 | while ($RELOAD) { |
3841 | while ($RELOAD) { |
3793 | warn "reloading..."; |
3842 | info "reloading..."; |
3794 | |
3843 | |
3795 | warn "entering sync_job"; |
3844 | trace "entering sync_job"; |
3796 | |
3845 | |
3797 | cf::sync_job { |
3846 | cf::sync_job { |
3798 | cf::write_runtime_sync; # external watchdog should not bark |
3847 | cf::write_runtime_sync; # external watchdog should not bark |
3799 | cf::emergency_save; |
3848 | cf::emergency_save; |
3800 | cf::write_runtime_sync; # external watchdog should not bark |
3849 | cf::write_runtime_sync; # external watchdog should not bark |
3801 | |
3850 | |
3802 | warn "syncing database to disk"; |
3851 | trace "syncing database to disk"; |
3803 | BDB::db_env_txn_checkpoint $DB_ENV; |
3852 | BDB::db_env_txn_checkpoint $DB_ENV; |
3804 | |
3853 | |
3805 | # if anything goes wrong in here, we should simply crash as we already saved |
3854 | # if anything goes wrong in here, we should simply crash as we already saved |
3806 | |
3855 | |
3807 | warn "flushing outstanding aio requests"; |
3856 | trace "flushing outstanding aio requests"; |
3808 | while (IO::AIO::nreqs || BDB::nreqs) { |
3857 | while (IO::AIO::nreqs || BDB::nreqs) { |
3809 | Coro::EV::timer_once 0.01; # let the sync_job do it's thing |
3858 | Coro::EV::timer_once 0.01; # let the sync_job do it's thing |
3810 | } |
3859 | } |
3811 | |
3860 | |
3812 | warn "cancelling all extension coros"; |
3861 | trace "cancelling all extension coros"; |
3813 | $_->cancel for values %EXT_CORO; |
3862 | $_->cancel for values %EXT_CORO; |
3814 | %EXT_CORO = (); |
3863 | %EXT_CORO = (); |
3815 | |
3864 | |
3816 | warn "removing commands"; |
3865 | trace "removing commands"; |
3817 | %COMMAND = (); |
3866 | %COMMAND = (); |
3818 | |
3867 | |
3819 | warn "removing ext/exti commands"; |
3868 | trace "removing ext/exti commands"; |
3820 | %EXTCMD = (); |
3869 | %EXTCMD = (); |
3821 | %EXTICMD = (); |
3870 | %EXTICMD = (); |
3822 | |
3871 | |
3823 | warn "unloading/nuking all extensions"; |
3872 | trace "unloading/nuking all extensions"; |
3824 | for my $pkg (@EXTS) { |
3873 | for my $pkg (@EXTS) { |
3825 | warn "... unloading $pkg"; |
3874 | trace "... unloading $pkg"; |
3826 | |
3875 | |
3827 | if (my $cb = $pkg->can ("unload")) { |
3876 | if (my $cb = $pkg->can ("unload")) { |
3828 | eval { |
3877 | eval { |
3829 | $cb->($pkg); |
3878 | $cb->($pkg); |
3830 | 1 |
3879 | 1 |
3831 | } or warn "$pkg unloaded, but with errors: $@"; |
3880 | } or error "$pkg unloaded, but with errors: $@"; |
3832 | } |
3881 | } |
3833 | |
3882 | |
3834 | warn "... clearing $pkg"; |
3883 | trace "... clearing $pkg"; |
3835 | clear_package $pkg; |
3884 | clear_package $pkg; |
3836 | } |
3885 | } |
3837 | |
3886 | |
3838 | warn "unloading all perl modules loaded from $LIBDIR"; |
3887 | trace "unloading all perl modules loaded from $LIBDIR"; |
3839 | while (my ($k, $v) = each %INC) { |
3888 | while (my ($k, $v) = each %INC) { |
3840 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
3889 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
3841 | |
3890 | |
3842 | warn "... unloading $k"; |
3891 | trace "... unloading $k"; |
3843 | delete $INC{$k}; |
3892 | delete $INC{$k}; |
3844 | |
3893 | |
3845 | $k =~ s/\.pm$//; |
3894 | $k =~ s/\.pm$//; |
3846 | $k =~ s/\//::/g; |
3895 | $k =~ s/\//::/g; |
3847 | |
3896 | |
… | |
… | |
3850 | } |
3899 | } |
3851 | |
3900 | |
3852 | clear_package $k; |
3901 | clear_package $k; |
3853 | } |
3902 | } |
3854 | |
3903 | |
3855 | warn "getting rid of safe::, as good as possible"; |
3904 | trace "getting rid of safe::, as good as possible"; |
3856 | clear_package "safe::$_" |
3905 | clear_package "safe::$_" |
3857 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
3906 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
3858 | |
3907 | |
3859 | warn "unloading cf.pm \"a bit\""; |
3908 | trace "unloading cf.pm \"a bit\""; |
3860 | delete $INC{"cf.pm"}; |
3909 | delete $INC{"cf.pm"}; |
3861 | delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; |
3910 | delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; |
3862 | |
3911 | |
3863 | # don't, removes xs symbols, too, |
3912 | # don't, removes xs symbols, too, |
3864 | # and global variables created in xs |
3913 | # and global variables created in xs |
3865 | #clear_package __PACKAGE__; |
3914 | #clear_package __PACKAGE__; |
3866 | |
3915 | |
3867 | warn "unload completed, starting to reload now"; |
3916 | info "unload completed, starting to reload now"; |
3868 | |
3917 | |
3869 | warn "reloading cf.pm"; |
3918 | trace "reloading cf.pm"; |
3870 | require cf; |
3919 | require cf; |
3871 | cf::_connect_to_perl_1; |
3920 | cf::_connect_to_perl_1; |
3872 | |
3921 | |
3873 | warn "loading config and database again"; |
3922 | trace "loading config and database again"; |
3874 | cf::reload_config; |
3923 | cf::reload_config; |
3875 | |
3924 | |
3876 | warn "loading extensions"; |
3925 | trace "loading extensions"; |
3877 | cf::load_extensions; |
3926 | cf::load_extensions; |
3878 | |
3927 | |
3879 | if ($REATTACH_ON_RELOAD) { |
3928 | if ($REATTACH_ON_RELOAD) { |
3880 | warn "reattaching attachments to objects/players"; |
3929 | trace "reattaching attachments to objects/players"; |
3881 | _global_reattach; # objects, sockets |
3930 | _global_reattach; # objects, sockets |
3882 | warn "reattaching attachments to maps"; |
3931 | trace "reattaching attachments to maps"; |
3883 | reattach $_ for values %MAP; |
3932 | reattach $_ for values %MAP; |
3884 | warn "reattaching attachments to players"; |
3933 | trace "reattaching attachments to players"; |
3885 | reattach $_ for values %PLAYER; |
3934 | reattach $_ for values %PLAYER; |
3886 | } |
3935 | } |
3887 | |
3936 | |
3888 | warn "running post_init jobs"; |
3937 | trace "running post_init jobs"; |
3889 | (pop @POST_INIT)->(1) while @POST_INIT; |
3938 | (pop @POST_INIT)->(1) while @POST_INIT; |
3890 | |
3939 | |
3891 | warn "leaving sync_job"; |
3940 | trace "leaving sync_job"; |
3892 | |
3941 | |
3893 | 1 |
3942 | 1 |
3894 | } or do { |
3943 | } or do { |
3895 | warn $@; |
3944 | error $@; |
3896 | cf::cleanup "error while reloading, exiting."; |
3945 | cf::cleanup "error while reloading, exiting."; |
3897 | }; |
3946 | }; |
3898 | |
3947 | |
3899 | warn "reloaded"; |
3948 | info "reloaded"; |
3900 | --$RELOAD; |
3949 | --$RELOAD; |
3901 | } |
3950 | } |
3902 | |
3951 | |
3903 | $t1 = AE::time - $t1; |
3952 | $t1 = AE::time - $t1; |
3904 | warn "reload completed in ${t1}s\n"; |
3953 | info "reload completed in ${t1}s\n"; |
3905 | }; |
3954 | }; |
3906 | |
3955 | |
3907 | our $RELOAD_WATCHER; # used only during reload |
3956 | our $RELOAD_WATCHER; # used only during reload |
3908 | |
3957 | |
3909 | sub reload_perl() { |
3958 | sub reload_perl() { |
… | |
… | |
3969 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
4018 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3970 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
4019 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
3971 | Coro::async_pool { |
4020 | Coro::async_pool { |
3972 | $Coro::current->{desc} = "runtime saver"; |
4021 | $Coro::current->{desc} = "runtime saver"; |
3973 | write_runtime_sync |
4022 | write_runtime_sync |
3974 | or warn "ERROR: unable to write runtime file: $!"; |
4023 | or error "ERROR: unable to write runtime file: $!"; |
3975 | }; |
4024 | }; |
3976 | } |
4025 | } |
3977 | |
4026 | |
3978 | if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { |
4027 | if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { |
3979 | $sig->send; |
4028 | $sig->send; |
… | |
… | |
3987 | |
4036 | |
3988 | if (0) { |
4037 | if (0) { |
3989 | if ($NEXT_TICK) { |
4038 | if ($NEXT_TICK) { |
3990 | my $jitter = $TICK_START - $NEXT_TICK; |
4039 | my $jitter = $TICK_START - $NEXT_TICK; |
3991 | $JITTER = $JITTER * 0.75 + $jitter * 0.25; |
4040 | $JITTER = $JITTER * 0.75 + $jitter * 0.25; |
3992 | warn "jitter $JITTER\n";#d# |
4041 | debug "jitter $JITTER\n";#d# |
3993 | } |
4042 | } |
3994 | } |
4043 | } |
3995 | } |
4044 | } |
3996 | |
4045 | |
3997 | { |
4046 | { |
… | |
… | |
4007 | $DB_ENV->set_flags (&BDB::LOG_AUTOREMOVE ) if BDB::VERSION v0, v4.7; |
4056 | $DB_ENV->set_flags (&BDB::LOG_AUTOREMOVE ) if BDB::VERSION v0, v4.7; |
4008 | $DB_ENV->log_set_config (&BDB::LOG_AUTO_REMOVE) if BDB::VERSION v4.7; |
4057 | $DB_ENV->log_set_config (&BDB::LOG_AUTO_REMOVE) if BDB::VERSION v4.7; |
4009 | $DB_ENV->set_timeout (30, BDB::SET_TXN_TIMEOUT); |
4058 | $DB_ENV->set_timeout (30, BDB::SET_TXN_TIMEOUT); |
4010 | $DB_ENV->set_timeout (30, BDB::SET_LOCK_TIMEOUT); |
4059 | $DB_ENV->set_timeout (30, BDB::SET_LOCK_TIMEOUT); |
4011 | |
4060 | |
|
|
4061 | (Coro::async { # async to keep sync_job from complaining |
4012 | cf::sync_job { |
4062 | cf::sync_job { |
4013 | eval { |
4063 | eval { |
4014 | BDB::db_env_open |
4064 | BDB::db_env_open |
4015 | $DB_ENV, |
4065 | $DB_ENV, |
4016 | $BDBDIR, |
4066 | $BDBDIR, |
4017 | BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL | BDB::INIT_TXN |
4067 | BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL | BDB::INIT_TXN |
4018 | | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, |
4068 | | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, |
4019 | 0666; |
4069 | 0666; |
4020 | |
4070 | |
4021 | cf::cleanup "db_env_open($BDBDIR): $!" if $!; |
4071 | cf::cleanup "db_env_open($BDBDIR): $!" if $!; |
|
|
4072 | }; |
|
|
4073 | |
|
|
4074 | cf::cleanup "db_env_open(db): $@" if $@; |
4022 | }; |
4075 | }; |
4023 | |
4076 | })->join; |
4024 | cf::cleanup "db_env_open(db): $@" if $@; |
|
|
4025 | }; |
|
|
4026 | } |
4077 | } |
4027 | |
4078 | |
4028 | $BDB_DEADLOCK_WATCHER = EV::periodic 0, 3, 0, sub { |
4079 | $BDB_DEADLOCK_WATCHER = EV::periodic 0, 3, 0, sub { |
4029 | BDB::db_env_lock_detect $DB_ENV, 0, BDB::LOCK_DEFAULT, 0, sub { }; |
4080 | BDB::db_env_lock_detect $DB_ENV, 0, BDB::LOCK_DEFAULT, 0, sub { }; |
4030 | }; |
4081 | }; |