jasper/util/scripts/file-to-cpp.py
Gary Talent ce53be9271 Squashed 'deps/nostalgia/' changes from 89ae226b..9676ea59
9676ea59 [turbine/glfw] Fix programmatic shutdown to invoke shutdownHandler
de8ac106 [turbine/glfw] Fix closing when no shutdown handler is set
88a6cd59 [turbine/glfw] Treat close window event like other events with regard to a mandatory refresh period
cd43fb7f [turbine,studio] Fix confirm app close pop up to work with Ctrl-Q
136f4224 [nostalgia] Update release notes
e773d6f0 [studio] Rename StudioContext to Context
7da2f68d [nostalgia/sample_project] Add assets
d20889ae [nostalgia/gfx/studio] Update for Ox changes
50c8302f [ox] Rename itoa to intToStr
d8195d30 [olympic,nostalgia] Address unsafe buffer warnings
a8c1387d [ox] Address unsafe buffer warnings
ff1e8f26 [studio] Add popup to warn about UUID duplication
d4329981 [studio,nostalgia] Cleanup
00034543 [studio,nostalgia/gfx/studio] Cleanup
8c6b2234 [olympic/util] Make pkg-gba script check return code of subprocesses
aad4b8a4 [studio] Cleanup
7cab1331 [keel] Add ability to log UUID duplication
640ac85d [nostalgia/gfx/studio/palette] Make page rename dialog accept on enter if input focused
b8d76586 [nostalgia/studio] Update generated icondata.cpp with Clang fix
2503bb3b [nostalgia/sample_project] Update type descriptors
e5dd448f [turbine,studio] Make Studio confirm with user before closing app if any unsaved changes
4770bb6a [olympic/util] Cleanup
c0bac696 [nostalgia/gfx/studio/paletteeditor] Fix color number key range
95f7c334 [studio] Change Studio font
535d8876 [keel] Cleanup
845e4332 [turbine] Fix Mac build
5169a607 [turbine] Disable useless window icon on Mac, it causes GLFW warning
8f03af99 [keel] Style updates
ee63a4a1 [keel] Cleanup

git-subtree-dir: deps/nostalgia
git-subtree-split: 9676ea59787215b01498dfa82f88d426363b3cfd
2025-05-07 00:11:20 -05:00

107 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},"
out += '\n\t' if i % 10 == 0 else ' '
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 const 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()
all_files = data['all_files'] if 'all_files' in data else None
namespace = data['namespace'] if 'namespace' in data else ''
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'
if all_files is not None:
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)