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.31 by root, Thu Feb 23 15:01:43 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, "<", $path
61 or die "$path: $!";
62 binmode $fh;
63 local $/;
64 thaw <$fh>
65}
66
67sub save_ref($$) {
68 my ($ref, $path) = @_;
69
70 open my $fh, ">", "$path~"
71 or die "$path~: $!";
72 binmode $fh;
73 print $fh freeze $ref;
74 close $fh;
75 rename "$path~", $path
76 or die "$path: $!";
77}
49 78
50sub normalize_arch($) { 79sub normalize_arch($) {
51 my ($ob) = @_; 80 my ($ob) = @_;
52 81
53 my $arch = $ARCH{$ob->{_name}} 82 my $arch = $ARCH{$ob->{_name}}
113sub read_pak($;$) { 142sub read_pak($;$) {
114 my ($path, $cache) = @_; 143 my ($path, $cache) = @_;
115 144
116 eval { 145 eval {
117 defined $cache 146 defined $cache
118 && -e $cache
119 && -M $cache < -M $path 147 && -M $cache < -M $path
120 && Storable::retrieve ($cache) 148 && load_ref $cache
121 } or do { 149 } or do {
122 my %pak; 150 my %pak;
123 151
124 open my $fh, "<:raw", $path 152 open my $fh, "<", $path
125 or Carp::croak "$_[0]: $!"; 153 or Carp::croak "$_[0]: $!";
154 binmode $fh;
126 while (<$fh>) { 155 while (<$fh>) {
127 my ($type, $id, $len, $path) = split; 156 my ($type, $id, $len, $path) = split;
128 $path =~ s/.*\///; 157 $path =~ s/.*\///;
129 read $fh, $pak{$path}, $len; 158 read $fh, $pak{$path}, $len;
130 } 159 }
131 160
132 Storable::nstore (\%pak, $cache) 161 save_ref \%pak, $cache
133 if defined $cache; 162 if defined $cache;
134 163
135 \%pak 164 \%pak
136 } 165 }
137} 166}
139sub read_arch($;$) { 168sub read_arch($;$) {
140 my ($path, $cache) = @_; 169 my ($path, $cache) = @_;
141 170
142 eval { 171 eval {
143 defined $cache 172 defined $cache
144 && -e $cache
145 && -M $cache < -M $path 173 && -M $cache < -M $path
146 && Storable::retrieve ($cache) 174 && load_ref $cache
147 } or do { 175 } or do {
148 my %arc; 176 my %arc;
149 my ($more, $prev); 177 my ($more, $prev);
150 178
151 open my $fh, "<:raw", $path 179 open my $fh, "<", $path
152 or Carp::croak "$path: $!"; 180 or Carp::croak "$path: $!";
181
182 binmode $fh;
153 183
154 my $parse_block; $parse_block = sub { 184 my $parse_block; $parse_block = sub {
155 my %arc = @_; 185 my %arc = @_;
156 186
157 while (<$fh>) { 187 while (<$fh>) {
206 } 236 }
207 } 237 }
208 238
209 undef $parse_block; # work around bug in perl not freeing $fh etc. 239 undef $parse_block; # work around bug in perl not freeing $fh etc.
210 240
211 warn "hoi\n";#d# 241 save_ref \%arc, $cache
212 Storable::nstore (\%arc, $cache)
213 if defined $cache; 242 if defined $cache;
214 warn "hox\n";#d#
215 243
216 \%arc 244 \%arc
217 } 245 }
218} 246}
219 247
383 411
384 mkdir $cachedir, 0777; 412 mkdir $cachedir, 0777;
385 *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst"; 413 *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst";
386} 414}
387 415
388$VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/crossfire" : File::Spec->tmpdir; 416$VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire";
389 417
390init $VARDIR; 418init $VARDIR;
391 419
392=head1 AUTHOR 420=head1 AUTHOR
393 421

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines