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.311 by root, Fri Jul 20 16:11:10 2007 UTC vs.
Revision 1.336 by root, Tue Aug 21 00:31:18 2007 UTC

10use Event; 10use Event;
11use Opcode; 11use Opcode;
12use Safe; 12use Safe;
13use Safe::Hole; 13use Safe::Hole;
14 14
15use Coro 3.61 (); 15use Coro 3.64 ();
16use Coro::State; 16use Coro::State;
17use Coro::Handle; 17use Coro::Handle;
18use Coro::Event; 18use Coro::Event;
19use Coro::Timer; 19use Coro::Timer;
20use Coro::Signal; 20use Coro::Signal;
21use Coro::Semaphore; 21use Coro::Semaphore;
22use Coro::AIO; 22use Coro::AIO;
23use Coro::Storable; 23use Coro::Storable;
24use Coro::Util ();
24 25
25use JSON::XS 1.4 (); 26use JSON::XS 1.4 ();
26use BDB (); 27use BDB ();
27use Data::Dumper; 28use Data::Dumper;
28use Digest::MD5; 29use Digest::MD5;
322 }; 323 };
323 $TICK_WATCHER->stop; 324 $TICK_WATCHER->stop;
324 $guard 325 $guard
325} 326}
326 327
328=item cf::get_slot $time[, $priority[, $name]]
329
330Allocate $time seconds of blocking CPU time at priority C<$priority>:
331This call blocks and returns only when you have at least C<$time> seconds
332of cpu time till the next tick. The slot is only valid till the next cede.
333
334The optional C<$name> can be used to identify the job to run. It might be
335used for statistical purposes and should identify the same time-class.
336
337Useful for short background jobs.
338
339=cut
340
341our @SLOT_QUEUE;
342our $SLOT_QUEUE;
343
344$SLOT_QUEUE->cancel if $SLOT_QUEUE;
345$SLOT_QUEUE = Coro::async {
346 my $signal = new Coro::Signal;
347
348 while () {
349 next_job:
350 my $avail = cf::till_tick;
351 if ($avail > 0.01) {
352 for (0 .. $#SLOT_QUEUE) {
353 if ($SLOT_QUEUE[$_][0] < $avail) {
354 my $job = splice @SLOT_QUEUE, $_, 1, ();
355 $job->[2]->send;
356 Coro::cede;
357 goto next_job;
358 }
359 }
360 }
361
362 if (@SLOT_QUEUE) {
363 # we do not use wait_For_tick() as it returns immediately when tick is inactive
364 push @cf::WAIT_FOR_TICK, $signal;
365 $signal->wait;
366 } else {
367 Coro::schedule;
368 }
369 }
370};
371
372sub get_slot($;$$) {
373 my ($time, $pri, $name) = @_;
374
375 $time = $TICK * .6 if $time > $TICK * .6;
376 my $sig = new Coro::Signal;
377
378 push @SLOT_QUEUE, [$time, $pri, $sig, $name];
379 @SLOT_QUEUE = sort { $b->[1] <=> $a->[1] } @SLOT_QUEUE;
380 $SLOT_QUEUE->ready;
381 $sig->wait;
382}
383
327=item cf::async { BLOCK } 384=item cf::async { BLOCK }
328 385
329Currently the same as Coro::async_pool, meaning you cannot use 386Currently the same as Coro::async_pool, meaning you cannot use
330C<on_destroy>, C<join> or other gimmicks on these coroutines. The only 387C<on_destroy>, C<join> or other gimmicks on these coroutines. The only
331thing you are allowed to do is call C<prio> on it. 388thing you are allowed to do is call C<prio> on it.
416Coro::Storable. May, of course, block. Note that the executed sub may 473Coro::Storable. May, of course, block. Note that the executed sub may
417never block itself or use any form of Event handling. 474never block itself or use any form of Event handling.
418 475
419=cut 476=cut
420 477
421sub _store_scalar {
422 open my $fh, ">", \my $buf
423 or die "fork_call: cannot open fh-to-buf in child : $!";
424 Storable::store_fd $_[0], $fh;
425 close $fh;
426
427 $buf
428}
429
430sub fork_call(&@) { 478sub fork_call(&@) {
431 my ($cb, @args) = @_; 479 my ($cb, @args) = @_;
432 480
433# socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC 481 # we seemingly have to make a local copy of the whole thing,
434# or die "socketpair: $!"; 482 # otherwise perl prematurely frees the stuff :/
435 pipe my $fh1, my $fh2 483 # TODO: investigate and fix (liekly this will be rather laborious)
436 or die "pipe: $!";
437 484
438 if (my $pid = fork) { 485 my @res = Coro::Util::fork_eval {
439 close $fh2;
440
441 my $res = (Coro::Handle::unblock $fh1)->readline (undef);
442 warn "pst<$res>" unless $res =~ /^pst/;
443 $res = Coro::Storable::thaw $res;
444
445 waitpid $pid, 0; # should not block anymore, we expect the child to simply behave
446
447 Carp::confess $$res unless "ARRAY" eq ref $res;
448
449 return wantarray ? @$res : $res->[-1];
450 } else {
451 reset_signals; 486 reset_signals;
452 local $SIG{__WARN__}; 487 &$cb
453 local $SIG{__DIE__}; 488 }, @args;
454 # just in case, this hack effectively disables event
455 # in the child. cleaner and slower would be canceling all watchers,
456 # but this works for the time being.
457 local $Coro::idle;
458 $Coro::current->prio (Coro::PRIO_MAX);
459 489
460 eval { 490 wantarray ? @res : $res[-1]
461 close $fh1;
462
463 my @res = eval { $cb->(@args) };
464
465 syswrite $fh2, _store_scalar $@ ? \"$@" : \@res;
466 close $fh2;
467 };
468
469 warn $@ if $@;
470 _exit 0;
471 }
472} 491}
473 492
474=item $value = cf::db_get $family => $key 493=item $value = cf::db_get $family => $key
475 494
476Returns a single value from the environment database. 495Returns a single value from the environment database.
918 937
919=cut 938=cut
920 939
921############################################################################# 940#############################################################################
922# object support 941# object support
942#
943
944sub _can_merge {
945 my ($ob1, $ob2) = @_;
946
947 local $Storable::canonical = 1;
948 my $fob1 = Storable::freeze $ob1;
949 my $fob2 = Storable::freeze $ob2;
950
951 $fob1 eq $fob2
952}
923 953
924sub reattach { 954sub reattach {
925 # basically do the same as instantiate, without calling instantiate 955 # basically do the same as instantiate, without calling instantiate
926 my ($obj) = @_; 956 my ($obj) = @_;
927 957
1114 my ($pl, $buf) = @_; 1144 my ($pl, $buf) = @_;
1115 1145
1116 my $msg = eval { $pl->ns->{json_coder}->decode ($buf) }; 1146 my $msg = eval { $pl->ns->{json_coder}->decode ($buf) };
1117 1147
1118 if (ref $msg) { 1148 if (ref $msg) {
1149 my ($type, $reply, @payload) =
1150 "ARRAY" eq ref $msg
1151 ? @$msg
1152 : ($msg->{msgtype}, $msg->{msgid}, %$msg); # TODO: version 1, remove
1153
1119 if (my $cb = $EXTCMD{$msg->{msgtype}}) { 1154 if (my $cb = $EXTCMD{$type}) {
1120 if (my %reply = $cb->($pl, $msg)) { 1155 my @reply = $cb->($pl, @payload);
1156
1121 $pl->ext_reply ($msg->{msgid}, %reply); 1157 $pl->ext_reply ($reply, @reply)
1122 } 1158 if $reply;
1123 } 1159 }
1124 } else { 1160 } else {
1125 warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; 1161 warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n";
1126 } 1162 }
1127 1163
1297 $self->{deny_save} = 1; 1333 $self->{deny_save} = 1;
1298 1334
1299 $cf::PLAYER{$login} = $self; 1335 $cf::PLAYER{$login} = $self;
1300 1336
1301 $self 1337 $self
1338}
1339
1340=item $player->send_msg ($channel, $msg, $color, [extra...])
1341
1342=cut
1343
1344sub send_msg {
1345 my $ns = shift->ns
1346 or return;
1347 $ns->send_msg (@_);
1302} 1348}
1303 1349
1304=item $pl->quit_character 1350=item $pl->quit_character
1305 1351
1306Nukes the player without looking back. If logged in, the connection will 1352Nukes the player without looking back. If logged in, the connection will
1450sub hintmode { 1496sub hintmode {
1451 $_[0]{hintmode} = $_[1] if @_ > 1; 1497 $_[0]{hintmode} = $_[1] if @_ > 1;
1452 $_[0]{hintmode} 1498 $_[0]{hintmode}
1453} 1499}
1454 1500
1455=item $player->ext_reply ($msgid, %msg) 1501=item $player->ext_reply ($msgid, @msg)
1456 1502
1457Sends an ext reply to the player. 1503Sends an ext reply to the player.
1458 1504
1459=cut 1505=cut
1460 1506
1461sub ext_reply($$%) { 1507sub ext_reply($$@) {
1462 my ($self, $id, %msg) = @_; 1508 my ($self, $id, @msg) = @_;
1463 1509
1464 $msg{msgid} = $id; 1510 $self->ns->ext_reply ($id, @msg)
1465 $self->send ("ext " . $self->ns->{json_coder}->encode (\%msg));
1466} 1511}
1467 1512
1468=item $player->ext_event ($type, %msg) 1513=item $player->ext_msg ($type, @msg)
1469 1514
1470Sends an ext event to the client. 1515Sends an ext event to the client.
1471 1516
1472=cut 1517=cut
1473 1518
1474sub ext_event($$%) { 1519sub ext_msg($$@) {
1475 my ($self, $type, %msg) = @_; 1520 my ($self, $type, @msg) = @_;
1476 1521
1477 $self->ns->ext_event ($type, %msg); 1522 $self->ns->ext_msg ($type, @msg);
1478} 1523}
1479 1524
1480=head3 cf::region 1525=head3 cf::region
1481 1526
1482=over 4 1527=over 4
1843 $self->set_darkness_map; 1888 $self->set_darkness_map;
1844 Coro::cede; 1889 Coro::cede;
1845 $self->activate; 1890 $self->activate;
1846 } 1891 }
1847 1892
1893 $self->{last_save} = $cf::RUNTIME;
1894 $self->last_access ($cf::RUNTIME);
1895
1848 $self->in_memory (cf::MAP_IN_MEMORY); 1896 $self->in_memory (cf::MAP_IN_MEMORY);
1849 } 1897 }
1850 1898
1851 $self->post_load; 1899 $self->post_load;
1852} 1900}
1862 1910
1863 $self 1911 $self
1864} 1912}
1865 1913
1866# find and load all maps in the 3x3 area around a map 1914# find and load all maps in the 3x3 area around a map
1867sub load_diag { 1915sub load_neighbours {
1868 my ($map) = @_; 1916 my ($map) = @_;
1869 1917
1870 my @diag; # diagonal neighbours 1918 my @neigh; # diagonal neighbours
1871 1919
1872 for (0 .. 3) { 1920 for (0 .. 3) {
1873 my $neigh = $map->tile_path ($_) 1921 my $neigh = $map->tile_path ($_)
1874 or next; 1922 or next;
1875 $neigh = find $neigh, $map 1923 $neigh = find $neigh, $map
1876 or next; 1924 or next;
1877 $neigh->load; 1925 $neigh->load;
1878 1926
1927 push @neigh,
1879 push @diag, [$neigh->tile_path (($_ + 3) % 4), $neigh], 1928 [$neigh->tile_path (($_ + 3) % 4), $neigh],
1880 [$neigh->tile_path (($_ + 1) % 4), $neigh]; 1929 [$neigh->tile_path (($_ + 1) % 4), $neigh];
1881 } 1930 }
1882 1931
1883 for (@diag) { 1932 for (grep defined $_->[0], @neigh) {
1933 my ($path, $origin) = @$_;
1884 my $neigh = find @$_ 1934 my $neigh = find $path, $origin
1885 or next; 1935 or next;
1886 $neigh->load; 1936 $neigh->load;
1887 } 1937 }
1888} 1938}
1889 1939
2152 2202
2153 } else { 2203 } else {
2154 my $pl = $self->contr; 2204 my $pl = $self->contr;
2155 2205
2156 if ($pl->{npc_dialog} && $pl->{npc_dialog}->{id}) { 2206 if ($pl->{npc_dialog} && $pl->{npc_dialog}->{id}) {
2157 my $diag = $pl->{npc_dialog}; 2207 my $dialog = $pl->{npc_dialog};
2158 $diag->{pl}->ext_reply ( 2208 $dialog->{pl}->ext_msg ($dialog->{id}, update => msg => $dialog->{pl}->expand_cfpod ($msg));
2159 $diag->{id},
2160 msgtype => "reply",
2161 msg => $diag->{pl}->expand_cfpod ($msg),
2162 add_topics => []
2163 );
2164 2209
2165 } else { 2210 } else {
2166 $msg = $npc->name . " says: $msg" if $npc; 2211 $msg = $npc->name . " says: $msg" if $npc;
2167 $self->message ($msg, $flags); 2212 $self->message ($msg, $flags);
2168 } 2213 }
2169 } 2214 }
2215}
2216
2217=item $object->send_msg ($channel, $msg, $color, [extra...])
2218
2219=cut
2220
2221sub cf::object::send_msg {
2222 my $pl = shift->contr
2223 or return;
2224 $pl->send_msg (@_);
2170} 2225}
2171 2226
2172=item $player_object->may ("access") 2227=item $player_object->may ("access")
2173 2228
2174Returns wether the given player is authorized to access resource "access" 2229Returns wether the given player is authorized to access resource "access"
2253 # use -1 or undef as default coordinates, not 0, 0 2308 # use -1 or undef as default coordinates, not 0, 0
2254 ($x, $y) = ($map->enter_x, $map->enter_y) 2309 ($x, $y) = ($map->enter_x, $map->enter_y)
2255 if $x <=0 && $y <= 0; 2310 if $x <=0 && $y <= 0;
2256 2311
2257 $map->load; 2312 $map->load;
2258 $map->load_diag; 2313 $map->load_neighbours;
2259 2314
2260 return unless $self->contr->active; 2315 return unless $self->contr->active;
2261 $self->activate_recursive; 2316 $self->activate_recursive;
2262 2317
2263 local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext 2318 local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext
2439sub cf::client::send_msg { 2494sub cf::client::send_msg {
2440 my ($self, $channel, $msg, $color, @extra) = @_; 2495 my ($self, $channel, $msg, $color, @extra) = @_;
2441 2496
2442 $msg = $self->pl->expand_cfpod ($msg); 2497 $msg = $self->pl->expand_cfpod ($msg);
2443 2498
2444 return unless @extra || length $msg;
2445
2446 $color &= ~cf::NDI_UNIQUE; # just in case... 2499 $color &= cf::NDI_CLIENT_MASK; # just in case...
2447 2500
2448 if (ref $channel) { 2501 if (ref $channel) {
2449 # send meta info to client, if not yet sent 2502 # send meta info to client, if not yet sent
2450 unless (exists $self->{channel}{$channel->{id}}) { 2503 unless (exists $self->{channel}{$channel->{id}}) {
2451 $self->{channel}{$channel->{id}} = $channel; 2504 $self->{channel}{$channel->{id}} = $channel;
2452 $self->ext_event (channel_info => %$channel); 2505 $self->ext_msg (channel_info => $channel);
2453 } 2506 }
2454 2507
2455 $channel = $channel->{id}; 2508 $channel = $channel->{id};
2456 } 2509 }
2457 2510
2511 return unless @extra || length $msg;
2512
2458 if ($self->can_msg) { 2513 if ($self->can_msg) {
2514 # default colour, mask it out
2515 $color &= ~(cf::NDI_COLOR_MASK | cf::NDI_DEF)
2516 if $color & cf::NDI_DEF;
2517
2459 $self->send_packet ("msg " . $self->{json_coder}->encode ([$color, $channel, $msg, @extra])); 2518 $self->send_packet ("msg " . $self->{json_coder}->encode (
2519 [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra]));
2460 } else { 2520 } else {
2461 # replace some tags by gcfclient-compatible ones
2462 for ($msg) {
2463 1 while
2464 s/<b>([^<]*)<\/b>/[b]${1}[\/b]/
2465 || s/<i>([^<]*)<\/i>/[i]${1}[\/i]/
2466 || s/<u>([^<]*)<\/u>/[ul]${1}[\/ul]/
2467 || s/<tt>([^<]*)<\/tt>/[fixed]${1}[\/fixed]/
2468 || s/<fg name=\"([^"]+)\">([^<]*)<\/fg>/[color=$1]${2}[\/color]/;
2469 }
2470
2471 if ($color >= 0) { 2521 if ($color >= 0) {
2522 # replace some tags by gcfclient-compatible ones
2523 for ($msg) {
2524 1 while
2525 s/<b>([^<]*)<\/b>/[b]${1}[\/b]/
2526 || s/<i>([^<]*)<\/i>/[i]${1}[\/i]/
2527 || s/<u>([^<]*)<\/u>/[ul]${1}[\/ul]/
2528 || s/<tt>([^<]*)<\/tt>/[fixed]${1}[\/fixed]/
2529 || s/<fg name=\"([^"]+)\">([^<]*)<\/fg>/[color=$1]${2}[\/color]/;
2530 }
2531
2532 $color &= cf::NDI_COLOR_MASK;
2533
2534 utf8::encode $msg;
2535
2472 if (0 && $msg =~ /\[/) { 2536 if (0 && $msg =~ /\[/) {
2537 # COMMAND/INFO
2473 $self->send_packet ("drawextinfo $color 4 0 $msg") 2538 $self->send_packet ("drawextinfo $color 10 8 $msg")
2474 } else { 2539 } else {
2475 $msg =~ s/\[\/?(?:b|i|u|fixed|color)[^\]]*\]//g; 2540 $msg =~ s/\[\/?(?:b|i|u|fixed|color)[^\]]*\]//g;
2476 $self->send_packet ("drawinfo $color $msg") 2541 $self->send_packet ("drawinfo $color $msg")
2477 } 2542 }
2478 } 2543 }
2479 } 2544 }
2480} 2545}
2481 2546
2482=item $client->ext_event ($type, %msg) 2547=item $client->ext_msg ($type, @msg)
2483 2548
2484Sends an ext event to the client. 2549Sends an ext event to the client.
2485 2550
2486=cut 2551=cut
2487 2552
2488sub cf::client::ext_event($$%) { 2553sub cf::client::ext_msg($$@) {
2489 my ($self, $type, %msg) = @_; 2554 my ($self, $type, @msg) = @_;
2490 2555
2491 return unless $self->extcmd; 2556 my $extcmd = $self->extcmd;
2492 2557
2558 if ($extcmd == 2) {
2559 $self->send_packet ("ext " . $self->{json_coder}->encode ([$type, @msg]));
2560 } elsif ($extcmd == 1) { # TODO: remove
2493 $msg{msgtype} = "event_$type"; 2561 push @msg, msgtype => "event_$type";
2494 $self->send_packet ("ext " . $self->{json_coder}->encode (\%msg)); 2562 $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg}));
2563 }
2564}
2565
2566=item $client->ext_reply ($msgid, @msg)
2567
2568Sends an ext reply to the client.
2569
2570=cut
2571
2572sub cf::client::ext_reply($$@) {
2573 my ($self, $id, @msg) = @_;
2574
2575 if ($self->extcmd == 2) {
2576 $self->send_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg]));
2577 } elsif ($self->ns->extcmd == 1) {
2578 #TODO: version 1, remove
2579 unshift @msg, msgtype => "reply", msgid => $id;
2580 $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg}));
2581 }
2495} 2582}
2496 2583
2497=item $success = $client->query ($flags, "text", \&cb) 2584=item $success = $client->query ($flags, "text", \&cb)
2498 2585
2499Queues a query to the client, calling the given callback with 2586Queues a query to the client, calling the given callback with
2554 my ($ns, $buf) = @_; 2641 my ($ns, $buf) = @_;
2555 2642
2556 my $msg = eval { $ns->{json_coder}->decode ($buf) }; 2643 my $msg = eval { $ns->{json_coder}->decode ($buf) };
2557 2644
2558 if (ref $msg) { 2645 if (ref $msg) {
2646 my ($type, $reply, @payload) =
2647 "ARRAY" eq ref $msg
2648 ? @$msg
2649 : ($msg->{msgtype}, $msg->{msgid}, %$msg); # TODO: version 1, remove
2650
2559 if (my $cb = $EXTICMD{$msg->{msgtype}}) { 2651 if (my $cb = $EXTICMD{$type}) {
2560 if (my %reply = $cb->($ns, $msg)) { 2652 my @reply = $cb->($ns, @payload);
2561 $reply{msgid} = $msg->{msgid}; 2653
2562 $ns->send ("ext " . $ns->{json_coder}->encode (\%reply)); 2654 $ns->ext_reply ($reply, @reply)
2563 } 2655 if $reply;
2564 } 2656 }
2565 } else { 2657 } else {
2566 warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; 2658 warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n";
2567 } 2659 }
2568 2660
2616our $safe = new Safe "safe"; 2708our $safe = new Safe "safe";
2617our $safe_hole = new Safe::Hole; 2709our $safe_hole = new Safe::Hole;
2618 2710
2619$SIG{FPE} = 'IGNORE'; 2711$SIG{FPE} = 'IGNORE';
2620 2712
2621$safe->permit_only (Opcode::opset qw(:base_core :base_mem :base_orig :base_math sort time)); 2713$safe->permit_only (Opcode::opset qw(
2714 :base_core :base_mem :base_orig :base_math
2715 grepstart grepwhile mapstart mapwhile
2716 sort time
2717));
2622 2718
2623# here we export the classes and methods available to script code 2719# here we export the classes and methods available to script code
2624 2720
2625=pod 2721=pod
2626 2722
2627The following functions and methods are available within a safe environment: 2723The following functions and methods are available within a safe environment:
2628 2724
2629 cf::object 2725 cf::object
2630 contr pay_amount pay_player map x y force_find force_add 2726 contr pay_amount pay_player map x y force_find force_add
2631 insert remove 2727 insert remove name archname title slaying race
2632 2728
2633 cf::object::player 2729 cf::object::player
2634 player 2730 player
2635 2731
2636 cf::player 2732 cf::player
2641 2737
2642=cut 2738=cut
2643 2739
2644for ( 2740for (
2645 ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y 2741 ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y
2646 insert remove)], 2742 insert remove inv name archname title slaying race)],
2647 ["cf::object::player" => qw(player)], 2743 ["cf::object::player" => qw(player)],
2648 ["cf::player" => qw(peaceful)], 2744 ["cf::player" => qw(peaceful)],
2649 ["cf::map" => qw(trigger)], 2745 ["cf::map" => qw(trigger)],
2650) { 2746) {
2651 no strict 'refs'; 2747 no strict 'refs';
2727# the server's init and main functions 2823# the server's init and main functions
2728 2824
2729sub load_facedata($) { 2825sub load_facedata($) {
2730 my ($path) = @_; 2826 my ($path) = @_;
2731 2827
2828 my $enc = JSON::XS->new->utf8->canonical;
2829
2732 warn "loading facedata from $path\n"; 2830 warn "loading facedata from $path\n";
2733 2831
2734 my $facedata; 2832 my $facedata;
2735 0 < aio_load $path, $facedata 2833 0 < aio_load $path, $facedata
2736 or die "$path: $!"; 2834 or die "$path: $!";
2737 2835
2738 $facedata = Coro::Storable::thaw $facedata; 2836 $facedata = Coro::Storable::thaw $facedata;
2739 2837
2740 $facedata->{version} == 2 2838 $facedata->{version} == 2
2741 or cf::cleanup "$path: version mismatch, cannot proceed."; 2839 or cf::cleanup "$path: version mismatch, cannot proceed.";
2840
2841 # patch in the exptable
2842 $facedata->{resource}{"res/exp_table"} = {
2843 type => FT_RSRC,
2844 data => $enc->encode ([map cf::level_to_min_exp $_, 0 .. cf::settings->max_level]),
2845 };
2846 cf::cede_to_tick;
2742 2847
2743 { 2848 {
2744 my $faces = $facedata->{faceinfo}; 2849 my $faces = $facedata->{faceinfo};
2745 2850
2746 while (my ($face, $info) = each %$faces) { 2851 while (my ($face, $info) = each %$faces) {
2747 my $idx = (cf::face::find $face) || cf::face::alloc $face; 2852 my $idx = (cf::face::find $face) || cf::face::alloc $face;
2748 cf::face::set_visibility $idx, $info->{visibility}; 2853 cf::face::set_visibility $idx, $info->{visibility};
2749 cf::face::set_magicmap $idx, $info->{magicmap}; 2854 cf::face::set_magicmap $idx, $info->{magicmap};
2750 cf::face::set_data $idx, 0, $info->{data32}, $info->{chksum32}; 2855 cf::face::set_data $idx, 0, $info->{data32}, Digest::MD5::md5 $info->{data32};
2751 cf::face::set_data $idx, 1, $info->{data64}, $info->{chksum64}; 2856 cf::face::set_data $idx, 1, $info->{data64}, Digest::MD5::md5 $info->{data64};
2752 2857
2753 cf::cede_to_tick; 2858 cf::cede_to_tick;
2754 } 2859 }
2755 2860
2756 while (my ($face, $info) = each %$faces) { 2861 while (my ($face, $info) = each %$faces) {
2781 2886
2782 { 2887 {
2783 # TODO: for gcfclient pleasure, we should give resources 2888 # TODO: for gcfclient pleasure, we should give resources
2784 # that gcfclient doesn't grok a >10000 face index. 2889 # that gcfclient doesn't grok a >10000 face index.
2785 my $res = $facedata->{resource}; 2890 my $res = $facedata->{resource};
2786 my $enc = JSON::XS->new->utf8->canonical; 2891
2892 my $soundconf = delete $res->{"res/sound.conf"};
2787 2893
2788 while (my ($name, $info) = each %$res) { 2894 while (my ($name, $info) = each %$res) {
2789 my $meta = $enc->encode ({
2790 name => $name,
2791 type => $info->{type},
2792 copyright => $info->{copyright}, #TODO#
2793 });
2794
2795 my $idx = (cf::face::find $name) || cf::face::alloc $name; 2895 my $idx = (cf::face::find $name) || cf::face::alloc $name;
2896 my $data;
2796 2897
2797 if ($name =~ /\.jpg$/) { 2898 if ($info->{type} & 1) {
2798 cf::face::set_data $idx, 0, $info->{data}, $info->{chksum};#d# temp hack 2899 # prepend meta info
2799 cf::face::set_data $idx, 1, $info->{data}, $info->{chksum};#d# temp hack 2900
2800 } else { 2901 my $meta = $enc->encode ({
2902 name => $name,
2903 %{ $info->{meta} || {} },
2904 });
2905
2801 my $data = pack "(w/a*)*", $meta, $info->{data}; 2906 $data = pack "(w/a*)*", $meta, $info->{data};
2802 my $chk = Digest::MD5::md5 "$info->{chksum},$meta"; # mangle data checksum and metadata
2803
2804 cf::face::set_type $idx, 1;
2805 cf::face::set_data $idx, 0, $data, $chk;
2806 } 2907 }
2807 2908
2909 cf::face::set_data $idx, 0, $data, Digest::MD5::md5 $data;
2910 cf::face::set_type $idx, $info->{type};
2911
2808 cf::cede_to_tick; 2912 cf::cede_to_tick;
2809 } 2913 }
2914
2915 if ($soundconf) {
2916 $soundconf = $enc->decode (delete $soundconf->{data});
2917
2918 for (0 .. SOUND_CAST_SPELL_0 - 1) {
2919 my $sound = $soundconf->{compat}[$_]
2920 or next;
2921
2922 my $face = cf::face::find "sound/$sound->[1]";
2923 cf::sound::set $sound->[0] => $face;
2924 cf::sound::old_sound_index $_, $face; # gcfclient-compat
2925 }
2926
2927 while (my ($k, $v) = each %{$soundconf->{event}}) {
2928 my $face = cf::face::find "sound/$v";
2929 cf::sound::set $k => $face;
2930 }
2931 }
2810 } 2932 }
2811 2933
2812 1 2934 1
2813} 2935}
2936
2937register_exticmd fx_want => sub {
2938 my ($ns, $want) = @_;
2939
2940 while (my ($k, $v) = each %$want) {
2941 $ns->fx_want ($k, $v);
2942 }
2943};
2814 2944
2815sub reload_regions { 2945sub reload_regions {
2816 load_resource_file "$MAPDIR/regions" 2946 load_resource_file "$MAPDIR/regions"
2817 or die "unable to load regions file\n"; 2947 or die "unable to load regions file\n";
2818 2948

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines