2022-01-23 08:14:00 +01:00
|
|
|
import os
|
2022-02-26 03:27:58 +01:00
|
|
|
from xml.etree.ElementInclude import include
|
2022-01-30 07:47:22 +01:00
|
|
|
from common import *
|
2022-02-03 09:43:17 +01:00
|
|
|
from extract_constants import *
|
|
|
|
|
|
|
|
in_filename = 'autogen/lua_constants/built-in.lua'
|
|
|
|
out_filename = 'src/pc/lua/smlua_constants_autogen.c'
|
|
|
|
out_filename_docs = 'docs/lua/constants.md'
|
2022-03-13 06:28:57 +01:00
|
|
|
out_filename_defs = 'autogen/lua_definitions/constants.lua'
|
2022-02-03 09:43:17 +01:00
|
|
|
|
|
|
|
in_files = [
|
|
|
|
"include/types.h",
|
|
|
|
"include/sm64.h",
|
|
|
|
"src/pc/lua/smlua_hooks.h",
|
2022-05-20 02:06:51 +02:00
|
|
|
"src/game/area.h",
|
2022-02-03 09:43:17 +01:00
|
|
|
"src/game/camera.h",
|
|
|
|
"include/mario_animation_ids.h",
|
2022-03-13 09:17:10 +01:00
|
|
|
"include/sounds.h",
|
2022-02-03 09:43:17 +01:00
|
|
|
"src/game/characters.h",
|
2022-03-26 09:08:15 +01:00
|
|
|
"src/pc/network/network.h",
|
2022-02-03 09:43:17 +01:00
|
|
|
"src/pc/network/network_player.h",
|
|
|
|
"include/PR/os_cont.h",
|
2022-02-15 03:26:44 +01:00
|
|
|
"src/game/interaction.c",
|
2022-02-16 07:21:31 +01:00
|
|
|
"src/game/interaction.h",
|
2022-02-16 07:12:20 +01:00
|
|
|
"src/pc/djui/djui_hud_utils.h",
|
2022-02-22 07:46:39 +01:00
|
|
|
"include/behavior_table.h",
|
2022-03-10 08:09:33 +01:00
|
|
|
"src/pc/lua/utils/smlua_model_utils.h",
|
2022-05-01 02:33:38 +02:00
|
|
|
"src/pc/lua/utils/smlua_misc_utils.h",
|
2022-02-26 03:27:58 +01:00
|
|
|
"include/object_constants.h",
|
2022-02-26 07:44:37 +01:00
|
|
|
"include/mario_geo_switch_case_ids.h",
|
2022-03-03 10:04:15 +01:00
|
|
|
"src/game/object_list_processor.h",
|
|
|
|
"src/engine/graph_node.h",
|
2022-03-12 03:11:32 +01:00
|
|
|
"levels/level_defines.h",
|
2022-03-19 08:56:59 +01:00
|
|
|
"src/game/obj_behaviors.c",
|
2022-03-25 03:30:15 +01:00
|
|
|
"src/game/save_file.h",
|
2022-04-07 03:24:50 +02:00
|
|
|
"src/game/obj_behaviors_2.h",
|
2022-04-10 04:50:50 +02:00
|
|
|
"include/dialog_ids.h",
|
2022-04-16 06:51:18 +02:00
|
|
|
"include/seq_ids.h",
|
2022-05-03 02:31:55 +02:00
|
|
|
"include/surface_terrains.h"
|
2022-02-03 09:43:17 +01:00
|
|
|
]
|
|
|
|
|
2022-02-26 03:27:58 +01:00
|
|
|
exclude_constants = {
|
2022-03-19 08:56:59 +01:00
|
|
|
'*': [ '^MAXCONTROLLERS$', '^AREA_[^T].*', '^AREA_T[HTO]', '^CONT_ERR.*', '^READ_MASK$', '^SIGN_RANGE$', ],
|
|
|
|
'src/game/obj_behaviors.c': ['^o$'],
|
2022-02-26 03:27:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
include_constants = {
|
|
|
|
}
|
2022-02-03 09:43:17 +01:00
|
|
|
|
|
|
|
pretend_find = [
|
|
|
|
'SOUND_ARG_LOAD'
|
|
|
|
]
|
|
|
|
############################################################################
|
2022-01-23 08:14:00 +01:00
|
|
|
|
2022-02-03 09:43:17 +01:00
|
|
|
seen_constants = []
|
2022-02-05 22:33:22 +01:00
|
|
|
totalConstants = 0
|
2022-01-30 07:47:22 +01:00
|
|
|
|
|
|
|
############################################################################
|
|
|
|
|
2022-02-03 09:43:17 +01:00
|
|
|
def validate_identifiers(built_files):
|
|
|
|
all_identifiers = [x.group()[1:] for x in re.finditer(r'[(, ][A-Z_][A-Z0-9_]*', built_files)]
|
|
|
|
all_identifiers = set(all_identifiers)
|
|
|
|
for ident in all_identifiers:
|
|
|
|
if ident in pretend_find:
|
|
|
|
continue
|
|
|
|
if ident + ' = ' not in built_files:
|
|
|
|
print('COULD NOT FIND ' + ident)
|
|
|
|
|
2022-01-30 07:47:22 +01:00
|
|
|
|
|
|
|
############################################################################
|
|
|
|
|
2022-02-03 09:43:17 +01:00
|
|
|
def saw_constant(identifier):
|
|
|
|
if identifier in seen_constants:
|
|
|
|
print("SAW DUPLICATE CONSTANT: " + identifier)
|
|
|
|
return True
|
|
|
|
else:
|
2022-02-05 22:33:22 +01:00
|
|
|
global totalConstants
|
|
|
|
totalConstants += 1
|
2022-02-03 09:43:17 +01:00
|
|
|
seen_constants.append(identifier)
|
|
|
|
return False
|
|
|
|
|
2022-02-26 03:27:58 +01:00
|
|
|
def allowed_identifier(filename, ident):
|
|
|
|
exclude_list = exclude_constants['*']
|
|
|
|
|
|
|
|
if filename in exclude_constants:
|
|
|
|
exclude_list.extend(exclude_constants[filename])
|
|
|
|
|
|
|
|
for exclude in exclude_list:
|
|
|
|
if re.search(exclude, ident) != None:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if filename in include_constants:
|
|
|
|
for include in include_constants[filename]:
|
|
|
|
if re.search(include, ident) != None:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def process_enum(filename, line):
|
2022-02-03 09:43:17 +01:00
|
|
|
_, ident, val = line.split(' ', 2)
|
|
|
|
|
|
|
|
if '{' not in val or '}' not in val:
|
2022-02-22 07:46:39 +01:00
|
|
|
#print('UNRECOGNIZED ENUM: ' + line)
|
2022-02-03 09:43:17 +01:00
|
|
|
return None
|
|
|
|
|
|
|
|
# grab inside body
|
|
|
|
val = val.split('{', 1)[-1].rsplit('}', 1)[0]
|
|
|
|
|
|
|
|
ret = {}
|
|
|
|
ret['identifier'] = ident
|
|
|
|
|
|
|
|
constants = []
|
|
|
|
set_to = None
|
|
|
|
index = 0
|
|
|
|
fields = val.split(',')
|
|
|
|
for field in fields:
|
|
|
|
field = field.strip()
|
|
|
|
if len(field) == 0:
|
|
|
|
continue
|
2022-01-23 08:14:00 +01:00
|
|
|
|
2022-02-03 09:43:17 +01:00
|
|
|
if '=' in field:
|
|
|
|
ident, val = field.split('=', 2)
|
|
|
|
constants.append([ident.strip(), val.strip()])
|
|
|
|
set_to = ident
|
|
|
|
index = 1
|
|
|
|
continue
|
2022-01-30 07:47:22 +01:00
|
|
|
|
2022-02-03 09:43:17 +01:00
|
|
|
if set_to is not None:
|
|
|
|
constants.append([field, '((%s) + %d)' % (set_to, index)])
|
|
|
|
index += 1
|
2022-01-23 08:14:00 +01:00
|
|
|
continue
|
2022-01-30 07:47:22 +01:00
|
|
|
|
2022-02-26 03:27:58 +01:00
|
|
|
if allowed_identifier(filename, field):
|
2022-02-03 09:43:17 +01:00
|
|
|
constants.append([field, str(index)])
|
2022-01-30 07:47:22 +01:00
|
|
|
|
2022-02-03 09:43:17 +01:00
|
|
|
if saw_constant(field):
|
|
|
|
print('>>> ' + line)
|
2022-03-12 03:11:32 +01:00
|
|
|
|
2022-02-03 09:43:17 +01:00
|
|
|
index += 1
|
2022-01-23 08:14:00 +01:00
|
|
|
|
2022-02-03 09:43:17 +01:00
|
|
|
ret['constants'] = constants
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2022-02-26 03:27:58 +01:00
|
|
|
def process_define(filename, line):
|
2022-02-03 09:43:17 +01:00
|
|
|
_, ident, val = line.split(' ', 2)
|
2022-01-30 07:47:22 +01:00
|
|
|
|
2022-02-03 09:43:17 +01:00
|
|
|
val = val.replace('(u8)', '')
|
|
|
|
val = val.replace('(u64)', '')
|
|
|
|
|
|
|
|
for p in val.split(' '):
|
|
|
|
if p.startswith('0x'):
|
2022-01-30 07:47:22 +01:00
|
|
|
continue
|
2022-02-03 09:43:17 +01:00
|
|
|
p = re.sub(r'0x[a-fA-F0-9]+', '', p)
|
|
|
|
if re.search('[a-z]', p) != None:
|
2022-03-26 07:46:37 +01:00
|
|
|
if 'gCurrentObject' not in line:
|
|
|
|
print('UNRECOGNIZED DEFINE: ' + line)
|
2022-02-03 09:43:17 +01:00
|
|
|
return None
|
|
|
|
|
2022-02-26 03:27:58 +01:00
|
|
|
if not allowed_identifier(filename, ident):
|
|
|
|
return None
|
2022-02-03 09:43:17 +01:00
|
|
|
|
|
|
|
if saw_constant(ident):
|
|
|
|
print('>>> ' + line)
|
2022-01-30 07:47:22 +01:00
|
|
|
|
2022-02-03 09:43:17 +01:00
|
|
|
return [ident, val]
|
2022-01-30 07:47:22 +01:00
|
|
|
|
|
|
|
|
2022-02-26 03:27:58 +01:00
|
|
|
def process_line(filename, line):
|
2022-02-03 09:43:17 +01:00
|
|
|
if line.startswith('enum '):
|
2022-02-26 03:27:58 +01:00
|
|
|
return process_enum(filename, line)
|
2022-02-03 09:43:17 +01:00
|
|
|
elif line.startswith('#define '):
|
2022-02-26 03:27:58 +01:00
|
|
|
return process_define(filename, line)
|
2022-02-03 09:43:17 +01:00
|
|
|
else:
|
|
|
|
print("UNRECOGNIZED LINE: " + line)
|
|
|
|
return None
|
|
|
|
|
|
|
|
def process_file(filename):
|
|
|
|
processed_file = {}
|
|
|
|
processed_file['filename'] = filename.replace('\\', '/').split('/')[-1]
|
|
|
|
|
|
|
|
constants = []
|
|
|
|
lines = extract_constants(get_path(filename)).splitlines()
|
|
|
|
for line in lines:
|
2022-02-26 03:27:58 +01:00
|
|
|
c = process_line(filename, line)
|
2022-02-03 09:43:17 +01:00
|
|
|
if c != None:
|
|
|
|
constants.append(c)
|
2022-01-30 07:47:22 +01:00
|
|
|
|
2022-02-03 09:43:17 +01:00
|
|
|
processed_file['constants'] = constants
|
2022-01-30 07:47:22 +01:00
|
|
|
|
2022-02-03 09:43:17 +01:00
|
|
|
return processed_file
|
2022-01-30 07:47:22 +01:00
|
|
|
|
2022-02-03 09:43:17 +01:00
|
|
|
def process_files():
|
|
|
|
seen_constants = []
|
|
|
|
processed_files = []
|
|
|
|
files = sorted(in_files, key=lambda d: d.split('/')[-1])
|
|
|
|
for f in files:
|
|
|
|
processed_files.append(process_file(f))
|
|
|
|
return processed_files
|
|
|
|
|
|
|
|
############################################################################
|
|
|
|
|
|
|
|
def build_constant(processed_constant):
|
|
|
|
constants = processed_constant
|
|
|
|
s = ''
|
|
|
|
|
|
|
|
is_enum = 'identifier' in processed_constant
|
|
|
|
if is_enum:
|
|
|
|
constants = processed_constant['constants']
|
|
|
|
else:
|
|
|
|
constants = [processed_constant]
|
2022-01-30 07:47:22 +01:00
|
|
|
|
|
|
|
for c in constants:
|
2022-02-03 09:43:17 +01:00
|
|
|
s += '%s = %s\n' % (c[0], c[1])
|
2022-01-30 07:47:22 +01:00
|
|
|
|
2022-02-03 09:43:17 +01:00
|
|
|
return s
|
|
|
|
|
|
|
|
def build_file(processed_file):
|
|
|
|
s = ''
|
|
|
|
for c in processed_file['constants']:
|
|
|
|
s += build_constant(c)
|
|
|
|
|
|
|
|
return s
|
2022-01-30 07:47:22 +01:00
|
|
|
|
2022-02-03 09:43:17 +01:00
|
|
|
def build_files(processed_files):
|
|
|
|
s = ''
|
|
|
|
for file in processed_files:
|
|
|
|
s += build_file(file)
|
2022-01-30 07:47:22 +01:00
|
|
|
|
|
|
|
return s
|
|
|
|
|
2022-02-03 09:43:17 +01:00
|
|
|
def build_to_c(built_files):
|
|
|
|
txt = ''
|
|
|
|
with open(get_path(in_filename), 'r') as f:
|
|
|
|
txt = f.read()
|
|
|
|
txt += '\n' + built_files
|
|
|
|
|
|
|
|
while ('\n\n' in txt):
|
|
|
|
txt = txt.replace('\n\n', '\n')
|
|
|
|
|
|
|
|
lines = txt.splitlines()
|
|
|
|
txt = 'char gSmluaConstants[] = ""\n'
|
|
|
|
for line in lines:
|
|
|
|
txt += '"%s\\n"\n' % line
|
|
|
|
txt += ';'
|
|
|
|
return txt
|
|
|
|
|
|
|
|
############################################################################
|
|
|
|
|
|
|
|
def doc_constant_index(processed_files):
|
2022-01-30 07:47:22 +01:00
|
|
|
s = '# Supported Constants\n'
|
2022-02-03 09:43:17 +01:00
|
|
|
for processed_file in processed_files:
|
2022-03-05 07:24:21 +01:00
|
|
|
s += '- [%s](#%s)\n' % (processed_file['filename'], processed_file['filename'].replace('.', ''))
|
2022-02-03 09:43:17 +01:00
|
|
|
constants = [x for x in processed_file['constants'] if 'identifier' in x]
|
|
|
|
constants = sorted(constants, key=lambda d: d['identifier'])
|
|
|
|
for c in constants:
|
2022-03-05 07:11:10 +01:00
|
|
|
s += ' - [enum %s](#enum-%s)\n' % (c['identifier'], c['identifier'])
|
2022-02-03 09:43:17 +01:00
|
|
|
s += '\n<br />\n\n'
|
|
|
|
return s
|
2022-01-30 07:47:22 +01:00
|
|
|
|
2022-02-03 09:43:17 +01:00
|
|
|
def doc_constant(processed_constant):
|
|
|
|
constants = processed_constant
|
|
|
|
s = ''
|
|
|
|
|
|
|
|
is_enum = 'identifier' in processed_constant
|
|
|
|
if is_enum:
|
|
|
|
constants = processed_constant['constants']
|
|
|
|
if len(constants) == 0:
|
|
|
|
return ''
|
|
|
|
|
|
|
|
enum = 'enum ' + processed_constant['identifier']
|
|
|
|
s += '\n### [%s](#%s)\n' % (enum, processed_constant['identifier'])
|
|
|
|
s += '| Identifier | Value |\n'
|
|
|
|
s += '| :--------- | :---- |\n'
|
|
|
|
for c in constants:
|
|
|
|
s += '| %s | %s |\n' % (c[0], c[1])
|
|
|
|
return s
|
|
|
|
|
|
|
|
for c in [processed_constant]:
|
|
|
|
s += '- %s\n' % (c[0])
|
2022-01-30 07:47:22 +01:00
|
|
|
|
|
|
|
return s
|
|
|
|
|
2022-02-03 09:43:17 +01:00
|
|
|
def doc_file(processed_file):
|
|
|
|
s = '## [%s](#%s)\n' % (processed_file['filename'], processed_file['filename'])
|
|
|
|
constants = sorted(processed_file['constants'], key=lambda d: 'zzz' + d['identifier'] if 'identifier' in d else d[0])
|
|
|
|
for c in constants:
|
|
|
|
s += doc_constant(c)
|
|
|
|
|
2022-03-05 07:38:43 +01:00
|
|
|
s += '\n[:arrow_up_small:](#)\n'
|
2022-02-03 09:43:17 +01:00
|
|
|
s += '\n<br />\n\n'
|
|
|
|
return s
|
|
|
|
|
|
|
|
def doc_files(processed_files):
|
2022-01-30 07:47:22 +01:00
|
|
|
s = '## [:rewind: Lua Reference](lua.md)\n\n'
|
2022-02-03 09:43:17 +01:00
|
|
|
s += doc_constant_index(processed_files)
|
|
|
|
for file in processed_files:
|
|
|
|
s += doc_file(file)
|
2022-01-30 07:47:22 +01:00
|
|
|
|
2022-02-03 09:43:17 +01:00
|
|
|
return s
|
2022-01-30 07:47:22 +01:00
|
|
|
|
2022-02-03 09:43:17 +01:00
|
|
|
############################################################################
|
2022-01-30 07:47:22 +01:00
|
|
|
|
2022-03-13 06:28:57 +01:00
|
|
|
def def_constant(processed_constant):
|
|
|
|
constants = processed_constant
|
|
|
|
s = ''
|
|
|
|
|
|
|
|
is_enum = 'identifier' in processed_constant
|
|
|
|
if is_enum:
|
|
|
|
s += '\n--- @class %s\n' % translate_to_def(processed_constant['identifier'])
|
|
|
|
constants = processed_constant['constants']
|
|
|
|
if len(constants) == 0:
|
|
|
|
return ''
|
|
|
|
for c in constants:
|
|
|
|
s += '\n--- @type %s\n' % translate_to_def(processed_constant['identifier'])
|
|
|
|
s += '%s = %s\n' % (c[0], c[1])
|
|
|
|
return s
|
|
|
|
|
|
|
|
for c in [processed_constant]:
|
|
|
|
s += '\n--- @type integer\n'
|
|
|
|
s += '%s = %s\n' % (c[0], c[1])
|
|
|
|
|
|
|
|
return s
|
|
|
|
|
|
|
|
def build_to_def(processed_files):
|
|
|
|
s = '-- AUTOGENERATED FOR CODE EDITORS --\n\n'
|
|
|
|
with open(get_path(in_filename), 'r') as f:
|
|
|
|
s += f.read()
|
|
|
|
s += '\n'
|
|
|
|
|
|
|
|
for file in processed_files:
|
|
|
|
constants = sorted(file['constants'], key=lambda d: 'zzz' + d['identifier'] if 'identifier' in d else d[0])
|
|
|
|
for c in constants:
|
|
|
|
s += def_constant(c)
|
|
|
|
|
|
|
|
return s
|
|
|
|
|
|
|
|
############################################################################
|
|
|
|
|
2022-02-03 09:43:17 +01:00
|
|
|
def main():
|
|
|
|
processed_files = process_files()
|
|
|
|
built_files = build_files(processed_files)
|
|
|
|
validate_identifiers(built_files)
|
2022-01-30 07:47:22 +01:00
|
|
|
|
2022-02-03 09:43:17 +01:00
|
|
|
built_c = build_to_c(built_files)
|
2022-01-30 07:47:22 +01:00
|
|
|
|
2022-02-03 09:43:17 +01:00
|
|
|
with open(get_path(out_filename), 'w') as out:
|
|
|
|
out.write(built_c)
|
|
|
|
|
|
|
|
doc = doc_files(processed_files)
|
|
|
|
with open(get_path(out_filename_docs), 'w') as out:
|
|
|
|
out.write(doc)
|
2022-01-30 07:47:22 +01:00
|
|
|
|
2022-03-13 06:28:57 +01:00
|
|
|
defs = build_to_def(processed_files)
|
|
|
|
with open(get_path(out_filename_defs), 'w') as out:
|
|
|
|
out.write(defs)
|
|
|
|
|
2022-02-05 22:33:22 +01:00
|
|
|
global totalConstants
|
|
|
|
print("Total constants: " + str(totalConstants))
|
|
|
|
|
2022-02-16 07:08:01 +01:00
|
|
|
main()
|