… | |
… | |
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 | our $SMOOTH; |
92 | |
|
|
93 | our $QUANTIZE = "+dither -colorspace RGB -colors 256"; |
93 | |
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 | } |
… | |
… | |
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 | } |
141 | |
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/^.*\///; |
|
|
186 | |
|
|
187 | # split all bigfaces, but avoid smoothfaces (*_S) |
142 | if (($w > $T || $h > $T) && $face !~ /_S\./) { |
188 | if (($w > $T || $h > $T) && $face !~ /_S\./) { |
143 | # split |
189 | # split |
144 | my @tile; |
190 | my @tile; |
145 | for my $x (0 .. (int $w / $T) - 1) { |
191 | for my $x (0 .. (int $w / $T) - 1) { |
146 | for my $y (0 .. (int $h / $T) - 1) { |
192 | for my $y (0 .. (int $h / $T) - 1) { |
… | |
… | |
160 | ( |
206 | ( |
161 | "(", |
207 | "(", |
162 | "+clone", |
208 | "+clone", |
163 | -crop => (sprintf "%dx%d+%d+%d", $T, $T, $_->[0] * $T, $_->[1] * $T), |
209 | -crop => (sprintf "%dx%d+%d+%d", $T, $T, $_->[0] * $T, $_->[1] * $T), |
164 | "+repage", |
210 | "+repage", |
|
|
211 | -quality => "00", |
165 | -write => "png:$_->[2]~", |
212 | -write => "png32:$_->[2]~", |
166 | "+delete", |
213 | "+delete", |
167 | ")", |
214 | ")", |
168 | ) |
215 | ) |
169 | } @todo), |
216 | } @todo), |
170 | "null:"; |
217 | "null:"; |
… | |
… | |
174 | close $convert; |
221 | close $convert; |
175 | |
222 | |
176 | # pass 2, optimise, and rename |
223 | # pass 2, optimise, and rename |
177 | for (@todo) { |
224 | for (@todo) { |
178 | system $OPTIPNG, "-o5", "-i0", "-q", "$_->[2]~"; |
225 | system $OPTIPNG, "-o5", "-i0", "-q", "$_->[2]~"; |
|
|
226 | die "$_->[2]~ has zero size, aborting." unless -s "$_->[2]~"; |
179 | rename "$_->[2]~", $_->[2]; |
227 | rename "$_->[2]~", $_->[2]; |
180 | } |
228 | } |
181 | }; |
229 | }; |
182 | } |
230 | } |
183 | |
231 | |
… | |
… | |
187 | |
235 | |
188 | if (0 > aio_load $file, $tile) { |
236 | if (0 > aio_load $file, $tile) { |
189 | die "$path: unable to read tile +$x+$y, aborting.\n"; |
237 | die "$path: unable to read tile +$x+$y, aborting.\n"; |
190 | } |
238 | } |
191 | IO::AIO::aio_unlink $file unless $CACHE; |
239 | IO::AIO::aio_unlink $file unless $CACHE; |
192 | commit_png $x|$y ? "$face+$x+$y" : $face, $tile; |
240 | commit_png $x|$y ? "$face+$x+$y" : $face, $tile, $T; |
193 | } |
241 | } |
194 | } else { |
242 | } else { |
195 | # use as-is (either small, use smooth) |
243 | # use as-is (either small, use smooth) |
196 | commit_png $face, $png; |
244 | commit_png $face, $png, $T; |
197 | } |
245 | } |
|
|
246 | |
|
|
247 | aio_unlink $path if $delete; |
198 | } |
248 | } |
199 | } |
249 | } |
200 | |
250 | |
201 | sub process_arc { |
251 | sub process_arc { |
202 | while (@arc) { |
252 | while (@arc) { |
… | |
… | |
257 | $info->{visibility} = $visibility if defined $visibility; |
307 | $info->{visibility} = $visibility if defined $visibility; |
258 | $info->{magicmap} = $magicmap if defined $magicmap; |
308 | $info->{magicmap} = $magicmap if defined $magicmap; |
259 | } |
309 | } |
260 | |
310 | |
261 | if (my $smooth = delete $o->{smoothface}) { |
311 | if (my $smooth = delete $o->{smoothface}) { |
262 | $SMOOTH .= "$smooth\n"; |
312 | my %kv =split /\s+/, $smooth; |
|
|
313 | my $level = $o->{smoothlevel}; #TODO: delete from $o if !gcfclient-support |
|
|
314 | while (my ($face, $smooth) = each %kv) { |
|
|
315 | $FACEINFO{$face}{smooth} = $smooth; |
|
|
316 | $FACEINFO{$face}{smoothlevel} = $level; |
|
|
317 | } |
263 | } |
318 | } |
264 | } |
319 | } |
265 | } |
320 | } |
266 | } |
321 | } |
267 | } |
322 | } |
… | |
… | |
292 | find_files "$path/$_" |
347 | find_files "$path/$_" |
293 | for grep $_ !~ /^(?:CVS|dev)$/, @$dirs; |
348 | for grep $_ !~ /^(?:CVS|dev)$/, @$dirs; |
294 | |
349 | |
295 | for my $file (@$nondirs) { |
350 | for my $file (@$nondirs) { |
296 | if ($file =~ /\.png$/) { |
351 | if ($file =~ /\.png$/) { |
297 | push @png, "$path/$file"; |
352 | push @png, ["$path/$file", 0]; |
298 | } elsif ($file =~ /\.trs$/) { |
353 | } elsif ($file =~ /\.trs$/) { |
299 | push @trs, [$path, $file]; |
354 | push @trs, [$path, $file]; |
300 | } elsif ($file =~ /\.arc$/) { |
355 | } elsif ($file =~ /\.arc$/) { |
301 | push @arc, [$path, $file]; |
356 | push @arc, [$path, $file]; |
302 | } else { |
357 | } else { |
… | |
… | |
307 | } |
362 | } |
308 | |
363 | |
309 | sub inst_arch($) { |
364 | sub inst_arch($) { |
310 | my (undef, $path) = @_; |
365 | my (undef, $path) = @_; |
311 | |
366 | |
312 | print "installing '$path' to '$DATADIR'\n"; |
367 | print "installing '$path' to '$DATADIR'\n", |
|
|
368 | "(this can take a long time if you run this\n", |
|
|
369 | "for the first time or do not use --cache).\n"; |
313 | |
370 | |
314 | if (!-d "$path/treasures") { |
371 | if (!-d "$path/treasures") { |
315 | warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; |
372 | warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; |
316 | exit 1 unless $FORCE; |
373 | exit 1 unless $FORCE; |
317 | } |
374 | } |
318 | |
375 | |
319 | find_files $path; |
376 | find_files $path; |
320 | IO::AIO::flush; |
377 | IO::AIO::flush; |
321 | |
378 | |
322 | $_->join for ( |
379 | $_->join for ( |
323 | (async \&process_png), (async \&process_png), |
380 | # four png crunchers work fine for my 2x smp machine |
|
|
381 | (async \&process_png), (async \&process_png), (async \&process_png), (async \&process_png), |
324 | (async \&process_trs), (async \&process_trs), |
382 | (async \&process_trs), (async \&process_trs), |
325 | (async \&process_arc), (async \&process_arc), |
383 | (async \&process_arc), (async \&process_arc), |
326 | ); |
384 | ); |
327 | |
385 | |
328 | { |
386 | { |
… | |
… | |
337 | substr $_->{editor_folder}, 0, 1 + length $path, "" for @ARC; |
395 | substr $_->{editor_folder}, 0, 1 + length $path, "" for @ARC; |
338 | print $fh Crossfire::archlist_to_string \@ARC; |
396 | print $fh Crossfire::archlist_to_string \@ARC; |
339 | } |
397 | } |
340 | |
398 | |
341 | { |
399 | { |
342 | open my $fh, ">:utf8", "$DATADIR/smooth~" |
|
|
343 | or die "$DATADIR/smooth~: $!"; |
|
|
344 | print $fh $SMOOTH; |
|
|
345 | } |
|
|
346 | |
|
|
347 | { |
|
|
348 | open my $fh, ">:utf8", "$DATADIR/treasures~" |
400 | open my $fh, ">:utf8", "$DATADIR/treasures~" |
349 | or die "$DATADIR/treasures~: $!"; |
401 | or die "$DATADIR/treasures~: $!"; |
350 | print $fh $TRS; |
402 | print $fh $TRS; |
351 | } |
403 | } |
352 | |
404 | |
353 | { |
405 | { |
354 | while (my ($k, $v) = each %FACEINFO) { |
406 | while (my ($k, $v) = each %FACEINFO) { |
355 | $v->{data32} ||= delete $PNG32{$k}; |
|
|
356 | } |
|
|
357 | |
|
|
358 | while (my ($k, $v) = each %FACEINFO) { |
|
|
359 | length $v->{data32} or warn "$k: face has no png32. this will not work (shoddy gcfclient will crash of course).\n"; |
407 | length $v->{data32} or warn "$k: face has no png32. this will not work (shoddy gcfclient will crash of course).\n"; |
|
|
408 | length $v->{data64} or warn "$k: face has no png64. this will not work very well.\n"; |
|
|
409 | |
|
|
410 | length $v->{data32} <= 10000 or warn "$k: face32 larger than 10000 bytes, will not work with crossfire client.\n"; |
|
|
411 | #length $v->{data64} <= 10000 or warn "$k: face64 larger than 10000 bytes.\n"; |
360 | |
412 | |
361 | $v->{chksum32} = Digest::MD5::md5 $v->{data32}; |
413 | $v->{chksum32} = Digest::MD5::md5 $v->{data32}; |
|
|
414 | $v->{chksum64} = Digest::MD5::md5 $v->{data64}; |
362 | } |
415 | } |
363 | |
416 | |
364 | open my $fh, ">:perlio", "$DATADIR/faces~" |
417 | open my $fh, ">:perlio", "$DATADIR/facedata~" |
365 | or die "$DATADIR/faces~: $!"; |
418 | or die "$DATADIR/facedata~: $!"; |
366 | |
419 | |
|
|
420 | $FACEINFO{""} = { version => 1}; |
367 | print $fh Storable::nfreeze \%FACEINFO; |
421 | print $fh Storable::nfreeze \%FACEINFO; |
368 | } |
422 | } |
369 | |
423 | |
370 | for (qw(archetypes faces animations treasures smooth)) { |
424 | for (qw(archetypes facedata animations treasures)) { |
371 | chmod 0644, "$DATADIR/$_~"; |
425 | chmod 0644, "$DATADIR/$_~"; |
372 | rename "$DATADIR/$_~", "$DATADIR/$_" |
426 | rename "$DATADIR/$_~", "$DATADIR/$_" |
373 | or die "$DATADIR/$_: $!"; |
427 | or die "$DATADIR/$_: $!"; |
374 | } |
428 | } |
375 | |
429 | |