| 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.2 |
|
| 25 |
|
|
sub usage { |
| 26 |
|
|
warn <<EOF; |
| 27 |
root |
1.3 |
Usage: cfutil [-v] [-q] [--force] [--cache] |
| 28 |
root |
1.2 |
[--install-arch path] |
| 29 |
|
|
[--install-maps maps] |
| 30 |
|
|
[--print-statedir] |
| 31 |
|
|
[--print-confdir] |
| 32 |
|
|
[--print-datadir] |
| 33 |
|
|
[--print-libdir] |
| 34 |
|
|
[--print-bindir] |
| 35 |
|
|
EOF |
| 36 |
|
|
exit 1; |
| 37 |
|
|
} |
| 38 |
|
|
|
| 39 |
|
|
my $VERBOSE = 1; |
| 40 |
root |
1.3 |
my $CACHE = 0; |
| 41 |
root |
1.2 |
my $FORCE; |
| 42 |
root |
1.3 |
my $TMPDIR = "/tmp/cfutil$$~"; |
| 43 |
|
|
my $TMPFILE = "aaaa0"; |
| 44 |
root |
1.2 |
|
| 45 |
|
|
END { system "rm", "-rf", $TMPDIR } |
| 46 |
|
|
|
| 47 |
root |
1.3 |
Event->signal (signal => "INT", cb => sub { exit 1 }); |
| 48 |
|
|
Event->signal (signal => "TERM", cb => sub { exit 1 }); |
| 49 |
|
|
|
| 50 |
root |
1.2 |
mkdir $TMPDIR, 0700 |
| 51 |
|
|
or die "$TMPDIR: $!"; |
| 52 |
|
|
|
| 53 |
root |
1.3 |
sub 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 |
|
|
|
| 68 |
root |
1.2 |
sub 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 |
root |
1.3 |
our @PNG; |
| 83 |
|
|
our @ARC; |
| 84 |
|
|
our $NFILE; |
| 85 |
|
|
|
| 86 |
|
|
our (@png, @trs, @arc); # files we are interested in |
| 87 |
root |
1.2 |
|
| 88 |
|
|
sub commit_png { |
| 89 |
|
|
my ($name, $data) = @_; |
| 90 |
|
|
#warn "$name: commited\n"; |
| 91 |
|
|
} |
| 92 |
|
|
|
| 93 |
root |
1.3 |
sub process_png { |
| 94 |
|
|
while (@png) { |
| 95 |
|
|
my $path = pop @png; |
| 96 |
root |
1.2 |
|
| 97 |
root |
1.3 |
my $png; |
| 98 |
|
|
aio_lstat $path; |
| 99 |
root |
1.2 |
my ($size, $mtime) = (stat _)[7,9]; |
| 100 |
|
|
|
| 101 |
root |
1.3 |
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 |
root |
1.2 |
|
| 112 |
root |
1.3 |
my ($w, $h) = unpack "NN", $1; |
| 113 |
root |
1.2 |
|
| 114 |
root |
1.3 |
(my $face = $path) =~ s/^.*\///; |
| 115 |
|
|
my $T = 32; |
| 116 |
root |
1.2 |
|
| 117 |
root |
1.3 |
unless ($face =~ s/\.base\.(...)\.png$/.$1/) { |
| 118 |
|
|
warn "$path: weird filename, skipping.\n"; |
| 119 |
|
|
return; |
| 120 |
|
|
} |
| 121 |
root |
1.2 |
|
| 122 |
root |
1.3 |
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 |
root |
1.2 |
|
| 132 |
root |
1.3 |
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 |
root |
1.2 |
} |
| 142 |
|
|
|
| 143 |
root |
1.3 |
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 |
root |
1.2 |
} |
| 172 |
|
|
|
| 173 |
root |
1.3 |
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 |
root |
1.2 |
} |
| 183 |
root |
1.3 |
} else { |
| 184 |
|
|
# use as-is (either small, use smooth) |
| 185 |
|
|
commit_png $face, $png; |
| 186 |
|
|
} |
| 187 |
|
|
} |
| 188 |
root |
1.2 |
} |
| 189 |
|
|
|
| 190 |
root |
1.3 |
sub process_arc { |
| 191 |
|
|
while (@arc) { |
| 192 |
|
|
my ($dir, $file) = @{pop @arc}; |
| 193 |
root |
1.2 |
|
| 194 |
root |
1.3 |
my $arc; |
| 195 |
|
|
aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/ |
| 196 |
root |
1.2 |
my $arc = read_arch "$dir/$file"; |
| 197 |
root |
1.3 |
} |
| 198 |
root |
1.2 |
} |
| 199 |
|
|
|
| 200 |
root |
1.3 |
sub process_trs { |
| 201 |
|
|
while (@trs) { |
| 202 |
|
|
my ($dir, $file) = @{pop @trs}; |
| 203 |
|
|
} |
| 204 |
root |
1.2 |
} |
| 205 |
|
|
|
| 206 |
root |
1.3 |
sub find_files; |
| 207 |
|
|
sub find_files { |
| 208 |
root |
1.2 |
my ($path) = @_; |
| 209 |
|
|
|
| 210 |
root |
1.3 |
IO::AIO::aioreq_pri 4; |
| 211 |
|
|
IO::AIO::aio_scandir $path, 4, sub { |
| 212 |
root |
1.2 |
my ($dirs, $nondirs) = @_; |
| 213 |
|
|
|
| 214 |
root |
1.3 |
find_files "$path/$_" |
| 215 |
root |
1.2 |
for grep $_ !~ /^(?:CVS|dev)$/, @$dirs; |
| 216 |
|
|
|
| 217 |
|
|
for my $file (@$nondirs) { |
| 218 |
|
|
if ($file =~ /\.png$/) { |
| 219 |
root |
1.3 |
push @png, "$path/$file"; |
| 220 |
root |
1.2 |
} elsif ($file =~ /\.trs$/) { |
| 221 |
root |
1.3 |
push @trs, [$path, $file]; |
| 222 |
root |
1.2 |
} elsif ($file =~ /\.arc$/) { |
| 223 |
root |
1.3 |
push @arc, [$path, $file]; |
| 224 |
root |
1.2 |
} else { |
| 225 |
|
|
warn "ignoring $path/$file\n" if $VERBOSE >= 2; |
| 226 |
|
|
} |
| 227 |
|
|
} |
| 228 |
|
|
}; |
| 229 |
|
|
} |
| 230 |
|
|
|
| 231 |
|
|
sub inst_arch($) { |
| 232 |
|
|
my (undef, $path) = @_; |
| 233 |
|
|
|
| 234 |
|
|
print "installing '$path' to '$DATADIR'\n\n"; |
| 235 |
|
|
|
| 236 |
|
|
if (!-d "$path/treasures") { |
| 237 |
|
|
warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; |
| 238 |
|
|
exit 1 unless $FORCE; |
| 239 |
|
|
} |
| 240 |
|
|
|
| 241 |
root |
1.3 |
find_files $path; |
| 242 |
root |
1.2 |
IO::AIO::flush; |
| 243 |
|
|
|
| 244 |
root |
1.3 |
$_->join for ( |
| 245 |
|
|
(async \&process_png), (async \&process_png), |
| 246 |
|
|
(async \&process_trs), (async \&process_trs), |
| 247 |
|
|
(async \&process_arc), (async \&process_arc), |
| 248 |
|
|
); |
| 249 |
|
|
|
| 250 |
root |
1.2 |
die "--install-arch not yet implemented\n"; |
| 251 |
|
|
} |
| 252 |
|
|
} |
| 253 |
|
|
|
| 254 |
|
|
Getopt::Long::Configure ("bundling", "no_ignore_case"); |
| 255 |
|
|
GetOptions ( |
| 256 |
|
|
"verbose|v:+" => \$VERBOSE, |
| 257 |
root |
1.3 |
"cache" => \$CACHE, |
| 258 |
root |
1.2 |
"quiet|q" => sub { $VERBOSE = 0 }, |
| 259 |
|
|
"force" => sub { $FORCE = 1 }, |
| 260 |
|
|
"install-arch=s" => \&inst_arch, |
| 261 |
|
|
"install-maps=s" => \&inst_maps, |
| 262 |
|
|
"print-statedir" => sub { print "@pkgstatedir@\n" }, |
| 263 |
|
|
"print-datadir" => sub { print "$DATADIR\n" }, |
| 264 |
|
|
"print-confdir" => sub { print "@pkgconfdir@\n" }, |
| 265 |
|
|
"print-libdir" => sub { print "@libdir@/@PACKAGE@\n" }, |
| 266 |
|
|
"print-bindir" => sub { print "@bindir@/@PACKAGE@\n" }, |
| 267 |
|
|
) or usage; |
| 268 |
|
|
|