ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/utils/cfutil.in
Revision: 1.17
Committed: Wed Mar 14 16:23:26 2007 UTC (17 years, 2 months ago) by root
Branch: MAIN
Changes since 1.16: +1 -0 lines
Log Message:
enable face caching again for those too stupid to read :(

File Contents

# User Rev Content
1 root 1.1 #!@PERL@
2    
3 root 1.2 use strict;
4    
5     my $prefix = "@prefix@";
6     my $exec_prefix = "@exec_prefix@";
7     my $datarootdir = "@datarootdir@";
8     my $DATADIR = "@datadir@/@PACKAGE@";
9    
10     my $CONVERT = "@CONVERT@";
11     my $IDENTIFY = "@IDENTIFY@";
12 root 1.3 my $OPTIPNG = "@OPTIPNG@";
13 root 1.2 my $RSYNC = "@RSYNC@";
14    
15     use Getopt::Long;
16 root 1.3 use Coro::Event;
17     use AnyEvent;
18     use IO::AIO ();
19 root 1.2 use File::Temp;
20     use Crossfire;
21 root 1.3 use Coro;
22     use Coro::AIO;
23     use POSIX ();
24 root 1.6 use Digest::MD5;
25 root 1.17 use Storable; $Storable::canonical = 1;
26 root 1.2
27     sub usage {
28     warn <<EOF;
29 root 1.3 Usage: cfutil [-v] [-q] [--force] [--cache]
30 root 1.2 [--install-arch path]
31     [--install-maps maps]
32     [--print-statedir]
33     [--print-confdir]
34     [--print-datadir]
35     [--print-libdir]
36     [--print-bindir]
37     EOF
38     exit 1;
39     }
40    
41     my $VERBOSE = 1;
42 root 1.3 my $CACHE = 0;
43 root 1.2 my $FORCE;
44 root 1.3 my $TMPDIR = "/tmp/cfutil$$~";
45     my $TMPFILE = "aaaa0";
46 root 1.2
47     END { system "rm", "-rf", $TMPDIR }
48    
49 root 1.3 Event->signal (signal => "INT", cb => sub { exit 1 });
50     Event->signal (signal => "TERM", cb => sub { exit 1 });
51    
52 root 1.2 mkdir $TMPDIR, 0700
53     or die "$TMPDIR: $!";
54    
55 root 1.3 sub fork_sub(&) {
56     my ($cb) = @_;
57    
58     if (my $pid = fork) {
59     my $current = $Coro::current;
60     my $w = AnyEvent->child (pid => $pid, cb => sub { $current->ready });
61     Coro::schedule;
62     } else {
63     eval { $cb->() };
64     POSIX::_exit 0 unless $@;
65     warn $@;
66     POSIX::_exit 1;
67     }
68     }
69    
70 root 1.2 sub inst_maps($) {
71     my (undef, $path) = @_;
72    
73 root 1.13 print "installing '$path' to '$DATADIR/maps'\n";
74 root 1.2
75     if (!-f "$path/regions") {
76     warn "'$path' does not look like a maps directory ('regions' file is missing).\n";
77     exit 1 unless $FORCE;
78     }
79    
80 root 1.13 system $RSYNC, "-av", "$path/.", "$DATADIR/maps/.", "--delete", "--exclude", "CVS", "--delete-excluded"
81 root 1.16 and die "map installation failed.\n";
82 root 1.13
83     print "maps installed successfully.\n";
84 root 1.2 }
85    
86     {
87 root 1.6 our %PNG32;
88     our %FACEINFO;
89 root 1.3 our @ARC;
90 root 1.12 our $TRS;
91 root 1.3 our $NFILE;
92 root 1.6 our %ANIM;
93 root 1.3
94     our (@png, @trs, @arc); # files we are interested in
95 root 1.2
96     sub commit_png {
97     my ($name, $data) = @_;
98 root 1.5
99 root 1.6 $PNG32{$name} = $data;
100     $FACEINFO{$name} ||= {};
101 root 1.2 }
102    
103 root 1.3 sub process_png {
104     while (@png) {
105     my $path = pop @png;
106 root 1.2
107 root 1.3 my $png;
108     aio_lstat $path;
109 root 1.2 my ($size, $mtime) = (stat _)[7,9];
110    
111 root 1.3 if (0 > aio_load $path, $png) {
112     warn "$path: $!, skipping.\n";
113 root 1.5 next;
114 root 1.3 }
115    
116 root 1.6 # quickly extract width and height of the (necessarily PNG) image
117 root 1.3 unless ($png =~ /^\x89PNG\x0d\x0a\x1a\x0a....IHDR(........)/s) {
118     warn "$path: not a recongized png file, skipping.\n";
119 root 1.5 next;
120 root 1.3 }
121 root 1.2
122 root 1.3 my ($w, $h) = unpack "NN", $1;
123 root 1.2
124 root 1.3 (my $face = $path) =~ s/^.*\///;
125     my $T = 32;
126 root 1.2
127 root 1.3 unless ($face =~ s/\.base\.(...)\.png$/.$1/) {
128     warn "$path: weird filename, skipping.\n";
129 root 1.5 next;
130 root 1.3 }
131 root 1.2
132 root 1.3 if ($w < $T || $h < $T) {
133     warn "$path: too small ($w $h), skipping.\n";
134 root 1.5 next;
135 root 1.3 }
136    
137     if ($w % $T || $h % $T) {
138     warn "$path: weird png size ($w $h), skipping.\n";
139 root 1.5 next;
140 root 1.3 }
141 root 1.2
142 root 1.3 if (($w > $T || $h > $T) && $face !~ /_S\./) {
143     # split
144     my @tile;
145     for my $x (0 .. (int $w / $T) - 1) {
146     for my $y (0 .. (int $h / $T) - 1) {
147     my $file = "$path+$x+$y~";
148     aio_lstat $file;
149     push @tile, [$x, $y, $file, (stat _)[9]];
150     }
151 root 1.2 }
152    
153 root 1.3 my $mtime = (lstat $path)[9];
154     my @todo = grep { $_->[3] <= $mtime } @tile;
155     if (@todo) {
156     fork_sub {
157     open my $convert, "|-", $CONVERT,
158     "png:-",
159     (map {
160     (
161     "(",
162     "+clone",
163     -crop => (sprintf "%dx%d+%d+%d", $T, $T, $_->[0] * $T, $_->[1] * $T),
164 root 1.7 "+repage",
165 root 1.3 -write => "png:$_->[2]~",
166     "+delete",
167     ")",
168     )
169     } @todo),
170     "null:";
171    
172     binmode $convert;
173     print $convert $png;
174     close $convert;
175    
176     # pass 2, optimise, and rename
177     for (@todo) {
178     system $OPTIPNG, "-o5", "-i0", "-q", "$_->[2]~";
179     rename "$_->[2]~", $_->[2];
180     }
181     };
182 root 1.2 }
183    
184 root 1.3 for (@tile) {
185     my ($x, $y, $file) = @$_;
186     my $tile;
187    
188     if (0 > aio_load $file, $tile) {
189     die "$path: unable to read tile +$x+$y, aborting.\n";
190     }
191     IO::AIO::aio_unlink $file unless $CACHE;
192     commit_png $x|$y ? "$face+$x+$y" : $face, $tile;
193 root 1.2 }
194 root 1.3 } else {
195     # use as-is (either small, use smooth)
196     commit_png $face, $png;
197     }
198     }
199 root 1.2 }
200    
201 root 1.3 sub process_arc {
202     while (@arc) {
203     my ($dir, $file) = @{pop @arc};
204 root 1.2
205 root 1.3 my $arc;
206     aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/
207 root 1.4
208 root 1.2 my $arc = read_arch "$dir/$file";
209 root 1.4 for my $o (values %$arc) {
210     push @ARC, $o;
211 root 1.6
212 root 1.10 $o->{editor_folder} = $dir;
213    
214 root 1.6 my $visibility = delete $o->{visibility};
215     my $magicmap = delete $o->{magicmap};
216    
217     # find upper left corner :/
218     # omg, this is sooo broken
219 root 1.4 my ($dx, $dy);
220     for (my $o = $o; $o; $o = $o->{more}) {
221     $dx = $o->{x} if $o->{x} < $dx;
222     $dy = $o->{y} if $o->{y} < $dy;
223     }
224 root 1.6
225 root 1.4 for (my $o = $o; $o; $o = $o->{more}) {
226     my $x = $o->{x} - $dx;
227     my $y = $o->{y} - $dy;
228 root 1.6
229     my $ext = $x|$y ? "+$x+$y" : "";
230    
231 root 1.9 $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/;
232 root 1.6
233     my $visibility = delete $o->{visibility} if exists $o->{visibility};
234     my $magicmap = delete $o->{magicmap} if exists $o->{magicmap};
235    
236     my $anim = delete $o->{anim};
237    
238     if ($anim) {
239 root 1.8 $o->{animation} = "$o->{_name}";
240 root 1.6
241     for (@$anim) {
242 root 1.9 $_ .= $ext unless /^facings\s|^blank.x11$|^empty.x11$/;
243 root 1.6 }
244    
245     $ANIM{"$o->{_name}$ext"} =
246     join "", map "$_\n",
247 root 1.8 "anim $o->{_name}",
248 root 1.6 @$anim,
249     "mina";
250     }
251    
252     for my $face ($o->{face} || (), @{$anim || []}) {
253 root 1.9 next if $face =~ /^facings\s|^blank.x11$|^empty.x11$/;
254 root 1.6
255     my $info = $FACEINFO{$face} ||= {};
256    
257     $info->{visibility} = $visibility if defined $visibility;
258     $info->{magicmap} = $magicmap if defined $magicmap;
259 root 1.4 }
260 root 1.12
261     if (my $smooth = delete $o->{smoothface}) {
262 root 1.14 my ($face, $smooth) = split /\s+/, $smooth;
263     # skip empty_S.x11, it seems to server no purpose whatsoever
264     # but increases bandwidth demands and worse.
265     unless ($smooth eq "empty_S.x11") {
266     $FACEINFO{$face}{smooth} = $smooth;
267     }
268 root 1.12 }
269 root 1.4 }
270     }
271 root 1.3 }
272 root 1.2 }
273    
274 root 1.3 sub process_trs {
275     while (@trs) {
276     my ($dir, $file) = @{pop @trs};
277 root 1.12 my $path = "$dir/$file";
278    
279     my $trs;
280     if (0 > aio_load $path, $trs) {
281     warn "$path: $!, skipping.\n";
282     next;
283     }
284    
285     $TRS .= $trs;
286 root 1.3 }
287 root 1.2 }
288    
289 root 1.3 sub find_files;
290     sub find_files {
291 root 1.2 my ($path) = @_;
292    
293 root 1.3 IO::AIO::aioreq_pri 4;
294     IO::AIO::aio_scandir $path, 4, sub {
295 root 1.2 my ($dirs, $nondirs) = @_;
296    
297 root 1.3 find_files "$path/$_"
298 root 1.2 for grep $_ !~ /^(?:CVS|dev)$/, @$dirs;
299    
300     for my $file (@$nondirs) {
301     if ($file =~ /\.png$/) {
302 root 1.3 push @png, "$path/$file";
303 root 1.2 } elsif ($file =~ /\.trs$/) {
304 root 1.3 push @trs, [$path, $file];
305 root 1.2 } elsif ($file =~ /\.arc$/) {
306 root 1.3 push @arc, [$path, $file];
307 root 1.2 } else {
308     warn "ignoring $path/$file\n" if $VERBOSE >= 2;
309     }
310     }
311     };
312     }
313    
314     sub inst_arch($) {
315     my (undef, $path) = @_;
316    
317 root 1.13 print "installing '$path' to '$DATADIR'\n";
318 root 1.2
319     if (!-d "$path/treasures") {
320     warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n";
321     exit 1 unless $FORCE;
322     }
323    
324 root 1.3 find_files $path;
325 root 1.2 IO::AIO::flush;
326    
327 root 1.3 $_->join for (
328     (async \&process_png), (async \&process_png),
329     (async \&process_trs), (async \&process_trs),
330     (async \&process_arc), (async \&process_arc),
331     );
332    
333 root 1.5 {
334     open my $fh, ">:utf8", "$DATADIR/animations~"
335     or die "$DATADIR/animations~: $!";
336 root 1.6 print $fh join "", map $ANIM{$_}, sort keys %ANIM
337 root 1.5 }
338    
339     {
340     open my $fh, ">:utf8", "$DATADIR/archetypes~"
341     or die "$DATADIR/archetypes~: $!";
342 root 1.11 substr $_->{editor_folder}, 0, 1 + length $path, "" for @ARC;
343 root 1.5 print $fh Crossfire::archlist_to_string \@ARC;
344     }
345    
346 root 1.6 {
347 root 1.12 open my $fh, ">:utf8", "$DATADIR/treasures~"
348     or die "$DATADIR/treasures~: $!";
349     print $fh $TRS;
350     }
351    
352     {
353 root 1.6 while (my ($k, $v) = each %FACEINFO) {
354     $v->{data32} ||= delete $PNG32{$k};
355     }
356    
357     while (my ($k, $v) = each %FACEINFO) {
358 root 1.12 length $v->{data32} or warn "$k: face has no png32. this will not work (shoddy gcfclient will crash of course).\n";
359 root 1.5
360 root 1.6 $v->{chksum32} = Digest::MD5::md5 $v->{data32};
361     }
362    
363     open my $fh, ">:perlio", "$DATADIR/faces~"
364     or die "$DATADIR/faces~: $!";
365    
366 root 1.15 $FACEINFO{""} = { version => 1};
367 root 1.6 print $fh Storable::nfreeze \%FACEINFO;
368 root 1.5 }
369    
370 root 1.14 for (qw(archetypes faces animations treasures)) {
371 root 1.6 chmod 0644, "$DATADIR/$_~";
372 root 1.13 rename "$DATADIR/$_~", "$DATADIR/$_"
373     or die "$DATADIR/$_: $!";
374 root 1.6 }
375 root 1.13
376     print "archetype data installed successfully.\n";
377 root 1.2 }
378     }
379    
380     Getopt::Long::Configure ("bundling", "no_ignore_case");
381     GetOptions (
382     "verbose|v:+" => \$VERBOSE,
383 root 1.3 "cache" => \$CACHE,
384 root 1.2 "quiet|q" => sub { $VERBOSE = 0 },
385     "force" => sub { $FORCE = 1 },
386     "install-arch=s" => \&inst_arch,
387     "install-maps=s" => \&inst_maps,
388     "print-statedir" => sub { print "@pkgstatedir@\n" },
389     "print-datadir" => sub { print "$DATADIR\n" },
390     "print-confdir" => sub { print "@pkgconfdir@\n" },
391     "print-libdir" => sub { print "@libdir@/@PACKAGE@\n" },
392     "print-bindir" => sub { print "@bindir@/@PACKAGE@\n" },
393     ) or usage;
394