sm64coopdx/autogen/extract_structs.py
PeachyPeach 32f395fb0c
Some checks failed
Build coop / build-linux (push) Has been cancelled
Build coop / build-steamos (push) Has been cancelled
Build coop / build-windows-opengl (push) Has been cancelled
Build coop / build-windows-directx (push) Has been cancelled
Build coop / build-macos-arm (push) Has been cancelled
Build coop / build-macos-intel (push) Has been cancelled
ModFs improvements (#907)
* zip + json properties; check existing file in create file

* smlua_audio_utils_replace_sequence

* audio_stream_load, audio_sample_load, smlua_model_util_get_id

* get_texture_info + can also load PNG files

* smlua_collision_util_get

* add wildcard in properties files + set text mode

* filepath restrictions

* Some mod_storage improvements

- Cache mod storage files into a map to reduce file I/O
- Fix a bug in mod_storage_save
- Add mod_storage_load_all that returns all keys/values as a table

* shutdown; fix buffer overflow; fix warnings; lua table

* reject binary files starting with MZ or ELF

* function members

* better doc

* adding file rewind

* ModFS guide; replace yaml by ini; read string buffer changes
2025-10-21 19:42:06 +02:00

81 lines
No EOL
1.9 KiB
Python

import os
import re
import sys
from common import cobject_function_identifier
def extract_structs(filename):
with open(filename) as file:
lines = file.readlines()
txt = ''
# strip line continuation
for line in lines:
if line.strip().endswith('\\'):
txt += line.strip()[:-1] + ' '
else:
txt += line
# strip directives and comments
tmp = txt
txt = ''
for line in tmp.splitlines():
if line.strip().startswith('#'):
continue
if '//' in line:
line = line.split('//', 1)[0]
txt += line + '\n'
while ('/*' in txt):
s1 = txt.split('/*', 1)
s2 = s1[1].split('*/', 1)
txt = s1[0] + s2[-1]
# normalize newlines
txt = txt.replace('\n', ' ')
txt = txt.replace(';', ';\n')
txt = txt.replace('{', '{\n')
while (' ' in txt):
txt = txt.replace(' ', ' ')
# handle function members (NOT function pointers)
txt = re.sub(f'{cobject_function_identifier}\\((.*),(.*)\\)', f'{cobject_function_identifier} \\1 \\2', txt)
# strip macros
txt = re.sub(r'[^a-zA-Z0-9_][A-Z0-9_]+\(.*\)', '', txt)
# strip blocks
tmp = txt
txt = ''
inside = 0
for character in tmp:
if character == '\n':
if inside == 0:
txt += character
else:
txt += character
if character == '{':
inside += 1
if character == '}':
inside -= 1
# extract structs
tmp = txt
txt = ''
for line in tmp.splitlines():
line = line.strip()
if '{' not in line:
continue
if '}' not in line:
continue
if ';' not in line:
continue
if not line.startswith('struct ') and not line.startswith('typedef struct '):
continue
txt += line + '\n'
return txt.splitlines()
if __name__ == "__main__":
print(extract_structs(sys.argv[1]))