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.25 by root, Thu Feb 23 03:14:26 2006 UTC vs.
Revision 1.30 by root, Thu Feb 23 14:54:44 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);
19 18
20#XXX: The map_* procedures scream for a map-object 19#XXX: The map_* procedures scream for a map-object
21 20
44sub MOVE_FLY_LOW (){ 0x2 } 43sub MOVE_FLY_LOW (){ 0x2 }
45sub MOVE_FLY_HIGH (){ 0x4 } 44sub MOVE_FLY_HIGH (){ 0x4 }
46sub MOVE_FLYING (){ 0x6 } 45sub MOVE_FLYING (){ 0x6 }
47sub MOVE_SWIM (){ 0x8 } 46sub MOVE_SWIM (){ 0x8 }
48sub MOVE_ALL (){ 0xf } 47sub MOVE_ALL (){ 0xf }
48
49BEGIN {
50 if ($^O eq "MSWin32") {
51 eval "use FreezeThaw qw(freeze thaw)";
52 } else {
53 eval "use Storable qw(freeze thaw)";
54 }
55}
56
57sub load_ref($) {
58 my ($path) = @_;
59
60 open my $fh, "<:raw", "$path~"
61 or die "$path~: $!";
62 local $/;
63 thaw <$fh>
64}
65
66sub 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
50sub normalize_arch($) { 78sub normalize_arch($) {
51 my ($ob) = @_; 79 my ($ob) = @_;
52 80
53 my $arch = $ARCH{$ob->{_name}} 81 my $arch = $ARCH{$ob->{_name}}
113sub read_pak($;$) { 141sub 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}
139sub read_arch($;$) { 166sub 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
388init $VARDIR; 414init $VARDIR;
389 415
390=head1 AUTHOR 416=head1 AUTHOR
391 417

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines