… | |
… | |
33 | |
33 | |
34 | $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 |
35 | |
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 |
|
|
42 | our %EXT_MAP = (); # pluggable maps |
39 | |
43 | |
40 | our @EVENT; |
44 | our @EVENT; |
41 | our $LIBDIR = datadir . "/ext"; |
45 | our $LIBDIR = datadir . "/ext"; |
42 | |
46 | |
43 | our $TICK = MAX_TIME * 1e-6; |
47 | our $TICK = MAX_TIME * 1e-6; |
… | |
… | |
52 | |
56 | |
53 | our %PLAYER; # all users |
57 | our %PLAYER; # all users |
54 | our %MAP; # all maps |
58 | our %MAP; # all maps |
55 | our $LINK_MAP; # the special {link} map |
59 | our $LINK_MAP; # the special {link} map |
56 | our $RANDOM_MAPS = cf::localdir . "/random"; |
60 | our $RANDOM_MAPS = cf::localdir . "/random"; |
57 | our %EXT_CORO; # coroutines bound to extensions |
|
|
58 | |
61 | |
59 | our $WAIT_FOR_TICK; $WAIT_FOR_TICK ||= new Coro::Signal; |
62 | our $WAIT_FOR_TICK; $WAIT_FOR_TICK ||= new Coro::Signal; |
60 | our $WAIT_FOR_TICK_ONE; $WAIT_FOR_TICK_ONE ||= new Coro::Signal; |
63 | our $WAIT_FOR_TICK_ONE; $WAIT_FOR_TICK_ONE ||= new Coro::Signal; |
61 | |
64 | |
62 | binmode STDOUT; |
65 | binmode STDOUT; |
… | |
… | |
156 | } |
159 | } |
157 | |
160 | |
158 | $Event::DIED = sub { |
161 | $Event::DIED = sub { |
159 | warn "error in event callback: @_"; |
162 | warn "error in event callback: @_"; |
160 | }; |
163 | }; |
161 | |
|
|
162 | my %ext_pkg; |
|
|
163 | my @exts; |
|
|
164 | my @hook; |
|
|
165 | |
164 | |
166 | =head2 UTILITY FUNCTIONS |
165 | =head2 UTILITY FUNCTIONS |
167 | |
166 | |
168 | =over 4 |
167 | =over 4 |
169 | |
168 | |
… | |
… | |
319 | } |
318 | } |
320 | } |
319 | } |
321 | |
320 | |
322 | =item $coro = cf::async_ext { BLOCK } |
321 | =item $coro = cf::async_ext { BLOCK } |
323 | |
322 | |
324 | Like async, but this coro is automcatially being canceled when the |
323 | Like async, but this coro is automatically being canceled when the |
325 | extension calling this is being unloaded. |
324 | extension calling this is being unloaded. |
326 | |
325 | |
327 | =cut |
326 | =cut |
328 | |
327 | |
329 | sub async_ext(&) { |
328 | sub async_ext(&) { |
… | |
… | |
367 | |
366 | |
368 | ############################################################################# |
367 | ############################################################################# |
369 | |
368 | |
370 | package cf::path; |
369 | package cf::path; |
371 | |
370 | |
|
|
371 | use overload |
|
|
372 | '""' => \&as_string; |
|
|
373 | |
372 | # used to convert map paths into valid unix filenames by repalcing / by ∕ |
374 | # used to convert map paths into valid unix filenames by repalcing / by ∕ |
373 | our $PATH_SEP = "∕"; # U+2215, chosen purely for visual reasons |
375 | our $PATH_SEP = "∕"; # U+2215, chosen purely for visual reasons |
374 | |
376 | |
|
|
377 | sub register { |
|
|
378 | my ($pkg, $prefix) = @_; |
|
|
379 | |
|
|
380 | $EXT_MAP{$prefix} = $pkg; |
|
|
381 | } |
|
|
382 | |
375 | sub new { |
383 | sub new { |
376 | my ($class, $path, $base) = @_; |
384 | my ($class, $path, $base) = @_; |
377 | |
385 | |
378 | $path = $path->as_string if ref $path; |
386 | return $path if ref $path; |
379 | |
387 | |
380 | my $self = bless { }, $class; |
388 | my $self = {}; |
381 | |
389 | |
382 | # {... are special paths that are not touched |
390 | # {... are special paths that are not being touched |
383 | # ?xxx/... are special absolute paths |
391 | # ?xxx/... are special absolute paths |
384 | # ?random/... random maps |
392 | # ?random/... random maps |
385 | # /! non-realised random map exit |
393 | # /! non-realised random map exit |
386 | # /... normal maps |
394 | # /... normal maps |
387 | # ~/... per-player maps without a specific player (DO NOT USE) |
395 | # ~/... per-player maps without a specific player (DO NOT USE) |
388 | # ~user/... per-player map of a specific user |
396 | # ~user/... per-player map of a specific user |
389 | |
397 | |
|
|
398 | $path =~ s/$PATH_SEP/\//go; |
|
|
399 | |
390 | if ($path =~ /^{/) { |
400 | if ($path =~ /^{/) { |
391 | # fine as it is |
401 | # fine as it is |
392 | } elsif ($path =~ s{^\?random/}{}) { |
|
|
393 | Coro::AIO::aio_load "$cf::RANDOM_MAPS/$path.meta", my $data; |
|
|
394 | $self->{random} = cf::from_json $data; |
|
|
395 | } else { |
402 | } else { |
396 | if ($path =~ s{^~([^/]+)?}{}) { |
403 | if ($path =~ s{^~([^/]+)?}{}) { |
|
|
404 | # ~user |
397 | $self->{user_rel} = 1; |
405 | $self->{user_rel} = 1; |
398 | |
406 | |
399 | if (defined $1) { |
407 | if (defined $1) { |
400 | $self->{user} = $1; |
408 | $self->{user} = $1; |
401 | } elsif ($base =~ m{^~([^/]+)/}) { |
409 | } elsif ($base =~ m{^~([^/]+)/}) { |
402 | $self->{user} = $1; |
410 | $self->{user} = $1; |
403 | } else { |
411 | } else { |
404 | warn "cannot resolve user-relative path without user <$path,$base>\n"; |
412 | warn "cannot resolve user-relative path without user <$path,$base>\n"; |
405 | } |
413 | } |
|
|
414 | } elsif ($path =~ s{^\?([^/]+)/}{}) { |
|
|
415 | # ?... |
|
|
416 | $self->{ext} = $1; |
|
|
417 | if (my $ext = $EXT_MAP{$1}) { |
|
|
418 | bless $self, $ext; |
|
|
419 | } |
406 | } elsif ($path =~ /^\//) { |
420 | } elsif ($path =~ /^\//) { |
|
|
421 | # /... |
407 | # already absolute |
422 | # already absolute |
408 | } else { |
423 | } else { |
|
|
424 | # relative |
409 | $base =~ s{[^/]+/?$}{}; |
425 | $base =~ s{[^/]+/?$}{}; |
410 | return $class->new ("$base/$path"); |
426 | return $class->new ("$base/$path"); |
411 | } |
427 | } |
412 | |
428 | |
413 | for ($path) { |
429 | for ($path) { |
… | |
… | |
416 | } |
432 | } |
417 | } |
433 | } |
418 | |
434 | |
419 | $self->{path} = $path; |
435 | $self->{path} = $path; |
420 | |
436 | |
|
|
437 | if ("HASH" eq ref $self) { |
|
|
438 | bless $self, $class; |
|
|
439 | } else { |
|
|
440 | $self->init; |
|
|
441 | } |
|
|
442 | |
|
|
443 | for my $ext (values %EXT_MAP) { |
|
|
444 | if (my $subst = $ext->substitute ($self)) { |
|
|
445 | return $subst; |
|
|
446 | } |
|
|
447 | } |
|
|
448 | |
421 | $self |
449 | $self |
|
|
450 | } |
|
|
451 | |
|
|
452 | sub init { |
|
|
453 | # nop |
|
|
454 | } |
|
|
455 | |
|
|
456 | sub substitute { |
|
|
457 | () |
422 | } |
458 | } |
423 | |
459 | |
424 | # the name / primary key / in-game path |
460 | # the name / primary key / in-game path |
425 | sub as_string { |
461 | sub as_string { |
426 | my ($self) = @_; |
462 | my ($self) = @_; |
427 | |
463 | |
428 | $self->{user_rel} ? "~$self->{user}$self->{path}" |
464 | $self->{user_rel} ? "~$self->{user}$self->{path}" |
429 | : $self->{random} ? "?random/$self->{path}" |
465 | : $self->{ext} ? "?$self->{ext}/$self->{path}" |
430 | : $self->{path} |
466 | : $self->{path} |
431 | } |
467 | } |
432 | |
468 | |
433 | # the displayed name, this is a one way mapping |
469 | # the displayed name, this is a one way mapping |
434 | sub visible_name { |
470 | sub visible_name { |
435 | my ($self) = @_; |
471 | &as_string |
436 | |
|
|
437 | # if (my $rmp = $self->{random}) { |
|
|
438 | # # todo: be more intelligent about this |
|
|
439 | # "?random/$rmp->{origin_map}+$rmp->{origin_x}+$rmp->{origin_y}/$rmp->{dungeon_level}" |
|
|
440 | # } else { |
|
|
441 | $self->as_string |
|
|
442 | # } |
|
|
443 | } |
472 | } |
444 | |
473 | |
445 | # escape the /'s in the path |
474 | # escape the /'s in the path |
446 | sub _escaped_path { |
475 | sub _escaped_path { |
447 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
476 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
|
|
477 | |
448 | $path |
478 | $path |
449 | } |
479 | } |
450 | |
480 | |
451 | # the original (read-only) location |
481 | # the original (read-only) location |
452 | sub load_path { |
482 | sub load_path { |
… | |
… | |
457 | |
487 | |
458 | # the temporary/swap location |
488 | # the temporary/swap location |
459 | sub save_path { |
489 | sub save_path { |
460 | my ($self) = @_; |
490 | my ($self) = @_; |
461 | |
491 | |
|
|
492 | $self->{user_rel} |
462 | $self->{user_rel} ? sprintf "%s/%s/%s/%s", cf::localdir, cf::playerdir, $self->{user}, $self->_escaped_path |
493 | ? sprintf "%s/%s/%s/%s", cf::localdir, cf::playerdir, $self->{user}, $self->_escaped_path |
463 | : $self->{random} ? sprintf "%s/%s", $RANDOM_MAPS, $self->{path} |
|
|
464 | : sprintf "%s/%s/%s", cf::localdir, cf::tmpdir, $self->_escaped_path |
494 | : sprintf "%s/%s/%s", cf::localdir, cf::tmpdir, $self->_escaped_path |
465 | } |
495 | } |
466 | |
496 | |
467 | # the unique path, might be eq to save_path |
497 | # the unique path, undef == no special unique path |
468 | sub uniq_path { |
498 | sub uniq_path { |
469 | my ($self) = @_; |
499 | my ($self) = @_; |
470 | |
500 | |
471 | $self->{user_rel} || $self->{random} |
|
|
472 | ? undef |
|
|
473 | : sprintf "%s/%s/%s", cf::localdir, cf::uniquedir, $self->_escaped_path |
501 | sprintf "%s/%s/%s", cf::localdir, cf::uniquedir, $self->_escaped_path |
474 | } |
|
|
475 | |
|
|
476 | # return random map parameters, or undef |
|
|
477 | sub random_map_params { |
|
|
478 | my ($self) = @_; |
|
|
479 | |
|
|
480 | $self->{random} |
|
|
481 | } |
502 | } |
482 | |
503 | |
483 | # this is somewhat ugly, but style maps do need special treatment |
504 | # this is somewhat ugly, but style maps do need special treatment |
484 | sub is_style_map { |
505 | sub is_style_map { |
485 | $_[0]{path} =~ m{^/styles/} |
506 | $_[0]{path} =~ m{^/styles/} |
|
|
507 | } |
|
|
508 | |
|
|
509 | sub load_orig { |
|
|
510 | my ($self) = @_; |
|
|
511 | |
|
|
512 | &cf::map::load_map_header ($self->load_path) |
|
|
513 | } |
|
|
514 | |
|
|
515 | sub load_temp { |
|
|
516 | my ($self) = @_; |
|
|
517 | |
|
|
518 | &cf::map::load_map_header ($self->save_path) |
486 | } |
519 | } |
487 | |
520 | |
488 | package cf; |
521 | package cf; |
489 | |
522 | |
490 | ############################################################################# |
523 | ############################################################################# |
… | |
… | |
940 | =cut |
973 | =cut |
941 | |
974 | |
942 | sub register_extcmd { |
975 | sub register_extcmd { |
943 | my ($name, $cb) = @_; |
976 | my ($name, $cb) = @_; |
944 | |
977 | |
945 | my $caller = caller; |
|
|
946 | #warn "registering extcmd '$name' to '$caller'"; |
|
|
947 | |
|
|
948 | $EXTCMD{$name} = [$cb, $caller]; |
978 | $EXTCMD{$name} = $cb; |
949 | } |
979 | } |
950 | |
980 | |
951 | cf::player->attach ( |
981 | cf::player->attach ( |
952 | on_command => sub { |
982 | on_command => sub { |
953 | my ($pl, $name, $params) = @_; |
983 | my ($pl, $name, $params) = @_; |
… | |
… | |
966 | |
996 | |
967 | my $msg = eval { from_json $buf }; |
997 | my $msg = eval { from_json $buf }; |
968 | |
998 | |
969 | if (ref $msg) { |
999 | if (ref $msg) { |
970 | if (my $cb = $EXTCMD{$msg->{msgtype}}) { |
1000 | if (my $cb = $EXTCMD{$msg->{msgtype}}) { |
971 | if (my %reply = $cb->[0]->($pl, $msg)) { |
1001 | if (my %reply = $cb->($pl, $msg)) { |
972 | $pl->ext_reply ($msg->{msgid}, %reply); |
1002 | $pl->ext_reply ($msg->{msgid}, %reply); |
973 | } |
1003 | } |
974 | } |
1004 | } |
975 | } else { |
1005 | } else { |
976 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
1006 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
… | |
… | |
978 | |
1008 | |
979 | cf::override; |
1009 | cf::override; |
980 | }, |
1010 | }, |
981 | ); |
1011 | ); |
982 | |
1012 | |
983 | sub register { |
|
|
984 | my ($base, $pkg) = @_; |
|
|
985 | |
|
|
986 | #TODO |
|
|
987 | } |
|
|
988 | |
|
|
989 | sub load_extension { |
1013 | sub load_extension { |
990 | my ($path) = @_; |
1014 | my ($path) = @_; |
991 | |
1015 | |
992 | $path =~ /([^\/\\]+)\.ext$/ or die "$path"; |
1016 | $path =~ /([^\/\\]+)\.ext$/ or die "$path"; |
993 | my $base = $1; |
1017 | my $base = $1; |
994 | my $pkg = $1; |
1018 | my $pkg = $1; |
995 | $pkg =~ s/[^[:word:]]/_/g; |
1019 | $pkg =~ s/[^[:word:]]/_/g; |
996 | $pkg = "ext::$pkg"; |
1020 | $pkg = "ext::$pkg"; |
997 | |
1021 | |
998 | warn "loading '$path' into '$pkg'\n"; |
1022 | warn "... loading '$path' into '$pkg'\n"; |
999 | |
1023 | |
1000 | open my $fh, "<:utf8", $path |
1024 | open my $fh, "<:utf8", $path |
1001 | or die "$path: $!"; |
1025 | or die "$path: $!"; |
1002 | |
1026 | |
1003 | my $source = |
1027 | my $source = |
… | |
… | |
1008 | |
1032 | |
1009 | eval $source |
1033 | eval $source |
1010 | or die $@ ? "$path: $@\n" |
1034 | or die $@ ? "$path: $@\n" |
1011 | : "extension disabled.\n"; |
1035 | : "extension disabled.\n"; |
1012 | |
1036 | |
1013 | push @exts, $pkg; |
1037 | push @EXTS, $pkg; |
1014 | $ext_pkg{$base} = $pkg; |
|
|
1015 | |
|
|
1016 | # no strict 'refs'; |
|
|
1017 | # @{"$pkg\::ISA"} = ext::; |
|
|
1018 | |
|
|
1019 | register $base, $pkg; |
|
|
1020 | } |
|
|
1021 | |
|
|
1022 | sub unload_extension { |
|
|
1023 | my ($pkg) = @_; |
|
|
1024 | |
|
|
1025 | warn "removing extension $pkg\n"; |
|
|
1026 | |
|
|
1027 | # remove hooks |
|
|
1028 | #TODO |
|
|
1029 | # for my $idx (0 .. $#PLUGIN_EVENT) { |
|
|
1030 | # delete $hook[$idx]{$pkg}; |
|
|
1031 | # } |
|
|
1032 | |
|
|
1033 | # remove commands |
|
|
1034 | for my $name (keys %COMMAND) { |
|
|
1035 | my @cb = grep $_->[0] ne $pkg, @{ $COMMAND{$name} }; |
|
|
1036 | |
|
|
1037 | if (@cb) { |
|
|
1038 | $COMMAND{$name} = \@cb; |
|
|
1039 | } else { |
|
|
1040 | delete $COMMAND{$name}; |
|
|
1041 | } |
|
|
1042 | } |
|
|
1043 | |
|
|
1044 | # remove extcmds |
|
|
1045 | for my $name (grep $EXTCMD{$_}[1] eq $pkg, keys %EXTCMD) { |
|
|
1046 | delete $EXTCMD{$name}; |
|
|
1047 | } |
|
|
1048 | |
|
|
1049 | if (my $cb = $pkg->can ("unload")) { |
|
|
1050 | eval { |
|
|
1051 | $cb->($pkg); |
|
|
1052 | 1 |
|
|
1053 | } or warn "$pkg unloaded, but with errors: $@"; |
|
|
1054 | } |
|
|
1055 | |
|
|
1056 | Symbol::delete_package $pkg; |
|
|
1057 | } |
1038 | } |
1058 | |
1039 | |
1059 | sub load_extensions { |
1040 | sub load_extensions { |
1060 | for my $ext (<$LIBDIR/*.ext>) { |
1041 | for my $ext (<$LIBDIR/*.ext>) { |
1061 | next unless -r $ext; |
1042 | next unless -r $ext; |
… | |
… | |
1240 | my @paths; |
1221 | my @paths; |
1241 | |
1222 | |
1242 | for (@$files) { |
1223 | for (@$files) { |
1243 | utf8::decode $_; |
1224 | utf8::decode $_; |
1244 | next if /\.(?:pl|pst)$/; |
1225 | next if /\.(?:pl|pst)$/; |
1245 | next unless /^$PATH_SEP/; |
1226 | next unless /^$PATH_SEP/o; |
1246 | |
1227 | |
1247 | s/$PATH_SEP/\//g; |
|
|
1248 | push @paths, new cf::path "~" . $pl->ob->name . "/" . $_; |
1228 | push @paths, new cf::path "~" . $pl->ob->name . "/" . $_; |
1249 | } |
1229 | } |
1250 | |
1230 | |
1251 | \@paths |
1231 | \@paths |
1252 | } |
1232 | } |
… | |
… | |
1286 | |
1266 | |
1287 | sub generate_random_map { |
1267 | sub generate_random_map { |
1288 | my ($path, $rmp) = @_; |
1268 | my ($path, $rmp) = @_; |
1289 | |
1269 | |
1290 | # mit "rum" bekleckern, nicht |
1270 | # mit "rum" bekleckern, nicht |
1291 | cf::map::_create_random_map |
1271 | cf::map::_create_random_map ( |
1292 | $path, |
1272 | $path, |
1293 | $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, |
1273 | $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, |
1294 | $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, |
1274 | $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, |
1295 | $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, |
1275 | $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, |
1296 | $rmp->{exit_on_final_map}, |
1276 | $rmp->{exit_on_final_map}, |
… | |
… | |
1298 | $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3}, |
1278 | $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3}, |
1299 | $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase}, |
1279 | $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase}, |
1300 | $rmp->{dungeon_level}, $rmp->{dungeon_depth}, $rmp->{decoroptions}, $rmp->{orientation}, |
1280 | $rmp->{dungeon_level}, $rmp->{dungeon_depth}, $rmp->{decoroptions}, $rmp->{orientation}, |
1301 | $rmp->{origin_y}, $rmp->{origin_x}, $rmp->{random_seed}, $rmp->{total_map_hp}, |
1281 | $rmp->{origin_y}, $rmp->{origin_x}, $rmp->{random_seed}, $rmp->{total_map_hp}, |
1302 | $rmp->{map_layout_style}, $rmp->{treasureoptions}, $rmp->{symmetry_used}, |
1282 | $rmp->{map_layout_style}, $rmp->{treasureoptions}, $rmp->{symmetry_used}, |
1303 | (cf::region::find $rmp->{region}) |
1283 | (cf::region::find $rmp->{region}), $rmp->{custom} |
|
|
1284 | ) |
1304 | } |
1285 | } |
1305 | |
1286 | |
1306 | # and all this just because we cannot iterate over |
1287 | # and all this just because we cannot iterate over |
1307 | # all maps in C++... |
1288 | # all maps in C++... |
1308 | sub change_all_map_light { |
1289 | sub change_all_map_light { |
… | |
… | |
1310 | |
1291 | |
1311 | $_->change_map_light ($change) |
1292 | $_->change_map_light ($change) |
1312 | for grep $_->outdoor, values %cf::MAP; |
1293 | for grep $_->outdoor, values %cf::MAP; |
1313 | } |
1294 | } |
1314 | |
1295 | |
1315 | sub try_load_header($) { |
1296 | sub load_map_header($) { |
1316 | my ($path) = @_; |
1297 | my ($path) = @_; |
1317 | |
1298 | |
1318 | utf8::encode $path; |
1299 | utf8::encode $path; |
1319 | aio_open $path, O_RDONLY, 0 |
1300 | aio_open $path, O_RDONLY, 0 |
1320 | or return; |
1301 | or return; |
1321 | |
1302 | |
1322 | my $map = cf::map::new |
1303 | my $map = cf::map::new |
1323 | or return; |
1304 | or return; |
1324 | |
1305 | |
1325 | # for better error messages only, will be overwritten |
1306 | # for better error messages only, will be overwritten later |
1326 | $map->path ($path); |
1307 | $map->path ($path); |
1327 | |
1308 | |
1328 | $map->load_header ($path) |
1309 | $map->load_header ($path) |
1329 | or return; |
1310 | or return; |
1330 | |
1311 | |
… | |
… | |
1346 | |
1327 | |
1347 | $cf::MAP{$key} || do { |
1328 | $cf::MAP{$key} || do { |
1348 | my $guard = cf::lock_acquire "map_find:$key"; |
1329 | my $guard = cf::lock_acquire "map_find:$key"; |
1349 | |
1330 | |
1350 | # do it the slow way |
1331 | # do it the slow way |
1351 | my $map = try_load_header $path->save_path; |
1332 | my $map = $path->load_temp; |
1352 | |
1333 | |
1353 | Coro::cede; |
1334 | Coro::cede; |
1354 | |
1335 | |
1355 | if ($map) { |
1336 | if ($map) { |
1356 | $map->last_access ((delete $map->{last_access}) |
1337 | $map->last_access ((delete $map->{last_access}) |
1357 | || $cf::RUNTIME); #d# |
1338 | || $cf::RUNTIME); #d# |
1358 | # safety |
1339 | # safety |
1359 | $map->{instantiate_time} = $cf::RUNTIME |
1340 | $map->{instantiate_time} = $cf::RUNTIME |
1360 | if $map->{instantiate_time} > $cf::RUNTIME; |
1341 | if $map->{instantiate_time} > $cf::RUNTIME; |
1361 | } else { |
1342 | } else { |
1362 | if (my $rmp = $path->random_map_params) { |
1343 | $map = $path->load_orig |
1363 | $map = generate_random_map $key, $rmp; |
|
|
1364 | } else { |
|
|
1365 | $map = try_load_header $path->load_path; |
|
|
1366 | } |
|
|
1367 | |
|
|
1368 | $map or return; |
1344 | or return; |
1369 | |
1345 | |
1370 | $map->{load_original} = 1; |
1346 | $map->{load_original} = 1; |
1371 | $map->{instantiate_time} = $cf::RUNTIME; |
1347 | $map->{instantiate_time} = $cf::RUNTIME; |
1372 | $map->last_access ($cf::RUNTIME); |
1348 | $map->last_access ($cf::RUNTIME); |
1373 | $map->instantiate; |
1349 | $map->instantiate; |
… | |
… | |
1439 | Coro::cede; |
1415 | Coro::cede; |
1440 | |
1416 | |
1441 | $self->in_memory (cf::MAP_IN_MEMORY); |
1417 | $self->in_memory (cf::MAP_IN_MEMORY); |
1442 | } |
1418 | } |
1443 | |
1419 | |
|
|
1420 | # find and load all maps in the 3x3 area around a map |
|
|
1421 | sub load_diag { |
|
|
1422 | my ($map) = @_; |
|
|
1423 | |
|
|
1424 | my @diag; # diagonal neighbours |
|
|
1425 | |
|
|
1426 | for (0 .. 3) { |
|
|
1427 | my $neigh = $map->tile_path ($_) |
|
|
1428 | or next; |
|
|
1429 | $neigh = find $neigh, $map |
|
|
1430 | or next; |
|
|
1431 | $neigh->load; |
|
|
1432 | |
|
|
1433 | push @diag, [$neigh->tile_path (($_ + 3) % 4), $neigh], |
|
|
1434 | [$neigh->tile_path (($_ + 1) % 4), $neigh]; |
|
|
1435 | } |
|
|
1436 | |
|
|
1437 | for (@diag) { |
|
|
1438 | my $neigh = find @$_ |
|
|
1439 | or next; |
|
|
1440 | $neigh->load; |
|
|
1441 | } |
|
|
1442 | } |
|
|
1443 | |
1444 | sub find_sync { |
1444 | sub find_sync { |
1445 | my ($path, $origin) = @_; |
1445 | my ($path, $origin) = @_; |
1446 | |
1446 | |
1447 | cf::sync_job { cf::map::find $path, $origin } |
1447 | cf::sync_job { find $path, $origin } |
1448 | } |
1448 | } |
1449 | |
1449 | |
1450 | sub do_load_sync { |
1450 | sub do_load_sync { |
1451 | my ($map) = @_; |
1451 | my ($map) = @_; |
1452 | |
1452 | |
1453 | cf::sync_job { $map->load }; |
1453 | cf::sync_job { $map->load }; |
|
|
1454 | } |
|
|
1455 | |
|
|
1456 | our %MAP_PREFETCH; |
|
|
1457 | our $MAP_PREFETCHER = Coro::async { |
|
|
1458 | while () { |
|
|
1459 | while (%MAP_PREFETCH) { |
|
|
1460 | my $key = each %MAP_PREFETCH |
|
|
1461 | or next; |
|
|
1462 | my $path = delete $MAP_PREFETCH{$key}; |
|
|
1463 | |
|
|
1464 | my $map = find $path |
|
|
1465 | or next; |
|
|
1466 | $map->load; |
|
|
1467 | } |
|
|
1468 | Coro::schedule; |
|
|
1469 | } |
|
|
1470 | }; |
|
|
1471 | |
|
|
1472 | sub find_async { |
|
|
1473 | my ($path, $origin) = @_; |
|
|
1474 | |
|
|
1475 | $path = new cf::path $path, $origin && $origin->path; |
|
|
1476 | my $key = $path->as_string; |
|
|
1477 | |
|
|
1478 | if (my $map = $cf::MAP{$key}) { |
|
|
1479 | return $map if $map->in_memory == cf::MAP_IN_MEMORY; |
|
|
1480 | } |
|
|
1481 | |
|
|
1482 | $MAP_PREFETCH{$key} = $path; |
|
|
1483 | $MAP_PREFETCHER->ready; |
|
|
1484 | |
|
|
1485 | () |
1454 | } |
1486 | } |
1455 | |
1487 | |
1456 | sub save { |
1488 | sub save { |
1457 | my ($self) = @_; |
1489 | my ($self) = @_; |
1458 | |
1490 | |
… | |
… | |
1576 | } |
1608 | } |
1577 | |
1609 | |
1578 | $map |
1610 | $map |
1579 | } |
1611 | } |
1580 | |
1612 | |
|
|
1613 | =item cf::map::unique_maps |
|
|
1614 | |
|
|
1615 | Returns an arrayref of cf::path's of all shared maps that have |
|
|
1616 | instantiated unique items. May block. |
|
|
1617 | |
|
|
1618 | =cut |
|
|
1619 | |
|
|
1620 | sub unique_maps() { |
|
|
1621 | my $files = aio_readdir cf::localdir . "/" . cf::uniquedir |
|
|
1622 | or return; |
|
|
1623 | |
|
|
1624 | my @paths; |
|
|
1625 | |
|
|
1626 | for (@$files) { |
|
|
1627 | utf8::decode $_; |
|
|
1628 | next if /\.pst$/; |
|
|
1629 | next unless /^$PATH_SEP/o; |
|
|
1630 | |
|
|
1631 | push @paths, new cf::path $_; |
|
|
1632 | } |
|
|
1633 | |
|
|
1634 | \@paths |
|
|
1635 | } |
|
|
1636 | |
1581 | package cf; |
1637 | package cf; |
1582 | |
1638 | |
1583 | =back |
1639 | =back |
1584 | |
1640 | |
1585 | =head3 cf::object |
1641 | =head3 cf::object |
… | |
… | |
1707 | # use -1 or undef as default coordinates, not 0, 0 |
1763 | # use -1 or undef as default coordinates, not 0, 0 |
1708 | ($x, $y) = ($map->enter_x, $map->enter_y) |
1764 | ($x, $y) = ($map->enter_x, $map->enter_y) |
1709 | if $x <=0 && $y <= 0; |
1765 | if $x <=0 && $y <= 0; |
1710 | |
1766 | |
1711 | $map->load; |
1767 | $map->load; |
|
|
1768 | $map->load_diag; |
1712 | |
1769 | |
1713 | return unless $self->contr->active; |
1770 | return unless $self->contr->active; |
1714 | $self->activate_recursive; |
1771 | $self->activate_recursive; |
1715 | $self->enter_map ($map, $x, $y); |
1772 | $self->enter_map ($map, $x, $y); |
1716 | } |
1773 | } |
… | |
… | |
1753 | |
1810 | |
1754 | sub cf::object::player::goto { |
1811 | sub cf::object::player::goto { |
1755 | my ($self, $path, $x, $y) = @_; |
1812 | my ($self, $path, $x, $y) = @_; |
1756 | |
1813 | |
1757 | $path = new cf::path $path; |
1814 | $path = new cf::path $path; |
1758 | $path ne "/" or Carp::cluck ("oy");#d# |
|
|
1759 | |
1815 | |
1760 | $self->enter_link; |
1816 | $self->enter_link; |
1761 | |
1817 | |
1762 | (async { |
1818 | (async { |
1763 | my $map = cf::map::find $path->as_string; |
1819 | my $map = cf::map::find $path->as_string; |
… | |
… | |
1840 | |
1896 | |
1841 | 1; |
1897 | 1; |
1842 | }) { |
1898 | }) { |
1843 | $self->message ("Something went wrong deep within the crossfire server. " |
1899 | $self->message ("Something went wrong deep within the crossfire server. " |
1844 | . "I'll try to bring you back to the map you were before. " |
1900 | . "I'll try to bring you back to the map you were before. " |
1845 | . "Please report this to the dungeon master", |
1901 | . "Please report this to the dungeon master!", |
1846 | cf::NDI_UNIQUE | cf::NDI_RED); |
1902 | cf::NDI_UNIQUE | cf::NDI_RED); |
1847 | |
1903 | |
1848 | warn "ERROR in enter_exit: $@"; |
1904 | warn "ERROR in enter_exit: $@"; |
1849 | $self->leave_link; |
1905 | $self->leave_link; |
1850 | } |
1906 | } |
… | |
… | |
2218 | }, |
2274 | }, |
2219 | ); |
2275 | ); |
2220 | } |
2276 | } |
2221 | } |
2277 | } |
2222 | |
2278 | |
2223 | sub emergency_save { |
2279 | sub emergency_save() { |
2224 | my $freeze_guard = cf::freeze_mainloop; |
2280 | my $freeze_guard = cf::freeze_mainloop; |
2225 | |
2281 | |
2226 | warn "enter emergency perl save\n"; |
2282 | warn "enter emergency perl save\n"; |
2227 | |
2283 | |
2228 | cf::sync_job { |
2284 | cf::sync_job { |
… | |
… | |
2256 | return; |
2312 | return; |
2257 | } |
2313 | } |
2258 | |
2314 | |
2259 | warn "reloading..."; |
2315 | warn "reloading..."; |
2260 | |
2316 | |
|
|
2317 | warn "freezing server"; |
2261 | my $guard = freeze_mainloop; |
2318 | my $guard = freeze_mainloop; |
2262 | cf::emergency_save; |
2319 | cf::emergency_save; |
2263 | |
2320 | |
|
|
2321 | warn "sync database to disk"; |
|
|
2322 | cf::db_sync; |
|
|
2323 | IO::AIO::flush; |
|
|
2324 | |
2264 | eval { |
2325 | eval { |
2265 | # if anything goes wrong in here, we should simply crash as we already saved |
2326 | # if anything goes wrong in here, we should simply crash as we already saved |
2266 | |
2327 | |
2267 | # cancel all watchers |
2328 | warn "cancel all watchers"; |
2268 | for (Event::all_watchers) { |
2329 | for (Event::all_watchers) { |
2269 | $_->cancel if $_->data & WF_AUTOCANCEL; |
2330 | $_->cancel if $_->data & WF_AUTOCANCEL; |
2270 | } |
2331 | } |
2271 | |
2332 | |
2272 | # cancel all extension coros |
2333 | warn "cancel all extension coros"; |
2273 | $_->cancel for values %EXT_CORO; |
2334 | $_->cancel for values %EXT_CORO; |
2274 | %EXT_CORO = (); |
2335 | %EXT_CORO = (); |
2275 | |
2336 | |
|
|
2337 | warn "remove commands"; |
|
|
2338 | %COMMAND = (); |
|
|
2339 | |
|
|
2340 | warn "remove ext commands"; |
|
|
2341 | %EXTCMD = (); |
|
|
2342 | |
2276 | # unload all extensions |
2343 | warn "unload/nuke all extensions"; |
2277 | for (@exts) { |
2344 | for my $pkg (@EXTS) { |
2278 | warn "unloading <$_>"; |
2345 | warn "... unloading $pkg"; |
2279 | unload_extension $_; |
2346 | |
|
|
2347 | if (my $cb = $pkg->can ("unload")) { |
|
|
2348 | eval { |
|
|
2349 | $cb->($pkg); |
|
|
2350 | 1 |
|
|
2351 | } or warn "$pkg unloaded, but with errors: $@"; |
2280 | } |
2352 | } |
2281 | |
2353 | |
|
|
2354 | warn "... nuking $pkg"; |
|
|
2355 | Symbol::delete_package $pkg; |
|
|
2356 | } |
|
|
2357 | |
2282 | # unload all modules loaded from $LIBDIR |
2358 | warn "unload all perl modules loaded from $LIBDIR"; |
2283 | while (my ($k, $v) = each %INC) { |
2359 | while (my ($k, $v) = each %INC) { |
2284 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
2360 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
2285 | |
2361 | |
2286 | warn "removing <$k>"; |
2362 | warn "removing <$k>"; |
2287 | delete $INC{$k}; |
2363 | delete $INC{$k}; |
… | |
… | |
2294 | } |
2370 | } |
2295 | |
2371 | |
2296 | Symbol::delete_package $k; |
2372 | Symbol::delete_package $k; |
2297 | } |
2373 | } |
2298 | |
2374 | |
2299 | # sync database to disk |
|
|
2300 | cf::db_sync; |
|
|
2301 | IO::AIO::flush; |
|
|
2302 | |
|
|
2303 | # get rid of safe::, as good as possible |
2375 | warn "get rid of safe::, as good as possible"; |
2304 | Symbol::delete_package "safe::$_" |
2376 | Symbol::delete_package "safe::$_" |
2305 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
2377 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
2306 | |
2378 | |
2307 | # remove register_script_function callbacks |
|
|
2308 | # TODO |
|
|
2309 | |
|
|
2310 | # unload cf.pm "a bit" |
2379 | warn "unload cf.pm \"a bit\""; |
2311 | delete $INC{"cf.pm"}; |
2380 | delete $INC{"cf.pm"}; |
2312 | |
2381 | |
2313 | # don't, removes xs symbols, too, |
2382 | # don't, removes xs symbols, too, |
2314 | # and global variables created in xs |
2383 | # and global variables created in xs |
2315 | #Symbol::delete_package __PACKAGE__; |
2384 | #Symbol::delete_package __PACKAGE__; |
2316 | |
2385 | |
2317 | # reload cf.pm |
|
|
2318 | warn "reloading cf.pm"; |
2386 | warn "reloading cf.pm"; |
2319 | require cf; |
2387 | require cf; |
2320 | cf::_connect_to_perl; # nominally unnecessary, but cannot hurt |
2388 | cf::_connect_to_perl; # nominally unnecessary, but cannot hurt |
2321 | |
2389 | |
2322 | # load config and database again |
2390 | warn "load config and database again"; |
2323 | cf::cfg_load; |
2391 | cf::cfg_load; |
2324 | cf::db_load; |
2392 | cf::db_load; |
2325 | |
2393 | |
2326 | # load extensions |
|
|
2327 | warn "load extensions"; |
2394 | warn "load extensions"; |
2328 | cf::load_extensions; |
2395 | cf::load_extensions; |
2329 | |
2396 | |
2330 | # reattach attachments to objects |
2397 | warn "reattach attachments to objects/players"; |
2331 | warn "reattach"; |
|
|
2332 | _global_reattach; |
2398 | _global_reattach; |
|
|
2399 | warn "reattach attachments to maps"; |
2333 | reattach $_ for values %MAP; |
2400 | reattach $_ for values %MAP; |
2334 | }; |
2401 | }; |
2335 | |
2402 | |
2336 | if ($@) { |
2403 | if ($@) { |
2337 | warn $@; |
2404 | warn $@; |
2338 | warn "error while reloading, exiting."; |
2405 | warn "error while reloading, exiting."; |
2339 | exit 1; |
2406 | exit 1; |
2340 | } |
2407 | } |
2341 | |
2408 | |
2342 | warn "reloaded successfully"; |
2409 | warn "reloaded"; |
2343 | }; |
2410 | }; |
2344 | |
2411 | |
2345 | ############################################################################# |
2412 | ############################################################################# |
2346 | |
2413 | |
2347 | unless ($LINK_MAP) { |
2414 | unless ($LINK_MAP) { |
… | |
… | |
2383 | $LINK_MAP->{deny_reset} = 1; |
2450 | $LINK_MAP->{deny_reset} = 1; |
2384 | |
2451 | |
2385 | $cf::MAP{$LINK_MAP->path} = $LINK_MAP; |
2452 | $cf::MAP{$LINK_MAP->path} = $LINK_MAP; |
2386 | } |
2453 | } |
2387 | |
2454 | |
2388 | register "<global>", __PACKAGE__; |
|
|
2389 | |
|
|
2390 | register_command "reload" => sub { |
2455 | register_command "reload" => sub { |
2391 | my ($who, $arg) = @_; |
2456 | my ($who, $arg) = @_; |
2392 | |
2457 | |
2393 | if ($who->flag (FLAG_WIZ)) { |
2458 | if ($who->flag (FLAG_WIZ)) { |
2394 | $who->message ("start of reload."); |
2459 | $who->message ("start of reload."); |
… | |
… | |
2403 | reentrant => 0, |
2468 | reentrant => 0, |
2404 | prio => 0, |
2469 | prio => 0, |
2405 | at => $NEXT_TICK || $TICK, |
2470 | at => $NEXT_TICK || $TICK, |
2406 | data => WF_AUTOCANCEL, |
2471 | data => WF_AUTOCANCEL, |
2407 | cb => sub { |
2472 | cb => sub { |
|
|
2473 | $NOW = Event::time; |
|
|
2474 | |
2408 | cf::server_tick; # one server iteration |
2475 | cf::server_tick; # one server iteration |
2409 | $RUNTIME += $TICK; |
2476 | $RUNTIME += $TICK; |
2410 | $NEXT_TICK += $TICK; |
2477 | $NEXT_TICK += $TICK; |
2411 | |
2478 | |
2412 | $WAIT_FOR_TICK->broadcast; |
2479 | $WAIT_FOR_TICK->broadcast; |