From 9faa12e1f24dce85fe90404a6713be1c79119929 Mon Sep 17 00:00:00 2001 From: Fex Collects Date: Sun, 29 Dec 2024 18:00:21 +0000 Subject: [PATCH 1/2] clean: Update scripts to Python 3 --- battle-e/Makefile | 66 ++++++++++++++++++++-------------------- scripts/asmquote.py | 21 ++++++++++++- scripts/checksum.py | 14 ++++----- scripts/checksum_regi.py | 2 +- scripts/ereadertext.py | 6 ++-- scripts/gen3text.py | 41 +++++++++++++------------ scripts/regionalize.py | 8 ++--- scripts/stripgbc.py | 12 +++++--- 8 files changed, 96 insertions(+), 74 deletions(-) diff --git a/battle-e/Makefile b/battle-e/Makefile index f7e8067..25bacef 100644 --- a/battle-e/Makefile +++ b/battle-e/Makefile @@ -9,7 +9,7 @@ SERIES_2_BERRIES = ginema kuo yago touga niniku topo PROMO_EN_NUMS = B001 B002 PROMO_EN_TRAINERS = astrid nils P_PROMO_NUMS = P001 P002 P003 P004 P005 P006 P007 P008 -P_PROMO_TRAINERS = craig yasuo darlene mattego hidehiko yufu sagami mattegoDEMO +P_PROMO_TRAINERS = craig yasuo darlene mattego hidehiko yufu sagami mattegoDEMO N_PROMO_NUMS = N001 N002 N003 N004 N005 N006 N007 N008 N_PROMO_TRAINERS = teruko kimiko norton simon hozumi ritsue yuuma taisen @@ -26,127 +26,127 @@ verify: trainers/%-EN.tx: trainers/%.asm - python ../scripts/regionalize.py $< $@ EN EN + python3 ../scripts/regionalize.py $< $@ EN EN trainers/%.o: trainers/%.tx ./rgbasm.exe -o $@ $< trainers/%.gbc: trainers/%.o ./rgblink.exe -o $@ $< trainers/%.bin: trainers/%.gbc - python ../scripts/stripgbc.py $< $@ + python3 ../scripts/stripgbc.py $< $@ trainers/%.z80: trainers/%.gbc - python ../scripts/stripgbc.py $< $@ + python3 ../scripts/stripgbc.py $< $@ trainers/%.mev: trainers/%.bin - python ../scripts/checksum.py $< $@ + python3 ../scripts/checksum.py $< $@ berries/%-EN.tx: berries/%.asm - python ../scripts/regionalize.py $< $@ EN EN + python3 ../scripts/regionalize.py $< $@ EN EN berries/%.o: berries/%.tx ./rgbasm.exe -o $@ $< berries/%.gbc: berries/%.o ./rgblink.exe -o $@ $< berries/%.bin: berries/%.gbc - python ../scripts/stripgbc.py $< $@ + python3 ../scripts/stripgbc.py $< $@ berries/%.z80: berries/%.gbc - python ../scripts/stripgbc.py $< $@ + python3 ../scripts/stripgbc.py $< $@ berries/%.mev: berries/%.bin - python ../scripts/checksum.py $< $@ + python3 ../scripts/checksum.py $< $@ prologue-%.tx: prologue.asm - python ../scripts/regionalize.py $< $@ $* $* + python3 ../scripts/regionalize.py $< $@ $* $* prologue-%.o: prologue-%.tx ./rgbasm.exe -o $@ $< prologue-%.gbc: prologue-%.o ./rgblink.exe -o $@ $< prologue-%.bin: prologue-%.gbc - python ../scripts/stripgbc.py $< $@ + python3 ../scripts/stripgbc.py $< $@ battletrainer-%.tx: battletrainer.asm prologue-%.bin - python ../scripts/ereadertext.py $< $@ $* + python3 ../scripts/ereadertext.py $< $@ $* 08-A%-EN.tx: 08-A%.asm battletrainer-EN.tx - python ../scripts/ereadertext.py $< $@ EN + python3 ../scripts/ereadertext.py $< $@ EN 08-A%.o: 08-A%.tx ./rgbasm.exe -o $@ $< 08-A%.gbc: 08-A%.o ./rgblink.exe -o $@ $< 08-A%.z80: 08-A%.gbc - python ../scripts/stripgbc.py $< $@ + python3 ../scripts/stripgbc.py $< $@ 08-A%.vpk: 08-A%.z80 ./nevpk.exe -c -i $< -o $@ 08-A%.raw: 08-A%.vpk ./nedcmake.exe -i $< -o $@ -type 1 -region 1 08-B%-EN.tx: 08-B%.asm battletrainer-EN.tx - python ../scripts/ereadertext.py $< $@ EN + python3 ../scripts/ereadertext.py $< $@ EN 08-B%.o: 08-B%.tx ./rgbasm.exe -o $@ $< 08-B%.gbc: 08-B%.o ./rgblink.exe -o $@ $< 08-B%.z80: 08-B%.gbc - python ../scripts/stripgbc.py $< $@ + python3 ../scripts/stripgbc.py $< $@ 08-B%.vpk: 08-B%.z80 ./nevpk.exe -c -i $< -o $@ 08-B%.raw: 08-B%.vpk - ./nedcmake.exe -i $< -o $@ -type 1 -region 1 + ./nedcmake.exe -i $< -o $@ -type 1 -region 1 129-B%-EN.tx: 129-B%.asm battletrainer-EN.tx - python ../scripts/ereadertext.py $< $@ EN + python3 ../scripts/ereadertext.py $< $@ EN 129-B%.o: 129-B%.tx ./rgbasm.exe -o $@ $< 129-B%.gbc: 129-B%.o ./rgblink.exe -o $@ $< 129-B%.z80: 129-B%.gbc - python ../scripts/stripgbc.py $< $@ + python3 ../scripts/stripgbc.py $< $@ 129-B%.vpk: 129-B%.z80 ./nevpk.exe -c -i $< -o $@ 129-B%.raw: 129-B%.vpk ./nedcmake.exe -i $< -o $@ -type 1 -region 1 - + enigmaberry-%.tx: enigmaberry.asm prologue-%.bin - python ../scripts/ereadertext.py $< $@ $* + python3 ../scripts/ereadertext.py $< $@ $* 08-K%-EN.tx: 08-K%.asm enigmaberry-EN.tx - python ../scripts/ereadertext.py $< $@ EN + python3 ../scripts/ereadertext.py $< $@ EN 08-K%.o: 08-K%.tx ./rgbasm.exe -o $@ $< 08-K%.gbc: 08-K%.o ./rgblink.exe -o $@ $< 08-K%.z80: 08-K%.gbc - python ../scripts/stripgbc.py $< $@ + python3 ../scripts/stripgbc.py $< $@ 08-K%.vpk: 08-K%.z80 ./nevpk.exe -c -i $< -o $@ 08-K%.raw: 08-K%.vpk - ./nedcmake.exe -i $< -o $@ -type 1 -region 1 -raw - + ./nedcmake.exe -i $< -o $@ -type 1 -region 1 -raw + 08-N%-EN.tx: 08-N%.asm battletrainer-EN.tx - python ../scripts/ereadertext.py $< $@ EN + python3 ../scripts/ereadertext.py $< $@ EN 08-N%.o: 08-N%.tx ./rgbasm.exe -o $@ $< 08-N%.gbc: 08-N%.o ./rgblink.exe -o $@ $< 08-N%.z80: 08-N%.gbc - python ../scripts/stripgbc.py $< $@ + python3 ../scripts/stripgbc.py $< $@ 08-N%.vpk: 08-N%.z80 ./nevpk.exe -c -i $< -o $@ 08-N%.raw: 08-N%.vpk - ./nedcmake.exe -i $< -o $@ -type 1 -region 1 - + ./nedcmake.exe -i $< -o $@ -type 1 -region 1 + 08-P%-EN.tx: 08-P%.asm battletrainer-EN.tx - python ../scripts/ereadertext.py $< $@ EN + python3 ../scripts/ereadertext.py $< $@ EN 08-P%.o: 08-P%.tx ./rgbasm.exe -o $@ $< 08-P%.gbc: 08-P%.o ./rgblink.exe -o $@ $< 08-P%.z80: 08-P%.gbc - python ../scripts/stripgbc.py $< $@ + python3 ../scripts/stripgbc.py $< $@ 08-P%.vpk: 08-P%.z80 ./nevpk.exe -c -i $< -o $@ 08-P%.raw: 08-P%.vpk - ./nedcmake.exe -i $< -o $@ -type 1 -region 1 + ./nedcmake.exe -i $< -o $@ -type 1 -region 1 clean: rm -f *.z80 *.tx *.o *.gbc *.bin *.raw \ trainers/*.tx trainers/*.o trainers/*.gbc trainers/*.bin trainers/*.mev \ - berries/*.tx berries/*.o berries/*.gbc berries/*.bin berries/*.mev \ No newline at end of file + berries/*.tx berries/*.o berries/*.gbc berries/*.bin berries/*.mev diff --git a/scripts/asmquote.py b/scripts/asmquote.py index b0ccc71..21be843 100644 --- a/scripts/asmquote.py +++ b/scripts/asmquote.py @@ -25,4 +25,23 @@ def asmQuote(t): t = t[1:] if quoted: result += '"' - return result \ No newline at end of file + return result + +def asmQuoteBytes(t): + og_q = asmQuote(t) + og_b = "" + for b in og_q.replace('",0,"', '\x00').replace('"', '').replace(',0', '\x00'): + og_b += hex(ord(b)) + + result = "" + test = "" + for b in t: + result += '{0}'.format(ord(b)) + "," + test += hex(ord(b)) + + result = result[:-1] + + if og_b != test: + print("WARN:", og_q, og_b, test) + + return result diff --git a/scripts/checksum.py b/scripts/checksum.py index 1b78001..6f95cfc 100644 --- a/scripts/checksum.py +++ b/scripts/checksum.py @@ -11,7 +11,7 @@ wordwise_results = [] crcs = [] crc_results = [] -data = "" +data = None with open(sys.argv[1], 'rb') as f: data = f.read() f.closed @@ -19,7 +19,7 @@ f.closed base_address = struct.unpack(' 0x10: - print "Unknown chunk {0:X}".format(chunk_type) + print("Unknown chunk {0:X}".format(chunk_type)) raise TypeError i += chunk_lengths[chunk_type] @@ -58,7 +58,7 @@ for wordwise in wordwises: for bytewise in bytewises: sum = 0 for i in range(bytewise[1], bytewise[2]): - sum = (sum + ord(data[i])) & 0xFFFFFFFF + sum = (sum + data[i]) & 0xFFFFFFFF bytewise_results.append(sum) i = 0 for bytewise in bytewises: @@ -70,7 +70,7 @@ for bytewise in bytewises: for crc in crcs: sum = 0x1121 for i in range(crc[1], crc[2]): - sum ^= ord(data[i]) + sum ^= data[i] for j in range(8): if(sum & 1): sum = (sum >> 1) ^ 0x8408 @@ -86,5 +86,5 @@ for crc in crcs: # write the updated file -out = open(sys.argv[2], 'w') -out.write(data) \ No newline at end of file +out = open(sys.argv[2], 'wb') +out.write(data) diff --git a/scripts/checksum_regi.py b/scripts/checksum_regi.py index a88e51d..ff5a417 100644 --- a/scripts/checksum_regi.py +++ b/scripts/checksum_regi.py @@ -52,7 +52,7 @@ for i in pos: start_address = struct.unpack(' 0x11: - print "Unknown chunk {0:X}".format(chunk_type) + print("Unknown chunk {0:X}".format(chunk_type)) raise TypeError i += chunk_lengths[chunk_type] diff --git a/scripts/ereadertext.py b/scripts/ereadertext.py index 0e834fe..a5fcd79 100644 --- a/scripts/ereadertext.py +++ b/scripts/ereadertext.py @@ -7,7 +7,7 @@ region = sys.argv[3] out = open(sys.argv[2], 'w') -with open(sys.argv[1], 'rb') as f: +with open(sys.argv[1], 'r') as f: for asm in f: asms = asm.split('"') command = asms[0].strip() @@ -17,11 +17,11 @@ with open(sys.argv[1], 'rb') as f: asms[1] = asms[1].replace('\\n', '\n') asms[1] = asms[1].replace('é', '\x7F') - out.write("db " + asmQuote(asms[1]) + "\n") + out.write("\tdb " + asmQuote(asms[1]) + "\n") else: out.write(asm) if "macros.asm" in asm: out.write("REGION EQU REGION_{0}\n".format(region)) out.write('REGION_NAME EQUS "{0}"\n'.format(region)) -f.closed \ No newline at end of file +f.closed diff --git a/scripts/gen3text.py b/scripts/gen3text.py index 5c6ee1c..227e950 100644 --- a/scripts/gen3text.py +++ b/scripts/gen3text.py @@ -431,27 +431,28 @@ def utf8ToRSText(t, region = ""): if region == "DE": chars['“'] = '\xB2' - characters = [] - char = "" - while len(t): - if ord(t[0]) >= 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 = "" +# characters = [] +# char = "" +# while len(t): +# if ord(t[0]) >= 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: +# for char in characters: + for char in t: result += chars[char] return result @@ -479,4 +480,4 @@ def asmQuote(t): t = t[1:] if quoted: result += '"' - return result \ No newline at end of file + return result diff --git a/scripts/regionalize.py b/scripts/regionalize.py index c178d06..d49ce6c 100644 --- a/scripts/regionalize.py +++ b/scripts/regionalize.py @@ -1,14 +1,14 @@ # -*- coding: utf-8 -*- import sys from gen3text import utf8ToRSText -from asmquote import asmQuote +from asmquote import asmQuoteBytes 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: +with open(sys.argv[1], 'r') as f: for asm in f: asms = asm.split('"') command = asms[0].strip() @@ -23,11 +23,11 @@ with open(sys.argv[1], 'rb') as f: asms[1] += "\x00" except ValueError: pass - out.write("db " + asmQuote(asms[1]) + "\n") + out.write("\tdb " + asmQuoteBytes(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 +f.closed diff --git a/scripts/stripgbc.py b/scripts/stripgbc.py index c562a55..8fa1f36 100644 --- a/scripts/stripgbc.py +++ b/scripts/stripgbc.py @@ -2,9 +2,9 @@ #!/usr/bin/env python2.7 import sys -out = open(sys.argv[2], 'w') +out = open(sys.argv[2], 'wb') buffering = False -buf = "" +buf = bytes() with open(sys.argv[1], 'rb') as f: f.read(256) # skip to $0100 while True: @@ -14,17 +14,19 @@ with open(sys.argv[1], 'rb') as f: # the program shall end with $FF followed only by $00 bytes # for every $FF we hit, buffer until something that isn’t $00 - if (not buffering and ord(byte) == 0xFF) or (buffering and ord(byte) == 0x00): + if not buffering and ord(byte) == 0xFF: buf += byte buffering = True + elif buffering and ord(byte) == 0x00: + buf += byte elif buffering and ord(byte) == 0xFF: out.write(buf) buf = byte elif buffering: out.write(buf) out.write(byte) - buf = "" + buf = bytes() buffering = False else: out.write(byte) -f.closed \ No newline at end of file +f.closed From 2a293076bec19d813c59057bc54f441e4e73e782 Mon Sep 17 00:00:00 2001 From: Fex Collects Date: Sun, 29 Dec 2024 18:33:05 +0000 Subject: [PATCH 2/2] address comments --- scripts/asmquote.py | 10 ---------- scripts/checksum.py | 1 - scripts/gen3text.py | 20 -------------------- scripts/stripgbc.py | 4 +--- 4 files changed, 1 insertion(+), 34 deletions(-) diff --git a/scripts/asmquote.py b/scripts/asmquote.py index 21be843..15c9bbc 100644 --- a/scripts/asmquote.py +++ b/scripts/asmquote.py @@ -28,20 +28,10 @@ def asmQuote(t): return result def asmQuoteBytes(t): - og_q = asmQuote(t) - og_b = "" - for b in og_q.replace('",0,"', '\x00').replace('"', '').replace(',0', '\x00'): - og_b += hex(ord(b)) - result = "" - test = "" for b in t: result += '{0}'.format(ord(b)) + "," - test += hex(ord(b)) result = result[:-1] - if og_b != test: - print("WARN:", og_q, og_b, test) - return result diff --git a/scripts/checksum.py b/scripts/checksum.py index 6f95cfc..234390c 100644 --- a/scripts/checksum.py +++ b/scripts/checksum.py @@ -11,7 +11,6 @@ wordwise_results = [] crcs = [] crc_results = [] -data = None with open(sys.argv[1], 'rb') as f: data = f.read() f.closed diff --git a/scripts/gen3text.py b/scripts/gen3text.py index 227e950..e9b0ae1 100644 --- a/scripts/gen3text.py +++ b/scripts/gen3text.py @@ -431,27 +431,7 @@ def utf8ToRSText(t, region = ""): if region == "DE": chars['“'] = '\xB2' -# characters = [] -# char = "" -# while len(t): -# if ord(t[0]) >= 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: for char in t: result += chars[char] return result diff --git a/scripts/stripgbc.py b/scripts/stripgbc.py index 8fa1f36..9e8dc00 100644 --- a/scripts/stripgbc.py +++ b/scripts/stripgbc.py @@ -14,11 +14,9 @@ with open(sys.argv[1], 'rb') as f: # the program shall end with $FF followed only by $00 bytes # for every $FF we hit, buffer until something that isn’t $00 - if not buffering and ord(byte) == 0xFF: + if (not buffering and ord(byte) == 0xFF) or (buffering and ord(byte) == 0x00): buf += byte buffering = True - elif buffering and ord(byte) == 0x00: - buf += byte elif buffering and ord(byte) == 0xFF: out.write(buf) buf = byte