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.23 by root, Thu Feb 23 02:52:51 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
25our $LIB = $ENV{CROSSFIRE_LIBDIR} 24our $LIB = $ENV{CROSSFIRE_LIBDIR}
26 or Carp::croak "\$CROSSFIRE_LIBDIR must be set\n"; 25 or Carp::croak "\$CROSSFIRE_LIBDIR must be set\n";
27 26
28sub TILESIZE (){ 32 } 27sub TILESIZE (){ 32 }
29 28
30our $CACHEDIR; 29our $VARDIR;
31our %ARCH; 30our %ARCH;
32our %FACE; 31our %FACE;
33our $TILE; 32our $TILE;
34 33
35our %FIELD_MULTILINE = ( 34our %FIELD_MULTILINE = (
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}}
114 my ($path, $cache) = @_; 143 my ($path, $cache) = @_;
115 144
116 eval { 145 eval {
117 defined $cache 146 defined $cache
118 && -M $cache < -M $path 147 && -M $cache < -M $path
119 && Storable::retrieve ($cache) 148 && load_ref $cache
120 } or do { 149 } or do {
121 my %pak; 150 my %pak;
122 151
123 open my $fh, "<:raw", $path 152 open my $fh, "<", $path
124 or Carp::croak "$_[0]: $!"; 153 or Carp::croak "$_[0]: $!";
154 binmode $fh;
125 while (<$fh>) { 155 while (<$fh>) {
126 my ($type, $id, $len, $path) = split; 156 my ($type, $id, $len, $path) = split;
127 $path =~ s/.*\///; 157 $path =~ s/.*\///;
128 read $fh, $pak{$path}, $len; 158 read $fh, $pak{$path}, $len;
129 } 159 }
130 160
131 Storable::nstore (\%pak, $cache) 161 save_ref \%pak, $cache
132 if defined $cache; 162 if defined $cache;
133 163
134 \%pak 164 \%pak
135 } 165 }
136} 166}
139 my ($path, $cache) = @_; 169 my ($path, $cache) = @_;
140 170
141 eval { 171 eval {
142 defined $cache 172 defined $cache
143 && -M $cache < -M $path 173 && -M $cache < -M $path
144 && Storable::retrieve ($cache) 174 && load_ref $cache
145 } or do { 175 } or do {
146 my %arc; 176 my %arc;
147 my ($more, $prev); 177 my ($more, $prev);
148 178
149 open my $fh, "<:raw", $path 179 open my $fh, "<", $path
150 or Carp::croak "$path: $!"; 180 or Carp::croak "$path: $!";
181
182 binmode $fh;
151 183
152 my $parse_block; $parse_block = sub { 184 my $parse_block; $parse_block = sub {
153 my %arc = @_; 185 my %arc = @_;
154 186
155 while (<$fh>) { 187 while (<$fh>) {
204 } 236 }
205 } 237 }
206 238
207 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.
208 240
209 Storable::nstore (\%arc, $cache) 241 save_ref \%arc, $cache
210 if defined $cache; 242 if defined $cache;
211 243
212 \%arc 244 \%arc
213 } 245 }
214} 246}
263 295
264 } else { 296 } else {
265 # single face 297 # single face
266 return (0, 0, 0, 0); 298 return (0, 0, 0, 0);
267 } 299 }
268}
269
270sub init($) {
271 my ($cachedir) = @_;
272
273 return if %ARCH;
274
275 mkdir $cachedir, 0777;
276 *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst";
277} 300}
278 301
279=item $type = arch_attr $arch 302=item $type = arch_attr $arch
280 303
281Returns a hashref describing the object and its attributes. It can contain 304Returns a hashref describing the object and its attributes. It can contain
379# return(edit_type); 402# return(edit_type);
380# 403#
381# 404#
382} 405}
383 406
407sub init($) {
408 my ($cachedir) = @_;
409
410 return if %ARCH;
411
412 mkdir $cachedir, 0777;
413 *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst";
414}
415
384$CACHEDIR ||= $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir; 416$VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire";
385 417
386init $CACHEDIR; 418init $VARDIR;
387 419
388=head1 AUTHOR 420=head1 AUTHOR
389 421
390 Marc Lehmann <schmorp@schmorp.de> 422 Marc Lehmann <schmorp@schmorp.de>
391 http://home.schmorp.de/ 423 http://home.schmorp.de/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines