1 | #!@PERL@ |
1 | #!@PERL@ |
2 | |
2 | |
3 | use strict; |
3 | # |
|
|
4 | # This file is part of Deliantra, the Roguelike Realtime MMORPG. |
|
|
5 | # |
|
|
6 | # Copyright (©) 2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
|
|
7 | # |
|
|
8 | # Deliantra is free software: you can redistribute it and/or modify it under |
|
|
9 | # the terms of the Affero GNU General Public License as published by the |
|
|
10 | # Free Software Foundation, either version 3 of the License, or (at your |
|
|
11 | # option) any later version. |
|
|
12 | # |
|
|
13 | # This program is distributed in the hope that it will be useful, |
|
|
14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
16 | # GNU General Public License for more details. |
|
|
17 | # |
|
|
18 | # You should have received a copy of the Affero GNU General Public License |
|
|
19 | # and the GNU General Public License along with this program. If not, see |
|
|
20 | # <http://www.gnu.org/licenses/>. |
|
|
21 | # |
|
|
22 | # The authors can be reached via e-mail to <support@deliantra.net> |
|
|
23 | # |
|
|
24 | |
|
|
25 | use common::sense; |
4 | |
26 | |
5 | my $prefix = "@prefix@"; |
27 | my $prefix = "@prefix@"; |
6 | my $exec_prefix = "@exec_prefix@"; |
28 | my $exec_prefix = "@exec_prefix@"; |
7 | my $datarootdir = "@datarootdir@"; |
29 | my $datarootdir = "@datarootdir@"; |
8 | my $DATADIR = "@datadir@/@PACKAGE@"; |
30 | my $DATADIR = "@datadir@/@PACKAGE@"; |
9 | |
31 | |
10 | my $CONVERT = "@CONVERT@"; |
32 | my $CONVERT = "@CONVERT@"; |
11 | my $IDENTIFY = "@IDENTIFY@"; |
33 | my $IDENTIFY = "@IDENTIFY@"; |
12 | my $OPTIPNG = "@OPTIPNG@"; |
34 | my $OPTIPNG = "@OPTIPNG@"; |
13 | my $RSYNC = "@RSYNC@"; |
35 | my $RSYNC = "@RSYNC@"; |
|
|
36 | my $PNGNQ = "@PNGNQ@"; |
14 | |
37 | |
15 | use Getopt::Long; |
38 | use Getopt::Long; |
16 | use Coro::Event; |
39 | use File::Temp; |
|
|
40 | use POSIX (); |
|
|
41 | use Carp; |
|
|
42 | |
|
|
43 | use YAML::XS (); |
|
|
44 | use Digest::MD5 (); |
|
|
45 | use Storable (); |
|
|
46 | use JSON::XS (); |
|
|
47 | use IO::AIO (); |
|
|
48 | use Compress::LZF (); |
|
|
49 | |
17 | use AnyEvent; |
50 | use AnyEvent; |
18 | use IO::AIO (); |
51 | use Coro 5.12; |
19 | use File::Temp; |
|
|
20 | use Crossfire; |
|
|
21 | use Coro; |
52 | use Coro::EV; |
22 | use Coro::AIO; |
53 | use Coro::AIO; |
23 | use POSIX (); |
54 | use Coro::Util; |
24 | use Digest::MD5; |
55 | use Coro::Channel; |
|
|
56 | use Coro::AnyEvent; |
|
|
57 | use Coro::Storable; $Storable::canonical = 1; |
|
|
58 | |
|
|
59 | use Deliantra; |
|
|
60 | |
|
|
61 | $SIG{QUIT} = sub { Carp::cluck "QUIT" }; |
25 | |
62 | |
26 | sub usage { |
63 | sub usage { |
27 | warn <<EOF; |
64 | warn <<EOF; |
28 | Usage: cfutil [-v] [-q] [--force] [--cache] |
65 | Usage: cfutil [-v] [-q] [--force] [--cache] |
29 | [--install-arch path] |
66 | [--install-arch path] |
… | |
… | |
40 | my $VERBOSE = 1; |
77 | my $VERBOSE = 1; |
41 | my $CACHE = 0; |
78 | my $CACHE = 0; |
42 | my $FORCE; |
79 | my $FORCE; |
43 | my $TMPDIR = "/tmp/cfutil$$~"; |
80 | my $TMPDIR = "/tmp/cfutil$$~"; |
44 | my $TMPFILE = "aaaa0"; |
81 | my $TMPFILE = "aaaa0"; |
|
|
82 | my @COMMIT; |
|
|
83 | |
|
|
84 | our %COLOR = ( |
|
|
85 | # original cf colours |
|
|
86 | black => 0, |
|
|
87 | white => 1, |
|
|
88 | navy => 2, # "dark blue" |
|
|
89 | red => 3, |
|
|
90 | orange => 4, |
|
|
91 | blue => 5, # "light blue" |
|
|
92 | darkorange => 6, |
|
|
93 | green => 7, |
|
|
94 | lightgreen => 8, |
|
|
95 | grey => 9, |
|
|
96 | brown => 10, # "yellow" |
|
|
97 | gold => 11, # "light yellow" |
|
|
98 | tan => 12, # yellowish gray |
|
|
99 | |
|
|
100 | # new for deliantra |
|
|
101 | none => 13, |
|
|
102 | |
|
|
103 | # compatibility to existing archetypes |
|
|
104 | lightblue => 5, |
|
|
105 | gray => 9, |
|
|
106 | yellow => 11, |
|
|
107 | khaki => 12, |
|
|
108 | ); |
45 | |
109 | |
46 | END { system "rm", "-rf", $TMPDIR } |
110 | END { system "rm", "-rf", $TMPDIR } |
47 | |
111 | |
48 | Event->signal (signal => "INT", cb => sub { exit 1 }); |
112 | my $s_INT = EV::signal INT => sub { exit 1 }; |
49 | Event->signal (signal => "TERM", cb => sub { exit 1 }); |
113 | my $s_TERM = EV::signal TERM => sub { exit 1 }; |
|
|
114 | |
|
|
115 | our %hash; |
|
|
116 | |
|
|
117 | # here we could try to avoid collisions and reduce chksum size further |
|
|
118 | sub make_hash($\$\$;$) { |
|
|
119 | my ($id, $dataref, $hashref, $clen) = @_; |
|
|
120 | |
|
|
121 | my $hash = substr +(Digest::MD5::md5 $$dataref), 0, $clen || 4; |
|
|
122 | |
|
|
123 | if (exists $hash{$hash}) { |
|
|
124 | # hash collision, but some files are simply identical |
|
|
125 | if (${$hash{$hash}[1]} ne $$dataref) { |
|
|
126 | warn "hash collision $hash{$hash}[0] vs. $id\n"; |
|
|
127 | exit 1; |
|
|
128 | } else { |
|
|
129 | print "$hash{$hash}[0] and $id are identical (which is fine).\n" if $VERBOSE >= 3; |
|
|
130 | } |
|
|
131 | } |
|
|
132 | $hash{$hash} = [$id, $dataref, $hashref]; |
|
|
133 | |
|
|
134 | $$hashref = $hash; |
|
|
135 | } |
50 | |
136 | |
51 | mkdir $TMPDIR, 0700 |
137 | mkdir $TMPDIR, 0700 |
52 | or die "$TMPDIR: $!"; |
138 | or die "$TMPDIR: $!"; |
53 | |
139 | |
54 | sub fork_sub(&) { |
140 | sub fork_exec(&) { |
55 | my ($cb) = @_; |
141 | my ($cb) = @_; |
56 | |
142 | |
57 | if (my $pid = fork) { |
143 | if (my $pid = fork) { |
58 | my $current = $Coro::current; |
144 | my $current = $Coro::current; |
59 | my $w = AnyEvent->child (pid => $pid, cb => sub { $current->ready }); |
145 | my $w = AnyEvent->child (pid => $pid, cb => sub { $current->ready }); |
… | |
… | |
64 | warn $@; |
150 | warn $@; |
65 | POSIX::_exit 1; |
151 | POSIX::_exit 1; |
66 | } |
152 | } |
67 | } |
153 | } |
68 | |
154 | |
|
|
155 | sub imgsize($) { |
|
|
156 | open my $fh, "-|", $IDENTIFY, qw(-ping -format %w,%h --), $_[0] |
|
|
157 | or die "$IDENTIFY: $!"; |
|
|
158 | |
|
|
159 | Coro::AnyEvent::readable $fh; |
|
|
160 | |
|
|
161 | my ($w, $h) = split /,/, <$fh>; |
|
|
162 | |
|
|
163 | ($w+0, $h+0) |
|
|
164 | } |
|
|
165 | |
|
|
166 | # make $dst from $src, if not uptodate, by calling $how |
|
|
167 | sub make_file($$$) { |
|
|
168 | my ($src, $dst, $how) = @_; |
|
|
169 | |
|
|
170 | my $t = (aio_stat $dst) ? -1 : (stat _)[9]; |
|
|
171 | |
|
|
172 | for (ref $src ? @$src : $src) { |
|
|
173 | aio_stat $_ |
|
|
174 | and die "$_: $!"; |
|
|
175 | |
|
|
176 | if ((stat _)[9] > $t) { |
|
|
177 | # outdated, redo |
|
|
178 | $how->(); |
|
|
179 | last; |
|
|
180 | } |
|
|
181 | } |
|
|
182 | } |
|
|
183 | |
69 | sub inst_maps($) { |
184 | sub inst_maps($) { |
70 | my (undef, $path) = @_; |
185 | my (undef, $path) = @_; |
71 | |
186 | |
72 | print "installing '$path' to '$DATADIR/maps'\n"; |
187 | print "\nInstalling '$path' to '$DATADIR/maps'\n\n"; |
73 | |
188 | |
74 | if (!-f "$path/regions") { |
189 | if (!-f "$path/regions") { |
75 | warn "'$path' does not look like a maps directory ('regions' file is missing).\n"; |
190 | warn "'$path' does not look like a maps directory ('regions' file is missing).\n"; |
76 | exit 1 unless $FORCE; |
191 | exit 1 unless $FORCE; |
77 | } |
192 | } |
78 | |
193 | |
79 | system $RSYNC, "-av", "$path/.", "$DATADIR/maps/.", "--delete", "--exclude", "CVS", "--delete-excluded" |
194 | system $RSYNC, "-av", "--chmod=u=rwX,go=rX", |
|
|
195 | "$path/.", "$DATADIR/maps/.", |
|
|
196 | "--exclude", "CVS", "--exclude", "/world-precomposed", |
|
|
197 | "--delete", "--delete-excluded" |
80 | and die "map installation failed.\n"; |
198 | and die "map installation failed.\n"; |
81 | |
199 | |
82 | print "maps installed successfully.\n"; |
200 | print "maps installed successfully.\n"; |
83 | } |
201 | } |
84 | |
202 | |
85 | { |
203 | { |
86 | our %PNG32; |
204 | our %ANIMINFO; |
87 | our %FACEINFO; |
205 | our %FACEINFO; |
|
|
206 | our %RESOURCE; |
88 | our @ARC; |
207 | our @ARC; |
|
|
208 | our %ARC; |
89 | our $TRS; |
209 | our $TRS; |
90 | our $NFILE; |
210 | our $NFILE; |
91 | our %ANIM; |
211 | our $PATH; |
92 | |
212 | |
93 | our (@png, @trs, @arc); # files we are interested in |
213 | our $QUANTIZE = "+dither -colorspace RGB -colors 256"; |
94 | |
214 | |
|
|
215 | our $c_arc = new Coro::Channel; |
|
|
216 | our $c_any = new Coro::Channel; |
|
|
217 | |
|
|
218 | our @c_png; |
|
|
219 | |
95 | sub commit_png { |
220 | sub commit_png($$$$) { |
96 | my ($name, $data) = @_; |
221 | my ($stem, $name, $data, $T) = @_; |
97 | |
222 | |
98 | $PNG32{$name} = $data; |
223 | $FACEINFO{$name}{"stem"} = substr $stem, 1 + length $PATH; |
99 | $FACEINFO{$name} ||= {}; |
224 | $FACEINFO{$name}{"data$T"} = $data; |
100 | } |
225 | } |
101 | |
226 | |
102 | sub process_png { |
227 | sub process_png { |
103 | while (@png) { |
228 | while (@c_png) { |
104 | my $path = pop @png; |
229 | my ($path, $delete) = @{pop @c_png}; |
105 | |
230 | |
106 | my $png; |
231 | my $png; |
107 | aio_lstat $path; |
232 | aio_lstat $path; |
108 | my ($size, $mtime) = (stat _)[7,9]; |
233 | my ($size, $mtime) = (stat _)[7,9]; |
109 | |
234 | |
110 | if (0 > aio_load $path, $png) { |
235 | if (0 > aio_load $path, $png) { |
111 | warn "$path: $!, skipping.\n"; |
236 | warn "$path: $!, skipping.\n"; |
112 | next; |
237 | next; |
113 | } |
238 | } |
114 | |
239 | |
|
|
240 | my $stem = $path; |
|
|
241 | my $T; |
|
|
242 | |
|
|
243 | if ($stem =~ s/\.32x32\.png~?$//) { |
|
|
244 | $T = 32; |
|
|
245 | } elsif ($stem =~ s/\.64x64\.png~?$//) { |
|
|
246 | $T = 64; |
|
|
247 | } else { |
|
|
248 | warn "$path: weird filename, skipping.\n"; |
|
|
249 | next; |
|
|
250 | } |
|
|
251 | |
115 | # quickly extract width and height of the (necessarily PNG) image |
252 | # quickly extract width and height of the (necessarily PNG) image |
116 | unless ($png =~ /^\x89PNG\x0d\x0a\x1a\x0a....IHDR(........)/s) { |
253 | unless ($png =~ /^\x89PNG\x0d\x0a\x1a\x0a....IHDR(........)/s) { |
117 | warn "$path: not a recongized png file, skipping.\n"; |
254 | warn "$path: not a recognized png file, skipping.\n"; |
118 | next; |
255 | next; |
119 | } |
256 | } |
120 | |
257 | |
121 | my ($w, $h) = unpack "NN", $1; |
258 | my ($w, $h) = unpack "NN", $1; |
122 | |
|
|
123 | (my $face = $path) =~ s/^.*\///; |
|
|
124 | my $T = 32; |
|
|
125 | |
|
|
126 | unless ($face =~ s/\.base\.(...)\.png$/.$1/) { |
|
|
127 | warn "$path: weird filename, skipping.\n"; |
|
|
128 | next; |
|
|
129 | } |
|
|
130 | |
259 | |
131 | if ($w < $T || $h < $T) { |
260 | if ($w < $T || $h < $T) { |
132 | warn "$path: too small ($w $h), skipping.\n"; |
261 | warn "$path: too small ($w $h), skipping.\n"; |
133 | next; |
262 | next; |
134 | } |
263 | } |
… | |
… | |
136 | if ($w % $T || $h % $T) { |
265 | if ($w % $T || $h % $T) { |
137 | warn "$path: weird png size ($w $h), skipping.\n"; |
266 | warn "$path: weird png size ($w $h), skipping.\n"; |
138 | next; |
267 | next; |
139 | } |
268 | } |
140 | |
269 | |
|
|
270 | (my $base = $stem) =~ s/^.*\///; |
|
|
271 | |
|
|
272 | my $fi = $FACEINFO{$base}; |
|
|
273 | unless ($fi) { |
|
|
274 | #warn "$path: <$base> not referenced by any archetype, skipping.\n"; |
|
|
275 | #next; |
|
|
276 | } |
|
|
277 | |
|
|
278 | my $arc = $fi->{arc} || { }; |
|
|
279 | |
|
|
280 | unless ($path =~ /~$/) { |
|
|
281 | # possibly enlarge |
|
|
282 | if (0 > aio_stat "$stem.64x64.png") { |
|
|
283 | my $other = "$stem.64x64.png~"; |
|
|
284 | |
|
|
285 | make_file $path, $other, sub { |
|
|
286 | fork_exec { |
|
|
287 | my $CROP; |
|
|
288 | my $SRC = "png:\Q$path\E"; |
|
|
289 | |
|
|
290 | my $is_floor = $arc->{is_floor}; |
|
|
291 | my $is_wall = 0; |
|
|
292 | |
|
|
293 | my ($wall_pfx, $wall_dir, $wall_sfx); |
|
|
294 | |
|
|
295 | if ( |
|
|
296 | !$is_floor |
|
|
297 | && !$arc->{alive} |
|
|
298 | && $arc->{move_block} eq "all" |
|
|
299 | && $path =~ /^(.*_)([0-9A-F])(\.x11.*\.png)$/ |
|
|
300 | ) { |
|
|
301 | ($wall_pfx, $wall_dir, $wall_sfx) = ($1, hex $2, $3); |
|
|
302 | |
|
|
303 | unless (grep { !-e sprintf "%s%X%s", $wall_pfx, $_, $wall_sfx } 0..15) { |
|
|
304 | $is_wall = 1; |
|
|
305 | } |
|
|
306 | } |
|
|
307 | |
|
|
308 | if ($is_wall || $is_floor) { |
|
|
309 | # add a 4px border and add other images around it |
|
|
310 | $CROP = "-shave 8x8 +repage"; |
|
|
311 | |
|
|
312 | $w += 8; |
|
|
313 | $h += 8; |
|
|
314 | |
|
|
315 | $SRC = "-size ${w}x${h} xc:transparent"; |
|
|
316 | $SRC .= " png:\Q$path\E -geometry +4+4 -composite"; |
|
|
317 | |
|
|
318 | # 8 surrounding images |
|
|
319 | for ( |
|
|
320 | # x y b r0 r1 |
|
|
321 | [-1, -1, 0, 6], |
|
|
322 | [ 0, -1, 1, 10, 14], |
|
|
323 | [+1, -1, 0, 12], |
|
|
324 | |
|
|
325 | [-1, 0, 8, 5, 7], |
|
|
326 | # |
|
|
327 | [+1, 0, 2, 5, 13], |
|
|
328 | |
|
|
329 | [-1, +1, 0, 3], |
|
|
330 | [ 0, +1, 4, 10, 11], |
|
|
331 | [+1, +1, 0, 9], |
|
|
332 | ) { |
|
|
333 | my ($x, $y, $d, $r0, $r1) = @$_; |
|
|
334 | |
|
|
335 | my $tile = $is_floor ? $path |
|
|
336 | : $is_wall ? sprintf "%s%X%s", $wall_pfx, ($wall_dir & $d) ? $r1 : $r0, $wall_sfx |
|
|
337 | : die; |
|
|
338 | |
|
|
339 | $SRC .= sprintf " png:%s -geometry %+d%+d -composite", |
|
|
340 | "\Q$tile", |
|
|
341 | $x * ($w - 8) + 4, |
|
|
342 | $y * ($h - 8) + 4; |
|
|
343 | } |
|
|
344 | } |
|
|
345 | |
|
|
346 | system "convert -depth 8 $SRC rgba:-" |
|
|
347 | . "| $exec_prefix/bin/cfhq2xa $w $h 0" |
|
|
348 | . "| convert -depth 8 -size ".($w * 2)."x".($h * 2)." rgba:- $CROP $QUANTIZE -quality 00 png32:\Q$other\E~" |
|
|
349 | and die "convert/cfhq2xa pipeline error: status $? ($!)"; |
|
|
350 | system $OPTIPNG, "-i0", "-q", "$other~"; |
|
|
351 | die "$other~ has zero size, aborting." unless -s "$other~"; |
|
|
352 | rename "$other~", $other; |
|
|
353 | }; |
|
|
354 | }; |
|
|
355 | |
|
|
356 | push @c_png, [$other, !$CACHE]; |
|
|
357 | } |
|
|
358 | |
|
|
359 | # possibly scale down |
|
|
360 | if (0 > aio_stat "$stem.32x32.png") { |
|
|
361 | my $other = "$stem.32x32.png~"; |
|
|
362 | |
|
|
363 | make_file $path, $other, sub { |
|
|
364 | fork_exec { |
|
|
365 | system "convert png:\Q$path\E -geometry 50% -filter lanczos $QUANTIZE -quality 00 png32:\Q$other\E~"; |
|
|
366 | system $OPTIPNG, "-i0", "-q", "$other~"; |
|
|
367 | |
|
|
368 | # reduce smoothfaces >10000 bytes |
|
|
369 | # obsolete, no longer required |
|
|
370 | if (0 && $stem =~ /_S\./ && (-s "$other~") > 10000) { |
|
|
371 | my $ncolor = 256; |
|
|
372 | while () { |
|
|
373 | system "<\Q$other~\E $PNGNQ -s1 -n$ncolor >\Q$other~~\E"; |
|
|
374 | system $OPTIPNG, "-i0", "-q", "$other~~"; |
|
|
375 | last if 10000 > -s "$other~~"; |
|
|
376 | $ncolor = int $ncolor * 0.9; |
|
|
377 | $ncolor > 8 or die "cannot reduce filesize to < 10000 bytes"; |
|
|
378 | } |
|
|
379 | |
|
|
380 | printf "reduced %s from %d to %d bytes using %d colours.\n", |
|
|
381 | $other, -s "$other~", -s "$other~~", $ncolor |
|
|
382 | if $VERBOSE >= 2; |
|
|
383 | rename "$other~~", "$other~"; |
|
|
384 | } |
|
|
385 | |
|
|
386 | die "$other~ has zero size, aborting." unless -s "$other~"; |
|
|
387 | rename "$other~", $other; |
|
|
388 | }; |
|
|
389 | }; |
|
|
390 | |
|
|
391 | #warn "scaled down $path to $other\n";#d# |
|
|
392 | push @c_png, [$other, !$CACHE]; |
|
|
393 | } |
|
|
394 | } |
|
|
395 | |
|
|
396 | (my $face = $stem) =~ s/^.*\///; |
|
|
397 | |
|
|
398 | # split all bigfaces, but avoid smoothfaces (*_S) |
141 | if (($w > $T || $h > $T) && $face !~ /_S\./) { |
399 | if (($w > $T || $h > $T) && $face !~ /_S\./) { |
142 | # split |
400 | # split |
143 | my @tile; |
401 | my @tile; |
144 | for my $x (0 .. (int $w / $T) - 1) { |
402 | for my $x (0 .. (int $w / $T) - 1) { |
145 | for my $y (0 .. (int $h / $T) - 1) { |
403 | for my $y (0 .. (int $h / $T) - 1) { |
… | |
… | |
150 | } |
408 | } |
151 | |
409 | |
152 | my $mtime = (lstat $path)[9]; |
410 | my $mtime = (lstat $path)[9]; |
153 | my @todo = grep { $_->[3] <= $mtime } @tile; |
411 | my @todo = grep { $_->[3] <= $mtime } @tile; |
154 | if (@todo) { |
412 | if (@todo) { |
155 | fork_sub { |
413 | fork_exec { |
156 | open my $convert, "|-", $CONVERT, |
414 | open my $convert, "|-", $CONVERT, |
157 | "png:-", |
415 | "png:-", |
158 | (map { |
416 | (map { |
159 | ( |
417 | ( |
160 | "(", |
418 | "(", |
161 | "+clone", |
419 | "+clone", |
162 | -crop => (sprintf "%dx%d+%d+%d", $T, $T, $_->[0] * $T, $_->[1] * $T), |
420 | -crop => (sprintf "%dx%d+%d+%d", $T, $T, $_->[0] * $T, $_->[1] * $T), |
163 | "+repage", |
421 | "+repage", |
|
|
422 | -quality => "00", |
164 | -write => "png:$_->[2]~", |
423 | -write => "png32:$_->[2]~", |
165 | "+delete", |
424 | "+delete", |
166 | ")", |
425 | ")", |
167 | ) |
426 | ) |
168 | } @todo), |
427 | } @todo), |
169 | "null:"; |
428 | "null:"; |
… | |
… | |
173 | close $convert; |
432 | close $convert; |
174 | |
433 | |
175 | # pass 2, optimise, and rename |
434 | # pass 2, optimise, and rename |
176 | for (@todo) { |
435 | for (@todo) { |
177 | system $OPTIPNG, "-o5", "-i0", "-q", "$_->[2]~"; |
436 | system $OPTIPNG, "-o5", "-i0", "-q", "$_->[2]~"; |
|
|
437 | die "$_->[2]~ has zero size, aborting." unless -s "$_->[2]~"; |
178 | rename "$_->[2]~", $_->[2]; |
438 | rename "$_->[2]~", $_->[2]; |
179 | } |
439 | } |
180 | }; |
440 | }; |
181 | } |
441 | } |
182 | |
442 | |
… | |
… | |
186 | |
446 | |
187 | if (0 > aio_load $file, $tile) { |
447 | if (0 > aio_load $file, $tile) { |
188 | die "$path: unable to read tile +$x+$y, aborting.\n"; |
448 | die "$path: unable to read tile +$x+$y, aborting.\n"; |
189 | } |
449 | } |
190 | IO::AIO::aio_unlink $file unless $CACHE; |
450 | IO::AIO::aio_unlink $file unless $CACHE; |
191 | commit_png $x|$y ? "$face+$x+$y" : $face, $tile; |
451 | commit_png $stem, $x|$y ? "$face+$x+$y" : $face, $tile, $T; |
192 | } |
452 | } |
193 | } else { |
453 | } else { |
194 | # use as-is (either small, use smooth) |
454 | # use as-is (either small, use smooth) |
195 | commit_png $face, $png; |
455 | commit_png $stem, $face, $png, $T; |
|
|
456 | } |
|
|
457 | |
|
|
458 | aio_unlink $path if $delete; |
|
|
459 | } |
|
|
460 | } |
|
|
461 | |
|
|
462 | sub process_faceinfo { |
|
|
463 | my ($dir, $file) = @_; |
|
|
464 | my $path = "$dir/$file"; |
|
|
465 | |
|
|
466 | my $data; |
|
|
467 | if (0 > aio_load $path, $data) { |
|
|
468 | warn "$path: $!, skipping.\n"; |
|
|
469 | return; |
|
|
470 | } |
|
|
471 | |
|
|
472 | for (split /\n/, $data) { |
|
|
473 | chomp; |
|
|
474 | my ($face, $visibility, $fg, $bg, $glyph) = split /\s+/, $_, 5; |
|
|
475 | # bg not used except for text clients |
|
|
476 | |
|
|
477 | utf8::decode $glyph; |
|
|
478 | $glyph =~ s/^\?(?=.)//; # remove "autoglyph" flag |
|
|
479 | $glyph =~ s/^"(.+)"$/$1/; # allow for ""-style quoting |
|
|
480 | |
|
|
481 | $fg = "white" if $fg eq "none"; # lots of faces have no fg colour yet |
|
|
482 | |
|
|
483 | (my $fgi = $COLOR{$fg}) |
|
|
484 | // warn "WARNING: $path: $face specifies unknown foreground colour '$fg'.\n"; |
|
|
485 | (my $bgi = $COLOR{$bg}) |
|
|
486 | // warn "WARNING: $path: $face specifies unknown background colour '$bg'.\n"; |
|
|
487 | |
|
|
488 | my $fi = $FACEINFO{$face} ||= { }; |
|
|
489 | $fi->{visibility} = $visibility * 1; |
|
|
490 | $fi->{magicmap} = $fgi; # foreground colour becomes magicmap |
|
|
491 | |
|
|
492 | $glyph .= " " if 2 > length $glyph; # TODO kanji |
|
|
493 | die "glyph $face too long" if 2 < length $glyph; |
|
|
494 | |
|
|
495 | $fi->{glyph} = ""; |
|
|
496 | for (split //, $glyph) { |
|
|
497 | utf8::encode $_; |
|
|
498 | $fi->{glyph} .= (chr $fgi) . (chr $bgi) . $_; |
196 | } |
499 | } |
197 | } |
500 | } |
198 | } |
501 | } |
199 | |
502 | |
200 | sub process_arc { |
503 | sub process_arc { |
201 | while (@arc) { |
504 | while (my ($dir, $file) = @{ $c_arc->get }) { |
202 | my ($dir, $file) = @{pop @arc}; |
|
|
203 | |
|
|
204 | my $arc; |
505 | my $arc; |
205 | aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/ |
506 | aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/ |
206 | |
507 | |
207 | my $arc = read_arch "$dir/$file"; |
508 | my $arc = read_arch "$dir/$file"; |
208 | for my $o (values %$arc) { |
509 | for my $o (values %$arc) { |
209 | push @ARC, $o; |
510 | push @ARC, $o; |
|
|
511 | for (my $m = $o; $m; $m = $m->{more}) { |
|
|
512 | $ARC{$m->{_name}} = $m; |
|
|
513 | } |
210 | |
514 | |
211 | $o->{editor_folder} = $dir; |
515 | $o->{editor_folder} ||= "\x00$dir"; # horrible kludge |
212 | |
|
|
213 | my $visibility = delete $o->{visibility}; |
|
|
214 | my $magicmap = delete $o->{magicmap}; |
|
|
215 | |
516 | |
216 | # find upper left corner :/ |
517 | # find upper left corner :/ |
217 | # omg, this is sooo broken |
518 | # omg, this is sooo broken |
218 | my ($dx, $dy); |
519 | my ($dx, $dy); |
219 | for (my $o = $o; $o; $o = $o->{more}) { |
520 | for (my $o = $o; $o; $o = $o->{more}) { |
… | |
… | |
225 | my $x = $o->{x} - $dx; |
526 | my $x = $o->{x} - $dx; |
226 | my $y = $o->{y} - $dy; |
527 | my $y = $o->{y} - $dy; |
227 | |
528 | |
228 | my $ext = $x|$y ? "+$x+$y" : ""; |
529 | my $ext = $x|$y ? "+$x+$y" : ""; |
229 | |
530 | |
230 | $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/; |
531 | $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/ || !$o->{face}; |
231 | |
|
|
232 | my $visibility = delete $o->{visibility} if exists $o->{visibility}; |
|
|
233 | my $magicmap = delete $o->{magicmap} if exists $o->{magicmap}; |
|
|
234 | |
532 | |
235 | my $anim = delete $o->{anim}; |
533 | my $anim = delete $o->{anim}; |
236 | |
534 | |
237 | if ($anim) { |
535 | if ($anim) { |
|
|
536 | # possibly add $ext to the animation name to avoid |
|
|
537 | # the need to specify archnames for all parts |
|
|
538 | # of a multipart archetype. |
238 | $o->{animation} = "$o->{_name}"; |
539 | $o->{animation} = "$o->{_name}"; |
|
|
540 | my $facings = 1; |
|
|
541 | my @frames; |
239 | |
542 | |
240 | for (@$anim) { |
543 | for (@$anim) { |
241 | $_ .= $ext unless /^facings\s|^blank.x11$|^empty.x11$/; |
544 | if (/^facings\s+(\d+)/) { |
|
|
545 | $facings = $1*1; |
|
|
546 | } elsif (/^blank.x11$|^empty.x11$/) { |
|
|
547 | push @frames, $_; |
|
|
548 | } else { |
|
|
549 | push @frames, "$_$ext"; |
|
|
550 | } |
242 | } |
551 | } |
243 | |
552 | |
244 | $ANIM{"$o->{_name}$ext"} = |
553 | $ANIMINFO{$o->{animation}} = { |
245 | join "", map "$_\n", |
554 | facings => $facings, |
246 | "anim $o->{_name}", |
555 | frames => \@frames, |
247 | @$anim, |
556 | }; |
248 | "mina"; |
|
|
249 | } |
557 | } |
250 | |
558 | |
251 | for my $face ($o->{face} || (), @{$anim || []}) { |
559 | for ($o->{face} || (), @{$anim || []}) { |
252 | next if $face =~ /^facings\s|^blank.x11$|^empty.x11$/; |
560 | next if /^facings\s/; |
253 | |
561 | |
|
|
562 | my $face = $_; |
|
|
563 | $face =~ s/\+\d+\+\d+$//; # remove tile offset coordinates |
|
|
564 | |
254 | my $info = $FACEINFO{$face} ||= {}; |
565 | my $info = $FACEINFO{$face} ||= { }; |
|
|
566 | $info->{arc} = $o; |
255 | |
567 | |
256 | $info->{visibility} = $visibility if defined $visibility; |
568 | next if $face =~ /^blank.x11$|^empty.x11$/; |
257 | $info->{magicmap} = $magicmap if defined $magicmap; |
|
|
258 | } |
569 | } |
259 | |
570 | |
260 | if (my $smooth = delete $o->{smoothface}) { |
571 | if (my $smooth = delete $o->{smoothface}) { |
261 | my ($face, $smooth) = split /\s+/, $smooth; |
572 | my %kv = split /\s+/, $smooth; |
262 | # skip empty_S.x11, it seems to server no purpose whatsoever |
573 | my $level = $o->{smoothlevel}; #TODO: delete from $o if !gcfclient-support |
263 | # but increases bandwidth demands and worse. |
574 | while (my ($face, $smooth) = each %kv) { |
264 | unless ($smooth eq "empty_S.x11") { |
575 | $FACEINFO{$smooth}{arc} = $o; |
|
|
576 | |
265 | $FACEINFO{$face}{smooth} = $smooth; |
577 | $FACEINFO{$face}{smooth} = $smooth; |
|
|
578 | $FACEINFO{$face}{smoothlevel} = $level; |
266 | } |
579 | } |
267 | } |
580 | } |
268 | } |
581 | } |
269 | } |
582 | } |
270 | } |
583 | } |
271 | } |
584 | } |
272 | |
585 | |
273 | sub process_trs { |
586 | sub process_trs { |
274 | while (@trs) { |
|
|
275 | my ($dir, $file) = @{pop @trs}; |
587 | my ($dir, $file) = @_; |
276 | my $path = "$dir/$file"; |
588 | my $path = "$dir/$file"; |
277 | |
589 | |
278 | my $trs; |
590 | my $trs; |
279 | if (0 > aio_load $path, $trs) { |
591 | if (0 > aio_load $path, $trs) { |
280 | warn "$path: $!, skipping.\n"; |
592 | warn "$path: $!, skipping.\n"; |
|
|
593 | return; |
|
|
594 | } |
|
|
595 | |
|
|
596 | $TRS .= $trs; |
|
|
597 | } |
|
|
598 | |
|
|
599 | my %FILECACHE; |
|
|
600 | |
|
|
601 | sub load_cached($;$) { |
|
|
602 | unless (exists $FILECACHE{$_[0]}) { |
|
|
603 | my $data; |
|
|
604 | if (0 < aio_load $_[0], $data) { |
|
|
605 | if ($_[1]) { |
|
|
606 | $data = eval { $_[1]->($data) }; |
|
|
607 | warn "$_[0]: $@" if $@; |
281 | next; |
608 | } |
|
|
609 | } |
|
|
610 | |
|
|
611 | $FILECACHE{$_[0]} = $data; |
|
|
612 | } |
|
|
613 | |
|
|
614 | $FILECACHE{$_[0]} |
|
|
615 | } |
|
|
616 | |
|
|
617 | # convert an image and a palette to some indexed 2d-matrix structure |
|
|
618 | sub process_plt { |
|
|
619 | my ($base, $plt) = @_; |
|
|
620 | |
|
|
621 | my ($w, $h) = imgsize "$base.png"; |
|
|
622 | |
|
|
623 | $w * $h |
|
|
624 | or die "$base.png: unable to identify correct size\n"; |
|
|
625 | |
|
|
626 | my @plt; |
|
|
627 | my %map; |
|
|
628 | |
|
|
629 | for (split /\n/, $plt) { |
|
|
630 | next unless /\S/; |
|
|
631 | |
|
|
632 | /^([0-9a-fA-F]{3,6})\s*(.*?)\s*$/ |
|
|
633 | or die "unparseable palette entry for $base.plt: $_"; |
|
|
634 | |
|
|
635 | my ($rgb, $name) = ($1, $2); |
|
|
636 | |
|
|
637 | $rgb =~ s/^(.)(.)(.)$/$1$1$2$2$3$3/; |
|
|
638 | |
|
|
639 | $map{pack "H*", $rgb} = chr @plt; |
|
|
640 | push @plt, $name; |
|
|
641 | } |
|
|
642 | |
|
|
643 | make_file ["$base.plt", "$base.png"], "$base.tbl~", sub { |
|
|
644 | warn "building $base\n" if $VERBOSE >= 3; |
|
|
645 | |
|
|
646 | fork_exec { |
|
|
647 | open my $png, "-|", $CONVERT, qw(-depth 8 --), "$base.png", "rgb:" |
|
|
648 | or die "$base.png: $!"; |
|
|
649 | |
|
|
650 | local $/; |
|
|
651 | $png = <$png>; |
|
|
652 | |
|
|
653 | $w * $h * 3 == length $png |
|
|
654 | or die "$base.png: failed to read enough data from file\n"; |
|
|
655 | |
|
|
656 | $png =~ s/(...)/$map{$1}/ge; |
|
|
657 | |
|
|
658 | $w * $h == length $png |
|
|
659 | or die "$base.png: failed to map all data - wrong palette?\n"; |
|
|
660 | |
|
|
661 | { |
|
|
662 | open my $fh, ">:raw", "$base.tbl~~" |
|
|
663 | or die "$base.tbl~~: $!"; |
|
|
664 | syswrite $fh, $png; |
|
|
665 | } |
|
|
666 | |
|
|
667 | rename "$base.tbl~~", "$base.tbl~"; |
282 | } |
668 | }; |
|
|
669 | }; |
283 | |
670 | |
284 | $TRS .= $trs; |
671 | 0 <= aio_load "$base.tbl~", my $tbl |
|
|
672 | or die "$base.tbl~: $!"; |
|
|
673 | |
|
|
674 | IO::AIO::aio_unlink "$base.tbl~" unless $CACHE; |
|
|
675 | |
|
|
676 | Compress::LZF::compress nfreeze { |
|
|
677 | w => $w, |
|
|
678 | h => $h, |
|
|
679 | plt => \@plt, |
|
|
680 | tbl => $tbl, |
|
|
681 | } |
|
|
682 | } |
|
|
683 | |
|
|
684 | sub process_res { |
|
|
685 | my ($dir, $file, $type) = @_; |
|
|
686 | |
|
|
687 | 0 <= aio_load "$dir/$file", my $data |
|
|
688 | or die "$dir/$file: $!"; |
|
|
689 | |
|
|
690 | my $meta = load_cached "$dir/meta", sub { JSON::XS->new->utf8->relaxed->decode (shift) }; |
|
|
691 | |
|
|
692 | utf8::decode $dir; |
|
|
693 | utf8::decode $file; |
|
|
694 | |
|
|
695 | # a meta file for resources is now mandatory |
|
|
696 | unless (exists $meta->{$file}) { |
|
|
697 | warn "skipping $dir/$file\n" if $VERBOSE >= 3; |
|
|
698 | return; |
|
|
699 | } |
|
|
700 | |
|
|
701 | $meta = { |
|
|
702 | %{ $meta->{"" } || {} }, |
|
|
703 | %{ $meta->{$file} || {} }, |
|
|
704 | }; |
|
|
705 | |
|
|
706 | if ($meta->{license} =~ s/^#//) { |
|
|
707 | $meta->{license} = ({ |
|
|
708 | "pd" => "Public Domain", |
|
|
709 | "gpl" => "GNU General Public License, version 3.0 or any later", |
|
|
710 | "cc/by/2.0" => "Licensed under Creative Commons Attribution 2.0 http://creativecommons.org/licenses/by/2.0/", |
|
|
711 | "cc/by/2.1" => "Licensed under Creative Commons Attribution 2.1 http://creativecommons.org/licenses/by/2.1/", |
|
|
712 | "cc/by/2.5" => "Licensed under Creative Commons Attribution 2.5 http://creativecommons.org/licenses/by/2.5/", |
|
|
713 | "cc/by/3.0" => "Licensed under Creative Commons Attribution 3.0 http://creativecommons.org/licenses/by/3.0/", |
|
|
714 | })->{$meta->{license}} |
|
|
715 | || warn "$dir/$file: license tag '$meta->{license}' not found."; |
|
|
716 | } |
|
|
717 | |
|
|
718 | if (!exists $meta->{author} && $meta->{source} =~ m%^http://www.jamendo.com/en/artist/(.*)$%) { |
|
|
719 | ($meta->{author} = $1) =~ s/_/ /g; |
|
|
720 | } |
|
|
721 | |
|
|
722 | $file =~ s/\.res$//; |
|
|
723 | $file =~ s/\.(ogg|wav|jpg|png)$//; |
|
|
724 | |
|
|
725 | if ($file =~ s/\.plt$//) { |
|
|
726 | $data = process_plt "$dir/$file", $data; |
|
|
727 | } elsif (my $filter = $meta->{cfutil_filter}) { |
|
|
728 | if ($filter eq "yaml2json") { |
|
|
729 | $data = JSON::XS::encode_json YAML::XS::Load $data; |
|
|
730 | } elsif ($filter eq "json2json") { |
|
|
731 | $data = JSON::XS::encode_json JSON::XS->relaxed->utf8->decode ($data); |
|
|
732 | } elsif ($filter eq "perl2json") { |
|
|
733 | $data = eval $data; die if $@; |
|
|
734 | $data = JSON::XS::encode_json $data; |
|
|
735 | } else { |
|
|
736 | warn "$dir/$file: unknown filter $filter, skipping\n"; |
|
|
737 | } |
|
|
738 | } |
|
|
739 | |
|
|
740 | substr $dir, 0, 1 + length $PATH, ""; |
|
|
741 | |
|
|
742 | $RESOURCE{"$dir/$file"} = { |
|
|
743 | type => (exists $meta->{type} ? delete $meta->{type} : $type), |
|
|
744 | data => $data, |
|
|
745 | %$meta ? (meta => $meta) : (), |
|
|
746 | }; |
|
|
747 | } |
|
|
748 | |
|
|
749 | sub process_any { |
|
|
750 | while (my ($func, @args) = @{ $c_any->get }) { |
|
|
751 | $func->(@args); |
285 | } |
752 | } |
286 | } |
753 | } |
287 | |
754 | |
288 | sub find_files; |
755 | sub find_files; |
289 | sub find_files { |
756 | sub find_files { |
290 | my ($path) = @_; |
757 | my ($path) = @_; |
291 | |
758 | |
|
|
759 | my $grp = IO::AIO::aio_group; |
|
|
760 | |
|
|
761 | my $scandir; $scandir = sub { |
|
|
762 | my ($path) = @_; |
|
|
763 | |
292 | IO::AIO::aioreq_pri 4; |
764 | IO::AIO::aioreq_pri 4; |
293 | IO::AIO::aio_scandir $path, 4, sub { |
765 | add $grp IO::AIO::aio_scandir $path, 4, sub { |
294 | my ($dirs, $nondirs) = @_; |
766 | my ($dirs, $nondirs) = @_; |
295 | |
767 | |
296 | find_files "$path/$_" |
768 | $scandir->("$path/$_") |
297 | for grep $_ !~ /^(?:CVS|dev)$/, @$dirs; |
769 | for grep $_ !~ /^(?:CVS|dev|\..*)$/, @$dirs; |
298 | |
770 | |
|
|
771 | my $dir = $path; |
|
|
772 | substr $dir, 0, 1 + length $PATH, ""; |
|
|
773 | |
299 | for my $file (@$nondirs) { |
774 | for my $file (@$nondirs) { |
|
|
775 | if ($dir =~ /^music(?:\/|$)/) { |
|
|
776 | $c_any->put ([\&process_res, $path, $file, 3]) # FT_MUSIC |
|
|
777 | if $file =~ /\.(ogg)$/; |
|
|
778 | |
|
|
779 | } elsif ($dir =~ /^sound(?:\/|$)/) { |
|
|
780 | $c_any->put ([\&process_res, $path, $file, 5]) # FT_SOUND |
|
|
781 | if $file =~ /\.(wav|ogg)$/; |
|
|
782 | |
|
|
783 | } elsif ($dir =~ /^res(?:\/|$)/) { |
|
|
784 | if ($file =~ /\.(jpg|png)$/) { |
|
|
785 | $c_any->put ([\&process_res, $path, $file, 0]) # FT_FACE |
|
|
786 | } elsif ($file =~ /\.(res)$/) { |
|
|
787 | $c_any->put ([\&process_res, $path, $file, 6]) # FT_RSRC |
|
|
788 | } else { |
|
|
789 | $c_any->put ([\&process_res, $path, $file, undef]); |
|
|
790 | } |
|
|
791 | |
300 | if ($file =~ /\.png$/) { |
792 | } elsif ($file =~ /\.png$/) { |
301 | push @png, "$path/$file"; |
793 | push @c_png, ["$path/$file", 0]; |
|
|
794 | |
|
|
795 | } elsif ($file =~ /\.faceinfo$/) { |
|
|
796 | $c_any->put ([\&process_faceinfo, $path, $file]); |
|
|
797 | |
302 | } elsif ($file =~ /\.trs$/) { |
798 | } elsif ($file =~ /\.trs$/) { |
303 | push @trs, [$path, $file]; |
799 | $c_any->put ([\&process_trs, $path, $file]); |
|
|
800 | |
304 | } elsif ($file =~ /\.arc$/) { |
801 | } elsif ($file =~ /\.arc$/) { |
305 | push @arc, [$path, $file]; |
802 | $c_arc->put ([$path, $file]); |
|
|
803 | |
306 | } else { |
804 | } else { |
307 | warn "ignoring $path/$file\n" if $VERBOSE >= 2; |
805 | warn "ignoring $path/$file\n" if $VERBOSE >= 3; |
308 | } |
806 | } |
|
|
807 | } |
309 | } |
808 | }; |
310 | }; |
809 | }; |
|
|
810 | |
|
|
811 | $scandir->($path); |
|
|
812 | aio_wait $grp; |
|
|
813 | } |
|
|
814 | |
|
|
815 | sub generate_plurals { |
|
|
816 | # use Lingua::EN::Inflect (); |
|
|
817 | # Lingua::EN::Inflect::classical; |
|
|
818 | # Lingua::EN::Inflect::def_noun '(.*)staff' => '$1staves'; # policy |
|
|
819 | # Lingua::EN::Inflect::def_noun '(.*)boots' => '$1boots'; # hack |
|
|
820 | # |
|
|
821 | # for my $a (@ARC) { |
|
|
822 | # my $name = $a->{name} || $a->{_name}; |
|
|
823 | # |
|
|
824 | # next unless $a->{name_pl}; |
|
|
825 | # next if $a->{invisible}; |
|
|
826 | # next if $a->{is_floor}; |
|
|
827 | # next if $a->{no_pick}; |
|
|
828 | # |
|
|
829 | # my $test = Lingua::EN::Inflect::PL_N_eq $name, Lingua::EN::Inflect::PL $name; |
|
|
830 | # my $pl = $test =~ /^(?:eq|p:.)$/ |
|
|
831 | # ? $name |
|
|
832 | # : Lingua::EN::Inflect::PL $name; |
|
|
833 | # |
|
|
834 | # if ($pl ne $a->{name_pl}) { |
|
|
835 | # warn "$a->{_name}: plural differs, $pl vs $a->{name_pl}\n"; |
|
|
836 | # } |
|
|
837 | # } |
311 | } |
838 | } |
312 | |
839 | |
313 | sub inst_arch($) { |
840 | sub inst_arch($) { |
314 | my (undef, $path) = @_; |
841 | my (undef, $path) = @_; |
315 | |
842 | |
|
|
843 | $PATH = $path; |
|
|
844 | |
|
|
845 | print "\n", |
316 | print "installing '$path' to '$DATADIR'\n"; |
846 | "Installing '$path' to '$DATADIR'\n", |
|
|
847 | "\n", |
|
|
848 | "This can take a long time if you run this\n", |
|
|
849 | "for the first time or do not use --cache.\n", |
|
|
850 | "\n", |
|
|
851 | "Unless you run verbosely, all following warning\n", |
|
|
852 | "or error messages indicate serious problems.\n", |
|
|
853 | "\n"; |
317 | |
854 | |
318 | if (!-d "$path/treasures") { |
855 | if (!-d "$path/treasures") { |
319 | warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; |
856 | warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; |
320 | exit 1 unless $FORCE; |
857 | exit 1 unless $FORCE; |
321 | } |
858 | } |
322 | |
859 | |
|
|
860 | print "start file scan, arc, any processing...\n" if $VERBOSE; |
|
|
861 | |
|
|
862 | my @a_arc = map +(async \&process_arc), 1..2; |
|
|
863 | my @a_any = map +(async \&process_any), 1..4; |
|
|
864 | |
323 | find_files $path; |
865 | find_files $path; |
324 | IO::AIO::flush; |
|
|
325 | |
866 | |
|
|
867 | $c_arc->shutdown; |
|
|
868 | $c_any->shutdown; |
|
|
869 | |
|
|
870 | $_->join for @a_arc; # need to parse all archetypes before png processing |
|
|
871 | |
|
|
872 | print "end arc, start png processing...\n" if $VERBOSE; |
|
|
873 | |
|
|
874 | # eight png crunchers work fine for my 4x smp machine |
|
|
875 | my @a_png = map +(async \&process_png), 1..8; |
|
|
876 | |
|
|
877 | print "end any processing...\n" if $VERBOSE; |
326 | $_->join for ( |
878 | $_->join for @a_any; |
327 | (async \&process_png), (async \&process_png), |
|
|
328 | (async \&process_trs), (async \&process_trs), |
|
|
329 | (async \&process_arc), (async \&process_arc), |
|
|
330 | ); |
|
|
331 | |
879 | |
|
|
880 | print "end png processing...\n" if $VERBOSE; |
|
|
881 | $_->join for @a_png; |
|
|
882 | |
|
|
883 | print "scanning done, processing results...\n" if $VERBOSE; |
332 | { |
884 | { |
333 | open my $fh, ">:utf8", "$DATADIR/animations~" |
885 | # remove path prefix from editor_folder |
334 | or die "$DATADIR/animations~: $!"; |
886 | $_->{editor_folder} =~ /^\x00/ |
335 | print $fh join "", map $ANIM{$_}, sort keys %ANIM |
887 | and substr $_->{editor_folder}, 0, 2 + length $path, "" |
|
|
888 | for values %ARC; |
|
|
889 | |
|
|
890 | print "resolving inheritance tree...\n" if $VERBOSE; |
|
|
891 | # resolve inherit |
|
|
892 | while () { |
|
|
893 | my $progress; |
|
|
894 | my $loop; |
|
|
895 | |
|
|
896 | for my $o (values %ARC) { |
|
|
897 | for my $other (split /,/, $o->{inherit}) { |
|
|
898 | if (my $s = $ARC{$other}) { |
|
|
899 | if ($s->{inherit}) { |
|
|
900 | $loop = $s; |
|
|
901 | } else { |
|
|
902 | delete $o->{inherit}; |
|
|
903 | my %s = %$s; |
|
|
904 | delete @s{qw(_name more name name_pl)}; |
|
|
905 | %$o = ( %s, %$o ); |
|
|
906 | ++$progress; |
|
|
907 | } |
|
|
908 | } else { |
|
|
909 | warn "WARNING: archetype '$o->{_name}' tries to inherit from undefined archetype '$other', skipping.\n"; |
|
|
910 | delete $ARC{$o->{_name}}; |
|
|
911 | } |
|
|
912 | } |
|
|
913 | } |
|
|
914 | |
|
|
915 | unless ($progress) { |
|
|
916 | die "inheritance loop detected starting at archetype '$loop->{_name}', aborting.\n" |
|
|
917 | if $loop; |
|
|
918 | |
|
|
919 | last; |
|
|
920 | } |
336 | } |
921 | } |
337 | |
922 | |
338 | { |
923 | # remove base classes (by naming scheme, should use something like "baseclass xxx" to inherit |
|
|
924 | @ARC = grep $_->{_name} !~ /^(?:type|class)_/, @ARC; |
|
|
925 | |
|
|
926 | # fix up archetypes without names, where the archanme doesn't work at all |
|
|
927 | for (@ARC) { |
|
|
928 | if (!exists $_->{name} and $_->{_name} =~ /_/) { |
|
|
929 | for ($_->{name} = $_->{_name}) { |
|
|
930 | s/(?:_\d+)+$//; |
|
|
931 | s/_[nesw]+$//; |
|
|
932 | y/_/ /; |
|
|
933 | } |
|
|
934 | } |
|
|
935 | } |
|
|
936 | |
|
|
937 | #print "generating plurals...\n" if $VERBOSE; |
|
|
938 | #generate_plurals; |
|
|
939 | |
|
|
940 | printf "writing %d archetypes...\n", scalar @ARC if $VERBOSE; |
339 | open my $fh, ">:utf8", "$DATADIR/archetypes~" |
941 | open my $fh, ">:utf8", "$DATADIR/archetypes~" |
340 | or die "$DATADIR/archetypes~: $!"; |
942 | or die "$DATADIR/archetypes~: $!"; |
341 | substr $_->{editor_folder}, 0, 1 + length $path, "" for @ARC; |
943 | print $fh Deliantra::archlist_to_string [sort { $a->{_name} cmp $b->{_name} } @ARC]; |
342 | print $fh Crossfire::archlist_to_string \@ARC; |
|
|
343 | } |
944 | } |
344 | |
945 | |
345 | { |
946 | { |
|
|
947 | printf "writing treasures (%d octets)...\n", length $TRS if $VERBOSE; |
346 | open my $fh, ">:utf8", "$DATADIR/treasures~" |
948 | open my $fh, ">:utf8", "$DATADIR/treasures~" |
347 | or die "$DATADIR/treasures~: $!"; |
949 | or die "$DATADIR/treasures~: $!"; |
348 | print $fh $TRS; |
950 | print $fh $TRS; |
349 | } |
951 | } |
350 | |
952 | |
351 | { |
953 | { |
|
|
954 | print "processing facedata...\n" if $VERBOSE; |
352 | while (my ($k, $v) = each %FACEINFO) { |
955 | 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"; |
956 | length $v->{data32} or warn "WARNING: face '$k' has no png32. this will not work (shoddy gcfclient will crash of course).\n"; |
|
|
957 | length $v->{data64} or warn "WARNING: face '$k' has no png64. this will not work very well.\n"; |
358 | |
958 | |
359 | $v->{chksum32} = Digest::MD5::md5 $v->{data32}; |
959 | make_hash $k, $v->{data32}, $v->{hash32}; |
|
|
960 | make_hash $k, $v->{data64}, $v->{hash64}; |
|
|
961 | |
|
|
962 | #length $v->{data32} <= 10000 or warn "WARNING: face '$k' has face32 larger than 10000 bytes, will not work with crossfire client.\n"; |
|
|
963 | #length $v->{data64} <= 10000 or warn "WARNING: face '$k' has face64 larger than 10000 bytes.\n"; |
|
|
964 | |
|
|
965 | $v->{glyph} // warn "WARNING: face '$k' has glyph."; |
|
|
966 | $v->{visibility} // warn "WARNING: face '$k' has no visibility info, missing faceinfo entry?\n"; |
|
|
967 | $v->{magicmap} // warn "WARNING: face '$k' has foreground colour."; |
|
|
968 | |
|
|
969 | delete @$v{qw(arc stem)}; # not used by the server |
|
|
970 | } |
|
|
971 | |
|
|
972 | print "processing resources...\n" if $VERBOSE; |
|
|
973 | my $enc = JSON::XS->new->utf8->canonical->relaxed; |
|
|
974 | while (my ($k, $v) = each %RESOURCE) { |
|
|
975 | |
|
|
976 | if ($v->{meta} && $v->{meta}{datadir}) { |
|
|
977 | delete $RESOURCE{$k}; |
|
|
978 | |
|
|
979 | $k =~ s/^res\/// or die "$k: datadir files must be in res/"; |
|
|
980 | |
|
|
981 | printf "writing $k (%d octets)...\n", length $v->{data} if $VERBOSE; |
|
|
982 | open my $fh, ">:raw", "$DATADIR/$k~" |
|
|
983 | or die "$DATADIR/$k~: $!"; |
|
|
984 | syswrite $fh, $v->{data}; |
|
|
985 | push @COMMIT, $k; |
|
|
986 | |
|
|
987 | } else { |
|
|
988 | if ($v->{type} & 1) { |
|
|
989 | # prepend meta info |
|
|
990 | |
|
|
991 | my $meta = $enc->encode ({ |
|
|
992 | name => $k, |
|
|
993 | %{ $v->{meta} || {} }, |
|
|
994 | }); |
|
|
995 | |
|
|
996 | $v->{data} = pack "(w/a*)*", $meta, $v->{data}; |
|
|
997 | } |
|
|
998 | |
|
|
999 | make_hash $k, $v->{data}, $v->{hash}, 6; # 6 for the benefit of existing clients |
360 | } |
1000 | } |
|
|
1001 | } |
361 | |
1002 | |
|
|
1003 | printf "writing facedata (%d faces, %d anims, %d resources)...\n", |
|
|
1004 | scalar keys %FACEINFO, |
|
|
1005 | scalar keys %ANIMINFO, |
|
|
1006 | scalar keys %RESOURCE |
|
|
1007 | if $VERBOSE; |
|
|
1008 | |
362 | open my $fh, ">:perlio", "$DATADIR/faces~" |
1009 | open my $fh, ">:perlio", "$DATADIR/facedata~" |
363 | or die "$DATADIR/faces~: $!"; |
1010 | or die "$DATADIR/facedata~: $!"; |
364 | |
1011 | |
365 | $FACEINFO{""} = { version => 1}; |
1012 | print $fh nfreeze { |
366 | print $fh Storable::nfreeze \%FACEINFO; |
1013 | version => 2, |
|
|
1014 | faceinfo => \%FACEINFO, |
|
|
1015 | animinfo => \%ANIMINFO, |
|
|
1016 | resource => \%RESOURCE, |
367 | } |
1017 | }; |
|
|
1018 | } |
368 | |
1019 | |
|
|
1020 | print "committing files...\n" if $VERBOSE; |
|
|
1021 | |
369 | for (qw(archetypes faces animations treasures)) { |
1022 | for (qw(archetypes facedata treasures), @COMMIT) { |
370 | chmod 0644, "$DATADIR/$_~"; |
1023 | chmod 0644, "$DATADIR/$_~"; |
371 | rename "$DATADIR/$_~", "$DATADIR/$_" |
1024 | rename "$DATADIR/$_~", "$DATADIR/$_" |
372 | or die "$DATADIR/$_: $!"; |
1025 | or die "$DATADIR/$_: $!"; |
373 | } |
1026 | } |
374 | |
1027 | |