… | |
… | |
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 | |
… | |
… | |
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}} |
… | |
… | |
113 | sub read_pak($;$) { |
141 | sub read_pak($;$) { |
114 | my ($path, $cache) = @_; |
142 | my ($path, $cache) = @_; |
115 | |
143 | |
116 | eval { |
144 | eval { |
117 | defined $cache |
145 | defined $cache |
118 | && -e $cache |
|
|
119 | && -M $cache < -M $path |
146 | && -M $cache < -M $path |
120 | && Storable::retrieve ($cache) |
147 | && load_ref $cache |
121 | } or do { |
148 | } or do { |
122 | my %pak; |
149 | my %pak; |
123 | |
150 | |
124 | open my $fh, "<:raw", $path |
151 | open my $fh, "<:raw", $path |
125 | or Carp::croak "$_[0]: $!"; |
152 | or Carp::croak "$_[0]: $!"; |
… | |
… | |
127 | my ($type, $id, $len, $path) = split; |
154 | my ($type, $id, $len, $path) = split; |
128 | $path =~ s/.*\///; |
155 | $path =~ s/.*\///; |
129 | read $fh, $pak{$path}, $len; |
156 | read $fh, $pak{$path}, $len; |
130 | } |
157 | } |
131 | |
158 | |
132 | Storable::nstore (\%pak, $cache) |
159 | save_ref \%pak, $cache |
133 | if defined $cache; |
160 | if defined $cache; |
134 | |
161 | |
135 | \%pak |
162 | \%pak |
136 | } |
163 | } |
137 | } |
164 | } |
… | |
… | |
139 | sub read_arch($;$) { |
166 | sub read_arch($;$) { |
140 | my ($path, $cache) = @_; |
167 | my ($path, $cache) = @_; |
141 | |
168 | |
142 | eval { |
169 | eval { |
143 | defined $cache |
170 | defined $cache |
144 | && -e $cache |
|
|
145 | && -M $cache < -M $path |
171 | && -M $cache < -M $path |
146 | && Storable::retrieve ($cache) |
172 | && load_ref $cache |
147 | } or do { |
173 | } or do { |
148 | my %arc; |
174 | my %arc; |
149 | my ($more, $prev); |
175 | my ($more, $prev); |
150 | |
176 | |
151 | open my $fh, "<:raw", $path |
177 | open my $fh, "<:raw", $path |
… | |
… | |
206 | } |
232 | } |
207 | } |
233 | } |
208 | |
234 | |
209 | 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. |
210 | |
236 | |
211 | Storable::nstore (\%arc, $cache) |
237 | save_ref \%arc, $cache |
212 | if defined $cache; |
238 | if defined $cache; |
213 | |
239 | |
214 | \%arc |
240 | \%arc |
215 | } |
241 | } |
216 | } |
242 | } |
… | |
… | |
381 | |
407 | |
382 | mkdir $cachedir, 0777; |
408 | mkdir $cachedir, 0777; |
383 | *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; |
409 | *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; |
384 | } |
410 | } |
385 | |
411 | |
386 | $VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/crossfire" : File::Spec->tmpdir; |
412 | $VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire"; |
387 | |
413 | |
388 | init $VARDIR; |
414 | init $VARDIR; |
389 | |
415 | |
390 | =head1 AUTHOR |
416 | =head1 AUTHOR |
391 | |
417 | |