… | |
… | |
24 | use YAML::Syck (); |
24 | use YAML::Syck (); |
25 | use Time::HiRes; |
25 | use Time::HiRes; |
26 | |
26 | |
27 | use Event; $Event::Eval = 1; # no idea why this is required, but it is |
27 | use Event; $Event::Eval = 1; # no idea why this is required, but it is |
28 | |
28 | |
|
|
29 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
|
|
30 | |
29 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
31 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
30 | $YAML::Syck::ImplicitUnicode = 1; |
32 | $YAML::Syck::ImplicitUnicode = 1; |
31 | |
33 | |
32 | $Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority |
34 | $Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority |
33 | |
35 | |
34 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
|
|
35 | |
|
|
36 | our %COMMAND = (); |
36 | our %COMMAND = (); |
37 | our %COMMAND_TIME = (); |
37 | our %COMMAND_TIME = (); |
|
|
38 | |
|
|
39 | our @EXTS = (); # list of extension package names |
38 | our %EXTCMD = (); |
40 | our %EXTCMD = (); |
|
|
41 | our %EXT_CORO = (); # coroutines bound to extensions |
39 | |
42 | |
40 | our @EVENT; |
43 | our @EVENT; |
41 | our $LIBDIR = datadir . "/ext"; |
44 | our $LIBDIR = datadir . "/ext"; |
42 | |
45 | |
43 | our $TICK = MAX_TIME * 1e-6; |
46 | our $TICK = MAX_TIME * 1e-6; |
… | |
… | |
52 | |
55 | |
53 | our %PLAYER; # all users |
56 | our %PLAYER; # all users |
54 | our %MAP; # all maps |
57 | our %MAP; # all maps |
55 | our $LINK_MAP; # the special {link} map |
58 | our $LINK_MAP; # the special {link} map |
56 | our $RANDOM_MAPS = cf::localdir . "/random"; |
59 | our $RANDOM_MAPS = cf::localdir . "/random"; |
57 | our %EXT_CORO; # coroutines bound to extensions |
60 | |
|
|
61 | our $WAIT_FOR_TICK; $WAIT_FOR_TICK ||= new Coro::Signal; |
|
|
62 | our $WAIT_FOR_TICK_ONE; $WAIT_FOR_TICK_ONE ||= new Coro::Signal; |
58 | |
63 | |
59 | binmode STDOUT; |
64 | binmode STDOUT; |
60 | binmode STDERR; |
65 | binmode STDERR; |
61 | |
66 | |
62 | # read virtual server time, if available |
67 | # read virtual server time, if available |
… | |
… | |
106 | |
111 | |
107 | =item %cf::CFG |
112 | =item %cf::CFG |
108 | |
113 | |
109 | Configuration for the server, loaded from C</etc/crossfire/config>, or |
114 | Configuration for the server, loaded from C</etc/crossfire/config>, or |
110 | from wherever your confdir points to. |
115 | from wherever your confdir points to. |
|
|
116 | |
|
|
117 | =item $cf::WAIT_FOR_TICK, $cf::WAIT_FOR_TICK_ONE |
|
|
118 | |
|
|
119 | These are Coro::Signal objects that are C<< ->broadcast >> (WAIT_FOR_TICK) |
|
|
120 | or C<< ->send >> (WAIT_FOR_TICK_ONE) on after normal server tick |
|
|
121 | processing has been done. Call C<< ->wait >> on them to maximise the |
|
|
122 | window of cpu time available, or simply to synchronise to the server tick. |
111 | |
123 | |
112 | =back |
124 | =back |
113 | |
125 | |
114 | =cut |
126 | =cut |
115 | |
127 | |
… | |
… | |
146 | } |
158 | } |
147 | |
159 | |
148 | $Event::DIED = sub { |
160 | $Event::DIED = sub { |
149 | warn "error in event callback: @_"; |
161 | warn "error in event callback: @_"; |
150 | }; |
162 | }; |
151 | |
|
|
152 | my %ext_pkg; |
|
|
153 | my @exts; |
|
|
154 | my @hook; |
|
|
155 | |
163 | |
156 | =head2 UTILITY FUNCTIONS |
164 | =head2 UTILITY FUNCTIONS |
157 | |
165 | |
158 | =over 4 |
166 | =over 4 |
159 | |
167 | |
… | |
… | |
309 | } |
317 | } |
310 | } |
318 | } |
311 | |
319 | |
312 | =item $coro = cf::async_ext { BLOCK } |
320 | =item $coro = cf::async_ext { BLOCK } |
313 | |
321 | |
314 | Like async, but this coro is automcatially being canceled when the |
322 | Like async, but this coro is automatically being canceled when the |
315 | extension calling this is being unloaded. |
323 | extension calling this is being unloaded. |
316 | |
324 | |
317 | =cut |
325 | =cut |
318 | |
326 | |
319 | sub async_ext(&) { |
327 | sub async_ext(&) { |
… | |
… | |
356 | =cut |
364 | =cut |
357 | |
365 | |
358 | ############################################################################# |
366 | ############################################################################# |
359 | |
367 | |
360 | package cf::path; |
368 | package cf::path; |
|
|
369 | |
|
|
370 | use overload |
|
|
371 | '""' => \&as_string; |
361 | |
372 | |
362 | # used to convert map paths into valid unix filenames by repalcing / by ∕ |
373 | # used to convert map paths into valid unix filenames by repalcing / by ∕ |
363 | our $PATH_SEP = "∕"; # U+2215, chosen purely for visual reasons |
374 | our $PATH_SEP = "∕"; # U+2215, chosen purely for visual reasons |
364 | |
375 | |
365 | sub new { |
376 | sub new { |
… | |
… | |
374 | # ?random/... random maps |
385 | # ?random/... random maps |
375 | # /! non-realised random map exit |
386 | # /! non-realised random map exit |
376 | # /... normal maps |
387 | # /... normal maps |
377 | # ~/... per-player maps without a specific player (DO NOT USE) |
388 | # ~/... per-player maps without a specific player (DO NOT USE) |
378 | # ~user/... per-player map of a specific user |
389 | # ~user/... per-player map of a specific user |
|
|
390 | |
|
|
391 | $path =~ s/$PATH_SEP/\//go; |
379 | |
392 | |
380 | if ($path =~ /^{/) { |
393 | if ($path =~ /^{/) { |
381 | # fine as it is |
394 | # fine as it is |
382 | } elsif ($path =~ s{^\?random/}{}) { |
395 | } elsif ($path =~ s{^\?random/}{}) { |
383 | Coro::AIO::aio_load "$cf::RANDOM_MAPS/$path.meta", my $data; |
396 | Coro::AIO::aio_load "$cf::RANDOM_MAPS/$path.meta", my $data; |
… | |
… | |
930 | =cut |
943 | =cut |
931 | |
944 | |
932 | sub register_extcmd { |
945 | sub register_extcmd { |
933 | my ($name, $cb) = @_; |
946 | my ($name, $cb) = @_; |
934 | |
947 | |
935 | my $caller = caller; |
|
|
936 | #warn "registering extcmd '$name' to '$caller'"; |
|
|
937 | |
|
|
938 | $EXTCMD{$name} = [$cb, $caller]; |
948 | $EXTCMD{$name} = $cb; |
939 | } |
949 | } |
940 | |
950 | |
941 | cf::player->attach ( |
951 | cf::player->attach ( |
942 | on_command => sub { |
952 | on_command => sub { |
943 | my ($pl, $name, $params) = @_; |
953 | my ($pl, $name, $params) = @_; |
… | |
… | |
956 | |
966 | |
957 | my $msg = eval { from_json $buf }; |
967 | my $msg = eval { from_json $buf }; |
958 | |
968 | |
959 | if (ref $msg) { |
969 | if (ref $msg) { |
960 | if (my $cb = $EXTCMD{$msg->{msgtype}}) { |
970 | if (my $cb = $EXTCMD{$msg->{msgtype}}) { |
961 | if (my %reply = $cb->[0]->($pl, $msg)) { |
971 | if (my %reply = $cb->($pl, $msg)) { |
962 | $pl->ext_reply ($msg->{msgid}, %reply); |
972 | $pl->ext_reply ($msg->{msgid}, %reply); |
963 | } |
973 | } |
964 | } |
974 | } |
965 | } else { |
975 | } else { |
966 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
976 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
… | |
… | |
968 | |
978 | |
969 | cf::override; |
979 | cf::override; |
970 | }, |
980 | }, |
971 | ); |
981 | ); |
972 | |
982 | |
973 | sub register { |
|
|
974 | my ($base, $pkg) = @_; |
|
|
975 | |
|
|
976 | #TODO |
|
|
977 | } |
|
|
978 | |
|
|
979 | sub load_extension { |
983 | sub load_extension { |
980 | my ($path) = @_; |
984 | my ($path) = @_; |
981 | |
985 | |
982 | $path =~ /([^\/\\]+)\.ext$/ or die "$path"; |
986 | $path =~ /([^\/\\]+)\.ext$/ or die "$path"; |
983 | my $base = $1; |
987 | my $base = $1; |
… | |
… | |
998 | |
1002 | |
999 | eval $source |
1003 | eval $source |
1000 | or die $@ ? "$path: $@\n" |
1004 | or die $@ ? "$path: $@\n" |
1001 | : "extension disabled.\n"; |
1005 | : "extension disabled.\n"; |
1002 | |
1006 | |
1003 | push @exts, $pkg; |
1007 | push @EXTS, $pkg; |
1004 | $ext_pkg{$base} = $pkg; |
|
|
1005 | |
|
|
1006 | # no strict 'refs'; |
|
|
1007 | # @{"$pkg\::ISA"} = ext::; |
|
|
1008 | |
|
|
1009 | register $base, $pkg; |
|
|
1010 | } |
|
|
1011 | |
|
|
1012 | sub unload_extension { |
|
|
1013 | my ($pkg) = @_; |
|
|
1014 | |
|
|
1015 | warn "removing extension $pkg\n"; |
|
|
1016 | |
|
|
1017 | # remove hooks |
|
|
1018 | #TODO |
|
|
1019 | # for my $idx (0 .. $#PLUGIN_EVENT) { |
|
|
1020 | # delete $hook[$idx]{$pkg}; |
|
|
1021 | # } |
|
|
1022 | |
|
|
1023 | # remove commands |
|
|
1024 | for my $name (keys %COMMAND) { |
|
|
1025 | my @cb = grep $_->[0] ne $pkg, @{ $COMMAND{$name} }; |
|
|
1026 | |
|
|
1027 | if (@cb) { |
|
|
1028 | $COMMAND{$name} = \@cb; |
|
|
1029 | } else { |
|
|
1030 | delete $COMMAND{$name}; |
|
|
1031 | } |
|
|
1032 | } |
|
|
1033 | |
|
|
1034 | # remove extcmds |
|
|
1035 | for my $name (grep $EXTCMD{$_}[1] eq $pkg, keys %EXTCMD) { |
|
|
1036 | delete $EXTCMD{$name}; |
|
|
1037 | } |
|
|
1038 | |
|
|
1039 | if (my $cb = $pkg->can ("unload")) { |
|
|
1040 | eval { |
|
|
1041 | $cb->($pkg); |
|
|
1042 | 1 |
|
|
1043 | } or warn "$pkg unloaded, but with errors: $@"; |
|
|
1044 | } |
|
|
1045 | |
|
|
1046 | Symbol::delete_package $pkg; |
|
|
1047 | } |
1008 | } |
1048 | |
1009 | |
1049 | sub load_extensions { |
1010 | sub load_extensions { |
1050 | for my $ext (<$LIBDIR/*.ext>) { |
1011 | for my $ext (<$LIBDIR/*.ext>) { |
1051 | next unless -r $ext; |
1012 | next unless -r $ext; |
… | |
… | |
1069 | }; |
1030 | }; |
1070 | |
1031 | |
1071 | cf::map->attach (prio => -10000, package => cf::mapsupport::); |
1032 | cf::map->attach (prio => -10000, package => cf::mapsupport::); |
1072 | |
1033 | |
1073 | ############################################################################# |
1034 | ############################################################################# |
1074 | # load/save perl data associated with player->ob objects |
|
|
1075 | |
|
|
1076 | sub all_objects(@) { |
|
|
1077 | @_, map all_objects ($_->inv), @_ |
|
|
1078 | } |
|
|
1079 | |
|
|
1080 | # TODO: compatibility cruft, remove when no longer needed |
|
|
1081 | cf::player->attach ( |
|
|
1082 | on_load => sub { |
|
|
1083 | my ($pl, $path) = @_; |
|
|
1084 | |
|
|
1085 | for my $o (all_objects $pl->ob) { |
|
|
1086 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
|
|
1087 | $o->set_ob_key_value ("_perl_data"); |
|
|
1088 | |
|
|
1089 | %$o = %{ Storable::thaw pack "H*", $value }; |
|
|
1090 | } |
|
|
1091 | } |
|
|
1092 | }, |
|
|
1093 | ); |
|
|
1094 | |
|
|
1095 | ############################################################################# |
|
|
1096 | |
1035 | |
1097 | =head2 CORE EXTENSIONS |
1036 | =head2 CORE EXTENSIONS |
1098 | |
1037 | |
1099 | Functions and methods that extend core crossfire objects. |
1038 | Functions and methods that extend core crossfire objects. |
1100 | |
1039 | |
… | |
… | |
1225 | my @logins; |
1164 | my @logins; |
1226 | |
1165 | |
1227 | for my $login (@$dirs) { |
1166 | for my $login (@$dirs) { |
1228 | my $fh = aio_open path $login, Fcntl::O_RDONLY, 0 or next; |
1167 | my $fh = aio_open path $login, Fcntl::O_RDONLY, 0 or next; |
1229 | aio_read $fh, 0, 512, my $buf, 0 or next; |
1168 | aio_read $fh, 0, 512, my $buf, 0 or next; |
1230 | $buf !~ /^password -------------$/ or next; # official not-valid tag |
1169 | $buf !~ /^password -------------$/m or next; # official not-valid tag |
1231 | |
1170 | |
1232 | utf8::decode $login; |
1171 | utf8::decode $login; |
1233 | push @logins, $login; |
1172 | push @logins, $login; |
1234 | } |
1173 | } |
1235 | |
1174 | |
… | |
… | |
1252 | my @paths; |
1191 | my @paths; |
1253 | |
1192 | |
1254 | for (@$files) { |
1193 | for (@$files) { |
1255 | utf8::decode $_; |
1194 | utf8::decode $_; |
1256 | next if /\.(?:pl|pst)$/; |
1195 | next if /\.(?:pl|pst)$/; |
1257 | next unless /^$PATH_SEP/; |
1196 | next unless /^$PATH_SEP/o; |
1258 | |
1197 | |
1259 | s/$PATH_SEP/\//g; |
|
|
1260 | push @paths, new cf::path "~" . $pl->ob->name . "/" . $_; |
1198 | push @paths, new cf::path "~" . $pl->ob->name . "/" . $_; |
1261 | } |
1199 | } |
1262 | |
1200 | |
1263 | \@paths |
1201 | \@paths |
1264 | } |
1202 | } |
… | |
… | |
1451 | Coro::cede; |
1389 | Coro::cede; |
1452 | |
1390 | |
1453 | $self->in_memory (cf::MAP_IN_MEMORY); |
1391 | $self->in_memory (cf::MAP_IN_MEMORY); |
1454 | } |
1392 | } |
1455 | |
1393 | |
|
|
1394 | # find and load all maps in the 3x3 area around a map |
|
|
1395 | sub load_diag { |
|
|
1396 | my ($map) = @_; |
|
|
1397 | |
|
|
1398 | my @diag; # diagonal neighbours |
|
|
1399 | |
|
|
1400 | for (0 .. 3) { |
|
|
1401 | my $neigh = $map->tile_path ($_) |
|
|
1402 | or next; |
|
|
1403 | $neigh = find $neigh, $map |
|
|
1404 | or next; |
|
|
1405 | $neigh->load; |
|
|
1406 | |
|
|
1407 | push @diag, [$neigh->tile_path (($_ + 3) % 4), $neigh], |
|
|
1408 | [$neigh->tile_path (($_ + 1) % 4), $neigh]; |
|
|
1409 | } |
|
|
1410 | |
|
|
1411 | for (@diag) { |
|
|
1412 | my $neigh = find @$_ |
|
|
1413 | or next; |
|
|
1414 | $neigh->load; |
|
|
1415 | } |
|
|
1416 | } |
|
|
1417 | |
1456 | sub find_sync { |
1418 | sub find_sync { |
1457 | my ($path, $origin) = @_; |
1419 | my ($path, $origin) = @_; |
1458 | |
1420 | |
1459 | cf::sync_job { cf::map::find $path, $origin } |
1421 | cf::sync_job { find $path, $origin } |
1460 | } |
1422 | } |
1461 | |
1423 | |
1462 | sub do_load_sync { |
1424 | sub do_load_sync { |
1463 | my ($map) = @_; |
1425 | my ($map) = @_; |
1464 | |
1426 | |
1465 | cf::sync_job { $map->load }; |
1427 | cf::sync_job { $map->load }; |
|
|
1428 | } |
|
|
1429 | |
|
|
1430 | our %MAP_PREFETCH; |
|
|
1431 | our $MAP_PREFETCHER = Coro::async { |
|
|
1432 | while () { |
|
|
1433 | while (%MAP_PREFETCH) { |
|
|
1434 | my $key = each %MAP_PREFETCH |
|
|
1435 | or next; |
|
|
1436 | my $path = delete $MAP_PREFETCH{$key}; |
|
|
1437 | |
|
|
1438 | my $map = find $path |
|
|
1439 | or next; |
|
|
1440 | $map->load; |
|
|
1441 | } |
|
|
1442 | Coro::schedule; |
|
|
1443 | } |
|
|
1444 | }; |
|
|
1445 | |
|
|
1446 | sub find_async { |
|
|
1447 | my ($path, $origin) = @_; |
|
|
1448 | |
|
|
1449 | $path = new cf::path $path, $origin && $origin->path; |
|
|
1450 | my $key = $path->as_string; |
|
|
1451 | |
|
|
1452 | if (my $map = $cf::MAP{$key}) { |
|
|
1453 | return $map if $map->in_memory == cf::MAP_IN_MEMORY; |
|
|
1454 | } |
|
|
1455 | |
|
|
1456 | $MAP_PREFETCH{$key} = $path; |
|
|
1457 | $MAP_PREFETCHER->ready; |
|
|
1458 | |
|
|
1459 | () |
1466 | } |
1460 | } |
1467 | |
1461 | |
1468 | sub save { |
1462 | sub save { |
1469 | my ($self) = @_; |
1463 | my ($self) = @_; |
1470 | |
1464 | |
… | |
… | |
1588 | } |
1582 | } |
1589 | |
1583 | |
1590 | $map |
1584 | $map |
1591 | } |
1585 | } |
1592 | |
1586 | |
1593 | sub emergency_save { |
1587 | =item cf::map::unique_maps |
1594 | my $freeze_guard = cf::freeze_mainloop; |
|
|
1595 | |
1588 | |
1596 | warn "enter emergency perl save\n"; |
1589 | Returns an arrayref of cf::path's of all shared maps that have |
|
|
1590 | instantiated unique items. May block. |
1597 | |
1591 | |
1598 | cf::sync_job { |
1592 | =cut |
1599 | warn "begin emergency player save\n"; |
|
|
1600 | $_->save for values %cf::PLAYER; |
|
|
1601 | warn "end emergency player save\n"; |
|
|
1602 | |
1593 | |
1603 | warn "begin emergency map save\n"; |
1594 | sub unique_maps() { |
1604 | $_->save for values %cf::MAP; |
1595 | my $files = aio_readdir cf::localdir . "/" . cf::uniquedir |
1605 | warn "end emergency map save\n"; |
1596 | or return; |
|
|
1597 | |
|
|
1598 | my @paths; |
|
|
1599 | |
|
|
1600 | for (@$files) { |
|
|
1601 | utf8::decode $_; |
|
|
1602 | next if /\.pst$/; |
|
|
1603 | next unless /^$PATH_SEP/o; |
|
|
1604 | |
|
|
1605 | push @paths, new cf::path $_; |
1606 | }; |
1606 | } |
1607 | |
1607 | |
1608 | warn "leave emergency perl save\n"; |
1608 | \@paths |
1609 | } |
1609 | } |
1610 | |
1610 | |
1611 | package cf; |
1611 | package cf; |
1612 | |
1612 | |
1613 | =back |
1613 | =back |
1614 | |
1614 | |
|
|
1615 | =head3 cf::object |
|
|
1616 | |
|
|
1617 | =cut |
|
|
1618 | |
|
|
1619 | package cf::object; |
|
|
1620 | |
|
|
1621 | =over 4 |
|
|
1622 | |
|
|
1623 | =item $ob->inv_recursive |
|
|
1624 | |
|
|
1625 | Returns the inventory of the object _and_ their inventories, recursively. |
|
|
1626 | |
|
|
1627 | =cut |
|
|
1628 | |
|
|
1629 | sub inv_recursive_; |
|
|
1630 | sub inv_recursive_ { |
|
|
1631 | map { $_, inv_recursive_ $_->inv } @_ |
|
|
1632 | } |
|
|
1633 | |
|
|
1634 | sub inv_recursive { |
|
|
1635 | inv_recursive_ inv $_[0] |
|
|
1636 | } |
|
|
1637 | |
|
|
1638 | package cf; |
|
|
1639 | |
|
|
1640 | =back |
1615 | |
1641 | |
1616 | =head3 cf::object::player |
1642 | =head3 cf::object::player |
1617 | |
1643 | |
1618 | =over 4 |
1644 | =over 4 |
1619 | |
1645 | |
… | |
… | |
1711 | # use -1 or undef as default coordinates, not 0, 0 |
1737 | # use -1 or undef as default coordinates, not 0, 0 |
1712 | ($x, $y) = ($map->enter_x, $map->enter_y) |
1738 | ($x, $y) = ($map->enter_x, $map->enter_y) |
1713 | if $x <=0 && $y <= 0; |
1739 | if $x <=0 && $y <= 0; |
1714 | |
1740 | |
1715 | $map->load; |
1741 | $map->load; |
|
|
1742 | $map->load_diag; |
1716 | |
1743 | |
1717 | return unless $self->contr->active; |
1744 | return unless $self->contr->active; |
1718 | $self->activate_recursive; |
1745 | $self->activate_recursive; |
1719 | $self->enter_map ($map, $x, $y); |
1746 | $self->enter_map ($map, $x, $y); |
1720 | } |
1747 | } |
… | |
… | |
1757 | |
1784 | |
1758 | sub cf::object::player::goto { |
1785 | sub cf::object::player::goto { |
1759 | my ($self, $path, $x, $y) = @_; |
1786 | my ($self, $path, $x, $y) = @_; |
1760 | |
1787 | |
1761 | $path = new cf::path $path; |
1788 | $path = new cf::path $path; |
1762 | $path ne "/" or Carp::cluck ("oy");#d# |
|
|
1763 | |
1789 | |
1764 | $self->enter_link; |
1790 | $self->enter_link; |
1765 | |
1791 | |
1766 | (async { |
1792 | (async { |
1767 | my $map = cf::map::find $path->as_string; |
1793 | my $map = cf::map::find $path->as_string; |
… | |
… | |
1844 | |
1870 | |
1845 | 1; |
1871 | 1; |
1846 | }) { |
1872 | }) { |
1847 | $self->message ("Something went wrong deep within the crossfire server. " |
1873 | $self->message ("Something went wrong deep within the crossfire server. " |
1848 | . "I'll try to bring you back to the map you were before. " |
1874 | . "I'll try to bring you back to the map you were before. " |
1849 | . "Please report this to the dungeon master", |
1875 | . "Please report this to the dungeon master!", |
1850 | cf::NDI_UNIQUE | cf::NDI_RED); |
1876 | cf::NDI_UNIQUE | cf::NDI_RED); |
1851 | |
1877 | |
1852 | warn "ERROR in enter_exit: $@"; |
1878 | warn "ERROR in enter_exit: $@"; |
1853 | $self->leave_link; |
1879 | $self->leave_link; |
1854 | } |
1880 | } |
… | |
… | |
2207 | load_extensions; |
2233 | load_extensions; |
2208 | Event::loop; |
2234 | Event::loop; |
2209 | } |
2235 | } |
2210 | |
2236 | |
2211 | ############################################################################# |
2237 | ############################################################################# |
2212 | # initialisation |
2238 | # initialisation and cleanup |
|
|
2239 | |
|
|
2240 | # install some emergency cleanup handlers |
|
|
2241 | BEGIN { |
|
|
2242 | for my $signal (qw(INT HUP TERM)) { |
|
|
2243 | Event->signal ( |
|
|
2244 | data => WF_AUTOCANCEL, |
|
|
2245 | signal => $signal, |
|
|
2246 | cb => sub { |
|
|
2247 | cf::cleanup "SIG$signal"; |
|
|
2248 | }, |
|
|
2249 | ); |
|
|
2250 | } |
|
|
2251 | } |
|
|
2252 | |
|
|
2253 | sub emergency_save() { |
|
|
2254 | my $freeze_guard = cf::freeze_mainloop; |
|
|
2255 | |
|
|
2256 | warn "enter emergency perl save\n"; |
|
|
2257 | |
|
|
2258 | cf::sync_job { |
|
|
2259 | # use a peculiar iteration method to avoid tripping on perl |
|
|
2260 | # refcount bugs in for. also avoids problems with players |
|
|
2261 | # and maps saved/Destroyed asynchronously. |
|
|
2262 | warn "begin emergency player save\n"; |
|
|
2263 | for my $login (keys %cf::PLAYER) { |
|
|
2264 | my $pl = $cf::PLAYER{$login} or next; |
|
|
2265 | $pl->valid or next; |
|
|
2266 | $pl->save; |
|
|
2267 | } |
|
|
2268 | warn "end emergency player save\n"; |
|
|
2269 | |
|
|
2270 | warn "begin emergency map save\n"; |
|
|
2271 | for my $path (keys %cf::MAP) { |
|
|
2272 | my $map = $cf::MAP{$path} or next; |
|
|
2273 | $map->valid or next; |
|
|
2274 | $map->save; |
|
|
2275 | } |
|
|
2276 | warn "end emergency map save\n"; |
|
|
2277 | }; |
|
|
2278 | |
|
|
2279 | warn "leave emergency perl save\n"; |
|
|
2280 | } |
2213 | |
2281 | |
2214 | sub reload() { |
2282 | sub reload() { |
2215 | # can/must only be called in main |
2283 | # can/must only be called in main |
2216 | if ($Coro::current != $Coro::main) { |
2284 | if ($Coro::current != $Coro::main) { |
2217 | warn "can only reload from main coroutine\n"; |
2285 | warn "can only reload from main coroutine\n"; |
2218 | return; |
2286 | return; |
2219 | } |
2287 | } |
2220 | |
2288 | |
2221 | warn "reloading..."; |
2289 | warn "reloading..."; |
2222 | |
2290 | |
|
|
2291 | warn "freezing server"; |
2223 | my $guard = freeze_mainloop; |
2292 | my $guard = freeze_mainloop; |
2224 | cf::emergency_save; |
2293 | cf::emergency_save; |
2225 | |
2294 | |
|
|
2295 | warn "sync database to disk"; |
|
|
2296 | cf::db_sync; |
|
|
2297 | IO::AIO::flush; |
|
|
2298 | |
2226 | eval { |
2299 | eval { |
2227 | # if anything goes wrong in here, we should simply crash as we already saved |
2300 | # if anything goes wrong in here, we should simply crash as we already saved |
2228 | |
2301 | |
2229 | # cancel all watchers |
2302 | warn "cancel all watchers"; |
2230 | for (Event::all_watchers) { |
2303 | for (Event::all_watchers) { |
2231 | $_->cancel if $_->data & WF_AUTOCANCEL; |
2304 | $_->cancel if $_->data & WF_AUTOCANCEL; |
2232 | } |
2305 | } |
2233 | |
2306 | |
2234 | # cancel all extension coros |
2307 | warn "cancel all extension coros"; |
2235 | $_->cancel for values %EXT_CORO; |
2308 | $_->cancel for values %EXT_CORO; |
2236 | %EXT_CORO = (); |
2309 | %EXT_CORO = (); |
2237 | |
2310 | |
|
|
2311 | warn "remove commands"; |
|
|
2312 | %COMMAND = (); |
|
|
2313 | |
|
|
2314 | warn "remove ext commands"; |
|
|
2315 | %EXTCMD = (); |
|
|
2316 | |
2238 | # unload all extensions |
2317 | warn "unload/nuke all extensions"; |
2239 | for (@exts) { |
2318 | for my $pkg (@EXTS) { |
2240 | warn "unloading <$_>"; |
2319 | warn "unloading <$pkg>"; |
2241 | unload_extension $_; |
2320 | |
|
|
2321 | if (my $cb = $pkg->can ("unload")) { |
|
|
2322 | eval { |
|
|
2323 | $cb->($pkg); |
|
|
2324 | 1 |
|
|
2325 | } or warn "$pkg unloaded, but with errors: $@"; |
2242 | } |
2326 | } |
2243 | |
2327 | |
|
|
2328 | Symbol::delete_package $pkg; |
|
|
2329 | } |
|
|
2330 | |
2244 | # unload all modules loaded from $LIBDIR |
2331 | warn "unload all perl modules loaded from $LIBDIR"; |
2245 | while (my ($k, $v) = each %INC) { |
2332 | while (my ($k, $v) = each %INC) { |
2246 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
2333 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
2247 | |
2334 | |
2248 | warn "removing <$k>"; |
2335 | warn "removing <$k>"; |
2249 | delete $INC{$k}; |
2336 | delete $INC{$k}; |
… | |
… | |
2256 | } |
2343 | } |
2257 | |
2344 | |
2258 | Symbol::delete_package $k; |
2345 | Symbol::delete_package $k; |
2259 | } |
2346 | } |
2260 | |
2347 | |
2261 | # sync database to disk |
|
|
2262 | cf::db_sync; |
|
|
2263 | IO::AIO::flush; |
|
|
2264 | |
|
|
2265 | # get rid of safe::, as good as possible |
2348 | warn "get rid of safe::, as good as possible"; |
2266 | Symbol::delete_package "safe::$_" |
2349 | Symbol::delete_package "safe::$_" |
2267 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
2350 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
2268 | |
2351 | |
2269 | # remove register_script_function callbacks |
|
|
2270 | # TODO |
|
|
2271 | |
|
|
2272 | # unload cf.pm "a bit" |
2352 | warn "unload cf.pm \"a bit\""; |
2273 | delete $INC{"cf.pm"}; |
2353 | delete $INC{"cf.pm"}; |
2274 | |
2354 | |
2275 | # don't, removes xs symbols, too, |
2355 | # don't, removes xs symbols, too, |
2276 | # and global variables created in xs |
2356 | # and global variables created in xs |
2277 | #Symbol::delete_package __PACKAGE__; |
2357 | #Symbol::delete_package __PACKAGE__; |
2278 | |
2358 | |
2279 | # reload cf.pm |
|
|
2280 | warn "reloading cf.pm"; |
2359 | warn "reloading cf.pm"; |
2281 | require cf; |
2360 | require cf; |
2282 | cf::_connect_to_perl; # nominally unnecessary, but cannot hurt |
2361 | cf::_connect_to_perl; # nominally unnecessary, but cannot hurt |
2283 | |
2362 | |
2284 | # load config and database again |
2363 | warn "load config and database again"; |
2285 | cf::cfg_load; |
2364 | cf::cfg_load; |
2286 | cf::db_load; |
2365 | cf::db_load; |
2287 | |
2366 | |
2288 | # load extensions |
|
|
2289 | warn "load extensions"; |
2367 | warn "load extensions"; |
2290 | cf::load_extensions; |
2368 | cf::load_extensions; |
2291 | |
2369 | |
2292 | # reattach attachments to objects |
2370 | warn "reattach attachments to objects/players"; |
2293 | warn "reattach"; |
|
|
2294 | _global_reattach; |
2371 | _global_reattach; |
|
|
2372 | warn "reattach attachments to maps"; |
2295 | reattach $_ for values %MAP; |
2373 | reattach $_ for values %MAP; |
2296 | }; |
2374 | }; |
2297 | |
2375 | |
2298 | if ($@) { |
2376 | if ($@) { |
2299 | warn $@; |
2377 | warn $@; |
2300 | warn "error while reloading, exiting."; |
2378 | warn "error while reloading, exiting."; |
2301 | exit 1; |
2379 | exit 1; |
2302 | } |
2380 | } |
2303 | |
2381 | |
2304 | warn "reloaded successfully"; |
2382 | warn "reloaded"; |
2305 | }; |
2383 | }; |
2306 | |
2384 | |
2307 | ############################################################################# |
2385 | ############################################################################# |
2308 | |
2386 | |
2309 | unless ($LINK_MAP) { |
2387 | unless ($LINK_MAP) { |
… | |
… | |
2344 | $LINK_MAP->{deny_save} = 1; |
2422 | $LINK_MAP->{deny_save} = 1; |
2345 | $LINK_MAP->{deny_reset} = 1; |
2423 | $LINK_MAP->{deny_reset} = 1; |
2346 | |
2424 | |
2347 | $cf::MAP{$LINK_MAP->path} = $LINK_MAP; |
2425 | $cf::MAP{$LINK_MAP->path} = $LINK_MAP; |
2348 | } |
2426 | } |
2349 | |
|
|
2350 | register "<global>", __PACKAGE__; |
|
|
2351 | |
2427 | |
2352 | register_command "reload" => sub { |
2428 | register_command "reload" => sub { |
2353 | my ($who, $arg) = @_; |
2429 | my ($who, $arg) = @_; |
2354 | |
2430 | |
2355 | if ($who->flag (FLAG_WIZ)) { |
2431 | if ($who->flag (FLAG_WIZ)) { |
… | |
… | |
2368 | data => WF_AUTOCANCEL, |
2444 | data => WF_AUTOCANCEL, |
2369 | cb => sub { |
2445 | cb => sub { |
2370 | cf::server_tick; # one server iteration |
2446 | cf::server_tick; # one server iteration |
2371 | $RUNTIME += $TICK; |
2447 | $RUNTIME += $TICK; |
2372 | $NEXT_TICK += $TICK; |
2448 | $NEXT_TICK += $TICK; |
|
|
2449 | |
|
|
2450 | $WAIT_FOR_TICK->broadcast; |
|
|
2451 | $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited; |
2373 | |
2452 | |
2374 | # if we are delayed by four ticks or more, skip them all |
2453 | # if we are delayed by four ticks or more, skip them all |
2375 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
2454 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
2376 | |
2455 | |
2377 | $TICK_WATCHER->at ($NEXT_TICK); |
2456 | $TICK_WATCHER->at ($NEXT_TICK); |