ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/utils/cfutil.in
Revision: 1.53
Committed: Sun Aug 19 09:27:08 2007 UTC (16 years, 9 months ago) by root
Branch: MAIN
Changes since 1.52: +56 -26 lines
Log Message:
toyed around with cfutil, remembered archetype

File Contents

# Content
1 #!@PERL@
2
3 use strict;
4
5 my $prefix = "@prefix@";
6 my $exec_prefix = "@exec_prefix@";
7 my $datarootdir = "@datarootdir@";
8 my $DATADIR = "@datadir@/@PACKAGE@";
9
10 my $CONVERT = "@CONVERT@";
11 #my $IDENTIFY = "@IDENTIFY@";
12 my $OPTIPNG = "@OPTIPNG@";
13 my $RSYNC = "@RSYNC@";
14 my $PNGNQ = "@PNGNQ@";
15
16 use Getopt::Long;
17 use Coro::Event;
18 use AnyEvent;
19 use IO::AIO ();
20 use File::Temp;
21 use Crossfire;
22 use Coro;
23 use Coro::AIO;
24 use Coro::Util;
25 use POSIX ();
26 use Digest::MD5;
27 use Carp;
28 use Coro::Channel;
29 use Coro::Storable; $Storable::canonical = 1;
30
31 $SIG{QUIT} = sub { Carp::cluck "QUIT" };
32
33 sub usage {
34 warn <<EOF;
35 Usage: cfutil [-v] [-q] [--force] [--cache]
36 [--install-arch path]
37 [--install-maps maps]
38 [--print-statedir]
39 [--print-confdir]
40 [--print-datadir]
41 [--print-libdir]
42 [--print-bindir]
43 EOF
44 exit 1;
45 }
46
47 my $VERBOSE = 1;
48 my $CACHE = 0;
49 my $FORCE;
50 my $TMPDIR = "/tmp/cfutil$$~";
51 my $TMPFILE = "aaaa0";
52
53 our %COLOR = (
54 black => 0,
55 white => 1,
56 navy => 2,
57 red => 3,
58 orange => 4,
59 blue => 5,
60 darkorange => 6,
61 green => 7,
62 lightgreen => 8,
63 grey => 9,
64 brown => 10,
65 gold => 11,
66 tan => 12,
67 );
68
69 END { system "rm", "-rf", $TMPDIR }
70
71 Event->signal (signal => "INT", cb => sub { exit 1 });
72 Event->signal (signal => "TERM", cb => sub { exit 1 });
73
74 mkdir $TMPDIR, 0700
75 or die "$TMPDIR: $!";
76
77 sub fork_sub(&) {
78 my ($cb) = @_;
79
80 if (my $pid = fork) {
81 my $current = $Coro::current;
82 my $w = AnyEvent->child (pid => $pid, cb => sub { $current->ready });
83 Coro::schedule;
84 } else {
85 eval { $cb->() };
86 POSIX::_exit 0 unless $@;
87 warn $@;
88 POSIX::_exit 1;
89 }
90 }
91
92 sub inst_maps($) {
93 my (undef, $path) = @_;
94
95 print "\nInstalling '$path' to '$DATADIR/maps'\n\n";
96
97 if (!-f "$path/regions") {
98 warn "'$path' does not look like a maps directory ('regions' file is missing).\n";
99 exit 1 unless $FORCE;
100 }
101
102 system $RSYNC, "-a", "--chmod=u=rwX,go=rX", "$path/.", "$DATADIR/maps/.", "--delete", "--exclude", "CVS", "--delete-excluded"
103 and die "map installation failed.\n";
104
105 print "maps installed successfully.\n";
106 }
107
108 {
109 our %ANIMINFO;
110 our %FACEINFO;
111 our %RESOURCE;
112 our @ARC;
113 our %ARC;
114 our $TRS;
115 our $NFILE;
116 our $PATH;
117
118 our $QUANTIZE = "+dither -colorspace RGB -colors 256";
119
120 our $c_arc = new Coro::Channel;
121 our $c_trs = new Coro::Channel;
122 our $c_res = new Coro::Channel;
123
124 our @c_png;
125
126 sub commit_png($$$) {
127 my ($name, $data, $T) = @_;
128
129 $FACEINFO{$name}{"data$T"} = $data;
130 }
131
132 sub process_png {
133 while (@c_png) {
134 my ($path, $delete) = @{pop @c_png};
135
136 my $png;
137 aio_lstat $path;
138 my ($size, $mtime) = (stat _)[7,9];
139
140 if (0 > aio_load $path, $png) {
141 warn "$path: $!, skipping.\n";
142 next;
143 }
144
145 my $stem = $path;
146 my $T;
147
148 if ($stem =~ s/\.32x32\.png~?$//) {
149 $T = 32;
150 } elsif ($stem =~ s/\.64x64\.png~?$//) {
151 $T = 64;
152 } else {
153 warn "$path: weird filename, skipping.\n";
154 next;
155 }
156
157 # quickly extract width and height of the (necessarily PNG) image
158 unless ($png =~ /^\x89PNG\x0d\x0a\x1a\x0a....IHDR(........)/s) {
159 warn "$path: not a recognized png file, skipping.\n";
160 next;
161 }
162
163 my ($w, $h) = unpack "NN", $1;
164
165 if ($w < $T || $h < $T) {
166 warn "$path: too small ($w $h), skipping.\n";
167 next;
168 }
169
170 if ($w % $T || $h % $T) {
171 warn "$path: weird png size ($w $h), skipping.\n";
172 next;
173 }
174
175 unless ($path =~ /~$/) {
176 # possibly enlarge
177 if (0 > aio_stat "$stem.64x64.png") {
178 my $other = "$stem.64x64.png~";
179
180 if (0 > aio_lstat $other or (-M _) > (-M $path)) {
181 fork_sub {
182 my $CROP;
183 my $SRC = "png:\Q$path\E";
184
185 # check if this is a wall. ultra-ugly. ultra-ultra-ugly.
186 if ($path =~ /^(.*\/wall\/.*_)([0-9A-F])(\.x11.*\.png)$/) {
187 my ($pfx, $dir, $sfx) = ($1, hex $2, $3);
188 #check for 0..F images to be sure(?) this is a wall
189 unless (grep { !-e sprintf "%s%X%s", $pfx, $_, $sfx } 0..15) {
190 # add a 4px border and add other images around it
191 $CROP = "-shave 8x8 +repage";
192
193 $w += 8;
194 $h += 8;
195
196 $SRC = "-size ${w}x${h} xc:transparent";
197 $SRC .= " png:\Q$path\E -geometry +4+4 -composite";
198
199 # 8 surrounding images
200 for (
201 # x y b r0 r1
202 [-1, -1, 0, 6],
203 [ 0, -1, 1, 10, 14],
204 [+1, -1, 0, 12],
205
206 [-1, 0, 8, 5, 7],
207 #
208 [+1, 0, 2, 5, 13],
209
210 [-1, +1, 0, 3],
211 [ 0, +1, 4, 10, 11],
212 [+1, +1, 0, 9],
213 ) {
214 my ($x, $y, $d, $r0, $r1) = @$_;
215 $SRC .= sprintf " png:%s%X%s -geometry %+d%+d -composite",
216 "\Q$pfx",
217 ($dir & $d) ? $r1 : $r0,
218 "\Q$sfx",
219 $x * ($w - 8) + 4,
220 $y * ($h - 8) + 4;
221 }
222 }
223 }
224
225 system "convert -depth 8 $SRC rgba:-"
226 . "| $exec_prefix/bin/cfhq2xa $w $h 0"
227 . "| convert -depth 8 -size ".($w * 2)."x".($h * 2)." rgba:- $CROP $QUANTIZE -quality 00 png32:\Q$other\E~"
228 and die "convert/cfhq2xa pipeline error: status $? ($!)";
229 system $OPTIPNG, "-i0", "-q", "$other~";
230 die "$other~ has zero size, aborting." unless -s "$other~";
231 rename "$other~", $other;
232 };
233 }
234
235 push @c_png, [$other, !$CACHE];
236 }
237
238 # possibly scale down
239 if (0 > aio_stat "$stem.32x32.png") {
240 my $other = "$stem.32x32.png~";
241
242 if (0 > aio_lstat $other or (-M _) > (-M $path)) {
243 fork_sub {
244 system "convert png:\Q$path\E -geometry 50% -filter lanczos $QUANTIZE -quality 00 png32:\Q$other\E~";
245 system $OPTIPNG, "-i0", "-q", "$other~";
246
247 # reduce smoothfaces >10000 bytes
248 if ($stem =~ /_S\./ && (-s "$other~") > 10000) {
249 my $ncolor = 256;
250 while () {
251 system "<\Q$other~\E $PNGNQ -s1 -n$ncolor >\Q$other~~\E";
252 system $OPTIPNG, "-i0", "-q", "$other~~";
253 last if 10000 > -s "$other~~";
254 $ncolor = int $ncolor * 0.9;
255 $ncolor > 8 or die "cannot reduce filesize to < 10000 bytes";
256 }
257
258 printf "reduced %s from %d to %d bytes using %d colours.\n",
259 $other, -s "$other~", -s "$other~~", $ncolor
260 if $VERBOSE >= 2;
261 rename "$other~~", "$other~";
262 }
263
264 die "$other~ has zero size, aborting." unless -s "$other~";
265 rename "$other~", $other;
266 };
267 }
268
269 #warn "scaled down $path to $other\n";#d#
270 push @c_png, [$other, !$CACHE];
271 }
272 }
273
274 (my $face = $stem) =~ s/^.*\///;
275
276 # split all bigfaces, but avoid smoothfaces (*_S)
277 if (($w > $T || $h > $T) && $face !~ /_S\./) {
278 # split
279 my @tile;
280 for my $x (0 .. (int $w / $T) - 1) {
281 for my $y (0 .. (int $h / $T) - 1) {
282 my $file = "$path+$x+$y~";
283 aio_lstat $file;
284 push @tile, [$x, $y, $file, (stat _)[9]];
285 }
286 }
287
288 my $mtime = (lstat $path)[9];
289 my @todo = grep { $_->[3] <= $mtime } @tile;
290 if (@todo) {
291 fork_sub {
292 open my $convert, "|-", $CONVERT,
293 "png:-",
294 (map {
295 (
296 "(",
297 "+clone",
298 -crop => (sprintf "%dx%d+%d+%d", $T, $T, $_->[0] * $T, $_->[1] * $T),
299 "+repage",
300 -quality => "00",
301 -write => "png32:$_->[2]~",
302 "+delete",
303 ")",
304 )
305 } @todo),
306 "null:";
307
308 binmode $convert;
309 print $convert $png;
310 close $convert;
311
312 # pass 2, optimise, and rename
313 for (@todo) {
314 system $OPTIPNG, "-o5", "-i0", "-q", "$_->[2]~";
315 die "$_->[2]~ has zero size, aborting." unless -s "$_->[2]~";
316 rename "$_->[2]~", $_->[2];
317 }
318 };
319 }
320
321 for (@tile) {
322 my ($x, $y, $file) = @$_;
323 my $tile;
324
325 if (0 > aio_load $file, $tile) {
326 die "$path: unable to read tile +$x+$y, aborting.\n";
327 }
328 IO::AIO::aio_unlink $file unless $CACHE;
329 commit_png $x|$y ? "$face+$x+$y" : $face, $tile, $T;
330 }
331 } else {
332 # use as-is (either small, use smooth)
333 commit_png $face, $png, $T;
334 }
335
336 aio_unlink $path if $delete;
337 }
338 }
339
340 sub process_arc {
341 while (my $job = $c_arc->get) {
342 my ($dir, $file) = @$job;
343
344 my $arc;
345 aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/
346
347 my $arc = read_arch "$dir/$file";
348 for my $o (values %$arc) {
349 push @ARC, $o;
350 for (my $m = $o; $m; $m = $m->{more}) {
351 $ARC{$m->{_name}} = $m;
352 }
353
354 $o->{editor_folder} = $dir;
355
356 my $visibility = delete $o->{visibility};
357 my $magicmap = delete $o->{magicmap};
358
359 # find upper left corner :/
360 # omg, this is sooo broken
361 my ($dx, $dy);
362 for (my $o = $o; $o; $o = $o->{more}) {
363 $dx = $o->{x} if $o->{x} < $dx;
364 $dy = $o->{y} if $o->{y} < $dy;
365 }
366
367 for (my $o = $o; $o; $o = $o->{more}) {
368 my $x = $o->{x} - $dx;
369 my $y = $o->{y} - $dy;
370
371 my $ext = $x|$y ? "+$x+$y" : "";
372
373 $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/ || !$o->{face};
374
375 $visibility = delete $o->{visibility} if exists $o->{visibility};
376 $magicmap = delete $o->{magicmap} if exists $o->{magicmap};
377
378 my $anim = delete $o->{anim};
379
380 if ($anim) {
381 # possibly add $ext to the animation name to avoid
382 # the need to specify archnames for all parts
383 # of a multipart archetype.
384 $o->{animation} = "$o->{_name}";
385 my $facings = 1;
386 my @frames;
387
388 for (@$anim) {
389 if (/^facings\s+(\d+)/) {
390 $facings = $1*1;
391 } elsif (/^blank.x11$|^empty.x11$/) {
392 push @frames, $_;
393 } else {
394 push @frames, "$_$ext";
395 }
396 }
397
398 $ANIMINFO{$o->{animation}} = {
399 facings => $facings,
400 frames => \@frames,
401 };
402 }
403
404 for my $face ($o->{face} || (), @{$anim || []}) {
405 next if $face =~ /^facings\s|^blank.x11$|^empty.x11$/;
406
407 my $info = $FACEINFO{$face} ||= {};
408
409 $info->{arc} = $o;
410 $info->{visibility} = $visibility if defined $visibility;
411 $info->{magicmap} = $magicmap if defined $magicmap;
412 }
413
414 if (my $smooth = delete $o->{smoothface}) {
415 my %kv =split /\s+/, $smooth;
416 my $level = $o->{smoothlevel}; #TODO: delete from $o if !gcfclient-support
417 while (my ($face, $smooth) = each %kv) {
418 $FACEINFO{$face}{smooth} = $smooth;
419 $FACEINFO{$face}{smoothlevel} = $level;
420 }
421 }
422 }
423 }
424 }
425 }
426
427 sub process_trs {
428 while (my $job = $c_trs->get) {
429 my ($dir, $file) = @$job;
430 my $path = "$dir/$file";
431
432 my $trs;
433 if (0 > aio_load $path, $trs) {
434 warn "$path: $!, skipping.\n";
435 next;
436 }
437
438 $TRS .= $trs;
439 }
440 }
441
442 my %FILECACHE;
443
444 sub load_cached($;$) {
445 unless (exists $FILECACHE{$_[0]}) {
446 my $data;
447 if (0 < aio_load $_[0], $data) {
448 if ($_[1]) {
449 $data = eval { $_[1]->($data) };
450 warn "$_[0]: $@" if $@;
451 }
452 }
453
454 $FILECACHE{$_[0]} = $data;
455 }
456
457 $FILECACHE{$_[0]}
458 }
459
460 sub process_res {
461 while (my $job = $c_res->get) {
462 my ($dir, $file, $type) = @$job;
463
464 my $data;
465 aio_load "$dir/$file", $data;
466
467 my $meta = load_cached "$dir/meta", sub { JSON::XS::from_json shift };
468
469 next if $meta && !exists $meta->{$file};
470
471 $meta = {
472 %{ $meta->{"" } || {} },
473 %{ $meta->{$file} || {} },
474 };
475
476 if ($meta->{license} =~ s/^#//) {
477 $meta->{license} = ({
478 "pd" => "Public Domain",
479 "gpl" => "GNU General Public License, version 3.0 or any later",
480 "cc/by/2.0" => "Licensed under Creative Commons Attribution 2.0 http://creativecommons.org/licenses/by/2.0/",
481 "cc/by/2.5" => "Licensed under Creative Commons Attribution 2.5 http://creativecommons.org/licenses/by/2.5/",
482 "cc/by/3.0" => "Licensed under Creative Commons Attribution 3.0 http://creativecommons.org/licenses/by/3.0/",
483 })->{$meta->{license}}
484 || warn "$dir/$file: license tag '$meta->{license}' not found.";
485 }
486
487 $file =~ s/\.res$//;
488 $file =~ s/\.(ogg|wav|jpg|png)$//;
489
490 substr $dir, 0, 1 + length $PATH, "";
491
492 $RESOURCE{"$dir/$file"} = {
493 type => (delete $meta->{type}) || $type,
494 data => $data,
495 chksum => (Digest::MD5::md5 $data),
496 %$meta ? (meta => $meta) : (),
497 };
498 }
499 }
500
501 sub find_files;
502 sub find_files {
503 my ($path) = @_;
504
505 IO::AIO::aioreq_pri 4;
506 IO::AIO::aio_scandir $path, 4, sub {
507 my ($dirs, $nondirs) = @_;
508
509 find_files "$path/$_"
510 for grep $_ !~ /^(?:CVS|dev)$/, @$dirs;
511
512 my $dir = $path;
513 substr $dir, 0, 1 + length $PATH, "";
514
515 for my $file (@$nondirs) {
516 if ($dir =~ /^music(?:\/|$)/) {
517 $c_res->put ([$path, $file, 3]) # FT_MUSIC
518 if $file =~ /\.(ogg)$/;
519
520 } elsif ($dir =~ /^sound(?:\/|$)/) {
521 $c_res->put ([$path, $file, 5]) # FT_SOUND
522 if $file =~ /\.(wav|ogg)$/;
523
524 } elsif ($dir =~ /^res(?:\/|$)/) {
525 $c_res->put ([$path, $file, 0]) # FT_FACE
526 if $file =~ /\.(jpg|png)$/;
527 $c_res->put ([$path, $file, 7]) # FT_RSRC
528 if $file =~ /\.(res)$/;
529
530 } elsif ($file =~ /\.png$/) {
531 push @c_png, ["$path/$file", 0];
532
533 } elsif ($file =~ /\.trs$/) {
534 $c_trs->put ([$path, $file]);
535
536 } elsif ($file =~ /\.arc$/) {
537 $c_arc->put ([$path, $file]);
538
539 } else {
540 warn "ignoring $path/$file\n" if $VERBOSE >= 3;
541 }
542 }
543 };
544 }
545
546 sub inst_arch($) {
547 my (undef, $path) = @_;
548
549 $PATH = $path;
550
551 print "\n",
552 "Installing '$path' to '$DATADIR'\n",
553 "\n",
554 "This can take a long time if you run this\n",
555 "for the first time or do not use --cache.\n",
556 "\n",
557 "Unless you run verbosely, all following warning\n",
558 "or error messages indicate serious problems.\n",
559 "\n";
560
561 if (!-d "$path/treasures") {
562 warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n";
563 exit 1 unless $FORCE;
564 }
565
566 print "scanning files...\n" if $VERBOSE;
567
568 find_files $path;
569
570 my @a_arc = map +(async \&process_arc), 1..2;
571 my @a_res = map +(async \&process_res), 1..2;
572 my @a_trs = map +(async \&process_trs), 1..2;
573
574 IO::AIO::flush;
575
576 $c_res->put (undef) for @a_res;
577 $c_arc->put (undef) for @a_arc;
578 $c_trs->put (undef) for @a_trs;
579
580 print "start file scan, arc, res processing...\n" if $VERBOSE;
581
582 $_->join for @a_arc; # need to parse all archetypes before png processing
583
584 print "end arc, start png processing...\n" if $VERBOSE;
585
586 # four png crunchers work fine for my 2x smp machine
587 my @a_png = map +(async \&process_png), 1..4;
588
589 $_->join for (@a_trs, @a_res, @a_png);
590
591 print "scanning done, processing results...\n" if $VERBOSE;
592 {
593 # remove path prefix from editor_folder
594 substr $_->{editor_folder}, 0, 1 + length $path, ""
595 for values %ARC;
596
597 print "resolving inheritance tree...\n" if $VERBOSE;
598 # resolve inherit
599 while () {
600 my $progress;
601 my $loop;
602
603 for my $o (values %ARC) {
604 if (my $other = $o->{inherit}) {
605 if (my $s = $ARC{$other}) {
606 if ($s->{inherit}) {
607 $loop = $s;
608 } else {
609 delete $o->{inherit};
610 my %s = %$s;
611 delete @s{qw(_name more name name_pl)};
612 %$o = ( %s, %$o );
613 ++$progress;
614 }
615 } else {
616 warn "'$o->{_name}' tries to inherit from undefined archetype '$other', skipping.\n";
617 delete $ARC{$o->{_name}};
618 }
619 }
620 }
621
622 unless ($progress) {
623 die "inheritance loop detected starting at archetype '$loop->{_name}', aborting.\n"
624 if $loop;
625
626 last;
627 }
628 }
629
630 # remove base classes (by naming scheme, should use something like "baseclass xxx" to inherit
631 @ARC = grep $_->{_name} !~ /^(?:type|class)_/, @ARC;
632
633 print "writing archetypes...\n" if $VERBOSE;
634 open my $fh, ">:utf8", "$DATADIR/archetypes~"
635 or die "$DATADIR/archetypes~: $!";
636 print $fh Crossfire::archlist_to_string [sort { $a->{_name} cmp $b->{_name} } @ARC];
637 }
638
639 {
640 print "writing treasures...\n" if $VERBOSE;
641 open my $fh, ">:utf8", "$DATADIR/treasures~"
642 or die "$DATADIR/treasures~: $!";
643 print $fh $TRS;
644 }
645
646 {
647 print "processing facedata...\n" if $VERBOSE;
648 while (my ($k, $v) = each %FACEINFO) {
649 length $v->{data32} or warn "$k: face has no png32. this will not work (shoddy gcfclient will crash of course).\n";
650 length $v->{data64} or warn "$k: face has no png64. this will not work very well.\n";
651
652 length $v->{data32} <= 10000 or warn "$k: face32 larger than 10000 bytes, will not work with crossfire client.\n";
653 #length $v->{data64} <= 10000 or warn "$k: face64 larger than 10000 bytes.\n";
654
655 $v->{chksum32} = Digest::MD5::md5 $v->{data32};
656 $v->{chksum64} = Digest::MD5::md5 $v->{data64};
657
658 if (my $magicmap = $v->{magicmap}) {
659 $magicmap =~ y/A-Z_\-/a-z/d;
660 $v->{magicmap} = $COLOR{$magicmap};
661 }
662
663 delete $v->{arc};
664 }
665
666 print "writing facedata...\n" if $VERBOSE;
667 open my $fh, ">:perlio", "$DATADIR/facedata~"
668 or die "$DATADIR/facedata~: $!";
669
670 print $fh freeze {
671 version => 2,
672 faceinfo => \%FACEINFO,
673 animinfo => \%ANIMINFO,
674 resource => \%RESOURCE,
675 };
676 }
677
678 print "committing files...\n" if $VERBOSE;
679
680 for (qw(archetypes facedata treasures)) {
681 chmod 0644, "$DATADIR/$_~";
682 rename "$DATADIR/$_~", "$DATADIR/$_"
683 or die "$DATADIR/$_: $!";
684 }
685
686 print "archetype data installed successfully.\n";
687 }
688 }
689
690 Getopt::Long::Configure ("bundling", "no_ignore_case");
691 GetOptions (
692 "verbose|v:+" => \$VERBOSE,
693 "cache" => \$CACHE,
694 "quiet|q" => sub { $VERBOSE = 0 },
695 "force" => sub { $FORCE = 1 },
696 "install-arch=s" => \&inst_arch,
697 "install-maps=s" => \&inst_maps,
698 "print-statedir" => sub { print "@pkgstatedir@\n" },
699 "print-datadir" => sub { print "$DATADIR\n" },
700 "print-confdir" => sub { print "@pkgconfdir@\n" },
701 "print-libdir" => sub { print "@libdir@/@PACKAGE@\n" },
702 "print-bindir" => sub { print "@bindir@/@PACKAGE@\n" },
703 ) or usage;
704