Asset.pm

filter: Statementstmt 92.5% 87 / 94 Branchbran 61.1% 11 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 85.7% 18 / 21 Podpod 100% 2 / 2 totaltotal 87.4% 118 / 135 SCAR 24.2
CC
11
Cov
88%
_read_zip
7.6
_build_entries
5.0
_read_index
2.0
CRAP 11.2
↑ Summary Asset/MultiExample.pm »
1 # Copyright (c) 2016 Timm Murray
2 # All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are met:
6 #
7 # * Redistributions of source code must retain the above copyright notice,
8 # this list of conditions and the following disclaimer.
9 # * Redistributions in binary form must reproduce the above copyright
10 # notice, this list of conditions and the following disclaimer in the
11 # documentation and/or other materials provided with the distribution.
12 #
13 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23 # POSSIBILITY OF SUCH DAMAGE.
24 package Game::Asset;
25 7 $Game::Asset::VERSION = '0.4';
26 # ABSTRACT: Load assets (images, music, etc.) for games
27 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 7 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 7 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 7 ▶use Game::Asset::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 7 ▶use Game::Asset::Null;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 7 ▶use Game::Asset::PerlModule;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 7 ▶use Game::Asset::PlainText;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 7 ▶use Game::Asset::YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 7 ▶use Game::Asset::MultiExample;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 7 ▶use Archive::Zip qw( :ERROR_CODES );
Subroutine BEGIN: called CC=1 SCAR=0.0
40 7 ▶use YAML ();
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42  
43 7 has 'file' => (
44 is => 'ro',
45 isa => 'Str',
46 required => 1,
47 );
48 has 'mappings' => (
49 is => 'ro',
50 isa => 'HashRef[Game::Asset::Type]',
51 0 ▶ default => sub {{}},
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 7 auto_deref => 1,
53 );
54 has 'entries' => (
55 is => 'ro',
56 isa => 'ArrayRef[Game::Asset::Type]',
57 0 ▶ default => sub {[]},
Subroutine __ANON__: not called CC=1 SCAR=6.9
58 7 auto_deref => 1,
59 );
60 has '_entries_by_shortname' => (
61 traits => ['Hash'],
62 is => 'ro',
63 isa => 'HashRef[Game::Asset::Type]',
64 0 ▶ default => sub {{}},
Subroutine __ANON__: not called CC=1 SCAR=6.9
65 7 handles => {
66 _get_by_name => 'get',
67 },
68 );
69 7 has '_zip' => (
70 is => 'ro',
71 isa => 'Archive::Zip',
72 );
73  
74  
75 sub BUILDARGS
76 {
77 6 ▶ my ($class, $args) = @_;
Subroutine BUILDARGS: called CC=1 SCAR=0.0
Pod documented
78 6 my $file = $args->{file};
79  
80 6 my $zip = $class->_read_zip( $file );
81 6 $args->{'_zip'} = $zip;
82  
83 6 my $index = $class->_read_index( $zip, $file );
84 $args->{mappings} = {
85 6 yml => 'Game::Asset::YAML',
86 txt => 'Game::Asset::PlainText',
87 pm => 'Game::Asset::PerlModule',
88 %$index,
89 };
90  
91 my ($entries, $entries_by_shortname) = $class->_build_entries( $zip,
92 6 $args->{mappings} );
93 6 $args->{entries} = $entries;
94 6 $args->{'_entries_by_shortname'} = $entries_by_shortname;
95  
96 6 return $args;
97 }
98  
99  
100 sub get_by_name
101 {
102 6 ▶ my ($self, $name, @args) = @_;
Subroutine get_by_name: called CC=2 SCAR=7.0
Pod documented
103 6 my $entry = $self->_get_by_name( $name );
104  
105 6 ▶ if( $entry ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry) 6 0 6
106 6 my $full_name = $entry->full_name;
107 6 my $contents = $self->_zip->contents( $full_name );
108 6 $entry->process_content( $contents, @args );
109 }
110  
111 6 return $entry;
112 }
113  
114  
115 sub _read_zip
116 {
117 6 ▶ my ($class, $file) = @_;
Subroutine _read_zip: called CC=5 SCAR=20.3
118  
119 6 my $zip = Archive::Zip->new;
120 6 my $read_result = $zip->read( $file );
121 6 ▶ if( $read_result == AZ_STREAM_END ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($read_result == 1) { } 0 6 6
elsif ($read_result == 2) { } 0 6 6
elsif ($read_result == 3) { } 0 6 6
elsif ($read_result == 4) { } 0 6 6
122 0 die "Hit end of stream unexpectedly in '$file'\n";
123 }
124 elsif( $read_result == AZ_ERROR ) {
125 0 die "Generic error while reading '$file'\n";
126 }
127 elsif( $read_result == AZ_FORMAT_ERROR ) {
128 0 die "Formatting error while reading '$file'\n";
129 }
130 elsif( $read_result == AZ_IO_ERROR ) {
131 0 die "IO error while reading '$file'\n";
132 }
133  
134 6 return $zip;
135 }
136  
137 sub _read_index
138 {
139 6 ▶ my ($class, $zip, $file) = @_;
Subroutine _read_index: called CC=2 SCAR=7.0
140 6 my $index_contents = $zip->contents( 'index.yml' );
141 6 ▶ die "Could not find index.yml in '$file'\n" unless $index_contents;
Branch: 1 branch
BranchTrueFalseTotal
unless $index_contents 0 6 6
142  
143 6 my $index = YAML::Load( $index_contents );
144 6 return $index;
145 }
146  
147 sub _build_entries
148 {
149 6 ▶ my ($class, $zip, $mappings) = @_;
Subroutine _build_entries: called CC=5 SCAR=16.1
150 6 my %mappings = %$mappings;
151  
152 6 my (@entries, %entries_by_shortname);
153 6 foreach my $member ($zip->memberNames) {
154 42 ▶ next if $member eq 'index.yml'; # Ignore index
Branch: 1 branch
BranchTrueFalseTotal
if $member eq "index.yml" 6 36 42
155 36 ▶ next if $member =~ m!\/ \z!x;
Branch: 1 branch
BranchTrueFalseTotal
if $member =~ m[\/ \z]x 6 30 36
156 30 my ($short_name, $ext) = $member =~ /\A (.*) \. (.*?) \z/x;
157 die "Could not find mapping for '$ext' (full name: $member)\n"
158 30 ▶ if ! exists $mappings{$ext};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $mappings{$ext} 0 30 30
159  
160 30 my $entry_class = $mappings{$ext};
161 30 my $entry = $entry_class->new({
162 name => $short_name,
163 full_name => $member,
164 });
165 30 push @entries, $entry;
166 30 $entries_by_shortname{$short_name} = $entry;
167 }
168  
169 6 return (\@entries, \%entries_by_shortname);
170 }
171  
172  
173  
174 7 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
175 7 __PACKAGE__->meta->make_immutable;
176 7 1;
177 __END__
178  
179  
180 =head1 NAME
181  
182 Game::Asset - Load assets (images, music, etc.) for games
183  
184 =head1 SYNOPSIS
185  
186 my $asset = Game::Asset->new({
187 file => 't_data/test1.zip',
188 });
189 my $foo = $asset->get_by_name( 'foo' );
190 my $name = $foo->name;
191 my $type = $foo->type;
192  
193 =head1 DESCRIPTION
194  
195 A common way to handle game assets is to load them in one big zip file. It
196 might end up named with extensions like ".wad" or ".pk3" or even ".jar", but
197 it's a zip file.
198  
199 This module allows you to load up these files and fetch their contents into
200 Perl objects. Each type of file is represented by a class that does the
201 L<Game::Asset::Type> Moose role. Which type class, exactly, is determined with
202 mappings defined in the C<index.yml> file. There are also a few built-in
203 mappings.
204  
205 =head1 THE INDEX FILE
206  
207 A file named C<index.yml> (a L<YAML> file) is required inside the zip file,
208 and resolves to a hash. Keys are the file extensions (without the dot), and
209 values are the Perl class that will handle that type. That class must do
210 the L<Game::Asset::Type> Moose role.
211  
212 The file must exist. If you just want to use the built-in mappings, it can
213 resolve to an empty hash.
214  
215 =head2 Built-in Mappings
216  
217 The following mappings are always available without being in the index file:
218  
219 =over 4
220  
221 =item * txt -- L<Game::Asset::PlainText>
222  
223 =item * yml -- L<Game::Asset::YAML>
224  
225 =item * pm -- L<Game::Asset::PerlModule>
226  
227 =back
228  
229 =head2 Multi-mappings
230  
231 There are times when the given content should be processed by more than one
232 mapping. For instance, a game may want to process a L<Graphics::GVG> vector
233 in both OpenGL and Chipmunk (physics library).
234  
235 This is what multi-mappings are for. See L<Game::Asset::Multi> for details.
236  
237 =head1 ATTRIBUTES
238  
239 =head2 file
240  
241 The path to the zip file.
242  
243 =head2 mappings
244  
245 A hashref (with autoderef). The keys are the file extensions, and the values
246 are the L<Game::Asset::Type> classes that will handle that type.
247  
248 =head2 entries
249  
250 A list of all the assets with their file extensions removed. Note that the
251 C<index.yml> file is filtered out.
252  
253 =head1 METHODS
254  
255 =head2 get_by_name
256  
257 $asset->get_by_name( 'foo' );
258  
259 Pass in a name of an asset (without the extension). Returns an object
260 representing the data in the zip file.
261  
262 Any other arguments passed will be passed to the handling class.
263  
264 =head1 LICENSE
265  
266 Copyright (c) 2016 Timm Murray
267 All rights reserved.
268  
269 Redistribution and use in source and binary forms, with or without
270 modification, are permitted provided that the following conditions are met:
271  
272 * Redistributions of source code must retain the above copyright notice,
273 this list of conditions and the following disclaimer.
274 * Redistributions in binary form must reproduce the above copyright
275 notice, this list of conditions and the following disclaimer in the
276 documentation and/or other materials provided with the distribution.
277  
278 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
279 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
280 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
281 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
282 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
283 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
284 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
285 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
286 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
287 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
288 POSSIBILITY OF SUCH DAMAGE.
289  
290 =cut
↑ Summary Asset/MultiExample.pm »