You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.3 KiB

#!/usr/bin/env python3
from argparse import ArgumentParser, FileType
from struct import pack, unpack
import json
import re
def str_array2json(input, output):
if input.endswith(".bin"):
# str_array.binからJSONへ
with open(input, "rb") as f:
offsets = []
while True:
offset, = unpack("<I", f.read(4))
if offset == 0:
break
offsets.append(offset)
strings = []
for offset in offsets:
f.seek(offset)
bstr = f.read(256)
strings.append(bstr[:bstr.find(b"\0")].decode("utf8"))
with open(output, "w") as out:
json.dump(strings, out, indent="\t", ensure_ascii=False)
else:
# JSONからstr_array.binへ
with open(input, "r") as f, open(output, "wb") as out:
strings = json.loads(re.sub("//.*", "", f.read(), flags=re.MULTILINE))
cur_ofs = (len(strings) + 1) * 4
for string in strings:
out.write(pack("<I", cur_ofs))
cur_ofs += len(string.encode("utf8")) + 1
out.write(pack("<I", 0))
for string in strings:
out.write(string.encode("utf8") + b"\0")
if __name__ == "__main__":
parser = ArgumentParser(description="Project DIVA fのstr_array.binをJSONに変換")
parser.add_argument("input", type=str)
parser.add_argument("output", type=str)
args = parser.parse_args()
str_array2json(args.input, args.output)