ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf.pm
(Generate patch)

Comparing deliantra/server/lib/cf.pm (file contents):
Revision 1.113 by root, Mon Jan 1 15:32:40 2007 UTC vs.
Revision 1.131 by root, Thu Jan 4 00:53:54 2007 UTC

8use Storable; 8use Storable;
9use Opcode; 9use Opcode;
10use Safe; 10use Safe;
11use Safe::Hole; 11use Safe::Hole;
12 12
13use Coro 3.3; 13use Coro 3.3 ();
14use Coro::Event; 14use Coro::Event;
15use Coro::Timer; 15use Coro::Timer;
16use Coro::Signal; 16use Coro::Signal;
17use Coro::Semaphore; 17use Coro::Semaphore;
18use Coro::AIO; 18use Coro::AIO;
71mkdir cf::localdir . "/" . cf::uniquedir; 71mkdir cf::localdir . "/" . cf::uniquedir;
72mkdir $RANDOM_MAPS; 72mkdir $RANDOM_MAPS;
73 73
74# a special map that is always available 74# a special map that is always available
75our $LINK_MAP; 75our $LINK_MAP;
76 76our $EMERGENCY_POSITION;
77our $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
181sub to_json($) { 180sub to_json($) {
182 $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs 181 $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs
183 JSON::Syck::Dump $_[0] 182 JSON::Syck::Dump $_[0]
184} 183}
185 184
185=item my $guard = cf::guard { BLOCK }
186
187Run the given callback when the guard object gets destroyed (useful for
188coroutine cancellations).
189
190You can call C<< ->cancel >> on the guard object to stop the block from
191being executed.
192
193=cut
194
195sub guard(&) {
196 bless \(my $cb = $_[0]), cf::guard::;
197}
198
199sub cf::guard::cancel {
200 ${$_[0]} = sub { };
201}
202
203sub cf::guard::DESTROY {
204 ${$_[0]}->();
205}
206
207=item cf::lock_wait $string
208
209Wait until the given lock is available. See cf::lock_acquire.
210
211=item my $lock = cf::lock_acquire $string
212
213Wait until the given lock is available and then acquires it and returns
214a guard object. If the guard object gets destroyed (goes out of scope,
215for example when the coroutine gets canceled), the lock is automatically
216returned.
217
218Lock names should begin with a unique identifier (for example, find_map
219uses map_find and load_map uses map_load).
220
221=cut
222
223our %LOCK;
224
225sub lock_wait($) {
226 my ($key) = @_;
227
228 # wait for lock, if any
229 while ($LOCK{$key}) {
230 push @{ $LOCK{$key} }, $Coro::current;
231 Coro::schedule;
232 }
233}
234
235sub lock_acquire($) {
236 my ($key) = @_;
237
238 # wait, to be sure we are not locked
239 lock_wait $key;
240
241 $LOCK{$key} = [];
242
243 cf::guard {
244 # wake up all waiters, to be on the safe side
245 $_->ready for @{ delete $LOCK{$key} };
246 }
247}
248
249=item cf::async { BLOCK }
250
251Like C<Coro::async>, but runs the given BLOCK in an eval and only logs the
252error instead of exiting the server in case of a problem.
253
254=cut
255
256sub async(&) {
257 my ($cb) = @_;
258
259 Coro::async {
260 eval { $cb->() };
261 warn $@ if $@;
262 }
263}
264
186=item cf::sync_job { BLOCK } 265=item cf::sync_job { BLOCK }
187 266
188The design of crossfire+ requires that the main coro ($Coro::main) is 267The design of crossfire+ requires that the main coro ($Coro::main) is
189always able to handle events or runnable, as crossfire+ is only partly 268always able to handle events or runnable, as crossfire+ is only partly
190reentrant. Thus "blocking" it by e.g. waiting for I/O is not acceptable. 269reentrant. Thus "blocking" it by e.g. waiting for I/O is not acceptable.
237=cut 316=cut
238 317
239sub coro(&) { 318sub coro(&) {
240 my $cb = shift; 319 my $cb = shift;
241 320
242 my $coro; $coro = async { 321 my $coro = &cf::async ($cb);
243 eval {
244 $cb->();
245 };
246 warn $@ if $@;
247 };
248 322
249 $coro->on_destroy (sub { 323 $coro->on_destroy (sub {
250 delete $EXT_CORO{$coro+0}; 324 delete $EXT_CORO{$coro+0};
251 }); 325 });
252 $EXT_CORO{$coro+0} = $coro; 326 $EXT_CORO{$coro+0} = $coro;
289 363
290 $path = $path->as_string if ref $path; 364 $path = $path->as_string if ref $path;
291 365
292 my $self = bless { }, $class; 366 my $self = bless { }, $class;
293 367
368 # {... are special paths that are not touched
369 # ?xxx/... are special absolute paths
370 # ?random/... random maps
371 # /! non-realised random map exit
372 # /... normal maps
373 # ~/... per-player maps without a specific player (DO NOT USE)
374 # ~user/... per-player map of a specific user
375
376 if ($path =~ /^{/) {
377 # fine as it is
294 if ($path =~ s{^\?random/}{}) { 378 } elsif ($path =~ s{^\?random/}{}) {
295 Coro::AIO::aio_load "$cf::RANDOM_MAPS/$path.meta", my $data; 379 Coro::AIO::aio_load "$cf::RANDOM_MAPS/$path.meta", my $data;
296 $self->{random} = cf::from_json $data; 380 $self->{random} = cf::from_json $data;
297 } else { 381 } else {
298 if ($path =~ s{^~([^/]+)?}{}) { 382 if ($path =~ s{^~([^/]+)?}{}) {
299 $self->{user_rel} = 1; 383 $self->{user_rel} = 1;
807 (aio_load "$filename.pst", $av) >= 0 891 (aio_load "$filename.pst", $av) >= 0
808 or return; 892 or return;
809 $av = eval { (Storable::thaw $av)->{objs} }; 893 $av = eval { (Storable::thaw $av)->{objs} };
810 } 894 }
811 895
896 warn sprintf "loading %s (%d)\n",
897 $filename, length $data, scalar @{$av || []};#d#
812 return ($data, $av); 898 return ($data, $av);
813} 899}
814 900
815############################################################################# 901#############################################################################
816# command handling &c 902# command handling &c
1077# and all this just because we cannot iterate over 1163# and all this just because we cannot iterate over
1078# all maps in C++... 1164# all maps in C++...
1079sub change_all_map_light { 1165sub change_all_map_light {
1080 my ($change) = @_; 1166 my ($change) = @_;
1081 1167
1082 $_->change_map_light ($change) for values %cf::MAP; 1168 $_->change_map_light ($change)
1169 for grep $_->outdoor, values %cf::MAP;
1083} 1170}
1084 1171
1085sub try_load_header($) { 1172sub try_load_header($) {
1086 my ($path) = @_; 1173 my ($path) = @_;
1087 1174
1098 $map->{load_path} = $path; 1185 $map->{load_path} = $path;
1099 1186
1100 $map 1187 $map
1101} 1188}
1102 1189
1190sub find_map;
1103sub find_map { 1191sub find_map {
1104 my ($path, $origin) = @_; 1192 my ($path, $origin) = @_;
1105 1193
1106 #warn "find_map<$path,$origin>\n";#d# 1194 #warn "find_map<$path,$origin>\n";#d#
1107 1195
1108 $path = new cf::path $path, $origin && $origin->path; 1196 $path = new cf::path $path, $origin && $origin->path;
1109 my $key = $path->as_string; 1197 my $key = $path->as_string;
1110 1198
1199 cf::lock_wait "map_find:$key";
1200
1111 $cf::MAP{$key} || do { 1201 $cf::MAP{$key} || do {
1202 my $guard = cf::lock_acquire "map_find:$key";
1203
1112 # do it the slow way 1204 # do it the slow way
1113 my $map = try_load_header $path->save_path; 1205 my $map = try_load_header $path->save_path;
1114 1206
1115 if ($map) { 1207 if ($map) {
1116 # safety 1208 # safety
1133 $map->per_player (0) if $path->{user_rel}; 1225 $map->per_player (0) if $path->{user_rel};
1134 } 1226 }
1135 1227
1136 $map->path ($key); 1228 $map->path ($key);
1137 $map->{path} = $path; 1229 $map->{path} = $path;
1230 $map->{last_save} = $cf::RUNTIME;
1138 $map->last_access ($cf::RUNTIME); 1231 $map->last_access ($cf::RUNTIME);
1139 1232
1140 if ($map->should_reset) { 1233 if ($map->should_reset) {
1141 $map->reset; 1234 $map->reset;
1235 undef $guard;
1142 $map = find_map $path; 1236 $map = find_map $path
1237 or return;
1143 } 1238 }
1144 1239
1145 $cf::MAP{$key} = $map 1240 $cf::MAP{$key} = $map
1146 } 1241 }
1147} 1242}
1148 1243
1149sub load { 1244sub load {
1150 my ($self) = @_; 1245 my ($self) = @_;
1151 1246
1247 my $path = $self->{path};
1248 my $guard = cf::lock_acquire "map_load:" . $path->as_string;
1249
1152 return if $self->in_memory != cf::MAP_SWAPPED; 1250 return if $self->in_memory != cf::MAP_SWAPPED;
1153 1251
1154 $self->in_memory (cf::MAP_LOADING); 1252 $self->in_memory (cf::MAP_LOADING);
1155
1156 my $path = $self->{path};
1157 1253
1158 $self->alloc; 1254 $self->alloc;
1159 $self->load_objects ($self->{load_path}, 1) 1255 $self->load_objects ($self->{load_path}, 1)
1160 or return; 1256 or return;
1161 1257
1203} 1299}
1204 1300
1205sub save { 1301sub save {
1206 my ($self) = @_; 1302 my ($self) = @_;
1207 1303
1304 $self->{last_save} = $cf::RUNTIME;
1305
1306 return unless $self->dirty;
1307
1208 my $save = $self->{path}->save_path; utf8::encode $save; 1308 my $save = $self->{path}->save_path; utf8::encode $save;
1209 my $uniq = $self->{path}->uniq_path; utf8::encode $uniq; 1309 my $uniq = $self->{path}->uniq_path; utf8::encode $uniq;
1210
1211 $self->{last_save} = $cf::RUNTIME;
1212
1213 return unless $self->dirty;
1214 1310
1215 $self->{load_path} = $save; 1311 $self->{load_path} = $save;
1216 1312
1217 return if $self->{deny_save}; 1313 return if $self->{deny_save};
1218 1314
1225} 1321}
1226 1322
1227sub swap_out { 1323sub swap_out {
1228 my ($self) = @_; 1324 my ($self) = @_;
1229 1325
1326 # save first because save cedes
1327 $self->save;
1328
1230 return if $self->players; 1329 return if $self->players;
1231 return if $self->in_memory != cf::MAP_IN_MEMORY; 1330 return if $self->in_memory != cf::MAP_IN_MEMORY;
1232 return if $self->{deny_save}; 1331 return if $self->{deny_save};
1233 1332
1234 $self->save;
1235 $self->clear; 1333 $self->clear;
1236 $self->in_memory (cf::MAP_SWAPPED); 1334 $self->in_memory (cf::MAP_SWAPPED);
1237} 1335}
1238 1336
1239sub reset_at { 1337sub reset_at {
1240 my ($self) = @_; 1338 my ($self) = @_;
1241 1339
1242 # TODO: safety, remove and allow resettable per-player maps 1340 # TODO: safety, remove and allow resettable per-player maps
1243 return 1e100 if $self->{path}{user_rel}; 1341 return 1e99 if $self->{path}{user_rel};
1244 return 1e100 if $self->{deny_reset}; 1342 return 1e99 if $self->{deny_reset};
1245 1343
1246 my $time = $self->fixed_resettime ? $self->{instantiate_time} : $self->last_access; 1344 my $time = $self->fixed_resettime ? $self->{instantiate_time} : $self->last_access;
1247 my $to = $self->reset_timeout || $DEFAULT_RESET; 1345 my $to = List::Util::min $MAX_RESET, $self->reset_timeout || $DEFAULT_RESET;
1248 $to = $MAX_RESET if $to > $MAX_RESET;
1249 1346
1250 $time + $to 1347 $time + $to
1251} 1348}
1252 1349
1253sub should_reset { 1350sub should_reset {
1269 1366
1270 $self->unlink_save; 1367 $self->unlink_save;
1271 1368
1272 delete $cf::MAP{$self->path}; 1369 delete $cf::MAP{$self->path};
1273 $self->{path} = new cf::path $new_path; 1370 $self->{path} = new cf::path $new_path;
1274 $self->path ($self->{path}->path); 1371 $self->path ($self->{path}->as_string);
1275 $cf::MAP{$self->path} = $self; 1372 $cf::MAP{$self->path} = $self;
1276 1373
1277 $self->save; 1374 $self->save;
1278} 1375}
1279 1376
1289 1386
1290 $_->clear_links_to ($self) for values %cf::MAP; 1387 $_->clear_links_to ($self) for values %cf::MAP;
1291 1388
1292 $self->unlink_save; 1389 $self->unlink_save;
1293 $self->destroy; 1390 $self->destroy;
1391}
1392
1393my $nuke_counter = "aaaa";
1394
1395sub nuke {
1396 my ($self) = @_;
1397
1398 $self->{deny_save} = 1;
1399 $self->reset_timeout (1);
1400 $self->rename ("{nuke}/" . ($nuke_counter++));
1401 $self->reset; # polite request, might not happen
1294} 1402}
1295 1403
1296sub customise_for { 1404sub customise_for {
1297 my ($map, $ob) = @_; 1405 my ($map, $ob) = @_;
1298 1406
1363 (ref $cf::CFG{"may_$access"} 1471 (ref $cf::CFG{"may_$access"}
1364 ? scalar grep $self->name eq $_, @{$cf::CFG{"may_$access"}} 1472 ? scalar grep $self->name eq $_, @{$cf::CFG{"may_$access"}}
1365 : $cf::CFG{"may_$access"}) 1473 : $cf::CFG{"may_$access"})
1366} 1474}
1367 1475
1476=item $player_object->enter_link
1477
1478Freezes the player and moves him/her to a special map (C<{link}>).
1479
1480The player should be reaosnably safe there for short amounts of time. You
1481I<MUST> call C<leave_link> as soon as possible, though.
1482
1483=item $player_object->leave_link ($map, $x, $y)
1484
1485Moves the player out of the specila link map onto the given map. If the
1486map is not valid (or omitted), the player will be moved back to the
1487location he/she was before the call to C<enter_link>, or, if that fails,
1488to the emergency map position.
1489
1490Might block.
1491
1492=cut
1493
1368sub cf::object::player::enter_link { 1494sub cf::object::player::enter_link {
1369 my ($self) = @_; 1495 my ($self) = @_;
1370 1496
1497 $self->deactivate_recursive;
1498
1371 return if $self->map == $LINK_MAP; 1499 return if $self->map == $LINK_MAP;
1372 1500
1373 $self->{_link_pos} = [$self->map->{path}, $self->x, $self->y] 1501 $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y]
1374 if $self->map; 1502 if $self->map;
1375 1503
1376 $self->enter_map ($LINK_MAP, 20, 20); 1504 $self->enter_map ($LINK_MAP, 20, 20);
1377 $self->deactivate_recursive;
1378} 1505}
1379 1506
1380sub cf::object::player::leave_link { 1507sub cf::object::player::leave_link {
1381 my ($self, $map, $x, $y) = @_; 1508 my ($self, $map, $x, $y) = @_;
1382 1509
1383 my $link_pos = delete $self->{_link_pos}; 1510 my $link_pos = delete $self->{_link_pos};
1384 1511
1385 unless ($map) { 1512 unless ($map) {
1386 $self->message ("The exit is closed", cf::NDI_UNIQUE | cf::NDI_RED);
1387
1388 # restore original map position 1513 # restore original map position
1389 ($map, $x, $y) = @{ $link_pos || [] }; 1514 ($map, $x, $y) = @{ $link_pos || [] };
1390 $map = cf::map::find_map $map; 1515 $map = cf::map::find_map $map;
1391 1516
1392 unless ($map) { 1517 unless ($map) {
1407 1532
1408 $self->activate_recursive; 1533 $self->activate_recursive;
1409 $self->enter_map ($map, $x, $y); 1534 $self->enter_map ($map, $x, $y);
1410} 1535}
1411 1536
1537cf::player->attach (
1538 on_logout => sub {
1539 my ($pl) = @_;
1540
1541 # abort map switching before logout
1542 if ($pl->ob->{_link_pos}) {
1543 cf::sync_job {
1544 $pl->ob->leave_link
1545 };
1546 }
1547 },
1548 on_login => sub {
1549 my ($pl) = @_;
1550
1551 # try to abort aborted map switching on player login :)
1552 # should happen only on crashes
1553 if ($pl->ob->{_link_pos}) {
1554 $pl->ob->enter_link;
1555 cf::async {
1556 # we need this sleep as the login has a concurrent enter_exit running
1557 # and this sleep increases chances of the player not ending up in scorn
1558 Coro::Timer::sleep 1;
1559 $pl->ob->leave_link;
1560 };
1561 }
1562 },
1563);
1564
1412=item $player_object->goto_map ($map, $x, $y) 1565=item $player_object->goto_map ($path, $x, $y)
1413 1566
1414=cut 1567=cut
1415 1568
1416sub cf::object::player::goto_map { 1569sub cf::object::player::goto_map {
1417 my ($self, $path, $x, $y) = @_; 1570 my ($self, $path, $x, $y) = @_;
1418 1571
1419 $self->enter_link; 1572 $self->enter_link;
1420 1573
1421 (Coro::async { 1574 (cf::async {
1422 $path = new cf::path $path; 1575 $path = new cf::path $path;
1423 1576
1424 my $map = cf::map::find_map $path->as_string; 1577 my $map = cf::map::find_map $path->as_string;
1425 $map = $map->customise_for ($self) if $map; 1578 $map = $map->customise_for ($self) if $map;
1426 1579
1427 warn "entering ", $map->path, " at ($x, $y)\n" 1580# warn "entering ", $map->path, " at ($x, $y)\n"
1428 if $map; 1581# if $map;
1582
1583 $map or $self->message ("The exit is closed", cf::NDI_UNIQUE | cf::NDI_RED);
1429 1584
1430 $self->leave_link ($map, $x, $y); 1585 $self->leave_link ($map, $x, $y);
1431 })->prio (1); 1586 })->prio (1);
1432} 1587}
1433 1588
1486 1641
1487 return unless $self->type == cf::PLAYER; 1642 return unless $self->type == cf::PLAYER;
1488 1643
1489 $self->enter_link; 1644 $self->enter_link;
1490 1645
1491 (Coro::async { 1646 (cf::async {
1492 unless (eval { 1647 unless (eval {
1493
1494 prepare_random_map $exit 1648 prepare_random_map $exit
1495 if $exit->slaying eq "/!"; 1649 if $exit->slaying eq "/!";
1496 1650
1497 my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path; 1651 my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path;
1498 $self->goto_map ($path, $exit->stats->hp, $exit->stats->sp); 1652 $self->goto_map ($path, $exit->stats->hp, $exit->stats->sp);
1560 on_reply => sub { 1714 on_reply => sub {
1561 my ($ns, $msg) = @_; 1715 my ($ns, $msg) = @_;
1562 1716
1563 # this weird shuffling is so that direct followup queries 1717 # this weird shuffling is so that direct followup queries
1564 # get handled first 1718 # get handled first
1565 my $queue = delete $ns->{query_queue}; 1719 my $queue = delete $ns->{query_queue}
1720 or return; # be conservative, not sure how that can happen, but we saw a crash here
1566 1721
1567 (shift @$queue)->[1]->($msg); 1722 (shift @$queue)->[1]->($msg);
1568 1723
1569 push @{ $ns->{query_queue} }, @$queue; 1724 push @{ $ns->{query_queue} }, @$queue;
1570 1725
1587=cut 1742=cut
1588 1743
1589sub cf::client::coro { 1744sub cf::client::coro {
1590 my ($self, $cb) = @_; 1745 my ($self, $cb) = @_;
1591 1746
1592 my $coro; $coro = async { 1747 my $coro = &cf::async ($cb);
1593 eval {
1594 $cb->();
1595 };
1596 warn $@ if $@;
1597 };
1598 1748
1599 $coro->on_destroy (sub { 1749 $coro->on_destroy (sub {
1600 delete $self->{_coro}{$coro+0}; 1750 delete $self->{_coro}{$coro+0};
1601 }); 1751 });
1602 1752
1774 1924
1775{ 1925{
1776 my $path = cf::localdir . "/database.pst"; 1926 my $path = cf::localdir . "/database.pst";
1777 1927
1778 sub db_load() { 1928 sub db_load() {
1779 warn "loading database $path\n";#d# remove later
1780 $DB = stat $path ? Storable::retrieve $path : { }; 1929 $DB = stat $path ? Storable::retrieve $path : { };
1781 } 1930 }
1782 1931
1783 my $pid; 1932 my $pid;
1784 1933
1785 sub db_save() { 1934 sub db_save() {
1786 warn "saving database $path\n";#d# remove later
1787 waitpid $pid, 0 if $pid; 1935 waitpid $pid, 0 if $pid;
1788 if (0 == ($pid = fork)) { 1936 if (0 == ($pid = fork)) {
1789 $DB->{_meta}{version} = 1; 1937 $DB->{_meta}{version} = 1;
1790 Storable::nstore $DB, "$path~"; 1938 Storable::nstore $DB, "$path~";
1791 rename "$path~", $path; 1939 rename "$path~", $path;
1839 open my $fh, "<:utf8", cf::confdir . "/config" 1987 open my $fh, "<:utf8", cf::confdir . "/config"
1840 or return; 1988 or return;
1841 1989
1842 local $/; 1990 local $/;
1843 *CFG = YAML::Syck::Load <$fh>; 1991 *CFG = YAML::Syck::Load <$fh>;
1992
1993 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37];
1994
1995 if (exists $CFG{mlockall}) {
1996 eval {
1997 $CFG{mlockall} ? &mlockall : &munlockall
1998 and die "WARNING: m(un)lockall failed: $!\n";
1999 };
2000 warn $@ if $@;
2001 }
1844} 2002}
1845 2003
1846sub main { 2004sub main {
1847 # we must not ever block the main coroutine 2005 # we must not ever block the main coroutine
1848 local $Coro::idle = sub { 2006 local $Coro::idle = sub {
1849 Carp::cluck "FATAL: Coro::idle was called, major BUG\n";#d# 2007 Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d#
1850 (Coro::unblock_sub { 2008 (Coro::unblock_sub {
1851 Event::one_event; 2009 Event::one_event;
1852 })->(); 2010 })->();
1853 }; 2011 };
1854 2012
2051 or warn "ERROR: unable to write runtime file: $!"; 2209 or warn "ERROR: unable to write runtime file: $!";
2052 })->(); 2210 })->();
2053 }, 2211 },
2054); 2212);
2055 2213
2214END { cf::emergency_save }
2215
20561 22161
2057 2217

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines