1
0
mirror of https://github.com/zeldaret/oot.git synced 2024-09-21 04:24:43 +00:00

Make text headers encoding more robust (#1145)

* Improve encoding stage of text headers

* Fix r string

* Remove another unnecessary backslash
This commit is contained in:
Tharo 2022-02-14 23:26:03 +00:00 committed by GitHub
parent 2191c8b0a0
commit 251d90301c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 19 deletions

View File

@ -246,7 +246,8 @@ build/data/%.o: data/%.s
$(AS) $(ASFLAGS) $< -o $@
build/assets/text/%.enc.h: assets/text/%.h assets/text/charmap.txt
python3 tools/msgenc.py assets/text/charmap.txt $< $@
$(CPP) -P -dD -fpreprocessed $< > $(@:.enc.h=.h)
python3 tools/msgenc.py assets/text/charmap.txt $(@:.enc.h=.h) $@
build/assets/text/fra_message_data_static.o: build/assets/text/message_data.enc.h
build/assets/text/ger_message_data_static.o: build/assets/text/message_data.enc.h

View File

@ -5,20 +5,17 @@
import argparse, ast, re
charmap = {}
string_regex = re.compile(r"([\"'`])(?:[\s\S])*?(?:(?<!\\)\1)")
def read_charmap(path):
global charmap
with open(path) as infile:
charmap = infile.read()
charmap = ast.literal_eval(charmap)
charmap = dict([(repr(k)[1:-1],chr(v)) for k,v in charmap.items()])
charmap = { repr(k)[1:-1] : chr(v) for k,v in charmap.items() }
def cvt_str(m):
return charmap
def convert_text(text, charmap):
def cvt_str(m):
string = m.group(0)
for orig,char in charmap.items():
@ -26,20 +23,33 @@ def cvt_str(m):
return string
if __name__ == "__main__":
# Naive string matcher, assumes single line strings and no comments, handles escaped quotations
string_regex = re.compile(r'"((?:[^\\"\n]|\\.)*)"')
# Collapse escaped newlines
text = text.replace("\\\n", "")
# Encode according to charmap
text = re.sub(string_regex, cvt_str, text)
return text
def main():
parser = argparse.ArgumentParser(description="Encode message_data_static text headers")
parser.add_argument("charmap", help="path to charmap file specifying custom encoding elements")
parser.add_argument("input", help="path to file to be encoded")
parser.add_argument("output", help="encoded file")
args = parser.parse_args()
read_charmap(args.charmap)
charmap = read_charmap(args.charmap)
contents = ""
text = ""
with open(args.input, "r") as infile:
contents = infile.read()
text = infile.read()
contents = re.sub(string_regex, cvt_str, contents)
text = convert_text(text, charmap)
with open(args.output, "w", encoding="raw_unicode_escape") as outfile:
outfile.write(contents)
outfile.write(text)
if __name__ == "__main__":
main()