Pakset.pm

filter: Statementstmt 82.4% 155 / 188 Branchbran 56.5% 43 / 76 Conditioncond 31.5% 6 / 19 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 89.4% 34 / 38 Podpod 92.8% 13 / 14 totaltotal 72.9% 251 / 344 CC 66 SCAR 21.6
CC
66
Cov
72%
timeline
42.0
scan_liveries
17.0
object
12.5
CRAP 8.7
« Livery.pm ↑ Summary
1 package Games::Simutrans::Pakset;
2  
3 1 ▶use v5.32;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '0.02';
6  
7 1 ▶use Mojo::Base -base, -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Mojo::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Mojo::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use List::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Path::ExpandTilde;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 has 'name'; # An identifying name for the pak
14  
15 1 has 'path'; # This must be a path to a pakset's root.
16  
17 1 ▶sub valid ($self) {
Subroutine valid: called CC=3 SCAR=11.7
Pod documented
18 # Basic check as to whether a valid Pakset exists at the given path
19 1 ▶ return undef unless defined $self->path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->path 0 1 1
20 1 ▶ return 0 unless -e $self->path;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $self->path 0 1 1
21
22 1 return 1;
23 }
24  
25 ################
26 # LANGUAGE SUPPORT
27 ################
28  
29 1 has '_xlat_root';
30  
31 2 ▶sub xlat_root ($self, $new_root = undef) {
Subroutine xlat_root: called CC=6 SCAR=19.1
Pod documented
32 # Location of the translation text files. Lazy assignment in case
33 # we access before the path has been set.
34 2 my $xlat;
35 2 ▶ if (defined $new_root) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_root) { } 0 2 2
36 0 ▶ $xlat = ref $new_root ? $new_root : Mojo::Path->new($new_root);
Branch: 1 branch
BranchTrueFalseTotal
ref $new_root ? : 0 0 0
37 } else {
38 2 ▶ if (!defined $self->_xlat_root) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->_xlat_root) 1 1 2
39 1 ▶ return undef unless defined $self->path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->path 0 1 1
40 1 $xlat = Mojo::Path->new($self->path->to_string);
41 1 push @$xlat, 'text';
42 1 $self->_xlat_root($xlat);
43 }
44 }
45 2 return $self->_xlat_root->to_string;
46 };
47  
48 # Find a list of all the language (translation) files for the pak
49  
50 1 ▶has 'languages' => sub ($self) {
Subroutine __ANON__: called CC=1 SCAR=0.0
51 # Return a list of available languages
52  
53 1 ▶ my $files_collection = Mojo::File->new($self->xlat_root)->list_tree->grep(sub{$_ =~ /\.tab\z/});
Subroutine __ANON__: called CC=1 SCAR=0.0
54 1 ▶ return $files_collection->map(sub { $_->basename('.tab') } );
Subroutine __ANON__: called CC=1 SCAR=0.0
55 1 };
56  
57 1 ▶has 'language' => sub ($self, $lang = undef) {
Subroutine __ANON__: called CC=5 SCAR=19.2
58 # the default language
59
60 1 ▶ my $l = $lang // $ENV{LANGUAGE} // $ENV{LANG} // 'en'; $l =~ m/^(..)/;
Condition37%3/8
(($lang // $ENV{"LANGUAGE"}) // $ENV{"LANG"}) // "en"
l!l
01
($lang // $ENV{"LANGUAGE"}) // $ENV{"LANG"}
l!l&&r!l&&!r
001
$lang // $ENV{"LANGUAGE"}
l!l&&r!l&&!r
001
MC/DC0%0/3
(($lang // $ENV{"LANGUAGE"}) // $ENV{"LANG"}) // "en"
$lang $ENV{"LANGUAGE"} $ENV{"LANG"}
Truth table25%1/4
A // B // C
A= $langB= $ENV{"LANGUAGE"}C= $ENV{"LANG"}
ABCresult
1XX1
01X1
0011
0000
61 1 return $1;
62 1 };
63  
64 1 ▶has 'language_tables' => sub { {}; };
Subroutine __ANON__: called CC=1 SCAR=0.0
65  
66 1 ▶sub load_language($self, $language = $self->language) {
Subroutine load_language: called CC=8 SCAR=20.8
Pod undocumented
67 # Load a language file
68 1 my $lang_file = Mojo::Path->new($self->xlat_root);
69 1 push @$lang_file, "${language}.tab";
70 1 my $filename = $lang_file->to_string;
71  
72 1 my $translate_from;
73 1 ▶ open (my $xlat, '<', $filename) or die "Can't open translation file $filename\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $xlat, "<", $filename or die "Can't open translation file $filename\n" 1 0 1
74 1 ▶ while (<$xlat>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $xlat)) 8 1 9
75 8 chomp;
76 8 ▶ if (/^\s*#(.*)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^\s*#(.*)$/u) { } 3 5 8
elsif (/\S{1,}/u) { } 4 1 5
77 3 my $comment_text = $1;
78 3 ▶ if ($comment_text =~ /\blanguage\s*:\s*(\w+)\s(\w+)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comment_text =~ /\blanguage\s*:\s*(\w+)\s(\w+)/iu) 1 2 3
79 1 my ($lang_code, $lang_name) = ($1, $2);
80 1 $self->language_tables->{$lang_code}{name} = $lang_name;
81 }
82 } elsif (/\S{1,}/) { # if anything non-blank
83 4 ▶ if (defined $translate_from) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $translate_from) { } 2 2 4
84 2 $self->language_tables->{$language}{$translate_from} = $_;
85 2 undef $translate_from;
86 } else {
87 2 $translate_from = $_;
88 }
89 }
90 }
91 1 close $xlat;
92 }
93  
94 2 ▶sub translate($self, $string, $language = $self->language) {
Subroutine translate: called CC=6 SCAR=22.7
Pod documented
95 # Translate a string, in the given language or the default if none given
96 2 ▶ if (!defined $self->language_tables->{$language}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->language_tables->{$language}) 0 2 2
97 0 eval { $self->load_language($language); }
98 }
99 2 ▶ return '??' unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 2 2
100 2 ▶ return $self->language_tables->{$language}{$string} // $string // '??';
Condition40%2/5
($self->language_tables->{$language}{$string} // $string) // "??"
l!l
20
$self->language_tables->{$language}{$string} // $string
l!l&&r!l&&!r
200
MC/DC0%0/2
($self->language_tables->{$language}{$string} // $string) // "??"
$self->language_tables->{$language}{$string} $string
Truth table33%1/3
A // B
A= $self->language_tables->{$language}{$string}B= $string
ABresult
1X1
011
000
101 }
102  
103 ################
104 # OBJECT SUPPORT
105 ################
106  
107 # NOTE: "Object" here refers to Simutrans's idea of an object (vehicle, waytype, etc.)
108 # as defined in the pakset source.
109  
110 # objects is a simple hash. Thus, $pak->objects() returns the entire
111 # pak object-hash
112  
113 1 ▶has objects => sub { {}; };
Subroutine __ANON__: called CC=1 SCAR=0.0
114  
115 # $pak->object('objname') # returns entire parameter-hash for given object
116 # $pak->object('objname',\{...}) # sets an object's parameter-hash
117 # $pak->object('objname','objkey') # returns the value of a parameter of an object (objkey must be string)
118 # $pak->object('objname','objkey','value') # sets parameter value. value could be a reference.
119  
120 7 ▶sub object ($self, $objname = undef, $attr = undef, $value = undef) {
Subroutine object: called CC=8 SCAR=25.2
Pod documented
121
122 0 ▶ return %{$self->objects} unless defined $objname;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $objname 0 7 7
123 7 ▶ return $self->objects->{$objname} unless defined $attr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attr 5 2 7
124 2 ▶ return ($self->objects->{$objname} = $attr) if ref($attr);
Branch: 1 branch
BranchTrueFalseTotal
if ref $attr 2 0 2
125 0 ▶ return $self->objects->{$objname}{$attr} unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
126 0 $self->objects->{$objname}{$attr} = $value;
127 }
128  
129 # Returns a hash of objects (in the same format as ->objects() )
130 # matching the coderef. Uses List::Util::pairgrep to populate
131 # ($a, $b) each time, $a being the object key, $b being the value hash.
132 # We then pass these as the two parameters to the callback.
133 # e.g.,
134 # $mypak->grep( sub {$_[1]->{intro_year} > 1960} )
135 # $mypak->grep( sub {$_[1]->{obj} eq 'bridge'} )
136  
137 1 ▶sub grep ($self, $cb) {
Subroutine grep: called CC=1 SCAR=0.0
Pod documented
138 1 ▶ return {List::Util::pairgrep (sub {&$cb($a, $b)}, %{$self->objects}) };
Subroutine __ANON__: called CC=1 SCAR=0.0
139 }
140  
141 1 ▶has 'object_types' => sub ($self) { {}; };
Subroutine __ANON__: called CC=1 SCAR=0.0
142  
143 0 ▶sub objects_of_type ($self, $type) {
Subroutine objects_of_type: not called CC=1 SCAR=6.9
Pod documented
144 0 ▶ return $self->grep( sub {$_[1]->{obj} eq $type} )
Subroutine __ANON__: not called CC=1 SCAR=6.9
145 0 }
146  
147 # Various Simutrans-object filters before saving to our object
148  
149 # Instead, make this 'save_object' which filters and then saves in one.
150  
151 1 ▶use Games::Simutrans::Pak;
Subroutine BEGIN: called CC=1 SCAR=0.0
152  
153 ################
154 #
155 # TODO: save_object, _object_definition_line() to be moved into Pak.pm
156 #
157 ################
158  
159 2 ▶sub save ($self, $obj) {
Subroutine save: called CC=2 SCAR=7.1
Pod documented
160  
161 # Remember each Pak object instance
162 2 ▶ if (defined $obj) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $obj) 2 0 2
163 2 $self->object($obj->{name}, $obj);
164 2 $self->object_types->{$obj->{obj}}++;
165 }
166 }
167  
168 ################
169 #
170 # Pakset-wide image collection
171 #
172 ################
173  
174 1 ▶use Games::Simutrans::Image;
Subroutine BEGIN: called CC=1 SCAR=0.0
175 1 ▶has 'imagefiles' => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
176  
177 58 ▶sub _image_level ($self, $object_name, $level, $image_spec) {
Subroutine _image_level: called CC=8 SCAR=22.0
178 # Drills down recursively, regardless of starting level, so complete proper structure exists
179 58 ▶ if ($level == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($level == 0) { } 16 42 58
elsif (ref $image_spec eq 'HASH') { } 42 0 42
180 16 ▶ if (ref $image_spec ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $image_spec ne "HASH") 0 16 16
181 0 print STDERR "Improperly formed $object_name\n";
182 0 return;
183 }
184 16 my $image_file_path = scalar $image_spec->{imagefile};
185 16 ▶ if (defined $image_file_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $image_file_path) 16 0 16
186 16 ▶ if (!defined $self->imagefiles->{ $image_file_path }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->imagefiles->{$image_file_path}) 4 12 16
187 4 $self->imagefiles->{ $image_file_path } = Games::Simutrans::Image->new(
188 file => $image_file_path , # Full path, as string
189 );
190 }
191 16 $self->imagefiles->{$image_file_path}->record_grid_coordinate($image_spec->{x}, $image_spec->{y});
192 }
193 } elsif (ref $image_spec eq 'HASH') {
194 42 foreach my $k (keys %{$image_spec}) {
195 56 ▶ $self->_image_level($object_name, $level - 1, $image_spec->{$k}) if defined $image_spec->{$k};
Branch: 1 branch
BranchTrueFalseTotal
if defined $image_spec->{$k} 56 0 56
196 }
197 }
198 }
199  
200 1 ▶sub find_all_images ($self) {
Subroutine find_all_images: called CC=4 SCAR=14.0
Pod documented
201
202 1 ▶ my $has_images = $self->grep( sub {defined $_[1]->{_hasimages}} );
Subroutine __ANON__: called CC=1 SCAR=0.0
203 1 foreach my $ii (keys %{$has_images}) {
204 2 my $o = $self->object($ii);
205 2 my @imagekeys = keys %{$o->{_hasimages}};
206 2 foreach my $imagetype (@imagekeys) {
207 2 my @images;
208 2 ▶ if ($imagetype =~ /^(?:freight|empty|cursor|icon)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($imagetype =~ /^(?:freight|empty|cursor|icon)/u) { } 0 2 2
209 # {rotation}{good_index} where direction as 'E', 'NE' etc
210 0 $self->_image_level($ii, 3, $o->{$imagetype});
211 } else {
212 # } elsif ($imagetype =~ /^(front|back)/) { # Assume all others have 6 dimensional axes
213 # {rotation}{north-south}{east-west}{height}{animation_frame}{season} where rotation = 0..15
214 2 $self->_image_level($ii, 6, $o->{$imagetype});
215 }
216 }
217 }
218 }
219  
220 ################
221 # IMAGE FILES
222 ################
223  
224 # See comments in Games::Simutrans::Image for details on why and how
225 # we impute the tilesize for each image.
226  
227 1 ▶sub find_image_tile_sizes ($self, $params = {}) {
Subroutine find_image_tile_sizes: called CC=5 SCAR=16.4
Pod documented
228  
229 1 my $images = $self->imagefiles;
230 1 ▶ return unless defined $images;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $images 0 1 1
231 1 foreach my $file (keys %{$images}) {
232 4 ▶ if (defined $self->imagefiles->{$file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->imagefiles->{$file}) 4 0 4
233 4 $self->imagefiles->{$file}->read($params); # Computes tile size, and saves when parameter save=1.
234 }
235 }
236 }
237  
238 ################
239 #
240 # Liveries
241 #
242 ################
243  
244 1 ▶use Games::Simutrans::Livery;
Subroutine BEGIN: called CC=1 SCAR=0.0
245  
246 0 ▶has 'liveries' => sub { {}; };
Subroutine __ANON__: not called CC=1 SCAR=1.2
247  
248 1 ▶sub scan_liveries ($self, $type = undef) {
Subroutine scan_liveries: called CC=8 SCAR=28.3
Pod documented
249  
250 1 my $objects;
251 1 ▶ if (defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type) { } 0 1 1
252 0 $objects = $self->objects_of_type($type);
253 } else {
254 1 $objects = $self->objects;
255 }
256  
257 1 foreach my $obj_name (keys %{$objects}) {
258  
259 2 my $this_object = $objects->{$obj_name};
260 2 my $liveries = $this_object->{liverytype};
261  
262 2 ▶ next unless (defined $liveries) && (ref $liveries eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless defined $liveries and ref $liveries eq "HASH" 2 0 2
Condition33%1/3
defined $liveries and ref $liveries eq "HASH"
!ll&&!rl&&r
200
MC/DC0%0/2
defined $liveries and ref $liveries eq "HASH"
defined $liveries ref $liveries eq "HASH"
Truth table33%1/3
A and B
A= defined $liveriesB= ref $liveries eq "HASH"
ABresult
0X0
100
111
263 0 foreach my $l (values %{$liveries}) {
264 0 ▶ $self->liveries->{$l} //= Games::Simutrans::Livery->new(name => $l);
Condition0%0/3
$self->liveries->{$l} //= "Games::Simutrans::Livery"->new("name", $l)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->liveries->{$l} //= "Games::Simutrans::Livery"->new("name", $l)
$self->liveries->{$l} "Games::Simutrans::Livery"->new("name", $l)
Truth table0%0/3
A //= B
A= $self->liveries->{$l}B= "Games::Simutrans::Livery"->new("name", $l)
ABresult
1X1
011
000
265 0 my $livery = $self->liveries->{$l};
266 0 $livery->record_use($this_object);
267 }
268 }
269 }
270  
271 ################
272 #
273 # Timeline
274 #
275 ################
276  
277 0 ▶sub timeline ($self, $type = undef) {
Subroutine timeline: not called CC=6 SCAR=37.4
Pod documented
278
279 0 my $objects;
280 0 ▶ if (defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type) { } 0 0 0
281 0 $objects = $self->objects_of_type($type);
282 } else {
283 0 $objects = $self->objects;
284 }
285  
286 0 my $timeline;
287  
288 0 my @periods = (qw(intro retire));
289 0 foreach my $obj_name (keys %{$objects}) {
290 0 ▶ next if $objects->{$obj_name}{is_permanent};
Branch: 1 branch
BranchTrueFalseTotal
if $objects->{$obj_name}{"is_permanent"} 0 0 0
291 0 my $this_type = $objects->{$obj_name}{obj};
292 0 foreach my $period (0..1) {
293 # Value will be the opposite end of the availability period
294 $timeline->{$objects->{$obj_name}{$periods[$period]}}{$periods[$period]}{$this_type}{$objects->{$obj_name}{name}} =
295 0 $objects->{$obj_name}{$periods[1-$period]};
296 }
297 }
298  
299 0 return $timeline;
300 }
301  
302 ################
303 #
304 # OBJECT DATA (.dat) FILES
305 #
306 ################
307  
308 1 has 'dat_files';
309  
310 1 ▶sub read_dat ($self, $filename) {
Subroutine read_dat: called CC=4 SCAR=14.1
Pod documented
311  
312 # Read a .dat file and pass the entire string to be parsed
313 1 my $dat_text;
314 1 ▶ eval { $dat_text = Mojo::File->new($filename)->slurp; 1; } or die "Can't open $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $dat_text = "Mojo::File"->new($filename)->slurp; 1 } } or die "Can't open ${filename}: $!" 1 0 1
315  
316 # A dat file may contain multiple objects, separated by a dashed line.
317 1 foreach my $object_text (split(/\n-{2,}\s*\n/, $dat_text)) {
318 2 my $new_object = Games::Simutrans::Pak->new->from_string({ file => $filename,
319 text => $object_text});
320 2 ▶ $self->save($new_object) if defined $new_object;
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_object 2 0 2
321 }
322  
323 }
324  
325 1 ▶sub load ($self, $path = $self->path) {
Subroutine load: called CC=6 SCAR=18.5
Pod documented
326 # Loads (or reloads) the pak's data files
327  
328 1 ▶ if (!ref $path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $path) 0 1 1
329 0 $self->path($path = Mojo::File->new(expand_tilde($path)));
330 }
331  
332 1 ▶ return undef unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 1 1
333 # Load directory recursively; or load a single file.
334 $self->dat_files( -d $path ?
335 1 ▶ $path->list_tree->grep(sub{/\.dat\z/i}) :
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
-d $path ? : 1 0 1
336 Mojo::Collection->new($path) );
337  
338 1 ▶ $self->dat_files->each ( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
339 1 $self->read_dat($_);
340 1 });
341  
342 1 ▶ eval { $self->load_language(); 1; } or $self->{_xlat_root}->{error} = $@;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->load_language; 1 } } or $self->{"_xlat_root"}{"error"} = $@ 1 0 1
343 1 $self->find_all_images();
344 1 $self->find_image_tile_sizes();
345 1 $self->scan_liveries();
346 1 1;
347 }
348  
349 1 1;
350 __END__
« Livery.pm ↑ Summary