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.17 by root, Wed Mar 14 16:23:26 2007 UTC vs.
Revision 1.22 by root, Thu Apr 5 13:50:49 2007 UTC

82 82
83 print "maps installed successfully.\n"; 83 print "maps installed successfully.\n";
84} 84}
85 85
86{ 86{
87 our %PNG32;
88 our %FACEINFO; 87 our %FACEINFO;
89 our @ARC; 88 our @ARC;
90 our $TRS; 89 our $TRS;
91 our $NFILE; 90 our $NFILE;
92 our %ANIM; 91 our %ANIM;
93 92
93 our $QUANTIZE = "+dither -colorspace RGB -colors 256";
94
94 our (@png, @trs, @arc); # files we are interested in 95 our (@png, @trs, @arc); # files we are interested in
95 96
96 sub commit_png { 97 sub commit_png($$$) {
97 my ($name, $data) = @_; 98 my ($name, $data, $T) = @_;
98 99
99 $PNG32{$name} = $data; 100 $FACEINFO{$name}{"data$T"} = $data;
100 $FACEINFO{$name} ||= {};
101 } 101 }
102 102
103 sub process_png { 103 sub process_png {
104 while (@png) { 104 while (@png) {
105 my $path = pop @png; 105 my ($path, $delete) = @{pop @png};
106 106
107 my $png; 107 my $png;
108 aio_lstat $path; 108 aio_lstat $path;
109 my ($size, $mtime) = (stat _)[7,9]; 109 my ($size, $mtime) = (stat _)[7,9];
110 110
111 if (0 > aio_load $path, $png) { 111 if (0 > aio_load $path, $png) {
112 warn "$path: $!, skipping.\n"; 112 warn "$path: $!, skipping.\n";
113 next; 113 next;
114 } 114 }
115 115
116 my $stem = $path;
117 my $T;
118
119 if ($stem =~ s/\.32x32\.png~?$//) {
120 $T = 32;
121 } elsif ($stem =~ s/\.64x64\.png~?$//) {
122 $T = 64;
123 } else {
124 warn "$path: weird filename, skipping.\n";
125 next;
126 }
127
116 # quickly extract width and height of the (necessarily PNG) image 128 # quickly extract width and height of the (necessarily PNG) image
117 unless ($png =~ /^\x89PNG\x0d\x0a\x1a\x0a....IHDR(........)/s) { 129 unless ($png =~ /^\x89PNG\x0d\x0a\x1a\x0a....IHDR(........)/s) {
118 warn "$path: not a recongized png file, skipping.\n"; 130 warn "$path: not a recognized png file, skipping.\n";
119 next; 131 next;
120 } 132 }
121 133
122 my ($w, $h) = unpack "NN", $1; 134 my ($w, $h) = unpack "NN", $1;
123
124 (my $face = $path) =~ s/^.*\///;
125 my $T = 32;
126
127 unless ($face =~ s/\.base\.(...)\.png$/.$1/) {
128 warn "$path: weird filename, skipping.\n";
129 next;
130 }
131 135
132 if ($w < $T || $h < $T) { 136 if ($w < $T || $h < $T) {
133 warn "$path: too small ($w $h), skipping.\n"; 137 warn "$path: too small ($w $h), skipping.\n";
134 next; 138 next;
135 } 139 }
136 140
137 if ($w % $T || $h % $T) { 141 if ($w % $T || $h % $T) {
138 warn "$path: weird png size ($w $h), skipping.\n"; 142 warn "$path: weird png size ($w $h), skipping.\n";
139 next; 143 next;
140 } 144 }
145
146 unless ($path =~ /~$/) {
147 # possibly enlarge
148 if (0 > aio_stat "$stem.64x64.png") {
149 my $other = "$stem.64x64.png~";
150
151 if (0 > aio_lstat $other or (-M _) > (-M $path)) {
152 my $wrap = 0; # for the time being
153 fork_sub {
154 system "convert png:\Q$path\E -depth 8 rgba:-"
155 . "| $exec_prefix/bin/cfhq2xa $w $h $wrap"
156 . "| convert -depth 8 -size ".($w * 2)."x".($h * 2)." rgba:- $QUANTIZE -quality 00 png32:\Q$other\E~"
157 and die "convert/hq2xa pipeline error: status $? ($!)";
158 system $OPTIPNG, "-o5", "-i0", "-q", "$other~";
159 die "$other~ has zero size, aborting." unless -s "$other~";
160 rename "$other~", $other;
161 };
162 }
163
164 push @png, [$other, !$CACHE];
165 }
166
167 # possibly scale down
168 if (0 > aio_stat "$stem.32x32.png") {
169 my $other = "$stem.32x32.png~";
170
171 if (0 > aio_lstat $other or (-M _) > (-M $path)) {
172 fork_sub {
173 system "convert png:\Q$path\E -geometry 50% -filter lanczos $QUANTIZE -quality 00 png32:\Q$other\E~";
174 system $OPTIPNG, "-o5", "-i0", "-q", "$other~";
175 die "$other~ has zero size, aborting." unless -s "$other~";
176 rename "$other~", $other;
177 };
178 }
179
180 #warn "scaled down $path to $other\n";#d#
181 push @png, [$other, !$CACHE];
182 }
183 }
184
185 (my $face = $stem) =~ s/^.*\///;
141 186
142 if (($w > $T || $h > $T) && $face !~ /_S\./) { 187 if (($w > $T || $h > $T) && $face !~ /_S\./) {
143 # split 188 # split
144 my @tile; 189 my @tile;
145 for my $x (0 .. (int $w / $T) - 1) { 190 for my $x (0 .. (int $w / $T) - 1) {
160 ( 205 (
161 "(", 206 "(",
162 "+clone", 207 "+clone",
163 -crop => (sprintf "%dx%d+%d+%d", $T, $T, $_->[0] * $T, $_->[1] * $T), 208 -crop => (sprintf "%dx%d+%d+%d", $T, $T, $_->[0] * $T, $_->[1] * $T),
164 "+repage", 209 "+repage",
210 -quality => "00",
165 -write => "png:$_->[2]~", 211 -write => "png32:$_->[2]~",
166 "+delete", 212 "+delete",
167 ")", 213 ")",
168 ) 214 )
169 } @todo), 215 } @todo),
170 "null:"; 216 "null:";
174 close $convert; 220 close $convert;
175 221
176 # pass 2, optimise, and rename 222 # pass 2, optimise, and rename
177 for (@todo) { 223 for (@todo) {
178 system $OPTIPNG, "-o5", "-i0", "-q", "$_->[2]~"; 224 system $OPTIPNG, "-o5", "-i0", "-q", "$_->[2]~";
225 die "$_->[2]~ has zero size, aborting." unless -s "$_->[2]~";
179 rename "$_->[2]~", $_->[2]; 226 rename "$_->[2]~", $_->[2];
180 } 227 }
181 }; 228 };
182 } 229 }
183 230
187 234
188 if (0 > aio_load $file, $tile) { 235 if (0 > aio_load $file, $tile) {
189 die "$path: unable to read tile +$x+$y, aborting.\n"; 236 die "$path: unable to read tile +$x+$y, aborting.\n";
190 } 237 }
191 IO::AIO::aio_unlink $file unless $CACHE; 238 IO::AIO::aio_unlink $file unless $CACHE;
192 commit_png $x|$y ? "$face+$x+$y" : $face, $tile; 239 commit_png $x|$y ? "$face+$x+$y" : $face, $tile, $T;
193 } 240 }
194 } else { 241 } else {
195 # use as-is (either small, use smooth) 242 # use as-is (either small, use smooth)
196 commit_png $face, $png; 243 commit_png $face, $png, $T;
197 } 244 }
245
246 aio_unlink $path if $delete;
198 } 247 }
199 } 248 }
200 249
201 sub process_arc { 250 sub process_arc {
202 while (@arc) { 251 while (@arc) {
297 find_files "$path/$_" 346 find_files "$path/$_"
298 for grep $_ !~ /^(?:CVS|dev)$/, @$dirs; 347 for grep $_ !~ /^(?:CVS|dev)$/, @$dirs;
299 348
300 for my $file (@$nondirs) { 349 for my $file (@$nondirs) {
301 if ($file =~ /\.png$/) { 350 if ($file =~ /\.png$/) {
302 push @png, "$path/$file"; 351 push @png, ["$path/$file", 0];
303 } elsif ($file =~ /\.trs$/) { 352 } elsif ($file =~ /\.trs$/) {
304 push @trs, [$path, $file]; 353 push @trs, [$path, $file];
305 } elsif ($file =~ /\.arc$/) { 354 } elsif ($file =~ /\.arc$/) {
306 push @arc, [$path, $file]; 355 push @arc, [$path, $file];
307 } else { 356 } else {
312 } 361 }
313 362
314 sub inst_arch($) { 363 sub inst_arch($) {
315 my (undef, $path) = @_; 364 my (undef, $path) = @_;
316 365
317 print "installing '$path' to '$DATADIR'\n"; 366 print "installing '$path' to '$DATADIR'\n",
367 "(this can take a long time if you run this\n",
368 "for the first time or do not use --cache).\n";
318 369
319 if (!-d "$path/treasures") { 370 if (!-d "$path/treasures") {
320 warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; 371 warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n";
321 exit 1 unless $FORCE; 372 exit 1 unless $FORCE;
322 } 373 }
323 374
324 find_files $path; 375 find_files $path;
325 IO::AIO::flush; 376 IO::AIO::flush;
326 377
327 $_->join for ( 378 $_->join for (
328 (async \&process_png), (async \&process_png), 379 # four png crunchers work fine for my 2x smp machine
380 (async \&process_png), (async \&process_png), (async \&process_png), (async \&process_png),
329 (async \&process_trs), (async \&process_trs), 381 (async \&process_trs), (async \&process_trs),
330 (async \&process_arc), (async \&process_arc), 382 (async \&process_arc), (async \&process_arc),
331 ); 383 );
332 384
333 { 385 {
349 print $fh $TRS; 401 print $fh $TRS;
350 } 402 }
351 403
352 { 404 {
353 while (my ($k, $v) = each %FACEINFO) { 405 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"; 406 length $v->{data32} or warn "$k: face has no png32. this will not work (shoddy gcfclient will crash of course).\n";
407 length $v->{data64} or warn "$k: face has no png64. this will not work very well.\n";
408
409 length $v->{data32} <= 10000 or warn "$k: face32 larger than 10000 bytes, will not work with crossfire client.\n";
410 #length $v->{data64} <= 10000 or warn "$k: face64 larger than 10000 bytes.\n";
359 411
360 $v->{chksum32} = Digest::MD5::md5 $v->{data32}; 412 $v->{chksum32} = Digest::MD5::md5 $v->{data32};
413 $v->{chksum64} = Digest::MD5::md5 $v->{data64};
361 } 414 }
362 415
363 open my $fh, ">:perlio", "$DATADIR/faces~" 416 open my $fh, ">:perlio", "$DATADIR/facedata~"
364 or die "$DATADIR/faces~: $!"; 417 or die "$DATADIR/facedata~: $!";
365 418
366 $FACEINFO{""} = { version => 1}; 419 $FACEINFO{""} = { version => 1};
367 print $fh Storable::nfreeze \%FACEINFO; 420 print $fh Storable::nfreeze \%FACEINFO;
368 } 421 }
369 422
370 for (qw(archetypes faces animations treasures)) { 423 for (qw(archetypes facedata animations treasures)) {
371 chmod 0644, "$DATADIR/$_~"; 424 chmod 0644, "$DATADIR/$_~";
372 rename "$DATADIR/$_~", "$DATADIR/$_" 425 rename "$DATADIR/$_~", "$DATADIR/$_"
373 or die "$DATADIR/$_: $!"; 426 or die "$DATADIR/$_: $!";
374 } 427 }
375 428

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines