Created
November 11, 2023 22:46
-
-
Save Puyodead1/cac741e9fd35f0593bd234adf381d41f to your computer and use it in GitHub Desktop.
FTL.dat unpacker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pathlib import Path | |
from typing import List | |
from binreader import BinaryReader | |
class PkgEntry: | |
path_hash: int | |
path_offset: int | |
data_deflated: bool | |
data_offset: int | |
data_size: int | |
data_unpacked_size: int | |
def __str__(self): | |
return f"hash: {self.path_hash}; offset: {self.path_offset}; deflated: {self.data_deflated}; data_offset: {self.data_offset}; data_size: {self.data_size}; data_unpacked_size: {self.data_unpacked_size}" | |
PKG_MAGIC = b"PKG\n" | |
ENTRIES: List[PkgEntry] = [] | |
with open("ftl.dat", "rb") as f: | |
reader = BinaryReader(f, ">") | |
magic = reader.read(4) | |
assert magic == PKG_MAGIC | |
header_size = reader.read_uint16() | |
assert header_size == 0x10, "Invalid Header Size" | |
entry_size = reader.read_uint16() | |
assert entry_size == 0x20, "Invalid Entry Size" | |
entry_count = reader.read_uint32() | |
paths_region_size = reader.read_uint32() | |
i = 0 | |
while i < entry_count: | |
entry = PkgEntry() | |
entry.path_hash = reader.read_uint32() | |
path_offset_and_flags = reader.read_uint32() | |
entry.path_offset = path_offset_and_flags & 0x00FFFFFF | |
entry.data_deflated = (path_offset_and_flags & (1 << 24)) != 0 | |
entry.data_offset = reader.read_uint32() | |
entry.data_size = reader.read_uint32() | |
entry.data_unpacked_size = reader.read_uint32() | |
if entry.data_size == 0: | |
ENTRIES.append(None) | |
break | |
else: | |
ENTRIES.append(entry) | |
i += 1 | |
path_region = reader.read(paths_region_size) | |
for entry in ENTRIES: | |
path = "" | |
while True: | |
c = path_region[entry.path_offset] | |
entry.path_offset += 1 | |
if c == 0: | |
break | |
path += chr(c) | |
print(f"Processing {path}") | |
reader.seek(entry.data_offset) | |
data = reader.read(entry.data_size) | |
path = Path(path) | |
path.parent.mkdir(parents=True, exist_ok=True) | |
with open(path, "wb") as f: | |
f.write(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment