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\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 |
our %PNG32; |
84 |
our %FACEINFO; |
85 |
our @ARC; |
86 |
our $NFILE; |
87 |
our %ANIM; |
88 |
|
89 |
our (@png, @trs, @arc); # files we are interested in |
90 |
|
91 |
sub commit_png { |
92 |
my ($name, $data) = @_; |
93 |
|
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 |
my $visibility = delete $o->{visibility}; |
208 |
my $magicmap = delete $o->{magicmap}; |
209 |
|
210 |
# find upper left corner :/ |
211 |
# omg, this is sooo broken |
212 |
my ($dx, $dy); |
213 |
for (my $o = $o; $o; $o = $o->{more}) { |
214 |
$dx = $o->{x} if $o->{x} < $dx; |
215 |
$dy = $o->{y} if $o->{y} < $dy; |
216 |
} |
217 |
|
218 |
for (my $o = $o; $o; $o = $o->{more}) { |
219 |
my $x = $o->{x} - $dx; |
220 |
my $y = $o->{y} - $dy; |
221 |
|
222 |
my $ext = $x|$y ? "+$x+$y" : ""; |
223 |
|
224 |
$o->{face} .= $ext; |
225 |
|
226 |
my $visibility = delete $o->{visibility} if exists $o->{visibility}; |
227 |
my $magicmap = delete $o->{magicmap} if exists $o->{magicmap}; |
228 |
|
229 |
my $anim = delete $o->{anim}; |
230 |
|
231 |
if ($anim) { |
232 |
$o->{animation} = "$o->{_name}$ext"; |
233 |
|
234 |
for (@$anim) { |
235 |
$_ .= $ext unless /^facings\s/; |
236 |
} |
237 |
|
238 |
$ANIM{"$o->{_name}$ext"} = |
239 |
join "", map "$_\n", |
240 |
"anim $o->{_name}$ext", |
241 |
@$anim, |
242 |
"mina"; |
243 |
} |
244 |
|
245 |
for my $face ($o->{face} || (), @{$anim || []}) { |
246 |
next if /^facings\s/; |
247 |
|
248 |
my $info = $FACEINFO{$face} ||= {}; |
249 |
|
250 |
$info->{visibility} = $visibility if defined $visibility; |
251 |
$info->{magicmap} = $magicmap if defined $magicmap; |
252 |
} |
253 |
} |
254 |
} |
255 |
} |
256 |
} |
257 |
|
258 |
sub process_trs { |
259 |
while (@trs) { |
260 |
my ($dir, $file) = @{pop @trs}; |
261 |
} |
262 |
} |
263 |
|
264 |
sub find_files; |
265 |
sub find_files { |
266 |
my ($path) = @_; |
267 |
|
268 |
IO::AIO::aioreq_pri 4; |
269 |
IO::AIO::aio_scandir $path, 4, sub { |
270 |
my ($dirs, $nondirs) = @_; |
271 |
|
272 |
find_files "$path/$_" |
273 |
for grep $_ !~ /^(?:CVS|dev)$/, @$dirs; |
274 |
|
275 |
for my $file (@$nondirs) { |
276 |
if ($file =~ /\.png$/) { |
277 |
push @png, "$path/$file"; |
278 |
} elsif ($file =~ /\.trs$/) { |
279 |
push @trs, [$path, $file]; |
280 |
} elsif ($file =~ /\.arc$/) { |
281 |
push @arc, [$path, $file]; |
282 |
} else { |
283 |
warn "ignoring $path/$file\n" if $VERBOSE >= 2; |
284 |
} |
285 |
} |
286 |
}; |
287 |
} |
288 |
|
289 |
sub inst_arch($) { |
290 |
my (undef, $path) = @_; |
291 |
|
292 |
print "installing '$path' to '$DATADIR'\n\n"; |
293 |
|
294 |
if (!-d "$path/treasures") { |
295 |
warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; |
296 |
exit 1 unless $FORCE; |
297 |
} |
298 |
|
299 |
find_files $path; |
300 |
IO::AIO::flush; |
301 |
|
302 |
$_->join for ( |
303 |
(async \&process_png), (async \&process_png), |
304 |
(async \&process_trs), (async \&process_trs), |
305 |
(async \&process_arc), (async \&process_arc), |
306 |
); |
307 |
|
308 |
{ |
309 |
open my $fh, ">:utf8", "$DATADIR/animations~" |
310 |
or die "$DATADIR/animations~: $!"; |
311 |
print $fh join "", map $ANIM{$_}, sort keys %ANIM |
312 |
} |
313 |
|
314 |
{ |
315 |
open my $fh, ">:utf8", "$DATADIR/archetypes~" |
316 |
or die "$DATADIR/archetypes~: $!"; |
317 |
print $fh Crossfire::archlist_to_string \@ARC; |
318 |
} |
319 |
|
320 |
{ |
321 |
while (my ($k, $v) = each %FACEINFO) { |
322 |
$v->{data32} ||= delete $PNG32{$k}; |
323 |
} |
324 |
|
325 |
while (my ($k, $v) = each %FACEINFO) { |
326 |
exists $v->{data32} or warn "$k: face has no png32. this will crash the server.\n"; |
327 |
|
328 |
$v->{chksum32} = Digest::MD5::md5 $v->{data32}; |
329 |
} |
330 |
|
331 |
open my $fh, ">:perlio", "$DATADIR/faces~" |
332 |
or die "$DATADIR/faces~: $!"; |
333 |
|
334 |
print $fh Storable::nfreeze \%FACEINFO; |
335 |
|
336 |
#use PApp::Util; warn PApp::Util::dumpval \%FACEINFO; |
337 |
} |
338 |
|
339 |
for (qw(archetypes faces animations)) { |
340 |
chmod 0644, "$DATADIR/$_~"; |
341 |
rename "$DATADIR/$_~", "$DATADIR/$_"; |
342 |
} |
343 |
|
344 |
die "--install-arch not fully implemented\n"; |
345 |
} |
346 |
} |
347 |
|
348 |
Getopt::Long::Configure ("bundling", "no_ignore_case"); |
349 |
GetOptions ( |
350 |
"verbose|v:+" => \$VERBOSE, |
351 |
"cache" => \$CACHE, |
352 |
"quiet|q" => sub { $VERBOSE = 0 }, |
353 |
"force" => sub { $FORCE = 1 }, |
354 |
"install-arch=s" => \&inst_arch, |
355 |
"install-maps=s" => \&inst_maps, |
356 |
"print-statedir" => sub { print "@pkgstatedir@\n" }, |
357 |
"print-datadir" => sub { print "$DATADIR\n" }, |
358 |
"print-confdir" => sub { print "@pkgconfdir@\n" }, |
359 |
"print-libdir" => sub { print "@libdir@/@PACKAGE@\n" }, |
360 |
"print-bindir" => sub { print "@bindir@/@PACKAGE@\n" }, |
361 |
) or usage; |
362 |
|