… | |
… | |
19 | use File::Temp; |
19 | use File::Temp; |
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; |
|
|
25 | use Storable; $Storable::canonical = 1; |
24 | |
26 | |
25 | sub usage { |
27 | sub usage { |
26 | warn <<EOF; |
28 | warn <<EOF; |
27 | Usage: cfutil [-v] [-q] [--force] [--cache] |
29 | Usage: cfutil [-v] [-q] [--force] [--cache] |
28 | [--install-arch path] |
30 | [--install-arch path] |
… | |
… | |
66 | } |
68 | } |
67 | |
69 | |
68 | sub inst_maps($) { |
70 | sub inst_maps($) { |
69 | my (undef, $path) = @_; |
71 | my (undef, $path) = @_; |
70 | |
72 | |
71 | print "installing '$path' to '$DATADIR/maps'\n\n"; |
73 | print "installing '$path' to '$DATADIR/maps'\n"; |
72 | |
74 | |
73 | if (!-f "$path/regions") { |
75 | if (!-f "$path/regions") { |
74 | 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"; |
75 | exit 1 unless $FORCE; |
77 | exit 1 unless $FORCE; |
76 | } |
78 | } |
77 | |
79 | |
78 | system $RSYNC, "-av", "$path/.", "$DATADIR/maps/.", "--delete", "--exclude", "CVS", "--delete-excluded"; |
80 | system $RSYNC, "-av", "$path/.", "$DATADIR/maps/.", "--delete", "--exclude", "CVS", "--delete-excluded" |
|
|
81 | and die "map installation failed.\n"; |
|
|
82 | |
|
|
83 | print "maps installed successfully.\n"; |
79 | } |
84 | } |
80 | |
85 | |
81 | { |
86 | { |
82 | our @PNG; |
87 | our %FACEINFO; |
83 | our @ARC; |
88 | our @ARC; |
|
|
89 | our $TRS; |
84 | our $NFILE; |
90 | our $NFILE; |
|
|
91 | our %ANIM; |
|
|
92 | |
|
|
93 | our $QUANTIZE = "+dither -colorspace RGB -colors 256"; |
85 | |
94 | |
86 | our (@png, @trs, @arc); # files we are interested in |
95 | our (@png, @trs, @arc); # files we are interested in |
87 | |
96 | |
88 | sub commit_png { |
97 | sub commit_png($$$) { |
89 | my ($name, $data) = @_; |
98 | my ($name, $data, $T) = @_; |
90 | #warn "$name: commited\n"; |
99 | |
|
|
100 | $FACEINFO{$name}{"data$T"} = $data; |
91 | } |
101 | } |
92 | |
102 | |
93 | sub process_png { |
103 | sub process_png { |
94 | while (@png) { |
104 | while (@png) { |
95 | my $path = pop @png; |
105 | my ($path, $delete) = @{pop @png}; |
96 | |
106 | |
97 | my $png; |
107 | my $png; |
98 | aio_lstat $path; |
108 | aio_lstat $path; |
99 | my ($size, $mtime) = (stat _)[7,9]; |
109 | my ($size, $mtime) = (stat _)[7,9]; |
100 | |
110 | |
101 | if (0 > aio_load $path, $png) { |
111 | if (0 > aio_load $path, $png) { |
102 | warn "$path: $!, skipping.\n"; |
112 | warn "$path: $!, skipping.\n"; |
103 | return; |
113 | next; |
104 | } |
114 | } |
105 | |
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 | |
106 | # quickly extratc width and height of the (necessarily PNG) image |
128 | # quickly extract width and height of the (necessarily PNG) image |
107 | unless ($png =~ /^\x89PNG\x0d\x0a\x1a\x0a....IHDR(........)/s) { |
129 | unless ($png =~ /^\x89PNG\x0d\x0a\x1a\x0a....IHDR(........)/s) { |
108 | warn "$path: not a recongized png file, skipping.\n"; |
130 | warn "$path: not a recognized png file, skipping.\n"; |
109 | return; |
131 | next; |
110 | } |
132 | } |
111 | |
133 | |
112 | my ($w, $h) = unpack "NN", $1; |
134 | my ($w, $h) = unpack "NN", $1; |
113 | |
|
|
114 | (my $face = $path) =~ s/^.*\///; |
|
|
115 | my $T = 32; |
|
|
116 | |
|
|
117 | unless ($face =~ s/\.base\.(...)\.png$/.$1/) { |
|
|
118 | warn "$path: weird filename, skipping.\n"; |
|
|
119 | return; |
|
|
120 | } |
|
|
121 | |
135 | |
122 | if ($w < $T || $h < $T) { |
136 | if ($w < $T || $h < $T) { |
123 | warn "$path: too small ($w $h), skipping.\n"; |
137 | warn "$path: too small ($w $h), skipping.\n"; |
124 | return; |
138 | next; |
125 | } |
139 | } |
126 | |
140 | |
127 | if ($w % $T || $h % $T) { |
141 | if ($w % $T || $h % $T) { |
128 | warn "$path: weird png size ($w $h), skipping.\n"; |
142 | warn "$path: weird png size ($w $h), skipping.\n"; |
129 | return; |
143 | next; |
|
|
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 | warn "rescale $other\n";#d# |
|
|
153 | my $wrap = 0; # for the time being |
|
|
154 | fork_sub { |
|
|
155 | system "convert png:\Q$path\E -depth 8 rgba:-" |
|
|
156 | . "| $exec_prefix/bin/cfhq2xa $w $h $wrap" |
|
|
157 | . "| convert -depth 8 -size ".($w * 2)."x".($h * 2)." rgba:- $QUANTIZE -quality 00 png32:\Q$other\E~" |
|
|
158 | and die "convert/hq2xa pipeline error: status $? ($!)"; |
|
|
159 | system $OPTIPNG, "-o5", "-i0", "-q", "$other~"; |
|
|
160 | die "$other~ has zero size, aborting." unless -s "$other~"; |
|
|
161 | rename "$other~", $other; |
|
|
162 | }; |
|
|
163 | } |
|
|
164 | |
|
|
165 | push @png, [$other, !$CACHE]; |
130 | } |
166 | } |
|
|
167 | |
|
|
168 | # possibly scale down |
|
|
169 | if (0 > aio_stat "$stem.32x32.png") { |
|
|
170 | my $other = "$stem.32x32.png~"; |
|
|
171 | |
|
|
172 | if (0 > aio_lstat $other or (-M _) > (-M $path)) { |
|
|
173 | fork_sub { |
|
|
174 | system "convert png:\Q$path\E -geometry 50% -filter lanczos $QUANTIZE -quality 00 png32:\Q$other\E~"; |
|
|
175 | system $OPTIPNG, "-o5", "-i0", "-q", "$other~"; |
|
|
176 | die "$other~ has zero size, aborting." unless -s "$other~"; |
|
|
177 | rename "$other~", $other; |
|
|
178 | }; |
|
|
179 | } |
|
|
180 | |
|
|
181 | #warn "scaled down $path to $other\n";#d# |
|
|
182 | push @png, [$other, !$CACHE]; |
|
|
183 | } |
|
|
184 | } |
|
|
185 | |
|
|
186 | (my $face = $stem) =~ s/^.*\///; |
131 | |
187 | |
132 | if (($w > $T || $h > $T) && $face !~ /_S\./) { |
188 | if (($w > $T || $h > $T) && $face !~ /_S\./) { |
133 | # split |
189 | # split |
134 | my @tile; |
190 | my @tile; |
135 | for my $x (0 .. (int $w / $T) - 1) { |
191 | for my $x (0 .. (int $w / $T) - 1) { |
… | |
… | |
149 | (map { |
205 | (map { |
150 | ( |
206 | ( |
151 | "(", |
207 | "(", |
152 | "+clone", |
208 | "+clone", |
153 | -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), |
|
|
210 | "+repage", |
|
|
211 | -quality => "00", |
154 | -write => "png:$_->[2]~", |
212 | -write => "png32:$_->[2]~", |
155 | "+delete", |
213 | "+delete", |
156 | ")", |
214 | ")", |
157 | ) |
215 | ) |
158 | } @todo), |
216 | } @todo), |
159 | "null:"; |
217 | "null:"; |
… | |
… | |
163 | close $convert; |
221 | close $convert; |
164 | |
222 | |
165 | # pass 2, optimise, and rename |
223 | # pass 2, optimise, and rename |
166 | for (@todo) { |
224 | for (@todo) { |
167 | system $OPTIPNG, "-o5", "-i0", "-q", "$_->[2]~"; |
225 | system $OPTIPNG, "-o5", "-i0", "-q", "$_->[2]~"; |
|
|
226 | die "$_->[2]~ has zero size, aborting." unless -s "$_->[2]~"; |
168 | rename "$_->[2]~", $_->[2]; |
227 | rename "$_->[2]~", $_->[2]; |
169 | } |
228 | } |
170 | }; |
229 | }; |
171 | } |
230 | } |
172 | |
231 | |
… | |
… | |
176 | |
235 | |
177 | if (0 > aio_load $file, $tile) { |
236 | if (0 > aio_load $file, $tile) { |
178 | die "$path: unable to read tile +$x+$y, aborting.\n"; |
237 | die "$path: unable to read tile +$x+$y, aborting.\n"; |
179 | } |
238 | } |
180 | IO::AIO::aio_unlink $file unless $CACHE; |
239 | IO::AIO::aio_unlink $file unless $CACHE; |
181 | commit_png $x|$y ? "$face+$x+$y" : $face, $tile; |
240 | commit_png $x|$y ? "$face+$x+$y" : $face, $tile, $T; |
182 | } |
241 | } |
183 | } else { |
242 | } else { |
184 | # use as-is (either small, use smooth) |
243 | # use as-is (either small, use smooth) |
185 | commit_png $face, $png; |
244 | commit_png $face, $png, $T; |
186 | } |
245 | } |
|
|
246 | |
|
|
247 | aio_unlink $path if $delete; |
187 | } |
248 | } |
188 | } |
249 | } |
189 | |
250 | |
190 | sub process_arc { |
251 | sub process_arc { |
191 | while (@arc) { |
252 | while (@arc) { |
… | |
… | |
195 | aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/ |
256 | aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/ |
196 | |
257 | |
197 | my $arc = read_arch "$dir/$file"; |
258 | my $arc = read_arch "$dir/$file"; |
198 | for my $o (values %$arc) { |
259 | for my $o (values %$arc) { |
199 | push @ARC, $o; |
260 | push @ARC, $o; |
|
|
261 | |
|
|
262 | $o->{editor_folder} = $dir; |
|
|
263 | |
|
|
264 | my $visibility = delete $o->{visibility}; |
|
|
265 | my $magicmap = delete $o->{magicmap}; |
|
|
266 | |
|
|
267 | # find upper left corner :/ |
|
|
268 | # omg, this is sooo broken |
200 | my ($dx, $dy); |
269 | my ($dx, $dy); |
201 | # omg, this is sooo broken |
|
|
202 | for (my $o = $o; $o; $o = $o->{more}) { |
270 | for (my $o = $o; $o; $o = $o->{more}) { |
203 | $dx = $o->{x} if $o->{x} < $dx; |
271 | $dx = $o->{x} if $o->{x} < $dx; |
204 | $dy = $o->{y} if $o->{y} < $dy; |
272 | $dy = $o->{y} if $o->{y} < $dy; |
205 | } |
273 | } |
|
|
274 | |
206 | for (my $o = $o; $o; $o = $o->{more}) { |
275 | for (my $o = $o; $o; $o = $o->{more}) { |
207 | my $x = $o->{x} - $dx; |
276 | my $x = $o->{x} - $dx; |
208 | my $y = $o->{y} - $dy; |
277 | my $y = $o->{y} - $dy; |
|
|
278 | |
|
|
279 | my $ext = $x|$y ? "+$x+$y" : ""; |
|
|
280 | |
|
|
281 | $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/; |
|
|
282 | |
|
|
283 | my $visibility = delete $o->{visibility} if exists $o->{visibility}; |
|
|
284 | my $magicmap = delete $o->{magicmap} if exists $o->{magicmap}; |
|
|
285 | |
|
|
286 | my $anim = delete $o->{anim}; |
|
|
287 | |
209 | if ($x|$y) { |
288 | if ($anim) { |
210 | $_ .= "+$x+$y" for $o->{face}, @{$o->{anim} || []}; |
289 | $o->{animation} = "$o->{_name}"; |
|
|
290 | |
|
|
291 | for (@$anim) { |
|
|
292 | $_ .= $ext unless /^facings\s|^blank.x11$|^empty.x11$/; |
|
|
293 | } |
|
|
294 | |
|
|
295 | $ANIM{"$o->{_name}$ext"} = |
|
|
296 | join "", map "$_\n", |
|
|
297 | "anim $o->{_name}", |
|
|
298 | @$anim, |
|
|
299 | "mina"; |
|
|
300 | } |
|
|
301 | |
|
|
302 | for my $face ($o->{face} || (), @{$anim || []}) { |
|
|
303 | next if $face =~ /^facings\s|^blank.x11$|^empty.x11$/; |
|
|
304 | |
|
|
305 | my $info = $FACEINFO{$face} ||= {}; |
|
|
306 | |
|
|
307 | $info->{visibility} = $visibility if defined $visibility; |
|
|
308 | $info->{magicmap} = $magicmap if defined $magicmap; |
|
|
309 | } |
|
|
310 | |
|
|
311 | if (my $smooth = delete $o->{smoothface}) { |
|
|
312 | my ($face, $smooth) = split /\s+/, $smooth; |
|
|
313 | # skip empty_S.x11, it seems to server no purpose whatsoever |
|
|
314 | # but increases bandwidth demands and worse. |
|
|
315 | unless ($smooth eq "empty_S.x11") { |
|
|
316 | $FACEINFO{$face}{smooth} = $smooth; |
|
|
317 | } |
211 | } |
318 | } |
212 | } |
319 | } |
213 | } |
320 | } |
214 | } |
321 | } |
215 | } |
322 | } |
216 | |
323 | |
217 | sub process_trs { |
324 | sub process_trs { |
218 | while (@trs) { |
325 | while (@trs) { |
219 | my ($dir, $file) = @{pop @trs}; |
326 | my ($dir, $file) = @{pop @trs}; |
|
|
327 | my $path = "$dir/$file"; |
|
|
328 | |
|
|
329 | my $trs; |
|
|
330 | if (0 > aio_load $path, $trs) { |
|
|
331 | warn "$path: $!, skipping.\n"; |
|
|
332 | next; |
|
|
333 | } |
|
|
334 | |
|
|
335 | $TRS .= $trs; |
220 | } |
336 | } |
221 | } |
337 | } |
222 | |
338 | |
223 | sub find_files; |
339 | sub find_files; |
224 | sub find_files { |
340 | sub find_files { |
… | |
… | |
231 | find_files "$path/$_" |
347 | find_files "$path/$_" |
232 | for grep $_ !~ /^(?:CVS|dev)$/, @$dirs; |
348 | for grep $_ !~ /^(?:CVS|dev)$/, @$dirs; |
233 | |
349 | |
234 | for my $file (@$nondirs) { |
350 | for my $file (@$nondirs) { |
235 | if ($file =~ /\.png$/) { |
351 | if ($file =~ /\.png$/) { |
236 | push @png, "$path/$file"; |
352 | push @png, ["$path/$file", 0]; |
237 | } elsif ($file =~ /\.trs$/) { |
353 | } elsif ($file =~ /\.trs$/) { |
238 | push @trs, [$path, $file]; |
354 | push @trs, [$path, $file]; |
239 | } elsif ($file =~ /\.arc$/) { |
355 | } elsif ($file =~ /\.arc$/) { |
240 | push @arc, [$path, $file]; |
356 | push @arc, [$path, $file]; |
241 | } else { |
357 | } else { |
… | |
… | |
246 | } |
362 | } |
247 | |
363 | |
248 | sub inst_arch($) { |
364 | sub inst_arch($) { |
249 | my (undef, $path) = @_; |
365 | my (undef, $path) = @_; |
250 | |
366 | |
251 | print "installing '$path' to '$DATADIR'\n\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"; |
252 | |
370 | |
253 | if (!-d "$path/treasures") { |
371 | if (!-d "$path/treasures") { |
254 | 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"; |
255 | exit 1 unless $FORCE; |
373 | exit 1 unless $FORCE; |
256 | } |
374 | } |
257 | |
375 | |
258 | find_files $path; |
376 | find_files $path; |
259 | IO::AIO::flush; |
377 | IO::AIO::flush; |
260 | |
378 | |
261 | $_->join for ( |
379 | $_->join for ( |
262 | (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), |
263 | (async \&process_trs), (async \&process_trs), |
382 | (async \&process_trs), (async \&process_trs), |
264 | (async \&process_arc), (async \&process_arc), |
383 | (async \&process_arc), (async \&process_arc), |
265 | ); |
384 | ); |
266 | |
385 | |
|
|
386 | { |
|
|
387 | open my $fh, ">:utf8", "$DATADIR/animations~" |
|
|
388 | or die "$DATADIR/animations~: $!"; |
|
|
389 | print $fh join "", map $ANIM{$_}, sort keys %ANIM |
|
|
390 | } |
|
|
391 | |
|
|
392 | { |
267 | open my $archetypes, ">:utf8", "$DATADIR/archetypes~" |
393 | open my $fh, ">:utf8", "$DATADIR/archetypes~" |
268 | or die "$DATADIR/archetypes~: $!"; |
394 | or die "$DATADIR/archetypes~: $!"; |
|
|
395 | substr $_->{editor_folder}, 0, 1 + length $path, "" for @ARC; |
269 | print $archetypes Crossfire::archlist_to_string \@ARC; |
396 | print $fh Crossfire::archlist_to_string \@ARC; |
270 | close $archetypes; |
397 | } |
271 | rename "$DATADIR/archetypes~", "$DATADIR/archetypes"; |
|
|
272 | |
398 | |
273 | die "--install-arch not fully implemented\n"; |
399 | { |
|
|
400 | open my $fh, ">:utf8", "$DATADIR/treasures~" |
|
|
401 | or die "$DATADIR/treasures~: $!"; |
|
|
402 | print $fh $TRS; |
|
|
403 | } |
|
|
404 | |
|
|
405 | { |
|
|
406 | while (my ($k, $v) = each %FACEINFO) { |
|
|
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"; |
|
|
412 | |
|
|
413 | $v->{chksum32} = Digest::MD5::md5 $v->{data32}; |
|
|
414 | $v->{chksum64} = Digest::MD5::md5 $v->{data64}; |
|
|
415 | } |
|
|
416 | |
|
|
417 | open my $fh, ">:perlio", "$DATADIR/facedata~" |
|
|
418 | or die "$DATADIR/facedata~: $!"; |
|
|
419 | |
|
|
420 | $FACEINFO{""} = { version => 1}; |
|
|
421 | print $fh Storable::nfreeze \%FACEINFO; |
|
|
422 | } |
|
|
423 | |
|
|
424 | for (qw(archetypes facedata animations treasures)) { |
|
|
425 | chmod 0644, "$DATADIR/$_~"; |
|
|
426 | rename "$DATADIR/$_~", "$DATADIR/$_" |
|
|
427 | or die "$DATADIR/$_: $!"; |
|
|
428 | } |
|
|
429 | |
|
|
430 | print "archetype data installed successfully.\n"; |
274 | } |
431 | } |
275 | } |
432 | } |
276 | |
433 | |
277 | Getopt::Long::Configure ("bundling", "no_ignore_case"); |
434 | Getopt::Long::Configure ("bundling", "no_ignore_case"); |
278 | GetOptions ( |
435 | GetOptions ( |