ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra/Deliantra.pm
(Generate patch)

Comparing deliantra/Deliantra/Deliantra.pm (file contents):
Revision 1.24 by root, Thu Feb 23 03:13:33 2006 UTC vs.
Revision 1.34 by root, Fri Feb 24 11:50:36 2006 UTC

11use strict; 11use strict;
12 12
13use base 'Exporter'; 13use base 'Exporter';
14 14
15use Carp (); 15use Carp ();
16use Storable;
17use File::Spec; 16use File::Spec;
18use List::Util qw(min max); 17use List::Util qw(min max);
18use Storable;
19 19
20#XXX: The map_* procedures scream for a map-object 20#XXX: The map_* procedures scream for a map-object
21 21
22our @EXPORT = 22our @EXPORT =
23 qw(read_pak read_arch %ARCH TILESIZE $TILE %FACE editor_archs arch_extents); 23 qw(read_pak read_arch %ARCH TILESIZE $TILE %FACE editor_archs arch_extents);
25our $LIB = $ENV{CROSSFIRE_LIBDIR} 25our $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
28sub TILESIZE (){ 32 } 28sub TILESIZE (){ 32 }
29 29
30our $CACHEDIR; 30our $VARDIR;
31our %ARCH; 31our %ARCH;
32our %FACE; 32our %FACE;
33our $TILE; 33our $TILE;
34 34
35our %FIELD_MULTILINE = ( 35our %FIELD_MULTILINE = (
44sub MOVE_FLY_LOW (){ 0x2 } 44sub MOVE_FLY_LOW (){ 0x2 }
45sub MOVE_FLY_HIGH (){ 0x4 } 45sub MOVE_FLY_HIGH (){ 0x4 }
46sub MOVE_FLYING (){ 0x6 } 46sub MOVE_FLYING (){ 0x6 }
47sub MOVE_SWIM (){ 0x8 } 47sub MOVE_SWIM (){ 0x8 }
48sub MOVE_ALL (){ 0xf } 48sub MOVE_ALL (){ 0xf }
49
50sub load_ref($) {
51 my ($path) = @_;
52
53 open my $fh, "<", $path
54 or die "$path: $!";
55 binmode $fh;
56 local $/;
57
58 Storable::thaw <$fh>
59}
60
61sub save_ref($$) {
62 my ($ref, $path) = @_;
63
64 open my $fh, ">", "$path~"
65 or die "$path~: $!";
66 binmode $fh;
67 print $fh Storable::freeze $ref;
68 close $fh;
69 rename "$path~", $path
70 or die "$path: $!";
71}
49 72
50sub normalize_arch($) { 73sub normalize_arch($) {
51 my ($ob) = @_; 74 my ($ob) = @_;
52 75
53 my $arch = $ARCH{$ob->{_name}} 76 my $arch = $ARCH{$ob->{_name}}
113sub read_pak($;$) { 136sub read_pak($;$) {
114 my ($path, $cache) = @_; 137 my ($path, $cache) = @_;
115 138
116 eval { 139 eval {
117 defined $cache 140 defined $cache
118 && -e $cache
119 && -M $cache < -M $path 141 && -M $cache < -M $path
120 && Storable::retrieve ($cache) 142 && load_ref $cache
121 } or do { 143 } or do {
122 my %pak; 144 my %pak;
123 145
124 open my $fh, "<:raw", $path 146 open my $fh, "<", $path
125 or Carp::croak "$_[0]: $!"; 147 or Carp::croak "$_[0]: $!";
148 binmode $fh;
126 while (<$fh>) { 149 while (<$fh>) {
127 my ($type, $id, $len, $path) = split; 150 my ($type, $id, $len, $path) = split;
128 $path =~ s/.*\///; 151 $path =~ s/.*\///;
129 read $fh, $pak{$path}, $len; 152 read $fh, $pak{$path}, $len;
130 } 153 }
131 154
132 Storable::nstore (\%pak, $cache) 155 save_ref \%pak, $cache
133 if defined $cache; 156 if defined $cache;
134 157
135 \%pak 158 \%pak
136 } 159 }
137} 160}
139sub read_arch($;$) { 162sub read_arch($;$) {
140 my ($path, $cache) = @_; 163 my ($path, $cache) = @_;
141 164
142 eval { 165 eval {
143 defined $cache 166 defined $cache
144 && -e $cache
145 && -M $cache < -M $path 167 && -M $cache < -M $path
146 && Storable::retrieve ($cache) 168 && load_ref $cache
147 } or do { 169 } or do {
148 my %arc; 170 my %arc;
149 my ($more, $prev); 171 my ($more, $prev);
150 172
151 open my $fh, "<:raw", $path 173 open my $fh, "<", $path
152 or Carp::croak "$path: $!"; 174 or Carp::croak "$path: $!";
175
176 binmode $fh;
153 177
154 my $parse_block; $parse_block = sub { 178 my $parse_block; $parse_block = sub {
155 my %arc = @_; 179 my %arc = @_;
156 180
157 while (<$fh>) { 181 while (<$fh>) {
206 } 230 }
207 } 231 }
208 232
209 undef $parse_block; # work around bug in perl not freeing $fh etc. 233 undef $parse_block; # work around bug in perl not freeing $fh etc.
210 234
211 Storable::nstore (\%arc, $cache) 235 save_ref \%arc, $cache
212 if defined $cache; 236 if defined $cache;
213 237
214 \%arc 238 \%arc
215 } 239 }
216} 240}
381 405
382 mkdir $cachedir, 0777; 406 mkdir $cachedir, 0777;
383 *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; 407 *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst";
384} 408}
385 409
386$CACHEDIR ||= $ENV{HOME} ? "$ENV{HOME}/crossfire" : File::Spec->tmpdir; 410$VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire";
387 411
388init $CACHEDIR; 412init $VARDIR;
389 413
390=head1 AUTHOR 414=head1 AUTHOR
391 415
392 Marc Lehmann <schmorp@schmorp.de> 416 Marc Lehmann <schmorp@schmorp.de>
393 http://home.schmorp.de/ 417 http://home.schmorp.de/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines