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.26 by root, Thu Feb 23 05:23:01 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);
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 warn "hoi\n";#d# 235 save_ref \%arc, $cache
212 Storable::nstore (\%arc, $cache)
213 if defined $cache; 236 if defined $cache;
214 warn "hox\n";#d#
215 237
216 \%arc 238 \%arc
217 } 239 }
218} 240}
219 241
383 405
384 mkdir $cachedir, 0777; 406 mkdir $cachedir, 0777;
385 *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; 407 *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst";
386} 408}
387 409
388$VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/crossfire" : File::Spec->tmpdir; 410$VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire";
389 411
390init $VARDIR; 412init $VARDIR;
391 413
392=head1 AUTHOR 414=head1 AUTHOR
393 415

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines