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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines