… | |
… | |
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.3 (); |
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; |
… | |
… | |
245 | # wake up all waiters, to be on the safe side |
245 | # wake up all waiters, to be on the safe side |
246 | $_->ready for @{ delete $LOCK{$key} }; |
246 | $_->ready for @{ delete $LOCK{$key} }; |
247 | } |
247 | } |
248 | } |
248 | } |
249 | |
249 | |
|
|
250 | =item cf::async { BLOCK } |
|
|
251 | |
|
|
252 | Like C<Coro::async>, but runs the given BLOCK in an eval and only logs the |
|
|
253 | error instead of exiting the server in case of a problem. |
|
|
254 | |
|
|
255 | =cut |
|
|
256 | |
|
|
257 | sub async(&) { |
|
|
258 | my ($cb) = @_; |
|
|
259 | |
|
|
260 | Coro::async { |
|
|
261 | eval { $cb->() }; |
|
|
262 | warn $@ if $@; |
|
|
263 | } |
|
|
264 | } |
|
|
265 | |
250 | =item cf::sync_job { BLOCK } |
266 | =item cf::sync_job { BLOCK } |
251 | |
267 | |
252 | The design of crossfire+ requires that the main coro ($Coro::main) is |
268 | The design of crossfire+ requires that the main coro ($Coro::main) is |
253 | always able to handle events or runnable, as crossfire+ is only partly |
269 | always able to handle events or runnable, as crossfire+ is only partly |
254 | reentrant. Thus "blocking" it by e.g. waiting for I/O is not acceptable. |
270 | reentrant. Thus "blocking" it by e.g. waiting for I/O is not acceptable. |
… | |
… | |
301 | =cut |
317 | =cut |
302 | |
318 | |
303 | sub coro(&) { |
319 | sub coro(&) { |
304 | my $cb = shift; |
320 | my $cb = shift; |
305 | |
321 | |
306 | my $coro; $coro = async { |
322 | my $coro = &cf::async ($cb); |
307 | eval { |
|
|
308 | $cb->(); |
|
|
309 | }; |
|
|
310 | warn $@ if $@; |
|
|
311 | }; |
|
|
312 | |
323 | |
313 | $coro->on_destroy (sub { |
324 | $coro->on_destroy (sub { |
314 | delete $EXT_CORO{$coro+0}; |
325 | delete $EXT_CORO{$coro+0}; |
315 | }); |
326 | }); |
316 | $EXT_CORO{$coro+0} = $coro; |
327 | $EXT_CORO{$coro+0} = $coro; |
… | |
… | |
1153 | # and all this just because we cannot iterate over |
1164 | # and all this just because we cannot iterate over |
1154 | # all maps in C++... |
1165 | # all maps in C++... |
1155 | sub change_all_map_light { |
1166 | sub change_all_map_light { |
1156 | my ($change) = @_; |
1167 | my ($change) = @_; |
1157 | |
1168 | |
1158 | $_->change_map_light ($change) for values %cf::MAP; |
1169 | $_->change_map_light ($change) |
|
|
1170 | for grep $_->outdoor, values %cf::MAP; |
1159 | } |
1171 | } |
1160 | |
1172 | |
1161 | sub try_load_header($) { |
1173 | sub try_load_header($) { |
1162 | my ($path) = @_; |
1174 | my ($path) = @_; |
1163 | |
1175 | |
… | |
… | |
1174 | $map->{load_path} = $path; |
1186 | $map->{load_path} = $path; |
1175 | |
1187 | |
1176 | $map |
1188 | $map |
1177 | } |
1189 | } |
1178 | |
1190 | |
|
|
1191 | sub find_map; |
1179 | sub find_map { |
1192 | sub find_map { |
1180 | my ($path, $origin) = @_; |
1193 | my ($path, $origin) = @_; |
1181 | |
1194 | |
1182 | #warn "find_map<$path,$origin>\n";#d# |
1195 | #warn "find_map<$path,$origin>\n";#d# |
1183 | |
1196 | |
… | |
… | |
1210 | $map->instantiate; |
1223 | $map->instantiate; |
1211 | |
1224 | |
1212 | # per-player maps become, after loading, normal maps |
1225 | # per-player maps become, after loading, normal maps |
1213 | $map->per_player (0) if $path->{user_rel}; |
1226 | $map->per_player (0) if $path->{user_rel}; |
1214 | } |
1227 | } |
1215 | #Coro::Timer::sleep 1;#d# |
|
|
1216 | |
1228 | |
1217 | $map->path ($key); |
1229 | $map->path ($key); |
1218 | $map->{path} = $path; |
1230 | $map->{path} = $path; |
1219 | $map->{last_save} = $cf::RUNTIME; |
1231 | $map->{last_save} = $cf::RUNTIME; |
1220 | $map->last_access ($cf::RUNTIME); |
1232 | $map->last_access ($cf::RUNTIME); |
1221 | |
1233 | |
1222 | if ($map->should_reset) { |
1234 | if ($map->should_reset) { |
1223 | $map->reset; |
1235 | $map->reset; |
|
|
1236 | undef $guard; |
1224 | $map = find_map $path; |
1237 | $map = find_map $path |
|
|
1238 | or return; |
1225 | } |
1239 | } |
1226 | |
1240 | |
1227 | $cf::MAP{$key} = $map |
1241 | $cf::MAP{$key} = $map |
1228 | } |
1242 | } |
1229 | } |
1243 | } |
… | |
… | |
1308 | } |
1322 | } |
1309 | |
1323 | |
1310 | sub swap_out { |
1324 | sub swap_out { |
1311 | my ($self) = @_; |
1325 | my ($self) = @_; |
1312 | |
1326 | |
|
|
1327 | # save first because save cedes |
|
|
1328 | $self->save; |
|
|
1329 | |
1313 | return if $self->players; |
1330 | return if $self->players; |
1314 | return if $self->in_memory != cf::MAP_IN_MEMORY; |
1331 | return if $self->in_memory != cf::MAP_IN_MEMORY; |
1315 | return if $self->{deny_save}; |
1332 | return if $self->{deny_save}; |
1316 | |
1333 | |
1317 | $self->save; |
|
|
1318 | $self->clear; |
1334 | $self->clear; |
1319 | $self->in_memory (cf::MAP_SWAPPED); |
1335 | $self->in_memory (cf::MAP_SWAPPED); |
1320 | } |
1336 | } |
1321 | |
1337 | |
1322 | sub reset_at { |
1338 | sub reset_at { |
… | |
… | |
1535 | |
1551 | |
1536 | # try to abort aborted map switching on player login :) |
1552 | # try to abort aborted map switching on player login :) |
1537 | # should happen only on crashes |
1553 | # should happen only on crashes |
1538 | if ($pl->ob->{_link_pos}) { |
1554 | if ($pl->ob->{_link_pos}) { |
1539 | $pl->ob->enter_link; |
1555 | $pl->ob->enter_link; |
1540 | Coro::async { |
1556 | cf::async { |
1541 | # we need this sleep as the login has a concurrent enter_exit running |
1557 | # we need this sleep as the login has a concurrent enter_exit running |
1542 | # and this sleep increases chances of the player not ending up in scorn |
1558 | # and this sleep increases chances of the player not ending up in scorn |
1543 | Coro::Timer::sleep 1; |
1559 | Coro::Timer::sleep 1; |
1544 | $pl->ob->leave_link; |
1560 | $pl->ob->leave_link; |
1545 | }; |
1561 | }; |
… | |
… | |
1554 | sub cf::object::player::goto_map { |
1570 | sub cf::object::player::goto_map { |
1555 | my ($self, $path, $x, $y) = @_; |
1571 | my ($self, $path, $x, $y) = @_; |
1556 | |
1572 | |
1557 | $self->enter_link; |
1573 | $self->enter_link; |
1558 | |
1574 | |
1559 | (Coro::async { |
1575 | (cf::async { |
1560 | $path = new cf::path $path; |
1576 | $path = new cf::path $path; |
1561 | |
1577 | |
1562 | my $map = cf::map::find_map $path->as_string; |
1578 | my $map = cf::map::find_map $path->as_string; |
1563 | $map = $map->customise_for ($self) if $map; |
1579 | $map = $map->customise_for ($self) if $map; |
1564 | |
1580 | |
… | |
… | |
1626 | |
1642 | |
1627 | return unless $self->type == cf::PLAYER; |
1643 | return unless $self->type == cf::PLAYER; |
1628 | |
1644 | |
1629 | $self->enter_link; |
1645 | $self->enter_link; |
1630 | |
1646 | |
1631 | (Coro::async { |
1647 | (cf::async { |
1632 | unless (eval { |
1648 | unless (eval { |
1633 | |
|
|
1634 | prepare_random_map $exit |
1649 | prepare_random_map $exit |
1635 | if $exit->slaying eq "/!"; |
1650 | if $exit->slaying eq "/!"; |
1636 | |
1651 | |
1637 | my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path; |
1652 | my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path; |
1638 | $self->goto_map ($path, $exit->stats->hp, $exit->stats->sp); |
1653 | $self->goto_map ($path, $exit->stats->hp, $exit->stats->sp); |
… | |
… | |
1700 | on_reply => sub { |
1715 | on_reply => sub { |
1701 | my ($ns, $msg) = @_; |
1716 | my ($ns, $msg) = @_; |
1702 | |
1717 | |
1703 | # this weird shuffling is so that direct followup queries |
1718 | # this weird shuffling is so that direct followup queries |
1704 | # get handled first |
1719 | # get handled first |
1705 | my $queue = delete $ns->{query_queue}; |
1720 | my $queue = delete $ns->{query_queue} |
|
|
1721 | or return; # be conservative, not sure how that can happen, but we saw a crash here |
1706 | |
1722 | |
1707 | (shift @$queue)->[1]->($msg); |
1723 | (shift @$queue)->[1]->($msg); |
1708 | |
1724 | |
1709 | push @{ $ns->{query_queue} }, @$queue; |
1725 | push @{ $ns->{query_queue} }, @$queue; |
1710 | |
1726 | |
… | |
… | |
1727 | =cut |
1743 | =cut |
1728 | |
1744 | |
1729 | sub cf::client::coro { |
1745 | sub cf::client::coro { |
1730 | my ($self, $cb) = @_; |
1746 | my ($self, $cb) = @_; |
1731 | |
1747 | |
1732 | my $coro; $coro = async { |
1748 | my $coro = &cf::async ($cb); |
1733 | eval { |
|
|
1734 | $cb->(); |
|
|
1735 | }; |
|
|
1736 | warn $@ if $@; |
|
|
1737 | }; |
|
|
1738 | |
1749 | |
1739 | $coro->on_destroy (sub { |
1750 | $coro->on_destroy (sub { |
1740 | delete $self->{_coro}{$coro+0}; |
1751 | delete $self->{_coro}{$coro+0}; |
1741 | }); |
1752 | }); |
1742 | |
1753 | |
… | |
… | |
1914 | |
1925 | |
1915 | { |
1926 | { |
1916 | my $path = cf::localdir . "/database.pst"; |
1927 | my $path = cf::localdir . "/database.pst"; |
1917 | |
1928 | |
1918 | sub db_load() { |
1929 | sub db_load() { |
1919 | warn "loading database $path\n";#d# remove later |
|
|
1920 | $DB = stat $path ? Storable::retrieve $path : { }; |
1930 | $DB = stat $path ? Storable::retrieve $path : { }; |
1921 | } |
1931 | } |
1922 | |
1932 | |
1923 | my $pid; |
1933 | my $pid; |
1924 | |
1934 | |
1925 | sub db_save() { |
1935 | sub db_save() { |
1926 | warn "saving database $path\n";#d# remove later |
|
|
1927 | waitpid $pid, 0 if $pid; |
1936 | waitpid $pid, 0 if $pid; |
1928 | if (0 == ($pid = fork)) { |
1937 | if (0 == ($pid = fork)) { |
1929 | $DB->{_meta}{version} = 1; |
1938 | $DB->{_meta}{version} = 1; |
1930 | Storable::nstore $DB, "$path~"; |
1939 | Storable::nstore $DB, "$path~"; |
1931 | rename "$path~", $path; |
1940 | rename "$path~", $path; |
… | |
… | |
2191 | or warn "ERROR: unable to write runtime file: $!"; |
2200 | or warn "ERROR: unable to write runtime file: $!"; |
2192 | })->(); |
2201 | })->(); |
2193 | }, |
2202 | }, |
2194 | ); |
2203 | ); |
2195 | |
2204 | |
|
|
2205 | END { cf::emergency_save } |
|
|
2206 | |
2196 | 1 |
2207 | 1 |
2197 | |
2208 | |