[nostalgia] Cleanup icon rsrc generation
Some checks are pending
Build / build (push) Waiting to run

This commit is contained in:
2025-04-12 16:19:32 -05:00
parent 3910f4e77c
commit b66f459f75
11 changed files with 782 additions and 695 deletions

View File

@ -9,13 +9,16 @@
#
import argparse
import os
import json
import pathlib
import sys
def write_txt_file(path: str, txt: str):
with open(path, "w") as f:
f.write(txt)
print(f'Wrote {path}')
with open(path, "w") as f:
f.write(txt)
print(f'Wrote {path}')
def file_to_hex(path: str, line_prefix: str) -> tuple[str, int]:
@ -33,35 +36,73 @@ def file_to_hex(path: str, line_prefix: str) -> tuple[str, int]:
return out[:-1], len(data)
def file_to_cpp(path: str, var_name: str, namespace: str, cpp_path: str, hpp_path: str):
if len(namespace) > 0:
push_ns = f'namespace {namespace} {{\n'
pop_ns = '\n}\n'
data, dataLen = file_to_hex(path, "\t")
cpp = '\n#include <ox/std/array.hpp>\n\n'
cpp = '\n#include <ox/std/span.hpp>\n\n'
cpp += push_ns
cpp += f'\nstatic constexpr ox::Array<uint8_t, {dataLen}> {var_name}Data {{\n{data}\n}};\n'
cpp += f'\nox::SpanView<uint8_t> {var_name}() noexcept {{ return {var_name}Data; }}\n'
cpp += pop_ns
write_txt_file(cpp_path, cpp)
if hpp_path is not None:
hpp = '\n#include <ox/std/span.hpp>\n\n'
hpp += push_ns
hpp += f'\n[[nodiscard]]\nox::SpanView<uint8_t> {var_name}() noexcept;\n'
hpp += pop_ns
write_txt_file(hpp_path, hpp)
def file_to_cpp(path: str, cpp_name: str) -> tuple[str, str]:
cpp = ''
hpp = ''
data, data_len = file_to_hex(path, "\t")
cpp += f'\nstatic constexpr ox::Array<uint8_t, {data_len}> {cpp_name}Data {{\n{data}\n}};\n'
cpp += f'\nox::SpanView<uint8_t> {cpp_name}() noexcept {{ return {cpp_name}Data; }}\n'
hpp += f'\n[[nodiscard]]\nox::SpanView<uint8_t> {cpp_name}() noexcept;\n'
return cpp, hpp
def proc_rsrc_file(rsrc_path: str):# Open and read the JSON file
with open(rsrc_path, 'r') as file:
data = json.load(file)
base_path = pathlib.Path(rsrc_path).parent.absolute()
if 'all_files' in data:
all_files = data['all_files']
else:
all_files = None
if 'namespace' in data:
namespace = data['namespace']
else:
namespace = ''
if len(namespace) > 0:
push_ns = f'namespace {namespace} {{\n'
pop_ns = '\n}\n'
else:
push_ns = ''
pop_ns = ''
cpp = '// Generated\n\n#include <ox/std/array.hpp>\n'
cpp += '\n#include <ox/std/span.hpp>\n\n'
hpp = '// Generated\n\n#include <ox/std/span.hpp>\n\n'
cpp += push_ns
hpp += push_ns
all_files_func_decl = ''
all_files_func = ''
if all_files is not None:
all_files_func_decl += f'\n[[nodiscard]]\nox::Vector<ox::SpanView<uint8_t>> {all_files}() noexcept;\n'
all_files_func += f'\nox::Vector<ox::SpanView<uint8_t>> {all_files}() noexcept {{\n\treturn {{\n'
for f in data['files']:
if 'path' not in f:
print('src file path missing', file=sys.stderr)
sys.exit(1)
path = f['path']
if 'cpp_name' not in f:
print('var name missing', file=sys.stderr)
sys.exit(1)
cpp_name = f['cpp_name']
c, h = file_to_cpp(os.path.join(base_path, path), cpp_name)
cpp += c
if len(h) > 0:
hpp += h
if all_files is not None:
all_files_func += f'\t\t{cpp_name}(),\n'
cpp += all_files_func + '\t};\n}\n'
hpp += all_files_func_decl
cpp += pop_ns
hpp += pop_ns
write_txt_file(os.path.join(base_path, data['cpp']), cpp)
if 'hpp' in data:
write_txt_file(os.path.join(base_path, data['hpp']), hpp)
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument('--file', help='path to file', required=True)
parser.add_argument('--cpp', help='path to output cpp file', required=True)
parser.add_argument('--hpp', help='path to output hpp file')
parser.add_argument('--namespace', help='path to output hpp file')
parser.add_argument('--var-name', help='path to output hpp file', required=True)
parser.add_argument('--rsrc', help='path to file', required=True)
args = parser.parse_args()
file_to_cpp(args.file, args.var_name, args.namespace, args.cpp, args.hpp)
proc_rsrc_file(args.rsrc)
return 0