83 lines
2.5 KiB
Python
83 lines
2.5 KiB
Python
import mmap
|
||
import struct
|
||
from pathlib import Path
|
||
|
||
def iter_offsets(offset_file: str):
|
||
skip_header = False
|
||
with open(offset_file, "rb") as f:
|
||
if skip_header == False:
|
||
print(f.read(8))
|
||
skip_header = True
|
||
while chunk := f.read(4):
|
||
if len(chunk) != 4:
|
||
raise ValueError("Incomplete 4-byte offset at end of file")
|
||
#yield struct.unpact("<I", chunk)[0]
|
||
print(chunk)
|
||
|
||
# with open("YGO_2020/CARD_Name_E.bin", "rb") as tf:
|
||
# with mmap.mmap(tf.fileno(), 0, access=mmap.ACCESS_READ) as mm:
|
||
# for off in iter_offsets("YGO_2020\CARD_Indx_E.bin"):
|
||
# print(off)
|
||
|
||
## Test iter_offsets
|
||
#iter_offsets("YGO_2020/CARD_Indx_E.bin")
|
||
|
||
chr(65322) # int to char
|
||
ord('L') # char to unicode val
|
||
|
||
|
||
# offsets_data = Path("YGO_2020/CARD_Indx_E.bin").read_bytes()
|
||
# text_data = Path("YGO_2020/CARD_Name_E.bin").read_bytes()
|
||
|
||
offsets_data = Path("YGO_2020/CARD_Indx_J.bin").read_bytes()
|
||
text_data = Path("YGO_2020/CARD_Name_J.bin").read_bytes()
|
||
|
||
|
||
offsets = [x[0] for x in struct.iter_unpack("<I", offsets_data)]
|
||
## Getting ranges of offsets
|
||
offsets[:2] # first 2 bytes
|
||
offsets[4:8] # 8 bytes from the 4th
|
||
|
||
## Printing values for debugging
|
||
print(f"Foo {[f'0x{x:x}' for x in offsets[2:4]]}") # Getting range and convert to hex
|
||
print(", ".join("{:04x}".format(x) for x in offsets[6:8]))
|
||
|
||
# for off in offsets[:10]:
|
||
# print(f"offset={off} hex{off:08X}")
|
||
# print(text_data[off:off+32].hex(" "))
|
||
# print()
|
||
|
||
cd_db = {}
|
||
cd_db_ctr = 3900
|
||
starting_offset = 2
|
||
off = starting_offset
|
||
## Read two bytes at a time
|
||
#while off+2 < len(offsets_data): ## didnt work
|
||
#while offsets[off+2] and off < 10:
|
||
while off+2 < len(offsets):
|
||
print("Offset Pair:", ", ".join("{:04x}".format(x) for x in offsets[off:off+2]))
|
||
name_start_off = offsets[off]
|
||
## Peek method
|
||
# name_end_off = offsets[off+2] # peek to next offset
|
||
# print((text_data[name_start_off:(name_end_off-1)]).hex(" "))
|
||
# print((text_data[name_start_off:(name_end_off-1)]).decode("utf-16le", errors="replace"))
|
||
# print()
|
||
|
||
#read until null found
|
||
end = name_start_off
|
||
while end + 1 < len(text_data):
|
||
## Read 2 bytes at a time until null found
|
||
if text_data[end:end+2] == b"\x00\x00":
|
||
break
|
||
end += 2
|
||
text = text_data[name_start_off:end].decode("utf-16le", errors="replace")
|
||
print(text)
|
||
cd_db[cd_db_ctr] = {"nm":text, "addr":hex(name_start_off)}
|
||
cd_db_ctr += 1
|
||
print()
|
||
## Iterate offset
|
||
off += 2
|
||
|
||
print("how do?")
|
||
|