ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/utils/cfutil.in
Revision: 1.6
Committed: Sun Mar 11 02:12:45 2007 UTC (17 years, 2 months ago) by root
Branch: MAIN
Changes since 1.5: +66 -26 lines
Log Message:
- MAJOR CHANGE
- you now need to use cfutil to install arches.
- former bigfaces are broken in the server
- bigfaces are no longer supported. at all.
- use face numbers instead of pointers
  * saves lotsa space
  * saves lotsa indirections
  * saves lots(?) cpu cycles
- completely rewrote face handling
- faces can now be added at runtime
- reload will add new faces
- this does not apply to animations
- use a hastable instead of binary search (faster) for faces
- face caching is broken
- facesets are gone
- server always reports MAX_FACES to any client who asks

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     -write => "png:$_->[2]~",
160     "+delete",
161     ")",
162     )
163     } @todo),
164     "null:";
165    
166     binmode $convert;
167     print $convert $png;
168     close $convert;
169    
170     # pass 2, optimise, and rename
171     for (@todo) {
172     system $OPTIPNG, "-o5", "-i0", "-q", "$_->[2]~";
173     rename "$_->[2]~", $_->[2];
174     }
175     };
176 root 1.2 }
177    
178 root 1.3 for (@tile) {
179     my ($x, $y, $file) = @$_;
180     my $tile;
181    
182     if (0 > aio_load $file, $tile) {
183     die "$path: unable to read tile +$x+$y, aborting.\n";
184     }
185     IO::AIO::aio_unlink $file unless $CACHE;
186     commit_png $x|$y ? "$face+$x+$y" : $face, $tile;
187 root 1.2 }
188 root 1.3 } else {
189     # use as-is (either small, use smooth)
190     commit_png $face, $png;
191     }
192     }
193 root 1.2 }
194    
195 root 1.3 sub process_arc {
196     while (@arc) {
197     my ($dir, $file) = @{pop @arc};
198 root 1.2
199 root 1.3 my $arc;
200     aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/
201 root 1.4
202 root 1.2 my $arc = read_arch "$dir/$file";
203 root 1.4 for my $o (values %$arc) {
204     push @ARC, $o;
205 root 1.6
206     my $visibility = delete $o->{visibility};
207     my $magicmap = delete $o->{magicmap};
208    
209     # find upper left corner :/
210     # omg, this is sooo broken
211 root 1.4 my ($dx, $dy);
212     for (my $o = $o; $o; $o = $o->{more}) {
213     $dx = $o->{x} if $o->{x} < $dx;
214     $dy = $o->{y} if $o->{y} < $dy;
215     }
216 root 1.6
217 root 1.4 for (my $o = $o; $o; $o = $o->{more}) {
218     my $x = $o->{x} - $dx;
219     my $y = $o->{y} - $dy;
220 root 1.6
221     my $ext = $x|$y ? "+$x+$y" : "";
222    
223     $o->{face} .= $ext;
224    
225     my $visibility = delete $o->{visibility} if exists $o->{visibility};
226     my $magicmap = delete $o->{magicmap} if exists $o->{magicmap};
227    
228     my $anim = delete $o->{anim};
229    
230     if ($anim) {
231     $o->{animation} = "$o->{_name}$ext";
232    
233     for (@$anim) {
234     $_ .= $ext unless /^facings\s/;
235     }
236    
237     $ANIM{"$o->{_name}$ext"} =
238     join "", map "$_\n",
239     "anim $o->{_name}$ext",
240     @$anim,
241     "mina";
242     }
243    
244     for my $face ($o->{face} || (), @{$anim || []}) {
245     next if /^facings\s/;
246    
247     my $info = $FACEINFO{$face} ||= {};
248    
249     $info->{visibility} = $visibility if defined $visibility;
250     $info->{magicmap} = $magicmap if defined $magicmap;
251 root 1.4 }
252     }
253     }
254 root 1.3 }
255 root 1.2 }
256    
257 root 1.3 sub process_trs {
258     while (@trs) {
259     my ($dir, $file) = @{pop @trs};
260     }
261 root 1.2 }
262    
263 root 1.3 sub find_files;
264     sub find_files {
265 root 1.2 my ($path) = @_;
266    
267 root 1.3 IO::AIO::aioreq_pri 4;
268     IO::AIO::aio_scandir $path, 4, sub {
269 root 1.2 my ($dirs, $nondirs) = @_;
270    
271 root 1.3 find_files "$path/$_"
272 root 1.2 for grep $_ !~ /^(?:CVS|dev)$/, @$dirs;
273    
274     for my $file (@$nondirs) {
275     if ($file =~ /\.png$/) {
276 root 1.3 push @png, "$path/$file";
277 root 1.2 } elsif ($file =~ /\.trs$/) {
278 root 1.3 push @trs, [$path, $file];
279 root 1.2 } elsif ($file =~ /\.arc$/) {
280 root 1.3 push @arc, [$path, $file];
281 root 1.2 } else {
282     warn "ignoring $path/$file\n" if $VERBOSE >= 2;
283     }
284     }
285     };
286     }
287    
288     sub inst_arch($) {
289     my (undef, $path) = @_;
290    
291     print "installing '$path' to '$DATADIR'\n\n";
292    
293     if (!-d "$path/treasures") {
294     warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n";
295     exit 1 unless $FORCE;
296     }
297    
298 root 1.3 find_files $path;
299 root 1.2 IO::AIO::flush;
300    
301 root 1.3 $_->join for (
302     (async \&process_png), (async \&process_png),
303     (async \&process_trs), (async \&process_trs),
304     (async \&process_arc), (async \&process_arc),
305     );
306    
307 root 1.5 {
308     open my $fh, ">:utf8", "$DATADIR/animations~"
309     or die "$DATADIR/animations~: $!";
310 root 1.6 print $fh join "", map $ANIM{$_}, sort keys %ANIM
311 root 1.5 }
312    
313     {
314     open my $fh, ">:utf8", "$DATADIR/archetypes~"
315     or die "$DATADIR/archetypes~: $!";
316     print $fh Crossfire::archlist_to_string \@ARC;
317     }
318    
319 root 1.6 {
320     while (my ($k, $v) = each %FACEINFO) {
321     $v->{data32} ||= delete $PNG32{$k};
322     }
323    
324     while (my ($k, $v) = each %FACEINFO) {
325     exists $v->{data32} or warn "$k: face has no png32. this will crash the server.\n";
326 root 1.5
327 root 1.6 $v->{chksum32} = Digest::MD5::md5 $v->{data32};
328     }
329    
330     open my $fh, ">:perlio", "$DATADIR/faces~"
331     or die "$DATADIR/faces~: $!";
332    
333     print $fh Storable::nfreeze \%FACEINFO;
334    
335     #use PApp::Util; warn PApp::Util::dumpval \%FACEINFO;
336 root 1.5 }
337    
338 root 1.6 for (qw(archetypes faces animations)) {
339     chmod 0644, "$DATADIR/$_~";
340     rename "$DATADIR/$_~", "$DATADIR/$_";
341     }
342 root 1.4
343     die "--install-arch not fully implemented\n";
344 root 1.2 }
345     }
346    
347     Getopt::Long::Configure ("bundling", "no_ignore_case");
348     GetOptions (
349     "verbose|v:+" => \$VERBOSE,
350 root 1.3 "cache" => \$CACHE,
351 root 1.2 "quiet|q" => sub { $VERBOSE = 0 },
352     "force" => sub { $FORCE = 1 },
353     "install-arch=s" => \&inst_arch,
354     "install-maps=s" => \&inst_maps,
355     "print-statedir" => sub { print "@pkgstatedir@\n" },
356     "print-datadir" => sub { print "$DATADIR\n" },
357     "print-confdir" => sub { print "@pkgconfdir@\n" },
358     "print-libdir" => sub { print "@libdir@/@PACKAGE@\n" },
359     "print-bindir" => sub { print "@bindir@/@PACKAGE@\n" },
360     ) or usage;
361