… | |
… | |
19 | use File::Temp; |
19 | use File::Temp; |
20 | use Crossfire; |
20 | use Crossfire; |
21 | use Coro; |
21 | use Coro; |
22 | use Coro::AIO; |
22 | use Coro::AIO; |
23 | use POSIX (); |
23 | use POSIX (); |
|
|
24 | use Digest::MD5; |
24 | |
25 | |
25 | sub usage { |
26 | sub usage { |
26 | warn <<EOF; |
27 | warn <<EOF; |
27 | Usage: cfutil [-v] [-q] [--force] [--cache] |
28 | Usage: cfutil [-v] [-q] [--force] [--cache] |
28 | [--install-arch path] |
29 | [--install-arch path] |
… | |
… | |
77 | |
78 | |
78 | system $RSYNC, "-av", "$path/.", "$DATADIR/maps/.", "--delete", "--exclude", "CVS", "--delete-excluded"; |
79 | system $RSYNC, "-av", "$path/.", "$DATADIR/maps/.", "--delete", "--exclude", "CVS", "--delete-excluded"; |
79 | } |
80 | } |
80 | |
81 | |
81 | { |
82 | { |
82 | our @PNG; |
83 | our %PNG32; |
|
|
84 | our %FACEINFO; |
83 | our @ARC; |
85 | our @ARC; |
84 | our $NFILE; |
86 | our $NFILE; |
|
|
87 | our %ANIM; |
85 | |
88 | |
86 | our (@png, @trs, @arc); # files we are interested in |
89 | our (@png, @trs, @arc); # files we are interested in |
87 | |
90 | |
88 | sub commit_png { |
91 | sub commit_png { |
89 | my ($name, $data) = @_; |
92 | my ($name, $data) = @_; |
90 | #warn "$name: commited\n"; |
93 | |
|
|
94 | $PNG32{$name} = $data; |
|
|
95 | $FACEINFO{$name} ||= {}; |
91 | } |
96 | } |
92 | |
97 | |
93 | sub process_png { |
98 | sub process_png { |
94 | while (@png) { |
99 | while (@png) { |
95 | my $path = pop @png; |
100 | my $path = pop @png; |
… | |
… | |
98 | aio_lstat $path; |
103 | aio_lstat $path; |
99 | my ($size, $mtime) = (stat _)[7,9]; |
104 | my ($size, $mtime) = (stat _)[7,9]; |
100 | |
105 | |
101 | if (0 > aio_load $path, $png) { |
106 | if (0 > aio_load $path, $png) { |
102 | warn "$path: $!, skipping.\n"; |
107 | warn "$path: $!, skipping.\n"; |
103 | return; |
108 | next; |
104 | } |
109 | } |
105 | |
110 | |
106 | # quickly extratc width and height of the (necessarily PNG) image |
111 | # quickly extract width and height of the (necessarily PNG) image |
107 | unless ($png =~ /^\x89PNG\x0d\x0a\x1a\x0a....IHDR(........)/s) { |
112 | unless ($png =~ /^\x89PNG\x0d\x0a\x1a\x0a....IHDR(........)/s) { |
108 | warn "$path: not a recongized png file, skipping.\n"; |
113 | warn "$path: not a recongized png file, skipping.\n"; |
109 | return; |
114 | next; |
110 | } |
115 | } |
111 | |
116 | |
112 | my ($w, $h) = unpack "NN", $1; |
117 | my ($w, $h) = unpack "NN", $1; |
113 | |
118 | |
114 | (my $face = $path) =~ s/^.*\///; |
119 | (my $face = $path) =~ s/^.*\///; |
115 | my $T = 32; |
120 | my $T = 32; |
116 | |
121 | |
117 | unless ($face =~ s/\.base\.(...)\.png$/.$1/) { |
122 | unless ($face =~ s/\.base\.(...)\.png$/.$1/) { |
118 | warn "$path: weird filename, skipping.\n"; |
123 | warn "$path: weird filename, skipping.\n"; |
119 | return; |
124 | next; |
120 | } |
125 | } |
121 | |
126 | |
122 | if ($w < $T || $h < $T) { |
127 | if ($w < $T || $h < $T) { |
123 | warn "$path: too small ($w $h), skipping.\n"; |
128 | warn "$path: too small ($w $h), skipping.\n"; |
124 | return; |
129 | next; |
125 | } |
130 | } |
126 | |
131 | |
127 | if ($w % $T || $h % $T) { |
132 | if ($w % $T || $h % $T) { |
128 | warn "$path: weird png size ($w $h), skipping.\n"; |
133 | warn "$path: weird png size ($w $h), skipping.\n"; |
129 | return; |
134 | next; |
130 | } |
135 | } |
131 | |
136 | |
132 | if (($w > $T || $h > $T) && $face !~ /_S\./) { |
137 | if (($w > $T || $h > $T) && $face !~ /_S\./) { |
133 | # split |
138 | # split |
134 | my @tile; |
139 | my @tile; |
… | |
… | |
149 | (map { |
154 | (map { |
150 | ( |
155 | ( |
151 | "(", |
156 | "(", |
152 | "+clone", |
157 | "+clone", |
153 | -crop => (sprintf "%dx%d+%d+%d", $T, $T, $_->[0] * $T, $_->[1] * $T), |
158 | -crop => (sprintf "%dx%d+%d+%d", $T, $T, $_->[0] * $T, $_->[1] * $T), |
|
|
159 | "+repage", |
154 | -write => "png:$_->[2]~", |
160 | -write => "png:$_->[2]~", |
155 | "+delete", |
161 | "+delete", |
156 | ")", |
162 | ")", |
157 | ) |
163 | ) |
158 | } @todo), |
164 | } @todo), |
… | |
… | |
195 | aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/ |
201 | aio_load "$dir/$file", $arc; # simply pre-cache, as read_arch wants a file :/ |
196 | |
202 | |
197 | my $arc = read_arch "$dir/$file"; |
203 | my $arc = read_arch "$dir/$file"; |
198 | for my $o (values %$arc) { |
204 | for my $o (values %$arc) { |
199 | push @ARC, $o; |
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 |
200 | my ($dx, $dy); |
212 | my ($dx, $dy); |
201 | # omg, this is sooo broken |
|
|
202 | for (my $o = $o; $o; $o = $o->{more}) { |
213 | for (my $o = $o; $o; $o = $o->{more}) { |
203 | $dx = $o->{x} if $o->{x} < $dx; |
214 | $dx = $o->{x} if $o->{x} < $dx; |
204 | $dy = $o->{y} if $o->{y} < $dy; |
215 | $dy = $o->{y} if $o->{y} < $dy; |
205 | } |
216 | } |
|
|
217 | |
206 | for (my $o = $o; $o; $o = $o->{more}) { |
218 | for (my $o = $o; $o; $o = $o->{more}) { |
207 | my $x = $o->{x} - $dx; |
219 | my $x = $o->{x} - $dx; |
208 | my $y = $o->{y} - $dy; |
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 | |
209 | if ($x|$y) { |
231 | if ($anim) { |
210 | $_ .= "+$x+$y" for $o->{face}, @{$o->{anim} || []}; |
232 | $o->{animation} = "$o->{_name}"; |
|
|
233 | |
|
|
234 | for (@$anim) { |
|
|
235 | $_ .= $ext unless /^facings\s/; |
|
|
236 | } |
|
|
237 | |
|
|
238 | $ANIM{"$o->{_name}$ext"} = |
|
|
239 | join "", map "$_\n", |
|
|
240 | "anim $o->{_name}", |
|
|
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; |
211 | } |
252 | } |
212 | } |
253 | } |
213 | } |
254 | } |
214 | } |
255 | } |
215 | } |
256 | } |
… | |
… | |
262 | (async \&process_png), (async \&process_png), |
303 | (async \&process_png), (async \&process_png), |
263 | (async \&process_trs), (async \&process_trs), |
304 | (async \&process_trs), (async \&process_trs), |
264 | (async \&process_arc), (async \&process_arc), |
305 | (async \&process_arc), (async \&process_arc), |
265 | ); |
306 | ); |
266 | |
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 | { |
267 | open my $archetypes, ">:utf8", "$DATADIR/archetypes~" |
315 | open my $fh, ">:utf8", "$DATADIR/archetypes~" |
268 | or die "$DATADIR/archetypes~: $!"; |
316 | or die "$DATADIR/archetypes~: $!"; |
269 | print $archetypes Crossfire::archlist_to_string \@ARC; |
317 | print $fh Crossfire::archlist_to_string \@ARC; |
270 | close $archetypes; |
318 | } |
271 | rename "$DATADIR/archetypes~", "$DATADIR/archetypes"; |
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 | } |
272 | |
343 | |
273 | die "--install-arch not fully implemented\n"; |
344 | die "--install-arch not fully implemented\n"; |
274 | } |
345 | } |
275 | } |
346 | } |
276 | |
347 | |