|
import json |
|
import os |
|
import sys |
|
import zlib |
|
from pathlib import Path |
|
|
|
if len(sys.argv) < 2: |
|
print("Usage: python decompressor.py <Directory of MAP Data> (<optional output directory, default: same dir with map data>)") |
|
|
|
BASE_PATH = sys.argv[1] or "" |
|
|
|
SAVE_DIR = os.path.join(BASE_PATH, "uncompressed") |
|
|
|
if len(sys.argv) == 3: |
|
SAVE_DIR = sys.argv[2] |
|
|
|
COMPR_LIST = [] |
|
|
|
|
|
|
|
with open(os.path.join(BASE_PATH, "CMP.LST"), "rb") as f: |
|
string = "" |
|
data = f.read() |
|
count = 0 |
|
while count < len(data): |
|
byte = bytes([data[count]]) |
|
if byte == b'\x00': |
|
COMPR_LIST.append(string.replace("\\SystemSD\\", "").replace("\\", "/")) |
|
string = "" |
|
else: |
|
string += byte.decode("ascii") |
|
count += 1 |
|
|
|
if len(COMPR_LIST) == 0: |
|
COMPR_LIST = ["REFER001/"] |
|
|
|
if len(COMPR_LIST) == 1: |
|
dir = COMPR_LIST[0] |
|
|
|
COMPR_LIST = [] |
|
for itm in os.listdir(os.path.join(BASE_PATH, dir)): |
|
if itm.endswith(".ind") or itm.endswith(".cmp"): |
|
name = itm.replace(".ind", "").replace(".cmp", "") |
|
if name not in COMPR_LIST: |
|
COMPR_LIST.append(dir+name) |
|
|
|
Path(SAVE_DIR).mkdir(parents=True, exist_ok=True) |
|
|
|
for itm in COMPR_LIST: |
|
zlib_offsets = [] |
|
FILE_PATH = os.path.join(SAVE_DIR, itm) |
|
|
|
with open(os.path.join(BASE_PATH, f"{itm}.ind"), "rb") as f: |
|
index_data = f.read() |
|
uncmp_size = int.from_bytes(index_data[:4], "little") |
|
cmp_size = int.from_bytes(index_data[4:8], "little") |
|
blocks_size = int.from_bytes(index_data[8:9], "little") |
|
meta_data = index_data[9:0x10] |
|
|
|
offset = 0x10 |
|
while offset < len(index_data): |
|
zlib_offsets.append(int.from_bytes(index_data[offset:offset + 4], "little")) |
|
offset += 4 |
|
|
|
print(itm, "UNCOMP SIZE", uncmp_size, "COMP SIZE", cmp_size, "BLOCKS", blocks_size, meta_data,) |
|
|
|
|
|
Path(FILE_PATH).mkdir(parents=True, exist_ok=True) |
|
|
|
with open(os.path.join(FILE_PATH, "info.json"), "w") as f: |
|
json.dump({ |
|
"uncompressed_size": uncmp_size, |
|
"compressed_size": cmp_size, |
|
"blocks_count": blocks_size, |
|
"meta_data": meta_data.hex(), |
|
}, f) |
|
|
|
|
|
with open(os.path.join(BASE_PATH, f"{itm}.cmp"), "rb") as f: |
|
comp_data = f.read() |
|
for idx, block in enumerate(zlib_offsets): |
|
offset_dat = comp_data[block:] |
|
print(" >> Decompressing block", idx, end="") |
|
data = zlib.decompress(offset_dat) |
|
with open(os.path.join(FILE_PATH, f"data.bin"), "ab") as f: |
|
f.write(data) |
|
|
|
|