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