From 0cb51030641c31abe91e9a1e40719429333de9e3 Mon Sep 17 00:00:00 2001 From: Charlie Birks Date: Thu, 27 May 2021 23:37:53 +0100 Subject: [PATCH 1/3] Support outputting a standalone metadata file --- src/ttblit/tool/metadata.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/ttblit/tool/metadata.py b/src/ttblit/tool/metadata.py index 0163866..b23bfc2 100644 --- a/src/ttblit/tool/metadata.py +++ b/src/ttblit/tool/metadata.py @@ -12,7 +12,8 @@ from ..asset.formatters.c import c_initializer from ..core.struct import (blit_game, blit_game_with_meta, blit_game_with_meta_and_relo, blit_icns, - struct_blit_image, struct_blit_image_bi, struct_blit_pixel) + struct_blit_image, struct_blit_image_bi, + struct_blit_meta_standalone, struct_blit_pixel) from ..core.yamlloader import YamlLoader @@ -123,7 +124,7 @@ def write_pico_bi_source(self, pico_bi_file, metadata): logging.info(f'Wrote pico-sdk binary info to {pico_bi_file}') - def run(self, config, icns, pico_bi, file, force, dump_images): + def run(self, config, icns, pico_bi, file, metadata_file, force, dump_images): if file is None and config is None: raise click.UsageError('the following arguments are required: --config and/or --file') @@ -215,6 +216,7 @@ def run(self, config, icns, pico_bi, file, force, dump_images): metadata = { + 'checksum': 0xFFFFFFFF, 'date': datetime.now().strftime("%Y%m%dT%H%M%S"), 'title': title, 'description': description, @@ -231,6 +233,14 @@ def run(self, config, icns, pico_bi, file, force, dump_images): if not pico_bi.is_file() or force: self.write_pico_bi_source(pico_bi, metadata) + # Standalone metadata + if metadata_file: + if metadata_file.exists() and not force: + logging.critical(f'Refusing to overwrite metadata in {metadata_file}') + return + + open(metadata_file, 'wb').write(struct_blit_meta_standalone.build({'data': metadata})) + # Add to the game file if not game: return @@ -263,7 +273,8 @@ def run(self, config, icns, pico_bi, file, force, dump_images): @click.option('--icns', type=pathlib.Path, help='macOS icon output file') @click.option('--pico-bi', type=pathlib.Path, help='pico-sdk binary info source file output') @click.option('--file', type=pathlib.Path, help='Input file') +@click.option('--metadata-file', type=pathlib.Path, help='Output standalone metadata file') @click.option('--force', is_flag=True, help='Force file overwrite') @click.option('--dump-images', is_flag=True, help='Dump images from metadata') -def metadata_cli(config, icns, pico_bi, file, force, dump_images): - Metadata().run(config, icns, pico_bi, file, force, dump_images) +def metadata_cli(config, icns, pico_bi, file, metadata_file, force, dump_images): + Metadata().run(config, icns, pico_bi, file, metadata_file, force, dump_images) From fbed258be3f75009fc1e6d507f579ac97af6b8d3 Mon Sep 17 00:00:00 2001 From: Charlie Birks Date: Fri, 9 Sep 2022 22:21:07 +0100 Subject: [PATCH 2/3] Standalone metadata test --- src/tests/test_metadata.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/tests/test_metadata.py b/src/tests/test_metadata.py index 5c3e205..ca45d3e 100644 --- a/src/tests/test_metadata.py +++ b/src/tests/test_metadata.py @@ -13,6 +13,12 @@ def test_bi_source_file(): temp_src = tempfile.NamedTemporaryFile('rb+', suffix='.cpp') return temp_src +@pytest.fixture +def test_blmeta_file(): + temp_src = tempfile.NamedTemporaryFile('rb+', suffix='.blmeta') + return temp_src + + @pytest.fixture def test_binary_file(): temp_bin = tempfile.NamedTemporaryFile('wb', suffix='.bin') @@ -93,6 +99,17 @@ def test_metadata_pico_bi(test_resources, test_binary_file, test_bi_source_file) '--force' ]) +def test_metadata_standalone(test_resources, test_binary_file, test_blmeta_file): + from ttblit import main + + with pytest.raises(SystemExit): + main([ + 'metadata', + '--config', str(test_resources / 'metadata-basic.yml'), + '--metadata-file', test_blmeta_file.name, + '--force' + ]) + def test_metadata_invalid_bin(test_resources, test_invalid_binary_file): from ttblit import main From d69670554a392e327dbe622703ab0b3b9c9e2852 Mon Sep 17 00:00:00 2001 From: Charlie Birks Date: Fri, 9 Sep 2022 23:24:59 +0100 Subject: [PATCH 3/3] Add a test for dumping images from metadata Coveralls made me do it. --- src/tests/test_metadata.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/tests/test_metadata.py b/src/tests/test_metadata.py index ca45d3e..e101bbd 100644 --- a/src/tests/test_metadata.py +++ b/src/tests/test_metadata.py @@ -88,6 +88,21 @@ def test_metadata_dump(test_resources): '--file', str(test_resources / 'doom-fire.blit') ]) +def test_metadata_dump_images(test_resources): + from ttblit import main + + file = test_resources / 'doom-fire.blit' + + with pytest.raises(SystemExit): + main([ + 'metadata', + '--file', str(file), + '--dump-images' + ]) + + assert open(file.with_suffix(".icon.png"), 'rb').read(4) == b'\x89PNG' + assert open(file.with_suffix(".splash.png"), 'rb').read(4) == b'\x89PNG' + def test_metadata_pico_bi(test_resources, test_binary_file, test_bi_source_file): from ttblit import main