… | |
… | |
7 | my $datarootdir = "@datarootdir@"; |
7 | my $datarootdir = "@datarootdir@"; |
8 | my $DATADIR = "@datadir@/@PACKAGE@"; |
8 | my $DATADIR = "@datadir@/@PACKAGE@"; |
9 | |
9 | |
10 | my $CONVERT = "@CONVERT@"; |
10 | my $CONVERT = "@CONVERT@"; |
11 | my $IDENTIFY = "@IDENTIFY@"; |
11 | my $IDENTIFY = "@IDENTIFY@"; |
|
|
12 | my $OPTIPNG = "@OPTIPNG@"; |
12 | my $RSYNC = "@RSYNC@"; |
13 | my $RSYNC = "@RSYNC@"; |
13 | |
14 | |
14 | use Getopt::Long; |
15 | use Getopt::Long; |
|
|
16 | use Coro::Event; |
|
|
17 | use AnyEvent; |
15 | use IO::AIO; |
18 | use IO::AIO (); |
16 | use File::Temp; |
19 | use File::Temp; |
17 | use Crossfire; |
20 | use Crossfire; |
|
|
21 | use Coro; |
|
|
22 | use Coro::AIO; |
|
|
23 | use POSIX (); |
18 | |
24 | |
19 | sub usage { |
25 | sub usage { |
20 | warn <<EOF; |
26 | warn <<EOF; |
21 | Usage: cfutil [-v] [-q] [--force] |
27 | Usage: 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] |
… | |
… | |
29 | EOF |
35 | EOF |
30 | exit 1; |
36 | exit 1; |
31 | } |
37 | } |
32 | |
38 | |
33 | my $VERBOSE = 1; |
39 | my $VERBOSE = 1; |
|
|
40 | my $CACHE = 0; |
34 | my $FORCE; |
41 | my $FORCE; |
35 | my $TMPDIR = "/tmp/cfutil$$~"; |
42 | my $TMPDIR = "/tmp/cfutil$$~"; |
|
|
43 | my $TMPFILE = "aaaa0"; |
36 | |
44 | |
37 | END { system "rm", "-rf", $TMPDIR } |
45 | END { system "rm", "-rf", $TMPDIR } |
|
|
46 | |
|
|
47 | Event->signal (signal => "INT", cb => sub { exit 1 }); |
|
|
48 | Event->signal (signal => "TERM", cb => sub { exit 1 }); |
38 | |
49 | |
39 | mkdir $TMPDIR, 0700 |
50 | mkdir $TMPDIR, 0700 |
40 | or die "$TMPDIR: $!"; |
51 | or die "$TMPDIR: $!"; |
|
|
52 | |
|
|
53 | 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 | } |
41 | |
67 | |
42 | sub inst_maps($) { |
68 | sub 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 | |
|
|
86 | our (@png, @trs, @arc); # files we are interested in |
58 | |
87 | |
59 | sub commit_png { |
88 | sub commit_png { |
60 | my ($name, $data) = @_; |
89 | my ($name, $data) = @_; |
61 | #warn "$name: commited\n"; |
90 | #warn "$name: commited\n"; |
62 | } |
91 | } |
63 | |
92 | |
64 | sub add_png($) { |
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 { |
65 | my ($path) = @_; |
225 | my ($path) = @_; |
66 | |
226 | |
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; |
227 | IO::AIO::aioreq_pri 4; |
134 | aio_scandir $path, 4, sub { |
228 | IO::AIO::aio_scandir $path, 4, sub { |
135 | my ($dirs, $nondirs) = @_; |
229 | my ($dirs, $nondirs) = @_; |
136 | |
230 | |
137 | collect_arch "$path/$_" |
231 | find_files "$path/$_" |
138 | for grep $_ !~ /^(?:CVS|dev)$/, @$dirs; |
232 | for grep $_ !~ /^(?:CVS|dev)$/, @$dirs; |
139 | |
233 | |
140 | for my $file (@$nondirs) { |
234 | for my $file (@$nondirs) { |
141 | if ($file =~ /\.png$/) { |
235 | if ($file =~ /\.png$/) { |
142 | add_png "$path/$file"; |
236 | push @png, "$path/$file"; |
143 | } elsif ($file =~ /\.trs$/) { |
237 | } elsif ($file =~ /\.trs$/) { |
144 | add_trs $path, $file; |
238 | push @trs, [$path, $file]; |
145 | } elsif ($file =~ /\.arc$/) { |
239 | } elsif ($file =~ /\.arc$/) { |
146 | add_arc $path, $file; |
240 | push @arc, [$path, $file]; |
147 | } else { |
241 | } else { |
148 | warn "ignoring $path/$file\n" if $VERBOSE >= 2; |
242 | warn "ignoring $path/$file\n" if $VERBOSE >= 2; |
149 | } |
243 | } |
150 | } |
244 | } |
151 | }; |
245 | }; |
… | |
… | |
159 | if (!-d "$path/treasures") { |
253 | if (!-d "$path/treasures") { |
160 | warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; |
254 | warn "'$path' does not look like an arch directory ('treasures' directory is missing).\n"; |
161 | exit 1 unless $FORCE; |
255 | exit 1 unless $FORCE; |
162 | } |
256 | } |
163 | |
257 | |
164 | collect_arch $path; |
258 | find_files $path; |
165 | IO::AIO::flush; |
259 | IO::AIO::flush; |
166 | |
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 | |
167 | die "--install-arch not yet implemented\n"; |
273 | die "--install-arch not fully implemented\n"; |
168 | } |
274 | } |
169 | } |
275 | } |
170 | |
276 | |
171 | Getopt::Long::Configure ("bundling", "no_ignore_case"); |
277 | Getopt::Long::Configure ("bundling", "no_ignore_case"); |
172 | GetOptions ( |
278 | GetOptions ( |
173 | "verbose|v:+" => \$VERBOSE, |
279 | "verbose|v:+" => \$VERBOSE, |
|
|
280 | "cache" => \$CACHE, |
174 | "quiet|q" => sub { $VERBOSE = 0 }, |
281 | "quiet|q" => sub { $VERBOSE = 0 }, |
175 | "force" => sub { $FORCE = 1 }, |
282 | "force" => sub { $FORCE = 1 }, |
176 | "install-arch=s" => \&inst_arch, |
283 | "install-arch=s" => \&inst_arch, |
177 | "install-maps=s" => \&inst_maps, |
284 | "install-maps=s" => \&inst_maps, |
178 | "print-statedir" => sub { print "@pkgstatedir@\n" }, |
285 | "print-statedir" => sub { print "@pkgstatedir@\n" }, |