ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/utils/cfutil.in
(Generate patch)

Comparing deliantra/server/utils/cfutil.in (file contents):
Revision 1.5 by root, Thu Mar 8 15:19:08 2007 UTC vs.
Revision 1.33 by root, Thu Apr 26 00:50:03 2007 UTC

9 9
10my $CONVERT = "@CONVERT@"; 10my $CONVERT = "@CONVERT@";
11my $IDENTIFY = "@IDENTIFY@"; 11my $IDENTIFY = "@IDENTIFY@";
12my $OPTIPNG = "@OPTIPNG@"; 12my $OPTIPNG = "@OPTIPNG@";
13my $RSYNC = "@RSYNC@"; 13my $RSYNC = "@RSYNC@";
14my $PNGNQ = "@PNGNQ@";
14 15
15use Getopt::Long; 16use Getopt::Long;
16use Coro::Event; 17use Coro::Event;
17use AnyEvent; 18use AnyEvent;
18use IO::AIO (); 19use IO::AIO ();
19use File::Temp; 20use File::Temp;
20use Crossfire; 21use Crossfire;
21use Coro; 22use Coro;
22use Coro::AIO; 23use Coro::AIO;
23use POSIX (); 24use POSIX ();
25use Digest::MD5;
26use Coro::Storable; $Storable::canonical = 1;
24 27
25sub usage { 28sub usage {
26 warn <<EOF; 29 warn <<EOF;
27Usage: cfutil [-v] [-q] [--force] [--cache] 30Usage: cfutil [-v] [-q] [--force] [--cache]
28 [--install-arch path] 31 [--install-arch path]
66} 69}
67 70
68sub inst_maps($) { 71sub inst_maps($) {
69 my (undef, $path) = @_; 72 my (undef, $path) = @_;
70 73
71 print "installing '$path' to '$DATADIR/maps'\n\n"; 74 print "\nInstalling '$path' to '$DATADIR/maps'\n\n";
72 75
73 if (!-f "$path/regions") { 76 if (!-f "$path/regions") {
74 warn "'$path' does not look like a maps directory ('regions' file is missing).\n"; 77 warn "'$path' does not look like a maps directory ('regions' file is missing).\n";
75 exit 1 unless $FORCE; 78 exit 1 unless $FORCE;
76 } 79 }
77 80
78 system $RSYNC, "-av", "$path/.", "$DATADIR/maps/.", "--delete", "--exclude", "CVS", "--delete-excluded"; 81 system $RSYNC, "-av", "$path/.", "$DATADIR/maps/.", "--delete", "--exclude", "CVS", "--delete-excluded"
82 and die "map installation failed.\n";
83
84 print "maps installed successfully.\n";
79} 85}
80 86
81{ 87{
82 our @PNG; 88 our %ANIMINFO;
89 our %FACEINFO;
83 our @ARC; 90 our @ARC;
91 our %ARC;
92 our $TRS;
84 our $NFILE; 93 our $NFILE;
85 our @FACE; 94
86 our $ANIM; 95 our $QUANTIZE = "+dither -colorspace RGB -colors 256";
87 96
88 our (@png, @trs, @arc); # files we are interested in 97 our (@png, @trs, @arc); # files we are interested in
89 98
90 sub commit_png { 99 sub commit_png($$$) {
91 my ($name, $data) = @_; 100 my ($name, $data, $T) = @_;
92 101
93 push @PNG, [$name => $data]; 102 $FACEINFO{$name}{"data$T"} = $data;
94 } 103 }
95 104
96 sub process_png { 105 sub process_png {
97 while (@png) { 106 while (@png) {
98 my $path = pop @png; 107 my ($path, $delete) = @{pop @png};
99 108
100 my $png; 109 my $png;
101 aio_lstat $path; 110 aio_lstat $path;
102 my ($size, $mtime) = (stat _)[7,9]; 111 my ($size, $mtime) = (stat _)[7,9];
103 112
104 if (0 > aio_load $path, $png) { 113 if (0 > aio_load $path, $png) {
105 warn "$path: $!, skipping.\n"; 114 warn "$path: $!, skipping.\n";
106 next; 115 next;
107 } 116 }
108 117
109 # quickly extratc width and height of the (necessarily PNG) image 118 my $stem = $path;
110 unless ($png =~ /^\x89PNG\x0d\x0a\x1a\x0a....IHDR(........)/s) { 119 my $T;
111 warn "$path: not a recongized png file, skipping.\n";
112 next;
113 }
114 120
115 my ($w, $h) = unpack "NN", $1; 121 if ($stem =~ s/\.32x32\.png~?$//) {
116
117 (my $face = $path) =~ s/^.*\///;
118 my $T = 32; 122 $T = 32;
119 123 } elsif ($stem =~ s/\.64x64\.png~?$//) {
120 unless ($face =~ s/\.base\.(...)\.png$/.$1/) { 124 $T = 64;
125 } else {
121 warn "$path: weird filename, skipping.\n"; 126 warn "$path: weird filename, skipping.\n";
122 next; 127 next;
123 } 128 }
129
130 # quickly extract width and height of the (necessarily PNG) image
131 unless ($png =~ /^\x89PNG\x0d\x0a\x1a\x0a....IHDR(........)/s) {
132 warn "$path: not a recognized png file, skipping.\n";
133 next;
134 }
135
136 my ($w, $h) = unpack "NN", $1;
124 137
125 if ($w < $T || $h < $T) { 138 if ($w < $T || $h < $T) {
126 warn "$path: too small ($w $h), skipping.\n"; 139 warn "$path: too small ($w $h), skipping.\n";
127 next; 140 next;
128 } 141 }
130 if ($w % $T || $h % $T) { 143 if ($w % $T || $h % $T) {
131 warn "$path: weird png size ($w $h), skipping.\n"; 144 warn "$path: weird png size ($w $h), skipping.\n";
132 next; 145 next;
133 } 146 }
134 147
148 unless ($path =~ /~$/) {
149 # possibly enlarge
150 if (0 > aio_stat "$stem.64x64.png") {
151 my $other = "$stem.64x64.png~";
152
153 if (0 > aio_lstat $other or (-M _) > (-M $path)) {
154 my $wrap = 0; # for the time being
155 fork_sub {
156 system "convert png:\Q$path\E -depth 8 rgba:-"
157 . "| $exec_prefix/bin/cfhq2xa $w $h $wrap"
158 . "| convert -depth 8 -size ".($w * 2)."x".($h * 2)." rgba:- $QUANTIZE -quality 00 png32:\Q$other\E~"
159 and die "convert/hq2xa pipeline error: status $? ($!)";
160 system $OPTIPNG, "-i0", "-q", "$other~";
161 die "$other~ has zero size, aborting." unless -s "$other~";
162 rename "$other~", $other;
163 };
164 }
165
166 push @png, [$other, !$CACHE];
167 }
168
169 # possibly scale down
170 if (0 > aio_stat "$stem.32x32.png") {
171 my $other = "$stem.32x32.png~";
172
173 if (0 > aio_lstat $other or (-M _) > (-M $path)) {
174 fork_sub {
175 system "convert png:\Q$path\E -geometry 50% -filter lanczos $QUANTIZE -quality 00 png32:\Q$other\E~";
176 system $OPTIPNG, "-i0", "-q", "$other~";
177
178 # reduce smoothfaces >10000 bytes
179 if ($stem =~ /_S\./ && (-s "$other~") > 10000) {
180 my $ncolor = 256;
181 while () {
182 system "<\Q$other~\E $PNGNQ -s1 -n$ncolor >\Q$other~~\E";
183 system $OPTIPNG, "-i0", "-q", "$other~~";
184 last if 10000 > -s "$other~~";
185 $ncolor = int $ncolor * 0.9;
186 $ncolor > 8 or die "cannot reduce filesize to < 10000 bytes";
187 }
188
189 printf "reduced %s from %d to %d bytes using %d colours.\n",
190 $other, -s "$other~", -s "$other~~", $ncolor
191 if $VERBOSE >= 2;
192 rename "$other~~", "$other~";
193 }
194
195 die "$other~ has zero size, aborting." unless -s "$other~";
196 rename "$other~", $other;
197 };
198 }
199
200 #warn "scaled down $path to $other\n";#d#
201 push @png, [$other, !$CACHE];
202 }
203 }
204
205 (my $face = $stem) =~ s/^.*\///;
206
207 # split all bigfaces, but avoid smoothfaces (*_S)
135 if (($w > $T || $h > $T) && $face !~ /_S\./) { 208 if (($w > $T || $h > $T) && $face !~ /_S\./) {
136 # split 209 # split
137 my @tile; 210 my @tile;
138 for my $x (0 .. (int $w / $T) - 1) { 211 for my $x (0 .. (int $w / $T) - 1) {
139 for my $y (0 .. (int $h / $T) - 1) { 212 for my $y (0 .. (int $h / $T) - 1) {
147 my @todo = grep { $_->[3] <= $mtime } @tile; 220 my @todo = grep { $_->[3] <= $mtime } @tile;
148 if (@todo) { 221 if (@todo) {
149 fork_sub { 222 fork_sub {
150 open my $convert, "|-", $CONVERT, 223 open my $convert, "|-", $CONVERT,
151 "png:-", 224 "png:-",
152 -negate,#d#
153 (map { 225 (map {
154 ( 226 (
155 "(", 227 "(",
156 "+clone", 228 "+clone",
157 -crop => (sprintf "%dx%d+%d+%d", $T, $T, $_->[0] * $T, $_->[1] * $T), 229 -crop => (sprintf "%dx%d+%d+%d", $T, $T, $_->[0] * $T, $_->[1] * $T),
230 "+repage",
231 -quality => "00",
158 -write => "png:$_->[2]~", 232 -write => "png32:$_->[2]~",
159 "+delete", 233 "+delete",
160 ")", 234 ")",
161 ) 235 )
162 } @todo), 236 } @todo),
163 "null:"; 237 "null:";
167 close $convert; 241 close $convert;
168 242
169 # pass 2, optimise, and rename 243 # pass 2, optimise, and rename
170 for (@todo) { 244 for (@todo) {
171 system $OPTIPNG, "-o5", "-i0", "-q", "$_->[2]~"; 245 system $OPTIPNG, "-o5", "-i0", "-q", "$_->[2]~";
246 die "$_->[2]~ has zero size, aborting." unless -s "$_->[2]~";
172 rename "$_->[2]~", $_->[2]; 247 rename "$_->[2]~", $_->[2];
173 } 248 }
174 }; 249 };
175 } 250 }
176 251
180 255
181 if (0 > aio_load $file, $tile) { 256 if (0 > aio_load $file, $tile) {
182 die "$path: unable to read tile +$x+$y, aborting.\n"; 257 die "$path: unable to read tile +$x+$y, aborting.\n";
183 } 258 }
184 IO::AIO::aio_unlink $file unless $CACHE; 259 IO::AIO::aio_unlink $file unless $CACHE;
185 commit_png $x|$y ? "$face+$x+$y" : $face, $tile; 260 commit_png $x|$y ? "$face+$x+$y" : $face, $tile, $T;
186 } 261 }
187 } else { 262 } else {
188 # use as-is (either small, use smooth) 263 # use as-is (either small, use smooth)
189 commit_png $face, $png; 264 commit_png $face, $png, $T;
190 } 265 }
266
267 aio_unlink $path if $delete;
191 } 268 }
192 } 269 }
193 270
194 sub process_arc { 271 sub process_arc {
195 while (@arc) { 272 while (@arc) {
199 aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/ 276 aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/
200 277
201 my $arc = read_arch "$dir/$file"; 278 my $arc = read_arch "$dir/$file";
202 for my $o (values %$arc) { 279 for my $o (values %$arc) {
203 push @ARC, $o; 280 push @ARC, $o;
281 for (my $m = $o; $m; $m = $m->{more}) {
282 $ARC{$m->{_name}} = $m;
283 }
284
285 $o->{editor_folder} = $dir;
286
287 my $visibility = delete $o->{visibility};
288 my $magicmap = delete $o->{magicmap};
289
290 # find upper left corner :/
291 # omg, this is sooo broken
204 my ($dx, $dy); 292 my ($dx, $dy);
205 # omg, this is sooo broken
206 for (my $o = $o; $o; $o = $o->{more}) { 293 for (my $o = $o; $o; $o = $o->{more}) {
207 $dx = $o->{x} if $o->{x} < $dx; 294 $dx = $o->{x} if $o->{x} < $dx;
208 $dy = $o->{y} if $o->{y} < $dy; 295 $dy = $o->{y} if $o->{y} < $dy;
209 } 296 }
297
210 for (my $o = $o; $o; $o = $o->{more}) { 298 for (my $o = $o; $o; $o = $o->{more}) {
211 my $x = $o->{x} - $dx; 299 my $x = $o->{x} - $dx;
212 my $y = $o->{y} - $dy; 300 my $y = $o->{y} - $dy;
213 if ($x|$y) { 301
214 $_ .= "+$x+$y" for $o->{face}, @{$o->{anim} || []}; 302 my $ext = $x|$y ? "+$x+$y" : "";
215 } 303
216 } 304 $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/ || !$o->{face};
305
306 my $visibility = delete $o->{visibility} if exists $o->{visibility};
307 my $magicmap = delete $o->{magicmap} if exists $o->{magicmap};
308
217 if (my $anim = delete $o->{anim}) { 309 my $anim = delete $o->{anim};
310
311 if ($anim) {
312 # possibly add $ext to the animation name to avoid
313 # the need to specify archnames for all parts
314 # of a multipart archetype.
218 $o->{animation} = $o->{_name}; 315 $o->{animation} = "$o->{_name}";
219 $ANIM .= join "", map "$_\n", 316 my $facings = 1;
220 "anim $o->{_name}", 317 my @frames;
318
319 for (@$anim) {
320 if (/^facings\s+(\d+)/) {
321 $facings = $1*1;
322 } elsif (/^blank.x11$|^empty.x11$/) {
323 push @frames, $_;
324 } else {
325 push @frames, "$_$ext";
221 @$anim, 326 }
222 "mina"; 327 }
328
329 $ANIMINFO{$o->{animation}} = {
330 facings => $facings,
331 frames => \@frames,
332 };
333 }
334
335 for my $face ($o->{face} || (), @{$anim || []}) {
336 next if $face =~ /^facings\s|^blank.x11$|^empty.x11$/;
337
338 my $info = $FACEINFO{$face} ||= {};
339
340 $info->{visibility} = $visibility if defined $visibility;
341 $info->{magicmap} = $magicmap if defined $magicmap;
342 }
343
344 if (my $smooth = delete $o->{smoothface}) {
345 my %kv =split /\s+/, $smooth;
346 my $level = $o->{smoothlevel}; #TODO: delete from $o if !gcfclient-support
347 while (my ($face, $smooth) = each %kv) {
348 $FACEINFO{$face}{smooth} = $smooth;
349 $FACEINFO{$face}{smoothlevel} = $level;
350 }
351 }
223 } 352 }
224 } 353 }
225 } 354 }
226 } 355 }
227 356
228 sub process_trs { 357 sub process_trs {
229 while (@trs) { 358 while (@trs) {
230 my ($dir, $file) = @{pop @trs}; 359 my ($dir, $file) = @{pop @trs};
360 my $path = "$dir/$file";
361
362 my $trs;
363 if (0 > aio_load $path, $trs) {
364 warn "$path: $!, skipping.\n";
365 next;
366 }
367
368 $TRS .= $trs;
231 } 369 }
232 } 370 }
233 371
234 sub find_files; 372 sub find_files;
235 sub find_files { 373 sub find_files {
242 find_files "$path/$_" 380 find_files "$path/$_"
243 for grep $_ !~ /^(?:CVS|dev)$/, @$dirs; 381 for grep $_ !~ /^(?:CVS|dev)$/, @$dirs;
244 382
245 for my $file (@$nondirs) { 383 for my $file (@$nondirs) {
246 if ($file =~ /\.png$/) { 384 if ($file =~ /\.png$/) {
247 push @png, "$path/$file"; 385 push @png, ["$path/$file", 0];
248 } elsif ($file =~ /\.trs$/) { 386 } elsif ($file =~ /\.trs$/) {
249 push @trs, [$path, $file]; 387 push @trs, [$path, $file];
250 } elsif ($file =~ /\.arc$/) { 388 } elsif ($file =~ /\.arc$/) {
251 push @arc, [$path, $file]; 389 push @arc, [$path, $file];
252 } else { 390 } else {
253 warn "ignoring $path/$file\n" if $VERBOSE >= 2; 391 warn "ignoring $path/$file\n" if $VERBOSE >= 3;
254 } 392 }
255 } 393 }
256 }; 394 };
257 } 395 }
258 396
259 sub inst_arch($) { 397 sub inst_arch($) {
260 my (undef, $path) = @_; 398 my (undef, $path) = @_;
261 399
400 print "\n",
262 print "installing '$path' to '$DATADIR'\n\n"; 401 "Installing '$path' to '$DATADIR'\n",
402 "\n",
403 "This can take a long time if you run this\n",
404 "for the first time or do not use --cache.\n",
405 "\n",
406 "Unless you run verbosely, all following warning\n",
407 "or error messages indicate serious problems.\n",
408 "\n";
263 409
264 if (!-d "$path/treasures") { 410 if (!-d "$path/treasures") {
265 warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; 411 warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n";
266 exit 1 unless $FORCE; 412 exit 1 unless $FORCE;
267 } 413 }
268 414
269 find_files $path; 415 find_files $path;
270 IO::AIO::flush; 416 IO::AIO::flush;
271 417
272 $_->join for ( 418 $_->join for (
273 (async \&process_png), (async \&process_png), 419 # four png crunchers work fine for my 2x smp machine
420 (async \&process_png), (async \&process_png), (async \&process_png), (async \&process_png),
274 (async \&process_trs), (async \&process_trs), 421 (async \&process_trs), (async \&process_trs),
275 (async \&process_arc), (async \&process_arc), 422 (async \&process_arc), (async \&process_arc),
276 ); 423 );
277 424
278 { 425 {
279 open my $fh, ">:utf8", "$DATADIR/animations~" 426 # remove path prefix from editor_folder
280 or die "$DATADIR/animations~: $!"; 427 substr $_->{editor_folder}, 0, 1 + length $path, ""
281 print $fh $ANIM; 428 for values %ARC;
429
430 # resolve inherit
431 while () {
432 my $progress;
433 my $loop;
434
435 for my $o (values %ARC) {
436 if (my $other = $o->{inherit}) {
437 if (my $s = $ARC{$other}) {
438 if ($s->{inherit}) {
439 $loop = $s;
440 } else {
441 delete $o->{inherit};
442 my %s = %$s;
443 delete @s{qw(_name more name name_pl)};
444 %$o = ( %s, %$o );
445 ++$progress;
446 }
447 } else {
448 warn "'$o->{_name}' tries to inherit from undefined archetype '$other', skipping.\n";
449 delete $ARC{$o->{_name}};
450 }
451 }
452 }
453
454 unless ($progress) {
455 die "inheritance loop detected starting at archetype '$loop->{_name}', aborting.\n"
456 if $loop;
457
458 last;
459 }
282 } 460 }
283 461
284 {
285 open my $fh, ">:utf8", "$DATADIR/archetypes~" 462 open my $fh, ">:utf8", "$DATADIR/archetypes~"
286 or die "$DATADIR/archetypes~: $!"; 463 or die "$DATADIR/archetypes~: $!";
287 print $fh Crossfire::archlist_to_string \@ARC; 464 print $fh Crossfire::archlist_to_string [sort { $a->{_name} cmp $b->{_name} } @ARC];
288 } 465 }
289
290 @PNG = sort { $a->[0] cmp $b->[0] } @PNG;
291 466
292 { 467 {
468 open my $fh, ">:utf8", "$DATADIR/treasures~"
469 or die "$DATADIR/treasures~: $!";
470 print $fh $TRS;
471 }
472
473 {
474 while (my ($k, $v) = each %FACEINFO) {
475 length $v->{data32} or warn "$k: face has no png32. this will not work (shoddy gcfclient will crash of course).\n";
476 length $v->{data64} or warn "$k: face has no png64. this will not work very well.\n";
477
478 length $v->{data32} <= 10000 or warn "$k: face32 larger than 10000 bytes, will not work with crossfire client.\n";
479 #length $v->{data64} <= 10000 or warn "$k: face64 larger than 10000 bytes.\n";
480
481 $v->{chksum32} = Digest::MD5::md5 $v->{data32};
482 $v->{chksum64} = Digest::MD5::md5 $v->{data64};
483 }
484
293 open my $fh, ">:perlio", "$DATADIR/crossfire.0~" 485 open my $fh, ">:perlio", "$DATADIR/facedata~"
294 or die "$DATADIR/crossfire.0~: $!"; 486 or die "$DATADIR/facedata~: $!";
295 printf $fh "IMAGE %d %d %s\x0a%s", $_, (length $PNG[$_][1]), $PNG[$_][0], $PNG[$_][1] 487
296 for 0.. $#PNG; 488 print $fh freeze {
489 version => 2,
490 faceinfo => \%FACEINFO,
491 animinfo => \%ANIMINFO,
297 } 492 };
493 }
298 494
299 rename "$DATADIR/archetypes~" , "$DATADIR/archetypes"; 495 for (qw(archetypes facedata treasures)) {
300 rename "$DATADIR/crossfire.0~", "$DATADIR/crossfire.0"; 496 chmod 0644, "$DATADIR/$_~";
301 rename "$DATADIR/animations~" , "$DATADIR/animations"; 497 rename "$DATADIR/$_~", "$DATADIR/$_"
498 or die "$DATADIR/$_: $!";
499 }
302 500
303 die "--install-arch not fully implemented\n"; 501 print "archetype data installed successfully.\n";
304 } 502 }
305} 503}
306 504
307Getopt::Long::Configure ("bundling", "no_ignore_case"); 505Getopt::Long::Configure ("bundling", "no_ignore_case");
308GetOptions ( 506GetOptions (

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines