perfect_dark/tools/assetmgr/mklang

457 lines
7.9 KiB
Python
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
import assetmgr
import json
import os
import re
import sys
def main():
fd = open(sys.argv[1], 'r')
data = fd.read()
fd.close()
basename = os.path.basename(sys.argv[1]).replace('.json', '')
shortname = get_shortname(basename)
rows = json.loads(data)
if sys.argv[2] == 'en':
make_header(rows, shortname)
make_object(rows, basename, sys.argv[2])
def get_shortname(basename):
return re.match(r'^[a-z0-9]+', basename)[0]
def make_header(rows, shortname):
typename = 'l_%s' % shortname
enums = [row['id'] for row in rows]
filename = 'lang/%s.h' % shortname
terminator = 'L_%s_END' % shortname.upper()
start = banks.index(shortname) * 512
assetmgr.write_enums(typename, enums, filename, terminator, start=start)
def make_object(rows, basename, key):
binary = make_binary(rows, key)
zipped = assetmgr.zip(binary)
suffix = {
'en': 'E',
'gb': 'P',
'jp': 'J',
'fr': '_str_f',
'de': '_str_g',
'it': '_str_i',
'es': '_str_s',
}[key]
filename = 'files/L%s%s.o' % (basename, suffix)
assetmgr.write_object(zipped, filename)
def make_binary(rows, key):
if len(rows) == 0:
return (0).to_bytes(16, 'big')
output = bytes()
pos = len([row for row in rows if key in row]) * 4
strings = []
for index, row in enumerate(rows):
if key in row:
if row[key] is None:
output += (0).to_bytes(4, 'big')
else:
output += pos.to_bytes(4, 'big')
if os.environ['ROMID'] == 'jpn-final' and key == 'jp':
string = encode_jp(row[key])
else:
string = row[key].encode('latin_1')
strings.append(string)
pos += assetmgr.align4(len(string) + 1)
for string in strings:
output += string
amount = 4 - (len(string) % 4)
output += (0).to_bytes(amount, 'big')
if len(output) % 16:
amount = 16 - (len(output) % 16)
output += (0).to_bytes(amount, 'big')
return output
"""
The input string is UTF-8, and it needs to be re-encoded to a custom encoding.
A lookup array is used for this.
However, some UTF-8 characters appear multiple types with a different encoding.
For duplicates, we store the character in JSON as something like \habcd where
abcd is the target value in hex.
"""
def encode_jp(string):
outbytes = bytes()
i = 0
while i < len(string):
if string[i] in jpnchars:
outbytes += jpnchars[string[i]].to_bytes(2, 'big')
i += 1;
else:
match = re.match(r'^\\h([a-z0-9]{4})', string[i:])
if match:
dec = int(match[1], 16)
outbytes += dec.to_bytes(2, 'big')
i += 6;
else:
outbytes += ord(string[i]).to_bytes(1, 'big')
i += 1;
return outbytes
banks = [
'',
'ame',
'arch',
'ark',
'ash',
'azt',
'cat',
'cave',
'arec',
'crad',
'cryp',
'dam',
'depo',
'dest',
'dish',
'ear',
'eld',
'imp',
'jun',
'lee',
'len',
'lip',
'lue',
'oat',
'pam',
'pete',
'ref',
'rit',
'run',
'sevb',
'sev',
'sevx',
'sevxb',
'sho',
'silo',
'stat',
'tra',
'wax',
'gun',
'title',
'mpmenu',
'propobj',
'mpweapons',
'options',
'misc',
'uff',
'old',
'ate',
'lam',
'mp1',
'mp2',
'mp3',
'mp4',
'mp5',
'mp6',
'mp7',
'mp8',
'mp9',
'mp10',
'mp11',
'mp12',
'mp13',
'mp14',
'mp15',
'mp16',
'mp17',
'mp18',
'mp19',
'mp20',
]
jpnchars = {
' ': 0x8080,
'': 0x8081,
'': 0x8082,
'': 0x8083,
'': 0x8084,
'': 0x8085,
'': 0x8086,
'': 0x8087,
'': 0x8088,
'': 0x8089,
'': 0x808a,
'': 0x808b,
'': 0x808c,
'': 0x808d,
'': 0x808e,
' ': 0x808f,
'': 0x8090,
'': 0x8091,
'': 0x8092,
'': 0x8093,
'': 0x8094,
'': 0x8095,
'': 0x8096,
'': 0x8097,
'': 0x8098,
'': 0x8099,
'': 0x809a,
'': 0x809b,
'': 0x809c,
'': 0x809d,
'': 0x809e,
'': 0x809f,
'': 0x80a0,
'': 0x80a1,
'': 0x80a2,
'': 0x80a3,
'': 0x80a4,
'': 0x80a5,
'': 0x80a6,
'': 0x80a7,
'': 0x80a8,
'': 0x80a9,
'': 0x80aa,
'': 0x80ab,
'': 0x80ac,
'': 0x80ad,
'': 0x80ae,
'': 0x80af,
'': 0x80b0,
'': 0x80b1,
'': 0x80b2,
'': 0x80b3,
'': 0x80b4,
'': 0x80b5,
'': 0x80b6,
'': 0x80b7,
'': 0x80b8,
'': 0x80b9,
'': 0x80ba,
'': 0x80bb,
'': 0x80bc,
'': 0x80bd,
'': 0x80be,
'': 0x80bf,
'': 0x80c0,
'': 0x80c1,
'': 0x80c2,
'': 0x80c3,
'': 0x80c4,
'': 0x80c5,
'': 0x80c6,
'': 0x80c7,
'': 0x80c8,
'': 0x80c9,
'': 0x80ca,
'': 0x80cb,
'': 0x80cc,
'': 0x80cd,
'': 0x80ce,
'': 0x80cf,
'': 0x80d0,
'': 0x80d1,
'': 0x80d2,
'': 0x80d3,
'': 0x80d4,
'': 0x80d5,
'': 0x80d6,
'': 0x80d7,
'': 0x80d8,
'': 0x80d9,
'': 0x80da,
'': 0x80db,
'': 0x80dc,
'': 0x80dd,
'': 0x80de,
'': 0x80df,
'': 0x80e0,
'': 0x80e1,
'': 0x80e2,
'': 0x80e3,
'': 0x80e4,
'': 0x80e5,
'': 0x80e6,
'': 0x80e7,
'': 0x80e8,
'': 0x80e9,
'': 0x80ea,
'': 0x80eb,
'': 0x80ec,
'': 0x80ed,
'': 0x80ee,
'': 0x80ef,
'': 0x80f0,
'': 0x80f1,
'': 0x80f2,
'': 0x80f3,
'': 0x80f4,
'': 0x80f5,
'': 0x80f6,
'': 0x80f7,
'': 0x80f8,
'': 0x80f9,
'': 0x80fa,
'': 0x80fb,
'': 0x80fc,
'': 0x80fd,
'': 0x80fe,
'': 0x80ff,
'': 0x8180,
'': 0x8181,
'': 0x8182,
'': 0x8183,
'': 0x8184,
'': 0x8185,
'': 0x8186,
'': 0x8187,
'': 0x8188,
'': 0x8189,
'': 0x818a,
'': 0x818b,
'': 0x818c,
'': 0x818d,
'': 0x818e,
'': 0x818f,
'': 0x8190,
'': 0x8191,
'': 0x8192,
'': 0x8193,
'': 0x8194,
'': 0x8195,
'': 0x8196,
'': 0x8197,
'': 0x8198,
'': 0x8199,
'': 0x819a,
'': 0x819b,
'': 0x819c,
'': 0x819d,
'': 0x819e,
'': 0x819f,
'': 0x81a0,
'': 0x81a1,
'': 0x81a2,
'': 0x81a3,
'': 0x81a4,
'': 0x81a5,
'': 0x81a6,
'': 0x81a7,
'': 0x81a8,
'': 0x81a9,
'': 0x81aa,
'': 0x81ab,
'': 0x81ac,
'': 0x81ad,
'': 0x81ae,
'': 0x81af,
'': 0x81b0,
'': 0x81b1,
'': 0x81b2,
'': 0x81b3,
'': 0x81b4,
'': 0x81b5,
'': 0x81b6,
'': 0x81b7,
'': 0x81b8,
'': 0x81b9,
'': 0x81ba,
'': 0x81bb,
'': 0x81bc,
'': 0x81bd,
'': 0x81be,
'': 0x81bf,
'': 0x81c0,
'': 0x81c1,
'': 0x81c2,
'': 0x81c3,
'': 0x81c4,
'': 0x81c5,
'': 0x81c6,
'': 0x81c7,
'': 0x81c8,
'': 0x81c9,
'': 0x81ca,
'': 0x81cb,
'': 0x81cc,
'': 0x81cd,
'': 0x81ce,
'': 0x81cf,
'': 0x81d0,
'': 0x81d1,
'': 0x81d2,
'': 0x81d3,
'': 0x81d4,
'': 0x81d5,
'': 0x81d6,
'': 0x81d7,
'': 0x81d8,
'': 0x81d9,
'': 0x81da,
'': 0x81db,
'': 0x81dc,
'': 0x81dd,
'': 0x81de,
'': 0x81df,
'': 0x81e0,
'': 0x81e1,
'': 0x81e2,
'': 0x81e3,
'': 0x81e4,
'': 0x81e5,
'': 0x81e6,
'': 0x81e7,
'': 0x81e8,
'': 0x81e9,
'': 0x81ea,
'': 0x81eb,
'': 0x81ec,
'': 0x81ed,
'': 0x81ee,
'': 0x81ef,
'': 0x81f0,
'': 0x81f1,
'': 0x81f2,
'': 0x81f3,
'': 0x81f4,
'': 0x81f5,
'': 0x81f6,
'': 0x81f7,
'': 0x81f8,
'': 0x81f9,
'': 0x81fa,
'': 0x81fb,
'': 0x81fc,
'': 0x81fd,
'': 0x81fe,
'': 0x81ff,
'': 0x8288,
'': 0x82a1,
'': 0x82ba,
'': 0x8387,
'': 0x8499,
'': 0x868c,
'': 0x8692,
'': 0x86b3,
'': 0x86b9,
'': 0x86c9,
}
main()