… | |
… | |
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.5 (); |
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; |
… | |
… | |
44 | our @EVENT; |
44 | our @EVENT; |
45 | our $LIBDIR = datadir . "/ext"; |
45 | our $LIBDIR = datadir . "/ext"; |
46 | |
46 | |
47 | our $TICK = MAX_TIME * 1e-6; |
47 | our $TICK = MAX_TIME * 1e-6; |
48 | our $TICK_WATCHER; |
48 | our $TICK_WATCHER; |
|
|
49 | our $AIO_POLL_WATCHER; |
|
|
50 | our $WRITE_RUNTIME_WATCHER; |
49 | our $NEXT_TICK; |
51 | our $NEXT_TICK; |
50 | our $NOW; |
52 | our $NOW; |
51 | |
53 | |
52 | our %CFG; |
54 | our %CFG; |
53 | |
55 | |
… | |
… | |
80 | mkdir cf::localdir . "/" . cf::tmpdir; |
82 | mkdir cf::localdir . "/" . cf::tmpdir; |
81 | mkdir cf::localdir . "/" . cf::uniquedir; |
83 | mkdir cf::localdir . "/" . cf::uniquedir; |
82 | mkdir $RANDOM_MAPS; |
84 | mkdir $RANDOM_MAPS; |
83 | |
85 | |
84 | our $EMERGENCY_POSITION; |
86 | our $EMERGENCY_POSITION; |
|
|
87 | |
|
|
88 | sub cf::map::normalise; |
85 | |
89 | |
86 | ############################################################################# |
90 | ############################################################################# |
87 | |
91 | |
88 | =head2 GLOBAL VARIABLES |
92 | =head2 GLOBAL VARIABLES |
89 | |
93 | |
… | |
… | |
257 | } |
261 | } |
258 | |
262 | |
259 | sub freeze_mainloop { |
263 | sub freeze_mainloop { |
260 | return unless $TICK_WATCHER->is_active; |
264 | return unless $TICK_WATCHER->is_active; |
261 | |
265 | |
262 | my $guard = Coro::guard { $TICK_WATCHER->start }; |
266 | my $guard = Coro::guard { |
|
|
267 | $TICK_WATCHER->start; |
|
|
268 | $WRITE_RUNTIME_WATCHER->start; |
|
|
269 | }; |
|
|
270 | $WRITE_RUNTIME_WATCHER->stop; |
263 | $TICK_WATCHER->stop; |
271 | $TICK_WATCHER->stop; |
264 | $guard |
272 | $guard |
265 | } |
273 | } |
266 | |
274 | |
267 | =item cf::async { BLOCK } |
275 | =item cf::async { BLOCK } |
… | |
… | |
338 | |
346 | |
339 | $coro |
347 | $coro |
340 | } |
348 | } |
341 | |
349 | |
342 | sub write_runtime { |
350 | sub write_runtime { |
343 | return unless $TICK_WATCHER->is_active; |
|
|
344 | |
|
|
345 | my $runtime = cf::localdir . "/runtime"; |
351 | my $runtime = cf::localdir . "/runtime"; |
346 | |
352 | |
347 | my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 |
353 | my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 |
348 | or return; |
354 | or return; |
349 | |
355 | |
|
|
356 | my $value = $cf::RUNTIME + 90 + 10; |
350 | my $value = $cf::RUNTIME + 1 + 10; # 10 is the runtime save interval, for a monotonic clock |
357 | # 10 is the runtime save interval, for a monotonic clock |
|
|
358 | # 60 allows for the watchdog to kill the server. |
|
|
359 | |
351 | (aio_write $fh, 0, (length $value), $value, 0) <= 0 |
360 | (aio_write $fh, 0, (length $value), $value, 0) <= 0 |
352 | and return; |
361 | and return; |
353 | |
362 | |
354 | aio_fsync $fh |
363 | aio_fsync $fh |
355 | and return; |
364 | and return; |
… | |
… | |
499 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
508 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
500 | our @CB_MAP = (); |
509 | our @CB_MAP = (); |
501 | |
510 | |
502 | my %attachment; |
511 | my %attachment; |
503 | |
512 | |
|
|
513 | sub cf::attachable::thawer_merge { |
|
|
514 | # simply override everything except _meta |
|
|
515 | local $_[0]{_meta}; |
|
|
516 | %{$_[0]} = %{$_[1]}; |
|
|
517 | } |
|
|
518 | |
504 | sub _attach_cb($$$$) { |
519 | sub _attach_cb($$$$) { |
505 | my ($registry, $event, $prio, $cb) = @_; |
520 | my ($registry, $event, $prio, $cb) = @_; |
506 | |
521 | |
507 | use sort 'stable'; |
522 | use sort 'stable'; |
508 | |
523 | |
… | |
… | |
679 | # object support |
694 | # object support |
680 | |
695 | |
681 | sub reattach { |
696 | sub reattach { |
682 | # basically do the same as instantiate, without calling instantiate |
697 | # basically do the same as instantiate, without calling instantiate |
683 | my ($obj) = @_; |
698 | my ($obj) = @_; |
|
|
699 | |
|
|
700 | bless $obj, ref $obj; # re-bless in case extensions have been reloaded |
684 | |
701 | |
685 | my $registry = $obj->registry; |
702 | my $registry = $obj->registry; |
686 | |
703 | |
687 | @$registry = (); |
704 | @$registry = (); |
688 | |
705 | |
… | |
… | |
759 | } |
776 | } |
760 | } else { |
777 | } else { |
761 | aio_unlink $filename; |
778 | aio_unlink $filename; |
762 | aio_unlink "$filename.pst"; |
779 | aio_unlink "$filename.pst"; |
763 | } |
780 | } |
|
|
781 | |
|
|
782 | #d##TODO# nuke non .map-files if exist |
|
|
783 | if ($filename =~ s/\.map$//) { |
|
|
784 | aio_unlink $filename; |
|
|
785 | aio_unlink "$filename.pst"; |
|
|
786 | } |
764 | } |
787 | } |
765 | } |
788 | } |
766 | |
789 | |
767 | sub object_freezer_as_string { |
790 | sub object_freezer_as_string { |
768 | my ($rdata, $objs) = @_; |
791 | my ($rdata, $objs) = @_; |
… | |
… | |
774 | |
797 | |
775 | sub object_thawer_load { |
798 | sub object_thawer_load { |
776 | my ($filename) = @_; |
799 | my ($filename) = @_; |
777 | |
800 | |
778 | my ($data, $av); |
801 | my ($data, $av); |
|
|
802 | |
|
|
803 | #d#TODO remove .map if file does not exist |
|
|
804 | aio_stat $filename and $filename =~ s/\.map$//; |
779 | |
805 | |
780 | (aio_load $filename, $data) >= 0 |
806 | (aio_load $filename, $data) >= 0 |
781 | or return; |
807 | or return; |
782 | |
808 | |
783 | unless (aio_stat "$filename.pst") { |
809 | unless (aio_stat "$filename.pst") { |
… | |
… | |
900 | } or warn "$ext not loaded: $@"; |
926 | } or warn "$ext not loaded: $@"; |
901 | } |
927 | } |
902 | } |
928 | } |
903 | |
929 | |
904 | ############################################################################# |
930 | ############################################################################# |
905 | # load/save/clean perl data associated with a map |
|
|
906 | |
|
|
907 | *cf::mapsupport::on_clean = sub { |
|
|
908 | my ($map) = @_; |
|
|
909 | |
|
|
910 | my $path = $map->tmpname; |
|
|
911 | defined $path or return; |
|
|
912 | |
|
|
913 | unlink "$path.pst"; |
|
|
914 | }; |
|
|
915 | |
|
|
916 | cf::map->attach (prio => -10000, package => cf::mapsupport::); |
|
|
917 | |
|
|
918 | ############################################################################# |
|
|
919 | |
931 | |
920 | =head2 CORE EXTENSIONS |
932 | =head2 CORE EXTENSIONS |
921 | |
933 | |
922 | Functions and methods that extend core crossfire objects. |
934 | Functions and methods that extend core crossfire objects. |
923 | |
935 | |
… | |
… | |
959 | |
971 | |
960 | sub exists($) { |
972 | sub exists($) { |
961 | my ($login) = @_; |
973 | my ($login) = @_; |
962 | |
974 | |
963 | $cf::PLAYER{$login} |
975 | $cf::PLAYER{$login} |
964 | or cf::sync_job { !aio_stat $login } |
976 | or cf::sync_job { !aio_stat path $login } |
965 | } |
977 | } |
966 | |
978 | |
967 | sub find($) { |
979 | sub find($) { |
968 | return $cf::PLAYER{$_[0]} || do { |
980 | return $cf::PLAYER{$_[0]} || do { |
969 | my $login = $_[0]; |
981 | my $login = $_[0]; |
… | |
… | |
1067 | =cut |
1079 | =cut |
1068 | |
1080 | |
1069 | sub maps($) { |
1081 | sub maps($) { |
1070 | my ($pl) = @_; |
1082 | my ($pl) = @_; |
1071 | |
1083 | |
|
|
1084 | $pl = ref $pl ? $pl->ob->name : $pl; |
|
|
1085 | |
1072 | my $files = aio_readdir playerdir $pl |
1086 | my $files = aio_readdir playerdir $pl |
1073 | or return; |
1087 | or return; |
1074 | |
1088 | |
1075 | my @paths; |
1089 | my @paths; |
1076 | |
1090 | |
1077 | for (@$files) { |
1091 | for (@$files) { |
1078 | utf8::decode $_; |
1092 | utf8::decode $_; |
1079 | next if /\.(?:pl|pst)$/; |
1093 | next if /\.(?:pl|pst)$/; |
1080 | next unless /^$PATH_SEP/o; |
1094 | next unless /^$PATH_SEP/o; |
1081 | |
1095 | |
1082 | push @paths, "~" . $pl->ob->name . "/" . $_; |
1096 | push @paths, cf::map::normalise "~$pl/$_"; |
1083 | } |
1097 | } |
1084 | |
1098 | |
1085 | \@paths |
1099 | \@paths |
1086 | } |
1100 | } |
1087 | |
1101 | |
… | |
… | |
1114 | |
1128 | |
1115 | use Fcntl; |
1129 | use Fcntl; |
1116 | use Coro::AIO; |
1130 | use Coro::AIO; |
1117 | |
1131 | |
1118 | use overload |
1132 | use overload |
1119 | '""' => \&as_string; |
1133 | '""' => \&as_string, |
|
|
1134 | fallback => 1; |
1120 | |
1135 | |
1121 | our $MAX_RESET = 3600; |
1136 | our $MAX_RESET = 3600; |
1122 | our $DEFAULT_RESET = 3000; |
1137 | our $DEFAULT_RESET = 3000; |
1123 | |
1138 | |
1124 | sub generate_random_map { |
1139 | sub generate_random_map { |
1125 | my ($self, $rmp) = @_; |
1140 | my ($self, $rmp) = @_; |
1126 | |
|
|
1127 | # mit "rum" bekleckern, nicht |
1141 | # mit "rum" bekleckern, nicht |
1128 | $self->_create_random_map ( |
1142 | $self->_create_random_map ( |
1129 | $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, |
1143 | $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, |
1130 | $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, |
1144 | $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, |
1131 | $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, |
1145 | $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, |
… | |
… | |
1138 | $rmp->{map_layout_style}, $rmp->{treasureoptions}, $rmp->{symmetry_used}, |
1152 | $rmp->{map_layout_style}, $rmp->{treasureoptions}, $rmp->{symmetry_used}, |
1139 | (cf::region::find $rmp->{region}), $rmp->{custom} |
1153 | (cf::region::find $rmp->{region}), $rmp->{custom} |
1140 | ) |
1154 | ) |
1141 | } |
1155 | } |
1142 | |
1156 | |
|
|
1157 | =item cf::map->register ($regex, $prio) |
|
|
1158 | |
|
|
1159 | Register a handler for the map path matching the given regex at the |
|
|
1160 | givne priority (higher is better, built-in handlers have priority 0, the |
|
|
1161 | default). |
|
|
1162 | |
|
|
1163 | =cut |
|
|
1164 | |
1143 | sub register { |
1165 | sub register { |
1144 | my (undef, $regex) = @_; |
1166 | my (undef, $regex, $prio) = @_; |
1145 | my $pkg = caller; |
1167 | my $pkg = caller; |
1146 | |
1168 | |
1147 | no strict; |
1169 | no strict; |
1148 | push @{"$pkg\::ISA"}, __PACKAGE__; |
1170 | push @{"$pkg\::ISA"}, __PACKAGE__; |
1149 | |
1171 | |
1150 | $EXT_MAP{$pkg} = qr<$regex>; |
1172 | $EXT_MAP{$pkg} = [$prio, qr<$regex>]; |
1151 | } |
1173 | } |
1152 | |
1174 | |
1153 | # also paths starting with '/' |
1175 | # also paths starting with '/' |
1154 | $EXT_MAP{"cf::map"} = qr{^(?=/)}; |
1176 | $EXT_MAP{"cf::map"} = [0, qr{^(?=/)}]; |
|
|
1177 | |
|
|
1178 | sub thawer_merge { |
|
|
1179 | my ($self, $merge) = @_; |
|
|
1180 | |
|
|
1181 | # we have to keep some variables in memory intact |
|
|
1182 | local $self->{path}; |
|
|
1183 | local $self->{load_path}; |
|
|
1184 | local $self->{deny_save}; |
|
|
1185 | local $self->{deny_reset}; |
|
|
1186 | |
|
|
1187 | $self->SUPER::thawer_merge ($merge); |
|
|
1188 | } |
1155 | |
1189 | |
1156 | sub normalise { |
1190 | sub normalise { |
1157 | my ($path, $base) = @_; |
1191 | my ($path, $base) = @_; |
|
|
1192 | |
|
|
1193 | $path = "$path"; # make sure its a string |
|
|
1194 | |
|
|
1195 | $path =~ s/\.map$//; |
1158 | |
1196 | |
1159 | # map plan: |
1197 | # map plan: |
1160 | # |
1198 | # |
1161 | # /! non-realised random map exit (special hack!) |
1199 | # /! non-realised random map exit (special hack!) |
1162 | # {... are special paths that are not being touched |
1200 | # {... are special paths that are not being touched |
… | |
… | |
1190 | |
1228 | |
1191 | return $path if UNIVERSAL::isa $path, "cf::map"; # already a map object |
1229 | return $path if UNIVERSAL::isa $path, "cf::map"; # already a map object |
1192 | |
1230 | |
1193 | $path = normalise $path, $base; |
1231 | $path = normalise $path, $base; |
1194 | |
1232 | |
1195 | for my $pkg (keys %EXT_MAP) { |
1233 | for my $pkg (sort { $EXT_MAP{$b}[0] <=> $EXT_MAP{$a}[0] } keys %EXT_MAP) { |
1196 | if ($path =~ $EXT_MAP{$pkg}) { |
1234 | if ($path =~ $EXT_MAP{$pkg}[1]) { |
1197 | my $self = bless cf::map::new, $pkg; |
1235 | my $self = bless cf::map::new, $pkg; |
1198 | $self->{path} = $path; $self->path ($path); |
1236 | $self->{path} = $path; $self->path ($path); |
1199 | $self->init; # pass $1 etc. |
1237 | $self->init; # pass $1 etc. |
1200 | return $self; |
1238 | return $self; |
1201 | } |
1239 | } |
1202 | } |
1240 | } |
1203 | |
1241 | |
1204 | Carp::carp "unable to resolve path '$path'."; |
1242 | Carp::carp "unable to resolve path '$path' (base '$base')."; |
1205 | () |
1243 | () |
1206 | } |
1244 | } |
1207 | |
1245 | |
1208 | sub init { |
1246 | sub init { |
1209 | my ($self) = @_; |
1247 | my ($self) = @_; |
… | |
… | |
1220 | # the displayed name, this is a one way mapping |
1258 | # the displayed name, this is a one way mapping |
1221 | sub visible_name { |
1259 | sub visible_name { |
1222 | &as_string |
1260 | &as_string |
1223 | } |
1261 | } |
1224 | |
1262 | |
1225 | # escape the /'s in the path |
|
|
1226 | sub _escaped_path { |
|
|
1227 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
|
|
1228 | |
|
|
1229 | $path |
|
|
1230 | } |
|
|
1231 | |
|
|
1232 | # the original (read-only) location |
1263 | # the original (read-only) location |
1233 | sub load_path { |
1264 | sub load_path { |
1234 | my ($self) = @_; |
1265 | my ($self) = @_; |
1235 | |
1266 | |
1236 | sprintf "%s/%s/%s", cf::datadir, cf::mapdir, $self->{path} |
1267 | sprintf "%s/%s/%s.map", cf::datadir, cf::mapdir, $self->{path} |
1237 | } |
1268 | } |
1238 | |
1269 | |
1239 | # the temporary/swap location |
1270 | # the temporary/swap location |
1240 | sub save_path { |
1271 | sub save_path { |
1241 | my ($self) = @_; |
1272 | my ($self) = @_; |
1242 | |
1273 | |
|
|
1274 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
1243 | sprintf "%s/%s/%s.map", cf::localdir, cf::tmpdir, $self->_escaped_path |
1275 | sprintf "%s/%s/%s.map", cf::localdir, cf::tmpdir, $path |
1244 | } |
1276 | } |
1245 | |
1277 | |
1246 | # the unique path, undef == no special unique path |
1278 | # the unique path, undef == no special unique path |
1247 | sub uniq_path { |
1279 | sub uniq_path { |
1248 | my ($self) = @_; |
1280 | my ($self) = @_; |
1249 | |
1281 | |
|
|
1282 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
1250 | sprintf "%s/%s/%s", cf::localdir, cf::uniquedir, $self->_escaped_path |
1283 | sprintf "%s/%s/%s", cf::localdir, cf::uniquedir, $path |
1251 | } |
1284 | } |
1252 | |
1285 | |
1253 | # and all this just because we cannot iterate over |
1286 | # and all this just because we cannot iterate over |
1254 | # all maps in C++... |
1287 | # all maps in C++... |
1255 | sub change_all_map_light { |
1288 | sub change_all_map_light { |
… | |
… | |
1261 | |
1294 | |
1262 | sub unlink_save { |
1295 | sub unlink_save { |
1263 | my ($self) = @_; |
1296 | my ($self) = @_; |
1264 | |
1297 | |
1265 | utf8::encode (my $save = $self->save_path); |
1298 | utf8::encode (my $save = $self->save_path); |
1266 | IO::AIO::aioreq_pri 4; IO::AIO::aio_unlink $save; |
1299 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink $save; |
1267 | IO::AIO::aioreq_pri 4; IO::AIO::aio_unlink "$save.pst"; |
1300 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink "$save.pst"; |
|
|
1301 | |
|
|
1302 | #d#TODO remove .map and also nuke |
|
|
1303 | $save =~ s/\.map// or return;#d# |
|
|
1304 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink $save;#d# |
|
|
1305 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink "$save.pst";#d# |
1268 | } |
1306 | } |
1269 | |
1307 | |
1270 | sub load_header_from($) { |
1308 | sub load_header_from($) { |
1271 | my ($self, $path) = @_; |
1309 | my ($self, $path) = @_; |
1272 | |
1310 | |
1273 | utf8::encode $path; |
1311 | utf8::encode $path; |
1274 | aio_open $path, O_RDONLY, 0 |
1312 | #aio_open $path, O_RDONLY, 0 |
1275 | or return; |
1313 | # or return; |
1276 | |
1314 | |
1277 | $self->_load_header ($path) |
1315 | $self->_load_header ($path) |
1278 | or return; |
1316 | or return; |
1279 | |
1317 | |
1280 | $self->{load_path} = $path; |
1318 | $self->{load_path} = $path; |
1281 | |
1319 | |
1282 | 1 |
1320 | 1 |
1283 | } |
1321 | } |
1284 | |
1322 | |
1285 | sub load_orig { |
1323 | sub load_header_orig { |
1286 | my ($self) = @_; |
1324 | my ($self) = @_; |
1287 | |
1325 | |
1288 | $self->load_header_from ($self->load_path) |
1326 | $self->load_header_from ($self->load_path) |
1289 | } |
1327 | } |
1290 | |
1328 | |
1291 | sub load_temp { |
1329 | sub load_header_temp { |
1292 | my ($self) = @_; |
1330 | my ($self) = @_; |
1293 | |
1331 | |
1294 | $self->load_header_from ($self->save_path) |
1332 | $self->load_header_from ($self->save_path) |
|
|
1333 | } |
|
|
1334 | |
|
|
1335 | sub prepare_temp { |
|
|
1336 | my ($self) = @_; |
|
|
1337 | |
|
|
1338 | $self->last_access ((delete $self->{last_access}) |
|
|
1339 | || $cf::RUNTIME); #d# |
|
|
1340 | # safety |
|
|
1341 | $self->{instantiate_time} = $cf::RUNTIME |
|
|
1342 | if $self->{instantiate_time} > $cf::RUNTIME; |
|
|
1343 | } |
|
|
1344 | |
|
|
1345 | sub prepare_orig { |
|
|
1346 | my ($self) = @_; |
|
|
1347 | |
|
|
1348 | $self->{load_original} = 1; |
|
|
1349 | $self->{instantiate_time} = $cf::RUNTIME; |
|
|
1350 | $self->last_access ($cf::RUNTIME); |
|
|
1351 | $self->instantiate; |
1295 | } |
1352 | } |
1296 | |
1353 | |
1297 | sub load_header { |
1354 | sub load_header { |
1298 | my ($self) = @_; |
1355 | my ($self) = @_; |
1299 | |
1356 | |
1300 | if ($self->load_temp) { |
1357 | if ($self->load_header_temp) { |
1301 | $self->last_access ((delete $self->{last_access}) |
1358 | $self->prepare_temp; |
1302 | || $cf::RUNTIME); #d# |
|
|
1303 | # safety |
|
|
1304 | $self->{instantiate_time} = $cf::RUNTIME |
|
|
1305 | if $self->{instantiate_time} > $cf::RUNTIME; |
|
|
1306 | } else { |
1359 | } else { |
1307 | $self->load_orig |
1360 | $self->load_header_orig |
1308 | or return; |
1361 | or return; |
1309 | |
1362 | $self->prepare_orig; |
1310 | $self->{load_original} = 1; |
|
|
1311 | $self->{instantiate_time} = $cf::RUNTIME; |
|
|
1312 | $self->last_access ($cf::RUNTIME); |
|
|
1313 | $self->instantiate; |
|
|
1314 | } |
1363 | } |
1315 | |
1364 | |
1316 | 1 |
1365 | 1 |
1317 | } |
1366 | } |
1318 | |
1367 | |
… | |
… | |
1332 | $map->{last_save} = $cf::RUNTIME; |
1381 | $map->{last_save} = $cf::RUNTIME; |
1333 | |
1382 | |
1334 | $map->load_header |
1383 | $map->load_header |
1335 | or return; |
1384 | or return; |
1336 | |
1385 | |
1337 | if ($map->should_reset) { |
1386 | if ($map->should_reset && 0) {#d#TODO# disabled, crashy (locking issue?) |
|
|
1387 | # doing this can freeze the server in a sync job, obviously |
|
|
1388 | #$cf::WAIT_FOR_TICK->wait; |
1338 | $map->reset; |
1389 | $map->reset; |
1339 | undef $guard; |
1390 | undef $guard; |
1340 | $map = find $path |
1391 | return find $path; |
1341 | or return; |
|
|
1342 | } |
1392 | } |
1343 | |
1393 | |
1344 | $cf::MAP{$path} = $map |
1394 | $cf::MAP{$path} = $map |
1345 | } |
1395 | } |
1346 | } |
1396 | } |
|
|
1397 | |
|
|
1398 | sub pre_load { } |
|
|
1399 | sub post_load { } |
1347 | |
1400 | |
1348 | sub load { |
1401 | sub load { |
1349 | my ($self) = @_; |
1402 | my ($self) = @_; |
1350 | |
1403 | |
|
|
1404 | local $self->{deny_reset} = 1; # loading can take a long time |
|
|
1405 | |
1351 | my $path = $self->{path}; |
1406 | my $path = $self->{path}; |
1352 | my $guard = cf::lock_acquire "map_load:$path"; |
1407 | my $guard = cf::lock_acquire "map_load:$path"; |
1353 | |
1408 | |
1354 | return if $self->in_memory != cf::MAP_SWAPPED; |
1409 | return if $self->in_memory != cf::MAP_SWAPPED; |
1355 | |
1410 | |
1356 | $self->in_memory (cf::MAP_LOADING); |
1411 | $self->in_memory (cf::MAP_LOADING); |
1357 | |
1412 | |
1358 | $self->alloc; |
1413 | $self->alloc; |
|
|
1414 | |
|
|
1415 | $self->pre_load; |
|
|
1416 | |
1359 | $self->_load_objects ($self->{load_path}, 1) |
1417 | $self->_load_objects ($self->{load_path}, 1) |
1360 | or return; |
1418 | or return; |
1361 | |
1419 | |
1362 | $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1) |
1420 | $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1) |
1363 | if delete $self->{load_original}; |
1421 | if delete $self->{load_original}; |
… | |
… | |
1385 | unless $self->difficulty; |
1443 | unless $self->difficulty; |
1386 | Coro::cede; |
1444 | Coro::cede; |
1387 | $self->activate; |
1445 | $self->activate; |
1388 | } |
1446 | } |
1389 | |
1447 | |
|
|
1448 | $self->post_load; |
|
|
1449 | |
1390 | $self->in_memory (cf::MAP_IN_MEMORY); |
1450 | $self->in_memory (cf::MAP_IN_MEMORY); |
1391 | } |
1451 | } |
1392 | |
1452 | |
1393 | sub customise_for { |
1453 | sub customise_for { |
1394 | my ($self, $ob) = @_; |
1454 | my ($self, $ob) = @_; |
… | |
… | |
1434 | |
1494 | |
1435 | cf::sync_job { $map->load }; |
1495 | cf::sync_job { $map->load }; |
1436 | } |
1496 | } |
1437 | |
1497 | |
1438 | our %MAP_PREFETCH; |
1498 | our %MAP_PREFETCH; |
1439 | our $MAP_PREFETCHER = Coro::async { |
1499 | our $MAP_PREFETCHER = undef; |
1440 | while () { |
|
|
1441 | for my $path (keys %MAP_PREFETCH) { |
|
|
1442 | my $map = find $path |
|
|
1443 | or next; |
|
|
1444 | $map->load; |
|
|
1445 | |
|
|
1446 | delete $MAP_PREFETCH{$path}; |
|
|
1447 | } |
|
|
1448 | Coro::schedule; |
|
|
1449 | } |
|
|
1450 | }; |
|
|
1451 | |
1500 | |
1452 | sub find_async { |
1501 | sub find_async { |
1453 | my ($path, $origin) = @_; |
1502 | my ($path, $origin) = @_; |
1454 | |
1503 | |
1455 | $path = normalise $path, $origin && $origin->{path}; |
1504 | $path = normalise $path, $origin && $origin->{path}; |
1456 | |
1505 | |
1457 | if (my $map = $cf::MAP{$path}) { |
1506 | if (my $map = $cf::MAP{$path}) { |
1458 | return $map if $map->in_memory == cf::MAP_IN_MEMORY; |
1507 | return $map if $map->in_memory == cf::MAP_IN_MEMORY; |
1459 | } |
1508 | } |
1460 | |
1509 | |
1461 | $MAP_PREFETCH{$path} = 0; |
1510 | undef $MAP_PREFETCH{$path}; |
|
|
1511 | $MAP_PREFETCHER ||= cf::async { |
|
|
1512 | while (%MAP_PREFETCH) { |
|
|
1513 | for my $path (keys %MAP_PREFETCH) { |
|
|
1514 | my $map = find $path |
|
|
1515 | or next; |
|
|
1516 | $map->load; |
|
|
1517 | |
|
|
1518 | delete $MAP_PREFETCH{$path}; |
|
|
1519 | } |
|
|
1520 | } |
|
|
1521 | undef $MAP_PREFETCHER; |
|
|
1522 | }; |
1462 | $MAP_PREFETCHER->ready; |
1523 | $MAP_PREFETCHER->prio (6); |
1463 | |
1524 | |
1464 | () |
1525 | () |
1465 | } |
1526 | } |
1466 | |
1527 | |
1467 | sub save { |
1528 | sub save { |
… | |
… | |
1512 | |
1573 | |
1513 | sub reset_at { |
1574 | sub reset_at { |
1514 | my ($self) = @_; |
1575 | my ($self) = @_; |
1515 | |
1576 | |
1516 | # TODO: safety, remove and allow resettable per-player maps |
1577 | # TODO: safety, remove and allow resettable per-player maps |
1517 | return 1e99 if $self->isa ("ext::map_per_player"); |
1578 | return 1e99 if $self->isa ("ext::map_per_player");#d# |
1518 | return 1e99 if $self->{deny_reset}; |
1579 | return 1e99 if $self->{deny_reset}; |
1519 | |
1580 | |
1520 | my $time = $self->fixed_resettime ? $self->{instantiate_time} : $self->last_access; |
1581 | my $time = $self->fixed_resettime ? $self->{instantiate_time} : $self->last_access; |
1521 | my $to = List::Util::min $MAX_RESET, $self->reset_timeout || $DEFAULT_RESET; |
1582 | my $to = List::Util::min $MAX_RESET, $self->reset_timeout || $DEFAULT_RESET; |
1522 | |
1583 | |
… | |
… | |
1527 | my ($self) = @_; |
1588 | my ($self) = @_; |
1528 | |
1589 | |
1529 | $self->reset_at <= $cf::RUNTIME |
1590 | $self->reset_at <= $cf::RUNTIME |
1530 | } |
1591 | } |
1531 | |
1592 | |
1532 | sub rename { |
|
|
1533 | my ($self, $new_path) = @_; |
|
|
1534 | |
|
|
1535 | normalise $new_path; |
|
|
1536 | |
|
|
1537 | $self->unlink_save; |
|
|
1538 | |
|
|
1539 | delete $cf::MAP{$self->path}; |
|
|
1540 | $self->{path} = $new_path; $self->path ($self->{path}); |
|
|
1541 | $cf::MAP{$self->path} = $self; |
|
|
1542 | |
|
|
1543 | $self->save; |
|
|
1544 | } |
|
|
1545 | |
|
|
1546 | sub reset { |
1593 | sub reset { |
1547 | my ($self) = @_; |
1594 | my ($self) = @_; |
1548 | |
1595 | |
1549 | my $lock = cf::lock_acquire "map_data:" . $self->path; |
1596 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
1550 | |
1597 | |
1551 | return if $self->players; |
1598 | return if $self->players; |
1552 | return if $self->isa ("ext::map_per_player");#d# |
1599 | return if $self->isa ("ext::map_per_player");#d# |
1553 | |
1600 | |
1554 | warn "resetting map ", $self->path;#d# |
1601 | warn "resetting map ", $self->path;#d# |
1555 | |
1602 | |
1556 | delete $cf::MAP{$self->path}; |
1603 | delete $cf::MAP{$self->path}; |
1557 | |
1604 | |
|
|
1605 | $self->in_memory (cf::MAP_SWAPPED); |
|
|
1606 | $self->clear; |
|
|
1607 | |
1558 | $_->clear_links_to ($self) for values %cf::MAP; |
1608 | $_->clear_links_to ($self) for values %cf::MAP; |
1559 | |
1609 | |
1560 | $self->unlink_save; |
1610 | $self->unlink_save; |
1561 | $self->destroy; |
1611 | $self->destroy; |
1562 | } |
1612 | } |
… | |
… | |
1564 | my $nuke_counter = "aaaa"; |
1614 | my $nuke_counter = "aaaa"; |
1565 | |
1615 | |
1566 | sub nuke { |
1616 | sub nuke { |
1567 | my ($self) = @_; |
1617 | my ($self) = @_; |
1568 | |
1618 | |
|
|
1619 | delete $cf::MAP{$self->path}; |
|
|
1620 | |
|
|
1621 | $self->unlink_save; |
|
|
1622 | |
|
|
1623 | bless $self, "cf::map"; |
|
|
1624 | delete $self->{deny_reset}; |
1569 | $self->{deny_save} = 1; |
1625 | $self->{deny_save} = 1; |
1570 | $self->reset_timeout (1); |
1626 | $self->reset_timeout (1); |
1571 | $self->rename ("{nuke}/" . ($nuke_counter++)); |
1627 | $self->path ($self->{path} = "{nuke}/" . ($nuke_counter++)); |
|
|
1628 | |
|
|
1629 | $cf::MAP{$self->path} = $self; |
|
|
1630 | |
1572 | $self->reset; # polite request, might not happen |
1631 | $self->reset; # polite request, might not happen |
1573 | } |
1632 | } |
1574 | |
1633 | |
1575 | =item cf::map::unique_maps |
1634 | =item cf::map::unique_maps |
1576 | |
1635 | |
… | |
… | |
1588 | for (@$files) { |
1647 | for (@$files) { |
1589 | utf8::decode $_; |
1648 | utf8::decode $_; |
1590 | next if /\.pst$/; |
1649 | next if /\.pst$/; |
1591 | next unless /^$PATH_SEP/o; |
1650 | next unless /^$PATH_SEP/o; |
1592 | |
1651 | |
1593 | push @paths, $_; |
1652 | push @paths, cf::map::normalise $_; |
1594 | } |
1653 | } |
1595 | |
1654 | |
1596 | \@paths |
1655 | \@paths |
1597 | } |
1656 | } |
1598 | |
1657 | |
… | |
… | |
1692 | =cut |
1751 | =cut |
1693 | |
1752 | |
1694 | sub link_map { |
1753 | sub link_map { |
1695 | unless ($LINK_MAP) { |
1754 | unless ($LINK_MAP) { |
1696 | $LINK_MAP = cf::map::find "{link}" |
1755 | $LINK_MAP = cf::map::find "{link}" |
1697 | or do { warn "FATAL: unable to provide {link} map, exiting."; exit 1 }; |
1756 | or cf::cleanup "FATAL: unable to provide {link} map, exiting."; |
1698 | $LINK_MAP->load; |
1757 | $LINK_MAP->load; |
1699 | } |
1758 | } |
1700 | |
1759 | |
1701 | $LINK_MAP |
1760 | $LINK_MAP |
1702 | } |
1761 | } |
… | |
… | |
1709 | return if UNIVERSAL::isa $self->map, "ext::map_link"; |
1768 | return if UNIVERSAL::isa $self->map, "ext::map_link"; |
1710 | |
1769 | |
1711 | $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y] |
1770 | $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y] |
1712 | if $self->map; |
1771 | if $self->map; |
1713 | |
1772 | |
1714 | $self->enter_map ($LINK_MAP || link_map, 20, 20); |
1773 | $self->enter_map ($LINK_MAP || link_map, 10, 10); |
1715 | } |
1774 | } |
1716 | |
1775 | |
1717 | sub cf::object::player::leave_link { |
1776 | sub cf::object::player::leave_link { |
1718 | my ($self, $map, $x, $y) = @_; |
1777 | my ($self, $map, $x, $y) = @_; |
1719 | |
1778 | |
… | |
… | |
1786 | my ($self, $path, $x, $y) = @_; |
1845 | my ($self, $path, $x, $y) = @_; |
1787 | |
1846 | |
1788 | $self->enter_link; |
1847 | $self->enter_link; |
1789 | |
1848 | |
1790 | (async { |
1849 | (async { |
|
|
1850 | my $map = eval { |
1791 | my $map = cf::map::find $path; |
1851 | my $map = cf::map::find $path; |
1792 | $map = $map->customise_for ($self) if $map; |
1852 | $map = $map->customise_for ($self) if $map; |
1793 | |
1853 | $map |
1794 | # warn "entering ", $map->path, " at ($x, $y)\n" |
1854 | } or |
1795 | # if $map; |
|
|
1796 | |
|
|
1797 | $map or $self->message ("The exit to '" . ($path->visible_name) . "' is closed", cf::NDI_UNIQUE | cf::NDI_RED); |
1855 | $self->message ("The exit to '$path' is closed", cf::NDI_UNIQUE | cf::NDI_RED); |
1798 | |
1856 | |
1799 | $self->leave_link ($map, $x, $y); |
1857 | $self->leave_link ($map, $x, $y); |
1800 | })->prio (1); |
1858 | })->prio (1); |
1801 | } |
1859 | } |
1802 | |
1860 | |
… | |
… | |
1806 | |
1864 | |
1807 | sub parse_random_map_params { |
1865 | sub parse_random_map_params { |
1808 | my ($spec) = @_; |
1866 | my ($spec) = @_; |
1809 | |
1867 | |
1810 | my $rmp = { # defaults |
1868 | my $rmp = { # defaults |
1811 | xsize => 10, |
1869 | xsize => (cf::rndm 15, 40), |
1812 | ysize => 10, |
1870 | ysize => (cf::rndm 15, 40), |
|
|
1871 | symmetry => (cf::rndm 1, cf::SYMMETRY_XY), |
|
|
1872 | #layout => string, |
1813 | }; |
1873 | }; |
1814 | |
1874 | |
1815 | for (split /\n/, $spec) { |
1875 | for (split /\n/, $spec) { |
1816 | my ($k, $v) = split /\s+/, $_, 2; |
1876 | my ($k, $v) = split /\s+/, $_, 2; |
1817 | |
1877 | |
… | |
… | |
1821 | $rmp |
1881 | $rmp |
1822 | } |
1882 | } |
1823 | |
1883 | |
1824 | sub prepare_random_map { |
1884 | sub prepare_random_map { |
1825 | my ($exit) = @_; |
1885 | my ($exit) = @_; |
|
|
1886 | |
|
|
1887 | my $guard = cf::lock_acquire "exit_prepare:$exit"; |
1826 | |
1888 | |
1827 | # all this does is basically replace the /! path by |
1889 | # all this does is basically replace the /! path by |
1828 | # a new random map path (?random/...) with a seed |
1890 | # a new random map path (?random/...) with a seed |
1829 | # that depends on the exit object |
1891 | # that depends on the exit object |
1830 | |
1892 | |
1831 | my $rmp = parse_random_map_params $exit->msg; |
1893 | my $rmp = parse_random_map_params $exit->msg; |
1832 | |
1894 | |
1833 | if ($exit->map) { |
1895 | if ($exit->map) { |
1834 | $rmp->{region} = $exit->map->region_name; |
1896 | $rmp->{region} = $exit->region->name; |
1835 | $rmp->{origin_map} = $exit->map->path; |
1897 | $rmp->{origin_map} = $exit->map->path; |
1836 | $rmp->{origin_x} = $exit->x; |
1898 | $rmp->{origin_x} = $exit->x; |
1837 | $rmp->{origin_y} = $exit->y; |
1899 | $rmp->{origin_y} = $exit->y; |
1838 | } |
1900 | } |
1839 | |
1901 | |
1840 | $rmp->{random_seed} ||= $exit->random_seed; |
1902 | $rmp->{random_seed} ||= $exit->random_seed; |
1841 | |
1903 | |
1842 | my $data = cf::to_json $rmp; |
1904 | my $data = cf::to_json $rmp; |
1843 | my $md5 = Digest::MD5::md5_hex $data; |
1905 | my $md5 = Digest::MD5::md5_hex $data; |
|
|
1906 | my $meta = "$cf::RANDOM_MAPS/$md5.meta"; |
1844 | |
1907 | |
1845 | if (my $fh = aio_open "$cf::RANDOM_MAPS/$md5.meta", O_WRONLY | O_CREAT, 0666) { |
1908 | if (my $fh = aio_open "$meta~", O_WRONLY | O_CREAT, 0666) { |
1846 | aio_write $fh, 0, (length $data), $data, 0; |
1909 | aio_write $fh, 0, (length $data), $data, 0; |
|
|
1910 | undef $fh; |
|
|
1911 | aio_rename "$meta~", $meta; |
1847 | |
1912 | |
1848 | $exit->slaying ("?random/$md5"); |
1913 | $exit->slaying ("?random/$md5"); |
1849 | $exit->msg (undef); |
1914 | $exit->msg (undef); |
1850 | } |
1915 | } |
1851 | } |
1916 | } |
1852 | |
1917 | |
1853 | sub cf::object::player::enter_exit { |
1918 | sub cf::object::player::enter_exit { |
1854 | my ($self, $exit) = @_; |
1919 | my ($self, $exit) = @_; |
1855 | |
1920 | |
1856 | return unless $self->type == cf::PLAYER; |
1921 | return unless $self->type == cf::PLAYER; |
|
|
1922 | |
|
|
1923 | if ($exit->slaying eq "/!") { |
|
|
1924 | #TODO: this should de-fi-ni-te-ly not be a sync-job |
|
|
1925 | cf::sync_job { prepare_random_map $exit }; |
|
|
1926 | } |
|
|
1927 | |
|
|
1928 | my $slaying = cf::map::normalise $exit->slaying, $exit->map && $exit->map->path; |
|
|
1929 | my $hp = $exit->stats->hp; |
|
|
1930 | my $sp = $exit->stats->sp; |
1857 | |
1931 | |
1858 | $self->enter_link; |
1932 | $self->enter_link; |
1859 | |
1933 | |
1860 | (async { |
1934 | (async { |
1861 | $self->deactivate_recursive; # just to be sure |
1935 | $self->deactivate_recursive; # just to be sure |
1862 | unless (eval { |
1936 | unless (eval { |
1863 | prepare_random_map $exit |
1937 | $self->goto ($slaying, $hp, $sp); |
1864 | if $exit->slaying eq "/!"; |
|
|
1865 | |
|
|
1866 | my $path = new_from_path cf::map $exit->slaying, $exit->map && $exit->map->path; |
|
|
1867 | $self->goto ($path, $exit->stats->hp, $exit->stats->sp); |
|
|
1868 | |
1938 | |
1869 | 1; |
1939 | 1; |
1870 | }) { |
1940 | }) { |
1871 | $self->message ("Something went wrong deep within the crossfire server. " |
1941 | $self->message ("Something went wrong deep within the crossfire server. " |
1872 | . "I'll try to bring you back to the map you were before. " |
1942 | . "I'll try to bring you back to the map you were before. " |
… | |
… | |
1933 | # get handled first |
2003 | # get handled first |
1934 | my $queue = delete $ns->{query_queue} |
2004 | my $queue = delete $ns->{query_queue} |
1935 | or return; # be conservative, not sure how that can happen, but we saw a crash here |
2005 | or return; # be conservative, not sure how that can happen, but we saw a crash here |
1936 | |
2006 | |
1937 | (shift @$queue)->[1]->($msg); |
2007 | (shift @$queue)->[1]->($msg); |
|
|
2008 | return unless $ns->valid; # temporary(?) workaround for callback destroying socket |
1938 | |
2009 | |
1939 | push @{ $ns->{query_queue} }, @$queue; |
2010 | push @{ $ns->{query_queue} }, @$queue; |
1940 | |
2011 | |
1941 | if (@{ $ns->{query_queue} } == @$queue) { |
2012 | if (@{ $ns->{query_queue} } == @$queue) { |
1942 | if (@$queue) { |
2013 | if (@$queue) { |
… | |
… | |
2161 | sub db_sync() { |
2232 | sub db_sync() { |
2162 | db_save if $dirty; |
2233 | db_save if $dirty; |
2163 | undef $dirty; |
2234 | undef $dirty; |
2164 | } |
2235 | } |
2165 | |
2236 | |
2166 | my $idle = Event->idle (min => $TICK * 2.8, max => 10, repeat => 0, data => WF_AUTOCANCEL, cb => sub { |
2237 | my $idle = Event->idle ( |
2167 | db_sync; |
2238 | reentrant => 0, |
|
|
2239 | min => 10, |
|
|
2240 | max => 20, |
|
|
2241 | repeat => 0, |
|
|
2242 | data => WF_AUTOCANCEL, |
|
|
2243 | cb => \&db_sync, |
2168 | }); |
2244 | ); |
2169 | |
2245 | |
2170 | sub db_dirty() { |
2246 | sub db_dirty() { |
2171 | $dirty = 1; |
2247 | $dirty = 1; |
2172 | $idle->start; |
2248 | $idle->start; |
2173 | } |
2249 | } |
… | |
… | |
2221 | |
2297 | |
2222 | sub main { |
2298 | sub main { |
2223 | # we must not ever block the main coroutine |
2299 | # we must not ever block the main coroutine |
2224 | local $Coro::idle = sub { |
2300 | local $Coro::idle = sub { |
2225 | Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# |
2301 | Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# |
|
|
2302 | (async { |
2226 | async { Event::one_event }; |
2303 | Event::one_event; |
|
|
2304 | })->prio (Coro::PRIO_MAX); |
2227 | }; |
2305 | }; |
2228 | |
2306 | |
2229 | cfg_load; |
2307 | cfg_load; |
2230 | db_load; |
2308 | db_load; |
2231 | load_extensions; |
2309 | load_extensions; |
|
|
2310 | |
|
|
2311 | $TICK_WATCHER->start; |
2232 | Event::loop; |
2312 | Event::loop; |
2233 | } |
2313 | } |
2234 | |
2314 | |
2235 | ############################################################################# |
2315 | ############################################################################# |
2236 | # initialisation and cleanup |
2316 | # initialisation and cleanup |
2237 | |
2317 | |
2238 | # install some emergency cleanup handlers |
2318 | # install some emergency cleanup handlers |
2239 | BEGIN { |
2319 | BEGIN { |
2240 | for my $signal (qw(INT HUP TERM)) { |
2320 | for my $signal (qw(INT HUP TERM)) { |
2241 | Event->signal ( |
2321 | Event->signal ( |
|
|
2322 | reentrant => 0, |
2242 | data => WF_AUTOCANCEL, |
2323 | data => WF_AUTOCANCEL, |
2243 | signal => $signal, |
2324 | signal => $signal, |
|
|
2325 | prio => 0, |
2244 | cb => sub { |
2326 | cb => sub { |
2245 | cf::cleanup "SIG$signal"; |
2327 | cf::cleanup "SIG$signal"; |
2246 | }, |
2328 | }, |
2247 | ); |
2329 | ); |
2248 | } |
2330 | } |
2249 | } |
2331 | } |
… | |
… | |
2254 | warn "enter emergency perl save\n"; |
2336 | warn "enter emergency perl save\n"; |
2255 | |
2337 | |
2256 | cf::sync_job { |
2338 | cf::sync_job { |
2257 | # use a peculiar iteration method to avoid tripping on perl |
2339 | # use a peculiar iteration method to avoid tripping on perl |
2258 | # refcount bugs in for. also avoids problems with players |
2340 | # refcount bugs in for. also avoids problems with players |
2259 | # and maps saved/Destroyed asynchronously. |
2341 | # and maps saved/destroyed asynchronously. |
2260 | warn "begin emergency player save\n"; |
2342 | warn "begin emergency player save\n"; |
2261 | for my $login (keys %cf::PLAYER) { |
2343 | for my $login (keys %cf::PLAYER) { |
2262 | my $pl = $cf::PLAYER{$login} or next; |
2344 | my $pl = $cf::PLAYER{$login} or next; |
2263 | $pl->valid or next; |
2345 | $pl->valid or next; |
2264 | $pl->save; |
2346 | $pl->save; |
… | |
… | |
2278 | } |
2360 | } |
2279 | |
2361 | |
2280 | sub reload() { |
2362 | sub reload() { |
2281 | # can/must only be called in main |
2363 | # can/must only be called in main |
2282 | if ($Coro::current != $Coro::main) { |
2364 | if ($Coro::current != $Coro::main) { |
2283 | warn "can only reload from main coroutine\n"; |
2365 | warn "can only reload from main coroutine"; |
2284 | return; |
2366 | return; |
2285 | } |
2367 | } |
2286 | |
2368 | |
2287 | warn "reloading..."; |
2369 | warn "reloading..."; |
2288 | |
2370 | |
2289 | warn "freezing server"; |
2371 | warn "cancelling server ticker"; |
2290 | my $guard = freeze_mainloop; |
2372 | $TICK_WATCHER->cancel; |
|
|
2373 | |
2291 | cf::emergency_save; |
2374 | cf::emergency_save; |
2292 | |
|
|
2293 | warn "sync database to disk"; |
|
|
2294 | cf::db_sync; |
|
|
2295 | IO::AIO::flush; |
|
|
2296 | |
2375 | |
2297 | eval { |
2376 | eval { |
2298 | # if anything goes wrong in here, we should simply crash as we already saved |
2377 | # if anything goes wrong in here, we should simply crash as we already saved |
2299 | |
2378 | |
|
|
2379 | warn "syncing database to disk"; |
|
|
2380 | cf::db_sync; |
|
|
2381 | |
2300 | warn "cancel all watchers"; |
2382 | warn "cancelling all WF_AUTOCANCEL watchers"; |
2301 | for (Event::all_watchers) { |
2383 | for (Event::all_watchers) { |
2302 | $_->cancel if $_->data & WF_AUTOCANCEL; |
2384 | $_->cancel if $_->data & WF_AUTOCANCEL; |
2303 | } |
2385 | } |
2304 | |
2386 | |
|
|
2387 | warn "flushing outstanding aio requests"; |
|
|
2388 | for (;;) { |
|
|
2389 | IO::AIO::flush; |
|
|
2390 | Coro::cede; |
|
|
2391 | last unless IO::AIO::nreqs; |
|
|
2392 | warn "iterate..."; |
|
|
2393 | } |
|
|
2394 | |
2305 | warn "cancel all extension coros"; |
2395 | warn "cancelling all extension coros"; |
2306 | $_->cancel for values %EXT_CORO; |
2396 | $_->cancel for values %EXT_CORO; |
2307 | %EXT_CORO = (); |
2397 | %EXT_CORO = (); |
2308 | |
2398 | |
2309 | warn "remove commands"; |
2399 | warn "removing commands"; |
2310 | %COMMAND = (); |
2400 | %COMMAND = (); |
2311 | |
2401 | |
2312 | warn "remove ext commands"; |
2402 | warn "removing ext commands"; |
2313 | %EXTCMD = (); |
2403 | %EXTCMD = (); |
2314 | |
2404 | |
2315 | warn "unload/nuke all extensions"; |
2405 | warn "unloading/nuking all extensions"; |
2316 | for my $pkg (@EXTS) { |
2406 | for my $pkg (@EXTS) { |
2317 | warn "... unloading $pkg"; |
2407 | warn "... unloading $pkg"; |
2318 | |
2408 | |
2319 | if (my $cb = $pkg->can ("unload")) { |
2409 | if (my $cb = $pkg->can ("unload")) { |
2320 | eval { |
2410 | eval { |
… | |
… | |
2325 | |
2415 | |
2326 | warn "... nuking $pkg"; |
2416 | warn "... nuking $pkg"; |
2327 | Symbol::delete_package $pkg; |
2417 | Symbol::delete_package $pkg; |
2328 | } |
2418 | } |
2329 | |
2419 | |
2330 | warn "unload all perl modules loaded from $LIBDIR"; |
2420 | warn "unloading all perl modules loaded from $LIBDIR"; |
2331 | while (my ($k, $v) = each %INC) { |
2421 | while (my ($k, $v) = each %INC) { |
2332 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
2422 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
2333 | |
2423 | |
2334 | warn "removing <$k>"; |
2424 | warn "... unloading $k"; |
2335 | delete $INC{$k}; |
2425 | delete $INC{$k}; |
2336 | |
2426 | |
2337 | $k =~ s/\.pm$//; |
2427 | $k =~ s/\.pm$//; |
2338 | $k =~ s/\//::/g; |
2428 | $k =~ s/\//::/g; |
2339 | |
2429 | |
… | |
… | |
2342 | } |
2432 | } |
2343 | |
2433 | |
2344 | Symbol::delete_package $k; |
2434 | Symbol::delete_package $k; |
2345 | } |
2435 | } |
2346 | |
2436 | |
2347 | warn "get rid of safe::, as good as possible"; |
2437 | warn "getting rid of safe::, as good as possible"; |
2348 | Symbol::delete_package "safe::$_" |
2438 | Symbol::delete_package "safe::$_" |
2349 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
2439 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
2350 | |
2440 | |
2351 | warn "unload cf.pm \"a bit\""; |
2441 | warn "unloading cf.pm \"a bit\""; |
2352 | delete $INC{"cf.pm"}; |
2442 | delete $INC{"cf.pm"}; |
2353 | |
2443 | |
2354 | # don't, removes xs symbols, too, |
2444 | # don't, removes xs symbols, too, |
2355 | # and global variables created in xs |
2445 | # and global variables created in xs |
2356 | #Symbol::delete_package __PACKAGE__; |
2446 | #Symbol::delete_package __PACKAGE__; |
2357 | |
2447 | |
|
|
2448 | warn "unload completed, starting to reload now"; |
|
|
2449 | |
2358 | warn "reloading cf.pm"; |
2450 | warn "reloading cf.pm"; |
2359 | require cf; |
2451 | require cf; |
2360 | cf::_connect_to_perl; # nominally unnecessary, but cannot hurt |
2452 | cf::_connect_to_perl; # nominally unnecessary, but cannot hurt |
2361 | |
2453 | |
2362 | warn "load config and database again"; |
2454 | warn "loading config and database again"; |
2363 | cf::cfg_load; |
2455 | cf::cfg_load; |
2364 | cf::db_load; |
2456 | cf::db_load; |
2365 | |
2457 | |
2366 | warn "load extensions"; |
2458 | warn "loading extensions"; |
2367 | cf::load_extensions; |
2459 | cf::load_extensions; |
2368 | |
2460 | |
2369 | warn "reattach attachments to objects/players"; |
2461 | warn "reattaching attachments to objects/players"; |
2370 | _global_reattach; |
2462 | _global_reattach; |
2371 | warn "reattach attachments to maps"; |
2463 | warn "reattaching attachments to maps"; |
2372 | reattach $_ for values %MAP; |
2464 | reattach $_ for values %MAP; |
|
|
2465 | |
|
|
2466 | warn "restarting server ticker"; |
|
|
2467 | |
|
|
2468 | $TICK_WATCHER->start; |
2373 | }; |
2469 | }; |
2374 | |
2470 | |
2375 | if ($@) { |
2471 | if ($@) { |
2376 | warn $@; |
2472 | warn $@; |
2377 | warn "error while reloading, exiting."; |
2473 | warn "error while reloading, exiting."; |
… | |
… | |
2379 | } |
2475 | } |
2380 | |
2476 | |
2381 | warn "reloaded"; |
2477 | warn "reloaded"; |
2382 | }; |
2478 | }; |
2383 | |
2479 | |
|
|
2480 | our $RELOAD_WATCHER; # used only during reload |
|
|
2481 | |
2384 | register_command "reload" => sub { |
2482 | register_command "reload" => sub { |
2385 | my ($who, $arg) = @_; |
2483 | my ($who, $arg) = @_; |
2386 | |
2484 | |
2387 | if ($who->flag (FLAG_WIZ)) { |
2485 | if ($who->flag (FLAG_WIZ)) { |
2388 | $who->message ("start of reload."); |
2486 | $who->message ("reloading server."); |
|
|
2487 | |
|
|
2488 | # doing reload synchronously and two reloads happen back-to-back, |
|
|
2489 | # coro crashes during coro_state_free->destroy here. |
|
|
2490 | |
|
|
2491 | $RELOAD_WATCHER ||= Event->timer ( |
|
|
2492 | reentrant => 0, |
|
|
2493 | after => 0, |
|
|
2494 | data => WF_AUTOCANCEL, |
|
|
2495 | cb => sub { |
2389 | reload; |
2496 | reload; |
2390 | $who->message ("end of reload."); |
2497 | undef $RELOAD_WATCHER; |
|
|
2498 | }, |
|
|
2499 | ); |
2391 | } |
2500 | } |
2392 | }; |
2501 | }; |
2393 | |
2502 | |
2394 | unshift @INC, $LIBDIR; |
2503 | unshift @INC, $LIBDIR; |
|
|
2504 | |
|
|
2505 | my $bug_warning = 0; |
2395 | |
2506 | |
2396 | $TICK_WATCHER = Event->timer ( |
2507 | $TICK_WATCHER = Event->timer ( |
2397 | reentrant => 0, |
2508 | reentrant => 0, |
|
|
2509 | parked => 1, |
2398 | prio => 0, |
2510 | prio => 0, |
2399 | at => $NEXT_TICK || $TICK, |
2511 | at => $NEXT_TICK || $TICK, |
2400 | data => WF_AUTOCANCEL, |
2512 | data => WF_AUTOCANCEL, |
2401 | cb => sub { |
2513 | cb => sub { |
|
|
2514 | if ($Coro::current != $Coro::main) { |
|
|
2515 | Carp::cluck "major BUG: server tick called outside of main coro, skipping it" |
|
|
2516 | unless ++$bug_warning > 10; |
|
|
2517 | return; |
|
|
2518 | } |
|
|
2519 | |
2402 | $NOW = Event::time; |
2520 | $NOW = Event::time; |
2403 | |
2521 | |
2404 | cf::server_tick; # one server iteration |
2522 | cf::server_tick; # one server iteration |
2405 | $RUNTIME += $TICK; |
2523 | $RUNTIME += $TICK; |
2406 | $NEXT_TICK += $TICK; |
2524 | $NEXT_TICK += $TICK; |
2407 | |
2525 | |
2408 | $WAIT_FOR_TICK->broadcast; |
2526 | $WAIT_FOR_TICK->broadcast; |
2409 | $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited; |
2527 | $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited; |
|
|
2528 | |
|
|
2529 | Event::sweep; |
|
|
2530 | Coro::cede_notself; |
|
|
2531 | |
|
|
2532 | # my $AFTER = Event::time; |
|
|
2533 | # warn $AFTER - $NOW;#d# |
2410 | |
2534 | |
2411 | # if we are delayed by four ticks or more, skip them all |
2535 | # if we are delayed by four ticks or more, skip them all |
2412 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
2536 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
2413 | |
2537 | |
2414 | $TICK_WATCHER->at ($NEXT_TICK); |
2538 | $TICK_WATCHER->at ($NEXT_TICK); |
2415 | $TICK_WATCHER->start; |
2539 | $TICK_WATCHER->start; |
2416 | }, |
2540 | }, |
2417 | ); |
2541 | ); |
2418 | |
2542 | |
2419 | IO::AIO::max_poll_time $TICK * 0.2; |
2543 | IO::AIO::max_poll_time $TICK * 0.1; |
2420 | |
2544 | |
2421 | Event->io ( |
2545 | undef $Coro::AIO::WATCHER; |
|
|
2546 | $AIO_POLL_WATCHER = Event->io ( |
|
|
2547 | reentrant => 0, |
2422 | fd => IO::AIO::poll_fileno, |
2548 | fd => IO::AIO::poll_fileno, |
2423 | poll => 'r', |
2549 | poll => 'r', |
2424 | prio => 5, |
2550 | prio => 6, |
2425 | data => WF_AUTOCANCEL, |
2551 | data => WF_AUTOCANCEL, |
2426 | cb => \&IO::AIO::poll_cb, |
2552 | cb => \&IO::AIO::poll_cb, |
2427 | ); |
2553 | ); |
2428 | |
2554 | |
2429 | Event->timer ( |
2555 | $WRITE_RUNTIME_WATCHER = Event->timer ( |
|
|
2556 | reentrant => 0, |
2430 | data => WF_AUTOCANCEL, |
2557 | data => WF_AUTOCANCEL, |
2431 | after => 0, |
2558 | after => 1, |
2432 | interval => 10, |
2559 | interval => 10, |
2433 | cb => sub { |
2560 | prio => 6, # keep it lowest so it acts like a watchdog |
2434 | (Coro::unblock_sub { |
2561 | cb => Coro::unblock_sub { |
2435 | write_runtime |
2562 | write_runtime |
2436 | or warn "ERROR: unable to write runtime file: $!"; |
2563 | or warn "ERROR: unable to write runtime file: $!"; |
2437 | })->(); |
|
|
2438 | }, |
2564 | }, |
2439 | ); |
2565 | ); |
2440 | |
2566 | |
2441 | END { cf::emergency_save } |
2567 | END { cf::emergency_save } |
2442 | |
2568 | |