blib/lib/Archive/SCS.pm untested

filter: Statementstmt 0.0% 0 / 115 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 13 Podpod n/a totaltotal 0.0% 0 / 180 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 use v5.34;
2 use warnings;
3 use Object::Pad 0.73;
4  
5 class Archive::SCS 1.12;
6  
7 use stable 0.031 'isa';
8  
9 use Archive::SCS::CityHash qw(cityhash64 cityhash64_hex cityhash64_as_hex);
10 use Carp 'croak';
11 use List::Util 1.45 qw(first uniqstr);
12 use Module::Load 'load';
13 use Path::Tiny 0.054 'path';
14  
15 use Archive::SCS::Directory;
16 use Archive::SCS::HashFS;
17 use Archive::SCS::HashFS2;
18 use Archive::SCS::Zip;
19  
20 field @formats = qw( HashFS2 HashFS Directory Zip );
21  
22 field @mounts;
23 field %entries;
24  
25 my @ROOTS = ('', 'locale');
26  
27  
28 method formats () {
29 @formats
30 }
31  
32  
33 method set_formats {
34 load __PACKAGE__ . "::$_" for @_;
35 @formats = @_;
36 return $self;
37 }
38  
39  
40 method format_module ($path) {
41 my $header = '';
42 if (! $path->is_dir) {
43 open my $fh, '<:raw', $path or croak
44 sprintf "%s: $!", $path->basename;
45 read $fh, $header, 8 or croak
46 sprintf "%s: $!", $path->basename;
47 close $fh;
48 }
49  
50 my @modules = map { __PACKAGE__ . "::$_" } @formats;
51 my $module = first { $_->handles_path($path, $header) } @modules or croak
52 sprintf "%s: No suitable format handler found", $path->basename;
53 return $module;
54 }
55  
56  
57 method mount ($mountable) {
58 if ( not $mountable isa Archive::SCS::Mountable ) {
59 my $path = path $mountable;
60 my $format = $self->format_module($path);
61 $mountable = $format->new(path => $path);
62 }
63 my $basename = $mountable->path->basename;
64  
65 $self->is_mounted($mountable) and croak
66 sprintf "%s: Already mounted", $basename;
67  
68 my $mount = $mountable->mount;
69 $mount->read_dir_tree(@ROOTS);
70 push @mounts, $mount;
71 push $entries{$_}->@*, $mount for my @entries = $mount->entries;
72  
73 return $mount;
74 }
75  
76  
77 method unmount ($mount) {
78 if ( not $mount isa Archive::SCS::Mountable ) {
79 my $path = path $mount;
80 $mount = first { $path->realpath eq $_->path->realpath } @mounts or croak
81 sprintf "%s: Not mounted", $path->basename;
82 }
83  
84 $mount->unmount;
85  
86 # Remove mount from entries list
87 for my $hash ( keys %entries ) {
88 $entries{$hash} = [ grep { $mount != $_ } $entries{$hash}->@* ];
89 $entries{$hash}->@* == 0 and delete $entries{$hash};
90 }
91 @mounts = grep { $mount != $_ } @mounts;
92  
93 return $mount;
94 }
95  
96  
97 method is_mounted ($mountable) {
98 first { $mountable->path->realpath eq $_->path->realpath } @mounts
99 }
100  
101  
102 method entry_mounts ($path) {
103 my $mounts = $entries{ cityhash64 $path };
104 $mounts //= $entries{ cityhash64_hex $path };
105 my @mounts = $mounts ? $mounts->@* : ();
106 return @mounts;
107 }
108  
109  
110 method read_entry ($path) {
111 my $hash;
112 my $mounts = $entries{ $hash = cityhash64 $path };
113 $mounts //= $entries{ $hash = cityhash64_hex $path };
114 $mounts or croak
115 sprintf "'%s': no entry found", $path;
116  
117 my $mount = $mounts->[ $#{$mounts} ];
118 return $mount->read_entry($hash);
119 }
120  
121  
122 method list_dirs ($root = '') {
123 my @dirs = uniqstr sort map { $_->list_dirs } @mounts;
124 @dirs = grep /^\Q$root\E(?:\/|$)/, @dirs if length $root;
125  
126 # Skip default root (the empty string)
127 shift @dirs unless length $dirs[0];
128 return @dirs;
129 }
130  
131  
132 method list_files ($root = '') {
133 my @files = uniqstr sort map { $_->list_files } @mounts;
134 @files = grep /^\Q$root\E(?:\/|$)/, @files if length $root;
135  
136 return @files;
137 }
138  
139  
140 method list_orphans () {
141 my @paths = ( eval { $self->list_dirs }, eval { $self->list_files } );
142 my %paths = map {( cityhash64 $_ => $_ )} @paths, @ROOTS;
143  
144 my @orphans;
145 for my $hash ( keys %entries ) {
146 defined $paths{$hash} or push @orphans, $hash;
147 }
148 return map { cityhash64_as_hex $_ } uniqstr sort @orphans;
149 }
150  
151 1;
152  
153  
154 =head1 NAME
155  
156 Archive::SCS - SCS archive controller
157  
158 =head1 SYNOPSIS
159  
160 my $scs = Archive::SCS->new;
161 $scs->mount("$path/base.scs");
162 $scs->mount("$path/def.scs");
163  
164 my @contents = sort $scs->list_dirs, $scs->list_files;
165  
166 say $scs->read_entry('def/env_data.sii');
167  
168 =head1 DESCRIPTION
169  
170 Handles the union file system used by SCS archive files.
171 Allows mounting of multiple files or extracted directories and
172 performs lookups in all of them using the SCS hash algorithm.
173  
174 These modules exist primarily to support the F<scs_archive>
175 command-line tool included in this distribution.
176  
177 =head1 METHODS
178  
179 =head2 formats
180  
181 @formats = $scs->formats;
182  
183 Returns the list of currently active formats.
184 See C<set_formats()>.
185  
186 =head2 list_dirs
187  
188 @directories = $scs->list_dirs;
189  
190 Returns an ordered list of all directory paths in currently
191 mounted archives. The root directory, represented by an empty
192 string, is currently omitted from the list.
193  
194 Paths are returned without a leading C</> because that's the
195 way they are stored internally. This is subject to change,
196 but the output of C<list_dirs()> will always be good to use
197 as path for C<read_entry()>.
198  
199 =head2 list_files
200  
201 @files = $scs->list_files;
202  
203 Returns an ordered list of all file paths in currently mounted
204 archives.
205  
206 Paths are returned without a leading C</> because that's the
207 way they are stored internally. This is subject to change,
208 but the output of C<list_files()> will always be good to use
209 as path for C<read_entry()>.
210  
211 =head2 list_orphans
212  
213 @orphan_hashes = $scs->list_orphans;
214  
215 Returns a list of hash values for
216 orphans in currently mounted archives.
217  
218 Some file formats allow files (or subdirs) without a directory entry.
219 These files may be accessed directly using their hash value. This
220 software refers to such files as orphans. For example, the following
221 orphans are known to exist in 1.49.3.14:
222  
223 05c075dc23d8d177 # in core.scs, 'def/achievements.sii'
224 0eeaffbe65995414 # in base.scs + core.scs, 'ui/desktop_demo.sii'
225 0fb3a3294f8ac99c # in base_cfg.scs
226 2a794836b65afe88 # in base.scs
227 34f7048e2d3b04b6 # in core.scs, 'def/online_economy_data.sii'
228 507dcc5fb3fb6443 # in core.scs, 'def/online_data.sii'
229 83a9902d7733b94d # in core.scs, 'def/mod_manager_data.sii'
230 88a1194cb25b253c # in core.scs, 'ui/desktop_standalone_demo.sii'
231 c09356068ea66aac # in core.scs, 'def/world/building_scheme_core_scs.sii'
232 d9d3d2a218c69f3d # in base.scs
233 e773fb1407c8468d # in core.scs, 'def/world/building_model_core_scs.sii'
234  
235 =head2 mount
236  
237 $archive = $scs->mount($pathname);
238 $archive = $scs->mount($mountable);
239  
240 Adds the given SCS archive to the currently mounted archives.
241 Returns an L<Archive::SCS::Mountable> object. If a file system
242 path is given as argument, the object will be created by
243 attempting to load the given archive with the currently active
244 formats. See C<set_formats()>.
245  
246 =head2 new
247  
248 $scs = Archive::SCS->new;
249  
250 Creates a new L<Archive::SCS> object.
251  
252 =head2 read_entry
253  
254 $data = $scs->read_entry($pathname);
255 $data = $scs->read_entry($hash);
256  
257 Returns the contents of the given entry. Directories will be
258 returned as L<Archive::SCS::DirIndex> objects and S<HashFS v2>
259 texture objects as L<Archive::SCS::TObj>.
260  
261 The argument may be the pathname within the archive or its hash
262 value, hex-encoded in network byte order as a 16-byte scalar PV.
263 Paths are expected without a leading C</> because that's the
264 way they are stored internally. This is subject to change,
265 but the output of C<list_files()> will always be good to use
266 as path for C<read_entry()>.
267  
268 =head2 set_formats
269  
270 $scs = $scs->set_formats(qw[ HashFS Zip ]);
271  
272 Sets the list of currently active formats. All formats must be
273 in the C<Archive::SCS> namespace. By default, the list includes
274 all formats implemented in this distribution, which currently
275 are the following:
276  
277 =over
278  
279 =item * L<Archive::SCS::Directory>
280  
281 =item * L<Archive::SCS::HashFS>
282  
283 =item * L<Archive::SCS::HashFS2>
284  
285 =item * L<Archive::SCS::Zip>
286  
287 =back
288  
289 =head2 unmount
290  
291 $archive = $scs->unmount($pathname);
292 $archive = $scs->unmount($mountable);
293  
294 Removes the given SCS archive from currently mounted archives.
295 Returns the archive's L<Archive::SCS::Mountable> object.
296  
297 =head1 LIMITATIONS
298  
299 The implementation of texture objects is still evolving and is
300 currently inconsistent between format handlers.
301  
302 =head1 SEE ALSO
303  
304 =over
305  
306 =item * L<Archive::SCS::GameDir>
307  
308 =back
309  
310 =head1 AUTHOR
311  
312 L<nautofon|https://github.com/nautofon>
313  
314 =head1 COPYRIGHT
315  
316 This software is copyright (c) 2026 by nautofon.
317  
318 This is free software; you can redistribute it and/or modify it under
319 the same terms as the Perl 5 programming language system itself.
320  
321 Includes L<CityHash|https://github.com/google/cityhash> 1.0.3,
322 Copyright (c) 2011 Google, Inc. (MIT license)