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.11 by root, Mon Mar 12 14:55:43 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 ();
24use Digest::MD5;
18 25
19sub usage { 26sub usage {
20 warn <<EOF; 27 warn <<EOF;
21Usage: cfutil [-v] [-q] [--force] 28Usage: cfutil [-v] [-q] [--force] [--cache]
22 [--install-arch path] 29 [--install-arch path]
23 [--install-maps maps] 30 [--install-maps maps]
24 [--print-statedir] 31 [--print-statedir]
25 [--print-confdir] 32 [--print-confdir]
26 [--print-datadir] 33 [--print-datadir]
29EOF 36EOF
30 exit 1; 37 exit 1;
31} 38}
32 39
33my $VERBOSE = 1; 40my $VERBOSE = 1;
41my $CACHE = 0;
34my $FORCE; 42my $FORCE;
35my $TMPDIR = "/tmp/cfutil$$~"; 43my $TMPDIR = "/tmp/cfutil$$~";
44my $TMPFILE = "aaaa0";
36 45
37END { system "rm", "-rf", $TMPDIR } 46END { system "rm", "-rf", $TMPDIR }
47
48Event->signal (signal => "INT", cb => sub { exit 1 });
49Event->signal (signal => "TERM", cb => sub { exit 1 });
38 50
39mkdir $TMPDIR, 0700 51mkdir $TMPDIR, 0700
40 or die "$TMPDIR: $!"; 52 or die "$TMPDIR: $!";
53
54sub 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}
41 68
42sub inst_maps($) { 69sub inst_maps($) {
43 my (undef, $path) = @_; 70 my (undef, $path) = @_;
44 71
45 print "installing '$path' to '$DATADIR/maps'\n\n"; 72 print "installing '$path' to '$DATADIR/maps'\n\n";
51 78
52 system $RSYNC, "-av", "$path/.", "$DATADIR/maps/.", "--delete", "--exclude", "CVS", "--delete-excluded"; 79 system $RSYNC, "-av", "$path/.", "$DATADIR/maps/.", "--delete", "--exclude", "CVS", "--delete-excluded";
53} 80}
54 81
55{ 82{
56 my @PNG; 83 our %PNG32;
84 our %FACEINFO;
57 my @ARC; 85 our @ARC;
86 our $NFILE;
87 our %ANIM;
88
89 our (@png, @trs, @arc); # files we are interested in
58 90
59 sub commit_png { 91 sub commit_png {
60 my ($name, $data) = @_; 92 my ($name, $data) = @_;
61 #warn "$name: commited\n";
62 }
63 93
64 sub add_png($) { 94 $PNG32{$name} = $data;
95 $FACEINFO{$name} ||= {};
96 }
97
98 sub process_png {
99 while (@png) {
100 my $path = pop @png;
101
102 my $png;
103 aio_lstat $path;
104 my ($size, $mtime) = (stat _)[7,9];
105
106 if (0 > aio_load $path, $png) {
107 warn "$path: $!, skipping.\n";
108 next;
109 }
110
111 # quickly extract width and height of the (necessarily PNG) image
112 unless ($png =~ /^\x89PNG\x0d\x0a\x1a\x0a....IHDR(........)/s) {
113 warn "$path: not a recongized png file, skipping.\n";
114 next;
115 }
116
117 my ($w, $h) = unpack "NN", $1;
118
119 (my $face = $path) =~ s/^.*\///;
120 my $T = 32;
121
122 unless ($face =~ s/\.base\.(...)\.png$/.$1/) {
123 warn "$path: weird filename, skipping.\n";
124 next;
125 }
126
127 if ($w < $T || $h < $T) {
128 warn "$path: too small ($w $h), skipping.\n";
129 next;
130 }
131
132 if ($w % $T || $h % $T) {
133 warn "$path: weird png size ($w $h), skipping.\n";
134 next;
135 }
136
137 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 }
147
148 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 "+repage",
160 -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 }
178
179 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 }
189 } else {
190 # use as-is (either small, use smooth)
191 commit_png $face, $png;
192 }
193 }
194 }
195
196 sub process_arc {
197 while (@arc) {
198 my ($dir, $file) = @{pop @arc};
199
200 my $arc;
201 aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/
202
203 my $arc = read_arch "$dir/$file";
204 for my $o (values %$arc) {
205 push @ARC, $o;
206
207 $o->{editor_folder} = $dir;
208
209 my $visibility = delete $o->{visibility};
210 my $magicmap = delete $o->{magicmap};
211
212 # find upper left corner :/
213 # omg, this is sooo broken
214 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
220 for (my $o = $o; $o; $o = $o->{more}) {
221 my $x = $o->{x} - $dx;
222 my $y = $o->{y} - $dy;
223
224 my $ext = $x|$y ? "+$x+$y" : "";
225
226 $o->{face} .= $ext unless /^blank.x11$|^empty.x11$/;
227
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 $o->{animation} = "$o->{_name}";
235
236 for (@$anim) {
237 $_ .= $ext unless /^facings\s|^blank.x11$|^empty.x11$/;
238 }
239
240 $ANIM{"$o->{_name}$ext"} =
241 join "", map "$_\n",
242 "anim $o->{_name}",
243 @$anim,
244 "mina";
245 }
246
247 for my $face ($o->{face} || (), @{$anim || []}) {
248 next if $face =~ /^facings\s|^blank.x11$|^empty.x11$/;
249
250 my $info = $FACEINFO{$face} ||= {};
251
252 $info->{visibility} = $visibility if defined $visibility;
253 $info->{magicmap} = $magicmap if defined $magicmap;
254 }
255 }
256 }
257 }
258 }
259
260 sub process_trs {
261 while (@trs) {
262 my ($dir, $file) = @{pop @trs};
263 }
264 }
265
266 sub find_files;
267 sub find_files {
65 my ($path) = @_; 268 my ($path) = @_;
66 269
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; 270 IO::AIO::aioreq_pri 4;
134 aio_scandir $path, 4, sub { 271 IO::AIO::aio_scandir $path, 4, sub {
135 my ($dirs, $nondirs) = @_; 272 my ($dirs, $nondirs) = @_;
136 273
137 collect_arch "$path/$_" 274 find_files "$path/$_"
138 for grep $_ !~ /^(?:CVS|dev)$/, @$dirs; 275 for grep $_ !~ /^(?:CVS|dev)$/, @$dirs;
139 276
140 for my $file (@$nondirs) { 277 for my $file (@$nondirs) {
141 if ($file =~ /\.png$/) { 278 if ($file =~ /\.png$/) {
142 add_png "$path/$file"; 279 push @png, "$path/$file";
143 } elsif ($file =~ /\.trs$/) { 280 } elsif ($file =~ /\.trs$/) {
144 add_trs $path, $file; 281 push @trs, [$path, $file];
145 } elsif ($file =~ /\.arc$/) { 282 } elsif ($file =~ /\.arc$/) {
146 add_arc $path, $file; 283 push @arc, [$path, $file];
147 } else { 284 } else {
148 warn "ignoring $path/$file\n" if $VERBOSE >= 2; 285 warn "ignoring $path/$file\n" if $VERBOSE >= 2;
149 } 286 }
150 } 287 }
151 }; 288 };
159 if (!-d "$path/treasures") { 296 if (!-d "$path/treasures") {
160 warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; 297 warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n";
161 exit 1 unless $FORCE; 298 exit 1 unless $FORCE;
162 } 299 }
163 300
164 collect_arch $path; 301 find_files $path;
165 IO::AIO::flush; 302 IO::AIO::flush;
166 303
304 $_->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 {
311 open my $fh, ">:utf8", "$DATADIR/animations~"
312 or die "$DATADIR/animations~: $!";
313 print $fh join "", map $ANIM{$_}, sort keys %ANIM
314 }
315
316 {
317 open my $fh, ">:utf8", "$DATADIR/archetypes~"
318 or die "$DATADIR/archetypes~: $!";
319 substr $_->{editor_folder}, 0, 1 + length $path, "" for @ARC;
320 print $fh Crossfire::archlist_to_string \@ARC;
321 }
322
323 {
324 while (my ($k, $v) = each %FACEINFO) {
325 $v->{data32} ||= delete $PNG32{$k};
326 }
327
328 while (my ($k, $v) = each %FACEINFO) {
329 length $v->{data32} or warn "$k: face has no png32. this will crash the server.\n";
330
331 $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 }
341
342 for (qw(archetypes faces animations)) {
343 chmod 0644, "$DATADIR/$_~";
344 rename "$DATADIR/$_~", "$DATADIR/$_";
345 }
346
167 die "--install-arch not yet implemented\n"; 347 die "--install-arch not fully implemented\n";
168 } 348 }
169} 349}
170 350
171Getopt::Long::Configure ("bundling", "no_ignore_case"); 351Getopt::Long::Configure ("bundling", "no_ignore_case");
172GetOptions ( 352GetOptions (
173 "verbose|v:+" => \$VERBOSE, 353 "verbose|v:+" => \$VERBOSE,
354 "cache" => \$CACHE,
174 "quiet|q" => sub { $VERBOSE = 0 }, 355 "quiet|q" => sub { $VERBOSE = 0 },
175 "force" => sub { $FORCE = 1 }, 356 "force" => sub { $FORCE = 1 },
176 "install-arch=s" => \&inst_arch, 357 "install-arch=s" => \&inst_arch,
177 "install-maps=s" => \&inst_maps, 358 "install-maps=s" => \&inst_maps,
178 "print-statedir" => sub { print "@pkgstatedir@\n" }, 359 "print-statedir" => sub { print "@pkgstatedir@\n" },

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines