… | |
… | |
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; |
19 | |
19 | |
|
|
20 | use Data::Dumper; |
20 | use Digest::MD5; |
21 | use Digest::MD5; |
21 | use Fcntl; |
22 | use Fcntl; |
22 | use IO::AIO 2.31 (); |
23 | use IO::AIO 2.32 (); |
23 | use YAML::Syck (); |
24 | use YAML::Syck (); |
24 | use Time::HiRes; |
25 | use Time::HiRes; |
25 | |
26 | |
26 | 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 |
27 | |
28 | |
|
|
29 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
|
|
30 | |
28 | # 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? |
29 | $YAML::Syck::ImplicitUnicode = 1; |
32 | $YAML::Syck::ImplicitUnicode = 1; |
30 | |
33 | |
31 | $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 |
32 | |
35 | |
33 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
|
|
34 | |
|
|
35 | our %COMMAND = (); |
36 | our %COMMAND = (); |
36 | our %COMMAND_TIME = (); |
37 | our %COMMAND_TIME = (); |
|
|
38 | |
|
|
39 | our @EXTS = (); # list of extension package names |
37 | our %EXTCMD = (); |
40 | our %EXTCMD = (); |
|
|
41 | our %EXT_CORO = (); # coroutines bound to extensions |
|
|
42 | our %EXT_MAP = (); # pluggable maps |
38 | |
43 | |
39 | our @EVENT; |
44 | our @EVENT; |
40 | our $LIBDIR = datadir . "/ext"; |
45 | our $LIBDIR = datadir . "/ext"; |
41 | |
46 | |
42 | our $TICK = MAX_TIME * 1e-6; |
47 | our $TICK = MAX_TIME * 1e-6; |
… | |
… | |
47 | our %CFG; |
52 | our %CFG; |
48 | |
53 | |
49 | our $UPTIME; $UPTIME ||= time; |
54 | our $UPTIME; $UPTIME ||= time; |
50 | our $RUNTIME; |
55 | our $RUNTIME; |
51 | |
56 | |
|
|
57 | our %PLAYER; # all users |
52 | our %MAP; # all maps |
58 | our %MAP; # all maps |
53 | our $LINK_MAP; # the special {link} map |
59 | our $LINK_MAP; # the special {link} map |
54 | our $RANDOM_MAPS = cf::localdir . "/random"; |
60 | our $RANDOM_MAPS = cf::localdir . "/random"; |
55 | our %EXT_CORO; |
61 | |
|
|
62 | our $WAIT_FOR_TICK; $WAIT_FOR_TICK ||= new Coro::Signal; |
|
|
63 | our $WAIT_FOR_TICK_ONE; $WAIT_FOR_TICK_ONE ||= new Coro::Signal; |
56 | |
64 | |
57 | binmode STDOUT; |
65 | binmode STDOUT; |
58 | binmode STDERR; |
66 | binmode STDERR; |
59 | |
67 | |
60 | # read virtual server time, if available |
68 | # read virtual server time, if available |
… | |
… | |
105 | =item %cf::CFG |
113 | =item %cf::CFG |
106 | |
114 | |
107 | Configuration for the server, loaded from C</etc/crossfire/config>, or |
115 | Configuration for the server, loaded from C</etc/crossfire/config>, or |
108 | from wherever your confdir points to. |
116 | from wherever your confdir points to. |
109 | |
117 | |
|
|
118 | =item $cf::WAIT_FOR_TICK, $cf::WAIT_FOR_TICK_ONE |
|
|
119 | |
|
|
120 | These are Coro::Signal objects that are C<< ->broadcast >> (WAIT_FOR_TICK) |
|
|
121 | or C<< ->send >> (WAIT_FOR_TICK_ONE) on after normal server tick |
|
|
122 | processing has been done. Call C<< ->wait >> on them to maximise the |
|
|
123 | window of cpu time available, or simply to synchronise to the server tick. |
|
|
124 | |
110 | =back |
125 | =back |
111 | |
126 | |
112 | =cut |
127 | =cut |
113 | |
128 | |
114 | BEGIN { |
129 | BEGIN { |
… | |
… | |
117 | utf8::encode $msg; |
132 | utf8::encode $msg; |
118 | |
133 | |
119 | $msg .= "\n" |
134 | $msg .= "\n" |
120 | unless $msg =~ /\n$/; |
135 | unless $msg =~ /\n$/; |
121 | |
136 | |
122 | LOG llevError, "cfperl: $msg"; |
137 | LOG llevError, $msg; |
123 | }; |
138 | }; |
124 | } |
139 | } |
125 | |
140 | |
126 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
141 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
127 | @safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; |
142 | @safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; |
… | |
… | |
145 | |
160 | |
146 | $Event::DIED = sub { |
161 | $Event::DIED = sub { |
147 | warn "error in event callback: @_"; |
162 | warn "error in event callback: @_"; |
148 | }; |
163 | }; |
149 | |
164 | |
150 | my %ext_pkg; |
|
|
151 | my @exts; |
|
|
152 | my @hook; |
|
|
153 | |
|
|
154 | =head2 UTILITY FUNCTIONS |
165 | =head2 UTILITY FUNCTIONS |
155 | |
166 | |
156 | =over 4 |
167 | =over 4 |
157 | |
168 | |
|
|
169 | =item dumpval $ref |
|
|
170 | |
158 | =cut |
171 | =cut |
|
|
172 | |
|
|
173 | sub dumpval { |
|
|
174 | eval { |
|
|
175 | local $SIG{__DIE__}; |
|
|
176 | my $d; |
|
|
177 | if (1) { |
|
|
178 | $d = new Data::Dumper([$_[0]], ["*var"]); |
|
|
179 | $d->Terse(1); |
|
|
180 | $d->Indent(2); |
|
|
181 | $d->Quotekeys(0); |
|
|
182 | $d->Useqq(1); |
|
|
183 | #$d->Bless(...); |
|
|
184 | $d->Seen($_[1]) if @_ > 1; |
|
|
185 | $d = $d->Dump(); |
|
|
186 | } |
|
|
187 | $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; |
|
|
188 | $d |
|
|
189 | } || "[unable to dump $_[0]: '$@']"; |
|
|
190 | } |
159 | |
191 | |
160 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
192 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
161 | |
193 | |
162 | =item $ref = cf::from_json $json |
194 | =item $ref = cf::from_json $json |
163 | |
195 | |
… | |
… | |
286 | } |
318 | } |
287 | } |
319 | } |
288 | |
320 | |
289 | =item $coro = cf::async_ext { BLOCK } |
321 | =item $coro = cf::async_ext { BLOCK } |
290 | |
322 | |
291 | Like async, but this coro is automcatially being canceled when the |
323 | Like async, but this coro is automatically being canceled when the |
292 | extension calling this is being unloaded. |
324 | extension calling this is being unloaded. |
293 | |
325 | |
294 | =cut |
326 | =cut |
295 | |
327 | |
296 | sub async_ext(&) { |
328 | sub async_ext(&) { |
… | |
… | |
334 | |
366 | |
335 | ############################################################################# |
367 | ############################################################################# |
336 | |
368 | |
337 | package cf::path; |
369 | package cf::path; |
338 | |
370 | |
|
|
371 | use overload |
|
|
372 | '""' => \&as_string; |
|
|
373 | |
|
|
374 | # used to convert map paths into valid unix filenames by repalcing / by ∕ |
|
|
375 | our $PATH_SEP = "∕"; # U+2215, chosen purely for visual reasons |
|
|
376 | |
|
|
377 | sub register { |
|
|
378 | my ($pkg, $prefix) = @_; |
|
|
379 | |
|
|
380 | $EXT_MAP{$prefix} = $pkg; |
|
|
381 | } |
|
|
382 | |
339 | sub new { |
383 | sub new { |
340 | my ($class, $path, $base) = @_; |
384 | my ($class, $path, $base) = @_; |
341 | |
385 | |
342 | $path = $path->as_string if ref $path; |
386 | return $path if ref $path; |
343 | |
387 | |
344 | my $self = bless { }, $class; |
388 | my $self = {}; |
345 | |
389 | |
346 | # {... are special paths that are not touched |
390 | # {... are special paths that are not being touched |
347 | # ?xxx/... are special absolute paths |
391 | # ?xxx/... are special absolute paths |
348 | # ?random/... random maps |
392 | # ?random/... random maps |
349 | # /! non-realised random map exit |
393 | # /! non-realised random map exit |
350 | # /... normal maps |
394 | # /... normal maps |
351 | # ~/... per-player maps without a specific player (DO NOT USE) |
395 | # ~/... per-player maps without a specific player (DO NOT USE) |
352 | # ~user/... per-player map of a specific user |
396 | # ~user/... per-player map of a specific user |
353 | |
397 | |
|
|
398 | $path =~ s/$PATH_SEP/\//go; |
|
|
399 | |
354 | if ($path =~ /^{/) { |
400 | if ($path =~ /^{/) { |
355 | # fine as it is |
401 | # fine as it is |
356 | } elsif ($path =~ s{^\?random/}{}) { |
|
|
357 | Coro::AIO::aio_load "$cf::RANDOM_MAPS/$path.meta", my $data; |
|
|
358 | $self->{random} = cf::from_json $data; |
|
|
359 | } else { |
402 | } else { |
360 | if ($path =~ s{^~([^/]+)?}{}) { |
403 | if ($path =~ s{^~([^/]+)?}{}) { |
|
|
404 | # ~user |
361 | $self->{user_rel} = 1; |
405 | $self->{user_rel} = 1; |
362 | |
406 | |
363 | if (defined $1) { |
407 | if (defined $1) { |
364 | $self->{user} = $1; |
408 | $self->{user} = $1; |
365 | } elsif ($base =~ m{^~([^/]+)/}) { |
409 | } elsif ($base =~ m{^~([^/]+)/}) { |
366 | $self->{user} = $1; |
410 | $self->{user} = $1; |
367 | } else { |
411 | } else { |
368 | warn "cannot resolve user-relative path without user <$path,$base>\n"; |
412 | warn "cannot resolve user-relative path without user <$path,$base>\n"; |
369 | } |
413 | } |
|
|
414 | } elsif ($path =~ s{^\?([^/]+)/}{}) { |
|
|
415 | # ?... |
|
|
416 | $self->{ext} = $1; |
|
|
417 | if (my $ext = $EXT_MAP{$1}) { |
|
|
418 | bless $self, $ext; |
|
|
419 | } |
370 | } elsif ($path =~ /^\//) { |
420 | } elsif ($path =~ /^\//) { |
|
|
421 | # /... |
371 | # already absolute |
422 | # already absolute |
372 | } else { |
423 | } else { |
|
|
424 | # relative |
373 | $base =~ s{[^/]+/?$}{}; |
425 | $base =~ s{[^/]+/?$}{}; |
374 | return $class->new ("$base/$path"); |
426 | return $class->new ("$base/$path"); |
375 | } |
427 | } |
376 | |
428 | |
377 | for ($path) { |
429 | for ($path) { |
… | |
… | |
380 | } |
432 | } |
381 | } |
433 | } |
382 | |
434 | |
383 | $self->{path} = $path; |
435 | $self->{path} = $path; |
384 | |
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 | |
385 | $self |
449 | $self |
|
|
450 | } |
|
|
451 | |
|
|
452 | sub init { |
|
|
453 | # nop |
|
|
454 | } |
|
|
455 | |
|
|
456 | sub substitute { |
|
|
457 | () |
386 | } |
458 | } |
387 | |
459 | |
388 | # the name / primary key / in-game path |
460 | # the name / primary key / in-game path |
389 | sub as_string { |
461 | sub as_string { |
390 | my ($self) = @_; |
462 | my ($self) = @_; |
391 | |
463 | |
392 | $self->{user_rel} ? "~$self->{user}$self->{path}" |
464 | $self->{user_rel} ? "~$self->{user}$self->{path}" |
393 | : $self->{random} ? "?random/$self->{path}" |
465 | : $self->{ext} ? "?$self->{ext}/$self->{path}" |
394 | : $self->{path} |
466 | : $self->{path} |
395 | } |
467 | } |
396 | |
468 | |
397 | # the displayed name, this is a one way mapping |
469 | # the displayed name, this is a one way mapping |
398 | sub visible_name { |
470 | sub visible_name { |
399 | my ($self) = @_; |
471 | &as_string |
400 | |
|
|
401 | # if (my $rmp = $self->{random}) { |
|
|
402 | # # todo: be more intelligent about this |
|
|
403 | # "?random/$rmp->{origin_map}+$rmp->{origin_x}+$rmp->{origin_y}/$rmp->{dungeon_level}" |
|
|
404 | # } else { |
|
|
405 | $self->as_string |
|
|
406 | # } |
|
|
407 | } |
472 | } |
408 | |
473 | |
409 | # escape the /'s in the path |
474 | # escape the /'s in the path |
410 | sub _escaped_path { |
475 | sub _escaped_path { |
411 | # ∕ is U+2215 |
|
|
412 | (my $path = $_[0]{path}) =~ s/\//∕/g; |
476 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
413 | $path |
477 | $path |
414 | } |
478 | } |
415 | |
479 | |
416 | # the original (read-only) location |
480 | # the original (read-only) location |
417 | sub load_path { |
481 | sub load_path { |
… | |
… | |
427 | $self->{user_rel} ? sprintf "%s/%s/%s/%s", cf::localdir, cf::playerdir, $self->{user}, $self->_escaped_path |
491 | $self->{user_rel} ? sprintf "%s/%s/%s/%s", cf::localdir, cf::playerdir, $self->{user}, $self->_escaped_path |
428 | : $self->{random} ? sprintf "%s/%s", $RANDOM_MAPS, $self->{path} |
492 | : $self->{random} ? sprintf "%s/%s", $RANDOM_MAPS, $self->{path} |
429 | : sprintf "%s/%s/%s", cf::localdir, cf::tmpdir, $self->_escaped_path |
493 | : sprintf "%s/%s/%s", cf::localdir, cf::tmpdir, $self->_escaped_path |
430 | } |
494 | } |
431 | |
495 | |
432 | # the unique path, might be eq to save_path |
496 | # the unique path, undef == no special unique path |
433 | sub uniq_path { |
497 | sub uniq_path { |
434 | my ($self) = @_; |
498 | my ($self) = @_; |
435 | |
499 | |
436 | $self->{user_rel} || $self->{random} |
|
|
437 | ? undef |
|
|
438 | : sprintf "%s/%s/%s", cf::localdir, cf::uniquedir, $self->_escaped_path |
500 | sprintf "%s/%s/%s", cf::localdir, cf::uniquedir, $self->_escaped_path |
439 | } |
|
|
440 | |
|
|
441 | # return random map parameters, or undef |
|
|
442 | sub random_map_params { |
|
|
443 | my ($self) = @_; |
|
|
444 | |
|
|
445 | $self->{random} |
|
|
446 | } |
501 | } |
447 | |
502 | |
448 | # this is somewhat ugly, but style maps do need special treatment |
503 | # this is somewhat ugly, but style maps do need special treatment |
449 | sub is_style_map { |
504 | sub is_style_map { |
450 | $_[0]{path} =~ m{^/styles/} |
505 | $_[0]{path} =~ m{^/styles/} |
|
|
506 | } |
|
|
507 | |
|
|
508 | sub load_orig { |
|
|
509 | my ($self) = @_; |
|
|
510 | |
|
|
511 | &cf::map::load_map_header ($self->load_path) |
|
|
512 | } |
|
|
513 | |
|
|
514 | sub load_temp { |
|
|
515 | my ($self) = @_; |
|
|
516 | |
|
|
517 | &cf::map::load_map_header ($self->save_path) |
451 | } |
518 | } |
452 | |
519 | |
453 | package cf; |
520 | package cf; |
454 | |
521 | |
455 | ############################################################################# |
522 | ############################################################################# |
… | |
… | |
905 | =cut |
972 | =cut |
906 | |
973 | |
907 | sub register_extcmd { |
974 | sub register_extcmd { |
908 | my ($name, $cb) = @_; |
975 | my ($name, $cb) = @_; |
909 | |
976 | |
910 | my $caller = caller; |
|
|
911 | #warn "registering extcmd '$name' to '$caller'"; |
|
|
912 | |
|
|
913 | $EXTCMD{$name} = [$cb, $caller]; |
977 | $EXTCMD{$name} = $cb; |
914 | } |
978 | } |
915 | |
979 | |
916 | cf::player->attach ( |
980 | cf::player->attach ( |
917 | on_command => sub { |
981 | on_command => sub { |
918 | my ($pl, $name, $params) = @_; |
982 | my ($pl, $name, $params) = @_; |
… | |
… | |
931 | |
995 | |
932 | my $msg = eval { from_json $buf }; |
996 | my $msg = eval { from_json $buf }; |
933 | |
997 | |
934 | if (ref $msg) { |
998 | if (ref $msg) { |
935 | if (my $cb = $EXTCMD{$msg->{msgtype}}) { |
999 | if (my $cb = $EXTCMD{$msg->{msgtype}}) { |
936 | if (my %reply = $cb->[0]->($pl, $msg)) { |
1000 | if (my %reply = $cb->($pl, $msg)) { |
937 | $pl->ext_reply ($msg->{msgid}, %reply); |
1001 | $pl->ext_reply ($msg->{msgid}, %reply); |
938 | } |
1002 | } |
939 | } |
1003 | } |
940 | } else { |
1004 | } else { |
941 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
1005 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
… | |
… | |
943 | |
1007 | |
944 | cf::override; |
1008 | cf::override; |
945 | }, |
1009 | }, |
946 | ); |
1010 | ); |
947 | |
1011 | |
948 | sub register { |
|
|
949 | my ($base, $pkg) = @_; |
|
|
950 | |
|
|
951 | #TODO |
|
|
952 | } |
|
|
953 | |
|
|
954 | sub load_extension { |
1012 | sub load_extension { |
955 | my ($path) = @_; |
1013 | my ($path) = @_; |
956 | |
1014 | |
957 | $path =~ /([^\/\\]+)\.ext$/ or die "$path"; |
1015 | $path =~ /([^\/\\]+)\.ext$/ or die "$path"; |
958 | my $base = $1; |
1016 | my $base = $1; |
959 | my $pkg = $1; |
1017 | my $pkg = $1; |
960 | $pkg =~ s/[^[:word:]]/_/g; |
1018 | $pkg =~ s/[^[:word:]]/_/g; |
961 | $pkg = "ext::$pkg"; |
1019 | $pkg = "ext::$pkg"; |
962 | |
1020 | |
963 | warn "loading '$path' into '$pkg'\n"; |
1021 | warn "... loading '$path' into '$pkg'\n"; |
964 | |
1022 | |
965 | open my $fh, "<:utf8", $path |
1023 | open my $fh, "<:utf8", $path |
966 | or die "$path: $!"; |
1024 | or die "$path: $!"; |
967 | |
1025 | |
968 | my $source = |
1026 | my $source = |
… | |
… | |
973 | |
1031 | |
974 | eval $source |
1032 | eval $source |
975 | or die $@ ? "$path: $@\n" |
1033 | or die $@ ? "$path: $@\n" |
976 | : "extension disabled.\n"; |
1034 | : "extension disabled.\n"; |
977 | |
1035 | |
978 | push @exts, $pkg; |
1036 | push @EXTS, $pkg; |
979 | $ext_pkg{$base} = $pkg; |
|
|
980 | |
|
|
981 | # no strict 'refs'; |
|
|
982 | # @{"$pkg\::ISA"} = ext::; |
|
|
983 | |
|
|
984 | register $base, $pkg; |
|
|
985 | } |
|
|
986 | |
|
|
987 | sub unload_extension { |
|
|
988 | my ($pkg) = @_; |
|
|
989 | |
|
|
990 | warn "removing extension $pkg\n"; |
|
|
991 | |
|
|
992 | # remove hooks |
|
|
993 | #TODO |
|
|
994 | # for my $idx (0 .. $#PLUGIN_EVENT) { |
|
|
995 | # delete $hook[$idx]{$pkg}; |
|
|
996 | # } |
|
|
997 | |
|
|
998 | # remove commands |
|
|
999 | for my $name (keys %COMMAND) { |
|
|
1000 | my @cb = grep $_->[0] ne $pkg, @{ $COMMAND{$name} }; |
|
|
1001 | |
|
|
1002 | if (@cb) { |
|
|
1003 | $COMMAND{$name} = \@cb; |
|
|
1004 | } else { |
|
|
1005 | delete $COMMAND{$name}; |
|
|
1006 | } |
|
|
1007 | } |
|
|
1008 | |
|
|
1009 | # remove extcmds |
|
|
1010 | for my $name (grep $EXTCMD{$_}[1] eq $pkg, keys %EXTCMD) { |
|
|
1011 | delete $EXTCMD{$name}; |
|
|
1012 | } |
|
|
1013 | |
|
|
1014 | if (my $cb = $pkg->can ("unload")) { |
|
|
1015 | eval { |
|
|
1016 | $cb->($pkg); |
|
|
1017 | 1 |
|
|
1018 | } or warn "$pkg unloaded, but with errors: $@"; |
|
|
1019 | } |
|
|
1020 | |
|
|
1021 | Symbol::delete_package $pkg; |
|
|
1022 | } |
1037 | } |
1023 | |
1038 | |
1024 | sub load_extensions { |
1039 | sub load_extensions { |
1025 | for my $ext (<$LIBDIR/*.ext>) { |
1040 | for my $ext (<$LIBDIR/*.ext>) { |
1026 | next unless -r $ext; |
1041 | next unless -r $ext; |
… | |
… | |
1044 | }; |
1059 | }; |
1045 | |
1060 | |
1046 | cf::map->attach (prio => -10000, package => cf::mapsupport::); |
1061 | cf::map->attach (prio => -10000, package => cf::mapsupport::); |
1047 | |
1062 | |
1048 | ############################################################################# |
1063 | ############################################################################# |
1049 | # load/save perl data associated with player->ob objects |
|
|
1050 | |
|
|
1051 | sub all_objects(@) { |
|
|
1052 | @_, map all_objects ($_->inv), @_ |
|
|
1053 | } |
|
|
1054 | |
|
|
1055 | # TODO: compatibility cruft, remove when no longer needed |
|
|
1056 | cf::player->attach ( |
|
|
1057 | on_load => sub { |
|
|
1058 | my ($pl, $path) = @_; |
|
|
1059 | |
|
|
1060 | for my $o (all_objects $pl->ob) { |
|
|
1061 | if (my $value = $o->get_ob_key_value ("_perl_data")) { |
|
|
1062 | $o->set_ob_key_value ("_perl_data"); |
|
|
1063 | |
|
|
1064 | %$o = %{ Storable::thaw pack "H*", $value }; |
|
|
1065 | } |
|
|
1066 | } |
|
|
1067 | }, |
|
|
1068 | ); |
|
|
1069 | |
|
|
1070 | ############################################################################# |
|
|
1071 | |
1064 | |
1072 | =head2 CORE EXTENSIONS |
1065 | =head2 CORE EXTENSIONS |
1073 | |
1066 | |
1074 | Functions and methods that extend core crossfire objects. |
1067 | Functions and methods that extend core crossfire objects. |
1075 | |
1068 | |
|
|
1069 | =cut |
|
|
1070 | |
|
|
1071 | package cf::player; |
|
|
1072 | |
|
|
1073 | use Coro::AIO; |
|
|
1074 | |
1076 | =head3 cf::player |
1075 | =head3 cf::player |
1077 | |
1076 | |
1078 | =over 4 |
1077 | =over 4 |
1079 | |
1078 | |
1080 | =item cf::player::exists $login |
1079 | =item cf::player::find $login |
1081 | |
1080 | |
1082 | Returns true when the given account exists. |
1081 | Returns the given player object, loading it if necessary (might block). |
1083 | |
1082 | |
1084 | =cut |
1083 | =cut |
1085 | |
1084 | |
1086 | sub cf::player::exists($) { |
1085 | sub playerdir($) { |
1087 | cf::player::find $_[0] |
1086 | cf::localdir |
1088 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
1087 | . "/" |
|
|
1088 | . cf::playerdir |
|
|
1089 | . "/" |
|
|
1090 | . (ref $_[0] ? $_[0]->ob->name : $_[0]) |
|
|
1091 | } |
|
|
1092 | |
|
|
1093 | sub path($) { |
|
|
1094 | my $login = ref $_[0] ? $_[0]->ob->name : $_[0]; |
|
|
1095 | |
|
|
1096 | (playerdir $login) . "/$login.pl" |
|
|
1097 | } |
|
|
1098 | |
|
|
1099 | sub find_active($) { |
|
|
1100 | $cf::PLAYER{$_[0]} |
|
|
1101 | and $cf::PLAYER{$_[0]}->active |
|
|
1102 | and $cf::PLAYER{$_[0]} |
|
|
1103 | } |
|
|
1104 | |
|
|
1105 | sub exists($) { |
|
|
1106 | my ($login) = @_; |
|
|
1107 | |
|
|
1108 | $cf::PLAYER{$login} |
|
|
1109 | or cf::sync_job { !aio_stat $login } |
|
|
1110 | } |
|
|
1111 | |
|
|
1112 | sub find($) { |
|
|
1113 | return $cf::PLAYER{$_[0]} || do { |
|
|
1114 | my $login = $_[0]; |
|
|
1115 | |
|
|
1116 | my $guard = cf::lock_acquire "user_find:$login"; |
|
|
1117 | |
|
|
1118 | $cf::PLAYER{$_[0]} || do { |
|
|
1119 | my $pl = load_pl path $login |
|
|
1120 | or return; |
|
|
1121 | $cf::PLAYER{$login} = $pl |
|
|
1122 | } |
|
|
1123 | } |
|
|
1124 | } |
|
|
1125 | |
|
|
1126 | sub save($) { |
|
|
1127 | my ($pl) = @_; |
|
|
1128 | |
|
|
1129 | return if $pl->{deny_save}; |
|
|
1130 | |
|
|
1131 | my $path = path $pl; |
|
|
1132 | my $guard = cf::lock_acquire "user_save:$path"; |
|
|
1133 | |
|
|
1134 | return if $pl->{deny_save}; |
|
|
1135 | |
|
|
1136 | aio_mkdir playerdir $pl, 0770; |
|
|
1137 | $pl->{last_save} = $cf::RUNTIME; |
|
|
1138 | |
|
|
1139 | $pl->save_pl ($path); |
|
|
1140 | Coro::cede; |
|
|
1141 | } |
|
|
1142 | |
|
|
1143 | sub new($) { |
|
|
1144 | my ($login) = @_; |
|
|
1145 | |
|
|
1146 | my $self = create; |
|
|
1147 | |
|
|
1148 | $self->ob->name ($login); |
|
|
1149 | $self->{deny_save} = 1; |
|
|
1150 | |
|
|
1151 | $cf::PLAYER{$login} = $self; |
|
|
1152 | |
|
|
1153 | $self |
|
|
1154 | } |
|
|
1155 | |
|
|
1156 | =item $pl->quit_character |
|
|
1157 | |
|
|
1158 | Nukes the player without looking back. If logged in, the connection will |
|
|
1159 | be destroyed. May block for a long time. |
|
|
1160 | |
|
|
1161 | =cut |
|
|
1162 | |
|
|
1163 | sub quit_character { |
|
|
1164 | my ($pl) = @_; |
|
|
1165 | |
|
|
1166 | $pl->{deny_save} = 1; |
|
|
1167 | $pl->password ("*"); # this should lock out the player until we nuked the dir |
|
|
1168 | |
|
|
1169 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; |
|
|
1170 | $pl->deactivate; |
|
|
1171 | $pl->invoke (cf::EVENT_PLAYER_QUIT); |
|
|
1172 | $pl->ns->destroy if $pl->ns; |
|
|
1173 | |
|
|
1174 | my $path = playerdir $pl; |
|
|
1175 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
|
|
1176 | aio_rename $path, $temp; |
|
|
1177 | delete $cf::PLAYER{$pl->ob->name}; |
|
|
1178 | $pl->destroy; |
|
|
1179 | IO::AIO::aio_rmtree $temp; |
|
|
1180 | } |
|
|
1181 | |
|
|
1182 | =item cf::player::list_logins |
|
|
1183 | |
|
|
1184 | Returns am arrayref of all valid playernames in the system, can take a |
|
|
1185 | while and may block, so not sync_job-capable, ever. |
|
|
1186 | |
|
|
1187 | =cut |
|
|
1188 | |
|
|
1189 | sub list_logins { |
|
|
1190 | my $dirs = aio_readdir cf::localdir . "/" . cf::playerdir |
|
|
1191 | or return []; |
|
|
1192 | |
|
|
1193 | my @logins; |
|
|
1194 | |
|
|
1195 | for my $login (@$dirs) { |
|
|
1196 | my $fh = aio_open path $login, Fcntl::O_RDONLY, 0 or next; |
|
|
1197 | aio_read $fh, 0, 512, my $buf, 0 or next; |
|
|
1198 | $buf !~ /^password -------------$/m or next; # official not-valid tag |
|
|
1199 | |
|
|
1200 | utf8::decode $login; |
|
|
1201 | push @logins, $login; |
|
|
1202 | } |
|
|
1203 | |
|
|
1204 | \@logins |
|
|
1205 | } |
|
|
1206 | |
|
|
1207 | =item $player->maps |
|
|
1208 | |
|
|
1209 | Returns an arrayref of cf::path's of all maps that are private for this |
|
|
1210 | player. May block. |
|
|
1211 | |
|
|
1212 | =cut |
|
|
1213 | |
|
|
1214 | sub maps($) { |
|
|
1215 | my ($pl) = @_; |
|
|
1216 | |
|
|
1217 | my $files = aio_readdir playerdir $pl |
|
|
1218 | or return; |
|
|
1219 | |
|
|
1220 | my @paths; |
|
|
1221 | |
|
|
1222 | for (@$files) { |
|
|
1223 | utf8::decode $_; |
|
|
1224 | next if /\.(?:pl|pst)$/; |
|
|
1225 | next unless /^$PATH_SEP/o; |
|
|
1226 | |
|
|
1227 | push @paths, new cf::path "~" . $pl->ob->name . "/" . $_; |
|
|
1228 | } |
|
|
1229 | |
|
|
1230 | \@paths |
1089 | } |
1231 | } |
1090 | |
1232 | |
1091 | =item $player->ext_reply ($msgid, $msgtype, %msg) |
1233 | =item $player->ext_reply ($msgid, $msgtype, %msg) |
1092 | |
1234 | |
1093 | Sends an ext reply to the player. |
1235 | Sends an ext reply to the player. |
1094 | |
1236 | |
1095 | =cut |
1237 | =cut |
1096 | |
1238 | |
1097 | sub cf::player::ext_reply($$$%) { |
1239 | sub ext_reply($$$%) { |
1098 | my ($self, $id, %msg) = @_; |
1240 | my ($self, $id, %msg) = @_; |
1099 | |
1241 | |
1100 | $msg{msgid} = $id; |
1242 | $msg{msgid} = $id; |
1101 | |
1243 | |
1102 | $self->send ("ext " . to_json \%msg); |
1244 | $self->send ("ext " . cf::to_json \%msg); |
1103 | } |
1245 | } |
|
|
1246 | |
|
|
1247 | package cf; |
1104 | |
1248 | |
1105 | =back |
1249 | =back |
1106 | |
1250 | |
1107 | |
1251 | |
1108 | =head3 cf::map |
1252 | =head3 cf::map |
… | |
… | |
1145 | |
1289 | |
1146 | $_->change_map_light ($change) |
1290 | $_->change_map_light ($change) |
1147 | for grep $_->outdoor, values %cf::MAP; |
1291 | for grep $_->outdoor, values %cf::MAP; |
1148 | } |
1292 | } |
1149 | |
1293 | |
1150 | sub try_load_header($) { |
1294 | sub load_map_header($) { |
1151 | my ($path) = @_; |
1295 | my ($path) = @_; |
1152 | |
1296 | |
1153 | utf8::encode $path; |
1297 | utf8::encode $path; |
1154 | aio_open $path, O_RDONLY, 0 |
1298 | aio_open $path, O_RDONLY, 0 |
1155 | or return; |
1299 | or return; |
1156 | |
1300 | |
1157 | my $map = cf::map::new |
1301 | my $map = cf::map::new |
1158 | or return; |
1302 | or return; |
1159 | |
1303 | |
1160 | # for better error messages only, will be overwritten |
1304 | # for better error messages only, will be overwritten later |
1161 | $map->path ($path); |
1305 | $map->path ($path); |
1162 | |
1306 | |
1163 | $map->load_header ($path) |
1307 | $map->load_header ($path) |
1164 | or return; |
1308 | or return; |
1165 | |
1309 | |
… | |
… | |
1181 | |
1325 | |
1182 | $cf::MAP{$key} || do { |
1326 | $cf::MAP{$key} || do { |
1183 | my $guard = cf::lock_acquire "map_find:$key"; |
1327 | my $guard = cf::lock_acquire "map_find:$key"; |
1184 | |
1328 | |
1185 | # do it the slow way |
1329 | # do it the slow way |
1186 | my $map = try_load_header $path->save_path; |
1330 | my $map = $path->load_temp; |
1187 | |
1331 | |
1188 | Coro::cede; |
1332 | Coro::cede; |
1189 | |
1333 | |
1190 | if ($map) { |
1334 | if ($map) { |
1191 | $map->last_access ((delete $map->{last_access}) |
1335 | $map->last_access ((delete $map->{last_access}) |
1192 | || $cf::RUNTIME); #d# |
1336 | || $cf::RUNTIME); #d# |
1193 | # safety |
1337 | # safety |
1194 | $map->{instantiate_time} = $cf::RUNTIME |
1338 | $map->{instantiate_time} = $cf::RUNTIME |
1195 | if $map->{instantiate_time} > $cf::RUNTIME; |
1339 | if $map->{instantiate_time} > $cf::RUNTIME; |
1196 | } else { |
1340 | } else { |
1197 | if (my $rmp = $path->random_map_params) { |
1341 | $map = $path->load_orig |
1198 | $map = generate_random_map $key, $rmp; |
|
|
1199 | } else { |
|
|
1200 | $map = try_load_header $path->load_path; |
|
|
1201 | } |
|
|
1202 | |
|
|
1203 | $map or return; |
1342 | or return; |
1204 | |
1343 | |
1205 | $map->{load_original} = 1; |
1344 | $map->{load_original} = 1; |
1206 | $map->{instantiate_time} = $cf::RUNTIME; |
1345 | $map->{instantiate_time} = $cf::RUNTIME; |
1207 | $map->last_access ($cf::RUNTIME); |
1346 | $map->last_access ($cf::RUNTIME); |
1208 | $map->instantiate; |
1347 | $map->instantiate; |
… | |
… | |
1274 | Coro::cede; |
1413 | Coro::cede; |
1275 | |
1414 | |
1276 | $self->in_memory (cf::MAP_IN_MEMORY); |
1415 | $self->in_memory (cf::MAP_IN_MEMORY); |
1277 | } |
1416 | } |
1278 | |
1417 | |
|
|
1418 | # find and load all maps in the 3x3 area around a map |
|
|
1419 | sub load_diag { |
|
|
1420 | my ($map) = @_; |
|
|
1421 | |
|
|
1422 | my @diag; # diagonal neighbours |
|
|
1423 | |
|
|
1424 | for (0 .. 3) { |
|
|
1425 | my $neigh = $map->tile_path ($_) |
|
|
1426 | or next; |
|
|
1427 | $neigh = find $neigh, $map |
|
|
1428 | or next; |
|
|
1429 | $neigh->load; |
|
|
1430 | |
|
|
1431 | push @diag, [$neigh->tile_path (($_ + 3) % 4), $neigh], |
|
|
1432 | [$neigh->tile_path (($_ + 1) % 4), $neigh]; |
|
|
1433 | } |
|
|
1434 | |
|
|
1435 | for (@diag) { |
|
|
1436 | my $neigh = find @$_ |
|
|
1437 | or next; |
|
|
1438 | $neigh->load; |
|
|
1439 | } |
|
|
1440 | } |
|
|
1441 | |
1279 | sub find_sync { |
1442 | sub find_sync { |
1280 | my ($path, $origin) = @_; |
1443 | my ($path, $origin) = @_; |
1281 | |
1444 | |
1282 | cf::sync_job { cf::map::find $path, $origin } |
1445 | cf::sync_job { find $path, $origin } |
1283 | } |
1446 | } |
1284 | |
1447 | |
1285 | sub do_load_sync { |
1448 | sub do_load_sync { |
1286 | my ($map) = @_; |
1449 | my ($map) = @_; |
1287 | |
1450 | |
1288 | cf::sync_job { $map->load }; |
1451 | cf::sync_job { $map->load }; |
1289 | } |
1452 | } |
1290 | |
1453 | |
|
|
1454 | our %MAP_PREFETCH; |
|
|
1455 | our $MAP_PREFETCHER = Coro::async { |
|
|
1456 | while () { |
|
|
1457 | while (%MAP_PREFETCH) { |
|
|
1458 | my $key = each %MAP_PREFETCH |
|
|
1459 | or next; |
|
|
1460 | my $path = delete $MAP_PREFETCH{$key}; |
|
|
1461 | |
|
|
1462 | my $map = find $path |
|
|
1463 | or next; |
|
|
1464 | $map->load; |
|
|
1465 | } |
|
|
1466 | Coro::schedule; |
|
|
1467 | } |
|
|
1468 | }; |
|
|
1469 | |
|
|
1470 | sub find_async { |
|
|
1471 | my ($path, $origin) = @_; |
|
|
1472 | |
|
|
1473 | $path = new cf::path $path, $origin && $origin->path; |
|
|
1474 | my $key = $path->as_string; |
|
|
1475 | |
|
|
1476 | if (my $map = $cf::MAP{$key}) { |
|
|
1477 | return $map if $map->in_memory == cf::MAP_IN_MEMORY; |
|
|
1478 | } |
|
|
1479 | |
|
|
1480 | $MAP_PREFETCH{$key} = $path; |
|
|
1481 | $MAP_PREFETCHER->ready; |
|
|
1482 | |
|
|
1483 | () |
|
|
1484 | } |
|
|
1485 | |
1291 | sub save { |
1486 | sub save { |
1292 | my ($self) = @_; |
1487 | my ($self) = @_; |
1293 | |
1488 | |
1294 | my $lock = cf::lock_acquire "map_data:" . $self->path; |
1489 | my $lock = cf::lock_acquire "map_data:" . $self->path; |
1295 | |
1490 | |
… | |
… | |
1303 | $self->{load_path} = $save; |
1498 | $self->{load_path} = $save; |
1304 | |
1499 | |
1305 | return if $self->{deny_save}; |
1500 | return if $self->{deny_save}; |
1306 | |
1501 | |
1307 | local $self->{last_access} = $self->last_access;#d# |
1502 | local $self->{last_access} = $self->last_access;#d# |
|
|
1503 | |
|
|
1504 | cf::async { |
|
|
1505 | $_->contr->save for $self->players; |
|
|
1506 | }; |
1308 | |
1507 | |
1309 | if ($uniq) { |
1508 | if ($uniq) { |
1310 | $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS); |
1509 | $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS); |
1311 | $self->save_objects ($uniq, cf::IO_UNIQUES); |
1510 | $self->save_objects ($uniq, cf::IO_UNIQUES); |
1312 | } else { |
1511 | } else { |
… | |
… | |
1407 | } |
1606 | } |
1408 | |
1607 | |
1409 | $map |
1608 | $map |
1410 | } |
1609 | } |
1411 | |
1610 | |
1412 | sub emergency_save { |
1611 | =item cf::map::unique_maps |
1413 | my $freeze_guard = cf::freeze_mainloop; |
|
|
1414 | |
1612 | |
1415 | warn "enter emergency map save\n"; |
1613 | Returns an arrayref of cf::path's of all shared maps that have |
|
|
1614 | instantiated unique items. May block. |
1416 | |
1615 | |
1417 | cf::sync_job { |
1616 | =cut |
1418 | warn "begin emergency map save\n"; |
1617 | |
1419 | $_->save for values %cf::MAP; |
1618 | sub unique_maps() { |
|
|
1619 | my $files = aio_readdir cf::localdir . "/" . cf::uniquedir |
|
|
1620 | or return; |
|
|
1621 | |
|
|
1622 | my @paths; |
|
|
1623 | |
|
|
1624 | for (@$files) { |
|
|
1625 | utf8::decode $_; |
|
|
1626 | next if /\.pst$/; |
|
|
1627 | next unless /^$PATH_SEP/o; |
|
|
1628 | |
|
|
1629 | push @paths, new cf::path $_; |
1420 | }; |
1630 | } |
1421 | |
1631 | |
1422 | warn "end emergency map save\n"; |
1632 | \@paths |
1423 | } |
1633 | } |
1424 | |
1634 | |
1425 | package cf; |
1635 | package cf; |
1426 | |
1636 | |
1427 | =back |
1637 | =back |
1428 | |
1638 | |
|
|
1639 | =head3 cf::object |
|
|
1640 | |
|
|
1641 | =cut |
|
|
1642 | |
|
|
1643 | package cf::object; |
|
|
1644 | |
|
|
1645 | =over 4 |
|
|
1646 | |
|
|
1647 | =item $ob->inv_recursive |
|
|
1648 | |
|
|
1649 | Returns the inventory of the object _and_ their inventories, recursively. |
|
|
1650 | |
|
|
1651 | =cut |
|
|
1652 | |
|
|
1653 | sub inv_recursive_; |
|
|
1654 | sub inv_recursive_ { |
|
|
1655 | map { $_, inv_recursive_ $_->inv } @_ |
|
|
1656 | } |
|
|
1657 | |
|
|
1658 | sub inv_recursive { |
|
|
1659 | inv_recursive_ inv $_[0] |
|
|
1660 | } |
|
|
1661 | |
|
|
1662 | package cf; |
|
|
1663 | |
|
|
1664 | =back |
1429 | |
1665 | |
1430 | =head3 cf::object::player |
1666 | =head3 cf::object::player |
1431 | |
1667 | |
1432 | =over 4 |
1668 | =over 4 |
1433 | |
1669 | |
… | |
… | |
1525 | # use -1 or undef as default coordinates, not 0, 0 |
1761 | # use -1 or undef as default coordinates, not 0, 0 |
1526 | ($x, $y) = ($map->enter_x, $map->enter_y) |
1762 | ($x, $y) = ($map->enter_x, $map->enter_y) |
1527 | if $x <=0 && $y <= 0; |
1763 | if $x <=0 && $y <= 0; |
1528 | |
1764 | |
1529 | $map->load; |
1765 | $map->load; |
|
|
1766 | $map->load_diag; |
1530 | |
1767 | |
|
|
1768 | return unless $self->contr->active; |
1531 | $self->activate_recursive; |
1769 | $self->activate_recursive; |
1532 | $self->enter_map ($map, $x, $y); |
1770 | $self->enter_map ($map, $x, $y); |
1533 | } |
1771 | } |
1534 | |
1772 | |
1535 | cf::player->attach ( |
1773 | cf::player->attach ( |
… | |
… | |
1547 | my ($pl) = @_; |
1785 | my ($pl) = @_; |
1548 | |
1786 | |
1549 | # try to abort aborted map switching on player login :) |
1787 | # try to abort aborted map switching on player login :) |
1550 | # should happen only on crashes |
1788 | # should happen only on crashes |
1551 | if ($pl->ob->{_link_pos}) { |
1789 | if ($pl->ob->{_link_pos}) { |
1552 | |
|
|
1553 | $pl->ob->enter_link; |
1790 | $pl->ob->enter_link; |
1554 | (async { |
1791 | (async { |
1555 | # we need this sleep as the login has a concurrent enter_exit running |
1792 | # we need this sleep as the login has a concurrent enter_exit running |
1556 | # and this sleep increases chances of the player not ending up in scorn |
1793 | # and this sleep increases chances of the player not ending up in scorn |
1557 | $pl->ob->reply (undef, |
1794 | $pl->ob->reply (undef, |
… | |
… | |
1570 | =cut |
1807 | =cut |
1571 | |
1808 | |
1572 | sub cf::object::player::goto { |
1809 | sub cf::object::player::goto { |
1573 | my ($self, $path, $x, $y) = @_; |
1810 | my ($self, $path, $x, $y) = @_; |
1574 | |
1811 | |
|
|
1812 | $path = new cf::path $path; |
|
|
1813 | |
1575 | $self->enter_link; |
1814 | $self->enter_link; |
1576 | |
1815 | |
1577 | (async { |
1816 | (async { |
1578 | $path = new cf::path $path; |
|
|
1579 | |
|
|
1580 | my $map = cf::map::find $path->as_string; |
1817 | my $map = cf::map::find $path->as_string; |
1581 | $map = $map->customise_for ($self) if $map; |
1818 | $map = $map->customise_for ($self) if $map; |
1582 | |
1819 | |
1583 | # warn "entering ", $map->path, " at ($x, $y)\n" |
1820 | # warn "entering ", $map->path, " at ($x, $y)\n" |
1584 | # if $map; |
1821 | # if $map; |
1585 | |
1822 | |
1586 | $map or $self->message ("The exit is closed", cf::NDI_UNIQUE | cf::NDI_RED); |
1823 | $map or $self->message ("The exit to '" . ($path->visible_name) . "' is closed", cf::NDI_UNIQUE | cf::NDI_RED); |
1587 | |
1824 | |
1588 | $self->leave_link ($map, $x, $y); |
1825 | $self->leave_link ($map, $x, $y); |
1589 | })->prio (1); |
1826 | })->prio (1); |
1590 | } |
1827 | } |
1591 | |
1828 | |
… | |
… | |
1657 | |
1894 | |
1658 | 1; |
1895 | 1; |
1659 | }) { |
1896 | }) { |
1660 | $self->message ("Something went wrong deep within the crossfire server. " |
1897 | $self->message ("Something went wrong deep within the crossfire server. " |
1661 | . "I'll try to bring you back to the map you were before. " |
1898 | . "I'll try to bring you back to the map you were before. " |
1662 | . "Please report this to the dungeon master", |
1899 | . "Please report this to the dungeon master!", |
1663 | cf::NDI_UNIQUE | cf::NDI_RED); |
1900 | cf::NDI_UNIQUE | cf::NDI_RED); |
1664 | |
1901 | |
1665 | warn "ERROR in enter_exit: $@"; |
1902 | warn "ERROR in enter_exit: $@"; |
1666 | $self->leave_link; |
1903 | $self->leave_link; |
1667 | } |
1904 | } |
… | |
… | |
1999 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
2236 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
2000 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
2237 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
2001 | |
2238 | |
2002 | if (exists $CFG{mlockall}) { |
2239 | if (exists $CFG{mlockall}) { |
2003 | eval { |
2240 | eval { |
2004 | $CFG{mlockall} ? &mlockall : &munlockall |
2241 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
2005 | and die "WARNING: m(un)lockall failed: $!\n"; |
2242 | and die "WARNING: m(un)lockall failed: $!\n"; |
2006 | }; |
2243 | }; |
2007 | warn $@ if $@; |
2244 | warn $@ if $@; |
2008 | } |
2245 | } |
2009 | } |
2246 | } |
… | |
… | |
2020 | load_extensions; |
2257 | load_extensions; |
2021 | Event::loop; |
2258 | Event::loop; |
2022 | } |
2259 | } |
2023 | |
2260 | |
2024 | ############################################################################# |
2261 | ############################################################################# |
2025 | # initialisation |
2262 | # initialisation and cleanup |
|
|
2263 | |
|
|
2264 | # install some emergency cleanup handlers |
|
|
2265 | BEGIN { |
|
|
2266 | for my $signal (qw(INT HUP TERM)) { |
|
|
2267 | Event->signal ( |
|
|
2268 | data => WF_AUTOCANCEL, |
|
|
2269 | signal => $signal, |
|
|
2270 | cb => sub { |
|
|
2271 | cf::cleanup "SIG$signal"; |
|
|
2272 | }, |
|
|
2273 | ); |
|
|
2274 | } |
|
|
2275 | } |
|
|
2276 | |
|
|
2277 | sub emergency_save() { |
|
|
2278 | my $freeze_guard = cf::freeze_mainloop; |
|
|
2279 | |
|
|
2280 | warn "enter emergency perl save\n"; |
|
|
2281 | |
|
|
2282 | cf::sync_job { |
|
|
2283 | # use a peculiar iteration method to avoid tripping on perl |
|
|
2284 | # refcount bugs in for. also avoids problems with players |
|
|
2285 | # and maps saved/Destroyed asynchronously. |
|
|
2286 | warn "begin emergency player save\n"; |
|
|
2287 | for my $login (keys %cf::PLAYER) { |
|
|
2288 | my $pl = $cf::PLAYER{$login} or next; |
|
|
2289 | $pl->valid or next; |
|
|
2290 | $pl->save; |
|
|
2291 | } |
|
|
2292 | warn "end emergency player save\n"; |
|
|
2293 | |
|
|
2294 | warn "begin emergency map save\n"; |
|
|
2295 | for my $path (keys %cf::MAP) { |
|
|
2296 | my $map = $cf::MAP{$path} or next; |
|
|
2297 | $map->valid or next; |
|
|
2298 | $map->save; |
|
|
2299 | } |
|
|
2300 | warn "end emergency map save\n"; |
|
|
2301 | }; |
|
|
2302 | |
|
|
2303 | warn "leave emergency perl save\n"; |
|
|
2304 | } |
2026 | |
2305 | |
2027 | sub reload() { |
2306 | sub reload() { |
2028 | # can/must only be called in main |
2307 | # can/must only be called in main |
2029 | if ($Coro::current != $Coro::main) { |
2308 | if ($Coro::current != $Coro::main) { |
2030 | warn "can only reload from main coroutine\n"; |
2309 | warn "can only reload from main coroutine\n"; |
2031 | return; |
2310 | return; |
2032 | } |
2311 | } |
2033 | |
2312 | |
2034 | warn "reloading..."; |
2313 | warn "reloading..."; |
2035 | |
2314 | |
|
|
2315 | warn "freezing server"; |
2036 | my $guard = freeze_mainloop; |
2316 | my $guard = freeze_mainloop; |
2037 | cf::emergency_save; |
2317 | cf::emergency_save; |
2038 | |
2318 | |
|
|
2319 | warn "sync database to disk"; |
|
|
2320 | cf::db_sync; |
|
|
2321 | IO::AIO::flush; |
|
|
2322 | |
2039 | eval { |
2323 | eval { |
2040 | # if anything goes wrong in here, we should simply crash as we already saved |
2324 | # if anything goes wrong in here, we should simply crash as we already saved |
2041 | |
2325 | |
2042 | # cancel all watchers |
2326 | warn "cancel all watchers"; |
2043 | for (Event::all_watchers) { |
2327 | for (Event::all_watchers) { |
2044 | $_->cancel if $_->data & WF_AUTOCANCEL; |
2328 | $_->cancel if $_->data & WF_AUTOCANCEL; |
2045 | } |
2329 | } |
2046 | |
2330 | |
2047 | # cancel all extension coros |
2331 | warn "cancel all extension coros"; |
2048 | $_->cancel for values %EXT_CORO; |
2332 | $_->cancel for values %EXT_CORO; |
2049 | %EXT_CORO = (); |
2333 | %EXT_CORO = (); |
2050 | |
2334 | |
|
|
2335 | warn "remove commands"; |
|
|
2336 | %COMMAND = (); |
|
|
2337 | |
|
|
2338 | warn "remove ext commands"; |
|
|
2339 | %EXTCMD = (); |
|
|
2340 | |
2051 | # unload all extensions |
2341 | warn "unload/nuke all extensions"; |
2052 | for (@exts) { |
2342 | for my $pkg (@EXTS) { |
2053 | warn "unloading <$_>"; |
2343 | warn "... unloading $pkg"; |
2054 | unload_extension $_; |
2344 | |
|
|
2345 | if (my $cb = $pkg->can ("unload")) { |
|
|
2346 | eval { |
|
|
2347 | $cb->($pkg); |
|
|
2348 | 1 |
|
|
2349 | } or warn "$pkg unloaded, but with errors: $@"; |
2055 | } |
2350 | } |
2056 | |
2351 | |
|
|
2352 | warn "... nuking $pkg"; |
|
|
2353 | Symbol::delete_package $pkg; |
|
|
2354 | } |
|
|
2355 | |
2057 | # unload all modules loaded from $LIBDIR |
2356 | warn "unload all perl modules loaded from $LIBDIR"; |
2058 | while (my ($k, $v) = each %INC) { |
2357 | while (my ($k, $v) = each %INC) { |
2059 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
2358 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
2060 | |
2359 | |
2061 | warn "removing <$k>"; |
2360 | warn "removing <$k>"; |
2062 | delete $INC{$k}; |
2361 | delete $INC{$k}; |
… | |
… | |
2069 | } |
2368 | } |
2070 | |
2369 | |
2071 | Symbol::delete_package $k; |
2370 | Symbol::delete_package $k; |
2072 | } |
2371 | } |
2073 | |
2372 | |
2074 | # sync database to disk |
|
|
2075 | cf::db_sync; |
|
|
2076 | IO::AIO::flush; |
|
|
2077 | |
|
|
2078 | # get rid of safe::, as good as possible |
2373 | warn "get rid of safe::, as good as possible"; |
2079 | Symbol::delete_package "safe::$_" |
2374 | Symbol::delete_package "safe::$_" |
2080 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
2375 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
2081 | |
2376 | |
2082 | # remove register_script_function callbacks |
|
|
2083 | # TODO |
|
|
2084 | |
|
|
2085 | # unload cf.pm "a bit" |
2377 | warn "unload cf.pm \"a bit\""; |
2086 | delete $INC{"cf.pm"}; |
2378 | delete $INC{"cf.pm"}; |
2087 | |
2379 | |
2088 | # don't, removes xs symbols, too, |
2380 | # don't, removes xs symbols, too, |
2089 | # and global variables created in xs |
2381 | # and global variables created in xs |
2090 | #Symbol::delete_package __PACKAGE__; |
2382 | #Symbol::delete_package __PACKAGE__; |
2091 | |
2383 | |
2092 | # reload cf.pm |
|
|
2093 | warn "reloading cf.pm"; |
2384 | warn "reloading cf.pm"; |
2094 | require cf; |
2385 | require cf; |
2095 | cf::_connect_to_perl; # nominally unnecessary, but cannot hurt |
2386 | cf::_connect_to_perl; # nominally unnecessary, but cannot hurt |
2096 | |
2387 | |
2097 | # load config and database again |
2388 | warn "load config and database again"; |
2098 | cf::cfg_load; |
2389 | cf::cfg_load; |
2099 | cf::db_load; |
2390 | cf::db_load; |
2100 | |
2391 | |
2101 | # load extensions |
|
|
2102 | warn "load extensions"; |
2392 | warn "load extensions"; |
2103 | cf::load_extensions; |
2393 | cf::load_extensions; |
2104 | |
2394 | |
2105 | # reattach attachments to objects |
2395 | warn "reattach attachments to objects/players"; |
2106 | warn "reattach"; |
|
|
2107 | _global_reattach; |
2396 | _global_reattach; |
|
|
2397 | warn "reattach attachments to maps"; |
|
|
2398 | reattach $_ for values %MAP; |
2108 | }; |
2399 | }; |
2109 | |
2400 | |
2110 | if ($@) { |
2401 | if ($@) { |
2111 | warn $@; |
2402 | warn $@; |
2112 | warn "error while reloading, exiting."; |
2403 | warn "error while reloading, exiting."; |
2113 | exit 1; |
2404 | exit 1; |
2114 | } |
2405 | } |
2115 | |
2406 | |
2116 | warn "reloaded successfully"; |
2407 | warn "reloaded"; |
2117 | }; |
2408 | }; |
2118 | |
2409 | |
2119 | ############################################################################# |
2410 | ############################################################################# |
2120 | |
2411 | |
2121 | unless ($LINK_MAP) { |
2412 | unless ($LINK_MAP) { |
… | |
… | |
2128 | $LINK_MAP->{path} = bless { path => "{link}" }, "cf::path"; |
2419 | $LINK_MAP->{path} = bless { path => "{link}" }, "cf::path"; |
2129 | $LINK_MAP->in_memory (MAP_IN_MEMORY); |
2420 | $LINK_MAP->in_memory (MAP_IN_MEMORY); |
2130 | |
2421 | |
2131 | # dirty hack because... archetypes are not yet loaded |
2422 | # dirty hack because... archetypes are not yet loaded |
2132 | Event->timer ( |
2423 | Event->timer ( |
2133 | after => 2, |
2424 | after => 10, |
2134 | cb => sub { |
2425 | cb => sub { |
2135 | $_[0]->w->cancel; |
2426 | $_[0]->w->cancel; |
2136 | |
2427 | |
2137 | # provide some exits "home" |
2428 | # provide some exits "home" |
2138 | my $exit = cf::object::new "exit"; |
2429 | my $exit = cf::object::new "exit"; |
… | |
… | |
2156 | $LINK_MAP->{deny_save} = 1; |
2447 | $LINK_MAP->{deny_save} = 1; |
2157 | $LINK_MAP->{deny_reset} = 1; |
2448 | $LINK_MAP->{deny_reset} = 1; |
2158 | |
2449 | |
2159 | $cf::MAP{$LINK_MAP->path} = $LINK_MAP; |
2450 | $cf::MAP{$LINK_MAP->path} = $LINK_MAP; |
2160 | } |
2451 | } |
2161 | |
|
|
2162 | register "<global>", __PACKAGE__; |
|
|
2163 | |
2452 | |
2164 | register_command "reload" => sub { |
2453 | register_command "reload" => sub { |
2165 | my ($who, $arg) = @_; |
2454 | my ($who, $arg) = @_; |
2166 | |
2455 | |
2167 | if ($who->flag (FLAG_WIZ)) { |
2456 | if ($who->flag (FLAG_WIZ)) { |
… | |
… | |
2180 | data => WF_AUTOCANCEL, |
2469 | data => WF_AUTOCANCEL, |
2181 | cb => sub { |
2470 | cb => sub { |
2182 | cf::server_tick; # one server iteration |
2471 | cf::server_tick; # one server iteration |
2183 | $RUNTIME += $TICK; |
2472 | $RUNTIME += $TICK; |
2184 | $NEXT_TICK += $TICK; |
2473 | $NEXT_TICK += $TICK; |
|
|
2474 | |
|
|
2475 | $WAIT_FOR_TICK->broadcast; |
|
|
2476 | $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited; |
2185 | |
2477 | |
2186 | # if we are delayed by four ticks or more, skip them all |
2478 | # if we are delayed by four ticks or more, skip them all |
2187 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
2479 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
2188 | |
2480 | |
2189 | $TICK_WATCHER->at ($NEXT_TICK); |
2481 | $TICK_WATCHER->at ($NEXT_TICK); |