… | |
… | |
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); |
|
|
18 | use Storable; |
19 | |
19 | |
20 | #XXX: The map_* procedures scream for a map-object |
20 | #XXX: The map_* procedures scream for a map-object |
21 | |
21 | |
22 | our @EXPORT = |
22 | our @EXPORT = |
23 | qw(read_pak read_arch %ARCH TILESIZE $TILE %FACE editor_archs arch_extents); |
23 | qw(read_pak read_arch %ARCH TILESIZE $TILE %FACE editor_archs arch_extents); |
… | |
… | |
44 | sub MOVE_FLY_LOW (){ 0x2 } |
44 | sub MOVE_FLY_LOW (){ 0x2 } |
45 | sub MOVE_FLY_HIGH (){ 0x4 } |
45 | sub MOVE_FLY_HIGH (){ 0x4 } |
46 | sub MOVE_FLYING (){ 0x6 } |
46 | sub MOVE_FLYING (){ 0x6 } |
47 | sub MOVE_SWIM (){ 0x8 } |
47 | sub MOVE_SWIM (){ 0x8 } |
48 | sub MOVE_ALL (){ 0xf } |
48 | sub MOVE_ALL (){ 0xf } |
|
|
49 | |
|
|
50 | sub load_ref($) { |
|
|
51 | my ($path) = @_; |
|
|
52 | |
|
|
53 | open my $fh, "<", $path |
|
|
54 | or die "$path: $!"; |
|
|
55 | binmode $fh; |
|
|
56 | local $/; |
|
|
57 | |
|
|
58 | Storable::thaw <$fh> |
|
|
59 | } |
|
|
60 | |
|
|
61 | sub save_ref($$) { |
|
|
62 | my ($ref, $path) = @_; |
|
|
63 | |
|
|
64 | open my $fh, ">", "$path~" |
|
|
65 | or die "$path~: $!"; |
|
|
66 | binmode $fh; |
|
|
67 | print $fh Storable::freeze $ref; |
|
|
68 | close $fh; |
|
|
69 | rename "$path~", $path |
|
|
70 | or die "$path: $!"; |
|
|
71 | } |
49 | |
72 | |
50 | sub normalize_arch($) { |
73 | sub normalize_arch($) { |
51 | my ($ob) = @_; |
74 | my ($ob) = @_; |
52 | |
75 | |
53 | my $arch = $ARCH{$ob->{_name}} |
76 | my $arch = $ARCH{$ob->{_name}} |
… | |
… | |
113 | sub read_pak($;$) { |
136 | sub read_pak($;$) { |
114 | my ($path, $cache) = @_; |
137 | my ($path, $cache) = @_; |
115 | |
138 | |
116 | eval { |
139 | eval { |
117 | defined $cache |
140 | defined $cache |
118 | && -e $cache |
|
|
119 | && -M $cache < -M $path |
141 | && -M $cache < -M $path |
120 | && Storable::retrieve ($cache) |
142 | && load_ref $cache |
121 | } or do { |
143 | } or do { |
122 | my %pak; |
144 | my %pak; |
123 | |
145 | |
124 | open my $fh, "<:raw", $path |
146 | open my $fh, "<", $path |
125 | or Carp::croak "$_[0]: $!"; |
147 | or Carp::croak "$_[0]: $!"; |
|
|
148 | binmode $fh; |
126 | while (<$fh>) { |
149 | while (<$fh>) { |
127 | my ($type, $id, $len, $path) = split; |
150 | my ($type, $id, $len, $path) = split; |
128 | $path =~ s/.*\///; |
151 | $path =~ s/.*\///; |
129 | read $fh, $pak{$path}, $len; |
152 | read $fh, $pak{$path}, $len; |
130 | } |
153 | } |
131 | |
154 | |
132 | Storable::nstore (\%pak, $cache) |
155 | save_ref \%pak, $cache |
133 | if defined $cache; |
156 | if defined $cache; |
134 | |
157 | |
135 | \%pak |
158 | \%pak |
136 | } |
159 | } |
137 | } |
160 | } |
… | |
… | |
139 | sub read_arch($;$) { |
162 | sub read_arch($;$) { |
140 | my ($path, $cache) = @_; |
163 | my ($path, $cache) = @_; |
141 | |
164 | |
142 | eval { |
165 | eval { |
143 | defined $cache |
166 | defined $cache |
144 | && -e $cache |
|
|
145 | && -M $cache < -M $path |
167 | && -M $cache < -M $path |
146 | && Storable::retrieve ($cache) |
168 | && load_ref $cache |
147 | } or do { |
169 | } or do { |
148 | my %arc; |
170 | my %arc; |
149 | my ($more, $prev); |
171 | my ($more, $prev); |
150 | |
172 | |
151 | open my $fh, "<:raw", $path |
173 | open my $fh, "<", $path |
152 | or Carp::croak "$path: $!"; |
174 | or Carp::croak "$path: $!"; |
|
|
175 | |
|
|
176 | binmode $fh; |
153 | |
177 | |
154 | my $parse_block; $parse_block = sub { |
178 | my $parse_block; $parse_block = sub { |
155 | my %arc = @_; |
179 | my %arc = @_; |
156 | |
180 | |
157 | while (<$fh>) { |
181 | while (<$fh>) { |
… | |
… | |
206 | } |
230 | } |
207 | } |
231 | } |
208 | |
232 | |
209 | undef $parse_block; # work around bug in perl not freeing $fh etc. |
233 | undef $parse_block; # work around bug in perl not freeing $fh etc. |
210 | |
234 | |
211 | Storable::nstore (\%arc, $cache) |
235 | save_ref \%arc, $cache |
212 | if defined $cache; |
236 | if defined $cache; |
213 | |
237 | |
214 | \%arc |
238 | \%arc |
215 | } |
239 | } |
216 | } |
240 | } |
… | |
… | |
381 | |
405 | |
382 | mkdir $cachedir, 0777; |
406 | mkdir $cachedir, 0777; |
383 | *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; |
407 | *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; |
384 | } |
408 | } |
385 | |
409 | |
386 | $VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/crossfire" : File::Spec->tmpdir; |
410 | $VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire"; |
387 | |
411 | |
388 | init $VARDIR; |
412 | init $VARDIR; |
389 | |
413 | |
390 | =head1 AUTHOR |
414 | =head1 AUTHOR |
391 | |
415 | |