ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/utils/cfutil.in
Revision: 1.13
Committed: Mon Mar 12 17:33:12 2007 UTC (17 years, 2 months ago) by root
Branch: MAIN
Changes since 1.12: +10 -4 lines
Log Message:
refine cfutil a bit

File Contents

# Content
1 #!@PERL@
2
3 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 my $OPTIPNG = "@OPTIPNG@";
13 my $RSYNC = "@RSYNC@";
14
15 use Getopt::Long;
16 use Coro::Event;
17 use AnyEvent;
18 use IO::AIO ();
19 use File::Temp;
20 use Crossfire;
21 use Coro;
22 use Coro::AIO;
23 use POSIX ();
24 use Digest::MD5;
25
26 sub usage {
27 warn <<EOF;
28 Usage: cfutil [-v] [-q] [--force] [--cache]
29 [--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 my $CACHE = 0;
42 my $FORCE;
43 my $TMPDIR = "/tmp/cfutil$$~";
44 my $TMPFILE = "aaaa0";
45
46 END { system "rm", "-rf", $TMPDIR }
47
48 Event->signal (signal => "INT", cb => sub { exit 1 });
49 Event->signal (signal => "TERM", cb => sub { exit 1 });
50
51 mkdir $TMPDIR, 0700
52 or die "$TMPDIR: $!";
53
54 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 sub inst_maps($) {
70 my (undef, $path) = @_;
71
72 print "installing '$path' to '$DATADIR/maps'\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 or die "map installation failed.\n";
81
82 print "maps installed successfully.\n";
83 }
84
85 {
86 our %PNG32;
87 our %FACEINFO;
88 our @ARC;
89 our $TRS;
90 our $NFILE;
91 our %ANIM;
92 our $SMOOTH;
93
94 our (@png, @trs, @arc); # files we are interested in
95
96 sub commit_png {
97 my ($name, $data) = @_;
98
99 $PNG32{$name} = $data;
100 $FACEINFO{$name} ||= {};
101 }
102
103 sub process_png {
104 while (@png) {
105 my $path = pop @png;
106
107 my $png;
108 aio_lstat $path;
109 my ($size, $mtime) = (stat _)[7,9];
110
111 if (0 > aio_load $path, $png) {
112 warn "$path: $!, skipping.\n";
113 next;
114 }
115
116 # quickly extract width and height of the (necessarily PNG) image
117 unless ($png =~ /^\x89PNG\x0d\x0a\x1a\x0a....IHDR(........)/s) {
118 warn "$path: not a recongized png file, skipping.\n";
119 next;
120 }
121
122 my ($w, $h) = unpack "NN", $1;
123
124 (my $face = $path) =~ s/^.*\///;
125 my $T = 32;
126
127 unless ($face =~ s/\.base\.(...)\.png$/.$1/) {
128 warn "$path: weird filename, skipping.\n";
129 next;
130 }
131
132 if ($w < $T || $h < $T) {
133 warn "$path: too small ($w $h), skipping.\n";
134 next;
135 }
136
137 if ($w % $T || $h % $T) {
138 warn "$path: weird png size ($w $h), skipping.\n";
139 next;
140 }
141
142 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 }
152
153 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 "+repage",
165 -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 }
183
184 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 }
194 } else {
195 # use as-is (either small, use smooth)
196 commit_png $face, $png;
197 }
198 }
199 }
200
201 sub process_arc {
202 while (@arc) {
203 my ($dir, $file) = @{pop @arc};
204
205 my $arc;
206 aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/
207
208 my $arc = read_arch "$dir/$file";
209 for my $o (values %$arc) {
210 push @ARC, $o;
211
212 $o->{editor_folder} = $dir;
213
214 my $visibility = delete $o->{visibility};
215 my $magicmap = delete $o->{magicmap};
216
217 # find upper left corner :/
218 # omg, this is sooo broken
219 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
225 for (my $o = $o; $o; $o = $o->{more}) {
226 my $x = $o->{x} - $dx;
227 my $y = $o->{y} - $dy;
228
229 my $ext = $x|$y ? "+$x+$y" : "";
230
231 $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/;
232
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 $o->{animation} = "$o->{_name}";
240
241 for (@$anim) {
242 $_ .= $ext unless /^facings\s|^blank.x11$|^empty.x11$/;
243 }
244
245 $ANIM{"$o->{_name}$ext"} =
246 join "", map "$_\n",
247 "anim $o->{_name}",
248 @$anim,
249 "mina";
250 }
251
252 for my $face ($o->{face} || (), @{$anim || []}) {
253 next if $face =~ /^facings\s|^blank.x11$|^empty.x11$/;
254
255 my $info = $FACEINFO{$face} ||= {};
256
257 $info->{visibility} = $visibility if defined $visibility;
258 $info->{magicmap} = $magicmap if defined $magicmap;
259 }
260
261 if (my $smooth = delete $o->{smoothface}) {
262 $SMOOTH .= "$smooth\n";
263 }
264 }
265 }
266 }
267 }
268
269 sub process_trs {
270 while (@trs) {
271 my ($dir, $file) = @{pop @trs};
272 my $path = "$dir/$file";
273
274 my $trs;
275 if (0 > aio_load $path, $trs) {
276 warn "$path: $!, skipping.\n";
277 next;
278 }
279
280 $TRS .= $trs;
281 }
282 }
283
284 sub find_files;
285 sub find_files {
286 my ($path) = @_;
287
288 IO::AIO::aioreq_pri 4;
289 IO::AIO::aio_scandir $path, 4, sub {
290 my ($dirs, $nondirs) = @_;
291
292 find_files "$path/$_"
293 for grep $_ !~ /^(?:CVS|dev)$/, @$dirs;
294
295 for my $file (@$nondirs) {
296 if ($file =~ /\.png$/) {
297 push @png, "$path/$file";
298 } elsif ($file =~ /\.trs$/) {
299 push @trs, [$path, $file];
300 } elsif ($file =~ /\.arc$/) {
301 push @arc, [$path, $file];
302 } else {
303 warn "ignoring $path/$file\n" if $VERBOSE >= 2;
304 }
305 }
306 };
307 }
308
309 sub inst_arch($) {
310 my (undef, $path) = @_;
311
312 print "installing '$path' to '$DATADIR'\n";
313
314 if (!-d "$path/treasures") {
315 warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n";
316 exit 1 unless $FORCE;
317 }
318
319 find_files $path;
320 IO::AIO::flush;
321
322 $_->join for (
323 (async \&process_png), (async \&process_png),
324 (async \&process_trs), (async \&process_trs),
325 (async \&process_arc), (async \&process_arc),
326 );
327
328 {
329 open my $fh, ">:utf8", "$DATADIR/animations~"
330 or die "$DATADIR/animations~: $!";
331 print $fh join "", map $ANIM{$_}, sort keys %ANIM
332 }
333
334 {
335 open my $fh, ">:utf8", "$DATADIR/archetypes~"
336 or die "$DATADIR/archetypes~: $!";
337 substr $_->{editor_folder}, 0, 1 + length $path, "" for @ARC;
338 print $fh Crossfire::archlist_to_string \@ARC;
339 }
340
341 {
342 open my $fh, ">:utf8", "$DATADIR/smooth~"
343 or die "$DATADIR/smooth~: $!";
344 print $fh $SMOOTH;
345 }
346
347 {
348 open my $fh, ">:utf8", "$DATADIR/treasures~"
349 or die "$DATADIR/treasures~: $!";
350 print $fh $TRS;
351 }
352
353 {
354 while (my ($k, $v) = each %FACEINFO) {
355 $v->{data32} ||= delete $PNG32{$k};
356 }
357
358 while (my ($k, $v) = each %FACEINFO) {
359 length $v->{data32} or warn "$k: face has no png32. this will not work (shoddy gcfclient will crash of course).\n";
360
361 $v->{chksum32} = Digest::MD5::md5 $v->{data32};
362 }
363
364 open my $fh, ">:perlio", "$DATADIR/faces~"
365 or die "$DATADIR/faces~: $!";
366
367 print $fh Storable::nfreeze \%FACEINFO;
368 }
369
370 for (qw(archetypes faces animations treasures smooth)) {
371 chmod 0644, "$DATADIR/$_~";
372 rename "$DATADIR/$_~", "$DATADIR/$_"
373 or die "$DATADIR/$_: $!";
374 }
375
376 print "archetype data installed successfully.\n";
377 }
378 }
379
380 Getopt::Long::Configure ("bundling", "no_ignore_case");
381 GetOptions (
382 "verbose|v:+" => \$VERBOSE,
383 "cache" => \$CACHE,
384 "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