From 319ed6d0e0266f5f6174a5acd929914db8cfae1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A1=C4=8Dky?= Date: Thu, 27 Nov 2014 07:15:45 +0000 Subject: Code cleanup; preparation for Japanese Battle e --- scripts/asmquote.py | 27 +++ scripts/berrychecksum.py | 16 -- scripts/charmap.py | 504 --------------------------------------------- scripts/checksum.py | 89 ++++++++ scripts/ereadertext.py | 26 +++ scripts/gen3text.py | 481 ++++++++++++++++++++++++++++++++++++++++++ scripts/regionalize.py | 33 +++ scripts/scriptchecksum.py | 28 --- scripts/stripgbc.py | 29 +++ scripts/trainerchecksum.py | 13 -- 10 files changed, 685 insertions(+), 561 deletions(-) create mode 100644 scripts/asmquote.py delete mode 100644 scripts/berrychecksum.py delete mode 100644 scripts/charmap.py create mode 100644 scripts/checksum.py create mode 100644 scripts/ereadertext.py create mode 100644 scripts/gen3text.py create mode 100644 scripts/regionalize.py delete mode 100644 scripts/scriptchecksum.py create mode 100644 scripts/stripgbc.py delete mode 100644 scripts/trainerchecksum.py (limited to 'scripts') diff --git a/scripts/asmquote.py b/scripts/asmquote.py new file mode 100644 index 0000000..cfc1440 --- /dev/null +++ b/scripts/asmquote.py @@ -0,0 +1,27 @@ +asmProblemBytes = ['\x00', '\x09', '\x0A', '\x22'] +def asmQuote(t): + result = "" + quoted = False + if t[0] in asmProblemBytes: + result = '{0}'.format(ord(t[0])) + else: + result = '"' + t[0] + quoted = True + t = t[1:] + + while len(t): + if quoted and t[0] in asmProblemBytes: + result += '",{0}'.format(ord(t[0])) + quoted = False + elif quoted: + result += t[0] + elif t[0] in asmProblemBytes: + result += ',{0}'.format(ord(t[0])) + quoted = False + else: + result += ',"' + t[0] + quoted = True + t = t[1:] + if quoted: + result += '"' + return result \ No newline at end of file diff --git a/scripts/berrychecksum.py b/scripts/berrychecksum.py deleted file mode 100644 index a973dc2..0000000 --- a/scripts/berrychecksum.py +++ /dev/null @@ -1,16 +0,0 @@ -import struct -import sys - -out = open(sys.argv[2], 'w') -sum = 0 -with open(sys.argv[1], 'rb') as f: - while True: - byte = f.read(1) - if not byte: - break - - sum += ord(byte) - out.write(byte) -f.closed - -out.write(struct.pack(' 0: - output += ",$FF" - for i in range(len(characters) + 1, pad_length): - output += ",$00" - - else: - output += token - even = not even - - else: - asm = asm.replace("\\0", "\",$00,\"") - asm = asm.replace("é", "\x7F") - output = asm - - out.write(output) -f.closed diff --git a/scripts/checksum.py b/scripts/checksum.py new file mode 100644 index 0000000..8b5f63c --- /dev/null +++ b/scripts/checksum.py @@ -0,0 +1,89 @@ +import struct +import sys + +chunk_lengths = [0,0,0,6,2,5,12,5,3,1,2,5,5,5,1,13,13] + +bytewises = [] +bytewise_results = [] +wordwises = [] +wordwise_results = [] +crcs = [] +crc_results = [] + +data = "" +with open(sys.argv[1], 'rb') as f: + data = f.read() +f.closed + +base_address = struct.unpack(' 0x10: + print "Unknown chunk {0:X}".format(chunk_type) + raise TypeError + i += chunk_lengths[chunk_type] + + +# calculate and insert all wordwise checksums +for wordwise in wordwises: + sum = 0 + for i in range(wordwise[1], wordwise[2], 4): + sum = (sum + struct.unpack('> 1) ^ 0x8408 + else: + sum >>= 1 + sum = ~sum & 0xFFFF + crc_results.append(sum) + +i = 0 +for crc in crcs: + data = data[0:crc[0]] + struct.pack('= 0xF0: + char += t[0:4] + t = t[4:] + elif ord(t[0]) >= 0xE0: + char += t[0:3] + t = t[3:] + elif ord(t[0]) >= 0xC0: + char += t[0:2] + t = t[2:] + else: + char += t[0:1] + t = t[1:] + if char != "\\" and char != "\\v" and (char[0:2] != "\\{" or char[-1] == "}"): + characters.append(char) + char = "" + + result = "" + for char in characters: + result += chars[char] + return result + +def asmQuote(t): + result = "" + quoted = False + if t[0] in asmProblemBytes: + result = '{0}'.format(ord(t[0])) + else: + result = '"' + t[0] + quoted = True + + while len(t): + if quoted and t[0] in asmProblemBytes: + result += '",{0}'.format(ord(t[0])) + quoted = False + elif quoted: + result += t[0] + elif t[0] in asmProblemBytes: + result += ',{0}'.format(ord(t[0])) + quoted = False + else: + result += ',"' + t[0] + quoted = True + t = t[1:] + if quoted: + result += '"' + return result \ No newline at end of file diff --git a/scripts/regionalize.py b/scripts/regionalize.py new file mode 100644 index 0000000..c178d06 --- /dev/null +++ b/scripts/regionalize.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- +import sys +from gen3text import utf8ToRSText +from asmquote import asmQuote + +data_region = sys.argv[3] # determines region code +text_region = sys.argv[4] # determines string translation + +out = open(sys.argv[2], 'w') + +with open(sys.argv[1], 'rb') as f: + for asm in f: + asms = asm.split('"') + command = asms[0].strip() + if (command == "Text_" + text_region) or (command == "Text"): + asms[1] = utf8ToRSText(asms[1], text_region) + try: + length = asms[2].split(';')[0] # strip trailing comment + padding = int(length) - len(asms[1]) + if padding > 0: + asms[1] += '\xFF' + for i in range(padding - 1): + asms[1] += "\x00" + except ValueError: + pass + out.write("db " + asmQuote(asms[1]) + "\n") + elif len(command) < 5 or command[0:5] != "Text_": + out.write(asm) + if "macros.asm" in asm: + # can’t do this until after REGION_EN, etc. are loaded + out.write("REGION EQU REGION_" + data_region + "\n") + # else this is foreign text, delete it +f.closed \ No newline at end of file diff --git a/scripts/scriptchecksum.py b/scripts/scriptchecksum.py deleted file mode 100644 index d40a12d..0000000 --- a/scripts/scriptchecksum.py +++ /dev/null @@ -1,28 +0,0 @@ -import struct -import sys - -out = open(sys.argv[2], 'w') -sum = 0x1121 -len = 0 -with open(sys.argv[1], 'rb') as f: - while True: - byte = f.read(1) - if not byte: - break - - sum ^= ord(byte) - for i in range(8): - if(sum & 1): - sum = (sum >> 1) ^ 0x8408 - else: - sum >>= 1 - len += 1 - sum = ~sum & 0xFFFF -f.closed - -out.write(struct.pack('