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.2 by root, Wed Mar 7 01:23:37 2007 UTC vs.
Revision 1.5 by root, Thu Mar 8 15:19:08 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines