… | |
… | |
20 | use Crossfire; |
20 | use Crossfire; |
21 | use Coro; |
21 | use Coro; |
22 | use Coro::AIO; |
22 | use Coro::AIO; |
23 | use POSIX (); |
23 | use POSIX (); |
24 | use Digest::MD5; |
24 | use Digest::MD5; |
|
|
25 | use Storable; $Storable::canonical = 1; |
25 | |
26 | |
26 | sub usage { |
27 | sub usage { |
27 | warn <<EOF; |
28 | warn <<EOF; |
28 | Usage: cfutil [-v] [-q] [--force] [--cache] |
29 | Usage: cfutil [-v] [-q] [--force] [--cache] |
29 | [--install-arch path] |
30 | [--install-arch path] |
… | |
… | |
75 | warn "'$path' does not look like a maps directory ('regions' file is missing).\n"; |
76 | warn "'$path' does not look like a maps directory ('regions' file is missing).\n"; |
76 | exit 1 unless $FORCE; |
77 | exit 1 unless $FORCE; |
77 | } |
78 | } |
78 | |
79 | |
79 | system $RSYNC, "-av", "$path/.", "$DATADIR/maps/.", "--delete", "--exclude", "CVS", "--delete-excluded" |
80 | system $RSYNC, "-av", "$path/.", "$DATADIR/maps/.", "--delete", "--exclude", "CVS", "--delete-excluded" |
80 | or die "map installation failed.\n"; |
81 | and die "map installation failed.\n"; |
81 | |
82 | |
82 | print "maps installed successfully.\n"; |
83 | print "maps installed successfully.\n"; |
83 | } |
84 | } |
84 | |
85 | |
85 | { |
86 | { |
86 | our %PNG32; |
|
|
87 | our %FACEINFO; |
87 | our %FACEINFO; |
88 | our @ARC; |
88 | our @ARC; |
89 | our $TRS; |
89 | our $TRS; |
90 | our $NFILE; |
90 | our $NFILE; |
91 | our %ANIM; |
91 | our %ANIM; |
92 | |
92 | |
|
|
93 | our $QUANTIZE = "+dither -colorspace RGB -colors 256"; |
|
|
94 | |
93 | our (@png, @trs, @arc); # files we are interested in |
95 | our (@png, @trs, @arc); # files we are interested in |
94 | |
96 | |
95 | sub commit_png { |
97 | sub commit_png($$$) { |
96 | my ($name, $data) = @_; |
98 | my ($name, $data, $T) = @_; |
97 | |
99 | |
98 | $PNG32{$name} = $data; |
100 | $FACEINFO{$name}{"data$T"} = $data; |
99 | $FACEINFO{$name} ||= {}; |
|
|
100 | } |
101 | } |
101 | |
102 | |
102 | sub process_png { |
103 | sub process_png { |
103 | while (@png) { |
104 | while (@png) { |
104 | my $path = pop @png; |
105 | my ($path, $delete) = @{pop @png}; |
105 | |
106 | |
106 | my $png; |
107 | my $png; |
107 | aio_lstat $path; |
108 | aio_lstat $path; |
108 | my ($size, $mtime) = (stat _)[7,9]; |
109 | my ($size, $mtime) = (stat _)[7,9]; |
109 | |
110 | |
110 | if (0 > aio_load $path, $png) { |
111 | if (0 > aio_load $path, $png) { |
111 | warn "$path: $!, skipping.\n"; |
112 | warn "$path: $!, skipping.\n"; |
112 | next; |
113 | next; |
113 | } |
114 | } |
114 | |
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 | |
115 | # quickly extract width and height of the (necessarily PNG) image |
128 | # quickly extract width and height of the (necessarily PNG) image |
116 | unless ($png =~ /^\x89PNG\x0d\x0a\x1a\x0a....IHDR(........)/s) { |
129 | unless ($png =~ /^\x89PNG\x0d\x0a\x1a\x0a....IHDR(........)/s) { |
117 | warn "$path: not a recongized png file, skipping.\n"; |
130 | warn "$path: not a recognized png file, skipping.\n"; |
118 | next; |
131 | next; |
119 | } |
132 | } |
120 | |
133 | |
121 | my ($w, $h) = unpack "NN", $1; |
134 | 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 | |
135 | |
131 | if ($w < $T || $h < $T) { |
136 | if ($w < $T || $h < $T) { |
132 | warn "$path: too small ($w $h), skipping.\n"; |
137 | warn "$path: too small ($w $h), skipping.\n"; |
133 | next; |
138 | next; |
134 | } |
139 | } |
135 | |
140 | |
136 | if ($w % $T || $h % $T) { |
141 | if ($w % $T || $h % $T) { |
137 | warn "$path: weird png size ($w $h), skipping.\n"; |
142 | warn "$path: weird png size ($w $h), skipping.\n"; |
138 | next; |
143 | next; |
139 | } |
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/^.*\///; |
140 | |
186 | |
141 | if (($w > $T || $h > $T) && $face !~ /_S\./) { |
187 | if (($w > $T || $h > $T) && $face !~ /_S\./) { |
142 | # split |
188 | # split |
143 | my @tile; |
189 | my @tile; |
144 | for my $x (0 .. (int $w / $T) - 1) { |
190 | for my $x (0 .. (int $w / $T) - 1) { |
… | |
… | |
159 | ( |
205 | ( |
160 | "(", |
206 | "(", |
161 | "+clone", |
207 | "+clone", |
162 | -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), |
163 | "+repage", |
209 | "+repage", |
|
|
210 | -quality => "00", |
164 | -write => "png:$_->[2]~", |
211 | -write => "png32:$_->[2]~", |
165 | "+delete", |
212 | "+delete", |
166 | ")", |
213 | ")", |
167 | ) |
214 | ) |
168 | } @todo), |
215 | } @todo), |
169 | "null:"; |
216 | "null:"; |
… | |
… | |
173 | close $convert; |
220 | close $convert; |
174 | |
221 | |
175 | # pass 2, optimise, and rename |
222 | # pass 2, optimise, and rename |
176 | for (@todo) { |
223 | for (@todo) { |
177 | system $OPTIPNG, "-o5", "-i0", "-q", "$_->[2]~"; |
224 | system $OPTIPNG, "-o5", "-i0", "-q", "$_->[2]~"; |
|
|
225 | die "$_->[2]~ has zero size, aborting." unless -s "$_->[2]~"; |
178 | rename "$_->[2]~", $_->[2]; |
226 | rename "$_->[2]~", $_->[2]; |
179 | } |
227 | } |
180 | }; |
228 | }; |
181 | } |
229 | } |
182 | |
230 | |
… | |
… | |
186 | |
234 | |
187 | if (0 > aio_load $file, $tile) { |
235 | if (0 > aio_load $file, $tile) { |
188 | die "$path: unable to read tile +$x+$y, aborting.\n"; |
236 | die "$path: unable to read tile +$x+$y, aborting.\n"; |
189 | } |
237 | } |
190 | IO::AIO::aio_unlink $file unless $CACHE; |
238 | IO::AIO::aio_unlink $file unless $CACHE; |
191 | commit_png $x|$y ? "$face+$x+$y" : $face, $tile; |
239 | commit_png $x|$y ? "$face+$x+$y" : $face, $tile, $T; |
192 | } |
240 | } |
193 | } else { |
241 | } else { |
194 | # use as-is (either small, use smooth) |
242 | # use as-is (either small, use smooth) |
195 | commit_png $face, $png; |
243 | commit_png $face, $png, $T; |
196 | } |
244 | } |
|
|
245 | |
|
|
246 | aio_unlink $path if $delete; |
197 | } |
247 | } |
198 | } |
248 | } |
199 | |
249 | |
200 | sub process_arc { |
250 | sub process_arc { |
201 | while (@arc) { |
251 | while (@arc) { |
… | |
… | |
296 | find_files "$path/$_" |
346 | find_files "$path/$_" |
297 | for grep $_ !~ /^(?:CVS|dev)$/, @$dirs; |
347 | for grep $_ !~ /^(?:CVS|dev)$/, @$dirs; |
298 | |
348 | |
299 | for my $file (@$nondirs) { |
349 | for my $file (@$nondirs) { |
300 | if ($file =~ /\.png$/) { |
350 | if ($file =~ /\.png$/) { |
301 | push @png, "$path/$file"; |
351 | push @png, ["$path/$file", 0]; |
302 | } elsif ($file =~ /\.trs$/) { |
352 | } elsif ($file =~ /\.trs$/) { |
303 | push @trs, [$path, $file]; |
353 | push @trs, [$path, $file]; |
304 | } elsif ($file =~ /\.arc$/) { |
354 | } elsif ($file =~ /\.arc$/) { |
305 | push @arc, [$path, $file]; |
355 | push @arc, [$path, $file]; |
306 | } else { |
356 | } else { |
… | |
… | |
311 | } |
361 | } |
312 | |
362 | |
313 | sub inst_arch($) { |
363 | sub inst_arch($) { |
314 | my (undef, $path) = @_; |
364 | my (undef, $path) = @_; |
315 | |
365 | |
316 | 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"; |
317 | |
369 | |
318 | if (!-d "$path/treasures") { |
370 | if (!-d "$path/treasures") { |
319 | 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"; |
320 | exit 1 unless $FORCE; |
372 | exit 1 unless $FORCE; |
321 | } |
373 | } |
322 | |
374 | |
323 | find_files $path; |
375 | find_files $path; |
324 | IO::AIO::flush; |
376 | IO::AIO::flush; |
325 | |
377 | |
326 | $_->join for ( |
378 | $_->join for ( |
327 | (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), |
328 | (async \&process_trs), (async \&process_trs), |
381 | (async \&process_trs), (async \&process_trs), |
329 | (async \&process_arc), (async \&process_arc), |
382 | (async \&process_arc), (async \&process_arc), |
330 | ); |
383 | ); |
331 | |
384 | |
332 | { |
385 | { |
… | |
… | |
348 | print $fh $TRS; |
401 | print $fh $TRS; |
349 | } |
402 | } |
350 | |
403 | |
351 | { |
404 | { |
352 | while (my ($k, $v) = each %FACEINFO) { |
405 | 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"; |
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"; |
358 | |
411 | |
359 | $v->{chksum32} = Digest::MD5::md5 $v->{data32}; |
412 | $v->{chksum32} = Digest::MD5::md5 $v->{data32}; |
|
|
413 | $v->{chksum64} = Digest::MD5::md5 $v->{data64}; |
360 | } |
414 | } |
361 | |
415 | |
362 | open my $fh, ">:perlio", "$DATADIR/faces~" |
416 | open my $fh, ">:perlio", "$DATADIR/facedata~" |
363 | or die "$DATADIR/faces~: $!"; |
417 | or die "$DATADIR/facedata~: $!"; |
364 | |
418 | |
|
|
419 | $FACEINFO{""} = { version => 1}; |
365 | print $fh Storable::nfreeze \%FACEINFO; |
420 | print $fh Storable::nfreeze \%FACEINFO; |
366 | } |
421 | } |
367 | |
422 | |
368 | for (qw(archetypes faces animations treasures)) { |
423 | for (qw(archetypes facedata animations treasures)) { |
369 | chmod 0644, "$DATADIR/$_~"; |
424 | chmod 0644, "$DATADIR/$_~"; |
370 | rename "$DATADIR/$_~", "$DATADIR/$_" |
425 | rename "$DATADIR/$_~", "$DATADIR/$_" |
371 | or die "$DATADIR/$_: $!"; |
426 | or die "$DATADIR/$_: $!"; |
372 | } |
427 | } |
373 | |
428 | |