blib/lib/Games/EternalLands/Loader.pm

filter: Statementstmt 21.6% 60 / 277 Branchbran 0.0% 0 / 76 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 41.0% 16 / 39 Podpod 100% 4 / 4 totaltotal 19.9% 81 / 406 CC 52 SCAR 32.1
CC
52
Cov
17%
_make_vertex_type
110.0
_locate
30.0
_check_header
30.0
CRAP 24.7
↑ Summary
1 package Games::EternalLands::Loader;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 $VERSION = '0.03';
9  
10 =head1 NAME
11  
12 Games::EternalLands::Loader - Access Eternal Lands content files
13  
14 =head1 SYNOPSIS
15  
16 use Games::EternalLands::Loader;
17  
18 my $loader = Games::EternalLands::Loader->new;
19 $loader->content_path('/usr/share/games/eternal-lands');
20
21 use Data::Dumper;
22  
23 my $map = $loader->load('maps/startmap.elm');
24 print Dumper($map);
25  
26 my $entity = $loader->load('3dobjects/bag1.e3d');
27 print Dumper($entity);
28  
29 ...
30  
31 =head1 ABSTRACT
32  
33 This module reads binary content files for the online game Eternal Lands
34 and unpacks them into perl data structures.
35  
36 =cut
37  
38 2 ▶use Carp qw(croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
39 2 ▶use Convert::Binary::C;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 2 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
41 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 2 ▶use IO::Uncompress::AnyUncompress;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 2 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 2 ▶use Games::EternalLands::Binary::Float16;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 2 ▶use Games::EternalLands::Binary::Unitvec16;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
49 2 _COMP_EXTS => [qw(gz zip bz2 Z xz)],
50 _MAP_SIG => 'elmf',
51 _ENT_SIG => 'e3dx',
52 2 };
53  
54 0 ▶sub _fail {
Subroutine _fail: not called CC=1 SCAR=6.9
55 0 my ($s, $m) = @_;
56 0 croak "_FAILED_" . $m;
57 }
58  
59 1 ▶sub _setup_parser {
Subroutine _setup_parser: called CC=2 SCAR=7.5
60 1 my ($s) = @_;
61 1 my $c = new Convert::Binary::C;
62 1 $c->configure(
63 ByteOrder => 'LittleEndian',
64 Alignment => 1,
65 CharSize => 1,
66 ShortSize => 2,
67 IntSize => 4,
68 FloatSize => 4,
69 );
70 1 $c->parse(<<'EOS');
71 typedef unsigned char byte;
72 typedef unsigned short ushort;
73 typedef unsigned int uint;
74 typedef ushort float16;
75 typedef ushort unitvec16;
76  
77 typedef struct {
78 char signature[4];
79 byte version[4];
80 byte md5_digest[16];
81 uint entity_data_offset;
82 } entity_header;
83  
84 typedef struct {
85 uint vertex_element_count;
86 uint vertex_element_size;
87 uint vertex_data_offset;
88 uint index_element_count;
89 uint index_element_size;
90 uint index_data_offset;
91 uint submesh_element_count;
92 uint submesh_element_size;
93 uint submesh_data_offset;
94 struct {
95 byte normals:1;
96 byte tangents:1;
97 byte twotextures:1;
98 byte colors:1;
99 byte :4;
100 } vertex_field_flags;
101 struct {
102 byte float16_positions:1;
103 byte float16_uv1s:1;
104 byte float16_uv2s:1;
105 byte quantized_unit_vectors:1;
106 byte :4;
107 } vertex_type_flags;
108 byte __unused[2];
109 } mesh_header;
110  
111 typedef struct {
112 char entity_name[80];
113 float position[3];
114 float rotation[3];
115 byte lighting_disabled;
116 byte blending_level;
117 byte __unused[2];
118 float color[3];
119 float scale;
120 byte __unused[20];
121 } mesh_object;
122  
123 typedef struct {
124 char entity_name[80];
125 float position[3];
126 float rotation[3];
127 byte __unused[24];
128 } quad_object;
129  
130 typedef struct {
131 float position[3];
132 float color[3];
133 byte __unused[16];
134 } light_object;
135  
136 typedef struct {
137 char entity_name[80];
138 float position[3];
139 byte __unused[12];
140 } fuzz_object;
141  
142 typedef struct {
143 char signature[4];
144 uint terrain_map_length;
145 uint terrain_map_breadth;
146 uint terrain_map_offset;
147 uint tile_map_offset;
148 uint mesh_object_size;
149 uint mesh_object_count;
150 uint mesh_data_offset;
151 uint quad_object_size;
152 uint quad_object_count;
153 uint quad_data_offset;
154 uint light_object_size;
155 uint light_object_count;
156 uint light_data_offset;
157 byte flag_indoors;
158 byte __unused[3];
159 float ambient_light[3];
160 uint fuzz_object_size;
161 uint fuzz_object_count;
162 uint fuzz_data_offset;
163 uint segment_data_offset;
164 byte __unused[36];
165 } map_header;
166 EOS
167 1 $c->tag($_, Format => 'String') for qw(
168 entity_header.signature
169 mesh_object.entity_name
170 quad_object.entity_name
171 fuzz_object.entity_name
172 map_header.signature
173 );
174 0 ▶ $c->tag('float16', Hooks => { unpack => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
175 0 Games::EternalLands::Binary::Float16::unpack_float16($_[0])
176 1 }});
177 0 ▶ $c->tag('unitvec16', Hooks => { unpack => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
178 0 Games::EternalLands::Binary::Unitvec16::unpack_unitvec16($_[0])
179 1 }});
180 0 ▶ $c->tag('entity_header.version', Hooks => { unpack => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
181 0 my $v = $_[0];
182 0 return $v->[0] * 1000 + $v->[1] * 100 + $v->[2] * 10 + $v->[3];
183 1 }});
184 1 $s->{parser} = $c;
185 }
186  
187 0 ▶sub _locate {
Subroutine _locate: not called CC=5 SCAR=34.0
188 0 my ($s, $n) = @_;
189 0 my $a = $s->{contpath};
190 0 for my $p (@$a) {
191 0 my $b = catfile $p, $n;
192 0 ▶ return $b if -e $b;
Branch: 1 branch
BranchTrueFalseTotal
if -e $b 0 0 0
193 0 for my $e (_COMP_EXTS) {
194 0 my $t = "$b.$e";
195 0 ▶ return $t if -e $t;
Branch: 1 branch
BranchTrueFalseTotal
if -e $t 0 0 0
196 }
197 }
198 0 $s->_fail("Failed to find '$n' in search path: @$a");
199 }
200  
201 0 ▶sub _open {
Subroutine _open: not called CC=3 SCAR=24.8
202 0 my ($s, $n) = @_;
203 0 my $p = $s->_locate($n);
204 0 my $f = new IO::Uncompress::AnyUncompress $p;
205 0 ▶ $f = new IO::File $p if not $f;
Branch: 1 branch
BranchTrueFalseTotal
unless $f 0 0 0
206 0 ▶ $s->_fail("Failed to open '$p': $!") if not $f;
Branch: 1 branch
BranchTrueFalseTotal
unless $f 0 0 0
207 0 return $f;
208 }
209  
210 0 ▶sub _parse_c {
Subroutine _parse_c: not called CC=2 SCAR=17.9
211 0 my ($s, $x) = @_;
212 0 eval {
213 0 $s->{parser}->parse($x);
214 };
215 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
216 0 confess "Failed to parse c code: $@\nCode:\n$x\n";
217 }
218 }
219  
220 0 ▶sub _unp {
Subroutine _unp: not called CC=4 SCAR=30.0
221 0 my ($s, $t, $d, $o, $x) = @_;
222 0 my $c = $s->{parser};
223 0 ▶ $o = 0 if not defined $o;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $o 0 0 0
224 0 ▶ $s->_parse_c($x) if defined $x;
Branch: 1 branch
BranchTrueFalseTotal
if defined $x 0 0 0
225 0 my $l = $c->sizeof($t);
226 0 my $r = length $d;
227 0 ▶ $s->_fail("Need to unpack $l bytes at offset $o,"
Branch: 1 branch
BranchTrueFalseTotal
if $o + $l > $r 0 0 0
228 . " but data has length only $r")
229 if $o + $l > $r;
230 0 my $p = substr $d, $o, $l;
231 0 return $c->unpack($t, $p);
232 }
233  
234 0 ▶sub _unpa {
Subroutine _unpa: not called CC=2 SCAR=17.9
235 0 my ($s, $t, $n, $d, $o) = @_;
236 0 my $i = "_${t}_array_$n";
237 0 ▶ my $x = $s->{parser}->def($i) ? undef : "typedef $t $i [$n];";
Branch: 1 branch
BranchTrueFalseTotal
$s->{'parser'}->def($i) ? : 0 0 0
238 0 return $s->_unp($i, $d, $o, $x);
239 }
240  
241 0 ▶sub _unpack_terrain {
Subroutine _unpack_terrain: not called CC=1 SCAR=6.9
242 0 my ($s, $m, $h, $d) = @_;
243  
244 0 my $rl = $h->{terrain_map_length};
245 0 my $rb = $h->{terrain_map_breadth};
246 0 my $rc = $rl * $rb;
247 0 my $ro = $h->{terrain_map_offset};
248 0 my $rm = $s->_unpa(byte => $rc, $d, $ro);
249 0 $m->{terrain_length} = $rl;
250 0 $m->{terrain_breadth} = $rb;
251 0 $m->{terrain_count} = $rc;
252 0 $m->{terrain_map} = $rm;
253  
254 0 my $tl = $rl * 6;
255 0 my $tb = $rb * 6;
256 0 my $tc = $tl * $tb;
257 0 my $to = $h->{tile_map_offset};
258 0 my $tm = $s->_unpa(byte => $tc, $d, $to);
259 0 $m->{tile_length} = $tl;
260 0 $m->{tile_breadth} = $tb;
261 0 $m->{tile_count} = $tc;
262 0 $m->{tile_map} = $tm;
263 }
264  
265 0 ▶sub _unpack_objects {
Subroutine _unpack_objects: not called CC=2 SCAR=17.9
266 0 my ($s, $k, $m, $h, $d) = @_;
267 0 my $kt = $k.'_object';
268 0 my $ka = $k.'_objects';
269 0 my $hs = $h->{$kt.'_size'};
270 0 my $ts = $s->{parser}->sizeof($kt);
271 0 ▶ $s->_fail("Size mismatch for '$kt': "
Branch: 1 branch
BranchTrueFalseTotal
unless $hs == $ts 0 0 0
272 . "header has $hs, but type is $ts")
273 unless $hs == $ts;
274 0 my $kc = $h->{$kt.'_count'};
275 0 my $ko = $h->{$k.'_data_offset'};
276 0 $m->{$ka} = $s->_unpa($kt, $kc, $d, $ko);
277 }
278  
279 0 ▶sub _assign_ids {
Subroutine _assign_ids: not called CC=3 SCAR=24.8
280 0 my ($s, $m) = @_;
281 0 for my $k (qw(mesh quad)) {
282 0 my $i = 0;
283 0 for my $o (@{$m->{$k."_objects"}}) {
284 0 $o->{id} = $i;
285 0 $i++;
286 }
287 }
288 }
289  
290 0 ▶sub _load_map {
Subroutine _load_map: not called CC=2 SCAR=17.9
291 0 my ($s, $n) = @_;
292 0 my $f = $s->_open($n);
293 0 my $d = join '', <$f>;
294 0 my $h = $s->_unp(map_header => $d);
295 0 my $g = $h->{signature};
296 0 ▶ $s->_fail("Map '$n' has wrong file signature: \"$g\"")
Branch: 1 branch
BranchTrueFalseTotal
if $g ne "elmf" 0 0 0
297 if $g ne _MAP_SIG;
298 my $m = {
299 name => $n,
300 indoors => $h->{flag_indoors},
301 ambient_light => $h->{ambient_light},
302 0 };
303 0 $s->_unpack_terrain($m, $h, $d);
304 0 $s->_unpack_objects(mesh => $m, $h, $d);
305 0 $s->_unpack_objects(quad => $m, $h, $d);
306 0 $s->_assign_ids($m);
307 0 $s->_unpack_objects(light => $m, $h, $d);
308 0 $s->_unpack_objects(fuzz => $m, $h, $d);
309 0 return $m;
310 }
311  
312 0 ▶sub _load_quad {
Subroutine _load_quad: not called CC=3 SCAR=24.8
313 0 my ($s, $n) = @_;
314 0 my $f = $s->_open($n);
315 0 my %d = split(/\s*[:\n]\s*/, join('', <$f>));
316 0 my @v = qw(texture file_x_len file_y_len
317 x_size y_size u_start u_end v_start v_end);
318 0 for my $k (@v) {
319 $s->_fail("Missing expected field '$k' in '$n'")
320 0 ▶ unless exists $d{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $d{$k} 0 0 0
321 }
322 0 my $ix = $d{file_x_len};
323 0 my $iy = $d{file_y_len};
324 0 my $u0 = $d{u_start} / $ix;
325 0 my $v0 = $d{v_start} / $iy;
326 0 my $u1 = $d{u_end} / $ix;
327 0 my $v1 = $d{v_end} / $iy;
328 0 my $sx = $d{x_size};
329 0 my $sy = $d{y_size};
330 0 my $x0 = - $sx / 2.0;
331 0 my $y0 = - $sy / 2.0;
332 0 my $x1 = $x0 + $sx;
333 0 my $y1 = $y0 + $sy;
334 0 my $z = 0.001;
335 0 my $nr = [0.0, 0.0, 1.0];
336 my $e = {
337 texture_name => $d{texture},
338 0 vertices => [
339 { position => [$x0, $y0, $z], uv => [$u0, $v0], normal => $nr, },
340 { position => [$x0, $y1, $z], uv => [$u0, $v1], normal => $nr, },
341 { position => [$x1, $y1, $z], uv => [$u1, $v1], normal => $nr, },
342 { position => [$x1, $y0, $z], uv => [$u1, $v0], normal => $nr, },
343 ],
344 indices => [ 0, 1, 2, 0, 2, 3 ],
345 };
346 0 return $e;
347 }
348  
349 0 ▶sub _check_header {
Subroutine _check_header: not called CC=5 SCAR=34.0
350 0 my ($s, $n, $h, $d) = @_;
351 0 my $es = $h->{signature};
352 0 ▶ $s->_fail("Wrong signature in entity file header for '$n': $es")
Branch: 1 branch
BranchTrueFalseTotal
unless $es eq "e3dx" 0 0 0
353 unless $es eq _ENT_SIG;
354 0 my $hc = join '', map { sprintf "%02x", $_ } @{$h->{md5_digest}};
355 0 my $fc = md5_hex(substr $d, $h->{entity_data_offset});
356 0 ▶ $s->_fail("Checksum mismatch for '$n':\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $hc eq $fc 0 0 0
357 . " Expected from header: $hc\n"
358 . " Calculated from file: $fc\n")
359 unless $hc eq $fc;
360 0 my $v = $h->{version};
361 0 ▶ $s->_fail("Unrecognized entity file version for '$n': $v")
Branch: 1 branch
BranchTrueFalseTotal
unless $v == 1000 or $v == 1100 0 0 0
Condition0%0/3
$v == 1000 or $v == 1100
l!l&&r!l&&!r
000
MC/DC0%0/2
$v == 1000 or $v == 1100
$v == 1000 $v == 1100
Truth table0%0/3
A or B
A= $v == 1000B= $v == 1100
ABresult
1X1
011
000
362 unless $v == 1000 or $v == 1100;
363 }
364  
365 0 ▶sub _adjust_vertex_flags {
Subroutine _adjust_vertex_flags: not called CC=4 SCAR=30.0
366 0 my ($s, $v, $h) = @_;
367 0 my $f = $h->{vertex_field_flags};
368 0 my $t = $h->{vertex_type_flags};
369 0 ▶ if ($v == 1000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v == 1000) 0 0 0
370 0 $f->{$_} ^= 1 for keys %$f;
371 0 $f->{colors} = 0;
372 0 $t->{$_} = 0 for keys %$t;
373 }
374 }
375  
376 0 ▶sub _make_vertex_type {
Subroutine _make_vertex_type: not called CC=10 SCAR=47.0
377 0 my ($s, $h) = @_;
378 0 my $f = $h->{vertex_field_flags};
379 0 my $t = $h->{vertex_type_flags};
380 0 my $g = join '_', (values %$f, values %$t);
381 0 my $v = "_vertex_type_$g";
382 0 ▶ return $v if $s->{parser}->def($v);
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"parser"}->def($v) 0 0 0
383  
384 0 my $f_nor = $f->{normals};
385 0 my $f_tan = $f->{tangents};
386 0 my $f_uv2 = $f->{twotextures};
387 0 my $f_col = $f->{colors};
388 0 ▶ my $t_pos = $t->{float16_positions} ? 'float16' : 'float';
Branch: 1 branch
BranchTrueFalseTotal
$t->{'float16_positions'} ? : 0 0 0
389 0 ▶ my $t_uv1 = $t->{float16_uv1s} ? 'float16' : 'float';
Branch: 1 branch
BranchTrueFalseTotal
$t->{'float16_uv1s'} ? : 0 0 0
390 0 ▶ my $t_uv2 = $t->{float16_uv2s} ? 'float16' : 'float';
Branch: 1 branch
BranchTrueFalseTotal
$t->{'float16_uv2s'} ? : 0 0 0
391 0 ▶ my $t_vec = $t->{quantized_unit_vectors} ? 'unitvec16' : 'float';
Branch: 1 branch
BranchTrueFalseTotal
$t->{'quantized_unit_vectors'} ? : 0 0 0
392 0 ▶ my $a_vec = $t->{quantized_unit_vectors} ? 0 : 3;
Branch: 1 branch
BranchTrueFalseTotal
$t->{'quantized_unit_vectors'} ? : 0 0 0
393 0 my @r = (
394 [1, $t_uv1, 'uv', 2],
395 [$f_uv2, $t_uv2, 'uv2', 2],
396 [$f_nor, $t_vec, 'normal', $a_vec],
397 [$f_tan, $t_vec, 'tangent', $a_vec],
398 [1, $t_pos, 'position', 3],
399 [$f_col, 'byte', 'color', 4],
400 );
401 0 my $d = "typedef struct {\n";
402 0 for (@r) {
403 0 my ($e, $k, $n, $a) = @$_;
404 0 ▶ $d .= " $k $n ".($a?"[$a]":'').";\n" if $e;
Branch: 2 branches
BranchTrueFalseTotal
if $e 0 0 0
$a ? : 0 0 0
405 }
406 0 $d .= "} $v;\n";
407 0 $s->_parse_c($d);
408 0 return $v;
409 }
410  
411 0 ▶sub _make_submesh_type {
Subroutine _make_submesh_type: not called CC=5 SCAR=34.0
412 0 my ($s, $h) = @_;
413 0 my $c = $s->{parser};
414 0 my $f = $h->{vertex_field_flags};
415 0 my $x = $f->{twotextures};
416 0 my $t = "_submesh$x";
417 0 ▶ return $t if $c->def($t);
Branch: 1 branch
BranchTrueFalseTotal
if $c->def($t) 0 0 0
418 0 my $d = <<'EOS';
419 typedef struct {
420 uint texture_flags;
421 char texture_name[128];
422 float minimum_position[3];
423 float maximum_position[3];
424 uint minimum_vertex_index;
425 uint maximum_vertex_index;
426 uint index_element_offset;
427 uint index_element_count;
428 EOS
429 0 ▶ $d .= " char texture2_name[128];" if $x;
Branch: 1 branch
BranchTrueFalseTotal
if $x 0 0 0
430 0 $d .= "} $t;\n";
431 0 $s->_parse_c($d);
432 0 my @n = ("$t.texture_name");
433 0 ▶ push @n, "$t.texture2_name" if $x;
Branch: 1 branch
BranchTrueFalseTotal
if $x 0 0 0
434 0 $c->tag($_, Format => 'String') for @n;
435 0 return $t;
436 }
437  
438 0 ▶sub _load_mesh {
Subroutine _load_mesh: not called CC=4 SCAR=30.0
439 0 my ($s, $n) = @_;
440 0 my $f = $s->_open($n);
441 0 my $d = join '', <$f>;
442 0 my $c = $s->{parser};
443 0 my $e = { name => $n };
444 0 my $eh = $s->_unp(entity_header => $d);
445 0 $s->_check_header($n, $eh, $d);
446  
447 0 my $mh = $s->_unp(mesh_header => $d, $eh->{entity_data_offset});
448 0 $s->_adjust_vertex_flags($eh->{version}, $mh);
449  
450 0 my $vt = $s->_make_vertex_type($mh);
451 0 my $ve = $c->sizeof($vt);
452 0 my $vs = $mh->{vertex_element_size};
453 0 ▶ $s->_fail("Unexpected vertex element size"
Branch: 1 branch
BranchTrueFalseTotal
unless $ve == $vs 0 0 0
454 . " for mesh entity '$n': $vs (expected $ve)")
455 unless $ve == $vs;
456 0 my $vn = $mh->{vertex_element_count};
457 0 my $vo = $mh->{vertex_data_offset};
458 0 $e->{vertices} = $s->_unpa($vt, $vn, $d, $vo);
459  
460 0 ▶ my $it = $mh->{index_element_size} == 2 ? 'ushort' : 'uint';
Branch: 1 branch
BranchTrueFalseTotal
$mh->{'index_element_size'} == 2 ? : 0 0 0
461 0 my $in = $mh->{index_element_count};
462 0 my $io = $mh->{index_data_offset};
463 0 $e->{indices} = $s->_unpa($it, $in, $d, $io);
464  
465 0 my $st = $s->_make_submesh_type($mh);
466 0 my $se = $c->sizeof($st);
467 0 my $ss = $mh->{submesh_element_size};
468 0 ▶ $s->_fail("Unexpected submesh element size"
Branch: 1 branch
BranchTrueFalseTotal
unless $se == $ss 0 0 0
469 . " for mesh entity '$n': $ss (expected $se)")
470 unless $se == $ss;
471 0 my $sn = $mh->{submesh_element_count};
472 0 my $so = $mh->{submesh_data_offset};
473 0 $e->{submeshes} = $s->_unpa($st, $sn, $d, $so);
474  
475 0 return $e;
476 }
477  
478 =head1 METHODS
479  
480 =head2 new
481  
482 Creates a new Games::EternalLands::Loader object.
483  
484 =cut
485  
486 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
487 1 my $proto = shift;
488 1 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
489 1 my $s = bless {}, $class;
490 1 $s->{contpath} = [curdir];
491 $s->{loaders} = {
492 1 'elm' => \&_load_map,
493 'e3d' => \&_load_mesh,
494 '2d0' => \&_load_quad,
495 };
496 1 $s->{errstr} = '';
497 1 $s->_setup_parser;
498 1 return $s;
499 }
500  
501 =head2 content_path
502  
503 Sets the directory where the content files are located. The
504 argument may be a string to set a single path, or an array
505 reference to set multiple paths to be used in turn. If no
506 argument is given the current value (an array reference)
507 is returned.
508  
509 =cut
510  
511 0 ▶sub content_path {
Subroutine content_path: not called CC=4 SCAR=30.0
Pod documented
512 0 my ($s, $p) = @_;
513 0 ▶ return $s->{contpath} if not defined $p;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p 0 0 0
514 0 ▶ if (ref $p eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $p eq 'ARRAY') { } 0 0 0
elsif (ref $p eq '') { } 0 0 0
515 0 $s->{contpath} = [@$p];
516 } elsif (ref $p eq '') {
517 0 $s->{contpath} = [$p];
518 } else {
519 0 croak "Expecting a string or array reference.";
520 }
521 }
522  
523 0 ▶sub _load {
Subroutine _load: not called CC=4 SCAR=30.0
524 0 my ($s, $n) = @_;
525 0 my $l;
526 0 for my $e (reverse split /\./, $n) {
527 0 ▶ last if $l = $s->{loaders}{$e};
Branch: 1 branch
BranchTrueFalseTotal
if $l = $s->{"loaders"}{$e} 0 0 0
528 }
529 0 ▶ $s->_fail("No loader found for '$n'") unless $l;
Branch: 1 branch
BranchTrueFalseTotal
unless $l 0 0 0
530 0 return $s->$l($n);
531 }
532  
533 =head2 load
534  
535 =over
536  
537 =item load NAME
538  
539 Finds, opens, reads and constructs the game asset identified by the
540 given name.
541  
542 In case there is some error opening the file or parsing its contents,
543 undef is returned. A description of the error can be retrieved using
544 the L</errstr> method.
545  
546 See L</"DATA STRUCTURES"> for what exactly is returned in each case.
547  
548 =back
549  
550 =cut
551  
552 0 ▶sub load {
Subroutine load: not called CC=4 SCAR=30.0
Pod documented
553 0 my ($s, $n) = @_;
554 0 ▶ croak "Content name argument expected" if not defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 0 0
555 0 my $e = eval { $s->_load($n); };
556 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
557 0 my $x = $@;
558 0 ▶ if ($x =~ s/^_FAILED_//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x =~ s/^_FAILED_//) 0 0 0
559 0 chomp $x;
560 0 $s->{errstr} = $x;
561 0 return;
562 }
563 0 confess $x;
564 }
565 0 return $e;
566 }
567  
568 =head2 errstr
569  
570 Returns a string describing the last error that occurred.
571  
572 =cut
573  
574 0 ▶sub errstr {
Subroutine errstr: not called CC=1 SCAR=6.9
Pod documented
575 0 return $_[0]->{errstr};
576 }
577  
578 2 1;
579 __END__
↑ Summary