… | |
… | |
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; |
24 | |
25 | |
25 | sub usage { |
26 | sub usage { |
26 | warn <<EOF; |
27 | warn <<EOF; |
27 | Usage: cfutil [-v] [-q] [--force] [--cache] |
28 | Usage: cfutil [-v] [-q] [--force] [--cache] |
28 | [--install-arch path] |
29 | [--install-arch path] |
… | |
… | |
77 | |
78 | |
78 | system $RSYNC, "-av", "$path/.", "$DATADIR/maps/.", "--delete", "--exclude", "CVS", "--delete-excluded"; |
79 | system $RSYNC, "-av", "$path/.", "$DATADIR/maps/.", "--delete", "--exclude", "CVS", "--delete-excluded"; |
79 | } |
80 | } |
80 | |
81 | |
81 | { |
82 | { |
82 | our @PNG; |
83 | our %PNG32; |
|
|
84 | our %FACEINFO; |
83 | our @ARC; |
85 | our @ARC; |
|
|
86 | our $TRS; |
84 | our $NFILE; |
87 | our $NFILE; |
|
|
88 | our %ANIM; |
|
|
89 | our $SMOOTH; |
85 | |
90 | |
86 | our (@png, @trs, @arc); # files we are interested in |
91 | our (@png, @trs, @arc); # files we are interested in |
87 | |
92 | |
88 | sub commit_png { |
93 | sub commit_png { |
89 | my ($name, $data) = @_; |
94 | my ($name, $data) = @_; |
90 | #warn "$name: commited\n"; |
95 | |
|
|
96 | $PNG32{$name} = $data; |
|
|
97 | $FACEINFO{$name} ||= {}; |
91 | } |
98 | } |
92 | |
99 | |
93 | sub process_png { |
100 | sub process_png { |
94 | while (@png) { |
101 | while (@png) { |
95 | my $path = pop @png; |
102 | my $path = pop @png; |
… | |
… | |
98 | aio_lstat $path; |
105 | aio_lstat $path; |
99 | my ($size, $mtime) = (stat _)[7,9]; |
106 | my ($size, $mtime) = (stat _)[7,9]; |
100 | |
107 | |
101 | if (0 > aio_load $path, $png) { |
108 | if (0 > aio_load $path, $png) { |
102 | warn "$path: $!, skipping.\n"; |
109 | warn "$path: $!, skipping.\n"; |
103 | return; |
110 | next; |
104 | } |
111 | } |
105 | |
112 | |
106 | # quickly extratc width and height of the (necessarily PNG) image |
113 | # quickly extract width and height of the (necessarily PNG) image |
107 | unless ($png =~ /^\x89PNG\x0d\x0a\x1a\x0a....IHDR(........)/s) { |
114 | unless ($png =~ /^\x89PNG\x0d\x0a\x1a\x0a....IHDR(........)/s) { |
108 | warn "$path: not a recongized png file, skipping.\n"; |
115 | warn "$path: not a recongized png file, skipping.\n"; |
109 | return; |
116 | next; |
110 | } |
117 | } |
111 | |
118 | |
112 | my ($w, $h) = unpack "NN", $1; |
119 | my ($w, $h) = unpack "NN", $1; |
113 | |
120 | |
114 | (my $face = $path) =~ s/^.*\///; |
121 | (my $face = $path) =~ s/^.*\///; |
115 | my $T = 32; |
122 | my $T = 32; |
116 | |
123 | |
117 | unless ($face =~ s/\.base\.(...)\.png$/.$1/) { |
124 | unless ($face =~ s/\.base\.(...)\.png$/.$1/) { |
118 | warn "$path: weird filename, skipping.\n"; |
125 | warn "$path: weird filename, skipping.\n"; |
119 | return; |
126 | next; |
120 | } |
127 | } |
121 | |
128 | |
122 | if ($w < $T || $h < $T) { |
129 | if ($w < $T || $h < $T) { |
123 | warn "$path: too small ($w $h), skipping.\n"; |
130 | warn "$path: too small ($w $h), skipping.\n"; |
124 | return; |
131 | next; |
125 | } |
132 | } |
126 | |
133 | |
127 | if ($w % $T || $h % $T) { |
134 | if ($w % $T || $h % $T) { |
128 | warn "$path: weird png size ($w $h), skipping.\n"; |
135 | warn "$path: weird png size ($w $h), skipping.\n"; |
129 | return; |
136 | next; |
130 | } |
137 | } |
131 | |
138 | |
132 | if (($w > $T || $h > $T) && $face !~ /_S\./) { |
139 | if (($w > $T || $h > $T) && $face !~ /_S\./) { |
133 | # split |
140 | # split |
134 | my @tile; |
141 | my @tile; |
… | |
… | |
149 | (map { |
156 | (map { |
150 | ( |
157 | ( |
151 | "(", |
158 | "(", |
152 | "+clone", |
159 | "+clone", |
153 | -crop => (sprintf "%dx%d+%d+%d", $T, $T, $_->[0] * $T, $_->[1] * $T), |
160 | -crop => (sprintf "%dx%d+%d+%d", $T, $T, $_->[0] * $T, $_->[1] * $T), |
|
|
161 | "+repage", |
154 | -write => "png:$_->[2]~", |
162 | -write => "png:$_->[2]~", |
155 | "+delete", |
163 | "+delete", |
156 | ")", |
164 | ")", |
157 | ) |
165 | ) |
158 | } @todo), |
166 | } @todo), |
… | |
… | |
191 | while (@arc) { |
199 | while (@arc) { |
192 | my ($dir, $file) = @{pop @arc}; |
200 | my ($dir, $file) = @{pop @arc}; |
193 | |
201 | |
194 | my $arc; |
202 | my $arc; |
195 | aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/ |
203 | aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/ |
|
|
204 | |
196 | my $arc = read_arch "$dir/$file"; |
205 | my $arc = read_arch "$dir/$file"; |
|
|
206 | for my $o (values %$arc) { |
|
|
207 | push @ARC, $o; |
|
|
208 | |
|
|
209 | $o->{editor_folder} = $dir; |
|
|
210 | |
|
|
211 | my $visibility = delete $o->{visibility}; |
|
|
212 | my $magicmap = delete $o->{magicmap}; |
|
|
213 | |
|
|
214 | # find upper left corner :/ |
|
|
215 | # omg, this is sooo broken |
|
|
216 | my ($dx, $dy); |
|
|
217 | for (my $o = $o; $o; $o = $o->{more}) { |
|
|
218 | $dx = $o->{x} if $o->{x} < $dx; |
|
|
219 | $dy = $o->{y} if $o->{y} < $dy; |
|
|
220 | } |
|
|
221 | |
|
|
222 | for (my $o = $o; $o; $o = $o->{more}) { |
|
|
223 | my $x = $o->{x} - $dx; |
|
|
224 | my $y = $o->{y} - $dy; |
|
|
225 | |
|
|
226 | my $ext = $x|$y ? "+$x+$y" : ""; |
|
|
227 | |
|
|
228 | $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/; |
|
|
229 | |
|
|
230 | my $visibility = delete $o->{visibility} if exists $o->{visibility}; |
|
|
231 | my $magicmap = delete $o->{magicmap} if exists $o->{magicmap}; |
|
|
232 | |
|
|
233 | my $anim = delete $o->{anim}; |
|
|
234 | |
|
|
235 | if ($anim) { |
|
|
236 | $o->{animation} = "$o->{_name}"; |
|
|
237 | |
|
|
238 | for (@$anim) { |
|
|
239 | $_ .= $ext unless /^facings\s|^blank.x11$|^empty.x11$/; |
|
|
240 | } |
|
|
241 | |
|
|
242 | $ANIM{"$o->{_name}$ext"} = |
|
|
243 | join "", map "$_\n", |
|
|
244 | "anim $o->{_name}", |
|
|
245 | @$anim, |
|
|
246 | "mina"; |
|
|
247 | } |
|
|
248 | |
|
|
249 | for my $face ($o->{face} || (), @{$anim || []}) { |
|
|
250 | next if $face =~ /^facings\s|^blank.x11$|^empty.x11$/; |
|
|
251 | |
|
|
252 | my $info = $FACEINFO{$face} ||= {}; |
|
|
253 | |
|
|
254 | $info->{visibility} = $visibility if defined $visibility; |
|
|
255 | $info->{magicmap} = $magicmap if defined $magicmap; |
|
|
256 | } |
|
|
257 | |
|
|
258 | if (my $smooth = delete $o->{smoothface}) { |
|
|
259 | $SMOOTH .= "$smooth\n"; |
|
|
260 | } |
|
|
261 | } |
|
|
262 | } |
197 | } |
263 | } |
198 | } |
264 | } |
199 | |
265 | |
200 | sub process_trs { |
266 | sub process_trs { |
201 | while (@trs) { |
267 | while (@trs) { |
202 | my ($dir, $file) = @{pop @trs}; |
268 | my ($dir, $file) = @{pop @trs}; |
|
|
269 | my $path = "$dir/$file"; |
|
|
270 | |
|
|
271 | my $trs; |
|
|
272 | if (0 > aio_load $path, $trs) { |
|
|
273 | warn "$path: $!, skipping.\n"; |
|
|
274 | next; |
|
|
275 | } |
|
|
276 | |
|
|
277 | $TRS .= $trs; |
203 | } |
278 | } |
204 | } |
279 | } |
205 | |
280 | |
206 | sub find_files; |
281 | sub find_files; |
207 | sub find_files { |
282 | sub find_files { |
… | |
… | |
245 | (async \&process_png), (async \&process_png), |
320 | (async \&process_png), (async \&process_png), |
246 | (async \&process_trs), (async \&process_trs), |
321 | (async \&process_trs), (async \&process_trs), |
247 | (async \&process_arc), (async \&process_arc), |
322 | (async \&process_arc), (async \&process_arc), |
248 | ); |
323 | ); |
249 | |
324 | |
250 | die "--install-arch not yet implemented\n"; |
325 | { |
|
|
326 | open my $fh, ">:utf8", "$DATADIR/animations~" |
|
|
327 | or die "$DATADIR/animations~: $!"; |
|
|
328 | print $fh join "", map $ANIM{$_}, sort keys %ANIM |
|
|
329 | } |
|
|
330 | |
|
|
331 | { |
|
|
332 | open my $fh, ">:utf8", "$DATADIR/archetypes~" |
|
|
333 | or die "$DATADIR/archetypes~: $!"; |
|
|
334 | substr $_->{editor_folder}, 0, 1 + length $path, "" for @ARC; |
|
|
335 | print $fh Crossfire::archlist_to_string \@ARC; |
|
|
336 | } |
|
|
337 | |
|
|
338 | { |
|
|
339 | open my $fh, ">:utf8", "$DATADIR/smooth~" |
|
|
340 | or die "$DATADIR/smooth~: $!"; |
|
|
341 | print $fh $SMOOTH; |
|
|
342 | } |
|
|
343 | |
|
|
344 | { |
|
|
345 | open my $fh, ">:utf8", "$DATADIR/treasures~" |
|
|
346 | or die "$DATADIR/treasures~: $!"; |
|
|
347 | print $fh $TRS; |
|
|
348 | } |
|
|
349 | |
|
|
350 | { |
|
|
351 | while (my ($k, $v) = each %FACEINFO) { |
|
|
352 | $v->{data32} ||= delete $PNG32{$k}; |
|
|
353 | } |
|
|
354 | |
|
|
355 | while (my ($k, $v) = each %FACEINFO) { |
|
|
356 | length $v->{data32} or warn "$k: face has no png32. this will not work (shoddy gcfclient will crash of course).\n"; |
|
|
357 | |
|
|
358 | $v->{chksum32} = Digest::MD5::md5 $v->{data32}; |
|
|
359 | } |
|
|
360 | |
|
|
361 | open my $fh, ">:perlio", "$DATADIR/faces~" |
|
|
362 | or die "$DATADIR/faces~: $!"; |
|
|
363 | |
|
|
364 | print $fh Storable::nfreeze \%FACEINFO; |
|
|
365 | } |
|
|
366 | |
|
|
367 | for (qw(archetypes faces animations treasures smooth)) { |
|
|
368 | chmod 0644, "$DATADIR/$_~"; |
|
|
369 | rename "$DATADIR/$_~", "$DATADIR/$_"; |
|
|
370 | } |
251 | } |
371 | } |
252 | } |
372 | } |
253 | |
373 | |
254 | Getopt::Long::Configure ("bundling", "no_ignore_case"); |
374 | Getopt::Long::Configure ("bundling", "no_ignore_case"); |
255 | GetOptions ( |
375 | GetOptions ( |