WiiDuktape/source/assets/data_asset_generator.py

52 lines
1.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import argparse
from pathlib import Path
parser = argparse.ArgumentParser()
parser.add_argument("input")
parser.add_argument("output")
args = parser.parse_args()
input = Path(args.input)
output = Path(args.output)
if not input.exists():
print("Input " + str(input.absolute()) + " not found.")
raise SystemExit(1)
file_array = list()
for child in Path(input).iterdir():
if child.is_file():
file_array.append({"filename": child.name, "data": child.read_bytes()})
def generate_data():
string_list = list()
for index, data in enumerate(file_array):
hexList = list()
for byte in data["data"]:
hexList.append(f"0x{byte:02x}")
string_list.append(f"unsigned char data{index}[] = {{ {','.join(hexList)} }};")
return '\n'.join(string_list)
def generate_hashmap_init():
string_list = list()
for index, data in enumerate(file_array):
2024-06-25 01:31:56 +00:00
filename = '"' + data["filename"] + '"'
string_list.append(f"if (strcmp(filename, {filename}) == 0) {{ *data = &data{index}; *size = sizeof(data{index}); return 1; }}")
strings = '\n'.join(string_list)
return f"""int get_file_pointer(const char* filename, void ** data, size_t * size) {{
2024-06-25 01:31:56 +00:00
{strings}
return 0;
}}"""
code = f"""#include \"data.h\"
#include <string.h>
{generate_data()}
{generate_hashmap_init()}
"""
with open(output, 'w') as f:
f.write(code)