jp_text_parsing/extract_cards.py

157 lines
4.8 KiB
Python

import mmap
import struct
import json
from pathlib import Path
# Generate Hiragana and Katakana characters
hiragana = tuple(chr(i) for i in range(12353, 12436)) # \u3041 - \u3094
katakana = tuple(chr(i) for i in range(12450, 12532)) # \u30A2 - \u30F4
# Combine both into one tuple
all_kana = hiragana + katakana
# Print example
print(all_kana)
prop_data = Path(f"YGO_2020/bin/CARD_Prop.bin").read_bytes() # Get all the bytes
language_code = 'J'
offsets_data = Path(f"YGO_2020/bin/CARD_Indx_{language_code}.bin").read_bytes() # Get all the bytes
name_data = Path(f"YGO_2020/bin/CARD_Name_{language_code}.bin").read_bytes() # Get all the bytes
desc_data = Path(f"YGO_2020/bin/CARD_Desc_{language_code}.bin").read_bytes()
offsets = [x[0] for x in struct.iter_unpack("<I", offsets_data)] ## load all bytes into file.
#fruits = ("apple", "banana", "cherry")
#print("orange" not in fruits) # Output: True
# if key not in my_dict:
char_db = {}
words = {}
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):
name_start_off = offsets[off]
desc_start_off = offsets[off + 1]
#read until null found
end = name_start_off
while end + 1 < len(name_data):
## Read 2 bytes at a time until null found
if name_data[end:end+2] == b"\x00\x00":
break
end += 2
name = name_data[name_start_off:end].decode("utf-16le", errors="replace")
print(name)
# for c in range(name_start_off, end, 2):
# c_value = name_data[c:c+2]
# if char_db.get(c_value) is None:
# char_db[c_value] = 0
# print(c_value, c_value.decode("utf-16le", errors="replace"))
# char_db[c_value] = (char_db[c_value] + 1 )
for char in name:
if char_db.get(char) is None:
char_db[char] = 0
print(char)
char_db[char] = (char_db[char] + 1 )
word = ''
for char in name:
if char in all_kana:
if len(word) > 0:
if words.get(word) is None:
words[word] = 0
print(word)
words[word] = (words[word] + 1)
word = ''
else:
word += char
if len(word) > 0:
if words.get(word) is None:
words[word] = 0
print(word)
words[word] = (words[word] + 1)
end = desc_start_off
while end + 1 < len(desc_data):
## Read 2 bytes at a time until null found
if desc_data[end:end+2] == b"\x00\x00":
break
end += 2
desc = desc_data[desc_start_off:end].decode("utf-16le", errors="replace")
#print(name)
cd_db[cd_db_ctr] = {"nm":name, "de": desc, "nm_addr":hex(name_start_off), "de_addr":hex(desc_start_off)}
cd_db_ctr += 1
#print()
## Iterate offset
off += 2
language_code = 'E'
offsets_data = Path(f"YGO_2020/bin/CARD_Indx_{language_code}.bin").read_bytes() # Get all the bytes
name_data = Path(f"YGO_2020/bin/CARD_Name_{language_code}.bin").read_bytes() # Get all the bytes
desc_data = Path(f"YGO_2020/bin/CARD_Desc_{language_code}.bin").read_bytes()
offsets = [x[0] for x in struct.iter_unpack("<I", offsets_data)] ## load all bytes into file.
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):
name_start_off = offsets[off]
desc_start_off = offsets[off + 1]
#read until null found
end = name_start_off
while end + 1 < len(name_data):
## Read 2 bytes at a time until null found
if name_data[end:end+2] == b"\x00\x00":
break
end += 2
name = name_data[name_start_off:end].decode("utf-16le", errors="replace")
print(name)
end = desc_start_off
while end + 1 < len(desc_data):
## Read 2 bytes at a time until null found
if desc_data[end:end+2] == b"\x00\x00":
break
end += 2
desc = desc_data[desc_start_off:end].decode("utf-16le", errors="replace")
#print(name)
cd_db[cd_db_ctr]["nm_e"] = name
cd_db[cd_db_ctr]["de_e"] = desc
cd_db[cd_db_ctr]["nm_addr_e"] = hex(name_start_off)
cd_db[cd_db_ctr]["de_addr_e"] = hex(desc_start_off)
cd_db_ctr += 1
#print()
## Iterate offset
off += 2
# Writing to a file
with open("dump.json", "w", encoding="utf-16le") as outfile:
json.dump(cd_db, outfile, ensure_ascii=False, indent=2)
with open("words.json", "w", encoding="utf-16le") as outfile:
json.dump(words, outfile, ensure_ascii=False, indent=2)
with open("char_db.json", "w", encoding="utf-16le") as outfile:
json.dump(char_db, outfile, ensure_ascii=False, indent=2)
print("how do?")