ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf.pm
(Generate patch)

Comparing deliantra/server/lib/cf.pm (file contents):
Revision 1.283 by root, Sun Jun 24 04:09:29 2007 UTC vs.
Revision 1.287 by root, Mon Jun 25 05:43:45 2007 UTC

49our %COMMAND = (); 49our %COMMAND = ();
50our %COMMAND_TIME = (); 50our %COMMAND_TIME = ();
51 51
52our @EXTS = (); # list of extension package names 52our @EXTS = (); # list of extension package names
53our %EXTCMD = (); 53our %EXTCMD = ();
54our %EXTICMD = ();
54our %EXT_CORO = (); # coroutines bound to extensions 55our %EXT_CORO = (); # coroutines bound to extensions
55our %EXT_MAP = (); # pluggable maps 56our %EXT_MAP = (); # pluggable maps
56 57
57our $RELOAD; # number of reloads so far 58our $RELOAD; # number of reloads so far
58our @EVENT; 59our @EVENT;
236 $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; 237 $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge;
237 $d 238 $d
238 } || "[unable to dump $_[0]: '$@']"; 239 } || "[unable to dump $_[0]: '$@']";
239} 240}
240 241
241use JSON::XS qw(to_json from_json); # TODO# replace by JSON::PC once working 242use JSON::XS ();
242 243
243=item $ref = cf::from_json $json 244=item $ref = cf::from_json $json
244 245
245Converts a JSON string into the corresponding perl data structure. 246Converts a JSON string into the corresponding perl data structure.
246 247
247=item $json = cf::to_json $ref 248=item $json = cf::to_json $ref
248 249
249Converts a perl data structure into its JSON representation. 250Converts a perl data structure into its JSON representation.
251
252=cut
253
254our $json_coder = JSON::XS->new->convert_blessed->utf8;
255
256sub to_json ($) { $json_coder->encode ($_[0]) }
257sub from_json ($) { $json_coder->decode ($_[0]) }
250 258
251=item cf::lock_wait $string 259=item cf::lock_wait $string
252 260
253Wait until the given lock is available. See cf::lock_acquire. 261Wait until the given lock is available. See cf::lock_acquire.
254 262
1029 push @{ $COMMAND{$name} }, [$caller, $cb]; 1037 push @{ $COMMAND{$name} }, [$caller, $cb];
1030} 1038}
1031 1039
1032=item cf::register_extcmd $name => \&callback($pl,$packet); 1040=item cf::register_extcmd $name => \&callback($pl,$packet);
1033 1041
1034Register a callback fro execution when the client sends an extcmd packet. 1042Register a callback for execution when the client sends an (synchronous)
1043extcmd packet. Ext commands will be processed in the order they are
1044received by the server, like other user commands. The first argument is
1045the logged-in player. Ext commands can only be processed after a player
1046has logged in successfully.
1035 1047
1036If the callback returns something, it is sent back as if reply was being 1048If the callback returns something, it is sent back as if reply was being
1037called. 1049called.
1038 1050
1051=item cf::register_exticmd $name => \&callback($ns,$packet);
1052
1053Register a callback for execution when the client sends an (asynchronous)
1054exticmd packet. Exti commands are processed by the server as soon as they
1055are received, i.e. out of order w.r.t. other commands. The first argument
1056is a client socket. Exti commands can be received anytime, even before
1057log-in.
1058
1059If the callback returns something, it is sent back as if reply was being
1060called.
1061
1039=cut 1062=cut
1040 1063
1041sub register_extcmd { 1064sub register_extcmd {
1042 my ($name, $cb) = @_; 1065 my ($name, $cb) = @_;
1043 1066
1044 $EXTCMD{$name} = $cb; 1067 $EXTCMD{$name} = $cb;
1068}
1069
1070sub register_exticmd {
1071 my ($name, $cb) = @_;
1072
1073 $EXTICMD{$name} = $cb;
1045} 1074}
1046 1075
1047cf::player->attach ( 1076cf::player->attach (
1048 on_command => sub { 1077 on_command => sub {
1049 my ($pl, $name, $params) = @_; 1078 my ($pl, $name, $params) = @_;
1390 1419
1391sub ext_reply($$%) { 1420sub ext_reply($$%) {
1392 my ($self, $id, %msg) = @_; 1421 my ($self, $id, %msg) = @_;
1393 1422
1394 $msg{msgid} = $id; 1423 $msg{msgid} = $id;
1395
1396 $self->send ("ext " . cf::to_json \%msg); 1424 $self->send ("ext " . cf::to_json \%msg);
1397} 1425}
1398 1426
1399=item $player->ext_event ($type, %msg) 1427=item $player->ext_event ($type, %msg)
1400 1428
2373 1 while 2401 1 while
2374 s/<b>([^<]*)<\/b>/[b]${1}[\/b]/ 2402 s/<b>([^<]*)<\/b>/[b]${1}[\/b]/
2375 || s/<i>([^<]*)<\/i>/[i]${1}[\/i]/ 2403 || s/<i>([^<]*)<\/i>/[i]${1}[\/i]/
2376 || s/<u>([^<]*)<\/u>/[ul]${1}[\/ul]/ 2404 || s/<u>([^<]*)<\/u>/[ul]${1}[\/ul]/
2377 || s/<tt>([^<]*)<\/tt>/[fixed]${1}[\/fixed]/ 2405 || s/<tt>([^<]*)<\/tt>/[fixed]${1}[\/fixed]/
2378 || s/<fg name=\"([^"]+)\">([^<]*)<\/fg>/[color=$1]${1}[\/color]/; 2406 || s/<fg name=\"([^"]+)\">([^<]*)<\/fg>/[color=$1]${2}[\/color]/;
2379 } 2407 }
2380 2408
2381 if ($color >= 0) { 2409 if ($color >= 0) {
2382 if ($msg =~ /\[/) { 2410 if (0 && $msg =~ /\[/) {
2383 $self->send_packet ("drawextinfo $color 4 0 $msg") 2411 $self->send_packet ("drawextinfo $color 4 0 $msg")
2384 } else { 2412 } else {
2413 $msg =~ s/\[\/?(?:b|i|u|fixed|color)[^\]]*\]//g;
2385 $self->send_packet ("drawinfo $color $msg") 2414 $self->send_packet ("drawinfo $color $msg")
2386 } 2415 }
2387 } 2416 }
2388 } 2417 }
2389} 2418}
2390 2419
2391=item $client->ext_event ($type, %msg) 2420=item $client->ext_event ($type, %msg)
2392 2421
2393Sends an exti event to the client. 2422Sends an ext event to the client.
2394 2423
2395=cut 2424=cut
2396 2425
2397sub cf::client::ext_event($$%) { 2426sub cf::client::ext_event($$%) {
2398 my ($self, $type, %msg) = @_; 2427 my ($self, $type, %msg) = @_;
2405 2434
2406Queues a query to the client, calling the given callback with 2435Queues a query to the client, calling the given callback with
2407the reply text on a reply. flags can be C<cf::CS_QUERY_YESNO>, 2436the reply text on a reply. flags can be C<cf::CS_QUERY_YESNO>,
2408C<cf::CS_QUERY_SINGLECHAR> or C<cf::CS_QUERY_HIDEINPUT> or C<0>. 2437C<cf::CS_QUERY_SINGLECHAR> or C<cf::CS_QUERY_HIDEINPUT> or C<0>.
2409 2438
2410Queries can fail, so check the return code. Or don't, as queries will become 2439Queries can fail, so check the return code. Or don't, as queries will
2411reliable at some point in the future. 2440become reliable at some point in the future.
2412 2441
2413=cut 2442=cut
2414 2443
2415sub cf::client::query { 2444sub cf::client::query {
2416 my ($self, $flags, $text, $cb) = @_; 2445 my ($self, $flags, $text, $cb) = @_;
2424 utf8::encode $text; 2453 utf8::encode $text;
2425 push @{ $self->{query_queue} }, [(sprintf "query %d %s", $flags, $text), $cb]; 2454 push @{ $self->{query_queue} }, [(sprintf "query %d %s", $flags, $text), $cb];
2426 2455
2427 $self->send_packet ($self->{query_queue}[0][0]) 2456 $self->send_packet ($self->{query_queue}[0][0])
2428 if @{ $self->{query_queue} } == 1; 2457 if @{ $self->{query_queue} } == 1;
2458
2459 1
2429} 2460}
2430 2461
2431cf::client->attach ( 2462cf::client->attach (
2432 on_reply => sub { 2463 on_reply => sub {
2433 my ($ns, $msg) = @_; 2464 my ($ns, $msg) = @_;
2447 $ns->send_packet ($ns->{query_queue}[0][0]); 2478 $ns->send_packet ($ns->{query_queue}[0][0]);
2448 } else { 2479 } else {
2449 $ns->state (ST_PLAYING) if $ns->state == ST_CUSTOM; 2480 $ns->state (ST_PLAYING) if $ns->state == ST_CUSTOM;
2450 } 2481 }
2451 } 2482 }
2483 },
2484 on_exticmd => sub {
2485 my ($ns, $buf) = @_;
2486
2487 my $msg = eval { from_json $buf };
2488
2489 if (ref $msg) {
2490 if (my $cb = $EXTICMD{$msg->{msgtype}}) {
2491 if (my %reply = $cb->($ns, $msg)) {
2492 $reply{msgid} = $msg->{msgid};
2493 $ns->send ("ext " . cf::to_json \%reply);
2494 }
2495 }
2496 } else {
2497 warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n";
2498 }
2499
2500 cf::override;
2452 }, 2501 },
2453); 2502);
2454 2503
2455=item $client->async (\&cb) 2504=item $client->async (\&cb)
2456 2505
2863 %EXT_CORO = (); 2912 %EXT_CORO = ();
2864 2913
2865 warn "removing commands"; 2914 warn "removing commands";
2866 %COMMAND = (); 2915 %COMMAND = ();
2867 2916
2868 warn "removing ext commands"; 2917 warn "removing ext/exti commands";
2869 %EXTCMD = (); 2918 %EXTCMD = ();
2919 %EXTICMD = ();
2870 2920
2871 warn "unloading/nuking all extensions"; 2921 warn "unloading/nuking all extensions";
2872 for my $pkg (@EXTS) { 2922 for my $pkg (@EXTS) {
2873 warn "... unloading $pkg"; 2923 warn "... unloading $pkg";
2874 2924

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines