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.22 by root, Thu Apr 5 13:50:49 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 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 "installing '$path' to '$DATADIR/maps'\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;
84 our %FACEINFO; 87 our %FACEINFO;
85 our @ARC; 88 our @ARC;
86 our $TRS; 89 our $TRS;
87 our $NFILE; 90 our $NFILE;
88 our %ANIM; 91 our %ANIM;
89 our $SMOOTH; 92
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 }
133 140
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 }
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/^.*\///;
138 186
139 if (($w > $T || $h > $T) && $face !~ /_S\./) { 187 if (($w > $T || $h > $T) && $face !~ /_S\./) {
140 # split 188 # split
141 my @tile; 189 my @tile;
142 for my $x (0 .. (int $w / $T) - 1) { 190 for my $x (0 .. (int $w / $T) - 1) {
157 ( 205 (
158 "(", 206 "(",
159 "+clone", 207 "+clone",
160 -crop => (sprintf "%dx%d+%d+%d", $T, $T, $_->[0] * $T, $_->[1] * $T), 208 -crop => (sprintf "%dx%d+%d+%d", $T, $T, $_->[0] * $T, $_->[1] * $T),
161 "+repage", 209 "+repage",
210 -quality => "00",
162 -write => "png:$_->[2]~", 211 -write => "png32:$_->[2]~",
163 "+delete", 212 "+delete",
164 ")", 213 ")",
165 ) 214 )
166 } @todo), 215 } @todo),
167 "null:"; 216 "null:";
171 close $convert; 220 close $convert;
172 221
173 # pass 2, optimise, and rename 222 # pass 2, optimise, and rename
174 for (@todo) { 223 for (@todo) {
175 system $OPTIPNG, "-o5", "-i0", "-q", "$_->[2]~"; 224 system $OPTIPNG, "-o5", "-i0", "-q", "$_->[2]~";
225 die "$_->[2]~ has zero size, aborting." unless -s "$_->[2]~";
176 rename "$_->[2]~", $_->[2]; 226 rename "$_->[2]~", $_->[2];
177 } 227 }
178 }; 228 };
179 } 229 }
180 230
184 234
185 if (0 > aio_load $file, $tile) { 235 if (0 > aio_load $file, $tile) {
186 die "$path: unable to read tile +$x+$y, aborting.\n"; 236 die "$path: unable to read tile +$x+$y, aborting.\n";
187 } 237 }
188 IO::AIO::aio_unlink $file unless $CACHE; 238 IO::AIO::aio_unlink $file unless $CACHE;
189 commit_png $x|$y ? "$face+$x+$y" : $face, $tile; 239 commit_png $x|$y ? "$face+$x+$y" : $face, $tile, $T;
190 } 240 }
191 } else { 241 } else {
192 # use as-is (either small, use smooth) 242 # use as-is (either small, use smooth)
193 commit_png $face, $png; 243 commit_png $face, $png, $T;
194 } 244 }
245
246 aio_unlink $path if $delete;
195 } 247 }
196 } 248 }
197 249
198 sub process_arc { 250 sub process_arc {
199 while (@arc) { 251 while (@arc) {
254 $info->{visibility} = $visibility if defined $visibility; 306 $info->{visibility} = $visibility if defined $visibility;
255 $info->{magicmap} = $magicmap if defined $magicmap; 307 $info->{magicmap} = $magicmap if defined $magicmap;
256 } 308 }
257 309
258 if (my $smooth = delete $o->{smoothface}) { 310 if (my $smooth = delete $o->{smoothface}) {
259 $SMOOTH .= "$smooth\n"; 311 my ($face, $smooth) = split /\s+/, $smooth;
312 # skip empty_S.x11, it seems to server no purpose whatsoever
313 # but increases bandwidth demands and worse.
314 unless ($smooth eq "empty_S.x11") {
315 $FACEINFO{$face}{smooth} = $smooth;
316 }
260 } 317 }
261 } 318 }
262 } 319 }
263 } 320 }
264 } 321 }
289 find_files "$path/$_" 346 find_files "$path/$_"
290 for grep $_ !~ /^(?:CVS|dev)$/, @$dirs; 347 for grep $_ !~ /^(?:CVS|dev)$/, @$dirs;
291 348
292 for my $file (@$nondirs) { 349 for my $file (@$nondirs) {
293 if ($file =~ /\.png$/) { 350 if ($file =~ /\.png$/) {
294 push @png, "$path/$file"; 351 push @png, ["$path/$file", 0];
295 } elsif ($file =~ /\.trs$/) { 352 } elsif ($file =~ /\.trs$/) {
296 push @trs, [$path, $file]; 353 push @trs, [$path, $file];
297 } elsif ($file =~ /\.arc$/) { 354 } elsif ($file =~ /\.arc$/) {
298 push @arc, [$path, $file]; 355 push @arc, [$path, $file];
299 } else { 356 } else {
304 } 361 }
305 362
306 sub inst_arch($) { 363 sub inst_arch($) {
307 my (undef, $path) = @_; 364 my (undef, $path) = @_;
308 365
309 print "installing '$path' to '$DATADIR'\n\n"; 366 print "installing '$path' to '$DATADIR'\n",
367 "(this can take a long time if you run this\n",
368 "for the first time or do not use --cache).\n";
310 369
311 if (!-d "$path/treasures") { 370 if (!-d "$path/treasures") {
312 warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; 371 warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n";
313 exit 1 unless $FORCE; 372 exit 1 unless $FORCE;
314 } 373 }
315 374
316 find_files $path; 375 find_files $path;
317 IO::AIO::flush; 376 IO::AIO::flush;
318 377
319 $_->join for ( 378 $_->join for (
320 (async \&process_png), (async \&process_png), 379 # four png crunchers work fine for my 2x smp machine
380 (async \&process_png), (async \&process_png), (async \&process_png), (async \&process_png),
321 (async \&process_trs), (async \&process_trs), 381 (async \&process_trs), (async \&process_trs),
322 (async \&process_arc), (async \&process_arc), 382 (async \&process_arc), (async \&process_arc),
323 ); 383 );
324 384
325 { 385 {
334 substr $_->{editor_folder}, 0, 1 + length $path, "" for @ARC; 394 substr $_->{editor_folder}, 0, 1 + length $path, "" for @ARC;
335 print $fh Crossfire::archlist_to_string \@ARC; 395 print $fh Crossfire::archlist_to_string \@ARC;
336 } 396 }
337 397
338 { 398 {
339 open my $fh, ">:utf8", "$DATADIR/smooth~"
340 or die "$DATADIR/smooth~: $!";
341 print $fh $SMOOTH;
342 }
343
344 {
345 open my $fh, ">:utf8", "$DATADIR/treasures~" 399 open my $fh, ">:utf8", "$DATADIR/treasures~"
346 or die "$DATADIR/treasures~: $!"; 400 or die "$DATADIR/treasures~: $!";
347 print $fh $TRS; 401 print $fh $TRS;
348 } 402 }
349 403
350 { 404 {
351 while (my ($k, $v) = each %FACEINFO) { 405 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"; 406 length $v->{data32} or warn "$k: face has no png32. this will not work (shoddy gcfclient will crash of course).\n";
407 length $v->{data64} or warn "$k: face has no png64. this will not work very well.\n";
408
409 length $v->{data32} <= 10000 or warn "$k: face32 larger than 10000 bytes, will not work with crossfire client.\n";
410 #length $v->{data64} <= 10000 or warn "$k: face64 larger than 10000 bytes.\n";
357 411
358 $v->{chksum32} = Digest::MD5::md5 $v->{data32}; 412 $v->{chksum32} = Digest::MD5::md5 $v->{data32};
413 $v->{chksum64} = Digest::MD5::md5 $v->{data64};
359 } 414 }
360 415
361 open my $fh, ">:perlio", "$DATADIR/faces~" 416 open my $fh, ">:perlio", "$DATADIR/facedata~"
362 or die "$DATADIR/faces~: $!"; 417 or die "$DATADIR/facedata~: $!";
363 418
419 $FACEINFO{""} = { version => 1};
364 print $fh Storable::nfreeze \%FACEINFO; 420 print $fh Storable::nfreeze \%FACEINFO;
365 } 421 }
366 422
367 for (qw(archetypes faces animations treasures smooth)) { 423 for (qw(archetypes facedata animations treasures)) {
368 chmod 0644, "$DATADIR/$_~"; 424 chmod 0644, "$DATADIR/$_~";
369 rename "$DATADIR/$_~", "$DATADIR/$_"; 425 rename "$DATADIR/$_~", "$DATADIR/$_"
426 or die "$DATADIR/$_: $!";
370 } 427 }
428
429 print "archetype data installed successfully.\n";
371 } 430 }
372} 431}
373 432
374Getopt::Long::Configure ("bundling", "no_ignore_case"); 433Getopt::Long::Configure ("bundling", "no_ignore_case");
375GetOptions ( 434GetOptions (

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines