Skip to content

Commit

Permalink
Merge pull request #97 from Daft-Freak/standalone-meta
Browse files Browse the repository at this point in the history
Standalone metadata output
  • Loading branch information
Gadgetoid authored Sep 27, 2022
2 parents 2209fbc + d696705 commit 9b12852
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
32 changes: 32 additions & 0 deletions src/tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -82,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

Expand All @@ -93,6 +114,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

Expand Down
19 changes: 15 additions & 4 deletions src/ttblit/tool/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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')

Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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)

0 comments on commit 9b12852

Please sign in to comment.