"""Read-only PBZ IoStore and localization metadata extraction. The IoStore field layout follows Epic's public format as implemented in retoc. This module deliberately does not parse the game's custom ContainerHeader. It reads the indexed file/chunk and compression block tables directly. """ from __future__ import annotations import ctypes import hashlib import struct from pathlib import Path from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes PAK_KEY = bytes.fromhex('4BC8A51E5381325073F2BC7D4E3089CD557C4E9B7D453901C2C795518867E01D') class Reader: def __init__(self, data: bytes, offset: int = 0): self.data, self.p = data, offset def take(self, size: int) -> bytes: if size < 0 or self.p + size > len(self.data): raise ValueError('Truncated resource') result = self.data[self.p:self.p+size]; self.p += size; return result def u32(self): return struct.unpack(' 16_000_000: raise ValueError('Invalid FString length') if length == 0: return '' return self.take(abs(length)*(2 if length < 0 else 1)).decode('utf-16le' if length < 0 else 'utf8').rstrip('\0') def aes_decrypt(data, key=PAK_KEY): if len(data) % 16: raise ValueError('AES block alignment') dec = Cipher(algorithms.AES(key), modes.ECB()).decryptor() return dec.update(data)+dec.finalize() class OodleReader: """Use a caller-selected installed codec. No proprietary library is shipped.""" def __init__(self, dll_path): self.dll = ctypes.WinDLL(str(Path(dll_path).resolve())) self.fn = self.dll.OodleLZ_Decompress self.fn.restype = ctypes.c_ssize_t self.fn.argtypes = [ctypes.c_void_p,ctypes.c_ssize_t,ctypes.c_void_p,ctypes.c_ssize_t,ctypes.c_int,ctypes.c_int,ctypes.c_int,ctypes.c_void_p,ctypes.c_ssize_t,ctypes.c_void_p,ctypes.c_void_p,ctypes.c_void_p,ctypes.c_ssize_t,ctypes.c_int] def decompress(self, data, size): out = ctypes.create_string_buffer(size) n = self.fn(data,len(data),out,size,1,0,0,None,0,None,None,None,0,3) if n != size: raise ValueError(f'Oodle decompression failed: {n} != {size}') return out.raw class IoStore: def __init__(self, utoc_path, codec): self.path, self.codec = Path(utoc_path), codec self.data = b = self.path.read_bytes() if b[:16] != b'-==--==--==--==-': raise ValueError('Unknown IoStore magic') self.version = b[16] vals = struct.unpack_from('<9I', b,20) header_size, self.count, block_count, block_entry_size, method_count, method_length, self.block_size, dir_size, self.partitions = vals if header_size != 144 or block_entry_size != 12: raise ValueError('Unsupported IoStore header') self.flags = b[80] seeds = struct.unpack_from('H',data,headers+i*2)[0] size=(header&0x7fff)*(2 if header&0x8000 else 1) names.append(data[pos:pos+size].decode('utf-16le' if header&0x8000 else 'utf8')) pos+=size if pos-start != string_length: raise ValueError('Name batch size mismatch') return names,pos