fxconv: add custom conversions

Custom conversions can be used by:
* Providing a converters.py in the main directory with a convert()
  function
* Specifying --custom on the fxconv command-line or using a subfolder or
  assets-{fx,cg} unused by standard fxconv
This commit is contained in:
Lephenixnoir 2020-10-23 13:05:50 +02:00
parent e3af6a5d4b
commit ee9c459c69
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
3 changed files with 33 additions and 4 deletions

View File

@ -46,6 +46,12 @@ def err(msg):
def warn(msg):
print("\x1b[33;1mwarning:\x1b[0m", msg, file=sys.stderr)
# "converters" module from the user project
try:
import converters
except ImportError:
converters = None
def main():
# Default execution mode is to run a Python script for conversion
modes = "script binary image font bopti-image libimg-image"
@ -53,6 +59,7 @@ def main():
output = None
model = None
target = { 'toolchain': None, 'arch': None, 'section': None }
use_custom = False
# Parse command-line arguments
@ -61,8 +68,8 @@ def main():
sys.exit(1)
try:
opts, args = getopt.gnu_getopt(sys.argv[1:], "hsbifo:",
("help output= fx cg toolchain= arch= section= "+modes).split())
longs = "help output= fx cg toolchain= arch= section= custom " + modes
opts, args = getopt.gnu_getopt(sys.argv[1:], "hsbifo:", longs.split())
except getopt.GetoptError as error:
err(error)
sys.exit(1)
@ -85,6 +92,9 @@ def main():
target['arch'] = value
elif name == "--section":
target['section'] = value
elif name == "--custom":
use_custom = True
mode = "custom"
# Other names are modes
else:
mode = name[1] if len(name)==2 else name[2:]
@ -148,8 +158,16 @@ def main():
warn("type 'image' is deprecated, use 'bopti-image' instead")
params["type"] = "bopti-image"
# Use the custom module
custom = None
if use_custom:
if converters is None:
err("--custom specified but no [converters] module in wd")
sys.exit(1)
custom = converters.convert
try:
fxconv.convert(input, params, target, output, model)
fxconv.convert(input, params, target, output, model, custom)
except fxconv.FxconvError as e:
err(e)
sys.exit(1)

View File

@ -928,7 +928,7 @@ def r5g6b5(img, color_count=0, alpha=None):
return encoded, encoded_palette, alpha
def convert(input, params, target, output=None, model=None):
def convert(input, params, target, output=None, model=None, custom=None):
"""
Convert a data file into an object that exports the following symbols:
* _<varname>
@ -942,6 +942,7 @@ def convert(input, params, target, output=None, model=None):
target -- String dictionary keys 'toolchain', 'arch' and 'section'
output -- Output file name [default: <input> with suffix '.o']
model -- 'fx' or 'cg' (some conversions require this) [default: None]
custom -- Custom conversion function
Produces an output file and returns nothing.
"""
@ -969,6 +970,8 @@ def convert(input, params, target, output=None, model=None):
convert_libimg_fx(input, output, params, target)
elif params["type"] == "libimg-image" and model == "cg":
convert_libimg_cg(input, output, params, target)
elif custom is not None:
custom(input, output, params, target)
else:
raise FxconvError(f'unknown resource type \'{params["type"]}\'')

View File

@ -157,6 +157,14 @@ build-cg/assets/bin/%.o: assets-cg/bin/%
@ mkdir -p $(dir $@)
fxconv -b $< -o $@ $(FXCONVCG) name:bin_$(basename $*) $(BIN.$*)
# Custom conversions
build-fx/assets/%.o: assets-fx/%
@ mkdir -p $(dir $@)
fxconv --custom $< -o $@ $(FXCONVFX) type:$(subst /,,$(dir $*)) name:$(subst /,_,$(basename $*))
build-cg/assets/%.o: assets-cg/%
@ mkdir -p $(dir $@)
fxconv --custom $< -o $@ $(FXCONVCG) type:$(subst /,,$(dir $*)) name:$(subst /,_,$(basename $*))
#
# Cleaning and utilities
#