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.25 by root, Fri Apr 13 05:08:51 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 "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; 87 our %ANIMINFO;
84 our %FACEINFO; 88 our %FACEINFO;
85 our @ARC; 89 our @ARC;
90 our $TRS;
86 our $NFILE; 91 our $NFILE;
87 our %ANIM; 92
93 our $QUANTIZE = "+dither -colorspace RGB -colors 256";
88 94
89 our (@png, @trs, @arc); # files we are interested in 95 our (@png, @trs, @arc); # files we are interested in
90 96
91 sub commit_png { 97 sub commit_png($$$) {
92 my ($name, $data) = @_; 98 my ($name, $data, $T) = @_;
93 99
94 $PNG32{$name} = $data; 100 $FACEINFO{$name}{"data$T"} = $data;
95 $FACEINFO{$name} ||= {};
96 } 101 }
97 102
98 sub process_png { 103 sub process_png {
99 while (@png) { 104 while (@png) {
100 my $path = pop @png; 105 my ($path, $delete) = @{pop @png};
101 106
102 my $png; 107 my $png;
103 aio_lstat $path; 108 aio_lstat $path;
104 my ($size, $mtime) = (stat _)[7,9]; 109 my ($size, $mtime) = (stat _)[7,9];
105 110
106 if (0 > aio_load $path, $png) { 111 if (0 > aio_load $path, $png) {
107 warn "$path: $!, skipping.\n"; 112 warn "$path: $!, skipping.\n";
108 next; 113 next;
109 } 114 }
110 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
111 # quickly extract width and height of the (necessarily PNG) image 128 # quickly extract width and height of the (necessarily PNG) image
112 unless ($png =~ /^\x89PNG\x0d\x0a\x1a\x0a....IHDR(........)/s) { 129 unless ($png =~ /^\x89PNG\x0d\x0a\x1a\x0a....IHDR(........)/s) {
113 warn "$path: not a recongized png file, skipping.\n"; 130 warn "$path: not a recognized png file, skipping.\n";
114 next; 131 next;
115 } 132 }
116 133
117 my ($w, $h) = unpack "NN", $1; 134 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 135
127 if ($w < $T || $h < $T) { 136 if ($w < $T || $h < $T) {
128 warn "$path: too small ($w $h), skipping.\n"; 137 warn "$path: too small ($w $h), skipping.\n";
129 next; 138 next;
130 } 139 }
132 if ($w % $T || $h % $T) { 141 if ($w % $T || $h % $T) {
133 warn "$path: weird png size ($w $h), skipping.\n"; 142 warn "$path: weird png size ($w $h), skipping.\n";
134 next; 143 next;
135 } 144 }
136 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)
137 if (($w > $T || $h > $T) && $face !~ /_S\./) { 188 if (($w > $T || $h > $T) && $face !~ /_S\./) {
138 # split 189 # split
139 my @tile; 190 my @tile;
140 for my $x (0 .. (int $w / $T) - 1) { 191 for my $x (0 .. (int $w / $T) - 1) {
141 for my $y (0 .. (int $h / $T) - 1) { 192 for my $y (0 .. (int $h / $T) - 1) {
155 ( 206 (
156 "(", 207 "(",
157 "+clone", 208 "+clone",
158 -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),
159 "+repage", 210 "+repage",
211 -quality => "00",
160 -write => "png:$_->[2]~", 212 -write => "png32:$_->[2]~",
161 "+delete", 213 "+delete",
162 ")", 214 ")",
163 ) 215 )
164 } @todo), 216 } @todo),
165 "null:"; 217 "null:";
169 close $convert; 221 close $convert;
170 222
171 # pass 2, optimise, and rename 223 # pass 2, optimise, and rename
172 for (@todo) { 224 for (@todo) {
173 system $OPTIPNG, "-o5", "-i0", "-q", "$_->[2]~"; 225 system $OPTIPNG, "-o5", "-i0", "-q", "$_->[2]~";
226 die "$_->[2]~ has zero size, aborting." unless -s "$_->[2]~";
174 rename "$_->[2]~", $_->[2]; 227 rename "$_->[2]~", $_->[2];
175 } 228 }
176 }; 229 };
177 } 230 }
178 231
182 235
183 if (0 > aio_load $file, $tile) { 236 if (0 > aio_load $file, $tile) {
184 die "$path: unable to read tile +$x+$y, aborting.\n"; 237 die "$path: unable to read tile +$x+$y, aborting.\n";
185 } 238 }
186 IO::AIO::aio_unlink $file unless $CACHE; 239 IO::AIO::aio_unlink $file unless $CACHE;
187 commit_png $x|$y ? "$face+$x+$y" : $face, $tile; 240 commit_png $x|$y ? "$face+$x+$y" : $face, $tile, $T;
188 } 241 }
189 } else { 242 } else {
190 # use as-is (either small, use smooth) 243 # use as-is (either small, use smooth)
191 commit_png $face, $png; 244 commit_png $face, $png, $T;
192 } 245 }
246
247 aio_unlink $path if $delete;
193 } 248 }
194 } 249 }
195 250
196 sub process_arc { 251 sub process_arc {
197 while (@arc) { 252 while (@arc) {
229 my $magicmap = delete $o->{magicmap} if exists $o->{magicmap}; 284 my $magicmap = delete $o->{magicmap} if exists $o->{magicmap};
230 285
231 my $anim = delete $o->{anim}; 286 my $anim = delete $o->{anim};
232 287
233 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.
234 $o->{animation} = "$o->{_name}"; 292 $o->{animation} = "$o->{_name}";
293 my $facings = 1;
294 my @frames;
235 295
236 for (@$anim) { 296 for (@$anim) {
237 $_ .= $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 }
238 } 304 }
239 305
240 $ANIM{"$o->{_name}$ext"} = 306 $ANIMINFO{$o->{animation}} = {
241 join "", map "$_\n", 307 facings => $facings,
242 "anim $o->{_name}", 308 frames => \@frames,
243 @$anim, 309 };
244 "mina";
245 } 310 }
246 311
247 for my $face ($o->{face} || (), @{$anim || []}) { 312 for my $face ($o->{face} || (), @{$anim || []}) {
248 next if $face =~ /^facings\s|^blank.x11$|^empty.x11$/; 313 next if $face =~ /^facings\s|^blank.x11$|^empty.x11$/;
249 314
250 my $info = $FACEINFO{$face} ||= {}; 315 my $info = $FACEINFO{$face} ||= {};
251 316
252 $info->{visibility} = $visibility if defined $visibility; 317 $info->{visibility} = $visibility if defined $visibility;
253 $info->{magicmap} = $magicmap if defined $magicmap; 318 $info->{magicmap} = $magicmap if defined $magicmap;
319 }
320
321 if (my $smooth = delete $o->{smoothface}) {
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 }
254 } 328 }
255 } 329 }
256 } 330 }
257 } 331 }
258 } 332 }
259 333
260 sub process_trs { 334 sub process_trs {
261 while (@trs) { 335 while (@trs) {
262 my ($dir, $file) = @{pop @trs}; 336 my ($dir, $file) = @{pop @trs};
337 my $path = "$dir/$file";
338
339 my $trs;
340 if (0 > aio_load $path, $trs) {
341 warn "$path: $!, skipping.\n";
342 next;
343 }
344
345 $TRS .= $trs;
263 } 346 }
264 } 347 }
265 348
266 sub find_files; 349 sub find_files;
267 sub find_files { 350 sub find_files {
274 find_files "$path/$_" 357 find_files "$path/$_"
275 for grep $_ !~ /^(?:CVS|dev)$/, @$dirs; 358 for grep $_ !~ /^(?:CVS|dev)$/, @$dirs;
276 359
277 for my $file (@$nondirs) { 360 for my $file (@$nondirs) {
278 if ($file =~ /\.png$/) { 361 if ($file =~ /\.png$/) {
279 push @png, "$path/$file"; 362 push @png, ["$path/$file", 0];
280 } elsif ($file =~ /\.trs$/) { 363 } elsif ($file =~ /\.trs$/) {
281 push @trs, [$path, $file]; 364 push @trs, [$path, $file];
282 } elsif ($file =~ /\.arc$/) { 365 } elsif ($file =~ /\.arc$/) {
283 push @arc, [$path, $file]; 366 push @arc, [$path, $file];
284 } else { 367 } else {
289 } 372 }
290 373
291 sub inst_arch($) { 374 sub inst_arch($) {
292 my (undef, $path) = @_; 375 my (undef, $path) = @_;
293 376
294 print "installing '$path' to '$DATADIR'\n\n"; 377 print "installing '$path' to '$DATADIR'\n",
378 "(this can take a long time if you run this\n",
379 "for the first time or do not use --cache).\n";
295 380
296 if (!-d "$path/treasures") { 381 if (!-d "$path/treasures") {
297 warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; 382 warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n";
298 exit 1 unless $FORCE; 383 exit 1 unless $FORCE;
299 } 384 }
300 385
301 find_files $path; 386 find_files $path;
302 IO::AIO::flush; 387 IO::AIO::flush;
303 388
304 $_->join for ( 389 $_->join for (
305 (async \&process_png), (async \&process_png), 390 # four png crunchers work fine for my 2x smp machine
391 (async \&process_png), (async \&process_png), (async \&process_png), (async \&process_png),
306 (async \&process_trs), (async \&process_trs), 392 (async \&process_trs), (async \&process_trs),
307 (async \&process_arc), (async \&process_arc), 393 (async \&process_arc), (async \&process_arc),
308 ); 394 );
309 395
310 { 396 {
311 open my $fh, ">:utf8", "$DATADIR/animations~"
312 or die "$DATADIR/animations~: $!";
313 print $fh join "", map $ANIM{$_}, sort keys %ANIM
314 }
315
316 {
317 open my $fh, ">:utf8", "$DATADIR/archetypes~" 397 open my $fh, ">:utf8", "$DATADIR/archetypes~"
318 or die "$DATADIR/archetypes~: $!"; 398 or die "$DATADIR/archetypes~: $!";
319 substr $_->{editor_folder}, 0, length $path, "" for @ARC; 399 substr $_->{editor_folder}, 0, 1 + length $path, "" for @ARC;
320 print $fh Crossfire::archlist_to_string \@ARC; 400 print $fh Crossfire::archlist_to_string \@ARC;
401 }
402
403 {
404 open my $fh, ">:utf8", "$DATADIR/treasures~"
405 or die "$DATADIR/treasures~: $!";
406 print $fh $TRS;
321 } 407 }
322 408
323 { 409 {
324 while (my ($k, $v) = each %FACEINFO) { 410 while (my ($k, $v) = each %FACEINFO) {
325 $v->{data32} ||= delete $PNG32{$k}; 411 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"; 412 length $v->{data64} or warn "$k: face has no png64. this will not work very well.\n";
413
414 length $v->{data32} <= 10000 or warn "$k: face32 larger than 10000 bytes, will not work with crossfire client.\n";
415 #length $v->{data64} <= 10000 or warn "$k: face64 larger than 10000 bytes.\n";
330 416
331 $v->{chksum32} = Digest::MD5::md5 $v->{data32}; 417 $v->{chksum32} = Digest::MD5::md5 $v->{data32};
418 $v->{chksum64} = Digest::MD5::md5 $v->{data64};
332 } 419 }
333 420
334 open my $fh, ">:perlio", "$DATADIR/faces~" 421 open my $fh, ">:perlio", "$DATADIR/facedata~"
335 or die "$DATADIR/faces~: $!"; 422 or die "$DATADIR/facedata~: $!";
336 423
337 print $fh Storable::nfreeze \%FACEINFO; 424 print $fh freeze {
338 425 version => 2,
339 #use PApp::Util; warn PApp::Util::dumpval \%FACEINFO; 426 faceinfo => \%FACEINFO,
427 animinfo => \%ANIMINFO,
340 } 428 };
429 }
341 430
342 for (qw(archetypes faces animations)) { 431 for (qw(archetypes facedata treasures)) {
343 chmod 0644, "$DATADIR/$_~"; 432 chmod 0644, "$DATADIR/$_~";
344 rename "$DATADIR/$_~", "$DATADIR/$_"; 433 rename "$DATADIR/$_~", "$DATADIR/$_"
434 or die "$DATADIR/$_: $!";
345 } 435 }
346 436
347 die "--install-arch not fully implemented\n"; 437 print "archetype data installed successfully.\n";
348 } 438 }
349} 439}
350 440
351Getopt::Long::Configure ("bundling", "no_ignore_case"); 441Getopt::Long::Configure ("bundling", "no_ignore_case");
352GetOptions ( 442GetOptions (

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines