From ee9c459c69f215de929a0e071a4305e37d0ed7c6 Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Fri, 23 Oct 2020 13:05:50 +0200 Subject: [PATCH] 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 --- fxconv/fxconv-main.py | 24 +++++++++++++++++++++--- fxconv/fxconv.py | 5 ++++- fxsdk/assets/Makefile | 8 ++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/fxconv/fxconv-main.py b/fxconv/fxconv-main.py index 9258bd4..cba14eb 100755 --- a/fxconv/fxconv-main.py +++ b/fxconv/fxconv-main.py @@ -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) diff --git a/fxconv/fxconv.py b/fxconv/fxconv.py index 755ea96..9f57349 100644 --- a/fxconv/fxconv.py +++ b/fxconv/fxconv.py @@ -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: * _ @@ -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: 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"]}\'') diff --git a/fxsdk/assets/Makefile b/fxsdk/assets/Makefile index 9018671..83e5192 100755 --- a/fxsdk/assets/Makefile +++ b/fxsdk/assets/Makefile @@ -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 #