… | |
… | |
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} |
… | |
… | |
48 | sub MOVE_ALL (){ 0xf } |
46 | sub MOVE_ALL (){ 0xf } |
49 | |
47 | |
50 | sub load_ref($) { |
48 | sub load_ref($) { |
51 | my ($path) = @_; |
49 | my ($path) = @_; |
52 | |
50 | |
53 | open my $fh, ">:raw", "$path~" |
51 | open my $fh, "<", $path |
54 | or die "$path~: $!"; |
52 | or die "$path: $!"; |
|
|
53 | binmode $fh; |
55 | local $/; |
54 | local $/; |
|
|
55 | |
56 | Storable::thaw <$fh> |
56 | Storable::thaw <$fh> |
57 | } |
57 | } |
58 | |
58 | |
59 | sub save_ref($$) { |
59 | sub save_ref($$) { |
60 | my ($ref, $path) = @_; |
60 | my ($ref, $path) = @_; |
61 | |
61 | |
62 | open my $fh, ">:raw", "$path~" |
62 | open my $fh, ">", "$path~" |
63 | or die "$path~: $!"; |
63 | or die "$path~: $!"; |
|
|
64 | binmode $fh; |
64 | print $fh Storable::nfreeze $ref; |
65 | print $fh Storable::freeze $ref; |
65 | close $fh; |
66 | close $fh; |
66 | rename "$path~", $path |
67 | rename "$path~", $path |
67 | or die "$path: $!"; |
68 | or die "$path: $!"; |
68 | } |
69 | } |
69 | |
70 | |
… | |
… | |
138 | && -M $cache < -M $path |
139 | && -M $cache < -M $path |
139 | && load_ref $cache |
140 | && load_ref $cache |
140 | } or do { |
141 | } or do { |
141 | my %pak; |
142 | my %pak; |
142 | |
143 | |
143 | open my $fh, "<:raw", $path |
144 | open my $fh, "<", $path |
144 | or Carp::croak "$_[0]: $!"; |
145 | or Carp::croak "$_[0]: $!"; |
|
|
146 | binmode $fh; |
145 | while (<$fh>) { |
147 | while (<$fh>) { |
146 | my ($type, $id, $len, $path) = split; |
148 | my ($type, $id, $len, $path) = split; |
147 | $path =~ s/.*\///; |
149 | $path =~ s/.*\///; |
148 | read $fh, $pak{$path}, $len; |
150 | read $fh, $pak{$path}, $len; |
149 | } |
151 | } |
… | |
… | |
164 | && load_ref $cache |
166 | && load_ref $cache |
165 | } or do { |
167 | } or do { |
166 | my %arc; |
168 | my %arc; |
167 | my ($more, $prev); |
169 | my ($more, $prev); |
168 | |
170 | |
169 | open my $fh, "<:raw", $path |
171 | open my $fh, "<", $path |
170 | or Carp::croak "$path: $!"; |
172 | or Carp::croak "$path: $!"; |
|
|
173 | |
|
|
174 | binmode $fh; |
171 | |
175 | |
172 | my $parse_block; $parse_block = sub { |
176 | my $parse_block; $parse_block = sub { |
173 | my %arc = @_; |
177 | my %arc = @_; |
174 | |
178 | |
175 | while (<$fh>) { |
179 | while (<$fh>) { |
… | |
… | |
305 | sub arch_attr($) { |
309 | sub arch_attr($) { |
306 | my ($arch) = @_; |
310 | my ($arch) = @_; |
307 | |
311 | |
308 | require Crossfire::Data; |
312 | require Crossfire::Data; |
309 | |
313 | |
310 | my $attr; |
314 | my $root; |
311 | |
315 | |
312 | if ($arch->{type} > 0) { |
316 | if ($arch->{type} > 0) { |
313 | $attr = $Crossfire::Data::ATTR{$arch->{type}+0}; |
317 | $root = $Crossfire::Data::ATTR{$arch->{type}+0}; |
314 | } else { |
318 | } else { |
315 | $attr = $Crossfire::Data::TYPE{Misc}; |
319 | $root = $Crossfire::Data::TYPE{Misc}; |
316 | |
320 | |
317 | type: |
321 | type: |
318 | for (@Crossfire::Data::ATTR0) { |
322 | for (@Crossfire::Data::ATTR0) { |
319 | my $req = $_->{required} |
323 | my $req = $_->{required} |
320 | or die "internal error: ATTR0 without 'required'"; |
324 | or die "internal error: ATTR0 without 'required'"; |
321 | |
325 | |
|
|
326 | keys %$req; |
322 | while (my ($k, $v) = each %$req) { |
327 | while (my ($k, $v) = each %$req) { |
323 | next type |
328 | next type |
324 | unless $arch->{$k} == $v; |
329 | unless $arch->{$k} == $v; |
325 | } |
330 | } |
326 | |
331 | |
327 | $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 | for (@$attr) { |
|
|
356 | my ($k, $v) = @$_; |
|
|
357 | push @attr_order, $k; |
|
|
358 | $section{$name}{$k} ||= $v; |
328 | } |
359 | } |
|
|
360 | } |
329 | } |
361 | } |
330 | |
362 | |
331 | $attr || \%Crossfire::Data::DEFAULT_ATTR; |
363 | $attr->{section} = [ |
|
|
364 | map !exists $section{$_} ? () : do { |
|
|
365 | my $attr = delete $section{$_}; |
|
|
366 | |
|
|
367 | [ |
|
|
368 | $_, |
|
|
369 | map exists $attr->{$_} && !$ignore{$_} |
|
|
370 | ? [$_ => delete $attr->{$_}] : (), |
|
|
371 | @attr_order |
|
|
372 | ] |
|
|
373 | }, |
|
|
374 | |
|
|
375 | exists $section{$_} ? [$_ => delete $section{$_}] : (), |
|
|
376 | @section_order |
|
|
377 | ]; |
|
|
378 | |
|
|
379 | $attr |
332 | } |
380 | } |
333 | |
381 | |
334 | sub arch_edit_sections { |
382 | sub arch_edit_sections { |
335 | # if (edit_type == IGUIConstants.TILE_EDIT_NONE) |
383 | # if (edit_type == IGUIConstants.TILE_EDIT_NONE) |
336 | # edit_type = 0; |
384 | # edit_type = 0; |
… | |
… | |
399 | |
447 | |
400 | mkdir $cachedir, 0777; |
448 | mkdir $cachedir, 0777; |
401 | *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; |
449 | *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; |
402 | } |
450 | } |
403 | |
451 | |
404 | $VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/crossfire" : File::Spec->tmpdir; |
452 | $VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire"; |
405 | |
453 | |
406 | init $VARDIR; |
454 | init $VARDIR; |
407 | |
455 | |
408 | =head1 AUTHOR |
456 | =head1 AUTHOR |
409 | |
457 | |