From 1920c4caae9afa3f6cdfbadb4fcd711df7c3a3e0 Mon Sep 17 00:00:00 2001 From: Stewie Date: Sat, 16 Jul 2022 15:11:15 -0400 Subject: [PATCH] Move functions to TOR instead of parent class --- ToolsTOR.py | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/ToolsTOR.py b/ToolsTOR.py index 69b2b4b..fcae423 100644 --- a/ToolsTOR.py +++ b/ToolsTOR.py @@ -37,6 +37,21 @@ class ToolsTOR(ToolsTales): super().__init__("TOR", tbl, "Tales-of-Rebirth") + with open("../{}/Data/Misc/{}".format(self.repo_name, self.tblFile), encoding="utf-8") as f: + + jsonRaw = json.load(f) + self.jsonTblTags ={ k1:{ int(k2,16) if (k1 != "TBL") else k2:v2 for k2,v2 in jsonRaw[k1].items()} for k1,v1 in jsonRaw.items()} + + + + self.itable = dict([[i, struct.pack(">H", int(j))] for j, i in self.jsonTblTags['TBL'].items()]) + self.itags = dict([[i, j] for j, i in self.jsonTblTags['TAGS'].items()]) + if "NAME" in self.jsonTblTags.keys(): + self.inames = dict([[i, j] for j, i in self.jsonTblTags['NAME'].items()]) + + if "COLOR" in self.jsonTblTags.keys(): + self.icolors = dict([[i, j] for j, i in self.jsonTblTags['COLOR'].items()]) + self.id = 1 #byteCode self.story_byte_code = b"\xF8" @@ -146,7 +161,104 @@ class ToolsTOR(ToolsTales): with open(os.path.join( self.story_XML_patch,"XML", self.get_file_name(scpk_file_name)+".xml"), "wb") as xmlFile: xmlFile.write(txt) + + #Convert a bytes object to text using TAGS and TBL in the json file + def bytes_to_text(self, fileRead, offset=-1, end_strings = b"\x00"): + + finalText = '' + TAGS = self.jsonTblTags['TAGS'] + if (offset > 0): + fileRead.seek(offset, 0) + + pos = fileRead.tell() + b = fileRead.read(1) + while b != end_strings: + #print(hex(fileRead.tell())) + b = ord(b) + + if (b >= 0x99 and b <= 0x9F) or (b >= 0xE0 and b <= 0xEB): + c = (b << 8) + ord(fileRead.read(1)) + + # if str(c) not in json_data.keys(): + # json_data[str(c)] = char_index[decode(c)] + try: + finalText += (self.jsonTblTags['TBL'][str(c)]) + except KeyError: + b_u = (c >> 8) & 0xff + b_l = c & 0xff + finalText += ("{%02X}" % b_u) + finalText += ("{%02X}" % b_l) + elif b == 0x1: + finalText += ("\n") + elif b in (0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xB, 0xC, 0xD, 0xE, 0xF): + b2 = struct.unpack("" % (tag_name, b2)) + else: + finalText += "<%02X:%08X>" % (b, b2) + elif chr(b) in self.PRINTABLE_CHARS: + finalText += chr(b) + elif b >= 0xA1 and b < 0xE0: + finalText += struct.pack("B", b).decode("cp932") + elif b in (0x13, 0x17, 0x1A): + tag_name = f"Unk{b:02X}" + hex_value = "" + + while fileRead.read(1) != b"\x80": + fileRead.seek(fileRead.tell()-1) + mark = fileRead.read(1) + hex_value += mark.hex() + if mark == "\x38": + hex_value += f"{struct.unpack('