jasper/util/scripts/file-to-cpp.py
Gary Talent 09b4a705a9 Squashed 'deps/nostalgia/' changes from ab11b885..3fe62464
3fe62464 [nostalgia/sample_project] Add NS_Logo32
db55fc72 [nostalgia/player] Cleanup
20944508 [studio] Cleanup
889bec04 [nostalgia/gfx/studio/tilesheet] Cleanup
ac1e34d4 [nostalgia] Update release notes
55ed75f4 [nostalgia/gfx/studio/tilesheet] Fix selection clearing to work when clicking outside image
2751872c [nostalgia] Cleanup file-to-cpp output
2a3cd35c [nostalgia] Fix release notes version, add d2025.02.1
b66f459f [nostalgia] Cleanup icon rsrc generation
3910f4e7 [nostalgia] Fix debug and gba-run commands in Makefile
c0e96216 [turbine] Make accessor functions take const ref to Context
f9512d72 [turbine/glfw] Fix implicit conversion
b7f2c169 [nostalgia/studio/gfx] Fix typo
1e5057d6 [nostalgia] Add app icon note to release notes
c6255e32 [nostalgia/studio] Add icon 16 src
02230ef6 [turbine,studio,nostalgia/studio] Add support for window icon scaling, expand icons sizes for Nostalgia Studio
9b6b60e4 [turbine] Cleanup
b9a26ab6 [turbine] Fix GLFWimage member init order
a521887d [studio,turbine] Add support for window icons
5ca7e2f2 [ox/fs] Cleanup
125a235d [ox/fs] Cleanup
91a7129f [nostalgia/gfx/keel] Cleanup
df48a232 [nostalgia/studio] Add icon to Windows executable

git-subtree-dir: deps/nostalgia
git-subtree-split: 3fe62464c3ee45055e8c732840f949cdf373ae2a
2025-04-16 19:48:37 -05:00

115 lines
3.3 KiB
Python
Executable File

#! /usr/bin/env python3
#
# Copyright 2016 - 2025 gary@drinkingtea.net
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
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}')
def file_to_hex(path: str, line_prefix: str) -> tuple[str, int]:
with open(path, 'rb') as f:
out = line_prefix
data = f.read()
i = 1
for b in data:
out += f"{b:#0{4}x},"
if i % 10 == 0:
out += '\n\t'
else:
out += ' '
i += 1
return out[:-1], len(data)
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 += '#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('--rsrc', help='path to file', required=True)
args = parser.parse_args()
proc_rsrc_file(args.rsrc)
return 0
if __name__ == '__main__':
try:
err = main()
sys.exit(err)
except KeyboardInterrupt:
sys.exit(1)