blib/lib/Games/Simutrans/Pakset.pm

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