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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines