mirror of
https://github.com/zeldaret/oot.git
synced 2024-11-11 03:39:59 +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:
parent
2191c8b0a0
commit
251d90301c
2 changed files with 30 additions and 19 deletions
3
Makefile
3
Makefile
|
@ -246,7 +246,8 @@ build/data/%.o: data/%.s
|
||||||
$(AS) $(ASFLAGS) $< -o $@
|
$(AS) $(ASFLAGS) $< -o $@
|
||||||
|
|
||||||
build/assets/text/%.enc.h: assets/text/%.h assets/text/charmap.txt
|
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/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
|
build/assets/text/ger_message_data_static.o: build/assets/text/message_data.enc.h
|
||||||
|
|
|
@ -5,20 +5,17 @@
|
||||||
|
|
||||||
import argparse, ast, re
|
import argparse, ast, re
|
||||||
|
|
||||||
charmap = {}
|
|
||||||
|
|
||||||
string_regex = re.compile(r"([\"'`])(?:[\s\S])*?(?:(?<!\\)\1)")
|
|
||||||
|
|
||||||
def read_charmap(path):
|
def read_charmap(path):
|
||||||
global charmap
|
|
||||||
|
|
||||||
with open(path) as infile:
|
with open(path) as infile:
|
||||||
charmap = infile.read()
|
charmap = infile.read()
|
||||||
|
|
||||||
charmap = ast.literal_eval(charmap)
|
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)
|
string = m.group(0)
|
||||||
|
|
||||||
for orig,char in charmap.items():
|
for orig,char in charmap.items():
|
||||||
|
@ -26,20 +23,33 @@ def cvt_str(m):
|
||||||
|
|
||||||
return string
|
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 = 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("charmap", help="path to charmap file specifying custom encoding elements")
|
||||||
parser.add_argument("input", help="path to file to be encoded")
|
parser.add_argument("input", help="path to file to be encoded")
|
||||||
parser.add_argument("output", help="encoded file")
|
parser.add_argument("output", help="encoded file")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
read_charmap(args.charmap)
|
charmap = read_charmap(args.charmap)
|
||||||
|
|
||||||
contents = ""
|
text = ""
|
||||||
with open(args.input, "r") as infile:
|
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:
|
with open(args.output, "w", encoding="raw_unicode_escape") as outfile:
|
||||||
outfile.write(contents)
|
outfile.write(text)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
|
Loading…
Reference in a new issue