| 1 | use v5.34; | ||
| 2 | use warnings; | ||
| 3 | use Object::Pad 0.73; | ||
| 4 | |||
| 5 | class Archive::SCS::HashFS2 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 Archive::SCS::TObj; | ||
| 19 | |||
| 20 | use Carp 'croak'; | ||
| 21 | use Compress::Raw::Zlib 2.048 qw(Z_OK Z_STREAM_END); | ||
| 22 | use Fcntl 'SEEK_SET'; | ||
| 23 | use List::Util 'first'; | ||
| 24 | use Path::Tiny 0.011 (); | ||
| 25 | |||
| 26 | our @CARP_NOT = qw( Archive::SCS ); | ||
| 27 | |||
| 28 | my $MAGIC = 'SCS#'; | ||
| 29 | |||
| 30 | field $path :param :reader; | ||
| 31 | field $fh; | ||
| 32 | |||
| 33 | field %entries; | ||
| 34 | field @dirs; | ||
| 35 | field @files; | ||
| 36 | |||
| 37 | ADJUST { | ||
| 38 | $path isa Path::Tiny && $path->exists or croak | ||
| 39 | "Param path must be a valid Path::Tiny object"; | ||
| 40 | } | ||
| 41 | |||
| 42 | my $zlib = do { | ||
| 43 | my %opts = ( -ConsumeInput => 0 ); | ||
| 44 | my ($zlib, $status) = Compress::Raw::Zlib::Inflate->new( %opts ); | ||
| 45 | $zlib or die $status; | ||
| 46 | }; | ||
| 47 | |||
| 48 | |||
| 49 | method file () { | ||
| 50 | warnings::warnif deprecated => "file() is deprecated; use path()"; | ||
| 51 | $path | ||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | sub handles_file ($class, $fh, $header) { | ||
| 56 | warnings::warnif deprecated => "handles_file() is deprecated; use handles_path()"; | ||
| 57 | $header =~ /^\Q$MAGIC\E\x02\x00/ | ||
| 58 | } | ||
| 59 | |||
| 60 | |||
| 61 | sub handles_path ($class, $path, $header) { | ||
| 62 | $header =~ /^\Q$MAGIC\E\x02\x00/ | ||
| 63 | } | ||
| 64 | |||
| 65 | |||
| 66 | method mount () { | ||
| 67 | my $filename = $path->basename; | ||
| 68 | $self->is_mounted and croak sprintf "%s: Already mounted", $filename; | ||
| 69 | |||
| 70 | # Read HashFS file header | ||
| 71 | |||
| 72 | open $fh, '<:raw', $path or croak "$filename: $!"; | ||
| 73 | read $fh, my $header, 0x34 or croak "$filename: $!"; | ||
| 74 | |||
| 75 | my ($magic, $version, $salt, $hash_method, $entry_count, | ||
| 76 | $size1, $word_count2, $size2, $start1, $start2, $cert_start) | ||
| 77 | = unpack 'A4 vv A4 V VVV (QQQ)<', $header; | ||
| 78 | |||
| 79 | $magic eq $MAGIC or croak | ||
| 80 | "$filename: Not an SCS HashFS archive"; | ||
| 81 | $version == 2 or croak | ||
| 82 | "$filename: HashFS version $version unsupported"; | ||
| 83 | $salt == 0 or croak | ||
| 84 | "$filename: HashFS salt unsupported"; | ||
| 85 | $hash_method eq 'CITY' or croak | ||
| 86 | "$filename: HashFS hash method '$hash_method' unsupported"; | ||
| 87 | |||
| 88 | # Read HashFS entry headers | ||
| 89 | |||
| 90 | my $index1 = $self->_get_index( $start1, $size1, $entry_count * 0x10 ); | ||
| 91 | my $index2 = $self->_get_index( $start2, $size2, $word_count2 * 4 ); | ||
| 92 | |||
| 93 | for (my $j = 0; $j < $entry_count; ++$j) { | ||
| 94 | |||
| 95 | # Index 1: hashes | ||
| 96 | |||
| 97 | my ($hash, $index_offset, $parts, $flags1) | ||
| 98 | = unpack '(QLSS)<', substr $index1, ($j * 0x10), 0x10; | ||
| 99 | |||
| 100 | my $is_dir = $flags1 & 1; | ||
| 101 | my $preload = $flags1 & 4; | ||
| 102 | |||
| 103 | # Index 2: headers | ||
| 104 | |||
| 105 | my $data_part_header; | ||
| 106 | my @parts; | ||
| 107 | for (my $k = 0; $k < $parts; ++$k) { | ||
| 108 | my ($offset, $offset_high, $kind ) | ||
| 109 | = unpack '(SCC)<', substr $index2, (($index_offset + $k) * 4), 4; | ||
| 110 | |||
| 111 | $offset |= $offset_high << 0x10; | ||
| 112 | my $size = (my $is_data_part = $kind & 0x80) ? 16 | ||
| 113 | : $kind == 1 ? 8 | ||
| 114 | : $kind == 2 ? 4 | ||
| 115 | : $kind == 5 ? 32 # seen with .pma files on 1.55+ | ||
| 116 | : $kind == 6 ? 8 # seen with .pmg files on 1.55+ | ||
| 117 | : 0 or warnings::warnif io => | ||
| 118 | sprintf "Encountered index 2 part with unknown kind %02x", $kind; | ||
| 119 | |||
| 120 | push @parts, { | ||
| 121 | offset => $offset, | ||
| 122 | kind => $kind, | ||
| 123 | header => (my $header = substr $index2, ($offset * 4), $size), | ||
| 124 | }; | ||
| 125 | $is_data_part and $data_part_header = $header; | ||
| 126 | } | ||
| 127 | |||
| 128 | my ( | ||
| 129 | $zsize, $zsize_high, $flags2, | ||
| 130 | $usize, $usize_high, $flags3, | ||
| 131 | $unknown7, $data_offset, | ||
| 132 | ) | ||
| 133 | = unpack '(SCC SCC LL)<', $data_part_header; | ||
| 134 | |||
| 135 | $zsize |= $zsize_high << 0x10; | ||
| 136 | $usize |= $usize_high << 0x10; | ||
| 137 | my $compression = $flags2 & 0xf0; | ||
| 138 | my $is_tobj = $parts[0]->{kind} == 1; | ||
| 139 | |||
| 140 | $entries{ cityhash64_int $hash } = { | ||
| 141 | offset => $data_offset * 0x10, | ||
| 142 | zsize => $zsize, | ||
| 143 | size => $usize, | ||
| 144 | compression => $compression, | ||
| 145 | is_dir => $is_dir, | ||
| 146 | is_tobj => $is_tobj, | ||
| 147 | parts => \@parts, | ||
| 148 | flags1 => $flags1, | ||
| 149 | flags2 => $flags2, | ||
| 150 | flags3 => $flags3, | ||
| 151 | }; | ||
| 152 | |||
| 153 | } | ||
| 154 | |||
| 155 | return $self; | ||
| 156 | } | ||
| 157 | |||
| 158 | |||
| 159 | method unmount () { | ||
| 160 | close $fh or croak sprintf "%s: $!", $path->basename; | ||
| 161 | $fh = undef; | ||
| 162 | } | ||
| 163 | |||
| 164 | |||
| 165 | method is_mounted () { | ||
| 166 | !! $fh | ||
| 167 | } | ||
| 168 | |||
| 169 | |||
| 170 | method _get_index ($start, $size, $expected) { | ||
| 171 | seek $fh, $start, SEEK_SET; | ||
| 172 | my $length = read $fh, (my $raw), $size; | ||
| 173 | |||
| 174 | defined $length or croak | ||
| 175 | sprintf "Failed to read index at %06x: %s", $start, $!; | ||
| 176 | $length == $size or croak | ||
| 177 | sprintf "Read %i bytes from index at %06x, expected %i bytes", | ||
| 178 | $length, $start, $size; | ||
| 179 | |||
| 180 | my $status = $zlib->inflate( \$raw, \(my $index) ); | ||
| 181 | |||
| 182 | $status == Z_OK || $status == Z_STREAM_END or warnings::warnif io => | ||
| 183 | sprintf "Failed to inflate index at %06x: %s (%i)", | ||
| 184 | $start, $zlib->msg // "", $status; | ||
| 185 | $expected == length $index or warnings::warnif io => | ||
| 186 | sprintf "Inflated index at %06x to %i bytes, expected %i bytes", | ||
| 187 | $start, length $index, $expected; | ||
| 188 | |||
| 189 | $zlib->inflateReset; | ||
| 190 | return $index; | ||
| 191 | } | ||
| 192 | |||
| 193 | |||
| 194 | method read_dir_tree (@roots) { | ||
| 195 | my $root = first { exists $entries{ cityhash64 $_ } } @roots; | ||
| 196 | defined $root or return; | ||
| 197 | |||
| 198 | @dirs = ($root); | ||
| 199 | @files = (); | ||
| 200 | for (my $i = 0; $i < @dirs; ++$i) { | ||
| 201 | my $dir = $dirs[$i]; | ||
| 202 | |||
| 203 | my $data = eval { $self->read_entry( cityhash64 $dir ) }; | ||
| 204 | $data isa Archive::SCS::DirIndex or croak | ||
| 205 | sprintf "%s: Directory '%s' not found", __PACKAGE__, $dir; | ||
| 206 | |||
| 207 | $dir .= '/' if length $dir; | ||
| 208 | push @dirs, map { "$dir$_" } $data->dirs; | ||
| 209 | push @files, map { "$dir$_" } $data->files; | ||
| 210 | } | ||
| 211 | } | ||
| 212 | |||
| 213 | |||
| 214 | method list_dirs () { | ||
| 215 | return @dirs; | ||
| 216 | } | ||
| 217 | |||
| 218 | |||
| 219 | method list_files () { | ||
| 220 | return @files; | ||
| 221 | } | ||
| 222 | |||
| 223 | |||
| 224 | method entry_meta ($hash) { | ||
| 225 | return $entries{ $hash }; | ||
| 226 | } | ||
| 227 | |||
| 228 | |||
| 229 | method read_entry ($hash) { | ||
| 230 | $fh or croak sprintf "%s: Not mounted", $path->basename; | ||
| 231 | |||
| 232 | my $entry = $entries{ $hash }; | ||
| 233 | |||
| 234 | seek $fh, $entry->{offset}, SEEK_SET; | ||
| 235 | my $length = read $fh, my $data, $entry->{zsize}; | ||
| 236 | |||
| 237 | defined $length or croak | ||
| 238 | sprintf "%s: %s", $path->basename, $!; | ||
| 239 | $length == $entry->{zsize} or croak | ||
| 240 | sprintf "%s: Read %i bytes, expected %i bytes", | ||
| 241 | $path->basename, $length, $entry->{zsize}; | ||
| 242 | |||
| 243 | if ($entry->{compression} == 0x10) { # zlib | ||
| 244 | my $status = $zlib->inflate( \(my $raw = $data), \$data ); | ||
| 245 | |||
| 246 | $status == Z_OK || $status == Z_STREAM_END | ||
| 247 | or warnings::warnif io => | ||
| 248 | sprintf "%s: Inflation failed: %s (%i)", | ||
| 249 | $path->basename, $zlib->msg // "", $status; | ||
| 250 | |||
| 251 | $zlib->inflateReset; | ||
| 252 | |||
| 253 | length $data == $entry->{size} | ||
| 254 | or warnings::warnif io => | ||
| 255 | sprintf "%s: Inflated to %i bytes, expected %i bytes", | ||
| 256 | $path->basename, length $data, $entry->{size}; | ||
| 257 | } | ||
| 258 | |||
| 259 | if ($entry->{is_dir}) { | ||
| 260 | # Parse directory listing | ||
| 261 | my %dir_index; | ||
| 262 | my $count = unpack "V", $data; | ||
| 263 | my @sizes = unpack "C[$count]", substr $data, 4; | ||
| 264 | my $offset = 4 + $count; | ||
| 265 | for (my $i = 0; $i < $count; ++$i) { | ||
| 266 | my $item = substr $data, $offset, $sizes[$i]; | ||
| 267 | $offset += $sizes[$i]; | ||
| 268 | if ('/' eq substr $item, 0, 1) { | ||
| 269 | push $dir_index{dirs}->@*, substr $item, 1; | ||
| 270 | } | ||
| 271 | else { | ||
| 272 | push $dir_index{files}->@*, $item; | ||
| 273 | } | ||
| 274 | } | ||
| 275 | return Archive::SCS::DirIndex->new(%dir_index); | ||
| 276 | } | ||
| 277 | elsif ($entry->{is_tobj}) { | ||
| 278 | return Archive::SCS::TObj->new( | ||
| 279 | meta => $entry, | ||
| 280 | data => $data, | ||
| 281 | ); | ||
| 282 | } | ||
| 283 | |||
| 284 | return $data; | ||
| 285 | } | ||
| 286 | |||
| 287 | |||
| 288 | method entries () { | ||
| 289 | keys %entries | ||
| 290 | } | ||
| 291 | |||
| 292 | |||
| 293 | sub create_file ($pathname, $scs) { | ||
| 294 | $scs isa Archive::SCS or die; | ||
| 295 | |||
| 296 | # This subroutine is designed for internal testing. It probably won't | ||
| 297 | # produce files that are compatible with SCS. All entry contents are | ||
| 298 | # loaded into memory. | ||
| 299 | |||
| 300 | my (@entries, %entries); | ||
| 301 | push @entries, map { cityhash64 $_ } $scs->list_dirs, $scs->list_files; | ||
| 302 | push @entries, map { cityhash64_hex $_ } $scs->list_orphans; | ||
| 303 | push @entries, cityhash64 '' if eval { $scs->read_entry(''); 1 }; | ||
| 304 | $entries{$_} = { | ||
| 305 | data => scalar eval { $scs->read_entry(cityhash64_as_hex $_) }, | ||
| 306 | kind => 0x80, | ||
| 307 | flags1 => 0, | ||
| 308 | } for @entries; | ||
| 309 | @entries = grep { defined $entries{$_}->{data} } @entries; | ||
| 310 | |||
| 311 | # Serialize directory listings | ||
| 312 | for my $hash ( @entries ) { | ||
| 313 | $entries{$hash}->{data} isa Archive::SCS::DirIndex or next; | ||
| 314 | my @dirs = map { "/$_" } $entries{$hash}->{data}->dirs; | ||
| 315 | my @files = $entries{$hash}->{data}->files; | ||
| 316 | my $count = @dirs + @files; | ||
| 317 | $entries{$hash}->{kind} = 0x81; | ||
| 318 | $entries{$hash}->{flags1} |= 1; # is_dir | ||
| 319 | $entries{$hash}->{data} = | ||
| 320 | (pack "V C[$count]", $count, map { length } @dirs, @files) | ||
| 321 | . join '', @dirs, @files; | ||
| 322 | } | ||
| 323 | |||
| 324 | # Texture objects are unimplemented | ||
| 325 | do { ... } for grep { $entries{$_}->{data} isa Archive::SCS::TObj } @entries; | ||
| 326 | |||
| 327 | @entries = sort { | ||
| 328 | ($entries{$b}->{flags1} & 4) <=> ($entries{$a}->{flags1} & 4) | ||
| 329 | or $entries{$a}->{flags1} <=> $entries{$b}->{flags1} | ||
| 330 | or ($entries{$b}->{kind} & 0x80) <=> ($entries{$a}->{kind} & 0x80) | ||
| 331 | or $entries{$a}->{kind} <=> $entries{$b}->{kind} | ||
| 332 | } @entries; | ||
| 333 | |||
| 334 | # Prepare entry contents | ||
| 335 | my $start = 0x60; | ||
| 336 | my $offset = 0; | ||
| 337 | for my $hash (@entries) { | ||
| 338 | my $entry = $entries{$hash}; | ||
| 339 | |||
| 340 | $entry->{usize} = length $entry->{data}; | ||
| 341 | $entry->{compression} = 0; | ||
| 342 | my $compressed = _compress_zlib($entry->{data}); | ||
| 343 | if (length $compressed < length $entry->{data}) { | ||
| 344 | $entry->{data} = $compressed; | ||
| 345 | $entry->{compression} = 0x10; | ||
| 346 | } | ||
| 347 | |||
| 348 | $entry->{offset} = $offset; | ||
| 349 | my $size = length $entry->{data}; | ||
| 350 | $entry->{padding} = $size & 0xf ? 0x10 - $size & 0xf : 0; | ||
| 351 | $offset += $size + $entry->{padding}; | ||
| 352 | } | ||
| 353 | |||
| 354 | # Prepare indexes | ||
| 355 | my $start1 = $start + $offset; | ||
| 356 | my $index_offset = 0; | ||
| 357 | for my $hash ( @entries ) { | ||
| 358 | my $entry = $entries{$hash}; | ||
| 359 | |||
| 360 | my $part_offset = $index_offset / 4 + 1; | ||
| 361 | my $part_offset_low = $part_offset & 0x00ffff; | ||
| 362 | my $part_offset_high = ($part_offset & 0xff0000) >> 0x10; | ||
| 363 | my $usize_low = $entry->{usize} & 0x00ffff; | ||
| 364 | my $usize_high = ($entry->{usize} & 0xff0000) >> 0x10; | ||
| 365 | my $zsize_low = length $entry->{data} & 0x00ffff; | ||
| 366 | my $zsize_high = (length $entry->{data} & 0xff0000) >> 0x10; | ||
| 367 | |||
| 368 | $entry->{index1} = pack '(QLSS)<', | ||
| 369 | cityhash64_as_int $hash, $index_offset / 4, 1, $entry->{flags1}; | ||
| 370 | $entry->{index2} = pack '(SCC SCC SCC LL)<', | ||
| 371 | $part_offset_low, $part_offset_high, $entry->{kind}, | ||
| 372 | $zsize_low, $zsize_high, $entry->{compression}, | ||
| 373 | $usize_low, $usize_high, 0, | ||
| 374 | 0, ($start + $entry->{offset}) / 0x10; | ||
| 375 | |||
| 376 | $index_offset += length $entry->{index2}; | ||
| 377 | } | ||
| 378 | |||
| 379 | my $index1 = join '', map { $entries{$_}->{index1} } sort @entries; | ||
| 380 | my $index2 = join '', map { $entries{$_}->{index2} } @entries; | ||
| 381 | $index1 = _compress_zlib($index1); | ||
| 382 | $index2 = _compress_zlib($index2); | ||
| 383 | my $padding1 = length $index1 & 0xf ? 0x10 - length $index1 & 0xf : 0; | ||
| 384 | my $start2 = $start1 + (length $index1) + $padding1; | ||
| 385 | |||
| 386 | # Write file with header | ||
| 387 | my $header = pack 'A4 vv A4 V VVV (QQQ)<', | ||
| 388 | $MAGIC, 2, 0, 'CITY', (scalar @entries), | ||
| 389 | (length $index1), (@entries * 5), (length $index2), | ||
| 390 | $start1, $start2, 0; | ||
| 391 | |||
| 392 | my $fh = Path::Tiny->new($pathname)->openw_raw; | ||
| 393 | print $fh $header, "\0" x ($start - length $header); | ||
| 394 | print $fh $entries{$_}->{data}, "\0" x $entries{$_}->{padding} for @entries; | ||
| 395 | print $fh $index1, "\0" x $padding1; | ||
| 396 | print $fh $index2; | ||
| 397 | } | ||
| 398 | |||
| 399 | |||
| 400 | sub _compress_zlib ($data) { | ||
| 401 | state $zlib_d = do { | ||
| 402 | my %opts = ( -CRC32 => 1, -WindowBits => 15, -Level => 9, -AppendOutput => 1 ); | ||
| 403 | Compress::Raw::Zlib::Deflate->new( %opts ) or die | ||
| 404 | }; | ||
| 405 | |||
| 406 | $zlib_d->deflate( \($data), \(my $compressed = '') ); | ||
| 407 | $zlib_d->flush( \$compressed ); | ||
| 408 | $zlib_d->deflateReset; | ||
| 409 | return $compressed; | ||
| 410 | } | ||
| 411 | |||
| 412 | 1; | ||
| 413 | |||
| 414 | |||
| 415 | =head1 NAME | ||
| 416 | |||
| 417 | Archive::SCS::HashFS2 - SCS HashFS version 2 format handler | ||
| 418 | |||
| 419 | =head1 SYNOPSIS | ||
| 420 | |||
| 421 | my $path = Path::Tiny->new('.../base_share.scs'); | ||
| 422 | my $archive = Archive::SCS::HashFS2->new(path => $path)->mount; | ||
| 423 | |||
| 424 | $archive->read_dir_tree; | ||
| 425 | my @contents = sort $archive->list_dirs, $archive->list_files; | ||
| 426 | |||
| 427 | my $hash = Archive::SCS::CityHash::cityhash64('def/city.sii'); | ||
| 428 | my $data = $archive->read_entry($hash); | ||
| 429 | |||
| 430 | =head1 DESCRIPTION | ||
| 431 | |||
| 432 | Represents an SCS archive file encoded in HashFS version 2 | ||
| 433 | (1.50 and later). | ||
| 434 | |||
| 435 | Hash values used with this module must be in the internal format | ||
| 436 | (currently, a 16-byte hex scalar in network byte order). | ||
| 437 | |||
| 438 | =head1 METHODS | ||
| 439 | |||
| 440 | =head2 entries | ||
| 441 | |||
| 442 | $entry_hashes = $archive->entries; | ||
| 443 | |||
| 444 | =head2 is_mounted | ||
| 445 | |||
| 446 | $bool = $archive->is_mounted; | ||
| 447 | |||
| 448 | =head2 handles_path | ||
| 449 | |||
| 450 | $bool = Archive::SCS::HashFS2->handles_path($path_tiny, $header); | ||
| 451 | |||
| 452 | I<Since version 1.06.> | ||
| 453 | |||
| 454 | In version 1.05 and earlier, there was an equivalent C<handles_file($fh, $header)> | ||
| 455 | method, which is now deprecated. | ||
| 456 | |||
| 457 | =head2 list_dirs | ||
| 458 | |||
| 459 | @directories = $archive->list_dirs; | ||
| 460 | |||
| 461 | =head2 list_files | ||
| 462 | |||
| 463 | @files = $archive->list_files; | ||
| 464 | |||
| 465 | =head2 mount | ||
| 466 | |||
| 467 | $archive = $archive->mount; | ||
| 468 | |||
| 469 | =head2 new | ||
| 470 | |||
| 471 | $archive = Archive::SCS::HashFS2->new(path => $path_tiny); | ||
| 472 | |||
| 473 | =head2 path | ||
| 474 | |||
| 475 | $path_tiny = $archive->path; | ||
| 476 | |||
| 477 | I<Since version 1.06.> | ||
| 478 | |||
| 479 | In version 1.05 and earlier, there was an equivalent C<file()> | ||
| 480 | method, which is now deprecated. | ||
| 481 | |||
| 482 | =head2 read_dir_tree | ||
| 483 | |||
| 484 | $archive->read_dir_tree; | ||
| 485 | |||
| 486 | =head2 read_entry | ||
| 487 | |||
| 488 | $data = $archive->read_entry($hash); | ||
| 489 | |||
| 490 | =head2 unmount | ||
| 491 | |||
| 492 | $archive->unmount; | ||
| 493 | |||
| 494 | =head1 LIMITATIONS | ||
| 495 | |||
| 496 | Texture objects are currently unimplemented. | ||
| 497 | |||
| 498 | =head1 SEE ALSO | ||
| 499 | |||
| 500 | =over | ||
| 501 | |||
| 502 | =item * L<https://modding.scssoft.com/index.php?title=Documentation/Tools/Game_Archive_Packer&oldid=5546> | ||
| 503 | |||
| 504 | =back | ||
| 505 | |||
| 506 | =head1 AUTHOR | ||
| 507 | |||
| 508 | L<nautofon|https://github.com/nautofon> | ||
| 509 | |||
| 510 | =head1 COPYRIGHT | ||
| 511 | |||
| 512 | This software is copyright (c) 2026 by nautofon. | ||
| 513 | |||
| 514 | This is free software; you can redistribute it and/or modify it under | ||
| 515 | the same terms as the Perl 5 programming language system itself. |