vxSDK/vxsdk/core/conv/asset.py

127 lines
3.5 KiB
Python

"""
Vhex assets converter
"""
import os
import toml
from core.logger import log
from core.conv.type.font import conv_font_generate
from core.conv.type.image import conv_image_generate
__all__ = [
'conv_assets_generate'
]
#---
# Private
#---
class _VxAsset():
"""Represent a asset object
This is an internal class which represents assets information with some
methods to abstract conversion and file type manipulation (for asset type
font and asset type bitmap).
Also note that this class is private because we use a tricky optimization to
parse the `vxconv.txt` file, this is why we have no "private" property with
setter and getter, and why this class is "hidden".
Some important methods to note:
================================== =========================================
Name Description
================================== =========================================
generate() Generate the source file (C)
================================== =========================================
"""
def __init__(self, prefix, name, meta):
if 'path' not in meta:
raise Exception(f"[{name}] missing required path information")
if 'type' not in meta:
raise Exception(f"[{name}] missing required type information")
if meta['type'] not in ['font', 'image']:
raise Exception(f"asset type '{meta[type]}' is not known")
self._name = name
self._meta = meta
self._type = meta['type']
self._path = prefix + '/' + meta['path']
if not os.path.exists(self.path):
raise Exception("asset path '{self._path}' cannot be openned")
def __repr__(self):
return f'<_VxAssetObj, {self.name}>'
def __str__(self):
content = f"[{self.name}]\n"
content += f" - type: {self.type}\n"
content += f" - path: {self.path}\n"
return content
#---
# Getter
#---
@property
def path(self):
"""<property> path"""
return self._path
@property
def name(self):
"""<property> name"""
return self._name
@property
def type(self):
"""<property> type"""
return self._type
@property
def meta(self):
"""<property> meta"""
return self._meta
#---
# Public method
#---
def generate_source_file(self, prefix_output):
"""generate source file """
if self.type == 'font':
return conv_font_generate(self, prefix_output)
return conv_image_generate(self, prefix_output)
#---
# Public
#---
def conv_assets_generate(prefix_assets, prefix_output):
""" Walk through the assets prefix and generate all source file
@args
> prefix_asset (str) - prefix used for recursivly search for asset info
> prefix_output (str) - prefix used for the output of generated file
@return
> a list of all generated sources pathname
"""
if not os.path.exists(prefix_output):
os.makedirs(prefix_output)
generated = []
for root, _, files in os.walk(prefix_assets):
if not 'vxconv.toml' in files:
continue
with open(root + '/vxconv.toml', "r", encoding='utf-8') as inf:
content = toml.loads(inf.read())
for asset_name in content:
log.user(f"converting {asset_name}...")
asset = _VxAsset(root, asset_name, content[asset_name])
generated += asset.generate_source_file(prefix_output)
return generated