… | |
… | |
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; |
85 | our @FACE; |
|
|
86 | our $ANIM; |
91 | our %ANIM; |
87 | |
92 | |
88 | our (@png, @trs, @arc); # files we are interested in |
93 | our (@png, @trs, @arc); # files we are interested in |
89 | |
94 | |
90 | sub commit_png { |
95 | sub commit_png { |
91 | my ($name, $data) = @_; |
96 | my ($name, $data) = @_; |
92 | |
97 | |
93 | push @PNG, [$name => $data]; |
98 | $PNG32{$name} = $data; |
|
|
99 | $FACEINFO{$name} ||= {}; |
94 | } |
100 | } |
95 | |
101 | |
96 | sub process_png { |
102 | sub process_png { |
97 | while (@png) { |
103 | while (@png) { |
98 | my $path = pop @png; |
104 | my $path = pop @png; |
… | |
… | |
104 | if (0 > aio_load $path, $png) { |
110 | if (0 > aio_load $path, $png) { |
105 | warn "$path: $!, skipping.\n"; |
111 | warn "$path: $!, skipping.\n"; |
106 | next; |
112 | next; |
107 | } |
113 | } |
108 | |
114 | |
109 | # quickly extratc width and height of the (necessarily PNG) image |
115 | # quickly extract width and height of the (necessarily PNG) image |
110 | unless ($png =~ /^\x89PNG\x0d\x0a\x1a\x0a....IHDR(........)/s) { |
116 | unless ($png =~ /^\x89PNG\x0d\x0a\x1a\x0a....IHDR(........)/s) { |
111 | warn "$path: not a recongized png file, skipping.\n"; |
117 | warn "$path: not a recongized png file, skipping.\n"; |
112 | next; |
118 | next; |
113 | } |
119 | } |
114 | |
120 | |
… | |
… | |
147 | my @todo = grep { $_->[3] <= $mtime } @tile; |
153 | my @todo = grep { $_->[3] <= $mtime } @tile; |
148 | if (@todo) { |
154 | if (@todo) { |
149 | fork_sub { |
155 | fork_sub { |
150 | open my $convert, "|-", $CONVERT, |
156 | open my $convert, "|-", $CONVERT, |
151 | "png:-", |
157 | "png:-", |
152 | -negate,#d# |
|
|
153 | (map { |
158 | (map { |
154 | ( |
159 | ( |
155 | "(", |
160 | "(", |
156 | "+clone", |
161 | "+clone", |
157 | -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", |
158 | -write => "png:$_->[2]~", |
164 | -write => "png:$_->[2]~", |
159 | "+delete", |
165 | "+delete", |
160 | ")", |
166 | ")", |
161 | ) |
167 | ) |
162 | } @todo), |
168 | } @todo), |
… | |
… | |
199 | 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 :/ |
200 | |
206 | |
201 | my $arc = read_arch "$dir/$file"; |
207 | my $arc = read_arch "$dir/$file"; |
202 | for my $o (values %$arc) { |
208 | for my $o (values %$arc) { |
203 | push @ARC, $o; |
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 |
204 | my ($dx, $dy); |
218 | my ($dx, $dy); |
205 | # omg, this is sooo broken |
|
|
206 | for (my $o = $o; $o; $o = $o->{more}) { |
219 | for (my $o = $o; $o; $o = $o->{more}) { |
207 | $dx = $o->{x} if $o->{x} < $dx; |
220 | $dx = $o->{x} if $o->{x} < $dx; |
208 | $dy = $o->{y} if $o->{y} < $dy; |
221 | $dy = $o->{y} if $o->{y} < $dy; |
209 | } |
222 | } |
|
|
223 | |
210 | for (my $o = $o; $o; $o = $o->{more}) { |
224 | for (my $o = $o; $o; $o = $o->{more}) { |
211 | my $x = $o->{x} - $dx; |
225 | my $x = $o->{x} - $dx; |
212 | my $y = $o->{y} - $dy; |
226 | my $y = $o->{y} - $dy; |
213 | if ($x|$y) { |
227 | |
214 | $_ .= "+$x+$y" for $o->{face}, @{$o->{anim} || []}; |
228 | my $ext = $x|$y ? "+$x+$y" : ""; |
215 | } |
229 | |
216 | } |
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 | |
217 | if (my $anim = delete $o->{anim}) { |
235 | my $anim = delete $o->{anim}; |
|
|
236 | |
|
|
237 | if ($anim) { |
218 | $o->{animation} = $o->{_name}; |
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"} = |
219 | $ANIM .= join "", map "$_\n", |
245 | join "", map "$_\n", |
220 | "anim $o->{_name}", |
246 | "anim $o->{_name}", |
221 | @$anim, |
247 | @$anim, |
222 | "mina"; |
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 | } |
223 | } |
268 | } |
224 | } |
269 | } |
225 | } |
270 | } |
226 | } |
271 | } |
227 | |
272 | |
228 | sub process_trs { |
273 | sub process_trs { |
229 | while (@trs) { |
274 | while (@trs) { |
230 | 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; |
231 | } |
285 | } |
232 | } |
286 | } |
233 | |
287 | |
234 | sub find_files; |
288 | sub find_files; |
235 | sub find_files { |
289 | sub find_files { |
… | |
… | |
257 | } |
311 | } |
258 | |
312 | |
259 | sub inst_arch($) { |
313 | sub inst_arch($) { |
260 | my (undef, $path) = @_; |
314 | my (undef, $path) = @_; |
261 | |
315 | |
262 | print "installing '$path' to '$DATADIR'\n\n"; |
316 | print "installing '$path' to '$DATADIR'\n"; |
263 | |
317 | |
264 | if (!-d "$path/treasures") { |
318 | if (!-d "$path/treasures") { |
265 | 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"; |
266 | exit 1 unless $FORCE; |
320 | exit 1 unless $FORCE; |
267 | } |
321 | } |
… | |
… | |
276 | ); |
330 | ); |
277 | |
331 | |
278 | { |
332 | { |
279 | open my $fh, ">:utf8", "$DATADIR/animations~" |
333 | open my $fh, ">:utf8", "$DATADIR/animations~" |
280 | or die "$DATADIR/animations~: $!"; |
334 | or die "$DATADIR/animations~: $!"; |
281 | print $fh $ANIM; |
335 | print $fh join "", map $ANIM{$_}, sort keys %ANIM |
282 | } |
336 | } |
283 | |
337 | |
284 | { |
338 | { |
285 | open my $fh, ">:utf8", "$DATADIR/archetypes~" |
339 | open my $fh, ">:utf8", "$DATADIR/archetypes~" |
286 | or die "$DATADIR/archetypes~: $!"; |
340 | or die "$DATADIR/archetypes~: $!"; |
|
|
341 | substr $_->{editor_folder}, 0, 1 + length $path, "" for @ARC; |
287 | print $fh Crossfire::archlist_to_string \@ARC; |
342 | print $fh Crossfire::archlist_to_string \@ARC; |
288 | } |
343 | } |
289 | |
344 | |
290 | @PNG = sort { $a->[0] cmp $b->[0] } @PNG; |
|
|
291 | |
|
|
292 | { |
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 | |
293 | open my $fh, ">:perlio", "$DATADIR/crossfire.0~" |
362 | open my $fh, ">:perlio", "$DATADIR/faces~" |
294 | or die "$DATADIR/crossfire.0~: $!"; |
363 | or die "$DATADIR/faces~: $!"; |
295 | printf $fh "IMAGE %d %d %s\x0a%s", $_, (length $PNG[$_][1]), $PNG[$_][0], $PNG[$_][1] |
|
|
296 | for 0.. $#PNG; |
|
|
297 | } |
|
|
298 | |
364 | |
299 | rename "$DATADIR/archetypes~" , "$DATADIR/archetypes"; |
365 | $FACEINFO{""} = { version => 1}; |
300 | rename "$DATADIR/crossfire.0~", "$DATADIR/crossfire.0"; |
366 | print $fh Storable::nfreeze \%FACEINFO; |
301 | rename "$DATADIR/animations~" , "$DATADIR/animations"; |
367 | } |
302 | |
368 | |
303 | die "--install-arch not fully implemented\n"; |
369 | for (qw(archetypes faces animations treasures)) { |
|
|
370 | chmod 0644, "$DATADIR/$_~"; |
|
|
371 | rename "$DATADIR/$_~", "$DATADIR/$_" |
|
|
372 | or die "$DATADIR/$_: $!"; |
|
|
373 | } |
|
|
374 | |
|
|
375 | print "archetype data installed successfully.\n"; |
304 | } |
376 | } |
305 | } |
377 | } |
306 | |
378 | |
307 | Getopt::Long::Configure ("bundling", "no_ignore_case"); |
379 | Getopt::Long::Configure ("bundling", "no_ignore_case"); |
308 | GetOptions ( |
380 | GetOptions ( |