… | |
… | |
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] |
… | |
… | |
66 | } |
67 | } |
67 | |
68 | |
68 | sub inst_maps($) { |
69 | sub inst_maps($) { |
69 | my (undef, $path) = @_; |
70 | my (undef, $path) = @_; |
70 | |
71 | |
71 | print "installing '$path' to '$DATADIR/maps'\n\n"; |
72 | print "installing '$path' to '$DATADIR/maps'\n"; |
72 | |
73 | |
73 | if (!-f "$path/regions") { |
74 | if (!-f "$path/regions") { |
74 | warn "'$path' does not look like a maps directory ('regions' file is missing).\n"; |
75 | warn "'$path' does not look like a maps directory ('regions' file is missing).\n"; |
75 | exit 1 unless $FORCE; |
76 | exit 1 unless $FORCE; |
76 | } |
77 | } |
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" |
|
|
80 | or die "map installation failed.\n"; |
|
|
81 | |
|
|
82 | print "maps installed successfully.\n"; |
79 | } |
83 | } |
80 | |
84 | |
81 | { |
85 | { |
82 | our @PNG; |
86 | our %PNG32; |
|
|
87 | our %FACEINFO; |
83 | our @ARC; |
88 | our @ARC; |
|
|
89 | our $TRS; |
84 | our $NFILE; |
90 | our $NFILE; |
|
|
91 | our %ANIM; |
85 | |
92 | |
86 | our (@png, @trs, @arc); # files we are interested in |
93 | our (@png, @trs, @arc); # files we are interested in |
87 | |
94 | |
88 | sub commit_png { |
95 | sub commit_png { |
89 | my ($name, $data) = @_; |
96 | my ($name, $data) = @_; |
90 | #warn "$name: commited\n"; |
97 | |
|
|
98 | $PNG32{$name} = $data; |
|
|
99 | $FACEINFO{$name} ||= {}; |
91 | } |
100 | } |
92 | |
101 | |
93 | sub process_png { |
102 | sub process_png { |
94 | while (@png) { |
103 | while (@png) { |
95 | my $path = pop @png; |
104 | my $path = pop @png; |
… | |
… | |
98 | aio_lstat $path; |
107 | aio_lstat $path; |
99 | my ($size, $mtime) = (stat _)[7,9]; |
108 | my ($size, $mtime) = (stat _)[7,9]; |
100 | |
109 | |
101 | if (0 > aio_load $path, $png) { |
110 | if (0 > aio_load $path, $png) { |
102 | warn "$path: $!, skipping.\n"; |
111 | warn "$path: $!, skipping.\n"; |
103 | return; |
112 | next; |
104 | } |
113 | } |
105 | |
114 | |
106 | # quickly extratc width and height of the (necessarily PNG) image |
115 | # quickly extract width and height of the (necessarily PNG) image |
107 | unless ($png =~ /^\x89PNG\x0d\x0a\x1a\x0a....IHDR(........)/s) { |
116 | unless ($png =~ /^\x89PNG\x0d\x0a\x1a\x0a....IHDR(........)/s) { |
108 | warn "$path: not a recongized png file, skipping.\n"; |
117 | warn "$path: not a recongized png file, skipping.\n"; |
109 | return; |
118 | next; |
110 | } |
119 | } |
111 | |
120 | |
112 | my ($w, $h) = unpack "NN", $1; |
121 | my ($w, $h) = unpack "NN", $1; |
113 | |
122 | |
114 | (my $face = $path) =~ s/^.*\///; |
123 | (my $face = $path) =~ s/^.*\///; |
115 | my $T = 32; |
124 | my $T = 32; |
116 | |
125 | |
117 | unless ($face =~ s/\.base\.(...)\.png$/.$1/) { |
126 | unless ($face =~ s/\.base\.(...)\.png$/.$1/) { |
118 | warn "$path: weird filename, skipping.\n"; |
127 | warn "$path: weird filename, skipping.\n"; |
119 | return; |
128 | next; |
120 | } |
129 | } |
121 | |
130 | |
122 | if ($w < $T || $h < $T) { |
131 | if ($w < $T || $h < $T) { |
123 | warn "$path: too small ($w $h), skipping.\n"; |
132 | warn "$path: too small ($w $h), skipping.\n"; |
124 | return; |
133 | next; |
125 | } |
134 | } |
126 | |
135 | |
127 | if ($w % $T || $h % $T) { |
136 | if ($w % $T || $h % $T) { |
128 | warn "$path: weird png size ($w $h), skipping.\n"; |
137 | warn "$path: weird png size ($w $h), skipping.\n"; |
129 | return; |
138 | next; |
130 | } |
139 | } |
131 | |
140 | |
132 | if (($w > $T || $h > $T) && $face !~ /_S\./) { |
141 | if (($w > $T || $h > $T) && $face !~ /_S\./) { |
133 | # split |
142 | # split |
134 | my @tile; |
143 | my @tile; |
… | |
… | |
149 | (map { |
158 | (map { |
150 | ( |
159 | ( |
151 | "(", |
160 | "(", |
152 | "+clone", |
161 | "+clone", |
153 | -crop => (sprintf "%dx%d+%d+%d", $T, $T, $_->[0] * $T, $_->[1] * $T), |
162 | -crop => (sprintf "%dx%d+%d+%d", $T, $T, $_->[0] * $T, $_->[1] * $T), |
|
|
163 | "+repage", |
154 | -write => "png:$_->[2]~", |
164 | -write => "png:$_->[2]~", |
155 | "+delete", |
165 | "+delete", |
156 | ")", |
166 | ")", |
157 | ) |
167 | ) |
158 | } @todo), |
168 | } @todo), |
… | |
… | |
191 | while (@arc) { |
201 | while (@arc) { |
192 | my ($dir, $file) = @{pop @arc}; |
202 | my ($dir, $file) = @{pop @arc}; |
193 | |
203 | |
194 | my $arc; |
204 | my $arc; |
195 | aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/ |
205 | aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/ |
|
|
206 | |
196 | my $arc = read_arch "$dir/$file"; |
207 | my $arc = read_arch "$dir/$file"; |
|
|
208 | for my $o (values %$arc) { |
|
|
209 | push @ARC, $o; |
|
|
210 | |
|
|
211 | $o->{editor_folder} = $dir; |
|
|
212 | |
|
|
213 | my $visibility = delete $o->{visibility}; |
|
|
214 | my $magicmap = delete $o->{magicmap}; |
|
|
215 | |
|
|
216 | # find upper left corner :/ |
|
|
217 | # omg, this is sooo broken |
|
|
218 | my ($dx, $dy); |
|
|
219 | for (my $o = $o; $o; $o = $o->{more}) { |
|
|
220 | $dx = $o->{x} if $o->{x} < $dx; |
|
|
221 | $dy = $o->{y} if $o->{y} < $dy; |
|
|
222 | } |
|
|
223 | |
|
|
224 | for (my $o = $o; $o; $o = $o->{more}) { |
|
|
225 | my $x = $o->{x} - $dx; |
|
|
226 | my $y = $o->{y} - $dy; |
|
|
227 | |
|
|
228 | my $ext = $x|$y ? "+$x+$y" : ""; |
|
|
229 | |
|
|
230 | $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/; |
|
|
231 | |
|
|
232 | my $visibility = delete $o->{visibility} if exists $o->{visibility}; |
|
|
233 | my $magicmap = delete $o->{magicmap} if exists $o->{magicmap}; |
|
|
234 | |
|
|
235 | my $anim = delete $o->{anim}; |
|
|
236 | |
|
|
237 | if ($anim) { |
|
|
238 | $o->{animation} = "$o->{_name}"; |
|
|
239 | |
|
|
240 | for (@$anim) { |
|
|
241 | $_ .= $ext unless /^facings\s|^blank.x11$|^empty.x11$/; |
|
|
242 | } |
|
|
243 | |
|
|
244 | $ANIM{"$o->{_name}$ext"} = |
|
|
245 | join "", map "$_\n", |
|
|
246 | "anim $o->{_name}", |
|
|
247 | @$anim, |
|
|
248 | "mina"; |
|
|
249 | } |
|
|
250 | |
|
|
251 | for my $face ($o->{face} || (), @{$anim || []}) { |
|
|
252 | next if $face =~ /^facings\s|^blank.x11$|^empty.x11$/; |
|
|
253 | |
|
|
254 | my $info = $FACEINFO{$face} ||= {}; |
|
|
255 | |
|
|
256 | $info->{visibility} = $visibility if defined $visibility; |
|
|
257 | $info->{magicmap} = $magicmap if defined $magicmap; |
|
|
258 | } |
|
|
259 | |
|
|
260 | if (my $smooth = delete $o->{smoothface}) { |
|
|
261 | my ($face, $smooth) = split /\s+/, $smooth; |
|
|
262 | # skip empty_S.x11, it seems to server no purpose whatsoever |
|
|
263 | # but increases bandwidth demands and worse. |
|
|
264 | unless ($smooth eq "empty_S.x11") { |
|
|
265 | $FACEINFO{$face}{smooth} = $smooth; |
|
|
266 | } |
|
|
267 | } |
|
|
268 | } |
|
|
269 | } |
197 | } |
270 | } |
198 | } |
271 | } |
199 | |
272 | |
200 | sub process_trs { |
273 | sub process_trs { |
201 | while (@trs) { |
274 | while (@trs) { |
202 | my ($dir, $file) = @{pop @trs}; |
275 | my ($dir, $file) = @{pop @trs}; |
|
|
276 | my $path = "$dir/$file"; |
|
|
277 | |
|
|
278 | my $trs; |
|
|
279 | if (0 > aio_load $path, $trs) { |
|
|
280 | warn "$path: $!, skipping.\n"; |
|
|
281 | next; |
|
|
282 | } |
|
|
283 | |
|
|
284 | $TRS .= $trs; |
203 | } |
285 | } |
204 | } |
286 | } |
205 | |
287 | |
206 | sub find_files; |
288 | sub find_files; |
207 | sub find_files { |
289 | sub find_files { |
… | |
… | |
229 | } |
311 | } |
230 | |
312 | |
231 | sub inst_arch($) { |
313 | sub inst_arch($) { |
232 | my (undef, $path) = @_; |
314 | my (undef, $path) = @_; |
233 | |
315 | |
234 | print "installing '$path' to '$DATADIR'\n\n"; |
316 | print "installing '$path' to '$DATADIR'\n"; |
235 | |
317 | |
236 | if (!-d "$path/treasures") { |
318 | if (!-d "$path/treasures") { |
237 | warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; |
319 | warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; |
238 | exit 1 unless $FORCE; |
320 | exit 1 unless $FORCE; |
239 | } |
321 | } |
… | |
… | |
245 | (async \&process_png), (async \&process_png), |
327 | (async \&process_png), (async \&process_png), |
246 | (async \&process_trs), (async \&process_trs), |
328 | (async \&process_trs), (async \&process_trs), |
247 | (async \&process_arc), (async \&process_arc), |
329 | (async \&process_arc), (async \&process_arc), |
248 | ); |
330 | ); |
249 | |
331 | |
250 | die "--install-arch not yet implemented\n"; |
332 | { |
|
|
333 | open my $fh, ">:utf8", "$DATADIR/animations~" |
|
|
334 | or die "$DATADIR/animations~: $!"; |
|
|
335 | print $fh join "", map $ANIM{$_}, sort keys %ANIM |
|
|
336 | } |
|
|
337 | |
|
|
338 | { |
|
|
339 | open my $fh, ">:utf8", "$DATADIR/archetypes~" |
|
|
340 | or die "$DATADIR/archetypes~: $!"; |
|
|
341 | substr $_->{editor_folder}, 0, 1 + length $path, "" for @ARC; |
|
|
342 | print $fh Crossfire::archlist_to_string \@ARC; |
|
|
343 | } |
|
|
344 | |
|
|
345 | { |
|
|
346 | open my $fh, ">:utf8", "$DATADIR/treasures~" |
|
|
347 | or die "$DATADIR/treasures~: $!"; |
|
|
348 | print $fh $TRS; |
|
|
349 | } |
|
|
350 | |
|
|
351 | { |
|
|
352 | 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"; |
|
|
358 | |
|
|
359 | $v->{chksum32} = Digest::MD5::md5 $v->{data32}; |
|
|
360 | } |
|
|
361 | |
|
|
362 | open my $fh, ">:perlio", "$DATADIR/faces~" |
|
|
363 | or die "$DATADIR/faces~: $!"; |
|
|
364 | |
|
|
365 | print $fh Storable::nfreeze \%FACEINFO; |
|
|
366 | } |
|
|
367 | |
|
|
368 | for (qw(archetypes faces animations treasures)) { |
|
|
369 | chmod 0644, "$DATADIR/$_~"; |
|
|
370 | rename "$DATADIR/$_~", "$DATADIR/$_" |
|
|
371 | or die "$DATADIR/$_: $!"; |
|
|
372 | } |
|
|
373 | |
|
|
374 | print "archetype data installed successfully.\n"; |
251 | } |
375 | } |
252 | } |
376 | } |
253 | |
377 | |
254 | Getopt::Long::Configure ("bundling", "no_ignore_case"); |
378 | Getopt::Long::Configure ("bundling", "no_ignore_case"); |
255 | GetOptions ( |
379 | GetOptions ( |