… | |
… | |
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, "<: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 | } |
49 | |
69 | |
50 | sub normalize_arch($) { |
70 | sub normalize_arch($) { |
51 | my ($ob) = @_; |
71 | my ($ob) = @_; |
52 | |
72 | |
53 | my $arch = $ARCH{$ob->{_name}} |
73 | my $arch = $ARCH{$ob->{_name}} |
… | |
… | |
113 | sub read_pak($;$) { |
133 | sub read_pak($;$) { |
114 | my ($path, $cache) = @_; |
134 | my ($path, $cache) = @_; |
115 | |
135 | |
116 | eval { |
136 | eval { |
117 | defined $cache |
137 | defined $cache |
118 | && -e $cache |
|
|
119 | && -M $cache < -M $path |
138 | && -M $cache < -M $path |
120 | && Storable::retrieve ($cache) |
139 | && load_ref $cache |
121 | } or do { |
140 | } or do { |
122 | my %pak; |
141 | my %pak; |
123 | |
142 | |
124 | open my $fh, "<:raw", $path |
143 | open my $fh, "<:raw", $path |
125 | or Carp::croak "$_[0]: $!"; |
144 | or Carp::croak "$_[0]: $!"; |
… | |
… | |
127 | my ($type, $id, $len, $path) = split; |
146 | my ($type, $id, $len, $path) = split; |
128 | $path =~ s/.*\///; |
147 | $path =~ s/.*\///; |
129 | read $fh, $pak{$path}, $len; |
148 | read $fh, $pak{$path}, $len; |
130 | } |
149 | } |
131 | |
150 | |
132 | Storable::nstore (\%pak, $cache) |
151 | save_ref \%pak, $cache |
133 | if defined $cache; |
152 | if defined $cache; |
134 | |
153 | |
135 | \%pak |
154 | \%pak |
136 | } |
155 | } |
137 | } |
156 | } |
… | |
… | |
139 | sub read_arch($;$) { |
158 | sub read_arch($;$) { |
140 | my ($path, $cache) = @_; |
159 | my ($path, $cache) = @_; |
141 | |
160 | |
142 | eval { |
161 | eval { |
143 | defined $cache |
162 | defined $cache |
144 | && -e $cache |
|
|
145 | && -M $cache < -M $path |
163 | && -M $cache < -M $path |
146 | && Storable::retrieve ($cache) |
164 | && load_ref $cache |
147 | } or do { |
165 | } or do { |
148 | my %arc; |
166 | my %arc; |
149 | my ($more, $prev); |
167 | my ($more, $prev); |
150 | |
168 | |
151 | open my $fh, "<:raw", $path |
169 | open my $fh, "<:raw", $path |
… | |
… | |
206 | } |
224 | } |
207 | } |
225 | } |
208 | |
226 | |
209 | 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. |
210 | |
228 | |
211 | Storable::nstore (\%arc, $cache) |
229 | save_ref \%arc, $cache |
212 | if defined $cache; |
230 | if defined $cache; |
213 | |
231 | |
214 | \%arc |
232 | \%arc |
215 | } |
233 | } |
216 | } |
234 | } |
… | |
… | |
381 | |
399 | |
382 | mkdir $cachedir, 0777; |
400 | mkdir $cachedir, 0777; |
383 | *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; |
401 | *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; |
384 | } |
402 | } |
385 | |
403 | |
386 | $CACHEDIR ||= $ENV{HOME} ? "$ENV{HOME}/crossfire" : File::Spec->tmpdir; |
404 | $VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire"; |
387 | |
405 | |
388 | init $CACHEDIR; |
406 | init $VARDIR; |
389 | |
407 | |
390 | =head1 AUTHOR |
408 | =head1 AUTHOR |
391 | |
409 | |
392 | Marc Lehmann <schmorp@schmorp.de> |
410 | Marc Lehmann <schmorp@schmorp.de> |
393 | http://home.schmorp.de/ |
411 | http://home.schmorp.de/ |