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.21 by root, Thu Feb 23 01:55:29 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
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, "<: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}}
114 my ($path, $cache) = @_; 142 my ($path, $cache) = @_;
115 143
116 eval { 144 eval {
117 defined $cache 145 defined $cache
118 && -M $cache < -M $path 146 && -M $cache < -M $path
119 && Storable::retrieve $cache 147 && load_ref $cache
120 } or do { 148 } or do {
121 my %pak; 149 my %pak;
122 150
123 open my $fh, "<:raw", $path 151 open my $fh, "<:raw", $path
124 or Carp::croak "$_[0]: $!"; 152 or Carp::croak "$_[0]: $!";
126 my ($type, $id, $len, $path) = split; 154 my ($type, $id, $len, $path) = split;
127 $path =~ s/.*\///; 155 $path =~ s/.*\///;
128 read $fh, $pak{$path}, $len; 156 read $fh, $pak{$path}, $len;
129 } 157 }
130 158
131 Storable::nstore \%pak, $cache 159 save_ref \%pak, $cache
132 if defined $cache; 160 if defined $cache;
133 161
134 \%pak 162 \%pak
135 } 163 }
136} 164}
139 my ($path, $cache) = @_; 167 my ($path, $cache) = @_;
140 168
141 eval { 169 eval {
142 defined $cache 170 defined $cache
143 && -M $cache < -M $path 171 && -M $cache < -M $path
144 && Storable::retrieve $cache 172 && load_ref $cache
145 } or do { 173 } or do {
146 my %arc; 174 my %arc;
147 my ($more, $prev); 175 my ($more, $prev);
148 176
149 open my $fh, "<:raw", $path 177 open my $fh, "<:raw", $path
204 } 232 }
205 } 233 }
206 234
207 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.
208 236
209 Storable::nstore \%arc, $cache 237 save_ref \%arc, $cache
210 if defined $cache; 238 if defined $cache;
211 239
212 \%arc 240 \%arc
213 } 241 }
214} 242}
263 291
264 } else { 292 } else {
265 # single face 293 # single face
266 return (0, 0, 0, 0); 294 return (0, 0, 0, 0);
267 } 295 }
268}
269
270sub init($) {
271 my ($cachedir) = @_;
272
273 return if %ARCH;
274
275 *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst";
276} 296}
277 297
278=item $type = arch_attr $arch 298=item $type = arch_attr $arch
279 299
280Returns a hashref describing the object and its attributes. It can contain 300Returns a hashref describing the object and its attributes. It can contain
378# return(edit_type); 398# return(edit_type);
379# 399#
380# 400#
381} 401}
382 402
403sub init($) {
404 my ($cachedir) = @_;
405
406 return if %ARCH;
407
408 mkdir $cachedir, 0777;
409 *ARCH = read_arch "$LIB/archetypes", "$cachedir/archetypes.pst";
410}
411
383$CACHEDIR ||= $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir; 412$VARDIR ||= $ENV{HOME} ? "$ENV{HOME}/.crossfire" : File::Spec->tmpdir . "/crossfire";
384 413
385init $CACHEDIR; 414init $VARDIR;
386 415
387=head1 AUTHOR 416=head1 AUTHOR
388 417
389 Marc Lehmann <schmorp@schmorp.de> 418 Marc Lehmann <schmorp@schmorp.de>
390 http://home.schmorp.de/ 419 http://home.schmorp.de/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines