blib/lib/Archive/SCS/GameDir.pm untested

filter: Statementstmt 0.0% 0 / 91 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 10 Podpod n/a totaltotal 0.0% 0 / 140 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 use v5.32;
2 use warnings;
3 use Object::Pad 0.73;
4  
5 class Archive::SCS::GameDir 1.12;
6  
7 use builtin::compat 'trim';
8  
9 use Archive::SCS;
10 use Carp 'croak';
11 use List::Util 1.33 qw(any first none);
12 use Path::Tiny 0.017 ();
13  
14 our @LOAD_ORDER = qw(
15 version.scs
16 effect.scs
17 base.scs
18 base_map.scs
19 base_vehicle.scs
20 base_share.scs
21 def.scs
22 core.scs
23 locale.scs
24 base_cfg.scs
25 );
26  
27 our @LIBRARY_PATHS = (
28 (split /:/, $ENV{STEAM_LIBRARY} // ''),
29 eval {
30 require Archive::SCS::GameDir::Steam;
31 @Archive::SCS::GameDir::Steam::library_paths
32 },
33 '~/.steam/steam',
34 '~/.local/share/Steam',
35 '~/.var/app/com.valvesoftware.Steam/.steam/steam',
36 '~/.var/app/com.valvesoftware.Steam/.local/share/Steam',
37 '~/Library/Application Support/Steam',
38 '~/Library/Application Support/SteamApps',
39 );
40  
41 our %GAMES = (
42 ATS => 'American Truck Simulator',
43 ETS2 => 'Euro Truck Simulator 2',
44 );
45  
46 field $game :param = undef;
47  
48 field $path;
49  
50 field @library_paths = @LIBRARY_PATHS;
51  
52 ADJUST {
53 $self->find($game);
54 }
55  
56  
57 method game () {
58 $game
59 }
60  
61  
62 method game_short () {
63 no warnings 'uninitialized';
64 return first { $game eq $GAMES{$_} } keys %GAMES;
65 }
66  
67  
68 method path () {
69 $path
70 }
71  
72  
73 method find ($name = '') {
74 $name //= '';
75 if ( length $name ) {
76 my $dir = Path::Tiny::path $name;
77 if ($dir->child('base.scs')->exists) {
78 $game = ($path = $dir)->basename;
79 return $self;
80 }
81 }
82  
83 my @games = map { $GAMES{$_} } sort keys %GAMES;
84 length $name and @games = $GAMES{ uc $name } // $name;
85  
86 my @paths;
87 for my $fullname ( @games ) {
88 push @paths, map { Path::Tiny::path "$_/steamapps/common/$fullname" } @library_paths;
89 }
90  
91 $path = first { $_->child('base.scs')->exists } @paths;
92 $game = defined $path ? $path->basename : undef;
93 return $self;
94 }
95  
96  
97 method library_paths () {
98 @library_paths
99 }
100  
101  
102 method set_library_paths (@paths) {
103 @library_paths = @paths;
104 return $self;
105 }
106  
107  
108 method archives () {
109 $path or croak 'Game dir not found; cannot list archives';
110  
111 my @files = grep { $path->child($_)->exists } @LOAD_ORDER;
112 push @files, sort
113 grep { my $file = $_; none { $file eq $_ } @files }
114 grep /\.scs$/, map { $_->basename } $path->children;
115 return @files;
116 }
117  
118  
119 method mounted (@files) {
120 $path or croak 'Game dir not found; cannot mount';
121  
122 @files or @files = $self->archives;
123 @files = map {
124 $_ =~ /\//
125 ? Path::Tiny::path($_)
126 : Path::Tiny::path($_)->absolute($path)
127 } @files;
128  
129 my $scs = Archive::SCS->new;
130 $scs->mount($_) for @files;
131 return $scs;
132 }
133  
134  
135 method version () {
136 $path or croak 'Game dir not found; cannot determine version';
137  
138 if ( any { 'version.scs' eq $_ } $self->archives ) { # >= 1.50
139 my $scs = $self->mounted('version.scs');
140 return eval { $scs->read_entry('version.sii') =~ m/version:\s*"([^"]*)"/; $1 };
141 }
142 else { # < 1.50
143 my $scs = $self->mounted('base.scs');
144 return eval { trim $scs->read_entry('version.txt') };
145 }
146 }
147  
148 1;
149  
150  
151 =head1 NAME
152  
153 Archive::SCS::GameDir - Find the game dir and mount its archives
154  
155 =head1 SYNOPSIS
156  
157 my $scs = Archive::SCS::GameDir->new->mounted;
158 # $scs isa Archive::SCS
159  
160 my $path = Archive::SCS::GameDir->new(game => 'ATS')->path;
161 # $path isa Path:Tiny
162  
163 my $gd = Archive::SCS::GameDir->new;
164 my @files = grep !/^dlc_/, $gd->archives;
165 my $scs = $gd->mounted( @files, 'dlc_ks.scs', 'dlc_ok.scs' );
166  
167 # Custom paths
168 $gd->set_library_paths( @steam_library_paths );
169 my $path = $gd->find('ATS')->path;
170 my $scs = $gd->find('/path/to/gamedir')->mounted;
171  
172 =head1 DESCRIPTION
173  
174 Finds the game install directory inside your home directory.
175  
176 Several default locations for the Steam library will be
177 searched. If your Steam library is in a non-standard
178 location, there are four different ways in which you can
179 supply additional paths to search first:
180  
181 =over
182  
183 =item *
184  
185 Specify the list of search paths for an individual GameDir
186 object using the C<set_library_paths()> object method.
187 Call C<find()> afterwards.
188  
189 =item *
190  
191 Manipulate the existing default list of search paths in the
192 C<@Archive::SCS::GameDir::LIBRARY_PATHS> global variable.
193  
194 =item *
195  
196 Set the C<STEAM_LIBRARY> environment variable.
197  
198 =item *
199  
200 Install a custom C<Archive::SCS::GameDir::Steam> module that
201 sets the global variable C<@library_paths>.
202  
203 =back
204  
205 =head1 METHODS
206  
207 =head2 archives
208  
209 @files = $gamedir->archives;
210  
211 Returns the list of filenames found in the game directory.
212  
213 =head2 find
214  
215 $gamedir = $gamedir->find;
216 $gamedir = $gamedir->find('ATS');
217 $gamedir = $gamedir->find('/path/to/gamedir');
218  
219 Tries to find the game directory for the given game in the
220 current Steam library paths. Will modify the object state.
221 Use C<game()> / C<game_short()> / C<path()> to read the result.
222  
223 If there are multiple game directories in the paths, the search
224 stops at the first one found.
225  
226 =head2 game
227  
228 $full_name = $gamedir->game;
229  
230 Returns the full name of the game directory, or C<undef>
231 if the search was unsuccessful.
232  
233 =head2 game_short
234  
235 $abbreviation = $gamedir->game_short;
236  
237 Returns an abbreviation for the game name (like C<ATS>),
238 or C<undef> if the search was unsuccessful.
239  
240 =head2 library_paths
241  
242 @steam_paths = $gamedir->library_paths;
243  
244 Returns the current list of paths to search for the Steam library.
245  
246 =head2 mounted
247  
248 $scs = $gamedir->mounted;
249 $scs = $gamedir->mounted(@files);
250  
251 Returns a new L<Archive::SCS> object with all files found in
252 the game directory already mounted. If a list of filenames is
253 given, only those files will be mounted.
254  
255 =head2 new
256  
257 $gamedir = Archive::SCS::GameDir->new;
258 $gamedir = Archive::SCS::GameDir->new(game => 'ATS');
259 $gamedir = Archive::SCS::GameDir->new(game => '/path/to/gamedir');
260  
261 Creates a new L<Archive::SCS::GameDir> object.
262 Will call C<find()> with the given game as argument.
263  
264 =head2 path
265  
266 $path_tiny = $gamedir->path;
267  
268 Returns a L<Path::Tiny> object for the game directory,
269 or C<undef> if the search was unsuccessful.
270  
271 =head2 set_library_paths
272  
273 $gamedir = $gamedir->set_library_paths(@steam_paths);
274  
275 Sets the list of paths to search for the Steam library. The Steam
276 library is the directory containing the C<steamapps> directory.
277  
278 =head2 version
279  
280 $installed_version = $gamedir->version;
281  
282 Reads the game directory and reports the installed game version.
283  
284 =head1 AUTHOR
285  
286 L<nautofon|https://github.com/nautofon>
287  
288 =head1 COPYRIGHT
289  
290 This software is copyright (c) 2026 by nautofon.
291  
292 This is free software; you can redistribute it and/or modify it under
293 the same terms as the Perl 5 programming language system itself.