… | |
… | |
12 | |
12 | |
13 | use base 'Exporter'; |
13 | use base 'Exporter'; |
14 | |
14 | |
15 | use Carp (); |
15 | use Carp (); |
16 | use Storable; |
16 | use Storable; |
|
|
17 | use File::Spec; |
17 | use List::Util qw(min max); |
18 | use List::Util qw(min max); |
18 | |
19 | |
19 | #XXX: The map_* procedures scream for a map-object |
20 | #XXX: The map_* procedures scream for a map-object |
20 | |
21 | |
21 | our @EXPORT = |
22 | our @EXPORT = |
… | |
… | |
24 | our $LIB = $ENV{CROSSFIRE_LIBDIR} |
25 | our $LIB = $ENV{CROSSFIRE_LIBDIR} |
25 | or Carp::croak "\$CROSSFIRE_LIBDIR must be set\n"; |
26 | or Carp::croak "\$CROSSFIRE_LIBDIR must be set\n"; |
26 | |
27 | |
27 | sub TILESIZE (){ 32 } |
28 | sub TILESIZE (){ 32 } |
28 | |
29 | |
29 | our $CACHEDIR; |
30 | our $VARDIR; |
30 | our %ARCH; |
31 | our %ARCH; |
31 | our %FACE; |
32 | our %FACE; |
32 | our $TILE; |
33 | our $TILE; |
33 | |
34 | |
34 | our %FIELD_MULTILINE = ( |
35 | our %FIELD_MULTILINE = ( |
… | |
… | |
43 | sub MOVE_FLY_LOW (){ 0x2 } |
44 | sub MOVE_FLY_LOW (){ 0x2 } |
44 | sub MOVE_FLY_HIGH (){ 0x4 } |
45 | sub MOVE_FLY_HIGH (){ 0x4 } |
45 | sub MOVE_FLYING (){ 0x6 } |
46 | sub MOVE_FLYING (){ 0x6 } |
46 | sub MOVE_SWIM (){ 0x8 } |
47 | sub MOVE_SWIM (){ 0x8 } |
47 | sub MOVE_ALL (){ 0xf } |
48 | sub MOVE_ALL (){ 0xf } |
|
|
49 | |
|
|
50 | sub load_ref($) { |
|
|
51 | my ($path) = @_; |
|
|
52 | |
|
|
53 | open my $fh, ">:raw", "$path~" |
|
|
54 | or die "$path~: $!"; |
|
|
55 | local $/; |
|
|
56 | Storable::thaw <$fh> |
|
|
57 | } |
|
|
58 | |
|
|
59 | sub save_ref($$) { |
|
|
60 | my ($ref, $path) = @_; |
|
|
61 | |
|
|
62 | open my $fh, ">:raw", "$path~" |
|
|
63 | or die "$path~: $!"; |
|
|
64 | print $fh Storable::nfreeze $ref; |
|
|
65 | close $fh; |
|
|
66 | rename "$path~", $path |
|
|
67 | or die "$path: $!"; |
|
|
68 | } |
48 | |
69 | |
49 | sub normalize_arch($) { |
70 | sub normalize_arch($) { |
50 | my ($ob) = @_; |
71 | my ($ob) = @_; |
51 | |
72 | |
52 | my $arch = $ARCH{$ob->{_name}} |
73 | my $arch = $ARCH{$ob->{_name}} |
… | |
… | |
113 | my ($path, $cache) = @_; |
134 | my ($path, $cache) = @_; |
114 | |
135 | |
115 | eval { |
136 | eval { |
116 | defined $cache |
137 | defined $cache |
117 | && -M $cache < -M $path |
138 | && -M $cache < -M $path |
118 | && Storable::retrieve $cache |
139 | && load_ref $cache |
119 | } or do { |
140 | } or do { |
120 | my %pak; |
141 | my %pak; |
121 | |
142 | |
122 | open my $fh, "<:raw", $path |
143 | open my $fh, "<:raw", $path |
123 | or Carp::croak "$_[0]: $!"; |
144 | or Carp::croak "$_[0]: $!"; |
… | |
… | |
125 | my ($type, $id, $len, $path) = split; |
146 | my ($type, $id, $len, $path) = split; |
126 | $path =~ s/.*\///; |
147 | $path =~ s/.*\///; |
127 | read $fh, $pak{$path}, $len; |
148 | read $fh, $pak{$path}, $len; |
128 | } |
149 | } |
129 | |
150 | |
130 | Storable::nstore \%pak, $cache |
151 | save_ref \%pak, $cache |
131 | if defined $cache; |
152 | if defined $cache; |
132 | |
153 | |
133 | \%pak |
154 | \%pak |
134 | } |
155 | } |
135 | } |
156 | } |
… | |
… | |
138 | my ($path, $cache) = @_; |
159 | my ($path, $cache) = @_; |
139 | |
160 | |
140 | eval { |
161 | eval { |
141 | defined $cache |
162 | defined $cache |
142 | && -M $cache < -M $path |
163 | && -M $cache < -M $path |
143 | && Storable::retrieve $cache |
164 | && load_ref $cache |
144 | } or do { |
165 | } or do { |
145 | my %arc; |
166 | my %arc; |
146 | my ($more, $prev); |
167 | my ($more, $prev); |
147 | |
168 | |
148 | open my $fh, "<:raw", $path |
169 | open my $fh, "<:raw", $path |
… | |
… | |
203 | } |
224 | } |
204 | } |
225 | } |
205 | |
226 | |
206 | undef $parse_block; # work around bug in perl not freeing $fh etc. |
227 | undef $parse_block; # work around bug in perl not freeing $fh etc. |
207 | |
228 | |
208 | Storable::nstore \%arc, $cache |
229 | save_ref \%arc, $cache |
209 | if defined $cache; |
230 | if defined $cache; |
210 | |
231 | |
211 | \%arc |
232 | \%arc |
212 | } |
233 | } |
213 | } |
234 | } |
… | |
… | |
262 | |
283 | |
263 | } else { |
284 | } else { |
264 | # single face |
285 | # single face |
265 | return (0, 0, 0, 0); |
286 | return (0, 0, 0, 0); |
266 | } |
287 | } |
267 | } |
|
|
268 | |
|
|
269 | sub init($) { |
|
|
270 | my ($cachedir) = @_; |
|
|
271 | |
|
|
272 | return if %ARCH; |
|
|
273 | |
|
|
274 | *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; |
|
|
275 | } |
288 | } |
276 | |
289 | |
277 | =item $type = arch_attr $arch |
290 | =item $type = arch_attr $arch |
278 | |
291 | |
279 | Returns a hashref describing the object and its attributes. It can contain |
292 | Returns a hashref describing the object and its attributes. It can contain |
… | |
… | |
312 | } |
325 | } |
313 | |
326 | |
314 | $attr = $_; |
327 | $attr = $_; |
315 | } |
328 | } |
316 | } |
329 | } |
317 | |
|
|
318 | use PApp::Util; |
|
|
319 | warn PApp::Util::dumpval $attr; |
|
|
320 | |
330 | |
321 | $attr || \%Crossfire::Data::DEFAULT_ATTR; |
331 | $attr || \%Crossfire::Data::DEFAULT_ATTR; |
322 | } |
332 | } |
323 | |
333 | |
324 | sub arch_edit_sections { |
334 | sub arch_edit_sections { |
… | |
… | |
380 | # return(edit_type); |
390 | # return(edit_type); |
381 | # |
391 | # |
382 | # |
392 | # |
383 | } |
393 | } |
384 | |
394 | |
385 | $CACHEDIR ||= "$ENV{HOME}/.crossfire"; |
395 | sub init($) { |
|
|
396 | my ($cachedir) = @_; |
386 | |
397 | |
|
|
398 | return if %ARCH; |
|
|
399 | |
|
|
400 | mkdir $cachedir, 0777; |
|
|
401 | *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; |
|
|
402 | } |
|
|
403 | |
|
|
404 | $VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/crossfire" : File::Spec->tmpdir; |
|
|
405 | |
387 | init $CACHEDIR; |
406 | init $VARDIR; |
388 | |
407 | |
389 | =head1 AUTHOR |
408 | =head1 AUTHOR |
390 | |
409 | |
391 | Marc Lehmann <schmorp@schmorp.de> |
410 | Marc Lehmann <schmorp@schmorp.de> |
392 | http://home.schmorp.de/ |
411 | http://home.schmorp.de/ |