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

filter: Statementstmt 0.0% 0 / 161 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 65 MC/DCmcdc 0.0% 0 / 31 Subroutinesub 0.0% 0 / 11 Podpod n/a totaltotal 0.0% 0 / 280 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::HashFS 1.12
6 :isa( Archive::SCS::Mountable );
7  
8 use stable 0.031 'isa';
9  
10 use Archive::SCS::CityHash qw(
11 cityhash64
12 cityhash64_int
13 cityhash64_hex
14 cityhash64_as_hex
15 cityhash64_as_int
16 );
17 use Archive::SCS::DirIndex;
18 use Carp 'croak';
19 use Compress::Raw::Zlib 2.048 qw(crc32 Z_OK Z_STREAM_END);
20 use Fcntl 'SEEK_SET';
21 use List::Util 'first';
22 use Path::Tiny 0.053 ();
23  
24 our @CARP_NOT = qw( Archive::SCS );
25  
26 my $MAGIC = 'SCS#';
27  
28 field $path :param :reader;
29 field $fh;
30  
31 field %entries;
32 field @dirs;
33 field @files;
34  
35 ADJUST {
36 $path isa Path::Tiny && $path->exists or croak
37 "Param path must be a valid Path::Tiny object";
38 }
39  
40 my $zlib = do {
41 my %opts = ( -ConsumeInput => 0, -CRC32 => 1 );
42 my ($zlib, $status) = Compress::Raw::Zlib::Inflate->new( %opts );
43 $zlib or die $status;
44 };
45  
46  
47 method file () {
48 warnings::warnif deprecated => "file() is deprecated; use path()";
49 $path
50 }
51  
52  
53 sub handles_file ($class, $fh, $header) {
54 warnings::warnif deprecated => "handles_file() is deprecated; use handles_path()";
55 ! -d $fh && $header =~ /^\Q$MAGIC\E\x01\x00/
56 }
57  
58  
59 sub handles_path ($class, $path, $header) {
60 $header =~ /^\Q$MAGIC\E\x01\x00/
61 }
62  
63  
64 method mount () {
65 my $filename = $path->basename;
66 $self->is_mounted and croak sprintf "%s: Already mounted", $filename;
67  
68 # Read HashFS file header
69  
70 open $fh, '<:raw', $path or croak "$filename: $!";
71 my $success = read $fh, my $header, 20;
72 $success and $MAGIC eq substr $header, 0, 4 or croak
73 "$filename: Not an SCS HashFS archive";
74  
75 my ($version, $salt, $hash_method, $entry_count, $start)
76 = unpack 'vv A4 VV', substr $header, 4;
77  
78 $version == 1 or croak
79 "$filename: HashFS version $version unsupported";
80 $salt == 0 or croak
81 "$filename: HashFS salt unsupported";
82 $hash_method eq 'CITY' or croak
83 "$filename: HashFS hash method '$hash_method' unsupported";
84  
85 # Read HashFS entry headers
86  
87 my $header_template = '(QQLLLL)<';
88 my $header_length = length pack $header_template;
89 seek $fh, $start, SEEK_SET;
90  
91 for my $i ( 0 .. $entry_count - 1 ) {
92 read $fh, my $header, $header_length or die
93 "$!" || "Unexpected EOF reading entry header $i";
94 my %entry;
95 (
96 $entry{hash},
97 $entry{offset},
98 $entry{flags},
99 $entry{crc},
100 $entry{size},
101 $entry{zsize}, # compressed size
102 ) = unpack $header_template, $header;
103  
104 # Read known flags
105 $entry{is_dir} = $entry{flags} & 0x1;
106 $entry{is_compressed} = $entry{flags} & 0x2;
107  
108 $entries{ cityhash64_int $entry{hash} } = \%entry;
109 }
110  
111 return $self;
112 }
113  
114  
115 method unmount () {
116 close $fh or croak sprintf "%s: $!", $path->basename;
117 $fh = undef;
118 }
119  
120  
121 method is_mounted () {
122 !! $fh
123 }
124  
125  
126 method read_dir_tree (@roots) {
127 my $root = first { exists $entries{ cityhash64 $_ } } @roots;
128 defined $root or return;
129  
130 @dirs = ($root);
131 @files = ();
132 for (my $i = 0; $i < @dirs; ++$i) {
133 my $dir = $dirs[$i];
134  
135 my $data = eval { $self->read_entry( cityhash64 $dir ) };
136 $data isa Archive::SCS::DirIndex or croak
137 sprintf "%s: Directory '%s' not found", __PACKAGE__, $dir;
138  
139 $dir .= '/' if length $dir;
140 push @dirs, map { "$dir$_" } $data->dirs;
141 push @files, map { "$dir$_" } $data->files;
142 }
143 }
144  
145  
146 method list_dirs () {
147 return @dirs;
148 }
149  
150  
151 method list_files () {
152 return @files;
153 }
154  
155  
156 method entry_meta ($hash) {
157 return $entries{ $hash };
158 }
159  
160  
161 method read_entry ($hash) {
162 $fh or croak sprintf "%s: Not mounted", $path->basename;
163  
164 my $entry = $entries{ $hash };
165  
166 seek $fh, $entry->{offset}, SEEK_SET;
167 my $length = read $fh, my $data, $entry->{zsize};
168  
169 defined $length or croak
170 sprintf "%s: %s", $path->basename, $!;
171 $length == $entry->{zsize} or croak
172 sprintf "%s: Read %i bytes, expected %i bytes",
173 $path->basename, $length, $entry->{zsize};
174  
175 my $crc;
176 if ($entry->{is_compressed}) {
177 my $status = $zlib->inflate( \(my $raw = $data), \$data );
178  
179 $status == Z_OK || $status == Z_STREAM_END
180 or warnings::warnif io =>
181 sprintf "%s: Inflation failed: %s (%i)",
182 $path->basename, $zlib->msg // "", $status;
183  
184 $crc = $zlib->crc32;
185 $zlib->inflateReset;
186  
187 length $data == $entry->{size}
188 or warnings::warnif io =>
189 sprintf "%s: Inflated to %i bytes, expected %i bytes",
190 $path->basename, length $data, $entry->{size};
191 }
192 else {
193 $crc = crc32($data);
194 }
195 $crc == $entry->{crc}
196 or warnings::warnif io =>
197 sprintf "%s: Found CRC32 %08X, expected %08X",
198 $path->basename, $crc, $entry->{crc};
199 # The official SCS extractor doesn't seem to verify the CRC
200  
201 # Parse directory listing
202  
203 $entry->{is_dir} or return $data;
204 my %dir_index;
205 for my $item (split /\n/, $data) {
206 if ('*' eq substr $item, 0, 1) {
207 push $dir_index{dirs}->@*, substr $item, 1;
208 }
209 else {
210 push $dir_index{files}->@*, $item;
211 }
212 }
213 return Archive::SCS::DirIndex->new(%dir_index);
214 }
215  
216  
217 method entries () {
218 keys %entries
219 }
220  
221  
222 sub create_file ($pathname, $scs) {
223 $scs isa Archive::SCS or die;
224  
225 my (@entries, %entries);
226 push @entries, map { cityhash64 $_ } $scs->list_dirs, $scs->list_files;
227 push @entries, map { cityhash64_hex $_ } $scs->list_orphans;
228 push @entries, cityhash64 '' if eval { $scs->read_entry(''); 1 };
229 @entries = sort @entries;
230  
231 my $start = 0x40;
232 my $offset = $start + @entries * 0x20;
233  
234 my $fh = Path::Tiny->new($pathname)->openw_raw;
235 print $fh "\0" x $offset;
236  
237 my %opts2 = ( -CRC32 => 1, -WindowBits => 15, -Level => 9, -AppendOutput => 1 );
238 my $zlib_d = Compress::Raw::Zlib::Deflate->new(%opts2) or die;
239  
240 for my $hash (@entries) {
241 $entries{$hash}->{flags} = 0;
242 my $data = $scs->read_entry(cityhash64_as_hex $hash);
243  
244 # Serialize directory listings
245 if ($data isa Archive::SCS::DirIndex) {
246 $entries{$hash}->{flags} = 0x1;
247 $data = join "\n", $data->files, map { "*$_" } $data->dirs;
248 }
249  
250 # Compress entry contents
251 $zlib_d->deflate( \$data, \(my $compressed = '') );
252 $zlib_d->flush( \$compressed );
253 $entries{$hash}->{crc} = $zlib_d->crc32;
254 $entries{$hash}->{size} = $zlib_d->total_in;
255 $zlib_d->deflateReset;
256 if (length $compressed < $entries{$hash}->{size}) {
257 $data = $compressed;
258 $entries{$hash}->{flags} |= 0x2;
259 }
260  
261 $entries{$hash}->{offset} = $offset;
262 $entries{$hash}->{zsize} = length $data;
263 $offset += length $data;
264  
265 print $fh $data;
266 }
267  
268 seek $fh, 0, SEEK_SET or croak "$pathname: $!";
269 print $fh pack 'A4 vv A4 VV',
270 $MAGIC, 1, 0, 'CITY', (scalar @entries), $start;
271 seek $fh, $start, SEEK_SET or croak "$pathname: $!";
272  
273 for my $hash (@entries) {
274 my $entry = $entries{$hash};
275 print $fh pack '(QQLLLL)<',
276 cityhash64_as_int $hash,
277 $entry->{offset},
278 $entry->{flags},
279 $entry->{crc},
280 $entry->{size},
281 $entry->{zsize};
282 }
283 }
284  
285 1;
286  
287  
288 =head1 NAME
289  
290 Archive::SCS::HashFS - SCS HashFS version 1 format handler
291  
292 =head1 SYNOPSIS
293  
294 my $path = Path::Tiny->new('.../base.scs');
295 my $archive = Archive::SCS::HashFS->new(path => $path)->mount;
296  
297 $archive->read_dir_tree;
298 my @contents = sort $archive->list_dirs, $archive->list_files;
299  
300 my $hash = Archive::SCS::CityHash::cityhash64('def/city.sii');
301 my $data = $archive->read_entry($hash);
302  
303 =head1 DESCRIPTION
304  
305 Represents an SCS archive file encoded in HashFS version 1
306 (basically 1.49 and earlier).
307  
308 Hash values used with this module must be in the internal format
309 (currently, a 16-byte hex scalar in network byte order).
310  
311 =head1 METHODS
312  
313 =head2 entries
314  
315 $entry_hashes = $archive->entries;
316  
317 =head2 is_mounted
318  
319 $bool = $archive->is_mounted;
320  
321 =head2 handles_path
322  
323 $bool = Archive::SCS::HashFS->handles_path($path_tiny, $header);
324  
325 I<Since version 1.06.>
326  
327 In version 1.05 and earlier, there was an equivalent C<handles_file($fh, $header)>
328 method, which is now deprecated.
329  
330 =head2 list_dirs
331  
332 @directories = $archive->list_dirs;
333  
334 =head2 list_files
335  
336 @files = $archive->list_files;
337  
338 =head2 mount
339  
340 $archive = $archive->mount;
341  
342 =head2 new
343  
344 $archive = Archive::SCS::HashFS->new(file => $path_tiny);
345  
346 =head2 path
347  
348 $path_tiny = $archive->path;
349  
350 I<Since version 1.06.>
351  
352 In version 1.05 and earlier, there was an equivalent C<file()>
353 method, which is now deprecated.
354  
355 =head2 read_dir_tree
356  
357 $archive->read_dir_tree;
358  
359 =head2 read_entry
360  
361 $data = $archive->read_entry($hash);
362  
363 =head2 unmount
364  
365 $archive->unmount;
366  
367 =head1 SEE ALSO
368  
369 =over
370  
371 =item * L<https://modding.scssoft.com/index.php?title=Documentation/Tools/Game_Archive_Extractor&oldid=4568>
372  
373 =item * L<https://github.com/sk-zk/TruckLib/tree/master/TruckLib.HashFs>
374  
375 =item * L<https://github.com/truckermudgeon/maps/tree/main/packages/clis/parser/game-files>
376  
377 =item * L<https://forum.scssoft.com/viewtopic.php?p=644638#p644638>
378  
379 =item * L<https://forum.scssoft.com/viewtopic.php?p=1233222#p1233222>
380  
381 =item * L<https://forum.scssoft.com/viewtopic.php?t=248485>
382  
383 =back
384  
385 =head1 AUTHOR
386  
387 L<nautofon|https://github.com/nautofon>
388  
389 =head1 COPYRIGHT
390  
391 This software is copyright (c) 2026 by nautofon.
392  
393 This is free software; you can redistribute it and/or modify it under
394 the same terms as the Perl 5 programming language system itself.