ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/utils/cfutil.in
Revision: 1.11
Committed: Mon Mar 12 14:55:43 2007 UTC (17 years, 2 months ago) by root
Branch: MAIN
Changes since 1.10: +1 -1 lines
Log Message:
*** empty log message ***

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