… | |
… | |
179 | sub to_json($) { |
179 | sub to_json($) { |
180 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
180 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
181 | JSON::Syck::Dump $_[0] |
181 | JSON::Syck::Dump $_[0] |
182 | } |
182 | } |
183 | |
183 | |
184 | =item my $guard = cf::guard { BLOCK } |
|
|
185 | |
|
|
186 | Run the given callback when the guard object gets destroyed (useful for |
|
|
187 | coroutine cancellations). |
|
|
188 | |
|
|
189 | You can call C<< ->cancel >> on the guard object to stop the block from |
|
|
190 | being executed. |
|
|
191 | |
|
|
192 | =cut |
|
|
193 | |
|
|
194 | sub guard(&) { |
|
|
195 | bless \(my $cb = $_[0]), cf::guard::; |
|
|
196 | } |
|
|
197 | |
|
|
198 | sub cf::guard::cancel { |
|
|
199 | ${$_[0]} = sub { }; |
|
|
200 | } |
|
|
201 | |
|
|
202 | sub cf::guard::DESTROY { |
|
|
203 | ${$_[0]}->(); |
|
|
204 | } |
|
|
205 | |
|
|
206 | =item cf::lock_wait $string |
184 | =item cf::lock_wait $string |
207 | |
185 | |
208 | Wait until the given lock is available. See cf::lock_acquire. |
186 | Wait until the given lock is available. See cf::lock_acquire. |
209 | |
187 | |
210 | =item my $lock = cf::lock_acquire $string |
188 | =item my $lock = cf::lock_acquire $string |
211 | |
189 | |
212 | Wait until the given lock is available and then acquires it and returns |
190 | Wait until the given lock is available and then acquires it and returns |
213 | a guard object. If the guard object gets destroyed (goes out of scope, |
191 | a Coro::guard object. If the guard object gets destroyed (goes out of scope, |
214 | for example when the coroutine gets canceled), the lock is automatically |
192 | for example when the coroutine gets canceled), the lock is automatically |
215 | returned. |
193 | returned. |
216 | |
194 | |
217 | Lock names should begin with a unique identifier (for example, cf::map::find |
195 | Lock names should begin with a unique identifier (for example, cf::map::find |
218 | uses map_find and cf::map::load uses map_load). |
196 | uses map_find and cf::map::load uses map_load). |
… | |
… | |
237 | # wait, to be sure we are not locked |
215 | # wait, to be sure we are not locked |
238 | lock_wait $key; |
216 | lock_wait $key; |
239 | |
217 | |
240 | $LOCK{$key} = []; |
218 | $LOCK{$key} = []; |
241 | |
219 | |
242 | cf::guard { |
220 | Coro::guard { |
243 | # wake up all waiters, to be on the safe side |
221 | # wake up all waiters, to be on the safe side |
244 | $_->ready for @{ delete $LOCK{$key} }; |
222 | $_->ready for @{ delete $LOCK{$key} }; |
245 | } |
223 | } |
246 | } |
224 | } |
247 | |
225 | |
248 | =item cf::async { BLOCK } |
|
|
249 | |
|
|
250 | Like C<Coro::async>, but runs the given BLOCK in an eval and only logs the |
|
|
251 | error instead of exiting the server in case of a problem. |
|
|
252 | |
|
|
253 | =cut |
|
|
254 | |
|
|
255 | sub async(&) { |
|
|
256 | my ($cb) = @_; |
|
|
257 | |
|
|
258 | Coro::async { |
|
|
259 | eval { $cb->() }; |
|
|
260 | warn $@ if $@; |
|
|
261 | } |
|
|
262 | } |
|
|
263 | |
|
|
264 | sub freeze_mainloop { |
226 | sub freeze_mainloop { |
265 | return unless $TICK_WATCHER->is_active; |
227 | return unless $TICK_WATCHER->is_active; |
266 | |
228 | |
267 | my $guard = guard { $TICK_WATCHER->start }; |
229 | my $guard = Coro::guard { $TICK_WATCHER->start }; |
268 | $TICK_WATCHER->stop; |
230 | $TICK_WATCHER->stop; |
269 | $guard |
231 | $guard |
270 | } |
232 | } |
271 | |
233 | |
272 | =item cf::sync_job { BLOCK } |
234 | =item cf::sync_job { BLOCK } |
… | |
… | |
294 | my $freeze_guard = freeze_mainloop; |
256 | my $freeze_guard = freeze_mainloop; |
295 | |
257 | |
296 | my $busy = 1; |
258 | my $busy = 1; |
297 | my @res; |
259 | my @res; |
298 | |
260 | |
299 | (Coro::async { |
261 | (Coro::async_pool { |
300 | @res = eval { $job->() }; |
262 | @res = eval { $job->() }; |
301 | warn $@ if $@; |
263 | warn $@ if $@; |
302 | undef $busy; |
264 | undef $busy; |
303 | })->prio (Coro::PRIO_MAX); |
265 | })->prio (Coro::PRIO_MAX); |
304 | |
266 | |
… | |
… | |
315 | } |
277 | } |
316 | } |
278 | } |
317 | |
279 | |
318 | =item $coro = cf::coro { BLOCK } |
280 | =item $coro = cf::coro { BLOCK } |
319 | |
281 | |
320 | Creates and returns a new coro. This coro is automcatially being canceled |
282 | Creates (and readies) and returns a new coro. This coro is automcatially |
321 | when the extension calling this is being unloaded. |
283 | being canceled when the extension calling this is being unloaded. |
322 | |
284 | |
323 | =cut |
285 | =cut |
324 | |
286 | |
325 | sub coro(&) { |
287 | sub coro(&) { |
326 | my $cb = shift; |
288 | my $cb = shift; |
327 | |
289 | |
328 | my $coro = &cf::async ($cb); |
290 | my $coro = &Coro::async_pool ($cb); |
329 | |
291 | |
330 | $coro->on_destroy (sub { |
292 | $coro->on_destroy (sub { |
331 | delete $EXT_CORO{$coro+0}; |
293 | delete $EXT_CORO{$coro+0}; |
332 | }); |
294 | }); |
333 | $EXT_CORO{$coro+0} = $coro; |
295 | $EXT_CORO{$coro+0} = $coro; |
… | |
… | |
1184 | or return; |
1146 | or return; |
1185 | |
1147 | |
1186 | my $map = cf::map::new |
1148 | my $map = cf::map::new |
1187 | or return; |
1149 | or return; |
1188 | |
1150 | |
|
|
1151 | # for better error messages only, will be overwritten |
|
|
1152 | $map->path ($path); |
|
|
1153 | |
1189 | $map->load_header ($path) |
1154 | $map->load_header ($path) |
1190 | or return; |
1155 | or return; |
1191 | |
1156 | |
1192 | $map->{load_path} = $path; |
1157 | $map->{load_path} = $path; |
1193 | |
1158 | |
… | |
… | |
1208 | $cf::MAP{$key} || do { |
1173 | $cf::MAP{$key} || do { |
1209 | my $guard = cf::lock_acquire "map_find:$key"; |
1174 | my $guard = cf::lock_acquire "map_find:$key"; |
1210 | |
1175 | |
1211 | # do it the slow way |
1176 | # do it the slow way |
1212 | my $map = try_load_header $path->save_path; |
1177 | my $map = try_load_header $path->save_path; |
|
|
1178 | |
|
|
1179 | Coro::cede; |
1213 | |
1180 | |
1214 | if ($map) { |
1181 | if ($map) { |
1215 | $map->last_access ((delete $map->{last_access}) |
1182 | $map->last_access ((delete $map->{last_access}) |
1216 | || $cf::RUNTIME); #d# |
1183 | || $cf::RUNTIME); #d# |
1217 | # safety |
1184 | # safety |
… | |
… | |
1237 | |
1204 | |
1238 | $map->path ($key); |
1205 | $map->path ($key); |
1239 | $map->{path} = $path; |
1206 | $map->{path} = $path; |
1240 | $map->{last_save} = $cf::RUNTIME; |
1207 | $map->{last_save} = $cf::RUNTIME; |
1241 | |
1208 | |
|
|
1209 | Coro::cede; |
|
|
1210 | |
1242 | if ($map->should_reset) { |
1211 | if ($map->should_reset) { |
1243 | $map->reset; |
1212 | $map->reset; |
1244 | undef $guard; |
1213 | undef $guard; |
1245 | $map = find $path |
1214 | $map = find $path |
1246 | or return; |
1215 | or return; |
… | |
… | |
1272 | if (aio_open $uniq, O_RDONLY, 0) { |
1241 | if (aio_open $uniq, O_RDONLY, 0) { |
1273 | $self->clear_unique_items; |
1242 | $self->clear_unique_items; |
1274 | $self->load_objects ($uniq, 0); |
1243 | $self->load_objects ($uniq, 0); |
1275 | } |
1244 | } |
1276 | } |
1245 | } |
|
|
1246 | |
|
|
1247 | Coro::cede; |
1277 | |
1248 | |
1278 | # now do the right thing for maps |
1249 | # now do the right thing for maps |
1279 | $self->link_multipart_objects; |
1250 | $self->link_multipart_objects; |
1280 | |
1251 | |
1281 | if ($self->{path}->is_style_map) { |
1252 | if ($self->{path}->is_style_map) { |
… | |
… | |
1289 | $self->difficulty ($self->estimate_difficulty) |
1260 | $self->difficulty ($self->estimate_difficulty) |
1290 | unless $self->difficulty; |
1261 | unless $self->difficulty; |
1291 | $self->activate; |
1262 | $self->activate; |
1292 | } |
1263 | } |
1293 | |
1264 | |
|
|
1265 | Coro::cede; |
|
|
1266 | |
1294 | $self->in_memory (cf::MAP_IN_MEMORY); |
1267 | $self->in_memory (cf::MAP_IN_MEMORY); |
1295 | } |
1268 | } |
1296 | |
1269 | |
1297 | sub find_sync { |
1270 | sub find_sync { |
1298 | my ($path, $origin) = @_; |
1271 | my ($path, $origin) = @_; |
… | |
… | |
1306 | cf::sync_job { $map->load }; |
1279 | cf::sync_job { $map->load }; |
1307 | } |
1280 | } |
1308 | |
1281 | |
1309 | sub save { |
1282 | sub save { |
1310 | my ($self) = @_; |
1283 | my ($self) = @_; |
|
|
1284 | |
|
|
1285 | my $lock = cf::lock_acquire "map_data:" . $self->path; |
1311 | |
1286 | |
1312 | $self->{last_save} = $cf::RUNTIME; |
1287 | $self->{last_save} = $cf::RUNTIME; |
1313 | |
1288 | |
1314 | return unless $self->dirty; |
1289 | return unless $self->dirty; |
1315 | |
1290 | |
… | |
… | |
1334 | my ($self) = @_; |
1309 | my ($self) = @_; |
1335 | |
1310 | |
1336 | # save first because save cedes |
1311 | # save first because save cedes |
1337 | $self->save; |
1312 | $self->save; |
1338 | |
1313 | |
|
|
1314 | my $lock = cf::lock_acquire "map_data:" . $self->path; |
|
|
1315 | |
1339 | return if $self->players; |
1316 | return if $self->players; |
1340 | return if $self->in_memory != cf::MAP_IN_MEMORY; |
1317 | return if $self->in_memory != cf::MAP_IN_MEMORY; |
1341 | return if $self->{deny_save}; |
1318 | return if $self->{deny_save}; |
1342 | |
1319 | |
1343 | $self->clear; |
1320 | $self->clear; |
… | |
… | |
1384 | $self->save; |
1361 | $self->save; |
1385 | } |
1362 | } |
1386 | |
1363 | |
1387 | sub reset { |
1364 | sub reset { |
1388 | my ($self) = @_; |
1365 | my ($self) = @_; |
|
|
1366 | |
|
|
1367 | my $lock = cf::lock_acquire "map_data:" . $self->path; |
1389 | |
1368 | |
1390 | return if $self->players; |
1369 | return if $self->players; |
1391 | return if $self->{path}{user_rel};#d# |
1370 | return if $self->{path}{user_rel};#d# |
1392 | |
1371 | |
1393 | warn "resetting map ", $self->path;#d# |
1372 | warn "resetting map ", $self->path;#d# |
… | |
… | |
1560 | |
1539 | |
1561 | # try to abort aborted map switching on player login :) |
1540 | # try to abort aborted map switching on player login :) |
1562 | # should happen only on crashes |
1541 | # should happen only on crashes |
1563 | if ($pl->ob->{_link_pos}) { |
1542 | if ($pl->ob->{_link_pos}) { |
1564 | $pl->ob->enter_link; |
1543 | $pl->ob->enter_link; |
1565 | cf::async { |
1544 | Coro::async_pool { |
1566 | # we need this sleep as the login has a concurrent enter_exit running |
1545 | # we need this sleep as the login has a concurrent enter_exit running |
1567 | # and this sleep increases chances of the player not ending up in scorn |
1546 | # and this sleep increases chances of the player not ending up in scorn |
1568 | Coro::Timer::sleep 1; |
1547 | Coro::Timer::sleep 1; |
1569 | $pl->ob->leave_link; |
1548 | $pl->ob->leave_link; |
1570 | }; |
1549 | }; |
1571 | } |
1550 | } |
1572 | }, |
1551 | }, |
1573 | ); |
1552 | ); |
1574 | |
1553 | |
1575 | =item $player_object->goto_map ($path, $x, $y) |
1554 | =item $player_object->goto ($path, $x, $y) |
1576 | |
1555 | |
1577 | =cut |
1556 | =cut |
1578 | |
1557 | |
1579 | sub cf::object::player::goto_map { |
1558 | sub cf::object::player::goto { |
1580 | my ($self, $path, $x, $y) = @_; |
1559 | my ($self, $path, $x, $y) = @_; |
1581 | |
1560 | |
1582 | $self->enter_link; |
1561 | $self->enter_link; |
1583 | |
1562 | |
1584 | (cf::async { |
1563 | (Coro::async_pool { |
1585 | $path = new cf::path $path; |
1564 | $path = new cf::path $path; |
1586 | |
1565 | |
1587 | my $map = cf::map::find $path->as_string; |
1566 | my $map = cf::map::find $path->as_string; |
1588 | $map = $map->customise_for ($self) if $map; |
1567 | $map = $map->customise_for ($self) if $map; |
1589 | |
1568 | |
… | |
… | |
1651 | |
1630 | |
1652 | return unless $self->type == cf::PLAYER; |
1631 | return unless $self->type == cf::PLAYER; |
1653 | |
1632 | |
1654 | $self->enter_link; |
1633 | $self->enter_link; |
1655 | |
1634 | |
1656 | (cf::async { |
1635 | (Coro::async_pool { |
1657 | $self->deactivate_recursive; # just to be sure |
1636 | $self->deactivate_recursive; # just to be sure |
1658 | unless (eval { |
1637 | unless (eval { |
1659 | prepare_random_map $exit |
1638 | prepare_random_map $exit |
1660 | if $exit->slaying eq "/!"; |
1639 | if $exit->slaying eq "/!"; |
1661 | |
1640 | |
1662 | my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path; |
1641 | my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path; |
1663 | $self->goto_map ($path, $exit->stats->hp, $exit->stats->sp); |
1642 | $self->goto ($path, $exit->stats->hp, $exit->stats->sp); |
1664 | |
1643 | |
1665 | 1; |
1644 | 1; |
1666 | }) { |
1645 | }) { |
1667 | $self->message ("Something went wrong deep within the crossfire server. " |
1646 | $self->message ("Something went wrong deep within the crossfire server. " |
1668 | . "I'll try to bring you back to the map you were before. " |
1647 | . "I'll try to bring you back to the map you were before. " |
… | |
… | |
1753 | =cut |
1732 | =cut |
1754 | |
1733 | |
1755 | sub cf::client::coro { |
1734 | sub cf::client::coro { |
1756 | my ($self, $cb) = @_; |
1735 | my ($self, $cb) = @_; |
1757 | |
1736 | |
1758 | my $coro = &cf::async ($cb); |
1737 | my $coro = &Coro::async_pool ($cb); |
1759 | |
1738 | |
1760 | $coro->on_destroy (sub { |
1739 | $coro->on_destroy (sub { |
1761 | delete $self->{_coro}{$coro+0}; |
1740 | delete $self->{_coro}{$coro+0}; |
1762 | }); |
1741 | }); |
1763 | |
1742 | |