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.12 by root, Mon Mar 12 17:26:41 2007 UTC vs.
Revision 1.29 by root, Wed Apr 18 09:38:58 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines