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