… | |
… | |
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; |
… | |
… | |
71 | mkdir cf::localdir . "/" . cf::uniquedir; |
71 | mkdir cf::localdir . "/" . cf::uniquedir; |
72 | mkdir $RANDOM_MAPS; |
72 | mkdir $RANDOM_MAPS; |
73 | |
73 | |
74 | # a special map that is always available |
74 | # a special map that is always available |
75 | our $LINK_MAP; |
75 | our $LINK_MAP; |
76 | |
76 | our $EMERGENCY_POSITION; |
77 | our $EMERGENCY_POSITION = $cf::CFG{emergency_position} || ["/world/world_105_115", 5, 37]; |
|
|
78 | |
77 | |
79 | ############################################################################# |
78 | ############################################################################# |
80 | |
79 | |
81 | =head2 GLOBAL VARIABLES |
80 | =head2 GLOBAL VARIABLES |
82 | |
81 | |
… | |
… | |
245 | # wake up all waiters, to be on the safe side |
244 | # wake up all waiters, to be on the safe side |
246 | $_->ready for @{ delete $LOCK{$key} }; |
245 | $_->ready for @{ delete $LOCK{$key} }; |
247 | } |
246 | } |
248 | } |
247 | } |
249 | |
248 | |
|
|
249 | =item cf::async { BLOCK } |
|
|
250 | |
|
|
251 | Like C<Coro::async>, but runs the given BLOCK in an eval and only logs the |
|
|
252 | error instead of exiting the server in case of a problem. |
|
|
253 | |
|
|
254 | =cut |
|
|
255 | |
|
|
256 | sub async(&) { |
|
|
257 | my ($cb) = @_; |
|
|
258 | |
|
|
259 | Coro::async { |
|
|
260 | eval { $cb->() }; |
|
|
261 | warn $@ if $@; |
|
|
262 | } |
|
|
263 | } |
|
|
264 | |
250 | =item cf::sync_job { BLOCK } |
265 | =item cf::sync_job { BLOCK } |
251 | |
266 | |
252 | The design of crossfire+ requires that the main coro ($Coro::main) is |
267 | 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 |
268 | 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. |
269 | reentrant. Thus "blocking" it by e.g. waiting for I/O is not acceptable. |
… | |
… | |
301 | =cut |
316 | =cut |
302 | |
317 | |
303 | sub coro(&) { |
318 | sub coro(&) { |
304 | my $cb = shift; |
319 | my $cb = shift; |
305 | |
320 | |
306 | my $coro; $coro = async { |
321 | my $coro = &cf::async ($cb); |
307 | eval { |
|
|
308 | $cb->(); |
|
|
309 | }; |
|
|
310 | warn $@ if $@; |
|
|
311 | }; |
|
|
312 | |
322 | |
313 | $coro->on_destroy (sub { |
323 | $coro->on_destroy (sub { |
314 | delete $EXT_CORO{$coro+0}; |
324 | delete $EXT_CORO{$coro+0}; |
315 | }); |
325 | }); |
316 | $EXT_CORO{$coro+0} = $coro; |
326 | $EXT_CORO{$coro+0} = $coro; |
… | |
… | |
1175 | $map->{load_path} = $path; |
1185 | $map->{load_path} = $path; |
1176 | |
1186 | |
1177 | $map |
1187 | $map |
1178 | } |
1188 | } |
1179 | |
1189 | |
|
|
1190 | sub find_map; |
1180 | sub find_map { |
1191 | sub find_map { |
1181 | my ($path, $origin) = @_; |
1192 | my ($path, $origin) = @_; |
1182 | |
1193 | |
1183 | #warn "find_map<$path,$origin>\n";#d# |
1194 | #warn "find_map<$path,$origin>\n";#d# |
1184 | |
1195 | |
… | |
… | |
1192 | |
1203 | |
1193 | # do it the slow way |
1204 | # do it the slow way |
1194 | my $map = try_load_header $path->save_path; |
1205 | my $map = try_load_header $path->save_path; |
1195 | |
1206 | |
1196 | if ($map) { |
1207 | if ($map) { |
|
|
1208 | $map->last_access ((delete $map->{last_access}) |
|
|
1209 | || $cf::RUNTIME); #d# |
1197 | # safety |
1210 | # safety |
1198 | $map->{instantiate_time} = $cf::RUNTIME |
1211 | $map->{instantiate_time} = $cf::RUNTIME |
1199 | if $map->{instantiate_time} > $cf::RUNTIME; |
1212 | if $map->{instantiate_time} > $cf::RUNTIME; |
1200 | } else { |
1213 | } else { |
1201 | if (my $rmp = $path->random_map_params) { |
1214 | if (my $rmp = $path->random_map_params) { |
… | |
… | |
1206 | |
1219 | |
1207 | $map or return; |
1220 | $map or return; |
1208 | |
1221 | |
1209 | $map->{load_original} = 1; |
1222 | $map->{load_original} = 1; |
1210 | $map->{instantiate_time} = $cf::RUNTIME; |
1223 | $map->{instantiate_time} = $cf::RUNTIME; |
|
|
1224 | $map->last_access ($cf::RUNTIME); |
1211 | $map->instantiate; |
1225 | $map->instantiate; |
1212 | |
1226 | |
1213 | # per-player maps become, after loading, normal maps |
1227 | # per-player maps become, after loading, normal maps |
1214 | $map->per_player (0) if $path->{user_rel}; |
1228 | $map->per_player (0) if $path->{user_rel}; |
1215 | } |
1229 | } |
1216 | |
1230 | |
1217 | $map->path ($key); |
1231 | $map->path ($key); |
1218 | $map->{path} = $path; |
1232 | $map->{path} = $path; |
1219 | $map->{last_save} = $cf::RUNTIME; |
1233 | $map->{last_save} = $cf::RUNTIME; |
1220 | $map->last_access ($cf::RUNTIME); |
|
|
1221 | |
1234 | |
1222 | if ($map->should_reset) { |
1235 | if ($map->should_reset) { |
1223 | $map->reset; |
1236 | $map->reset; |
|
|
1237 | undef $guard; |
1224 | $map = find_map $path; |
1238 | $map = find_map $path |
|
|
1239 | or return; |
1225 | } |
1240 | } |
1226 | |
1241 | |
1227 | $cf::MAP{$key} = $map |
1242 | $cf::MAP{$key} = $map |
1228 | } |
1243 | } |
1229 | } |
1244 | } |
… | |
… | |
1297 | |
1312 | |
1298 | $self->{load_path} = $save; |
1313 | $self->{load_path} = $save; |
1299 | |
1314 | |
1300 | return if $self->{deny_save}; |
1315 | return if $self->{deny_save}; |
1301 | |
1316 | |
|
|
1317 | local $self->{last_access} = $self->last_access;#d# |
|
|
1318 | |
1302 | if ($uniq) { |
1319 | if ($uniq) { |
1303 | $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS); |
1320 | $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS); |
1304 | $self->save_objects ($uniq, cf::IO_UNIQUES); |
1321 | $self->save_objects ($uniq, cf::IO_UNIQUES); |
1305 | } else { |
1322 | } else { |
1306 | $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES); |
1323 | $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES); |
… | |
… | |
1308 | } |
1325 | } |
1309 | |
1326 | |
1310 | sub swap_out { |
1327 | sub swap_out { |
1311 | my ($self) = @_; |
1328 | my ($self) = @_; |
1312 | |
1329 | |
|
|
1330 | # save first because save cedes |
|
|
1331 | $self->save; |
|
|
1332 | |
1313 | return if $self->players; |
1333 | return if $self->players; |
1314 | return if $self->in_memory != cf::MAP_IN_MEMORY; |
1334 | return if $self->in_memory != cf::MAP_IN_MEMORY; |
1315 | return if $self->{deny_save}; |
1335 | return if $self->{deny_save}; |
1316 | |
1336 | |
1317 | $self->save; |
|
|
1318 | $self->clear; |
1337 | $self->clear; |
1319 | $self->in_memory (cf::MAP_SWAPPED); |
1338 | $self->in_memory (cf::MAP_SWAPPED); |
1320 | } |
1339 | } |
1321 | |
1340 | |
1322 | sub reset_at { |
1341 | sub reset_at { |
… | |
… | |
1535 | |
1554 | |
1536 | # try to abort aborted map switching on player login :) |
1555 | # try to abort aborted map switching on player login :) |
1537 | # should happen only on crashes |
1556 | # should happen only on crashes |
1538 | if ($pl->ob->{_link_pos}) { |
1557 | if ($pl->ob->{_link_pos}) { |
1539 | $pl->ob->enter_link; |
1558 | $pl->ob->enter_link; |
1540 | Coro::async { |
1559 | cf::async { |
1541 | # we need this sleep as the login has a concurrent enter_exit running |
1560 | # 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 |
1561 | # and this sleep increases chances of the player not ending up in scorn |
1543 | Coro::Timer::sleep 1; |
1562 | Coro::Timer::sleep 1; |
1544 | $pl->ob->leave_link; |
1563 | $pl->ob->leave_link; |
1545 | }; |
1564 | }; |
… | |
… | |
1554 | sub cf::object::player::goto_map { |
1573 | sub cf::object::player::goto_map { |
1555 | my ($self, $path, $x, $y) = @_; |
1574 | my ($self, $path, $x, $y) = @_; |
1556 | |
1575 | |
1557 | $self->enter_link; |
1576 | $self->enter_link; |
1558 | |
1577 | |
1559 | (Coro::async { |
1578 | (cf::async { |
1560 | $path = new cf::path $path; |
1579 | $path = new cf::path $path; |
1561 | |
1580 | |
1562 | my $map = cf::map::find_map $path->as_string; |
1581 | my $map = cf::map::find_map $path->as_string; |
1563 | $map = $map->customise_for ($self) if $map; |
1582 | $map = $map->customise_for ($self) if $map; |
1564 | |
1583 | |
… | |
… | |
1626 | |
1645 | |
1627 | return unless $self->type == cf::PLAYER; |
1646 | return unless $self->type == cf::PLAYER; |
1628 | |
1647 | |
1629 | $self->enter_link; |
1648 | $self->enter_link; |
1630 | |
1649 | |
1631 | (Coro::async { |
1650 | (cf::async { |
1632 | unless (eval { |
1651 | unless (eval { |
1633 | |
|
|
1634 | prepare_random_map $exit |
1652 | prepare_random_map $exit |
1635 | if $exit->slaying eq "/!"; |
1653 | if $exit->slaying eq "/!"; |
1636 | |
1654 | |
1637 | my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path; |
1655 | my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path; |
1638 | $self->goto_map ($path, $exit->stats->hp, $exit->stats->sp); |
1656 | $self->goto_map ($path, $exit->stats->hp, $exit->stats->sp); |
… | |
… | |
1700 | on_reply => sub { |
1718 | on_reply => sub { |
1701 | my ($ns, $msg) = @_; |
1719 | my ($ns, $msg) = @_; |
1702 | |
1720 | |
1703 | # this weird shuffling is so that direct followup queries |
1721 | # this weird shuffling is so that direct followup queries |
1704 | # get handled first |
1722 | # get handled first |
1705 | my $queue = delete $ns->{query_queue}; |
1723 | my $queue = delete $ns->{query_queue} |
|
|
1724 | or return; # be conservative, not sure how that can happen, but we saw a crash here |
1706 | |
1725 | |
1707 | (shift @$queue)->[1]->($msg); |
1726 | (shift @$queue)->[1]->($msg); |
1708 | |
1727 | |
1709 | push @{ $ns->{query_queue} }, @$queue; |
1728 | push @{ $ns->{query_queue} }, @$queue; |
1710 | |
1729 | |
… | |
… | |
1727 | =cut |
1746 | =cut |
1728 | |
1747 | |
1729 | sub cf::client::coro { |
1748 | sub cf::client::coro { |
1730 | my ($self, $cb) = @_; |
1749 | my ($self, $cb) = @_; |
1731 | |
1750 | |
1732 | my $coro; $coro = async { |
1751 | my $coro = &cf::async ($cb); |
1733 | eval { |
|
|
1734 | $cb->(); |
|
|
1735 | }; |
|
|
1736 | warn $@ if $@; |
|
|
1737 | }; |
|
|
1738 | |
1752 | |
1739 | $coro->on_destroy (sub { |
1753 | $coro->on_destroy (sub { |
1740 | delete $self->{_coro}{$coro+0}; |
1754 | delete $self->{_coro}{$coro+0}; |
1741 | }); |
1755 | }); |
1742 | |
1756 | |
… | |
… | |
1977 | open my $fh, "<:utf8", cf::confdir . "/config" |
1991 | open my $fh, "<:utf8", cf::confdir . "/config" |
1978 | or return; |
1992 | or return; |
1979 | |
1993 | |
1980 | local $/; |
1994 | local $/; |
1981 | *CFG = YAML::Syck::Load <$fh>; |
1995 | *CFG = YAML::Syck::Load <$fh>; |
|
|
1996 | |
|
|
1997 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37]; |
|
|
1998 | |
|
|
1999 | if (exists $CFG{mlockall}) { |
|
|
2000 | eval { |
|
|
2001 | $CFG{mlockall} ? &mlockall : &munlockall |
|
|
2002 | and die "WARNING: m(un)lockall failed: $!\n"; |
|
|
2003 | }; |
|
|
2004 | warn $@ if $@; |
|
|
2005 | } |
1982 | } |
2006 | } |
1983 | |
2007 | |
1984 | sub main { |
2008 | sub main { |
1985 | # we must not ever block the main coroutine |
2009 | # we must not ever block the main coroutine |
1986 | local $Coro::idle = sub { |
2010 | local $Coro::idle = sub { |
… | |
… | |
2189 | or warn "ERROR: unable to write runtime file: $!"; |
2213 | or warn "ERROR: unable to write runtime file: $!"; |
2190 | })->(); |
2214 | })->(); |
2191 | }, |
2215 | }, |
2192 | ); |
2216 | ); |
2193 | |
2217 | |
|
|
2218 | END { cf::emergency_save } |
|
|
2219 | |
2194 | 1 |
2220 | 1 |
2195 | |
2221 | |