gck-map-extract-objects/ps2_read.py

174 lines
5.5 KiB
Python

import argparse
import os.path
from lib.fileutils import *
from lib.packet import Packet
def ftell(fp):
return hex(fp.tell())
def read_bin(_input: str, _output: str):
with open(_input, "rb") as fp:
# read 0x30 bytes
fileversion = read_int(fp)
render_data = read_int(fp)
texturesmemory_size = read_int(fp)
unk2_size = read_int(fp)
objects_size = read_int(fp)
some_bytes = read_int(fp)
fx_size = read_int(fp)
world_size = read_int(fp)
huds_size = read_int(fp)
worldflags_size = read_int(fp)
unk = read_int(fp)
unk = read_int(fp)
print("[%s] Game is %s bytes" % (ftell(fp), render_data))
read_bytes(fp, render_data)
print("[%s] World is %s bytes..." % (ftell(fp), world_size))
read_bytes(fp, world_size)
print("[%s] Texturesmemory is %s bytes..." % (ftell(fp), texturesmemory_size))
texturesmemory = read_bytes(fp, texturesmemory_size)
print("[%s] FX %s bytes..." % (ftell(fp), fx_size))
read_bytes(fp, fx_size)
print("[%s] unk2 %s bytes..." % (ftell(fp), unk2_size))
read_bytes(fp, unk2_size)
print("[%s] worldflags %s bytes..." % (ftell(fp), worldflags_size))
read_bytes(fp, worldflags_size)
print("[%s] huds %s bytes..." % (ftell(fp), huds_size))
read_bytes(fp, huds_size)
print("[%s] objects %s bytes..." % (ftell(fp), objects_size))
objects = read_bytes(fp, objects_size)
print("[%s] textures_attributes? %s bytes..." % (ftell(fp), some_bytes))
# textures_attributes = read_bytes(fp, some_bytes)
first_int = read_int(fp)
print("[%s] First int: %s" % (ftell(fp), first_int))
second_int = read_int(fp)
print("Second int: %s" % second_int)
with open(_output, "wb") as fp:
fp.write(texturesmemory)
print("Done")
def main():
parser = argparse.ArgumentParser()
parser.add_argument("input_bin", help="PS2 bin file to read")
parser.add_argument("output_file", help="output bin for objects")
args = parser.parse_args()
read_bin(args.input_bin, args.output_file)
def read_textures(file_input):
print("Reading %s" % file_input)
curr = 0
with open(file_input, "rb") as fp:
content = fp.read()
while curr < len(content):
byte_index = content.find(b"END-OF-FIL", curr)
if byte_index != -1:
byte_index += 0xc
print("content: %s (%s)" % (content[byte_index], hex(content[byte_index])))
while content[byte_index] == 0x2a or content[byte_index] == 0x00:
byte_index += 1
print("found texture at %s: %s %s" % (hex(byte_index), hex(content[byte_index]), hex(content[byte_index+1])))
# after CLUTS
byte_index += 0x20
p = Packet(content)
p.seek(byte_index)
# psm = p.get_short()
psm = content[byte_index]
psm = psm << 8 & 0x3f
d_comp = p.get_long()
d_comp = p.get_long()
is_compressed = d_comp >> 0x12 & 0xf
compare = d_comp >> 0xe & 0xf
if is_compressed != compare:
print(hex(d_comp))
new_compare = compare - is_compressed
if new_compare <= 0:
print("ERROOOOOOOOOR")
if new_compare == 1:
decomp_bytes = 2
elif new_compare == 2:
decomp_bytes = 3
elif new_compare == 3:
decomp_bytes = 4
else:
print("ERROOOOOOOOOOR: %s" % new_compare)
print("decomp_bytes: %s" % decomp_bytes)
print("psm: %s -> %s (compressed: %s, compare: %s)" % (psm, get_pixel_format(psm), is_compressed, compare))
curr = byte_index+1
else:
curr = len(content)
def get_pixel_format(psm) -> str:
_data = {
0: "PSMCT32",
1: "PSMCT24",
2: "PSMCT16",
10: "PSMCT16S",
19: "PSMT8",
20: "PSMT4",
27: "PSMT8H",
26: "PSMT4HL",
44: "PSMT4HH",
48: "PSMZ32",
49: "PSMZ24",
50: "PSMZ16",
58: "PSMZ16S",
}
return _data.get(psm, "UNKNOWN")
def read_sizes():
print("=====================================================================================")
print("=====================================================================================")
print("=====================================================================================")
print("=====================================================================================")
print("=====================================================================================")
print("=====================================================================================")
print("=====================================================================================")
parser = argparse.ArgumentParser()
parser.add_argument("input_bin", help="PS2 bin file to read")
args = parser.parse_args()
all_num = 0
if os.path.isdir(args.input_bin):
for currdir, dirs, files in os.walk(args.input_bin):
for file in files:
if file.lower().endswith(".bin"):
read_textures(currdir+"/"+file)
else:
read_textures(args.input_bin)
print("textures:%s" % all_num)
if __name__ == '__main__':
# main()
read_sizes()