sm64ex-coop/autogen/common.py

164 lines
3.9 KiB
Python
Raw Normal View History

import os
usf_types = ['u8', 'u16', 'u32', 'u64', 's8', 's16', 's32', 's64', 'f32']
vec3_types = ['Vec3s', 'Vec3f']
typedef_pointers = ['BehaviorScript', 'ObjectAnimPointer', 'Collision']
exclude_structs = [
'SPTask',
'VblankHandler',
'GraphNodeRoot',
'MarioAnimDmaRelatedThing',
'UnusedArea28',
]
def get_path(p):
return os.path.dirname(os.path.realpath(__file__)) + '/../' + p
def translate_type_to_lvt(ptype):
2022-02-16 07:09:21 +01:00
if ptype == 'char':
ptype = 'u8'
2022-02-16 07:13:10 +01:00
if 'const ' in ptype:
ptype = ptype.replace('const ', '').strip()
if ('char' in ptype and '[' in ptype):
return 'LVT_STRING'
2022-02-16 07:09:21 +01:00
if ptype == 'char*':
return 'LVT_STRING_P'
if '[' in ptype or '{' in ptype:
return 'LOT_???'
2022-02-16 07:09:21 +01:00
2022-02-03 04:24:51 +01:00
if 'enum ' in ptype:
return 'LVT_S32'
2022-02-16 07:09:21 +01:00
2022-02-03 04:24:51 +01:00
if ptype == 'bool':
2022-02-03 05:24:35 +01:00
return 'LVT_BOOL'
2022-02-16 07:09:21 +01:00
if ptype in usf_types:
return 'LVT_' + ptype.upper()
2022-02-16 07:09:21 +01:00
if ptype in vec3_types:
return 'LVT_COBJECT'
2022-02-16 07:09:21 +01:00
if ptype == 'float':
return 'LVT_F32'
2022-02-16 07:09:21 +01:00
if ptype == 'LuaFunction':
return 'LVT_LUAFUNCTION'
if 'struct' in ptype:
if ptype.count('*') > 1:
return 'LVT_???'
if '*' in ptype:
return 'LVT_COBJECT_P'
return 'LVT_COBJECT'
2022-02-16 07:09:21 +01:00
if ptype.count('*') == 1 and '(' not in ptype and '[' not in ptype:
ptype = ptype.replace('const', '').replace('*', '').strip()
if ptype in usf_types or ptype in typedef_pointers:
2022-02-16 07:09:21 +01:00
return 'LVT_%s_P' % ptype.upper()
return 'LVT_???'
def translate_type_to_lot(ptype):
if ptype == 'const char*':
return 'LOT_NONE'
2022-02-16 07:09:21 +01:00
if ptype == 'char*' or ('char' in ptype and '[' in ptype):
return 'LOT_NONE'
2022-02-16 07:13:10 +01:00
if 'const ' in ptype:
ptype = ptype.replace('const ', '')
if ptype == 'char':
ptype = 'u8'
if '[' in ptype or '{' in ptype:
return 'LOT_???'
2022-02-16 07:09:21 +01:00
2022-02-03 04:24:51 +01:00
if 'enum ' in ptype:
return 'LOT_NONE'
2022-02-16 07:09:21 +01:00
2022-02-03 04:24:51 +01:00
if ptype == 'bool':
return 'LOT_NONE'
2022-02-16 07:09:21 +01:00
if ptype in usf_types:
return 'LOT_NONE'
2022-02-16 07:09:21 +01:00
if ptype in vec3_types:
return 'LOT_' + ptype.upper()
2022-02-16 07:09:21 +01:00
if ptype == 'float':
return 'LOT_NONE'
2022-02-16 07:09:21 +01:00
if ptype == 'LuaFunction':
return 'LOT_NONE'
if 'struct' in ptype:
if ptype.count('*') > 1:
return 'LOT_???'
struct_id = ptype.split(' ')[1].replace('*', '')
if struct_id in exclude_structs:
return 'LOT_???'
return 'LOT_' + struct_id.upper()
2022-02-16 07:09:21 +01:00
if ptype.count('*') == 1 and '???' not in translate_type_to_lvt(ptype):
return 'LOT_POINTER'
2022-02-16 07:09:21 +01:00
return 'LOT_???'
2022-01-30 07:47:22 +01:00
def translate_type_to_lua(ptype):
if ptype.startswith('struct '):
ptype = ptype.split(' ')[1].replace('*', '')
return ptype, 'structs.md#%s' % ptype
2022-01-30 07:47:22 +01:00
if ptype == 'const char*':
return '`string`', None
if ptype == 'char*' or ('char' in ptype and '[' in ptype):
return '`string`', None
2022-01-30 07:47:22 +01:00
if 'Vec3' in ptype:
return ptype, 'structs.md#%s' % ptype
2022-01-30 07:47:22 +01:00
if ptype.startswith('enum '):
return ptype, 'constants.md#%s' % ptype.replace(' ', '-')
2022-01-30 07:47:22 +01:00
if ptype in usf_types:
if ptype.startswith('f'):
return '`number`', None
2022-01-30 07:47:22 +01:00
else:
return '`integer`', None
2022-01-30 07:47:22 +01:00
if 'void' == ptype:
return None, None
2022-01-30 07:47:22 +01:00
if ptype == 'LuaFunction':
return '`Lua Function` ()', None
2022-02-16 07:09:21 +01:00
if ptype.count('*') == 1 and '???' not in translate_type_to_lvt(ptype):
ptype = ptype.replace('const', '').replace('*', '').strip()
s = '`Pointer` <%s>' % translate_type_to_lua(ptype)[0]
return s, None
2022-02-16 07:09:21 +01:00
if not ptype.startswith('`'):
ptype = '`' + ptype + '`'
return ptype, None
2022-02-16 07:09:21 +01:00
def gen_comment_header(f):
comment_h = "// " + f + " //"
comment_l = "/" * len(comment_h)
s = ""
s += " " + comment_l + "\n"
s += " " + comment_h + "\n"
s += "" + comment_l + "\n"
s += "\n"
return s