… | |
… | |
29 | use Fcntl; |
29 | use Fcntl; |
30 | use YAML::Syck (); |
30 | use YAML::Syck (); |
31 | use IO::AIO 2.32 (); |
31 | use IO::AIO 2.32 (); |
32 | use Time::HiRes; |
32 | use Time::HiRes; |
33 | use Compress::LZF; |
33 | use Compress::LZF; |
|
|
34 | use Digest::MD5 (); |
34 | |
35 | |
35 | # configure various modules to our taste |
36 | # configure various modules to our taste |
36 | # |
37 | # |
37 | $Storable::canonical = 1; # reduce rsync transfers |
38 | $Storable::canonical = 1; # reduce rsync transfers |
38 | Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator |
39 | Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator |
… | |
… | |
165 | =item cf::wait_for_tick, cf::wait_for_tick_begin |
166 | =item cf::wait_for_tick, cf::wait_for_tick_begin |
166 | |
167 | |
167 | These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only |
168 | These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only |
168 | returns directly I<after> the tick processing (and consequently, can only wake one process |
169 | returns directly I<after> the tick processing (and consequently, can only wake one process |
169 | per tick), while cf::wait_for_tick wakes up all waiters after tick processing. |
170 | per tick), while cf::wait_for_tick wakes up all waiters after tick processing. |
|
|
171 | |
|
|
172 | =item @cf::INVOKE_RESULTS |
|
|
173 | |
|
|
174 | This array contains the results of the last C<invoke ()> call. When |
|
|
175 | C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of |
|
|
176 | that call. |
170 | |
177 | |
171 | =back |
178 | =back |
172 | |
179 | |
173 | =cut |
180 | =cut |
174 | |
181 | |
… | |
… | |
248 | |
255 | |
249 | Converts a perl data structure into its JSON representation. |
256 | Converts a perl data structure into its JSON representation. |
250 | |
257 | |
251 | =cut |
258 | =cut |
252 | |
259 | |
253 | our $json_coder = JSON::XS->new->convert_blessed->utf8->max_size (1e6); # accept ~1mb max |
260 | our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max |
254 | |
261 | |
255 | sub to_json ($) { $json_coder->encode ($_[0]) } |
262 | sub to_json ($) { $json_coder->encode ($_[0]) } |
256 | sub from_json ($) { $json_coder->decode ($_[0]) } |
263 | sub from_json ($) { $json_coder->decode ($_[0]) } |
257 | |
264 | |
258 | =item cf::lock_wait $string |
265 | =item cf::lock_wait $string |
… | |
… | |
313 | my $guard = Coro::guard { |
320 | my $guard = Coro::guard { |
314 | $TICK_WATCHER->start; |
321 | $TICK_WATCHER->start; |
315 | }; |
322 | }; |
316 | $TICK_WATCHER->stop; |
323 | $TICK_WATCHER->stop; |
317 | $guard |
324 | $guard |
|
|
325 | } |
|
|
326 | |
|
|
327 | =item cf::get_slot $time[, $priority] |
|
|
328 | |
|
|
329 | Allocate $time seconds of blocking CPU time at priority C<$priority>: |
|
|
330 | This call blocks and returns only when you have at least C<$time> seconds |
|
|
331 | of cpu time till the next tick. The slot is only valid till the next cede. |
|
|
332 | |
|
|
333 | Useful for short background jobs. |
|
|
334 | |
|
|
335 | =cut |
|
|
336 | |
|
|
337 | our @SLOT_QUEUE; |
|
|
338 | our $SLOT_QUEUE; |
|
|
339 | |
|
|
340 | $SLOT_QUEUE->cancel if $SLOT_QUEUE; |
|
|
341 | $SLOT_QUEUE = Coro::async { |
|
|
342 | my $signal = new Coro::Signal; |
|
|
343 | |
|
|
344 | while () { |
|
|
345 | next_job: |
|
|
346 | my $avail = cf::till_tick; |
|
|
347 | if ($avail > 0.01) { |
|
|
348 | for (0 .. $#SLOT_QUEUE) { |
|
|
349 | if ($SLOT_QUEUE[$_][0] < $avail) { |
|
|
350 | my $job = splice @SLOT_QUEUE, $_, 1, (); |
|
|
351 | $job->[2]->send; |
|
|
352 | Coro::cede; |
|
|
353 | goto next_job; |
|
|
354 | } |
|
|
355 | } |
|
|
356 | } |
|
|
357 | |
|
|
358 | if (@SLOT_QUEUE) { |
|
|
359 | # we do not use wait_For_tick() as it returns immediately when tick is inactive |
|
|
360 | push @cf::WAIT_FOR_TICK, $signal; |
|
|
361 | $signal->wait; |
|
|
362 | } else { |
|
|
363 | Coro::schedule; |
|
|
364 | } |
|
|
365 | } |
|
|
366 | }; |
|
|
367 | |
|
|
368 | sub get_slot($;$) { |
|
|
369 | my ($time, $pri) = @_; |
|
|
370 | |
|
|
371 | push @SLOT_QUEUE, [$time, $pri, my $sig = new Coro::Signal]; |
|
|
372 | @SLOT_QUEUE = sort { $b->[1] <=> $a->[1] } @SLOT_QUEUE; |
|
|
373 | $SLOT_QUEUE->ready; |
|
|
374 | $sig->wait; |
318 | } |
375 | } |
319 | |
376 | |
320 | =item cf::async { BLOCK } |
377 | =item cf::async { BLOCK } |
321 | |
378 | |
322 | Currently the same as Coro::async_pool, meaning you cannot use |
379 | Currently the same as Coro::async_pool, meaning you cannot use |
… | |
… | |
409 | Coro::Storable. May, of course, block. Note that the executed sub may |
466 | Coro::Storable. May, of course, block. Note that the executed sub may |
410 | never block itself or use any form of Event handling. |
467 | never block itself or use any form of Event handling. |
411 | |
468 | |
412 | =cut |
469 | =cut |
413 | |
470 | |
|
|
471 | sub _store_scalar { |
|
|
472 | open my $fh, ">", \my $buf |
|
|
473 | or die "fork_call: cannot open fh-to-buf in child : $!"; |
|
|
474 | Storable::store_fd $_[0], $fh; |
|
|
475 | close $fh; |
|
|
476 | |
|
|
477 | $buf |
|
|
478 | } |
|
|
479 | |
414 | sub fork_call(&@) { |
480 | sub fork_call(&@) { |
415 | my ($cb, @args) = @_; |
481 | my ($cb, @args) = @_; |
416 | |
482 | |
417 | # socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC |
483 | # socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC |
418 | # or die "socketpair: $!"; |
484 | # or die "socketpair: $!"; |
… | |
… | |
421 | |
487 | |
422 | if (my $pid = fork) { |
488 | if (my $pid = fork) { |
423 | close $fh2; |
489 | close $fh2; |
424 | |
490 | |
425 | my $res = (Coro::Handle::unblock $fh1)->readline (undef); |
491 | my $res = (Coro::Handle::unblock $fh1)->readline (undef); |
|
|
492 | warn "pst<$res>" unless $res =~ /^pst/; |
426 | $res = Coro::Storable::thaw $res; |
493 | $res = Coro::Storable::thaw $res; |
427 | |
494 | |
428 | waitpid $pid, 0; # should not block anymore, we expect the child to simply behave |
495 | waitpid $pid, 0; # should not block anymore, we expect the child to simply behave |
429 | |
496 | |
430 | die $$res unless "ARRAY" eq ref $res; |
497 | Carp::confess $$res unless "ARRAY" eq ref $res; |
431 | |
498 | |
432 | return wantarray ? @$res : $res->[-1]; |
499 | return wantarray ? @$res : $res->[-1]; |
433 | } else { |
500 | } else { |
434 | reset_signals; |
501 | reset_signals; |
435 | local $SIG{__WARN__}; |
502 | local $SIG{__WARN__}; |
436 | local $SIG{__DIE__}; |
503 | local $SIG{__DIE__}; |
|
|
504 | # just in case, this hack effectively disables event |
|
|
505 | # in the child. cleaner and slower would be canceling all watchers, |
|
|
506 | # but this works for the time being. |
|
|
507 | local $Coro::idle; |
|
|
508 | $Coro::current->prio (Coro::PRIO_MAX); |
|
|
509 | |
437 | eval { |
510 | eval { |
438 | close $fh1; |
511 | close $fh1; |
439 | |
512 | |
440 | my @res = eval { $cb->(@args) }; |
513 | my @res = eval { $cb->(@args) }; |
|
|
514 | |
441 | syswrite $fh2, Coro::Storable::freeze +($@ ? \"$@" : \@res); |
515 | syswrite $fh2, _store_scalar $@ ? \"$@" : \@res; |
|
|
516 | close $fh2; |
442 | }; |
517 | }; |
443 | |
518 | |
444 | warn $@ if $@; |
519 | warn $@ if $@; |
445 | _exit 0; |
520 | _exit 0; |
446 | } |
521 | } |
… | |
… | |
847 | "; |
922 | "; |
848 | die if $@; |
923 | die if $@; |
849 | } |
924 | } |
850 | |
925 | |
851 | our $override; |
926 | our $override; |
852 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
927 | our @INVOKE_RESULTS = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
853 | |
928 | |
854 | sub override { |
929 | sub override { |
855 | $override = 1; |
930 | $override = 1; |
856 | @invoke_results = (); |
931 | @INVOKE_RESULTS = (@_); |
857 | } |
932 | } |
858 | |
933 | |
859 | sub do_invoke { |
934 | sub do_invoke { |
860 | my $event = shift; |
935 | my $event = shift; |
861 | my $callbacks = shift; |
936 | my $callbacks = shift; |
862 | |
937 | |
863 | @invoke_results = (); |
938 | @INVOKE_RESULTS = (); |
864 | |
939 | |
865 | local $override; |
940 | local $override; |
866 | |
941 | |
867 | for (@$callbacks) { |
942 | for (@$callbacks) { |
868 | eval { &{$_->[1]} }; |
943 | eval { &{$_->[1]} }; |
… | |
… | |
885 | |
960 | |
886 | Generate an object-specific event with the given arguments. |
961 | Generate an object-specific event with the given arguments. |
887 | |
962 | |
888 | This API is preliminary (most likely, the EVENT_CLASS_xxx prefix will be |
963 | This API is preliminary (most likely, the EVENT_CLASS_xxx prefix will be |
889 | removed in future versions), and there is no public API to access override |
964 | removed in future versions), and there is no public API to access override |
890 | results (if you must, access C<@cf::invoke_results> directly). |
965 | results (if you must, access C<@cf::INVOKE_RESULTS> directly). |
891 | |
966 | |
892 | =back |
967 | =back |
893 | |
968 | |
894 | =cut |
969 | =cut |
895 | |
970 | |
896 | ############################################################################# |
971 | ############################################################################# |
897 | # object support |
972 | # object support |
|
|
973 | # |
|
|
974 | |
|
|
975 | sub _can_merge { |
|
|
976 | my ($ob1, $ob2) = @_; |
|
|
977 | |
|
|
978 | local $Storable::canonical = 1; |
|
|
979 | my $fob1 = Storable::freeze $ob1; |
|
|
980 | my $fob2 = Storable::freeze $ob2; |
|
|
981 | |
|
|
982 | $fob1 eq $fob2 |
|
|
983 | } |
898 | |
984 | |
899 | sub reattach { |
985 | sub reattach { |
900 | # basically do the same as instantiate, without calling instantiate |
986 | # basically do the same as instantiate, without calling instantiate |
901 | my ($obj) = @_; |
987 | my ($obj) = @_; |
902 | |
988 | |
… | |
… | |
1086 | cf::override; |
1172 | cf::override; |
1087 | }, |
1173 | }, |
1088 | on_extcmd => sub { |
1174 | on_extcmd => sub { |
1089 | my ($pl, $buf) = @_; |
1175 | my ($pl, $buf) = @_; |
1090 | |
1176 | |
1091 | my $msg = eval { from_json $buf }; |
1177 | my $msg = eval { $pl->ns->{json_coder}->decode ($buf) }; |
1092 | |
1178 | |
1093 | if (ref $msg) { |
1179 | if (ref $msg) { |
1094 | if (my $cb = $EXTCMD{$msg->{msgtype}}) { |
1180 | if (my $cb = $EXTCMD{$msg->{msgtype}}) { |
1095 | if (my %reply = $cb->($pl, $msg)) { |
1181 | if (my %reply = $cb->($pl, $msg)) { |
1096 | $pl->ext_reply ($msg->{msgid}, %reply); |
1182 | $pl->ext_reply ($msg->{msgid}, %reply); |
… | |
… | |
1400 | # replace G<male|female> tags |
1486 | # replace G<male|female> tags |
1401 | || s{G<([^>|]*)\|([^>]*)>}{ |
1487 | || s{G<([^>|]*)\|([^>]*)>}{ |
1402 | $self->gender ? $2 : $1 |
1488 | $self->gender ? $2 : $1 |
1403 | }ge |
1489 | }ge |
1404 | # replace H<hint text> |
1490 | # replace H<hint text> |
1405 | || s/H<([^\>]*)>/<fg name="lightblue">[$1]<\/fg>/g; |
1491 | || s{H<([^\>]*)>} |
|
|
1492 | { |
|
|
1493 | ("<fg name=\"lightblue\">[$1 (Use hintmode to suppress hints)]</fg>", |
|
|
1494 | "<fg name=\"lightblue\">[Hint suppressed, see hintmode]</fg>", |
|
|
1495 | "") |
|
|
1496 | [$self->{hintmode}] |
|
|
1497 | }ge; |
1406 | |
1498 | |
1407 | # create single paragraphs (very hackish) |
1499 | # create single paragraphs (very hackish) |
1408 | s/(?<=\S)\n(?=\w)/ /g; |
1500 | s/(?<=\S)\n(?=\w)/ /g; |
1409 | |
1501 | |
|
|
1502 | # compress some whitespace |
|
|
1503 | s/\s+\n/\n/g; # ws line-ends |
|
|
1504 | s/\n\n+/\n/g; # double lines |
|
|
1505 | s/^\n+//; # beginning lines |
|
|
1506 | s/\n+$//; # ending lines |
|
|
1507 | |
1410 | $_ |
1508 | $_ |
|
|
1509 | } |
|
|
1510 | |
|
|
1511 | sub hintmode { |
|
|
1512 | $_[0]{hintmode} = $_[1] if @_ > 1; |
|
|
1513 | $_[0]{hintmode} |
1411 | } |
1514 | } |
1412 | |
1515 | |
1413 | =item $player->ext_reply ($msgid, %msg) |
1516 | =item $player->ext_reply ($msgid, %msg) |
1414 | |
1517 | |
1415 | Sends an ext reply to the player. |
1518 | Sends an ext reply to the player. |
… | |
… | |
1418 | |
1521 | |
1419 | sub ext_reply($$%) { |
1522 | sub ext_reply($$%) { |
1420 | my ($self, $id, %msg) = @_; |
1523 | my ($self, $id, %msg) = @_; |
1421 | |
1524 | |
1422 | $msg{msgid} = $id; |
1525 | $msg{msgid} = $id; |
1423 | $self->send ("ext " . cf::to_json \%msg); |
1526 | $self->send ("ext " . $self->ns->{json_coder}->encode (\%msg)); |
1424 | } |
1527 | } |
1425 | |
1528 | |
1426 | =item $player->ext_event ($type, %msg) |
1529 | =item $player->ext_event ($type, %msg) |
1427 | |
1530 | |
1428 | Sends an ext event to the client. |
1531 | Sends an ext event to the client. |
… | |
… | |
1453 | my ($path) = @_; |
1556 | my ($path) = @_; |
1454 | |
1557 | |
1455 | my ($match, $specificity); |
1558 | my ($match, $specificity); |
1456 | |
1559 | |
1457 | for my $region (list) { |
1560 | for my $region (list) { |
1458 | if ($region->match && $path =~ $region->match) { |
1561 | if ($region->{match} && $path =~ $region->{match}) { |
1459 | ($match, $specificity) = ($region, $region->specificity) |
1562 | ($match, $specificity) = ($region, $region->specificity) |
1460 | if $region->specificity > $specificity; |
1563 | if $region->specificity > $specificity; |
1461 | } |
1564 | } |
1462 | } |
1565 | } |
1463 | |
1566 | |
… | |
… | |
1583 | $self->init; # pass $1 etc. |
1686 | $self->init; # pass $1 etc. |
1584 | return $self; |
1687 | return $self; |
1585 | } |
1688 | } |
1586 | } |
1689 | } |
1587 | |
1690 | |
1588 | Carp::carp "unable to resolve path '$path' (base '$base')."; |
1691 | Carp::cluck "unable to resolve path '$path' (base '$base')."; |
1589 | () |
1692 | () |
1590 | } |
1693 | } |
1591 | |
1694 | |
1592 | sub init { |
1695 | sub init { |
1593 | my ($self) = @_; |
1696 | my ($self) = @_; |
… | |
… | |
1871 | |
1974 | |
1872 | undef $MAP_PREFETCH{$path}; |
1975 | undef $MAP_PREFETCH{$path}; |
1873 | $MAP_PREFETCHER ||= cf::async { |
1976 | $MAP_PREFETCHER ||= cf::async { |
1874 | while (%MAP_PREFETCH) { |
1977 | while (%MAP_PREFETCH) { |
1875 | for my $path (keys %MAP_PREFETCH) { |
1978 | for my $path (keys %MAP_PREFETCH) { |
1876 | my $map = find $path |
1979 | if (my $map = find $path) { |
1877 | or next; |
|
|
1878 | $map->load; |
1980 | $map->load; |
|
|
1981 | } |
1879 | |
1982 | |
1880 | delete $MAP_PREFETCH{$path}; |
1983 | delete $MAP_PREFETCH{$path}; |
1881 | } |
1984 | } |
1882 | } |
1985 | } |
1883 | undef $MAP_PREFETCHER; |
1986 | undef $MAP_PREFETCHER; |
… | |
… | |
2220 | |
2323 | |
2221 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
2324 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
2222 | $self->enter_map ($map, $x, $y); |
2325 | $self->enter_map ($map, $x, $y); |
2223 | } |
2326 | } |
2224 | |
2327 | |
2225 | =item $player_object->goto ($path, $x, $y[, $check->($map)]) |
2328 | =item $player_object->goto ($path, $x, $y[, $check->($map)[, $done->()]]) |
2226 | |
2329 | |
2227 | Moves the player to the given map-path and coordinates by first freezing |
2330 | Moves the player to the given map-path and coordinates by first freezing |
2228 | her, loading and preparing them map, calling the provided $check callback |
2331 | her, loading and preparing them map, calling the provided $check callback |
2229 | that has to return the map if sucecssful, and then unfreezes the player on |
2332 | that has to return the map if sucecssful, and then unfreezes the player on |
2230 | the new (success) or old (failed) map position. |
2333 | the new (success) or old (failed) map position. In either case, $done will |
|
|
2334 | be called at the end of this process. |
2231 | |
2335 | |
2232 | =cut |
2336 | =cut |
2233 | |
2337 | |
2234 | our $GOTOGEN; |
2338 | our $GOTOGEN; |
2235 | |
2339 | |
2236 | sub cf::object::player::goto { |
2340 | sub cf::object::player::goto { |
2237 | my ($self, $path, $x, $y, $check) = @_; |
2341 | my ($self, $path, $x, $y, $check, $done) = @_; |
2238 | |
2342 | |
2239 | # do generation counting so two concurrent goto's will be executed in-order |
2343 | # do generation counting so two concurrent goto's will be executed in-order |
2240 | my $gen = $self->{_goto_generation} = ++$GOTOGEN; |
2344 | my $gen = $self->{_goto_generation} = ++$GOTOGEN; |
2241 | |
2345 | |
2242 | $self->enter_link; |
2346 | $self->enter_link; |
… | |
… | |
2262 | |
2366 | |
2263 | if ($gen == $self->{_goto_generation}) { |
2367 | if ($gen == $self->{_goto_generation}) { |
2264 | delete $self->{_goto_generation}; |
2368 | delete $self->{_goto_generation}; |
2265 | $self->leave_link ($map, $x, $y); |
2369 | $self->leave_link ($map, $x, $y); |
2266 | } |
2370 | } |
|
|
2371 | |
|
|
2372 | $done->() if $done; |
2267 | })->prio (1); |
2373 | })->prio (1); |
2268 | } |
2374 | } |
2269 | |
2375 | |
2270 | =item $player_object->enter_exit ($exit_object) |
2376 | =item $player_object->enter_exit ($exit_object) |
2271 | |
2377 | |
… | |
… | |
2340 | my $hp = $exit->stats->hp; |
2446 | my $hp = $exit->stats->hp; |
2341 | my $sp = $exit->stats->sp; |
2447 | my $sp = $exit->stats->sp; |
2342 | |
2448 | |
2343 | $self->enter_link; |
2449 | $self->enter_link; |
2344 | |
2450 | |
|
|
2451 | # if exit is damned, update players death & WoR home-position |
|
|
2452 | $self->contr->savebed ($slaying, $hp, $sp) |
|
|
2453 | if $exit->flag (FLAG_DAMNED); |
|
|
2454 | |
2345 | (async { |
2455 | (async { |
2346 | $self->deactivate_recursive; # just to be sure |
2456 | $self->deactivate_recursive; # just to be sure |
2347 | unless (eval { |
2457 | unless (eval { |
2348 | $self->goto ($slaying, $hp, $sp); |
2458 | $self->goto ($slaying, $hp, $sp); |
2349 | |
2459 | |
… | |
… | |
2376 | |
2486 | |
2377 | utf8::encode $text; |
2487 | utf8::encode $text; |
2378 | $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text); |
2488 | $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text); |
2379 | } |
2489 | } |
2380 | |
2490 | |
2381 | =item $client->send_msg ($color, $type, $msg, [extra...]) |
2491 | =item $client->send_msg ($channel, $msg, $color, [extra...]) |
2382 | |
2492 | |
2383 | Send a drawinfo or msg packet to the client, formatting the msg for the |
2493 | Send a drawinfo or msg packet to the client, formatting the msg for the |
2384 | client if neccessary. C<$type> should be a string identifying the type of |
2494 | client if neccessary. C<$type> should be a string identifying the type of |
2385 | the message, with C<log> being the default. If C<$color> is negative, suppress |
2495 | the message, with C<log> being the default. If C<$color> is negative, suppress |
2386 | the message unless the client supports the msg packet. |
2496 | the message unless the client supports the msg packet. |
2387 | |
2497 | |
2388 | =cut |
2498 | =cut |
2389 | |
2499 | |
2390 | sub cf::client::send_msg { |
2500 | sub cf::client::send_msg { |
2391 | my ($self, $color, $type, $msg, @extra) = @_; |
2501 | my ($self, $channel, $msg, $color, @extra) = @_; |
2392 | |
2502 | |
2393 | $msg = $self->pl->expand_cfpod ($msg); |
2503 | $msg = $self->pl->expand_cfpod ($msg); |
2394 | |
2504 | |
|
|
2505 | $color &= ~cf::NDI_UNIQUE; # just in case... |
|
|
2506 | |
|
|
2507 | if (ref $channel) { |
|
|
2508 | # send meta info to client, if not yet sent |
|
|
2509 | unless (exists $self->{channel}{$channel->{id}}) { |
|
|
2510 | $self->{channel}{$channel->{id}} = $channel; |
|
|
2511 | $self->ext_event (channel_info => %$channel); |
|
|
2512 | } |
|
|
2513 | |
|
|
2514 | $channel = $channel->{id}; |
|
|
2515 | } |
|
|
2516 | |
|
|
2517 | return unless @extra || length $msg; |
|
|
2518 | |
2395 | if ($self->can_msg) { |
2519 | if ($self->can_msg) { |
2396 | $self->send_packet ("msg " . cf::to_json [$color, $type, $msg, @extra]); |
2520 | $self->send_packet ("msg " . $self->{json_coder}->encode ([$color, $channel, $msg, @extra])); |
2397 | } else { |
2521 | } else { |
2398 | # replace some tags by gcfclient-compatible ones |
2522 | # replace some tags by gcfclient-compatible ones |
2399 | for ($msg) { |
2523 | for ($msg) { |
2400 | 1 while |
2524 | 1 while |
2401 | s/<b>([^<]*)<\/b>/[b]${1}[\/b]/ |
2525 | s/<b>([^<]*)<\/b>/[b]${1}[\/b]/ |
… | |
… | |
2423 | =cut |
2547 | =cut |
2424 | |
2548 | |
2425 | sub cf::client::ext_event($$%) { |
2549 | sub cf::client::ext_event($$%) { |
2426 | my ($self, $type, %msg) = @_; |
2550 | my ($self, $type, %msg) = @_; |
2427 | |
2551 | |
|
|
2552 | return unless $self->extcmd; |
|
|
2553 | |
2428 | $msg{msgtype} = "event_$type"; |
2554 | $msg{msgtype} = "event_$type"; |
2429 | $self->send_packet ("ext " . cf::to_json \%msg); |
2555 | $self->send_packet ("ext " . $self->{json_coder}->encode (\%msg)); |
2430 | } |
2556 | } |
2431 | |
2557 | |
2432 | =item $success = $client->query ($flags, "text", \&cb) |
2558 | =item $success = $client->query ($flags, "text", \&cb) |
2433 | |
2559 | |
2434 | Queues a query to the client, calling the given callback with |
2560 | Queues a query to the client, calling the given callback with |
… | |
… | |
2457 | |
2583 | |
2458 | 1 |
2584 | 1 |
2459 | } |
2585 | } |
2460 | |
2586 | |
2461 | cf::client->attach ( |
2587 | cf::client->attach ( |
|
|
2588 | on_connect => sub { |
|
|
2589 | my ($ns) = @_; |
|
|
2590 | |
|
|
2591 | $ns->{json_coder} = JSON::XS->new->utf8->max_size (1e6)->convert_blessed; |
|
|
2592 | }, |
2462 | on_reply => sub { |
2593 | on_reply => sub { |
2463 | my ($ns, $msg) = @_; |
2594 | my ($ns, $msg) = @_; |
2464 | |
2595 | |
2465 | # this weird shuffling is so that direct followup queries |
2596 | # this weird shuffling is so that direct followup queries |
2466 | # get handled first |
2597 | # get handled first |
… | |
… | |
2481 | } |
2612 | } |
2482 | }, |
2613 | }, |
2483 | on_exticmd => sub { |
2614 | on_exticmd => sub { |
2484 | my ($ns, $buf) = @_; |
2615 | my ($ns, $buf) = @_; |
2485 | |
2616 | |
2486 | my $msg = eval { from_json $buf }; |
2617 | my $msg = eval { $ns->{json_coder}->decode ($buf) }; |
2487 | |
2618 | |
2488 | if (ref $msg) { |
2619 | if (ref $msg) { |
2489 | if (my $cb = $EXTICMD{$msg->{msgtype}}) { |
2620 | if (my $cb = $EXTICMD{$msg->{msgtype}}) { |
2490 | if (my %reply = $cb->($ns, $msg)) { |
2621 | if (my %reply = $cb->($ns, $msg)) { |
2491 | $reply{msgid} = $msg->{msgid}; |
2622 | $reply{msgid} = $msg->{msgid}; |
2492 | $ns->send ("ext " . cf::to_json \%reply); |
2623 | $ns->send ("ext " . $ns->{json_coder}->encode (\%reply)); |
2493 | } |
2624 | } |
2494 | } |
2625 | } |
2495 | } else { |
2626 | } else { |
2496 | warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; |
2627 | warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; |
2497 | } |
2628 | } |
… | |
… | |
2554 | |
2685 | |
2555 | =pod |
2686 | =pod |
2556 | |
2687 | |
2557 | The following functions and methods are available within a safe environment: |
2688 | The following functions and methods are available within a safe environment: |
2558 | |
2689 | |
2559 | cf::object contr pay_amount pay_player map |
2690 | cf::object |
|
|
2691 | contr pay_amount pay_player map x y force_find force_add |
|
|
2692 | insert remove |
|
|
2693 | |
2560 | cf::object::player player |
2694 | cf::object::player |
2561 | cf::player peaceful |
2695 | player |
2562 | cf::map trigger |
2696 | |
|
|
2697 | cf::player |
|
|
2698 | peaceful |
|
|
2699 | |
|
|
2700 | cf::map |
|
|
2701 | trigger |
2563 | |
2702 | |
2564 | =cut |
2703 | =cut |
2565 | |
2704 | |
2566 | for ( |
2705 | for ( |
2567 | ["cf::object" => qw(contr pay_amount pay_player map)], |
2706 | ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y |
|
|
2707 | insert remove)], |
2568 | ["cf::object::player" => qw(player)], |
2708 | ["cf::object::player" => qw(player)], |
2569 | ["cf::player" => qw(peaceful)], |
2709 | ["cf::player" => qw(peaceful)], |
2570 | ["cf::map" => qw(trigger)], |
2710 | ["cf::map" => qw(trigger)], |
2571 | ) { |
2711 | ) { |
2572 | no strict 'refs'; |
2712 | no strict 'refs'; |
… | |
… | |
2664 | { |
2804 | { |
2665 | my $faces = $facedata->{faceinfo}; |
2805 | my $faces = $facedata->{faceinfo}; |
2666 | |
2806 | |
2667 | while (my ($face, $info) = each %$faces) { |
2807 | while (my ($face, $info) = each %$faces) { |
2668 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
2808 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
2669 | cf::face::set $idx, $info->{visibility}, $info->{magicmap}; |
2809 | cf::face::set_visibility $idx, $info->{visibility}; |
|
|
2810 | cf::face::set_magicmap $idx, $info->{magicmap}; |
2670 | cf::face::set_data $idx, 0, $info->{data32}, $info->{chksum32}; |
2811 | cf::face::set_data $idx, 0, $info->{data32}, $info->{chksum32}; |
2671 | cf::face::set_data $idx, 1, $info->{data64}, $info->{chksum64}; |
2812 | cf::face::set_data $idx, 1, $info->{data64}, $info->{chksum64}; |
2672 | Coro::cede; |
2813 | |
|
|
2814 | cf::cede_to_tick; |
2673 | } |
2815 | } |
2674 | |
2816 | |
2675 | while (my ($face, $info) = each %$faces) { |
2817 | while (my ($face, $info) = each %$faces) { |
2676 | next unless $info->{smooth}; |
2818 | next unless $info->{smooth}; |
2677 | my $idx = cf::face::find $face |
2819 | my $idx = cf::face::find $face |
2678 | or next; |
2820 | or next; |
2679 | if (my $smooth = cf::face::find $info->{smooth}) { |
2821 | if (my $smooth = cf::face::find $info->{smooth}) { |
|
|
2822 | cf::face::set_smooth $idx, $smooth; |
2680 | cf::face::set_smooth $idx, $smooth, $info->{smoothlevel}; |
2823 | cf::face::set_smoothlevel $idx, $info->{smoothlevel}; |
2681 | } else { |
2824 | } else { |
2682 | warn "smooth face '$info->{smooth}' not found for face '$face'"; |
2825 | warn "smooth face '$info->{smooth}' not found for face '$face'"; |
2683 | } |
2826 | } |
2684 | Coro::cede; |
2827 | |
|
|
2828 | cf::cede_to_tick; |
2685 | } |
2829 | } |
2686 | } |
2830 | } |
2687 | |
2831 | |
2688 | { |
2832 | { |
2689 | my $anims = $facedata->{animinfo}; |
2833 | my $anims = $facedata->{animinfo}; |
2690 | |
2834 | |
2691 | while (my ($anim, $info) = each %$anims) { |
2835 | while (my ($anim, $info) = each %$anims) { |
2692 | cf::anim::set $anim, $info->{frames}, $info->{facings}; |
2836 | cf::anim::set $anim, $info->{frames}, $info->{facings}; |
2693 | Coro::cede; |
2837 | cf::cede_to_tick; |
2694 | } |
2838 | } |
2695 | |
2839 | |
2696 | cf::anim::invalidate_all; # d'oh |
2840 | cf::anim::invalidate_all; # d'oh |
|
|
2841 | } |
|
|
2842 | |
|
|
2843 | { |
|
|
2844 | # TODO: for gcfclient pleasure, we should give resources |
|
|
2845 | # that gcfclient doesn't grok a >10000 face index. |
|
|
2846 | my $res = $facedata->{resource}; |
|
|
2847 | my $enc = JSON::XS->new->utf8->canonical; |
|
|
2848 | |
|
|
2849 | while (my ($name, $info) = each %$res) { |
|
|
2850 | my $meta = $enc->encode ({ |
|
|
2851 | name => $name, |
|
|
2852 | type => $info->{type}, |
|
|
2853 | copyright => $info->{copyright}, #TODO# |
|
|
2854 | }); |
|
|
2855 | |
|
|
2856 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
|
|
2857 | |
|
|
2858 | if ($name =~ /\.jpg$/) { |
|
|
2859 | cf::face::set_data $idx, 0, $info->{data}, $info->{chksum};#d# temp hack |
|
|
2860 | cf::face::set_data $idx, 1, $info->{data}, $info->{chksum};#d# temp hack |
|
|
2861 | } else { |
|
|
2862 | my $data = pack "(w/a*)*", $meta, $info->{data}; |
|
|
2863 | my $chk = Digest::MD5::md5 "$info->{chksum},$meta"; # mangle data checksum and metadata |
|
|
2864 | |
|
|
2865 | cf::face::set_type $idx, 1; |
|
|
2866 | cf::face::set_data $idx, 0, $data, $chk; |
|
|
2867 | } |
|
|
2868 | |
|
|
2869 | cf::cede_to_tick; |
|
|
2870 | } |
2697 | } |
2871 | } |
2698 | |
2872 | |
2699 | 1 |
2873 | 1 |
2700 | } |
2874 | } |
2701 | |
2875 | |
2702 | sub reload_regions { |
2876 | sub reload_regions { |
2703 | load_resource_file "$MAPDIR/regions" |
2877 | load_resource_file "$MAPDIR/regions" |
2704 | or die "unable to load regions file\n"; |
2878 | or die "unable to load regions file\n"; |
|
|
2879 | |
|
|
2880 | for (cf::region::list) { |
|
|
2881 | $_->{match} = qr/$_->{match}/ |
|
|
2882 | if exists $_->{match}; |
|
|
2883 | } |
2705 | } |
2884 | } |
2706 | |
2885 | |
2707 | sub reload_facedata { |
2886 | sub reload_facedata { |
2708 | load_facedata "$DATADIR/facedata" |
2887 | load_facedata "$DATADIR/facedata" |
2709 | or die "unable to load facedata\n"; |
2888 | or die "unable to load facedata\n"; |