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.1 by root, Tue Mar 6 22:07:56 2007 UTC vs.
Revision 1.4 by root, Wed Mar 7 20:30:18 2007 UTC

1#!@PERL@ 1#!@PERL@
2 2
3@CONVERT@ 3use strict;
4
5my $prefix = "@prefix@";
6my $exec_prefix = "@exec_prefix@";
7my $datarootdir = "@datarootdir@";
8my $DATADIR = "@datadir@/@PACKAGE@";
9
10my $CONVERT = "@CONVERT@";
11my $IDENTIFY = "@IDENTIFY@";
12my $OPTIPNG = "@OPTIPNG@";
13my $RSYNC = "@RSYNC@";
14
15use Getopt::Long;
16use Coro::Event;
17use AnyEvent;
18use IO::AIO ();
19use File::Temp;
20use Crossfire;
21use Coro;
22use Coro::AIO;
23use POSIX ();
24
25sub usage {
26 warn <<EOF;
27Usage: cfutil [-v] [-q] [--force] [--cache]
28 [--install-arch path]
29 [--install-maps maps]
30 [--print-statedir]
31 [--print-confdir]
32 [--print-datadir]
33 [--print-libdir]
34 [--print-bindir]
35EOF
36 exit 1;
37}
38
39my $VERBOSE = 1;
40my $CACHE = 0;
41my $FORCE;
42my $TMPDIR = "/tmp/cfutil$$~";
43my $TMPFILE = "aaaa0";
44
45END { system "rm", "-rf", $TMPDIR }
46
47Event->signal (signal => "INT", cb => sub { exit 1 });
48Event->signal (signal => "TERM", cb => sub { exit 1 });
49
50mkdir $TMPDIR, 0700
51 or die "$TMPDIR: $!";
52
53sub fork_sub(&) {
54 my ($cb) = @_;
55
56 if (my $pid = fork) {
57 my $current = $Coro::current;
58 my $w = AnyEvent->child (pid => $pid, cb => sub { $current->ready });
59 Coro::schedule;
60 } else {
61 eval { $cb->() };
62 POSIX::_exit 0 unless $@;
63 warn $@;
64 POSIX::_exit 1;
65 }
66}
67
68sub inst_maps($) {
69 my (undef, $path) = @_;
70
71 print "installing '$path' to '$DATADIR/maps'\n\n";
72
73 if (!-f "$path/regions") {
74 warn "'$path' does not look like a maps directory ('regions' file is missing).\n";
75 exit 1 unless $FORCE;
76 }
77
78 system $RSYNC, "-av", "$path/.", "$DATADIR/maps/.", "--delete", "--exclude", "CVS", "--delete-excluded";
79}
80
81{
82 our @PNG;
83 our @ARC;
84 our $NFILE;
85
86 our (@png, @trs, @arc); # files we are interested in
87
88 sub commit_png {
89 my ($name, $data) = @_;
90 #warn "$name: commited\n";
91 }
92
93 sub process_png {
94 while (@png) {
95 my $path = pop @png;
96
97 my $png;
98 aio_lstat $path;
99 my ($size, $mtime) = (stat _)[7,9];
100
101 if (0 > aio_load $path, $png) {
102 warn "$path: $!, skipping.\n";
103 return;
104 }
105
106 # quickly extratc width and height of the (necessarily PNG) image
107 unless ($png =~ /^\x89PNG\x0d\x0a\x1a\x0a....IHDR(........)/s) {
108 warn "$path: not a recongized png file, skipping.\n";
109 return;
110 }
111
112 my ($w, $h) = unpack "NN", $1;
113
114 (my $face = $path) =~ s/^.*\///;
115 my $T = 32;
116
117 unless ($face =~ s/\.base\.(...)\.png$/.$1/) {
118 warn "$path: weird filename, skipping.\n";
119 return;
120 }
121
122 if ($w < $T || $h < $T) {
123 warn "$path: too small ($w $h), skipping.\n";
124 return;
125 }
126
127 if ($w % $T || $h % $T) {
128 warn "$path: weird png size ($w $h), skipping.\n";
129 return;
130 }
131
132 if (($w > $T || $h > $T) && $face !~ /_S\./) {
133 # split
134 my @tile;
135 for my $x (0 .. (int $w / $T) - 1) {
136 for my $y (0 .. (int $h / $T) - 1) {
137 my $file = "$path+$x+$y~";
138 aio_lstat $file;
139 push @tile, [$x, $y, $file, (stat _)[9]];
140 }
141 }
142
143 my $mtime = (lstat $path)[9];
144 my @todo = grep { $_->[3] <= $mtime } @tile;
145 if (@todo) {
146 fork_sub {
147 open my $convert, "|-", $CONVERT,
148 "png:-",
149 (map {
150 (
151 "(",
152 "+clone",
153 -crop => (sprintf "%dx%d+%d+%d", $T, $T, $_->[0] * $T, $_->[1] * $T),
154 -write => "png:$_->[2]~",
155 "+delete",
156 ")",
157 )
158 } @todo),
159 "null:";
160
161 binmode $convert;
162 print $convert $png;
163 close $convert;
164
165 # pass 2, optimise, and rename
166 for (@todo) {
167 system $OPTIPNG, "-o5", "-i0", "-q", "$_->[2]~";
168 rename "$_->[2]~", $_->[2];
169 }
170 };
171 }
172
173 for (@tile) {
174 my ($x, $y, $file) = @$_;
175 my $tile;
176
177 if (0 > aio_load $file, $tile) {
178 die "$path: unable to read tile +$x+$y, aborting.\n";
179 }
180 IO::AIO::aio_unlink $file unless $CACHE;
181 commit_png $x|$y ? "$face+$x+$y" : $face, $tile;
182 }
183 } else {
184 # use as-is (either small, use smooth)
185 commit_png $face, $png;
186 }
187 }
188 }
189
190 sub process_arc {
191 while (@arc) {
192 my ($dir, $file) = @{pop @arc};
193
194 my $arc;
195 aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/
196
197 my $arc = read_arch "$dir/$file";
198 for my $o (values %$arc) {
199 push @ARC, $o;
200 my ($dx, $dy);
201 # omg, this is sooo broken
202 for (my $o = $o; $o; $o = $o->{more}) {
203 $dx = $o->{x} if $o->{x} < $dx;
204 $dy = $o->{y} if $o->{y} < $dy;
205 }
206 for (my $o = $o; $o; $o = $o->{more}) {
207 my $x = $o->{x} - $dx;
208 my $y = $o->{y} - $dy;
209 if ($x|$y) {
210 $_ .= "+$x+$y" for $o->{face}, @{$o->{anim} || []};
211 }
212 }
213 }
214 }
215 }
216
217 sub process_trs {
218 while (@trs) {
219 my ($dir, $file) = @{pop @trs};
220 }
221 }
222
223 sub find_files;
224 sub find_files {
225 my ($path) = @_;
226
227 IO::AIO::aioreq_pri 4;
228 IO::AIO::aio_scandir $path, 4, sub {
229 my ($dirs, $nondirs) = @_;
230
231 find_files "$path/$_"
232 for grep $_ !~ /^(?:CVS|dev)$/, @$dirs;
233
234 for my $file (@$nondirs) {
235 if ($file =~ /\.png$/) {
236 push @png, "$path/$file";
237 } elsif ($file =~ /\.trs$/) {
238 push @trs, [$path, $file];
239 } elsif ($file =~ /\.arc$/) {
240 push @arc, [$path, $file];
241 } else {
242 warn "ignoring $path/$file\n" if $VERBOSE >= 2;
243 }
244 }
245 };
246 }
247
248 sub inst_arch($) {
249 my (undef, $path) = @_;
250
251 print "installing '$path' to '$DATADIR'\n\n";
252
253 if (!-d "$path/treasures") {
254 warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n";
255 exit 1 unless $FORCE;
256 }
257
258 find_files $path;
259 IO::AIO::flush;
260
261 $_->join for (
262 (async \&process_png), (async \&process_png),
263 (async \&process_trs), (async \&process_trs),
264 (async \&process_arc), (async \&process_arc),
265 );
266
267 open my $archetypes, ">:utf8", "$DATADIR/archetypes~"
268 or die "$DATADIR/archetypes~: $!";
269 print $archetypes Crossfire::archlist_to_string \@ARC;
270 close $archetypes;
271 rename "$DATADIR/archetypes~", "$DATADIR/archetypes";
272
273 die "--install-arch not fully implemented\n";
274 }
275}
276
277Getopt::Long::Configure ("bundling", "no_ignore_case");
278GetOptions (
279 "verbose|v:+" => \$VERBOSE,
280 "cache" => \$CACHE,
281 "quiet|q" => sub { $VERBOSE = 0 },
282 "force" => sub { $FORCE = 1 },
283 "install-arch=s" => \&inst_arch,
284 "install-maps=s" => \&inst_maps,
285 "print-statedir" => sub { print "@pkgstatedir@\n" },
286 "print-datadir" => sub { print "$DATADIR\n" },
287 "print-confdir" => sub { print "@pkgconfdir@\n" },
288 "print-libdir" => sub { print "@libdir@/@PACKAGE@\n" },
289 "print-bindir" => sub { print "@bindir@/@PACKAGE@\n" },
290) or usage;
291

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines