… | |
… | |
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); |
… | |
… | |
25 | our $LIB = $ENV{CROSSFIRE_LIBDIR} |
25 | our $LIB = $ENV{CROSSFIRE_LIBDIR} |
26 | or Carp::croak "\$CROSSFIRE_LIBDIR must be set\n"; |
26 | or Carp::croak "\$CROSSFIRE_LIBDIR must be set\n"; |
27 | |
27 | |
28 | sub TILESIZE (){ 32 } |
28 | sub TILESIZE (){ 32 } |
29 | |
29 | |
30 | our $CACHEDIR; |
30 | our $VARDIR; |
31 | our %ARCH; |
31 | our %ARCH; |
32 | our %FACE; |
32 | our %FACE; |
33 | our $TILE; |
33 | our $TILE; |
34 | |
34 | |
35 | our %FIELD_MULTILINE = ( |
35 | our %FIELD_MULTILINE = ( |
… | |
… | |
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}} |
… | |
… | |
114 | my ($path, $cache) = @_; |
137 | my ($path, $cache) = @_; |
115 | |
138 | |
116 | eval { |
139 | eval { |
117 | defined $cache |
140 | defined $cache |
118 | && -M $cache < -M $path |
141 | && -M $cache < -M $path |
119 | && Storable::retrieve $cache |
142 | && load_ref $cache |
120 | } or do { |
143 | } or do { |
121 | my %pak; |
144 | my %pak; |
122 | |
145 | |
123 | open my $fh, "<:raw", $path |
146 | open my $fh, "<", $path |
124 | or Carp::croak "$_[0]: $!"; |
147 | or Carp::croak "$_[0]: $!"; |
|
|
148 | binmode $fh; |
125 | while (<$fh>) { |
149 | while (<$fh>) { |
126 | my ($type, $id, $len, $path) = split; |
150 | my ($type, $id, $len, $path) = split; |
127 | $path =~ s/.*\///; |
151 | $path =~ s/.*\///; |
128 | read $fh, $pak{$path}, $len; |
152 | read $fh, $pak{$path}, $len; |
129 | } |
153 | } |
130 | |
154 | |
131 | Storable::nstore \%pak, $cache |
155 | save_ref \%pak, $cache |
132 | if defined $cache; |
156 | if defined $cache; |
133 | |
157 | |
134 | \%pak |
158 | \%pak |
135 | } |
159 | } |
136 | } |
160 | } |
… | |
… | |
139 | my ($path, $cache) = @_; |
163 | my ($path, $cache) = @_; |
140 | |
164 | |
141 | eval { |
165 | eval { |
142 | defined $cache |
166 | defined $cache |
143 | && -M $cache < -M $path |
167 | && -M $cache < -M $path |
144 | && Storable::retrieve $cache |
168 | && load_ref $cache |
145 | } or do { |
169 | } or do { |
146 | my %arc; |
170 | my %arc; |
147 | my ($more, $prev); |
171 | my ($more, $prev); |
148 | |
172 | |
149 | open my $fh, "<:raw", $path |
173 | open my $fh, "<", $path |
150 | or Carp::croak "$path: $!"; |
174 | or Carp::croak "$path: $!"; |
|
|
175 | |
|
|
176 | binmode $fh; |
151 | |
177 | |
152 | my $parse_block; $parse_block = sub { |
178 | my $parse_block; $parse_block = sub { |
153 | my %arc = @_; |
179 | my %arc = @_; |
154 | |
180 | |
155 | while (<$fh>) { |
181 | while (<$fh>) { |
… | |
… | |
204 | } |
230 | } |
205 | } |
231 | } |
206 | |
232 | |
207 | 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. |
208 | |
234 | |
209 | Storable::nstore \%arc, $cache |
235 | save_ref \%arc, $cache |
210 | if defined $cache; |
236 | if defined $cache; |
211 | |
237 | |
212 | \%arc |
238 | \%arc |
213 | } |
239 | } |
214 | } |
240 | } |
… | |
… | |
263 | |
289 | |
264 | } else { |
290 | } else { |
265 | # single face |
291 | # single face |
266 | return (0, 0, 0, 0); |
292 | return (0, 0, 0, 0); |
267 | } |
293 | } |
268 | } |
|
|
269 | |
|
|
270 | sub init($) { |
|
|
271 | my ($cachedir) = @_; |
|
|
272 | |
|
|
273 | return if %ARCH; |
|
|
274 | |
|
|
275 | mkdir 0777, $cachedir; |
|
|
276 | *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; |
|
|
277 | } |
294 | } |
278 | |
295 | |
279 | =item $type = arch_attr $arch |
296 | =item $type = arch_attr $arch |
280 | |
297 | |
281 | Returns a hashref describing the object and its attributes. It can contain |
298 | Returns a hashref describing the object and its attributes. It can contain |
… | |
… | |
379 | # return(edit_type); |
396 | # return(edit_type); |
380 | # |
397 | # |
381 | # |
398 | # |
382 | } |
399 | } |
383 | |
400 | |
|
|
401 | sub init($) { |
|
|
402 | my ($cachedir) = @_; |
|
|
403 | |
|
|
404 | return if %ARCH; |
|
|
405 | |
|
|
406 | mkdir $cachedir, 0777; |
|
|
407 | *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; |
|
|
408 | } |
|
|
409 | |
384 | $CACHEDIR ||= $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir; |
410 | $VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire"; |
385 | |
411 | |
386 | init $CACHEDIR; |
412 | init $VARDIR; |
387 | |
413 | |
388 | =head1 AUTHOR |
414 | =head1 AUTHOR |
389 | |
415 | |
390 | Marc Lehmann <schmorp@schmorp.de> |
416 | Marc Lehmann <schmorp@schmorp.de> |
391 | http://home.schmorp.de/ |
417 | http://home.schmorp.de/ |