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.16 by root, Wed Mar 14 15:47:21 2007 UTC vs.
Revision 1.34 by root, Mon May 7 06:01:48 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines