… | |
… | |
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 | |
20 | #XXX: The map_* procedures scream for a map-object |
19 | #XXX: The map_* procedures scream for a map-object |
21 | |
20 | |
… | |
… | |
25 | our $LIB = $ENV{CROSSFIRE_LIBDIR} |
24 | our $LIB = $ENV{CROSSFIRE_LIBDIR} |
26 | or Carp::croak "\$CROSSFIRE_LIBDIR must be set\n"; |
25 | or Carp::croak "\$CROSSFIRE_LIBDIR must be set\n"; |
27 | |
26 | |
28 | sub TILESIZE (){ 32 } |
27 | sub TILESIZE (){ 32 } |
29 | |
28 | |
30 | our $CACHEDIR; |
29 | our $VARDIR; |
31 | our %ARCH; |
30 | our %ARCH; |
32 | our %FACE; |
31 | our %FACE; |
33 | our $TILE; |
32 | our $TILE; |
34 | |
33 | |
35 | our %FIELD_MULTILINE = ( |
34 | our %FIELD_MULTILINE = ( |
… | |
… | |
44 | sub MOVE_FLY_LOW (){ 0x2 } |
43 | sub MOVE_FLY_LOW (){ 0x2 } |
45 | sub MOVE_FLY_HIGH (){ 0x4 } |
44 | sub MOVE_FLY_HIGH (){ 0x4 } |
46 | sub MOVE_FLYING (){ 0x6 } |
45 | sub MOVE_FLYING (){ 0x6 } |
47 | sub MOVE_SWIM (){ 0x8 } |
46 | sub MOVE_SWIM (){ 0x8 } |
48 | sub MOVE_ALL (){ 0xf } |
47 | sub MOVE_ALL (){ 0xf } |
|
|
48 | |
|
|
49 | BEGIN { |
|
|
50 | if ($^O eq "MSWin32") { |
|
|
51 | eval "use FreezeThaw qw(freeze thaw)"; |
|
|
52 | } else { |
|
|
53 | eval "use Storable qw(freeze thaw)"; |
|
|
54 | } |
|
|
55 | } |
|
|
56 | |
|
|
57 | sub load_ref($) { |
|
|
58 | my ($path) = @_; |
|
|
59 | |
|
|
60 | open my $fh, "<:raw", "$path~" |
|
|
61 | or die "$path~: $!"; |
|
|
62 | local $/; |
|
|
63 | thaw <$fh> |
|
|
64 | } |
|
|
65 | |
|
|
66 | sub save_ref($$) { |
|
|
67 | my ($ref, $path) = @_; |
|
|
68 | |
|
|
69 | open my $fh, ">:raw", "$path~" |
|
|
70 | or die "$path~: $!"; |
|
|
71 | my $ref = freeze $ref; |
|
|
72 | print $fh $ref; |
|
|
73 | close $fh; |
|
|
74 | rename "$path~", $path |
|
|
75 | or die "$path: $!"; |
|
|
76 | } |
49 | |
77 | |
50 | sub normalize_arch($) { |
78 | sub normalize_arch($) { |
51 | my ($ob) = @_; |
79 | my ($ob) = @_; |
52 | |
80 | |
53 | my $arch = $ARCH{$ob->{_name}} |
81 | my $arch = $ARCH{$ob->{_name}} |
… | |
… | |
114 | my ($path, $cache) = @_; |
142 | my ($path, $cache) = @_; |
115 | |
143 | |
116 | eval { |
144 | eval { |
117 | defined $cache |
145 | defined $cache |
118 | && -M $cache < -M $path |
146 | && -M $cache < -M $path |
119 | && Storable::retrieve $cache |
147 | && load_ref $cache |
120 | } or do { |
148 | } or do { |
121 | my %pak; |
149 | my %pak; |
122 | |
150 | |
123 | open my $fh, "<:raw", $path |
151 | open my $fh, "<:raw", $path |
124 | or Carp::croak "$_[0]: $!"; |
152 | or Carp::croak "$_[0]: $!"; |
… | |
… | |
126 | my ($type, $id, $len, $path) = split; |
154 | my ($type, $id, $len, $path) = split; |
127 | $path =~ s/.*\///; |
155 | $path =~ s/.*\///; |
128 | read $fh, $pak{$path}, $len; |
156 | read $fh, $pak{$path}, $len; |
129 | } |
157 | } |
130 | |
158 | |
131 | Storable::nstore \%pak, $cache |
159 | save_ref \%pak, $cache |
132 | if defined $cache; |
160 | if defined $cache; |
133 | |
161 | |
134 | \%pak |
162 | \%pak |
135 | } |
163 | } |
136 | } |
164 | } |
… | |
… | |
139 | my ($path, $cache) = @_; |
167 | my ($path, $cache) = @_; |
140 | |
168 | |
141 | eval { |
169 | eval { |
142 | defined $cache |
170 | defined $cache |
143 | && -M $cache < -M $path |
171 | && -M $cache < -M $path |
144 | && Storable::retrieve $cache |
172 | && load_ref $cache |
145 | } or do { |
173 | } or do { |
146 | my %arc; |
174 | my %arc; |
147 | my ($more, $prev); |
175 | my ($more, $prev); |
148 | |
176 | |
149 | open my $fh, "<:raw", $path |
177 | open my $fh, "<:raw", $path |
… | |
… | |
204 | } |
232 | } |
205 | } |
233 | } |
206 | |
234 | |
207 | undef $parse_block; # work around bug in perl not freeing $fh etc. |
235 | undef $parse_block; # work around bug in perl not freeing $fh etc. |
208 | |
236 | |
209 | Storable::nstore \%arc, $cache |
237 | save_ref \%arc, $cache |
210 | if defined $cache; |
238 | if defined $cache; |
211 | |
239 | |
212 | \%arc |
240 | \%arc |
213 | } |
241 | } |
214 | } |
242 | } |
… | |
… | |
263 | |
291 | |
264 | } else { |
292 | } else { |
265 | # single face |
293 | # single face |
266 | return (0, 0, 0, 0); |
294 | return (0, 0, 0, 0); |
267 | } |
295 | } |
268 | } |
|
|
269 | |
|
|
270 | sub init($) { |
|
|
271 | my ($cachedir) = @_; |
|
|
272 | |
|
|
273 | return if %ARCH; |
|
|
274 | |
|
|
275 | *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; |
|
|
276 | } |
296 | } |
277 | |
297 | |
278 | =item $type = arch_attr $arch |
298 | =item $type = arch_attr $arch |
279 | |
299 | |
280 | Returns a hashref describing the object and its attributes. It can contain |
300 | Returns a hashref describing the object and its attributes. It can contain |
… | |
… | |
378 | # return(edit_type); |
398 | # return(edit_type); |
379 | # |
399 | # |
380 | # |
400 | # |
381 | } |
401 | } |
382 | |
402 | |
|
|
403 | sub init($) { |
|
|
404 | my ($cachedir) = @_; |
|
|
405 | |
|
|
406 | return if %ARCH; |
|
|
407 | |
|
|
408 | mkdir $cachedir, 0777; |
|
|
409 | *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; |
|
|
410 | } |
|
|
411 | |
383 | $CACHEDIR ||= $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir; |
412 | $VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire"; |
384 | |
413 | |
385 | init $CACHEDIR; |
414 | init $VARDIR; |
386 | |
415 | |
387 | =head1 AUTHOR |
416 | =head1 AUTHOR |
388 | |
417 | |
389 | Marc Lehmann <schmorp@schmorp.de> |
418 | Marc Lehmann <schmorp@schmorp.de> |
390 | http://home.schmorp.de/ |
419 | http://home.schmorp.de/ |