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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines