… | |
… | |
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 Data::Dumper"; |
|
|
52 | } else { |
|
|
53 | eval "use Storable qw(freeze thaw)"; |
|
|
54 | } |
|
|
55 | } |
|
|
56 | |
|
|
57 | sub load_ref($) { |
|
|
58 | my ($path) = @_; |
|
|
59 | |
|
|
60 | open my $fh, "<", $path |
|
|
61 | or die "$path: $!"; |
|
|
62 | binmode $fh; |
|
|
63 | local $/; |
|
|
64 | |
|
|
65 | $^O eq "MSWin32" ? eval <$fh> : thaw (<$fh>) |
|
|
66 | } |
|
|
67 | |
|
|
68 | sub save_ref($$) { |
|
|
69 | my ($ref, $path) = @_; |
|
|
70 | |
|
|
71 | open my $fh, ">", "$path~" |
|
|
72 | or die "$path~: $!"; |
|
|
73 | binmode $fh; |
|
|
74 | print $fh $^O eq "MSWin32" |
|
|
75 | ? Data::Dumper->new ([$ref])->Deepcopy (1)->Purity (1)->Terse (1)->Indent (0)->Dump |
|
|
76 | : freeze ($ref); |
|
|
77 | close $fh; |
|
|
78 | rename "$path~", $path |
|
|
79 | or die "$path: $!"; |
|
|
80 | } |
49 | |
81 | |
50 | sub normalize_arch($) { |
82 | sub normalize_arch($) { |
51 | my ($ob) = @_; |
83 | my ($ob) = @_; |
52 | |
84 | |
53 | my $arch = $ARCH{$ob->{_name}} |
85 | my $arch = $ARCH{$ob->{_name}} |
… | |
… | |
113 | sub read_pak($;$) { |
145 | sub read_pak($;$) { |
114 | my ($path, $cache) = @_; |
146 | my ($path, $cache) = @_; |
115 | |
147 | |
116 | eval { |
148 | eval { |
117 | defined $cache |
149 | defined $cache |
118 | && -e $cache |
|
|
119 | && -M $cache < -M $path |
150 | && -M $cache < -M $path |
120 | && Storable::retrieve ($cache) |
151 | && load_ref $cache |
121 | } or do { |
152 | } or do { |
122 | my %pak; |
153 | my %pak; |
123 | |
154 | |
124 | open my $fh, "<:raw", $path |
155 | open my $fh, "<", $path |
125 | or Carp::croak "$_[0]: $!"; |
156 | or Carp::croak "$_[0]: $!"; |
|
|
157 | binmode $fh; |
126 | while (<$fh>) { |
158 | while (<$fh>) { |
127 | my ($type, $id, $len, $path) = split; |
159 | my ($type, $id, $len, $path) = split; |
128 | $path =~ s/.*\///; |
160 | $path =~ s/.*\///; |
129 | read $fh, $pak{$path}, $len; |
161 | read $fh, $pak{$path}, $len; |
130 | } |
162 | } |
131 | |
163 | |
132 | Storable::nstore (\%pak, $cache) |
164 | save_ref \%pak, $cache |
133 | if defined $cache; |
165 | if defined $cache; |
134 | |
166 | |
135 | \%pak |
167 | \%pak |
136 | } |
168 | } |
137 | } |
169 | } |
… | |
… | |
139 | sub read_arch($;$) { |
171 | sub read_arch($;$) { |
140 | my ($path, $cache) = @_; |
172 | my ($path, $cache) = @_; |
141 | |
173 | |
142 | eval { |
174 | eval { |
143 | defined $cache |
175 | defined $cache |
144 | && -e $cache |
|
|
145 | && -M $cache < -M $path |
176 | && -M $cache < -M $path |
146 | && Storable::retrieve ($cache) |
177 | && load_ref $cache |
147 | } or do { |
178 | } or do { |
148 | my %arc; |
179 | my %arc; |
149 | my ($more, $prev); |
180 | my ($more, $prev); |
150 | |
181 | |
151 | open my $fh, "<:raw", $path |
182 | open my $fh, "<", $path |
152 | or Carp::croak "$path: $!"; |
183 | or Carp::croak "$path: $!"; |
|
|
184 | |
|
|
185 | binmode $fh; |
153 | |
186 | |
154 | my $parse_block; $parse_block = sub { |
187 | my $parse_block; $parse_block = sub { |
155 | my %arc = @_; |
188 | my %arc = @_; |
156 | |
189 | |
157 | while (<$fh>) { |
190 | while (<$fh>) { |
… | |
… | |
206 | } |
239 | } |
207 | } |
240 | } |
208 | |
241 | |
209 | undef $parse_block; # work around bug in perl not freeing $fh etc. |
242 | undef $parse_block; # work around bug in perl not freeing $fh etc. |
210 | |
243 | |
211 | Storable::nstore (\%arc, $cache) |
244 | save_ref \%arc, $cache |
212 | if defined $cache; |
245 | if defined $cache; |
213 | |
246 | |
214 | \%arc |
247 | \%arc |
215 | } |
248 | } |
216 | } |
249 | } |
… | |
… | |
381 | |
414 | |
382 | mkdir $cachedir, 0777; |
415 | mkdir $cachedir, 0777; |
383 | *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; |
416 | *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; |
384 | } |
417 | } |
385 | |
418 | |
386 | $VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/crossfire" : File::Spec->tmpdir; |
419 | $VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire"; |
387 | |
420 | |
388 | init $VARDIR; |
421 | init $VARDIR; |
389 | |
422 | |
390 | =head1 AUTHOR |
423 | =head1 AUTHOR |
391 | |
424 | |