1 | #!@PERL@ |
1 | #!@PERL@ |
|
|
2 | |
|
|
3 | # |
|
|
4 | # This file is part of Deliantra, the Roguelike Realtime MMORPG. |
|
|
5 | # |
|
|
6 | # Copyright (©) 2007,2008,2009 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 | # |
2 | |
24 | |
3 | use strict; |
25 | use strict; |
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; |
|
|
39 | use File::Temp; |
|
|
40 | use POSIX (); |
|
|
41 | use Carp; |
|
|
42 | |
16 | use Coro::Event; |
43 | use Coro::EV; |
17 | use AnyEvent; |
44 | use AnyEvent; |
|
|
45 | use YAML::XS (); |
|
|
46 | use JSON::XS (); |
18 | use IO::AIO (); |
47 | use IO::AIO (); |
19 | use File::Temp; |
48 | use Digest::MD5 (); |
20 | use Crossfire; |
49 | |
21 | use Coro; |
50 | use Coro 5.12; |
22 | use Coro::AIO; |
51 | use Coro::AIO; |
23 | use POSIX (); |
52 | use Coro::Util; |
24 | use Digest::MD5; |
53 | use Coro::Channel; |
25 | use Coro::Storable; $Storable::canonical = 1; |
54 | use Coro::Storable; $Storable::canonical = 1; |
|
|
55 | |
|
|
56 | use Deliantra; |
|
|
57 | |
|
|
58 | $SIG{QUIT} = sub { Carp::cluck "QUIT" }; |
26 | |
59 | |
27 | sub usage { |
60 | sub usage { |
28 | warn <<EOF; |
61 | warn <<EOF; |
29 | Usage: cfutil [-v] [-q] [--force] [--cache] |
62 | Usage: cfutil [-v] [-q] [--force] [--cache] |
30 | [--install-arch path] |
63 | [--install-arch path] |
… | |
… | |
41 | my $VERBOSE = 1; |
74 | my $VERBOSE = 1; |
42 | my $CACHE = 0; |
75 | my $CACHE = 0; |
43 | my $FORCE; |
76 | my $FORCE; |
44 | my $TMPDIR = "/tmp/cfutil$$~"; |
77 | my $TMPDIR = "/tmp/cfutil$$~"; |
45 | my $TMPFILE = "aaaa0"; |
78 | my $TMPFILE = "aaaa0"; |
|
|
79 | my @COMMIT; |
|
|
80 | |
|
|
81 | our %COLOR = ( |
|
|
82 | black => 0, |
|
|
83 | white => 1, |
|
|
84 | navy => 2, |
|
|
85 | red => 3, |
|
|
86 | orange => 4, |
|
|
87 | blue => 5, |
|
|
88 | darkorange => 6, |
|
|
89 | green => 7, |
|
|
90 | lightgreen => 8, |
|
|
91 | grey => 9, |
|
|
92 | brown => 10, |
|
|
93 | gold => 11, |
|
|
94 | tan => 12, |
|
|
95 | ); |
46 | |
96 | |
47 | END { system "rm", "-rf", $TMPDIR } |
97 | END { system "rm", "-rf", $TMPDIR } |
48 | |
98 | |
49 | Event->signal (signal => "INT", cb => sub { exit 1 }); |
99 | my $s_INT = EV::signal INT => sub { exit 1 }; |
50 | Event->signal (signal => "TERM", cb => sub { exit 1 }); |
100 | my $s_TERM = EV::signal TERM => sub { exit 1 }; |
|
|
101 | |
|
|
102 | our %hash; |
|
|
103 | |
|
|
104 | # here we could try to avoid collisions and reduce chksum size further |
|
|
105 | sub make_hash($\$\$;$) { |
|
|
106 | my ($id, $dataref, $hashref, $clen) = @_; |
|
|
107 | |
|
|
108 | my $hash = substr +(Digest::MD5::md5 $$dataref), 0, $clen || 4; |
|
|
109 | |
|
|
110 | if (exists $hash{$hash}) { |
|
|
111 | # hahs collinion, but some files are simply identical |
|
|
112 | if (${$hash{$hash}[1]} ne $$dataref) { |
|
|
113 | warn "hash collision $hash{$hash}[0] vs. $id\n"; |
|
|
114 | exit 1; |
|
|
115 | } else { |
|
|
116 | print "$hash{$hash}[0] and $id are identical (which is fine).\n" if $VERBOSE >= 3; |
|
|
117 | } |
|
|
118 | } |
|
|
119 | $hash{$hash} = [$id, $dataref, $hashref]; |
|
|
120 | |
|
|
121 | $$hashref = $hash; |
|
|
122 | } |
51 | |
123 | |
52 | mkdir $TMPDIR, 0700 |
124 | mkdir $TMPDIR, 0700 |
53 | or die "$TMPDIR: $!"; |
125 | or die "$TMPDIR: $!"; |
54 | |
126 | |
55 | sub fork_sub(&) { |
127 | sub fork_sub(&) { |
… | |
… | |
68 | } |
140 | } |
69 | |
141 | |
70 | sub inst_maps($) { |
142 | sub inst_maps($) { |
71 | my (undef, $path) = @_; |
143 | my (undef, $path) = @_; |
72 | |
144 | |
73 | print "installing '$path' to '$DATADIR/maps'\n"; |
145 | print "\nInstalling '$path' to '$DATADIR/maps'\n\n"; |
74 | |
146 | |
75 | if (!-f "$path/regions") { |
147 | if (!-f "$path/regions") { |
76 | warn "'$path' does not look like a maps directory ('regions' file is missing).\n"; |
148 | warn "'$path' does not look like a maps directory ('regions' file is missing).\n"; |
77 | exit 1 unless $FORCE; |
149 | exit 1 unless $FORCE; |
78 | } |
150 | } |
79 | |
151 | |
80 | system $RSYNC, "-av", "$path/.", "$DATADIR/maps/.", "--delete", "--exclude", "CVS", "--delete-excluded" |
152 | system $RSYNC, "-av", "--chmod=u=rwX,go=rX", |
|
|
153 | "$path/.", "$DATADIR/maps/.", |
|
|
154 | "--exclude", "CVS", "--exclude", "/world-precomposed", |
|
|
155 | "--delete", "--delete-excluded" |
81 | and die "map installation failed.\n"; |
156 | and die "map installation failed.\n"; |
82 | |
157 | |
83 | print "maps installed successfully.\n"; |
158 | print "maps installed successfully.\n"; |
84 | } |
159 | } |
85 | |
160 | |
86 | { |
161 | { |
87 | our %ANIMINFO; |
162 | our %ANIMINFO; |
88 | our %FACEINFO; |
163 | our %FACEINFO; |
|
|
164 | our %RESOURCE; |
|
|
165 | our @ARC; |
89 | our %ARC; |
166 | our %ARC; |
90 | our $TRS; |
167 | our $TRS; |
91 | our $NFILE; |
168 | our $NFILE; |
|
|
169 | our $PATH; |
92 | |
170 | |
93 | our $QUANTIZE = "+dither -colorspace RGB -colors 256"; |
171 | our $QUANTIZE = "+dither -colorspace RGB -colors 256"; |
94 | |
172 | |
95 | our (@png, @trs, @arc); # files we are interested in |
173 | our $c_arc = new Coro::Channel; |
|
|
174 | our $c_trs = new Coro::Channel; |
|
|
175 | our $c_res = new Coro::Channel; |
|
|
176 | |
|
|
177 | our @c_png; |
96 | |
178 | |
97 | sub commit_png($$$) { |
179 | sub commit_png($$$) { |
98 | my ($name, $data, $T) = @_; |
180 | my ($name, $data, $T) = @_; |
99 | |
181 | |
100 | $FACEINFO{$name}{"data$T"} = $data; |
182 | $FACEINFO{$name}{"data$T"} = $data; |
101 | } |
183 | } |
102 | |
184 | |
103 | sub process_png { |
185 | sub process_png { |
104 | while (@png) { |
186 | while (@c_png) { |
105 | my ($path, $delete) = @{pop @png}; |
187 | my ($path, $delete) = @{pop @c_png}; |
106 | |
188 | |
107 | my $png; |
189 | my $png; |
108 | aio_lstat $path; |
190 | aio_lstat $path; |
109 | my ($size, $mtime) = (stat _)[7,9]; |
191 | my ($size, $mtime) = (stat _)[7,9]; |
110 | |
192 | |
… | |
… | |
141 | if ($w % $T || $h % $T) { |
223 | if ($w % $T || $h % $T) { |
142 | warn "$path: weird png size ($w $h), skipping.\n"; |
224 | warn "$path: weird png size ($w $h), skipping.\n"; |
143 | next; |
225 | next; |
144 | } |
226 | } |
145 | |
227 | |
|
|
228 | (my $base = $stem) =~ s/^.*\///; |
|
|
229 | |
|
|
230 | my $fi = $FACEINFO{$base}; |
|
|
231 | unless ($fi) { |
|
|
232 | #warn "$path: <$base> not referenced by any archetype, skipping.\n"; |
|
|
233 | #next; |
|
|
234 | } |
|
|
235 | |
|
|
236 | my $arc = $fi->{arc} || { }; |
|
|
237 | |
146 | unless ($path =~ /~$/) { |
238 | unless ($path =~ /~$/) { |
147 | # possibly enlarge |
239 | # possibly enlarge |
148 | if (0 > aio_stat "$stem.64x64.png") { |
240 | if (0 > aio_stat "$stem.64x64.png") { |
149 | my $other = "$stem.64x64.png~"; |
241 | my $other = "$stem.64x64.png~"; |
150 | |
242 | |
151 | if (0 > aio_lstat $other or (-M _) > (-M $path)) { |
243 | if (0 > aio_lstat $other or (-M _) > (-M $path)) { |
152 | my $wrap = 0; # for the time being |
|
|
153 | fork_sub { |
244 | fork_sub { |
|
|
245 | my $CROP; |
|
|
246 | my $SRC = "png:\Q$path\E"; |
|
|
247 | |
|
|
248 | my $is_floor = $arc->{is_floor}; |
|
|
249 | my $is_wall = 0; |
|
|
250 | |
|
|
251 | my ($wall_pfx, $wall_dir, $wall_sfx); |
|
|
252 | |
|
|
253 | if ( |
|
|
254 | !$is_floor |
|
|
255 | && !$arc->{alive} |
|
|
256 | && $arc->{move_block} eq "all" |
|
|
257 | && $path =~ /^(.*_)([0-9A-F])(\.x11.*\.png)$/ |
|
|
258 | ) { |
|
|
259 | ($wall_pfx, $wall_dir, $wall_sfx) = ($1, hex $2, $3); |
|
|
260 | |
|
|
261 | unless (grep { !-e sprintf "%s%X%s", $wall_pfx, $_, $wall_sfx } 0..15) { |
|
|
262 | $is_wall = 1; |
|
|
263 | } |
|
|
264 | } |
|
|
265 | |
|
|
266 | if ($is_wall || $is_floor) { |
|
|
267 | # add a 4px border and add other images around it |
|
|
268 | $CROP = "-shave 8x8 +repage"; |
|
|
269 | |
|
|
270 | $w += 8; |
|
|
271 | $h += 8; |
|
|
272 | |
|
|
273 | $SRC = "-size ${w}x${h} xc:transparent"; |
|
|
274 | $SRC .= " png:\Q$path\E -geometry +4+4 -composite"; |
|
|
275 | |
|
|
276 | # 8 surrounding images |
|
|
277 | for ( |
|
|
278 | # x y b r0 r1 |
|
|
279 | [-1, -1, 0, 6], |
|
|
280 | [ 0, -1, 1, 10, 14], |
|
|
281 | [+1, -1, 0, 12], |
|
|
282 | |
|
|
283 | [-1, 0, 8, 5, 7], |
|
|
284 | # |
|
|
285 | [+1, 0, 2, 5, 13], |
|
|
286 | |
|
|
287 | [-1, +1, 0, 3], |
|
|
288 | [ 0, +1, 4, 10, 11], |
|
|
289 | [+1, +1, 0, 9], |
|
|
290 | ) { |
|
|
291 | my ($x, $y, $d, $r0, $r1) = @$_; |
|
|
292 | |
|
|
293 | my $tile = $is_floor ? $path |
|
|
294 | : $is_wall ? sprintf "%s%X%s", $wall_pfx, ($wall_dir & $d) ? $r1 : $r0, $wall_sfx |
|
|
295 | : die; |
|
|
296 | |
|
|
297 | $SRC .= sprintf " png:%s -geometry %+d%+d -composite", |
|
|
298 | "\Q$tile", |
|
|
299 | $x * ($w - 8) + 4, |
|
|
300 | $y * ($h - 8) + 4; |
|
|
301 | } |
|
|
302 | } |
|
|
303 | |
154 | system "convert png:\Q$path\E -depth 8 rgba:-" |
304 | system "convert -depth 8 $SRC rgba:-" |
155 | . "| $exec_prefix/bin/cfhq2xa $w $h $wrap" |
305 | . "| $exec_prefix/bin/cfhq2xa $w $h 0" |
156 | . "| convert -depth 8 -size ".($w * 2)."x".($h * 2)." rgba:- $QUANTIZE -quality 00 png32:\Q$other\E~" |
306 | . "| convert -depth 8 -size ".($w * 2)."x".($h * 2)." rgba:- $CROP $QUANTIZE -quality 00 png32:\Q$other\E~" |
157 | and die "convert/hq2xa pipeline error: status $? ($!)"; |
307 | and die "convert/cfhq2xa pipeline error: status $? ($!)"; |
158 | system $OPTIPNG, "-o5", "-i0", "-q", "$other~"; |
308 | system $OPTIPNG, "-i0", "-q", "$other~"; |
159 | die "$other~ has zero size, aborting." unless -s "$other~"; |
309 | die "$other~ has zero size, aborting." unless -s "$other~"; |
160 | rename "$other~", $other; |
310 | rename "$other~", $other; |
161 | }; |
311 | }; |
162 | } |
312 | } |
163 | |
313 | |
164 | push @png, [$other, !$CACHE]; |
314 | push @c_png, [$other, !$CACHE]; |
165 | } |
315 | } |
166 | |
316 | |
167 | # possibly scale down |
317 | # possibly scale down |
168 | if (0 > aio_stat "$stem.32x32.png") { |
318 | if (0 > aio_stat "$stem.32x32.png") { |
169 | my $other = "$stem.32x32.png~"; |
319 | my $other = "$stem.32x32.png~"; |
170 | |
320 | |
171 | if (0 > aio_lstat $other or (-M _) > (-M $path)) { |
321 | if (0 > aio_lstat $other or (-M _) > (-M $path)) { |
172 | fork_sub { |
322 | fork_sub { |
173 | system "convert png:\Q$path\E -geometry 50% -filter lanczos $QUANTIZE -quality 00 png32:\Q$other\E~"; |
323 | system "convert png:\Q$path\E -geometry 50% -filter lanczos $QUANTIZE -quality 00 png32:\Q$other\E~"; |
174 | system $OPTIPNG, "-o5", "-i0", "-q", "$other~"; |
324 | system $OPTIPNG, "-i0", "-q", "$other~"; |
|
|
325 | |
|
|
326 | # reduce smoothfaces >10000 bytes |
|
|
327 | # obsolete, no longer required |
|
|
328 | if (0 && $stem =~ /_S\./ && (-s "$other~") > 10000) { |
|
|
329 | my $ncolor = 256; |
|
|
330 | while () { |
|
|
331 | system "<\Q$other~\E $PNGNQ -s1 -n$ncolor >\Q$other~~\E"; |
|
|
332 | system $OPTIPNG, "-i0", "-q", "$other~~"; |
|
|
333 | last if 10000 > -s "$other~~"; |
|
|
334 | $ncolor = int $ncolor * 0.9; |
|
|
335 | $ncolor > 8 or die "cannot reduce filesize to < 10000 bytes"; |
|
|
336 | } |
|
|
337 | |
|
|
338 | printf "reduced %s from %d to %d bytes using %d colours.\n", |
|
|
339 | $other, -s "$other~", -s "$other~~", $ncolor |
|
|
340 | if $VERBOSE >= 2; |
|
|
341 | rename "$other~~", "$other~"; |
|
|
342 | } |
|
|
343 | |
175 | die "$other~ has zero size, aborting." unless -s "$other~"; |
344 | die "$other~ has zero size, aborting." unless -s "$other~"; |
176 | rename "$other~", $other; |
345 | rename "$other~", $other; |
177 | }; |
346 | }; |
178 | } |
347 | } |
179 | |
348 | |
180 | #warn "scaled down $path to $other\n";#d# |
349 | #warn "scaled down $path to $other\n";#d# |
181 | push @png, [$other, !$CACHE]; |
350 | push @c_png, [$other, !$CACHE]; |
182 | } |
351 | } |
183 | } |
352 | } |
184 | |
353 | |
185 | (my $face = $stem) =~ s/^.*\///; |
354 | (my $face = $stem) =~ s/^.*\///; |
186 | |
355 | |
… | |
… | |
247 | aio_unlink $path if $delete; |
416 | aio_unlink $path if $delete; |
248 | } |
417 | } |
249 | } |
418 | } |
250 | |
419 | |
251 | sub process_arc { |
420 | sub process_arc { |
252 | while (@arc) { |
421 | while (my $job = $c_arc->get) { |
253 | my ($dir, $file) = @{pop @arc}; |
422 | my ($dir, $file) = @$job; |
254 | |
423 | |
255 | my $arc; |
424 | my $arc; |
256 | aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/ |
425 | aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/ |
257 | |
426 | |
258 | my $arc = read_arch "$dir/$file"; |
427 | my $arc = read_arch "$dir/$file"; |
259 | for my $o (values %$arc) { |
428 | for my $o (values %$arc) { |
|
|
429 | push @ARC, $o; |
|
|
430 | for (my $m = $o; $m; $m = $m->{more}) { |
260 | $ARC{$o->{_name}} = $o; |
431 | $ARC{$m->{_name}} = $m; |
|
|
432 | } |
261 | |
433 | |
262 | $o->{editor_folder} = $dir; |
434 | $o->{editor_folder} ||= "\x00$dir"; # horrible kludge |
263 | |
435 | |
264 | my $visibility = delete $o->{visibility}; |
436 | my $visibility = delete $o->{visibility}; |
265 | my $magicmap = delete $o->{magicmap}; |
437 | my $magicmap = delete $o->{magicmap}; |
266 | |
438 | |
267 | # find upper left corner :/ |
439 | # find upper left corner :/ |
… | |
… | |
278 | |
450 | |
279 | my $ext = $x|$y ? "+$x+$y" : ""; |
451 | my $ext = $x|$y ? "+$x+$y" : ""; |
280 | |
452 | |
281 | $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/ || !$o->{face}; |
453 | $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/ || !$o->{face}; |
282 | |
454 | |
283 | my $visibility = delete $o->{visibility} if exists $o->{visibility}; |
455 | $visibility = delete $o->{visibility} if exists $o->{visibility}; |
284 | my $magicmap = delete $o->{magicmap} if exists $o->{magicmap}; |
456 | $magicmap = delete $o->{magicmap} if exists $o->{magicmap}; |
285 | |
457 | |
286 | my $anim = delete $o->{anim}; |
458 | my $anim = delete $o->{anim}; |
287 | |
459 | |
288 | if ($anim) { |
460 | if ($anim) { |
289 | # possibly add $ext to the animation name to avoid |
461 | # possibly add $ext to the animation name to avoid |
… | |
… | |
307 | facings => $facings, |
479 | facings => $facings, |
308 | frames => \@frames, |
480 | frames => \@frames, |
309 | }; |
481 | }; |
310 | } |
482 | } |
311 | |
483 | |
312 | for my $face ($o->{face} || (), @{$anim || []}) { |
484 | for ($o->{face} || (), @{$anim || []}) { |
313 | next if $face =~ /^facings\s|^blank.x11$|^empty.x11$/; |
485 | next if /^facings\s/; |
314 | |
486 | |
|
|
487 | my $face = $_; |
|
|
488 | $face =~ s/\+\d+\+\d+$//; # remove tile offset coordinates |
|
|
489 | |
315 | my $info = $FACEINFO{$face} ||= {}; |
490 | my $info = $FACEINFO{$face} ||= { }; |
|
|
491 | $info->{arc} = $o; |
|
|
492 | |
|
|
493 | next if $face =~ /^blank.x11$|^empty.x11$/; |
316 | |
494 | |
317 | $info->{visibility} = $visibility if defined $visibility; |
495 | $info->{visibility} = $visibility if defined $visibility; |
318 | $info->{magicmap} = $magicmap if defined $magicmap; |
496 | $info->{magicmap} = $magicmap if defined $magicmap; |
319 | } |
497 | } |
320 | |
498 | |
321 | if (my $smooth = delete $o->{smoothface}) { |
499 | if (my $smooth = delete $o->{smoothface}) { |
322 | my %kv =split /\s+/, $smooth; |
500 | my %kv = split /\s+/, $smooth; |
323 | my $level = $o->{smoothlevel}; #TODO: delete from $o if !gcfclient-support |
501 | my $level = $o->{smoothlevel}; #TODO: delete from $o if !gcfclient-support |
324 | while (my ($face, $smooth) = each %kv) { |
502 | while (my ($face, $smooth) = each %kv) { |
|
|
503 | $FACEINFO{$smooth}{arc} = $o; |
|
|
504 | |
325 | $FACEINFO{$face}{smooth} = $smooth; |
505 | $FACEINFO{$face}{smooth} = $smooth; |
326 | $FACEINFO{$face}{smoothlevel} = $level; |
506 | $FACEINFO{$face}{smoothlevel} = $level; |
327 | } |
507 | } |
328 | } |
508 | } |
329 | } |
509 | } |
330 | } |
510 | } |
331 | } |
511 | } |
332 | } |
512 | } |
333 | |
513 | |
334 | sub process_trs { |
514 | sub process_trs { |
335 | while (@trs) { |
515 | while (my $job = $c_trs->get) { |
336 | my ($dir, $file) = @{pop @trs}; |
516 | my ($dir, $file) = @$job; |
337 | my $path = "$dir/$file"; |
517 | my $path = "$dir/$file"; |
338 | |
518 | |
339 | my $trs; |
519 | my $trs; |
340 | if (0 > aio_load $path, $trs) { |
520 | if (0 > aio_load $path, $trs) { |
341 | warn "$path: $!, skipping.\n"; |
521 | warn "$path: $!, skipping.\n"; |
… | |
… | |
344 | |
524 | |
345 | $TRS .= $trs; |
525 | $TRS .= $trs; |
346 | } |
526 | } |
347 | } |
527 | } |
348 | |
528 | |
|
|
529 | my %FILECACHE; |
|
|
530 | |
|
|
531 | sub load_cached($;$) { |
|
|
532 | unless (exists $FILECACHE{$_[0]}) { |
|
|
533 | my $data; |
|
|
534 | if (0 < aio_load $_[0], $data) { |
|
|
535 | if ($_[1]) { |
|
|
536 | $data = eval { $_[1]->($data) }; |
|
|
537 | warn "$_[0]: $@" if $@; |
|
|
538 | } |
|
|
539 | } |
|
|
540 | |
|
|
541 | $FILECACHE{$_[0]} = $data; |
|
|
542 | } |
|
|
543 | |
|
|
544 | $FILECACHE{$_[0]} |
|
|
545 | } |
|
|
546 | |
|
|
547 | sub process_res { |
|
|
548 | while (my $job = $c_res->get) { |
|
|
549 | my ($dir, $file, $type) = @$job; |
|
|
550 | |
|
|
551 | my $data; |
|
|
552 | aio_load "$dir/$file", $data; |
|
|
553 | |
|
|
554 | my $meta = load_cached "$dir/meta", sub { JSON::XS->new->utf8->relaxed->decode (shift) }; |
|
|
555 | |
|
|
556 | utf8::decode $dir; |
|
|
557 | utf8::decode $file; |
|
|
558 | |
|
|
559 | # a meta file for resources is now mandatory |
|
|
560 | unless (exists $meta->{$file}) { |
|
|
561 | warn "skipping $dir/$file\n" if $VERBOSE >= 3; |
|
|
562 | next; |
|
|
563 | } |
|
|
564 | |
|
|
565 | $meta = { |
|
|
566 | %{ $meta->{"" } || {} }, |
|
|
567 | %{ $meta->{$file} || {} }, |
|
|
568 | }; |
|
|
569 | |
|
|
570 | if ($meta->{license} =~ s/^#//) { |
|
|
571 | $meta->{license} = ({ |
|
|
572 | "pd" => "Public Domain", |
|
|
573 | "gpl" => "GNU General Public License, version 3.0 or any later", |
|
|
574 | "cc/by/2.0" => "Licensed under Creative Commons Attribution 2.0 http://creativecommons.org/licenses/by/2.0/", |
|
|
575 | "cc/by/2.5" => "Licensed under Creative Commons Attribution 2.5 http://creativecommons.org/licenses/by/2.5/", |
|
|
576 | "cc/by/3.0" => "Licensed under Creative Commons Attribution 3.0 http://creativecommons.org/licenses/by/3.0/", |
|
|
577 | })->{$meta->{license}} |
|
|
578 | || warn "$dir/$file: license tag '$meta->{license}' not found."; |
|
|
579 | } |
|
|
580 | |
|
|
581 | $file =~ s/\.res$//; |
|
|
582 | $file =~ s/\.(ogg|wav|jpg|png)$//; |
|
|
583 | |
|
|
584 | substr $dir, 0, 1 + length $PATH, ""; |
|
|
585 | |
|
|
586 | if (my $filter = $meta->{cfutil_filter}) { |
|
|
587 | if ($filter eq "yaml2json") { |
|
|
588 | $data = JSON::XS::encode_json YAML::XS::Load $data; |
|
|
589 | } elsif ($filter eq "json2json") { |
|
|
590 | $data = JSON::XS::encode_json JSON::XS->relaxed->utf8->decode ($data); |
|
|
591 | } elsif ($filter eq "perl2json") { |
|
|
592 | $data = eval $data; die if $@; |
|
|
593 | $data = JSON::XS::encode_json $data; |
|
|
594 | } else { |
|
|
595 | warn "$dir/$file: unknown filter $filter, skipping\n"; |
|
|
596 | } |
|
|
597 | } |
|
|
598 | |
|
|
599 | $RESOURCE{"$dir/$file"} = { |
|
|
600 | type => (exists $meta->{type} ? delete $meta->{type} : $type), |
|
|
601 | data => $data, |
|
|
602 | %$meta ? (meta => $meta) : (), |
|
|
603 | }; |
|
|
604 | } |
|
|
605 | } |
|
|
606 | |
349 | sub find_files; |
607 | sub find_files; |
350 | sub find_files { |
608 | sub find_files { |
351 | my ($path) = @_; |
609 | my ($path) = @_; |
352 | |
610 | |
353 | IO::AIO::aioreq_pri 4; |
611 | IO::AIO::aioreq_pri 4; |
354 | IO::AIO::aio_scandir $path, 4, sub { |
612 | IO::AIO::aio_scandir $path, 4, sub { |
355 | my ($dirs, $nondirs) = @_; |
613 | my ($dirs, $nondirs) = @_; |
356 | |
614 | |
357 | find_files "$path/$_" |
615 | find_files "$path/$_" |
358 | for grep $_ !~ /^(?:CVS|dev)$/, @$dirs; |
616 | for grep $_ !~ /^(?:CVS|dev|\..*)$/, @$dirs; |
|
|
617 | |
|
|
618 | my $dir = $path; |
|
|
619 | substr $dir, 0, 1 + length $PATH, ""; |
359 | |
620 | |
360 | for my $file (@$nondirs) { |
621 | for my $file (@$nondirs) { |
|
|
622 | if ($dir =~ /^music(?:\/|$)/) { |
|
|
623 | $c_res->put ([$path, $file, 3]) # FT_MUSIC |
|
|
624 | if $file =~ /\.(ogg)$/; |
|
|
625 | |
|
|
626 | } elsif ($dir =~ /^sound(?:\/|$)/) { |
|
|
627 | $c_res->put ([$path, $file, 5]) # FT_SOUND |
|
|
628 | if $file =~ /\.(wav|ogg)$/; |
|
|
629 | |
|
|
630 | } elsif ($dir =~ /^res(?:\/|$)/) { |
|
|
631 | if ($file =~ /\.(jpg|png)$/) { |
|
|
632 | $c_res->put ([$path, $file, 0]) # FT_FACE |
|
|
633 | } elsif ($file =~ /\.(res)$/) { |
|
|
634 | $c_res->put ([$path, $file, 6]) # FT_RSRC |
|
|
635 | } else { |
|
|
636 | $c_res->put ([$path, $file, undef]); |
|
|
637 | } |
|
|
638 | |
361 | if ($file =~ /\.png$/) { |
639 | } elsif ($file =~ /\.png$/) { |
362 | push @png, ["$path/$file", 0]; |
640 | push @c_png, ["$path/$file", 0]; |
|
|
641 | |
363 | } elsif ($file =~ /\.trs$/) { |
642 | } elsif ($file =~ /\.trs$/) { |
364 | push @trs, [$path, $file]; |
643 | $c_trs->put ([$path, $file]); |
|
|
644 | |
365 | } elsif ($file =~ /\.arc$/) { |
645 | } elsif ($file =~ /\.arc$/) { |
366 | push @arc, [$path, $file]; |
646 | $c_arc->put ([$path, $file]); |
|
|
647 | |
367 | } else { |
648 | } else { |
368 | warn "ignoring $path/$file\n" if $VERBOSE >= 2; |
649 | warn "ignoring $path/$file\n" if $VERBOSE >= 3; |
369 | } |
650 | } |
370 | } |
651 | } |
371 | }; |
652 | }; |
|
|
653 | } |
|
|
654 | |
|
|
655 | sub generate_plurals { |
|
|
656 | # use Lingua::EN::Inflect (); |
|
|
657 | # Lingua::EN::Inflect::classical; |
|
|
658 | # Lingua::EN::Inflect::def_noun '(.*)staff' => '$1staves'; # policy |
|
|
659 | # Lingua::EN::Inflect::def_noun '(.*)boots' => '$1boots'; # hack |
|
|
660 | # |
|
|
661 | # for my $a (@ARC) { |
|
|
662 | # my $name = $a->{name} || $a->{_name}; |
|
|
663 | # |
|
|
664 | # next unless $a->{name_pl}; |
|
|
665 | # next if $a->{invisible}; |
|
|
666 | # next if $a->{is_floor}; |
|
|
667 | # next if $a->{no_pick}; |
|
|
668 | # |
|
|
669 | # my $test = Lingua::EN::Inflect::PL_N_eq $name, Lingua::EN::Inflect::PL $name; |
|
|
670 | # my $pl = $test =~ /^(?:eq|p:.)$/ |
|
|
671 | # ? $name |
|
|
672 | # : Lingua::EN::Inflect::PL $name; |
|
|
673 | # |
|
|
674 | # if ($pl ne $a->{name_pl}) { |
|
|
675 | # warn "$a->{_name}: plural differs, $pl vs $a->{name_pl}\n"; |
|
|
676 | # } |
|
|
677 | # } |
372 | } |
678 | } |
373 | |
679 | |
374 | sub inst_arch($) { |
680 | sub inst_arch($) { |
375 | my (undef, $path) = @_; |
681 | my (undef, $path) = @_; |
376 | |
682 | |
|
|
683 | $PATH = $path; |
|
|
684 | |
|
|
685 | print "\n", |
377 | print "Installing '$path' to '$DATADIR'\n", |
686 | "Installing '$path' to '$DATADIR'\n", |
378 | "\n", |
687 | "\n", |
379 | "This can take a long time if you run this\n", |
688 | "This can take a long time if you run this\n", |
380 | "for the first time or do not use --cache.\n", |
689 | "for the first time or do not use --cache.\n", |
381 | "\n", |
690 | "\n", |
382 | "Unless you run verbosely, all following warning\n", |
691 | "Unless you run verbosely, all following warning\n", |
… | |
… | |
386 | if (!-d "$path/treasures") { |
695 | if (!-d "$path/treasures") { |
387 | warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; |
696 | warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; |
388 | exit 1 unless $FORCE; |
697 | exit 1 unless $FORCE; |
389 | } |
698 | } |
390 | |
699 | |
|
|
700 | print "scanning files...\n" if $VERBOSE; |
|
|
701 | |
391 | find_files $path; |
702 | find_files $path; |
|
|
703 | |
|
|
704 | my @a_arc = map +(async \&process_arc), 1..2; |
|
|
705 | my @a_res = map +(async \&process_res), 1..2; |
|
|
706 | my @a_trs = map +(async \&process_trs), 1..2; |
|
|
707 | |
392 | IO::AIO::flush; |
708 | IO::AIO::flush; |
393 | |
709 | |
394 | $_->join for ( |
710 | $c_res->shutdown; |
|
|
711 | $c_arc->shutdown; |
|
|
712 | $c_trs->shutdown; |
|
|
713 | |
|
|
714 | print "start file scan, arc, res processing...\n" if $VERBOSE; |
|
|
715 | |
|
|
716 | $_->join for @a_arc; # need to parse all archetypes before png processing |
|
|
717 | |
|
|
718 | print "end arc, start png processing...\n" if $VERBOSE; |
|
|
719 | |
395 | # four png crunchers work fine for my 2x smp machine |
720 | # eight png crunchers work fine for my 4x smp machine |
396 | (async \&process_png), (async \&process_png), (async \&process_png), (async \&process_png), |
721 | my @a_png = map +(async \&process_png), 1..8; |
397 | (async \&process_trs), (async \&process_trs), |
|
|
398 | (async \&process_arc), (async \&process_arc), |
|
|
399 | ); |
|
|
400 | |
722 | |
|
|
723 | $_->join for (@a_trs, @a_res, @a_png); |
|
|
724 | |
|
|
725 | print "scanning done, processing results...\n" if $VERBOSE; |
401 | { |
726 | { |
402 | # remove path prefix from editor_folder |
727 | # remove path prefix from editor_folder |
|
|
728 | $_->{editor_folder} =~ /^\x00/ |
403 | substr $_->{editor_folder}, 0, 1 + length $path, "" |
729 | and substr $_->{editor_folder}, 0, 2 + length $path, "" |
404 | for values %ARC; |
730 | for values %ARC; |
405 | |
731 | |
|
|
732 | print "resolving inheritance tree...\n" if $VERBOSE; |
406 | # resolve inherit |
733 | # resolve inherit |
407 | while () { |
734 | while () { |
408 | my $progress; |
735 | my $progress; |
409 | my $loop; |
736 | my $loop; |
410 | |
737 | |
411 | for my $o (values %ARC) { |
738 | for my $o (values %ARC) { |
412 | if (my $other = $o->{inherit}) { |
739 | for my $other (split /,/, $o->{inherit}) { |
413 | if (my $s = $ARC{$other}) { |
740 | if (my $s = $ARC{$other}) { |
414 | if ($s->{inherit}) { |
741 | if ($s->{inherit}) { |
415 | $loop = $s; |
742 | $loop = $s; |
416 | } else { |
743 | } else { |
417 | delete $o->{inherit}; |
744 | delete $o->{inherit}; |
|
|
745 | my %s = %$s; |
|
|
746 | delete @s{qw(_name more name name_pl)}; |
418 | %$o = ( %$s, %$o ); |
747 | %$o = ( %s, %$o ); |
419 | ++$progress; |
748 | ++$progress; |
420 | } |
749 | } |
421 | } else { |
750 | } else { |
422 | warn "'$o->{_name}' tries to inherit from undefined archetype '$other', skipping.\n"; |
751 | warn "'$o->{_name}' tries to inherit from undefined archetype '$other', skipping.\n"; |
423 | delete $ARC{$o->{_name}}; |
752 | delete $ARC{$o->{_name}}; |
… | |
… | |
431 | |
760 | |
432 | last; |
761 | last; |
433 | } |
762 | } |
434 | } |
763 | } |
435 | |
764 | |
|
|
765 | # remove base classes (by naming scheme, should use something like "baseclass xxx" to inherit |
|
|
766 | @ARC = grep $_->{_name} !~ /^(?:type|class)_/, @ARC; |
|
|
767 | |
|
|
768 | print "generating plurals...\n" if $VERBOSE; |
|
|
769 | generate_plurals; |
|
|
770 | |
|
|
771 | printf "writing %d archetypes...\n", scalar @ARC if $VERBOSE; |
436 | open my $fh, ">:utf8", "$DATADIR/archetypes~" |
772 | open my $fh, ">:utf8", "$DATADIR/archetypes~" |
437 | or die "$DATADIR/archetypes~: $!"; |
773 | or die "$DATADIR/archetypes~: $!"; |
438 | print $fh Crossfire::archlist_to_string [sort { $a->{_name} cmp $b->{_name} } values %ARC]; |
774 | print $fh Deliantra::archlist_to_string [sort { $a->{_name} cmp $b->{_name} } @ARC]; |
439 | } |
775 | } |
440 | |
776 | |
441 | { |
777 | { |
|
|
778 | printf "writing treasures (%d octets)...\n", length $TRS if $VERBOSE; |
442 | open my $fh, ">:utf8", "$DATADIR/treasures~" |
779 | open my $fh, ">:utf8", "$DATADIR/treasures~" |
443 | or die "$DATADIR/treasures~: $!"; |
780 | or die "$DATADIR/treasures~: $!"; |
444 | print $fh $TRS; |
781 | print $fh $TRS; |
445 | } |
782 | } |
446 | |
783 | |
447 | { |
784 | { |
|
|
785 | print "processing facedata...\n" if $VERBOSE; |
448 | while (my ($k, $v) = each %FACEINFO) { |
786 | while (my ($k, $v) = each %FACEINFO) { |
449 | length $v->{data32} or warn "$k: face has no png32. this will not work (shoddy gcfclient will crash of course).\n"; |
787 | length $v->{data32} or warn "$k: face has no png32. this will not work (shoddy gcfclient will crash of course).\n"; |
450 | length $v->{data64} or warn "$k: face has no png64. this will not work very well.\n"; |
788 | length $v->{data64} or warn "$k: face has no png64. this will not work very well.\n"; |
451 | |
789 | |
|
|
790 | make_hash $k, $v->{data32}, $v->{hash32}; |
|
|
791 | make_hash $k, $v->{data64}, $v->{hash64}; |
|
|
792 | |
452 | length $v->{data32} <= 10000 or warn "$k: face32 larger than 10000 bytes, will not work with crossfire client.\n"; |
793 | #length $v->{data32} <= 10000 or warn "$k: face32 larger than 10000 bytes, will not work with crossfire client.\n"; |
453 | #length $v->{data64} <= 10000 or warn "$k: face64 larger than 10000 bytes.\n"; |
794 | #length $v->{data64} <= 10000 or warn "$k: face64 larger than 10000 bytes.\n"; |
454 | |
795 | |
455 | $v->{chksum32} = Digest::MD5::md5 $v->{data32}; |
796 | if (my $magicmap = $v->{magicmap}) { |
456 | $v->{chksum64} = Digest::MD5::md5 $v->{data64}; |
797 | $magicmap =~ y/A-Z_\-/a-z/d; |
|
|
798 | $v->{magicmap} = $COLOR{$magicmap}; |
457 | } |
799 | } |
|
|
800 | |
|
|
801 | delete $v->{arc}; |
|
|
802 | } |
|
|
803 | |
|
|
804 | print "processing resources...\n" if $VERBOSE; |
|
|
805 | my $enc = JSON::XS->new->utf8->canonical->relaxed; |
|
|
806 | while (my ($k, $v) = each %RESOURCE) { |
|
|
807 | |
|
|
808 | if ($v->{meta} && $v->{meta}{datadir}) { |
|
|
809 | delete $RESOURCE{$k}; |
|
|
810 | |
|
|
811 | $k =~ s/^res\/// or die "$k: datadir files must be in res/"; |
|
|
812 | |
|
|
813 | printf "writing $k (%d octets)...\n", length $v->{data} if $VERBOSE; |
|
|
814 | open my $fh, ">:raw", "$DATADIR/$k~" |
|
|
815 | or die "$DATADIR/$k~: $!"; |
|
|
816 | syswrite $fh, $v->{data}; |
|
|
817 | push @COMMIT, $k; |
|
|
818 | |
|
|
819 | } else { |
|
|
820 | if ($v->{type} & 1) { |
|
|
821 | # prepend meta info |
|
|
822 | |
|
|
823 | my $meta = $enc->encode ({ |
|
|
824 | name => $k, |
|
|
825 | %{ $v->{meta} || {} }, |
|
|
826 | }); |
|
|
827 | |
|
|
828 | $v->{data} = pack "(w/a*)*", $meta, $v->{data}; |
|
|
829 | } |
|
|
830 | |
|
|
831 | make_hash $k, $v->{data}, $v->{hash}, 6; # 6 for the benefit of existing clients |
|
|
832 | } |
|
|
833 | } |
|
|
834 | |
|
|
835 | printf "writing facedata (%d faces, %d anims, %d resources)...\n", |
|
|
836 | scalar keys %FACEINFO, |
|
|
837 | scalar keys %ANIMINFO, |
|
|
838 | scalar keys %RESOURCE |
|
|
839 | if $VERBOSE; |
458 | |
840 | |
459 | open my $fh, ">:perlio", "$DATADIR/facedata~" |
841 | open my $fh, ">:perlio", "$DATADIR/facedata~" |
460 | or die "$DATADIR/facedata~: $!"; |
842 | or die "$DATADIR/facedata~: $!"; |
461 | |
843 | |
462 | print $fh freeze { |
844 | print $fh nfreeze { |
463 | version => 2, |
845 | version => 2, |
464 | faceinfo => \%FACEINFO, |
846 | faceinfo => \%FACEINFO, |
465 | animinfo => \%ANIMINFO, |
847 | animinfo => \%ANIMINFO, |
|
|
848 | resource => \%RESOURCE, |
466 | }; |
849 | }; |
467 | } |
|
|
468 | |
850 | |
|
|
851 | } |
|
|
852 | |
|
|
853 | print "committing files...\n" if $VERBOSE; |
|
|
854 | |
469 | for (qw(archetypes facedata treasures)) { |
855 | for (qw(archetypes facedata treasures), @COMMIT) { |
470 | chmod 0644, "$DATADIR/$_~"; |
856 | chmod 0644, "$DATADIR/$_~"; |
471 | rename "$DATADIR/$_~", "$DATADIR/$_" |
857 | rename "$DATADIR/$_~", "$DATADIR/$_" |
472 | or die "$DATADIR/$_: $!"; |
858 | or die "$DATADIR/$_: $!"; |
473 | } |
859 | } |
474 | |
860 | |