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.9 by root, Sun Mar 11 22:36:53 2007 UTC vs.
Revision 1.21 by root, Thu Apr 5 12:34:31 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;
89 our $TRS;
86 our $NFILE; 90 our $NFILE;
87 our %ANIM; 91 our %ANIM;
88 92
93 our $QUANTIZE = "+dither -colorspace RGB -colors 256";
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 }
131 140
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 }
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 warn "rescale $other\n";#d#
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/^.*\///;
136 187
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) {
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) {
201 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 :/
202 257
203 my $arc = read_arch "$dir/$file"; 258 my $arc = read_arch "$dir/$file";
204 for my $o (values %$arc) { 259 for my $o (values %$arc) {
205 push @ARC, $o; 260 push @ARC, $o;
261
262 $o->{editor_folder} = $dir;
206 263
207 my $visibility = delete $o->{visibility}; 264 my $visibility = delete $o->{visibility};
208 my $magicmap = delete $o->{magicmap}; 265 my $magicmap = delete $o->{magicmap};
209 266
210 # find upper left corner :/ 267 # find upper left corner :/
248 my $info = $FACEINFO{$face} ||= {}; 305 my $info = $FACEINFO{$face} ||= {};
249 306
250 $info->{visibility} = $visibility if defined $visibility; 307 $info->{visibility} = $visibility if defined $visibility;
251 $info->{magicmap} = $magicmap if defined $magicmap; 308 $info->{magicmap} = $magicmap if defined $magicmap;
252 } 309 }
310
311 if (my $smooth = delete $o->{smoothface}) {
312 my ($face, $smooth) = split /\s+/, $smooth;
313 # skip empty_S.x11, it seems to server no purpose whatsoever
314 # but increases bandwidth demands and worse.
315 unless ($smooth eq "empty_S.x11") {
316 $FACEINFO{$face}{smooth} = $smooth;
317 }
318 }
253 } 319 }
254 } 320 }
255 } 321 }
256 } 322 }
257 323
258 sub process_trs { 324 sub process_trs {
259 while (@trs) { 325 while (@trs) {
260 my ($dir, $file) = @{pop @trs}; 326 my ($dir, $file) = @{pop @trs};
327 my $path = "$dir/$file";
328
329 my $trs;
330 if (0 > aio_load $path, $trs) {
331 warn "$path: $!, skipping.\n";
332 next;
333 }
334
335 $TRS .= $trs;
261 } 336 }
262 } 337 }
263 338
264 sub find_files; 339 sub find_files;
265 sub find_files { 340 sub find_files {
272 find_files "$path/$_" 347 find_files "$path/$_"
273 for grep $_ !~ /^(?:CVS|dev)$/, @$dirs; 348 for grep $_ !~ /^(?:CVS|dev)$/, @$dirs;
274 349
275 for my $file (@$nondirs) { 350 for my $file (@$nondirs) {
276 if ($file =~ /\.png$/) { 351 if ($file =~ /\.png$/) {
277 push @png, "$path/$file"; 352 push @png, ["$path/$file", 0];
278 } elsif ($file =~ /\.trs$/) { 353 } elsif ($file =~ /\.trs$/) {
279 push @trs, [$path, $file]; 354 push @trs, [$path, $file];
280 } elsif ($file =~ /\.arc$/) { 355 } elsif ($file =~ /\.arc$/) {
281 push @arc, [$path, $file]; 356 push @arc, [$path, $file];
282 } else { 357 } else {
287 } 362 }
288 363
289 sub inst_arch($) { 364 sub inst_arch($) {
290 my (undef, $path) = @_; 365 my (undef, $path) = @_;
291 366
292 print "installing '$path' to '$DATADIR'\n\n"; 367 print "installing '$path' to '$DATADIR'\n",
368 "(this can take a long time if you run this\n",
369 "for the first time or do not use --cache).\n";
293 370
294 if (!-d "$path/treasures") { 371 if (!-d "$path/treasures") {
295 warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; 372 warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n";
296 exit 1 unless $FORCE; 373 exit 1 unless $FORCE;
297 } 374 }
298 375
299 find_files $path; 376 find_files $path;
300 IO::AIO::flush; 377 IO::AIO::flush;
301 378
302 $_->join for ( 379 $_->join for (
303 (async \&process_png), (async \&process_png), 380 # four png crunchers work fine for my 2x smp machine
381 (async \&process_png), (async \&process_png), (async \&process_png), (async \&process_png),
304 (async \&process_trs), (async \&process_trs), 382 (async \&process_trs), (async \&process_trs),
305 (async \&process_arc), (async \&process_arc), 383 (async \&process_arc), (async \&process_arc),
306 ); 384 );
307 385
308 { 386 {
312 } 390 }
313 391
314 { 392 {
315 open my $fh, ">:utf8", "$DATADIR/archetypes~" 393 open my $fh, ">:utf8", "$DATADIR/archetypes~"
316 or die "$DATADIR/archetypes~: $!"; 394 or die "$DATADIR/archetypes~: $!";
395 substr $_->{editor_folder}, 0, 1 + length $path, "" for @ARC;
317 print $fh Crossfire::archlist_to_string \@ARC; 396 print $fh Crossfire::archlist_to_string \@ARC;
397 }
398
399 {
400 open my $fh, ">:utf8", "$DATADIR/treasures~"
401 or die "$DATADIR/treasures~: $!";
402 print $fh $TRS;
318 } 403 }
319 404
320 { 405 {
321 while (my ($k, $v) = each %FACEINFO) { 406 while (my ($k, $v) = each %FACEINFO) {
322 $v->{data32} ||= delete $PNG32{$k}; 407 length $v->{data32} or warn "$k: face has no png32. this will not work (shoddy gcfclient will crash of course).\n";
323 }
324
325 while (my ($k, $v) = each %FACEINFO) {
326 length $v->{data32} or warn "$k: face has no png32. this will crash the server.\n"; 408 length $v->{data64} or warn "$k: face has no png64. this will not work very well.\n";
409
410 length $v->{data32} <= 10000 or warn "$k: face32 larger than 10000 bytes, will not work with crossfire client.\n";
411 #length $v->{data64} <= 10000 or warn "$k: face64 larger than 10000 bytes.\n";
327 412
328 $v->{chksum32} = Digest::MD5::md5 $v->{data32}; 413 $v->{chksum32} = Digest::MD5::md5 $v->{data32};
414 $v->{chksum64} = Digest::MD5::md5 $v->{data64};
329 } 415 }
330 416
331 open my $fh, ">:perlio", "$DATADIR/faces~" 417 open my $fh, ">:perlio", "$DATADIR/facedata~"
332 or die "$DATADIR/faces~: $!"; 418 or die "$DATADIR/facedata~: $!";
333 419
420 $FACEINFO{""} = { version => 1};
334 print $fh Storable::nfreeze \%FACEINFO; 421 print $fh Storable::nfreeze \%FACEINFO;
335
336 #use PApp::Util; warn PApp::Util::dumpval \%FACEINFO;
337 } 422 }
338 423
339 for (qw(archetypes faces animations)) { 424 for (qw(archetypes facedata animations treasures)) {
340 chmod 0644, "$DATADIR/$_~"; 425 chmod 0644, "$DATADIR/$_~";
341 rename "$DATADIR/$_~", "$DATADIR/$_"; 426 rename "$DATADIR/$_~", "$DATADIR/$_"
427 or die "$DATADIR/$_: $!";
342 } 428 }
343 429
344 die "--install-arch not fully implemented\n"; 430 print "archetype data installed successfully.\n";
345 } 431 }
346} 432}
347 433
348Getopt::Long::Configure ("bundling", "no_ignore_case"); 434Getopt::Long::Configure ("bundling", "no_ignore_case");
349GetOptions ( 435GetOptions (

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines