… | |
… | |
11 | use strict; |
11 | use strict; |
12 | |
12 | |
13 | use base 'Exporter'; |
13 | use base 'Exporter'; |
14 | |
14 | |
15 | use Carp (); |
15 | use Carp (); |
16 | use Storable; |
|
|
17 | use File::Spec; |
16 | use File::Spec; |
18 | use List::Util qw(min max); |
17 | use List::Util qw(min max); |
19 | |
18 | use Storable; |
20 | #XXX: The map_* procedures scream for a map-object |
|
|
21 | |
19 | |
22 | our @EXPORT = |
20 | our @EXPORT = |
23 | qw(read_pak read_arch %ARCH TILESIZE $TILE %FACE editor_archs arch_extents); |
21 | qw(read_pak read_arch %ARCH TILESIZE $TILE %FACE editor_archs arch_extents); |
24 | |
22 | |
25 | our $LIB = $ENV{CROSSFIRE_LIBDIR} |
23 | our $LIB = $ENV{CROSSFIRE_LIBDIR} |
… | |
… | |
44 | sub MOVE_FLY_LOW (){ 0x2 } |
42 | sub MOVE_FLY_LOW (){ 0x2 } |
45 | sub MOVE_FLY_HIGH (){ 0x4 } |
43 | sub MOVE_FLY_HIGH (){ 0x4 } |
46 | sub MOVE_FLYING (){ 0x6 } |
44 | sub MOVE_FLYING (){ 0x6 } |
47 | sub MOVE_SWIM (){ 0x8 } |
45 | sub MOVE_SWIM (){ 0x8 } |
48 | sub MOVE_ALL (){ 0xf } |
46 | sub MOVE_ALL (){ 0xf } |
|
|
47 | |
|
|
48 | sub load_ref($) { |
|
|
49 | my ($path) = @_; |
|
|
50 | |
|
|
51 | open my $fh, "<", $path |
|
|
52 | or die "$path: $!"; |
|
|
53 | binmode $fh; |
|
|
54 | local $/; |
|
|
55 | |
|
|
56 | Storable::thaw <$fh> |
|
|
57 | } |
|
|
58 | |
|
|
59 | sub save_ref($$) { |
|
|
60 | my ($ref, $path) = @_; |
|
|
61 | |
|
|
62 | open my $fh, ">", "$path~" |
|
|
63 | or die "$path~: $!"; |
|
|
64 | binmode $fh; |
|
|
65 | print $fh Storable::freeze $ref; |
|
|
66 | close $fh; |
|
|
67 | rename "$path~", $path |
|
|
68 | or die "$path: $!"; |
|
|
69 | } |
49 | |
70 | |
50 | sub normalize_arch($) { |
71 | sub normalize_arch($) { |
51 | my ($ob) = @_; |
72 | my ($ob) = @_; |
52 | |
73 | |
53 | my $arch = $ARCH{$ob->{_name}} |
74 | my $arch = $ARCH{$ob->{_name}} |
… | |
… | |
113 | sub read_pak($;$) { |
134 | sub read_pak($;$) { |
114 | my ($path, $cache) = @_; |
135 | my ($path, $cache) = @_; |
115 | |
136 | |
116 | eval { |
137 | eval { |
117 | defined $cache |
138 | defined $cache |
118 | && -e $cache |
|
|
119 | && -M $cache < -M $path |
139 | && -M $cache < -M $path |
120 | && Storable::retrieve ($cache) |
140 | && load_ref $cache |
121 | } or do { |
141 | } or do { |
122 | my %pak; |
142 | my %pak; |
123 | |
143 | |
124 | open my $fh, "<:raw", $path |
144 | open my $fh, "<", $path |
125 | or Carp::croak "$_[0]: $!"; |
145 | or Carp::croak "$_[0]: $!"; |
|
|
146 | binmode $fh; |
126 | while (<$fh>) { |
147 | while (<$fh>) { |
127 | my ($type, $id, $len, $path) = split; |
148 | my ($type, $id, $len, $path) = split; |
128 | $path =~ s/.*\///; |
149 | $path =~ s/.*\///; |
129 | read $fh, $pak{$path}, $len; |
150 | read $fh, $pak{$path}, $len; |
130 | } |
151 | } |
131 | |
152 | |
132 | Storable::nstore (\%pak, $cache) |
153 | save_ref \%pak, $cache |
133 | if defined $cache; |
154 | if defined $cache; |
134 | |
155 | |
135 | \%pak |
156 | \%pak |
136 | } |
157 | } |
137 | } |
158 | } |
… | |
… | |
139 | sub read_arch($;$) { |
160 | sub read_arch($;$) { |
140 | my ($path, $cache) = @_; |
161 | my ($path, $cache) = @_; |
141 | |
162 | |
142 | eval { |
163 | eval { |
143 | defined $cache |
164 | defined $cache |
144 | && -e $cache |
|
|
145 | && -M $cache < -M $path |
165 | && -M $cache < -M $path |
146 | && Storable::retrieve ($cache) |
166 | && load_ref $cache |
147 | } or do { |
167 | } or do { |
148 | my %arc; |
168 | my %arc; |
149 | my ($more, $prev); |
169 | my ($more, $prev); |
150 | |
170 | |
151 | open my $fh, "<:raw", $path |
171 | open my $fh, "<", $path |
152 | or Carp::croak "$path: $!"; |
172 | or Carp::croak "$path: $!"; |
|
|
173 | |
|
|
174 | binmode $fh; |
153 | |
175 | |
154 | my $parse_block; $parse_block = sub { |
176 | my $parse_block; $parse_block = sub { |
155 | my %arc = @_; |
177 | my %arc = @_; |
156 | |
178 | |
157 | while (<$fh>) { |
179 | while (<$fh>) { |
… | |
… | |
206 | } |
228 | } |
207 | } |
229 | } |
208 | |
230 | |
209 | undef $parse_block; # work around bug in perl not freeing $fh etc. |
231 | undef $parse_block; # work around bug in perl not freeing $fh etc. |
210 | |
232 | |
211 | Storable::nstore (\%arc, $cache) |
233 | save_ref \%arc, $cache |
212 | if defined $cache; |
234 | if defined $cache; |
213 | |
235 | |
214 | \%arc |
236 | \%arc |
215 | } |
237 | } |
216 | } |
238 | } |
… | |
… | |
287 | sub arch_attr($) { |
309 | sub arch_attr($) { |
288 | my ($arch) = @_; |
310 | my ($arch) = @_; |
289 | |
311 | |
290 | require Crossfire::Data; |
312 | require Crossfire::Data; |
291 | |
313 | |
292 | my $attr; |
314 | my $root; |
293 | |
315 | |
294 | if ($arch->{type} > 0) { |
316 | if ($arch->{type} > 0) { |
295 | $attr = $Crossfire::Data::ATTR{$arch->{type}+0}; |
317 | $root = $Crossfire::Data::ATTR{$arch->{type}+0}; |
296 | } else { |
318 | } else { |
297 | $attr = $Crossfire::Data::TYPE{Misc}; |
319 | $root = $Crossfire::Data::TYPE{Misc}; |
298 | |
320 | |
299 | type: |
321 | type: |
300 | for (@Crossfire::Data::ATTR0) { |
322 | for (@Crossfire::Data::ATTR0) { |
301 | my $req = $_->{required} |
323 | my $req = $_->{required} |
302 | or die "internal error: ATTR0 without 'required'"; |
324 | or die "internal error: ATTR0 without 'required'"; |
303 | |
325 | |
|
|
326 | keys %$req; |
304 | while (my ($k, $v) = each %$req) { |
327 | while (my ($k, $v) = each %$req) { |
305 | next type |
328 | next type |
306 | unless $arch->{$k} == $v; |
329 | unless $arch->{$k} == $v; |
307 | } |
330 | } |
308 | |
331 | |
309 | $attr = $_; |
332 | $root = $_; |
|
|
333 | } |
|
|
334 | } |
|
|
335 | |
|
|
336 | my $attr = { }; |
|
|
337 | |
|
|
338 | my @import = (\%Crossfire::Data::DEFAULT_ATTR, $root); |
|
|
339 | my (%ignore); |
|
|
340 | my (@section_order, %section, @attr_order); |
|
|
341 | |
|
|
342 | while (my $type = shift @import) { |
|
|
343 | push @import, @{$type->{import} || []}; |
|
|
344 | |
|
|
345 | $attr->{$_} ||= $type->{$_} |
|
|
346 | for qw(name desc use); |
|
|
347 | |
|
|
348 | for (@{$type->{ignore} || []}) { |
|
|
349 | $ignore{$_}++ for ref $_ ? @$_ : $_; |
|
|
350 | } |
|
|
351 | |
|
|
352 | for ([general => ($type->{attr} || [])], @{$type->{section} || []}) { |
|
|
353 | my ($name, $attr) = @$_; |
|
|
354 | push @section_order, $name; |
|
|
355 | warn "$name, $attr\n";#d# |
|
|
356 | for (@$attr) { |
|
|
357 | my ($k, $v) = @$_; |
|
|
358 | push @attr_order, $k; |
|
|
359 | $section{$name}{$k} ||= $v; |
310 | } |
360 | } |
|
|
361 | } |
311 | } |
362 | } |
312 | |
363 | |
313 | $attr || \%Crossfire::Data::DEFAULT_ATTR; |
364 | $attr->{section} = [ |
|
|
365 | map !exists $section{$_} ? () : do { |
|
|
366 | my $attr = delete $section{$_}; |
|
|
367 | |
|
|
368 | [ |
|
|
369 | $_, |
|
|
370 | map exists $attr->{$_} && !$ignore{$_} |
|
|
371 | ? [$_ => delete $attr->{$_}] : (), |
|
|
372 | @attr_order |
|
|
373 | ] |
|
|
374 | }, |
|
|
375 | |
|
|
376 | exists $section{$_} ? [$_ => delete $section{$_}] : (), |
|
|
377 | @section_order |
|
|
378 | ]; |
|
|
379 | |
|
|
380 | $attr |
314 | } |
381 | } |
315 | |
382 | |
316 | sub arch_edit_sections { |
383 | sub arch_edit_sections { |
317 | # if (edit_type == IGUIConstants.TILE_EDIT_NONE) |
384 | # if (edit_type == IGUIConstants.TILE_EDIT_NONE) |
318 | # edit_type = 0; |
385 | # edit_type = 0; |
… | |
… | |
381 | |
448 | |
382 | mkdir $cachedir, 0777; |
449 | mkdir $cachedir, 0777; |
383 | *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; |
450 | *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; |
384 | } |
451 | } |
385 | |
452 | |
386 | $VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/crossfire" : File::Spec->tmpdir; |
453 | $VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire"; |
387 | |
454 | |
388 | init $VARDIR; |
455 | init $VARDIR; |
389 | |
456 | |
390 | =head1 AUTHOR |
457 | =head1 AUTHOR |
391 | |
458 | |