WiiDuktape/source/data_asset_generator.py
Fries 9f1b00d39d add file support
file loading is implemented with an asset generator script that reads
all the files in the data folder and converts them into c arrays and
puts them inside a c source file.
2024-06-20 18:41:49 -07:00

50 lines
1.3 KiB
Python
Executable file

#!/usr/bin/env python3
import argparse
from pathlib import Path
from PIL import Image
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):
string_list.append(f"if (strcmp(filename, \"{data["filename"]}\") == 0) {{ *data = &data{index}; *size = sizeof(data{index}); return 1; }}")
return f"""int get_file_pointer(const char* filename, void ** data, size_t * size) {{
{'\n'.join(string_list)}
return 0;
}}"""
code = f"""#include \"data.h\"
#include <string.h>
{generate_data()}
{generate_hashmap_init()}
"""
with open(output, 'w') as f:
f.write(code)