ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/utils/cfutil.in
(Generate patch)

Comparing deliantra/server/utils/cfutil.in (file contents):
Revision 1.4 by root, Wed Mar 7 20:30:18 2007 UTC vs.
Revision 1.40 by root, Mon Jul 16 19:43:33 2007 UTC

6my $exec_prefix = "@exec_prefix@"; 6my $exec_prefix = "@exec_prefix@";
7my $datarootdir = "@datarootdir@"; 7my $datarootdir = "@datarootdir@";
8my $DATADIR = "@datadir@/@PACKAGE@"; 8my $DATADIR = "@datadir@/@PACKAGE@";
9 9
10my $CONVERT = "@CONVERT@"; 10my $CONVERT = "@CONVERT@";
11my $IDENTIFY = "@IDENTIFY@"; 11#my $IDENTIFY = "@IDENTIFY@";
12my $OPTIPNG = "@OPTIPNG@"; 12my $OPTIPNG = "@OPTIPNG@";
13my $RSYNC = "@RSYNC@"; 13my $RSYNC = "@RSYNC@";
14my $PNGNQ = "@PNGNQ@";
14 15
15use Getopt::Long; 16use Getopt::Long;
16use Coro::Event; 17use Coro::Event;
17use AnyEvent; 18use AnyEvent;
18use IO::AIO (); 19use IO::AIO ();
19use File::Temp; 20use File::Temp;
20use Crossfire; 21use Crossfire;
21use Coro; 22use Coro;
22use Coro::AIO; 23use Coro::AIO;
23use POSIX (); 24use POSIX ();
25use Digest::MD5;
26use Coro::Storable; $Storable::canonical = 1;
24 27
25sub usage { 28sub usage {
26 warn <<EOF; 29 warn <<EOF;
27Usage: cfutil [-v] [-q] [--force] [--cache] 30Usage: cfutil [-v] [-q] [--force] [--cache]
28 [--install-arch path] 31 [--install-arch path]
40my $CACHE = 0; 43my $CACHE = 0;
41my $FORCE; 44my $FORCE;
42my $TMPDIR = "/tmp/cfutil$$~"; 45my $TMPDIR = "/tmp/cfutil$$~";
43my $TMPFILE = "aaaa0"; 46my $TMPFILE = "aaaa0";
44 47
48our %COLOR = (
49 black => 0,
50 white => 1,
51 navy => 2,
52 red => 3,
53 orange => 4,
54 blue => 5,
55 darkorange => 6,
56 green => 7,
57 lightgreen => 8,
58 grey => 9,
59 brown => 10,
60 gold => 11,
61 tan => 12,
62);
63
45END { system "rm", "-rf", $TMPDIR } 64END { system "rm", "-rf", $TMPDIR }
46 65
47Event->signal (signal => "INT", cb => sub { exit 1 }); 66Event->signal (signal => "INT", cb => sub { exit 1 });
48Event->signal (signal => "TERM", cb => sub { exit 1 }); 67Event->signal (signal => "TERM", cb => sub { exit 1 });
49 68
66} 85}
67 86
68sub inst_maps($) { 87sub inst_maps($) {
69 my (undef, $path) = @_; 88 my (undef, $path) = @_;
70 89
71 print "installing '$path' to '$DATADIR/maps'\n\n"; 90 print "\nInstalling '$path' to '$DATADIR/maps'\n\n";
72 91
73 if (!-f "$path/regions") { 92 if (!-f "$path/regions") {
74 warn "'$path' does not look like a maps directory ('regions' file is missing).\n"; 93 warn "'$path' does not look like a maps directory ('regions' file is missing).\n";
75 exit 1 unless $FORCE; 94 exit 1 unless $FORCE;
76 } 95 }
77 96
78 system $RSYNC, "-av", "$path/.", "$DATADIR/maps/.", "--delete", "--exclude", "CVS", "--delete-excluded"; 97 system $RSYNC, "-av", "--chmod=u=rwX,go=rX", "$path/.", "$DATADIR/maps/.", "--delete", "--exclude", "CVS", "--delete-excluded"
98 and die "map installation failed.\n";
99
100 print "maps installed successfully.\n";
79} 101}
80 102
81{ 103{
82 our @PNG; 104 our %ANIMINFO;
105 our %FACEINFO;
106 our %RESOURCE;
83 our @ARC; 107 our @ARC;
108 our %ARC;
109 our $TRS;
84 our $NFILE; 110 our $NFILE;
111 our $PATH;
85 112
113 our $QUANTIZE = "+dither -colorspace RGB -colors 256";
114
86 our (@png, @trs, @arc); # files we are interested in 115 our (@png, @trs, @arc, @res); # files we are interested in
87 116
88 sub commit_png { 117 sub commit_png($$$) {
89 my ($name, $data) = @_; 118 my ($name, $data, $T) = @_;
90 #warn "$name: commited\n"; 119
120 $FACEINFO{$name}{"data$T"} = $data;
91 } 121 }
92 122
93 sub process_png { 123 sub process_png {
94 while (@png) { 124 while (@png) {
95 my $path = pop @png; 125 my ($path, $delete) = @{pop @png};
96 126
97 my $png; 127 my $png;
98 aio_lstat $path; 128 aio_lstat $path;
99 my ($size, $mtime) = (stat _)[7,9]; 129 my ($size, $mtime) = (stat _)[7,9];
100 130
101 if (0 > aio_load $path, $png) { 131 if (0 > aio_load $path, $png) {
102 warn "$path: $!, skipping.\n"; 132 warn "$path: $!, skipping.\n";
103 return; 133 next;
104 } 134 }
105 135
136 my $stem = $path;
137 my $T;
138
139 if ($stem =~ s/\.32x32\.png~?$//) {
140 $T = 32;
141 } elsif ($stem =~ s/\.64x64\.png~?$//) {
142 $T = 64;
143 } else {
144 warn "$path: weird filename, skipping.\n";
145 next;
146 }
147
106 # quickly extratc width and height of the (necessarily PNG) image 148 # quickly extract width and height of the (necessarily PNG) image
107 unless ($png =~ /^\x89PNG\x0d\x0a\x1a\x0a....IHDR(........)/s) { 149 unless ($png =~ /^\x89PNG\x0d\x0a\x1a\x0a....IHDR(........)/s) {
108 warn "$path: not a recongized png file, skipping.\n"; 150 warn "$path: not a recognized png file, skipping.\n";
109 return; 151 next;
110 } 152 }
111 153
112 my ($w, $h) = unpack "NN", $1; 154 my ($w, $h) = unpack "NN", $1;
113
114 (my $face = $path) =~ s/^.*\///;
115 my $T = 32;
116
117 unless ($face =~ s/\.base\.(...)\.png$/.$1/) {
118 warn "$path: weird filename, skipping.\n";
119 return;
120 }
121 155
122 if ($w < $T || $h < $T) { 156 if ($w < $T || $h < $T) {
123 warn "$path: too small ($w $h), skipping.\n"; 157 warn "$path: too small ($w $h), skipping.\n";
124 return; 158 next;
125 } 159 }
126 160
127 if ($w % $T || $h % $T) { 161 if ($w % $T || $h % $T) {
128 warn "$path: weird png size ($w $h), skipping.\n"; 162 warn "$path: weird png size ($w $h), skipping.\n";
129 return; 163 next;
164 }
165
166 unless ($path =~ /~$/) {
167 # possibly enlarge
168 if (0 > aio_stat "$stem.64x64.png") {
169 my $other = "$stem.64x64.png~";
170
171 if (0 > aio_lstat $other or (-M _) > (-M $path)) {
172 my $wrap = 0; # for the time being
173 fork_sub {
174 system "convert png:\Q$path\E -depth 8 rgba:-"
175 . "| $exec_prefix/bin/cfhq2xa $w $h $wrap"
176 . "| convert -depth 8 -size ".($w * 2)."x".($h * 2)." rgba:- $QUANTIZE -quality 00 png32:\Q$other\E~"
177 and die "convert/hq2xa pipeline error: status $? ($!)";
178 system $OPTIPNG, "-i0", "-q", "$other~";
179 die "$other~ has zero size, aborting." unless -s "$other~";
180 rename "$other~", $other;
181 };
182 }
183
184 push @png, [$other, !$CACHE];
130 } 185 }
131 186
187 # possibly scale down
188 if (0 > aio_stat "$stem.32x32.png") {
189 my $other = "$stem.32x32.png~";
190
191 if (0 > aio_lstat $other or (-M _) > (-M $path)) {
192 fork_sub {
193 system "convert png:\Q$path\E -geometry 50% -filter lanczos $QUANTIZE -quality 00 png32:\Q$other\E~";
194 system $OPTIPNG, "-i0", "-q", "$other~";
195
196 # reduce smoothfaces >10000 bytes
197 if ($stem =~ /_S\./ && (-s "$other~") > 10000) {
198 my $ncolor = 256;
199 while () {
200 system "<\Q$other~\E $PNGNQ -s1 -n$ncolor >\Q$other~~\E";
201 system $OPTIPNG, "-i0", "-q", "$other~~";
202 last if 10000 > -s "$other~~";
203 $ncolor = int $ncolor * 0.9;
204 $ncolor > 8 or die "cannot reduce filesize to < 10000 bytes";
205 }
206
207 printf "reduced %s from %d to %d bytes using %d colours.\n",
208 $other, -s "$other~", -s "$other~~", $ncolor
209 if $VERBOSE >= 2;
210 rename "$other~~", "$other~";
211 }
212
213 die "$other~ has zero size, aborting." unless -s "$other~";
214 rename "$other~", $other;
215 };
216 }
217
218 #warn "scaled down $path to $other\n";#d#
219 push @png, [$other, !$CACHE];
220 }
221 }
222
223 (my $face = $stem) =~ s/^.*\///;
224
225 # split all bigfaces, but avoid smoothfaces (*_S)
132 if (($w > $T || $h > $T) && $face !~ /_S\./) { 226 if (($w > $T || $h > $T) && $face !~ /_S\./) {
133 # split 227 # split
134 my @tile; 228 my @tile;
135 for my $x (0 .. (int $w / $T) - 1) { 229 for my $x (0 .. (int $w / $T) - 1) {
136 for my $y (0 .. (int $h / $T) - 1) { 230 for my $y (0 .. (int $h / $T) - 1) {
149 (map { 243 (map {
150 ( 244 (
151 "(", 245 "(",
152 "+clone", 246 "+clone",
153 -crop => (sprintf "%dx%d+%d+%d", $T, $T, $_->[0] * $T, $_->[1] * $T), 247 -crop => (sprintf "%dx%d+%d+%d", $T, $T, $_->[0] * $T, $_->[1] * $T),
248 "+repage",
249 -quality => "00",
154 -write => "png:$_->[2]~", 250 -write => "png32:$_->[2]~",
155 "+delete", 251 "+delete",
156 ")", 252 ")",
157 ) 253 )
158 } @todo), 254 } @todo),
159 "null:"; 255 "null:";
163 close $convert; 259 close $convert;
164 260
165 # pass 2, optimise, and rename 261 # pass 2, optimise, and rename
166 for (@todo) { 262 for (@todo) {
167 system $OPTIPNG, "-o5", "-i0", "-q", "$_->[2]~"; 263 system $OPTIPNG, "-o5", "-i0", "-q", "$_->[2]~";
264 die "$_->[2]~ has zero size, aborting." unless -s "$_->[2]~";
168 rename "$_->[2]~", $_->[2]; 265 rename "$_->[2]~", $_->[2];
169 } 266 }
170 }; 267 };
171 } 268 }
172 269
176 273
177 if (0 > aio_load $file, $tile) { 274 if (0 > aio_load $file, $tile) {
178 die "$path: unable to read tile +$x+$y, aborting.\n"; 275 die "$path: unable to read tile +$x+$y, aborting.\n";
179 } 276 }
180 IO::AIO::aio_unlink $file unless $CACHE; 277 IO::AIO::aio_unlink $file unless $CACHE;
181 commit_png $x|$y ? "$face+$x+$y" : $face, $tile; 278 commit_png $x|$y ? "$face+$x+$y" : $face, $tile, $T;
182 } 279 }
183 } else { 280 } else {
184 # use as-is (either small, use smooth) 281 # use as-is (either small, use smooth)
185 commit_png $face, $png; 282 commit_png $face, $png, $T;
186 } 283 }
284
285 aio_unlink $path if $delete;
187 } 286 }
188 } 287 }
189 288
190 sub process_arc { 289 sub process_arc {
191 while (@arc) { 290 while (@arc) {
195 aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/ 294 aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/
196 295
197 my $arc = read_arch "$dir/$file"; 296 my $arc = read_arch "$dir/$file";
198 for my $o (values %$arc) { 297 for my $o (values %$arc) {
199 push @ARC, $o; 298 push @ARC, $o;
299 for (my $m = $o; $m; $m = $m->{more}) {
300 $ARC{$m->{_name}} = $m;
301 }
302
303 $o->{editor_folder} = $dir;
304
305 my $visibility = delete $o->{visibility};
306 my $magicmap = delete $o->{magicmap};
307
308 # find upper left corner :/
309 # omg, this is sooo broken
200 my ($dx, $dy); 310 my ($dx, $dy);
201 # omg, this is sooo broken
202 for (my $o = $o; $o; $o = $o->{more}) { 311 for (my $o = $o; $o; $o = $o->{more}) {
203 $dx = $o->{x} if $o->{x} < $dx; 312 $dx = $o->{x} if $o->{x} < $dx;
204 $dy = $o->{y} if $o->{y} < $dy; 313 $dy = $o->{y} if $o->{y} < $dy;
205 } 314 }
315
206 for (my $o = $o; $o; $o = $o->{more}) { 316 for (my $o = $o; $o; $o = $o->{more}) {
207 my $x = $o->{x} - $dx; 317 my $x = $o->{x} - $dx;
208 my $y = $o->{y} - $dy; 318 my $y = $o->{y} - $dy;
319
320 my $ext = $x|$y ? "+$x+$y" : "";
321
322 $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/ || !$o->{face};
323
324 $visibility = delete $o->{visibility} if exists $o->{visibility};
325 $magicmap = delete $o->{magicmap} if exists $o->{magicmap};
326
327 my $anim = delete $o->{anim};
328
209 if ($x|$y) { 329 if ($anim) {
210 $_ .= "+$x+$y" for $o->{face}, @{$o->{anim} || []}; 330 # possibly add $ext to the animation name to avoid
331 # the need to specify archnames for all parts
332 # of a multipart archetype.
333 $o->{animation} = "$o->{_name}";
334 my $facings = 1;
335 my @frames;
336
337 for (@$anim) {
338 if (/^facings\s+(\d+)/) {
339 $facings = $1*1;
340 } elsif (/^blank.x11$|^empty.x11$/) {
341 push @frames, $_;
342 } else {
343 push @frames, "$_$ext";
344 }
345 }
346
347 $ANIMINFO{$o->{animation}} = {
348 facings => $facings,
349 frames => \@frames,
350 };
351 }
352
353 for my $face ($o->{face} || (), @{$anim || []}) {
354 next if $face =~ /^facings\s|^blank.x11$|^empty.x11$/;
355
356 my $info = $FACEINFO{$face} ||= {};
357
358 $info->{visibility} = $visibility if defined $visibility;
359 $info->{magicmap} = $magicmap if defined $magicmap;
360 }
361
362 if (my $smooth = delete $o->{smoothface}) {
363 my %kv =split /\s+/, $smooth;
364 my $level = $o->{smoothlevel}; #TODO: delete from $o if !gcfclient-support
365 while (my ($face, $smooth) = each %kv) {
366 $FACEINFO{$face}{smooth} = $smooth;
367 $FACEINFO{$face}{smoothlevel} = $level;
368 }
211 } 369 }
212 } 370 }
213 } 371 }
214 } 372 }
215 } 373 }
216 374
217 sub process_trs { 375 sub process_trs {
218 while (@trs) { 376 while (@trs) {
219 my ($dir, $file) = @{pop @trs}; 377 my ($dir, $file) = @{pop @trs};
378 my $path = "$dir/$file";
379
380 my $trs;
381 if (0 > aio_load $path, $trs) {
382 warn "$path: $!, skipping.\n";
383 next;
384 }
385
386 $TRS .= $trs;
387 }
388 }
389
390 sub process_res {
391 while (@res) {
392 my ($dir, $file) = @{pop @res};
393
394 my $data;
395 aio_load "$dir/$file", $data;
396
397 my $copyright;
398 aio_load "$dir/copyright", $copyright;
399
400 $file =~ s/\.res$//;
401 $file =~ /\.([^.]+)$/
402 or next;
403
404 my $type = $1;
405
406 substr $dir, 0, 1 + length $PATH, "";
407
408 $RESOURCE{"$dir/$file"} = {
409 type => $1,
410 copyright => $copyright,
411 data => $data,
412 chksum => Digest::MD5::md5 $data,
413 };
220 } 414 }
221 } 415 }
222 416
223 sub find_files; 417 sub find_files;
224 sub find_files { 418 sub find_files {
231 find_files "$path/$_" 425 find_files "$path/$_"
232 for grep $_ !~ /^(?:CVS|dev)$/, @$dirs; 426 for grep $_ !~ /^(?:CVS|dev)$/, @$dirs;
233 427
234 for my $file (@$nondirs) { 428 for my $file (@$nondirs) {
235 if ($file =~ /\.png$/) { 429 if ($file =~ /\.png$/) {
236 push @png, "$path/$file"; 430 push @png, ["$path/$file", 0];
237 } elsif ($file =~ /\.trs$/) { 431 } elsif ($file =~ /\.trs$/) {
238 push @trs, [$path, $file]; 432 push @trs, [$path, $file];
239 } elsif ($file =~ /\.arc$/) { 433 } elsif ($file =~ /\.arc$/) {
240 push @arc, [$path, $file]; 434 push @arc, [$path, $file];
435 } elsif ($file =~ /\.(ogg|jpg|res)$/) {
436 push @res, [$path, $file];
241 } else { 437 } else {
242 warn "ignoring $path/$file\n" if $VERBOSE >= 2; 438 warn "ignoring $path/$file\n" if $VERBOSE >= 3;
243 } 439 }
244 } 440 }
245 }; 441 };
246 } 442 }
247 443
248 sub inst_arch($) { 444 sub inst_arch($) {
249 my (undef, $path) = @_; 445 my (undef, $path) = @_;
250 446
447 $PATH = $path;
448
449 print "\n",
251 print "installing '$path' to '$DATADIR'\n\n"; 450 "Installing '$path' to '$DATADIR'\n",
451 "\n",
452 "This can take a long time if you run this\n",
453 "for the first time or do not use --cache.\n",
454 "\n",
455 "Unless you run verbosely, all following warning\n",
456 "or error messages indicate serious problems.\n",
457 "\n";
252 458
253 if (!-d "$path/treasures") { 459 if (!-d "$path/treasures") {
254 warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; 460 warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n";
255 exit 1 unless $FORCE; 461 exit 1 unless $FORCE;
256 } 462 }
257 463
258 find_files $path; 464 find_files $path;
259 IO::AIO::flush; 465 IO::AIO::flush;
260 466
261 $_->join for ( 467 $_->join for (
262 (async \&process_png), (async \&process_png), 468 # four png crunchers work fine for my 2x smp machine
469 (async \&process_png), (async \&process_png), (async \&process_png), (async \&process_png),
263 (async \&process_trs), (async \&process_trs), 470 (async \&process_trs), (async \&process_trs),
264 (async \&process_arc), (async \&process_arc), 471 (async \&process_arc), (async \&process_arc),
472 (async \&process_res), (async \&process_res),
265 ); 473 );
266 474
475 {
476 # remove path prefix from editor_folder
477 substr $_->{editor_folder}, 0, 1 + length $path, ""
478 for values %ARC;
479
480 # resolve inherit
481 while () {
482 my $progress;
483 my $loop;
484
485 for my $o (values %ARC) {
486 if (my $other = $o->{inherit}) {
487 if (my $s = $ARC{$other}) {
488 if ($s->{inherit}) {
489 $loop = $s;
490 } else {
491 delete $o->{inherit};
492 my %s = %$s;
493 delete @s{qw(_name more name name_pl)};
494 %$o = ( %s, %$o );
495 ++$progress;
496 }
497 } else {
498 warn "'$o->{_name}' tries to inherit from undefined archetype '$other', skipping.\n";
499 delete $ARC{$o->{_name}};
500 }
501 }
502 }
503
504 unless ($progress) {
505 die "inheritance loop detected starting at archetype '$loop->{_name}', aborting.\n"
506 if $loop;
507
508 last;
509 }
510 }
511
512 # remove base classes (by naming scheme, should use something like "baseclass xxx" to inherit
513 @ARC = grep $_->{_name} !~ /^(?:type|class)_/, @ARC;
514
267 open my $archetypes, ">:utf8", "$DATADIR/archetypes~" 515 open my $fh, ">:utf8", "$DATADIR/archetypes~"
268 or die "$DATADIR/archetypes~: $!"; 516 or die "$DATADIR/archetypes~: $!";
269 print $archetypes Crossfire::archlist_to_string \@ARC; 517 print $fh Crossfire::archlist_to_string [sort { $a->{_name} cmp $b->{_name} } @ARC];
270 close $archetypes; 518 }
271 rename "$DATADIR/archetypes~", "$DATADIR/archetypes";
272 519
273 die "--install-arch not fully implemented\n"; 520 {
521 open my $fh, ">:utf8", "$DATADIR/treasures~"
522 or die "$DATADIR/treasures~: $!";
523 print $fh $TRS;
524 }
525
526 {
527 while (my ($k, $v) = each %FACEINFO) {
528 length $v->{data32} or warn "$k: face has no png32. this will not work (shoddy gcfclient will crash of course).\n";
529 length $v->{data64} or warn "$k: face has no png64. this will not work very well.\n";
530
531 length $v->{data32} <= 10000 or warn "$k: face32 larger than 10000 bytes, will not work with crossfire client.\n";
532 #length $v->{data64} <= 10000 or warn "$k: face64 larger than 10000 bytes.\n";
533
534 $v->{chksum32} = Digest::MD5::md5 $v->{data32};
535 $v->{chksum64} = Digest::MD5::md5 $v->{data64};
536
537 if (my $magicmap = $v->{magicmap}) {
538 $magicmap =~ y/A-Z_\-/a-z/d;
539 $v->{magicmap} = $COLOR{$magicmap};
540 }
541 }
542
543 open my $fh, ">:perlio", "$DATADIR/facedata~"
544 or die "$DATADIR/facedata~: $!";
545
546 print $fh freeze {
547 version => 2,
548 faceinfo => \%FACEINFO,
549 animinfo => \%ANIMINFO,
550 resource => \%RESOURCE,
551 };
552 }
553
554 for (qw(archetypes facedata treasures)) {
555 chmod 0644, "$DATADIR/$_~";
556 rename "$DATADIR/$_~", "$DATADIR/$_"
557 or die "$DATADIR/$_: $!";
558 }
559
560 print "archetype data installed successfully.\n";
274 } 561 }
275} 562}
276 563
277Getopt::Long::Configure ("bundling", "no_ignore_case"); 564Getopt::Long::Configure ("bundling", "no_ignore_case");
278GetOptions ( 565GetOptions (

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines