[FL-2183] [FL-2209] Dolphin Deeds, Level up, assets generation, refactoring (#965)
* Desktop: cleanup headers * Get loader pubsub via record * [FL-2183] Dolphin refactoring 2022.01 * Restruct animations assets structure * Rename assets * Cleanup headers * Update Recording animation * Add BadBattery animation * Provide loader's pubsub via record * Fix load/unload animations * Scripts: add flipper format support, initial dolphin packager rework. Assets: internal and external dolphin. * Sync internal meta.txt and manifest.txt * Reorder, rename dolphin assets * Split essential generated assets * Add ReadMe for dolphin assets * Separate essential blocking animations * Scripts: full dolphin validation before packaging * Assets, Scripts: dolphin external resources packer * Github: update codeowners * Scripts: proper slots handling in dolphin animation meta * Scripts: correct frames enumeration and fix compiled assets. * [FL-2209] Add Dolphin Deeds points and many more * Remove excess frame_rate * Change dolphin assets directory * Scripts: add internal resource support to dolphin compiler * Scripts: add internal assets generation, renaming * Scripts: correct assert, renaming * Code cleanup, documentation, fixes * Update Levelup animations * Rename essential -> blocking * Fix Unlocked hint * Scripts: rewrite Templite compiller, replace regexps with token parser, split block types into code and variable blocks. Update dolphin templates. * Documentation: add key combos description and use information * Scripts: cleanup templit, more debug info and add dev comment Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from flipper.app import App
|
||||
|
||||
import logging
|
||||
import argparse
|
||||
import subprocess
|
||||
@@ -25,16 +27,14 @@ ICONS_TEMPLATE_C_DATA = "const uint8_t *{name}[] = {data};\n"
|
||||
ICONS_TEMPLATE_C_ICONS = "const Icon {name} = {{.width={width},.height={height},.frame_count={frame_count},.frame_rate={frame_rate},.frames=_{name}}};\n"
|
||||
|
||||
|
||||
class Main:
|
||||
def __init__(self):
|
||||
class Main(App):
|
||||
def init(self):
|
||||
# command args
|
||||
self.parser = argparse.ArgumentParser()
|
||||
self.parser.add_argument("-d", "--debug", action="store_true", help="Debug")
|
||||
self.subparsers = self.parser.add_subparsers(help="sub-command help")
|
||||
self.parser_icons = self.subparsers.add_parser(
|
||||
"icons", help="Process icons and build icon registry"
|
||||
)
|
||||
self.parser_icons.add_argument("source_directory", help="Source directory")
|
||||
self.parser_icons.add_argument("input_directory", help="Source directory")
|
||||
self.parser_icons.add_argument("output_directory", help="Output directory")
|
||||
self.parser_icons.set_defaults(func=self.icons)
|
||||
|
||||
@@ -56,30 +56,49 @@ class Main:
|
||||
"dolphin", help="Assemble dolphin resources"
|
||||
)
|
||||
self.parser_dolphin.add_argument(
|
||||
"dolphin_sources", help="Doplhin sources directory"
|
||||
"-s",
|
||||
"--symbol-name",
|
||||
help="Symbol and file name in dolphin output directory",
|
||||
default=None,
|
||||
)
|
||||
self.parser_dolphin.add_argument(
|
||||
"dolphin_output", help="Doplhin output directory"
|
||||
"input_directory", help="Dolphin source directory"
|
||||
)
|
||||
self.parser_dolphin.add_argument(
|
||||
"output_directory", help="Dolphin output directory"
|
||||
)
|
||||
self.parser_dolphin.set_defaults(func=self.dolphin)
|
||||
|
||||
# logging
|
||||
self.logger = logging.getLogger()
|
||||
def _icon2header(self, file):
|
||||
output = subprocess.check_output(["convert", file, "xbm:-"])
|
||||
assert output
|
||||
f = io.StringIO(output.decode().strip())
|
||||
width = int(f.readline().strip().split(" ")[2])
|
||||
height = int(f.readline().strip().split(" ")[2])
|
||||
data = f.read().strip().replace("\n", "").replace(" ", "").split("=")[1][:-1]
|
||||
data_bin_str = data[1:-1].replace(",", " ").replace("0x", "")
|
||||
data_bin = bytearray.fromhex(data_bin_str)
|
||||
# Encode icon data with LZSS
|
||||
data_encoded_str = subprocess.check_output(
|
||||
["heatshrink", "-e", "-w8", "-l4"], input=data_bin
|
||||
)
|
||||
assert data_encoded_str
|
||||
data_enc = bytearray(data_encoded_str)
|
||||
data_enc = bytearray([len(data_enc) & 0xFF, len(data_enc) >> 8]) + data_enc
|
||||
# Use encoded data only if its lenght less than original, including header
|
||||
if len(data_enc) < len(data_bin) + 1:
|
||||
data = (
|
||||
"{0x01,0x00,"
|
||||
+ "".join("0x{:02x},".format(byte) for byte in data_enc)
|
||||
+ "}"
|
||||
)
|
||||
else:
|
||||
data = "{0x00," + data[1:]
|
||||
return width, height, data
|
||||
|
||||
def __call__(self):
|
||||
self.args = self.parser.parse_args()
|
||||
if "func" not in self.args:
|
||||
self.parser.error("Choose something to do")
|
||||
# configure log output
|
||||
self.log_level = logging.DEBUG if self.args.debug else logging.INFO
|
||||
self.logger.setLevel(self.log_level)
|
||||
self.handler = logging.StreamHandler(sys.stdout)
|
||||
self.handler.setLevel(self.log_level)
|
||||
self.formatter = logging.Formatter("%(asctime)s [%(levelname)s] %(message)s")
|
||||
self.handler.setFormatter(self.formatter)
|
||||
self.logger.addHandler(self.handler)
|
||||
# execute requested function
|
||||
self.args.func()
|
||||
def _iconIsSupported(self, filename):
|
||||
extension = filename.lower().split(".")[-1]
|
||||
return extension in ICONS_SUPPORTED_FORMATS
|
||||
|
||||
def icons(self):
|
||||
self.logger.debug(f"Converting icons")
|
||||
@@ -87,7 +106,7 @@ class Main:
|
||||
icons_c.write(ICONS_TEMPLATE_C_HEADER)
|
||||
icons = []
|
||||
# Traverse icons tree, append image data to source file
|
||||
for dirpath, dirnames, filenames in os.walk(self.args.source_directory):
|
||||
for dirpath, dirnames, filenames in os.walk(self.args.input_directory):
|
||||
self.logger.debug(f"Processing directory {dirpath}")
|
||||
dirnames.sort()
|
||||
filenames.sort()
|
||||
@@ -105,10 +124,10 @@ class Main:
|
||||
if filename == "frame_rate":
|
||||
frame_rate = int(open(fullfilename, "r").read().strip())
|
||||
continue
|
||||
elif not self.iconIsSupported(filename):
|
||||
elif not self._iconIsSupported(filename):
|
||||
continue
|
||||
self.logger.debug(f"Processing animation frame {filename}")
|
||||
temp_width, temp_height, data = self.icon2header(fullfilename)
|
||||
temp_width, temp_height, data = self._icon2header(fullfilename)
|
||||
if width is None:
|
||||
width = temp_width
|
||||
if height is None:
|
||||
@@ -133,14 +152,14 @@ class Main:
|
||||
else:
|
||||
# process icons
|
||||
for filename in filenames:
|
||||
if not self.iconIsSupported(filename):
|
||||
if not self._iconIsSupported(filename):
|
||||
continue
|
||||
self.logger.debug(f"Processing icon {filename}")
|
||||
icon_name = "I_" + "_".join(filename.split(".")[:-1]).replace(
|
||||
"-", "_"
|
||||
)
|
||||
fullfilename = os.path.join(dirpath, filename)
|
||||
width, height, data = self.icon2header(fullfilename)
|
||||
width, height, data = self._icon2header(fullfilename)
|
||||
frame_name = f"_{icon_name}_0"
|
||||
icons_c.write(
|
||||
ICONS_TEMPLATE_C_FRAME.format(name=frame_name, data=data)
|
||||
@@ -172,37 +191,7 @@ class Main:
|
||||
for name, width, height, frame_rate, frame_count in icons:
|
||||
icons_h.write(ICONS_TEMPLATE_H_ICON_NAME.format(name=name))
|
||||
self.logger.debug(f"Done")
|
||||
|
||||
def icon2header(self, file):
|
||||
output = subprocess.check_output(["convert", file, "xbm:-"])
|
||||
assert output
|
||||
f = io.StringIO(output.decode().strip())
|
||||
width = int(f.readline().strip().split(" ")[2])
|
||||
height = int(f.readline().strip().split(" ")[2])
|
||||
data = f.read().strip().replace("\n", "").replace(" ", "").split("=")[1][:-1]
|
||||
data_bin_str = data[1:-1].replace(",", " ").replace("0x", "")
|
||||
data_bin = bytearray.fromhex(data_bin_str)
|
||||
# Encode icon data with LZSS
|
||||
data_encoded_str = subprocess.check_output(
|
||||
["heatshrink", "-e", "-w8", "-l4"], input=data_bin
|
||||
)
|
||||
assert data_encoded_str
|
||||
data_enc = bytearray(data_encoded_str)
|
||||
data_enc = bytearray([len(data_enc) & 0xFF, len(data_enc) >> 8]) + data_enc
|
||||
# Use encoded data only if its lenght less than original, including header
|
||||
if len(data_enc) < len(data_bin) + 1:
|
||||
data = (
|
||||
"{0x01,0x00,"
|
||||
+ "".join("0x{:02x},".format(byte) for byte in data_enc)
|
||||
+ "}"
|
||||
)
|
||||
else:
|
||||
data = "{0x00," + data[1:]
|
||||
return width, height, data
|
||||
|
||||
def iconIsSupported(self, filename):
|
||||
extension = filename.lower().split(".")[-1]
|
||||
return extension in ICONS_SUPPORTED_FORMATS
|
||||
return 0
|
||||
|
||||
def manifest(self):
|
||||
from flipper.assets.manifest import Manifest
|
||||
@@ -233,17 +222,33 @@ class Main:
|
||||
self.logger.info(f"Only in new: {record}")
|
||||
self.logger.info(f"Complete")
|
||||
|
||||
return 0
|
||||
|
||||
def copro(self):
|
||||
from flipper.assets.copro import Copro
|
||||
|
||||
self.logger.info(f"Bundling coprocessor binaries")
|
||||
copro = Copro(self.args.mcu)
|
||||
self.logger.info(f"Loading CUBE info")
|
||||
copro.loadCubeInfo(self.args.cube_dir)
|
||||
self.logger.info(f"Bundling")
|
||||
copro.bundle(self.args.output_dir)
|
||||
self.logger.info(f"Complete")
|
||||
|
||||
return 0
|
||||
|
||||
def dolphin(self):
|
||||
from flipper.assets.dolphin import pack_dolphin
|
||||
from flipper.assets.dolphin import Dolphin
|
||||
|
||||
pack_dolphin(self.args.dolphin_sources, self.args.dolphin_output)
|
||||
self.logger.info(f"Processing Dolphin sources")
|
||||
dolphin = Dolphin()
|
||||
self.logger.info(f"Loading data")
|
||||
dolphin.load(self.args.input_directory)
|
||||
self.logger.info(f"Packing")
|
||||
dolphin.pack(self.args.output_directory, self.args.symbol_name)
|
||||
self.logger.info(f"Complete")
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
@@ -3,53 +3,361 @@ import logging
|
||||
import os
|
||||
import sys
|
||||
import shutil
|
||||
from collections import Counter
|
||||
|
||||
from flipper.utils.fff import *
|
||||
from flipper.utils.templite import *
|
||||
from .icon import *
|
||||
|
||||
|
||||
def _pack_animation(pair: set):
|
||||
source, destination = pair
|
||||
image = file2image(source)
|
||||
image.write(destination)
|
||||
def _convert_image_to_bm(pair: set):
|
||||
source_filename, destination_filename = pair
|
||||
image = file2image(source_filename)
|
||||
image.write(destination_filename)
|
||||
|
||||
|
||||
def pack_animations(source: str, destination: str):
|
||||
assert os.path.exists(source)
|
||||
# Create list for processing
|
||||
to_pack = []
|
||||
dirpath, dirnames = next(os.walk(source))[:2]
|
||||
dirnames.sort()
|
||||
for dirname in dirnames:
|
||||
current_directory = os.path.join(dirpath, dirname)
|
||||
# Ensure destination folder
|
||||
destination_directory = os.path.join(destination, dirname)
|
||||
os.makedirs(destination_directory, exist_ok=True)
|
||||
# Find all files
|
||||
filenames = next(os.walk(current_directory))[2]
|
||||
filenames.sort()
|
||||
for filename in filenames:
|
||||
if is_file_an_icon(filename):
|
||||
source_filename = os.path.join(current_directory, filename)
|
||||
destination_filename = os.path.join(
|
||||
destination_directory, os.path.splitext(filename)[0] + ".bm"
|
||||
def _convert_image(source_filename: str):
|
||||
image = file2image(source_filename)
|
||||
return image.data
|
||||
|
||||
|
||||
class DolphinBubbleAnimation:
|
||||
|
||||
FILE_TYPE = "Flipper Animation"
|
||||
FILE_VERSION = 1
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
name: str,
|
||||
min_butthurt: int,
|
||||
max_butthurt: int,
|
||||
min_level: int,
|
||||
max_level: int,
|
||||
weight: int,
|
||||
):
|
||||
# Manifest
|
||||
self.name = name
|
||||
self.min_butthurt = min_butthurt
|
||||
self.max_butthurt = max_butthurt
|
||||
self.min_level = min_level
|
||||
self.max_level = max_level
|
||||
self.weight = weight
|
||||
# Meta and data
|
||||
self.meta = {}
|
||||
self.frames = []
|
||||
self.bubbles = []
|
||||
self.bubble_slots = None
|
||||
# Logging
|
||||
self.logger = logging.getLogger("DolphinBubbleAnimation")
|
||||
|
||||
def load(self, animation_directory: str):
|
||||
if not os.path.isdir(animation_directory):
|
||||
raise Exception(f"Animation folder doesn't exists: { animation_directory }")
|
||||
|
||||
meta_filename = os.path.join(animation_directory, "meta.txt")
|
||||
if not os.path.isfile(meta_filename):
|
||||
raise Exception(f"Animation meta file doesn't exists: { meta_filename }")
|
||||
|
||||
self.logger.info(f"Loading meta from {meta_filename}")
|
||||
file = FlipperFormatFile()
|
||||
file.load(meta_filename)
|
||||
|
||||
# Check file header
|
||||
filetype, version = file.getHeader()
|
||||
assert filetype == self.FILE_TYPE
|
||||
assert version == self.FILE_VERSION
|
||||
|
||||
max_frame_number = None
|
||||
unique_frames = None
|
||||
total_frames_count = None
|
||||
|
||||
try:
|
||||
# Main meta
|
||||
self.meta["Width"] = file.readKeyInt("Width")
|
||||
self.meta["Height"] = file.readKeyInt("Height")
|
||||
self.meta["Passive frames"] = file.readKeyInt("Passive frames")
|
||||
self.meta["Active frames"] = file.readKeyInt("Active frames")
|
||||
self.meta["Frames order"] = file.readKeyIntArray("Frames order")
|
||||
self.meta["Active cycles"] = file.readKeyInt("Active cycles")
|
||||
self.meta["Frame rate"] = file.readKeyInt("Frame rate")
|
||||
self.meta["Duration"] = file.readKeyInt("Duration")
|
||||
self.meta["Active cooldown"] = file.readKeyInt("Active cooldown")
|
||||
self.bubble_slots = file.readKeyInt("Bubble slots")
|
||||
|
||||
# Sanity Check
|
||||
assert self.meta["Width"] > 0 and self.meta["Width"] <= 128
|
||||
assert self.meta["Height"] > 0 and self.meta["Height"] <= 128
|
||||
assert self.meta["Passive frames"] > 0
|
||||
assert self.meta["Active frames"] >= 0
|
||||
assert self.meta["Frames order"]
|
||||
if self.meta["Active frames"] > 0:
|
||||
assert self.meta["Active cooldown"] > 0
|
||||
assert self.meta["Active cycles"] > 0
|
||||
else:
|
||||
assert self.meta["Active cooldown"] == 0
|
||||
assert self.meta["Active cycles"] == 0
|
||||
assert self.meta["Frame rate"] > 0
|
||||
assert self.meta["Duration"] >= 0
|
||||
|
||||
# Frames sanity check
|
||||
max_frame_number = max(self.meta["Frames order"])
|
||||
ordered_frames_count = len(self.meta["Frames order"])
|
||||
for i in range(max_frame_number + 1):
|
||||
frame_filename = os.path.join(animation_directory, f"frame_{i}.png")
|
||||
assert os.path.isfile(frame_filename)
|
||||
self.frames.append(frame_filename)
|
||||
# Sanity check
|
||||
unique_frames = set(self.meta["Frames order"])
|
||||
unique_frames_count = len(unique_frames)
|
||||
if unique_frames_count != max_frame_number + 1:
|
||||
self.logger.warning(f"Not all frames were used in {self.name}")
|
||||
total_frames_count = self.meta["Passive frames"] + (
|
||||
self.meta["Active frames"] * self.meta["Active cycles"]
|
||||
)
|
||||
|
||||
# Extra checks
|
||||
assert self.meta["Passive frames"] <= total_frames_count
|
||||
assert self.meta["Active frames"] <= total_frames_count
|
||||
assert (
|
||||
self.meta["Passive frames"] + self.meta["Active frames"]
|
||||
== ordered_frames_count
|
||||
)
|
||||
except EOFError as e:
|
||||
raise Exception("Invalid meta file: too short")
|
||||
except AssertionError as e:
|
||||
self.logger.exception(e)
|
||||
self.logger.error(f"Animation {self.name} got incorrect meta")
|
||||
raise Exception("Meta file is invalid: incorrect data")
|
||||
|
||||
# Bubbles
|
||||
while True:
|
||||
try:
|
||||
# Bubble data
|
||||
bubble = {}
|
||||
bubble["Slot"] = file.readKeyInt("Slot")
|
||||
bubble["X"] = file.readKeyInt("X")
|
||||
bubble["Y"] = file.readKeyInt("Y")
|
||||
bubble["Text"] = file.readKey("Text")
|
||||
bubble["AlignH"] = file.readKey("AlignH")
|
||||
bubble["AlignV"] = file.readKey("AlignV")
|
||||
bubble["StartFrame"] = file.readKeyInt("StartFrame")
|
||||
bubble["EndFrame"] = file.readKeyInt("EndFrame")
|
||||
|
||||
# Sanity check
|
||||
assert bubble["Slot"] <= self.bubble_slots
|
||||
assert bubble["X"] >= 0 and bubble["X"] < 128
|
||||
assert bubble["Y"] >= 0 and bubble["Y"] < 128
|
||||
assert len(bubble["Text"]) > 0
|
||||
assert bubble["AlignH"] in ["Left", "Center", "Right"]
|
||||
assert bubble["AlignV"] in ["Bottom", "Center", "Top"]
|
||||
assert bubble["StartFrame"] < total_frames_count
|
||||
assert bubble["EndFrame"] < total_frames_count
|
||||
assert bubble["EndFrame"] >= bubble["StartFrame"]
|
||||
|
||||
# Store bubble
|
||||
self.bubbles.append(bubble)
|
||||
except AssertionError as e:
|
||||
self.logger.exception(e)
|
||||
self.logger.error(
|
||||
f"Animation {self.name} bubble slot {bubble_slot} got incorrect data: {bubble}"
|
||||
)
|
||||
to_pack.append((source_filename, destination_filename))
|
||||
elif filename == "meta.txt":
|
||||
source_filename = os.path.join(current_directory, filename)
|
||||
destination_filename = os.path.join(destination_directory, filename)
|
||||
shutil.copyfile(source_filename, destination_filename)
|
||||
raise Exception("Meta file is invalid: incorrect bubble data")
|
||||
except EOFError:
|
||||
break
|
||||
|
||||
# Process images in parallel
|
||||
pool = multiprocessing.Pool()
|
||||
pool.map(_pack_animation, to_pack)
|
||||
def prepare(self):
|
||||
bubbles_in_slots = Counter([bubble["Slot"] for bubble in self.bubbles])
|
||||
|
||||
shutil.copyfile(
|
||||
os.path.join(source, "manifest.txt"), os.path.join(destination, "manifest.txt")
|
||||
last_slot = -1
|
||||
bubble_index = 0
|
||||
for bubble in self.bubbles:
|
||||
slot = bubble["Slot"]
|
||||
if slot == last_slot:
|
||||
bubble_index += 1
|
||||
else:
|
||||
last_slot = slot
|
||||
bubble_index = 0
|
||||
bubble["_BubbleIndex"] = bubble_index
|
||||
|
||||
bubbles_in_slots[slot] -= 1
|
||||
if bubbles_in_slots[slot] != 0:
|
||||
bubble["_NextBubbleIndex"] = bubble_index + 1
|
||||
|
||||
def save(self, output_directory: str):
|
||||
animation_directory = os.path.join(output_directory, self.name)
|
||||
os.makedirs(animation_directory, exist_ok=True)
|
||||
meta_filename = os.path.join(animation_directory, "meta.txt")
|
||||
|
||||
file = FlipperFormatFile()
|
||||
file.setHeader(self.FILE_TYPE, self.FILE_VERSION)
|
||||
file.writeEmptyLine()
|
||||
|
||||
# Write meta data
|
||||
file.writeKey("Width", self.meta["Width"])
|
||||
file.writeKey("Height", self.meta["Height"])
|
||||
file.writeKey("Passive frames", self.meta["Passive frames"])
|
||||
file.writeKey("Active frames", self.meta["Active frames"])
|
||||
file.writeKey("Frames order", self.meta["Frames order"])
|
||||
file.writeKey("Active cycles", self.meta["Active cycles"])
|
||||
file.writeKey("Frame rate", self.meta["Frame rate"])
|
||||
file.writeKey("Duration", self.meta["Duration"])
|
||||
file.writeKey("Active cooldown", self.meta["Active cooldown"])
|
||||
file.writeEmptyLine()
|
||||
|
||||
file.writeKey("Bubble slots", self.bubble_slots)
|
||||
file.writeEmptyLine()
|
||||
|
||||
# Write bubble data
|
||||
for bubble in self.bubbles:
|
||||
file.writeKey("Slot", bubble["Slot"])
|
||||
file.writeKey("X", bubble["X"])
|
||||
file.writeKey("Y", bubble["Y"])
|
||||
file.writeKey("Text", bubble["Text"])
|
||||
file.writeKey("AlignH", bubble["AlignH"])
|
||||
file.writeKey("AlignV", bubble["AlignV"])
|
||||
file.writeKey("StartFrame", bubble["StartFrame"])
|
||||
file.writeKey("EndFrame", bubble["EndFrame"])
|
||||
file.writeEmptyLine()
|
||||
|
||||
file.save(meta_filename)
|
||||
|
||||
to_pack = []
|
||||
for index, frame in enumerate(self.frames):
|
||||
to_pack.append(
|
||||
(frame, os.path.join(animation_directory, f"frame_{index}.bm"))
|
||||
)
|
||||
|
||||
pool = multiprocessing.Pool()
|
||||
pool.map(_convert_image_to_bm, to_pack)
|
||||
|
||||
def process(self):
|
||||
pool = multiprocessing.Pool()
|
||||
self.frames = pool.map(_convert_image, self.frames)
|
||||
|
||||
|
||||
class DolphinManifest:
|
||||
|
||||
FILE_TYPE = "Flipper Animation Manifest"
|
||||
FILE_VERSION = 1
|
||||
|
||||
TEMPLATE_DIRECTORY = os.path.join(
|
||||
os.path.dirname(os.path.realpath(__file__)), "templates"
|
||||
)
|
||||
TEMPLATE_H = os.path.join(TEMPLATE_DIRECTORY, "dolphin.h.tmpl")
|
||||
TEMPLATE_C = os.path.join(TEMPLATE_DIRECTORY, "dolphin.c.tmpl")
|
||||
|
||||
def __init__(self):
|
||||
self.animations = []
|
||||
self.logger = logging.getLogger("DolphinManifest")
|
||||
|
||||
def load(self, source_directory: str):
|
||||
manifest_filename = os.path.join(source_directory, "manifest.txt")
|
||||
|
||||
file = FlipperFormatFile()
|
||||
file.load(manifest_filename)
|
||||
|
||||
# Check file header
|
||||
filetype, version = file.getHeader()
|
||||
assert filetype == self.FILE_TYPE
|
||||
assert version == self.FILE_VERSION
|
||||
|
||||
# Load animation data
|
||||
while True:
|
||||
try:
|
||||
# Read animation spcification
|
||||
name = file.readKey("Name")
|
||||
min_butthurt = file.readKeyInt("Min butthurt")
|
||||
max_butthurt = file.readKeyInt("Max butthurt")
|
||||
min_level = file.readKeyInt("Min level")
|
||||
max_level = file.readKeyInt("Max level")
|
||||
weight = file.readKeyInt("Weight")
|
||||
|
||||
assert len(name) > 0
|
||||
assert min_butthurt >= 0
|
||||
assert max_butthurt >= 0 and max_butthurt >= min_butthurt
|
||||
assert min_level >= 0
|
||||
assert max_level >= 0 and max_level >= min_level
|
||||
assert weight >= 0
|
||||
|
||||
# Initialize animation
|
||||
animation = DolphinBubbleAnimation(
|
||||
name, min_butthurt, max_butthurt, min_level, max_level, weight
|
||||
)
|
||||
|
||||
# Load Animation meta and frames
|
||||
animation.load(os.path.join(source_directory, name))
|
||||
|
||||
# Add to array
|
||||
self.animations.append(animation)
|
||||
except EOFError:
|
||||
break
|
||||
|
||||
def _renderTemplate(self, template_filename: str, output_filename: str, **kwargs):
|
||||
template = Templite(filename=template_filename)
|
||||
output = template.render(**kwargs)
|
||||
open(output_filename, "w").write(output)
|
||||
|
||||
def save2code(self, output_directory: str, symbol_name: str):
|
||||
# Process frames
|
||||
for animation in self.animations:
|
||||
animation.process()
|
||||
|
||||
# Prepare substitution data
|
||||
for animation in self.animations:
|
||||
animation.prepare()
|
||||
|
||||
# Render Header
|
||||
self._renderTemplate(
|
||||
self.TEMPLATE_H,
|
||||
os.path.join(output_directory, f"assets_{symbol_name}.h"),
|
||||
animations=self.animations,
|
||||
symbol_name=symbol_name,
|
||||
)
|
||||
# Render Source
|
||||
self._renderTemplate(
|
||||
self.TEMPLATE_C,
|
||||
os.path.join(output_directory, f"assets_{symbol_name}.c"),
|
||||
animations=self.animations,
|
||||
symbol_name=symbol_name,
|
||||
)
|
||||
|
||||
def save2folder(self, output_directory: str):
|
||||
manifest_filename = os.path.join(output_directory, "manifest.txt")
|
||||
file = FlipperFormatFile()
|
||||
file.setHeader(self.FILE_TYPE, self.FILE_VERSION)
|
||||
file.writeEmptyLine()
|
||||
|
||||
for animation in self.animations:
|
||||
file.writeKey("Name", animation.name)
|
||||
file.writeKey("Min butthurt", animation.min_butthurt)
|
||||
file.writeKey("Max butthurt", animation.max_butthurt)
|
||||
file.writeKey("Min level", animation.min_level)
|
||||
file.writeKey("Max level", animation.max_level)
|
||||
file.writeKey("Weight", animation.weight)
|
||||
file.writeEmptyLine()
|
||||
|
||||
animation.save(output_directory)
|
||||
|
||||
file.save(manifest_filename)
|
||||
|
||||
def save(self, output_directory: str, symbol_name: str):
|
||||
os.makedirs(output_directory, exist_ok=True)
|
||||
if symbol_name:
|
||||
self.save2code(output_directory, symbol_name)
|
||||
else:
|
||||
self.save2folder(output_directory)
|
||||
|
||||
|
||||
def pack_dolphin(source_directory: str, destination_directory: str):
|
||||
pack_animations(
|
||||
os.path.join(source_directory, "animations"),
|
||||
os.path.join(destination_directory, "animations"),
|
||||
)
|
||||
class Dolphin:
|
||||
def __init__(self):
|
||||
self.manifest = DolphinManifest()
|
||||
self.logger = logging.getLogger("Dolphin")
|
||||
|
||||
def load(self, source_directory: str):
|
||||
assert os.path.isdir(source_directory)
|
||||
# Load Manifest
|
||||
self.logger.info(f"Loading directory {source_directory}")
|
||||
self.manifest.load(source_directory)
|
||||
|
||||
def pack(self, output_directory: str, symbol_name: str = None):
|
||||
self.manifest.save(output_directory, symbol_name)
|
||||
|
92
scripts/flipper/assets/templates/dolphin.c.tmpl
Normal file
92
scripts/flipper/assets/templates/dolphin.c.tmpl
Normal file
@@ -0,0 +1,92 @@
|
||||
#include <assets_{{symbol_name}}.h>
|
||||
#include <desktop/animations/animation_storage_i.h>
|
||||
#include <desktop/animations/animation_manager.h>
|
||||
#include <gui/icon_i.h>
|
||||
|
||||
{% for animation in animations: %}
|
||||
{% for frame_number, frame in enumerate(animation.frames): %}
|
||||
const uint8_t _A_{{ animation.name }}_{{ frame_number }}[] = {
|
||||
{{ "%s" % ",".join("0x%x" % i for i in frame)}}
|
||||
};
|
||||
{% :endfor %}
|
||||
|
||||
const uint8_t *_A_{{animation.name}}[] = {
|
||||
{% for frame_number, frame in enumerate(animation.frames): %}
|
||||
_A_{{ animation.name }}_{{ frame_number }},
|
||||
{% :endfor %}
|
||||
};
|
||||
|
||||
{% if animation.bubble_slots > 0: %}
|
||||
{% for bubble in animation.bubbles: %}
|
||||
const FrameBubble {{ animation.name }}_bubble_{{ bubble["Slot"] }}_{{ bubble["_BubbleIndex"] }};
|
||||
{% :endfor %}
|
||||
|
||||
const FrameBubble* const {{animation.name}}_bubble_sequences[] = {
|
||||
{% for i in range(animation.bubble_slots): %}
|
||||
&{{animation.name}}_bubble_{{i}}_0,
|
||||
{% :endfor %}
|
||||
};
|
||||
|
||||
{% for bubble in animation.bubbles: %}
|
||||
{%
|
||||
if "_NextBubbleIndex" in bubble:
|
||||
next_bubble = f'&{animation.name}_bubble_{bubble["Slot"]}_{bubble["_NextBubbleIndex"]}'
|
||||
else:
|
||||
next_bubble = "NULL"
|
||||
%}
|
||||
const FrameBubble {{animation.name}}_bubble_{{bubble["Slot"]}}_{{bubble["_BubbleIndex"]}} = {
|
||||
.bubble = {
|
||||
.x = {{bubble["X"]}},
|
||||
.y = {{bubble["Y"]}},
|
||||
.text = "{{bubble["Text"]}}",
|
||||
.align_h = Align{{bubble["AlignH"]}},
|
||||
.align_v = Align{{bubble["AlignV"]}},
|
||||
},
|
||||
.start_frame = {{bubble["StartFrame"]}},
|
||||
.end_frame = {{bubble["EndFrame"]}},
|
||||
.next_bubble = {{next_bubble}},
|
||||
};
|
||||
{% :endfor %}
|
||||
{% :endif %}
|
||||
|
||||
const BubbleAnimation BA_{{animation.name}} = {
|
||||
.icon_animation = {
|
||||
.width = {{ animation.meta['Width'] }},
|
||||
.height = {{ animation.meta['Height'] }},
|
||||
.frame_count = {{ "%d" % len(animation.frames) }},
|
||||
.frame_rate = {{ animation.meta['Frame rate'] }},
|
||||
.frames = _A_{{ animation.name }}
|
||||
},
|
||||
.frame_order = { {{ "%s" % ", ".join(str(i) for i in animation.meta['Frames order']) }} },
|
||||
.passive_frames = {{ animation.meta['Passive frames'] }},
|
||||
.active_frames = {{ animation.meta['Active frames'] }},
|
||||
.active_cooldown = {{ animation.meta['Active cooldown'] }},
|
||||
.active_cycles = {{ animation.meta['Active cycles'] }},
|
||||
.duration = {{ animation.meta['Duration'] }},
|
||||
{% if animation.bubble_slots > 0: %}
|
||||
.frame_bubble_sequences = {{ animation.name }}_bubble_sequences,
|
||||
.frame_bubble_sequences_count = COUNT_OF({{ animation.name }}_bubble_sequences),
|
||||
{% :else: %}
|
||||
.frame_bubble_sequences = NULL,
|
||||
.frame_bubble_sequences_count = 0,
|
||||
{% :endif %}
|
||||
};
|
||||
{% :endfor %}
|
||||
|
||||
const StorageAnimation {{symbol_name}}[] = {
|
||||
{% for animation in animations: %}
|
||||
{
|
||||
.animation = &BA_{{animation.name}},
|
||||
.manifest_info = {
|
||||
.name = "{{animation.name}}",
|
||||
.min_butthurt = {{animation.min_butthurt}},
|
||||
.max_butthurt = {{animation.max_butthurt}},
|
||||
.min_level = {{animation.min_level}},
|
||||
.max_level = {{animation.max_level}},
|
||||
.weight = {{animation.weight}},
|
||||
}
|
||||
},
|
||||
{% :endfor %}
|
||||
};
|
||||
|
||||
const size_t {{symbol_name}}_size = COUNT_OF({{symbol_name}});
|
7
scripts/flipper/assets/templates/dolphin.h.tmpl
Normal file
7
scripts/flipper/assets/templates/dolphin.h.tmpl
Normal file
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
#include <desktop/animations/animation_storage_i.h>
|
||||
|
||||
extern const StorageAnimation {{ symbol_name }}[];
|
||||
extern const size_t {{ symbol_name }}_size;
|
103
scripts/flipper/utils/fff.py
Normal file
103
scripts/flipper/utils/fff.py
Normal file
@@ -0,0 +1,103 @@
|
||||
import logging
|
||||
|
||||
|
||||
class FlipperFormatFile:
|
||||
def __init__(self):
|
||||
# Storage
|
||||
self.lines = []
|
||||
self.cursor = 0
|
||||
# Logger
|
||||
self.logger = logging.getLogger("FlipperFormatFile")
|
||||
|
||||
def _resetCursor(self):
|
||||
self.cursor = 0
|
||||
|
||||
def nextLine(self):
|
||||
line = None
|
||||
while self.cursor < len(self.lines):
|
||||
temp_line = self.lines[self.cursor].strip()
|
||||
self.cursor += 1
|
||||
if len(temp_line) > 0 and not temp_line.startswith("#"):
|
||||
line = temp_line
|
||||
break
|
||||
if line is None:
|
||||
raise EOFError()
|
||||
return line
|
||||
|
||||
def readKeyValue(self):
|
||||
line = self.nextLine()
|
||||
data = line.split(":", 1)
|
||||
if len(data) != 2:
|
||||
self.logger.error(f"Incorrectly formated line {self.cursor}: `{line}`")
|
||||
raise Exception("Unexpected line: not `key:value`")
|
||||
return data[0].strip(), data[1].strip()
|
||||
|
||||
def readKey(self, key: str):
|
||||
k, v = self.readKeyValue()
|
||||
if k != key:
|
||||
raise KeyError(f"Unexpected key {k} != {key}")
|
||||
return v
|
||||
|
||||
def readKeyInt(self, key: str):
|
||||
value = self.readKey(key)
|
||||
return int(value) if value else None
|
||||
|
||||
def readKeyIntArray(self, key: str):
|
||||
value = self.readKey(key)
|
||||
return [int(i) for i in value.split(" ")] if value else None
|
||||
|
||||
def readKeyFloat(self, key: str):
|
||||
value = self.readKey(key)
|
||||
return float(value) if value else None
|
||||
|
||||
def writeLine(self, line: str):
|
||||
self.lines.insert(self.cursor, line)
|
||||
self.cursor += 1
|
||||
|
||||
def writeKey(self, key: str, value):
|
||||
if isinstance(value, (str, int, float)):
|
||||
pass
|
||||
elif isinstance(value, (list, set)):
|
||||
value = " ".join(map(str, value))
|
||||
else:
|
||||
raise Exception("Unknown value type")
|
||||
self.writeLine(f"{key}: {value}")
|
||||
|
||||
def writeEmptyLine(self):
|
||||
self.writeLine("")
|
||||
|
||||
def writeComment(self, text: str):
|
||||
self.writeLine(f"# {text}")
|
||||
|
||||
def getHeader(self):
|
||||
if self.cursor != 0 and len(self.lines) == 0:
|
||||
raise Exception("Can't read header data: cursor not at 0 or file is empty")
|
||||
|
||||
# Read Filetype
|
||||
key, value = self.readKeyValue()
|
||||
if key != "Filetype":
|
||||
raise Exception("Invalid Header: missing `Filetype`")
|
||||
filetype = value
|
||||
|
||||
# Read Version
|
||||
key, value = self.readKeyValue()
|
||||
if key != "Version":
|
||||
raise Exception("Invalid Header: missing `Version`")
|
||||
version = int(value)
|
||||
|
||||
return filetype, version
|
||||
|
||||
def setHeader(self, filetype: str, version: int):
|
||||
if self.cursor != 0 and len(self.lines) != 0:
|
||||
raise Exception("Can't set header data: file is not empty")
|
||||
|
||||
self.writeKey("Filetype", filetype)
|
||||
self.writeKey("Version", version)
|
||||
|
||||
def load(self, filename: str):
|
||||
file = open(filename, "r")
|
||||
self.lines = file.readlines()
|
||||
|
||||
def save(self, filename: str):
|
||||
file = open(filename, "w")
|
||||
file.write("\n".join(self.lines))
|
196
scripts/flipper/utils/templite.py
Normal file
196
scripts/flipper/utils/templite.py
Normal file
@@ -0,0 +1,196 @@
|
||||
# Templite++
|
||||
# A light-weight, fully functional, general purpose templating engine
|
||||
# Proudly made of shit and sticks. Strictly not for production use.
|
||||
# Extremly unsafe and difficult to debug.
|
||||
#
|
||||
# Copyright (c) 2022 Flipper Devices
|
||||
# Author: Aleksandr Kutuzov <alletam@gmail.com>
|
||||
#
|
||||
# Copyright (c) 2009 joonis new media
|
||||
# Author: Thimo Kraemer <thimo.kraemer@joonis.de>
|
||||
#
|
||||
# Based on Templite by Tomer Filiba
|
||||
# http://code.activestate.com/recipes/496702/
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
# MA 02110-1301, USA.
|
||||
|
||||
|
||||
from enum import Enum
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
|
||||
class TempliteCompiler:
|
||||
class State(Enum):
|
||||
TEXT = 1
|
||||
CONTROL = 2
|
||||
VARIABLE = 3
|
||||
|
||||
def __init__(self, source: str, encoding: str):
|
||||
self.blocks = [f"# -*- coding: {encoding} -*-"]
|
||||
self.block = ""
|
||||
self.source = source
|
||||
self.cursor = 0
|
||||
self.offset = 0
|
||||
|
||||
def processText(self):
|
||||
self.block = self.block.replace("\\", "\\\\").replace('"', '\\"')
|
||||
self.block = "\t" * self.offset + f'write("""{self.block}""")'
|
||||
self.blocks.append(self.block)
|
||||
self.block = ""
|
||||
|
||||
def getLine(self):
|
||||
return self.source[: self.cursor].count("\n") + 1
|
||||
|
||||
def controlIsEnding(self):
|
||||
block_stripped = self.block.lstrip()
|
||||
if block_stripped.startswith(":"):
|
||||
if not self.offset:
|
||||
raise SyntaxError(
|
||||
f"Line: {self.getLine()}, no statement to terminate: `{block_stripped}`"
|
||||
)
|
||||
self.offset -= 1
|
||||
self.block = block_stripped[1:]
|
||||
if not self.block.endswith(":"):
|
||||
return True
|
||||
return False
|
||||
|
||||
def processControl(self):
|
||||
self.block = self.block.rstrip()
|
||||
|
||||
if self.controlIsEnding():
|
||||
self.block = ""
|
||||
return
|
||||
|
||||
lines = self.block.splitlines()
|
||||
margin = min(len(l) - len(l.lstrip()) for l in lines if l.strip())
|
||||
self.block = "\n".join("\t" * self.offset + l[margin:] for l in lines)
|
||||
self.blocks.append(self.block)
|
||||
if self.block.endswith(":"):
|
||||
self.offset += 1
|
||||
self.block = ""
|
||||
|
||||
def processVariable(self):
|
||||
self.block = self.block.strip()
|
||||
self.block = "\t" * self.offset + f"write({self.block})"
|
||||
self.blocks.append(self.block)
|
||||
self.block = ""
|
||||
|
||||
def compile(self):
|
||||
state = self.State.TEXT
|
||||
|
||||
# Process template source
|
||||
while self.cursor < len(self.source):
|
||||
# Process plain text till first token occurance
|
||||
if state == self.State.TEXT:
|
||||
if self.source[self.cursor :].startswith("{%"):
|
||||
state = self.State.CONTROL
|
||||
self.cursor += 1
|
||||
elif self.source[self.cursor :].startswith("{{"):
|
||||
state = self.State.VARIABLE
|
||||
self.cursor += 1
|
||||
else:
|
||||
self.block += self.source[self.cursor]
|
||||
# Commit self.block if token was found
|
||||
if state != self.State.TEXT:
|
||||
self.processText()
|
||||
elif state == self.State.CONTROL:
|
||||
if self.source[self.cursor :].startswith("%}"):
|
||||
self.cursor += 1
|
||||
state = self.State.TEXT
|
||||
self.processControl()
|
||||
else:
|
||||
self.block += self.source[self.cursor]
|
||||
elif state == self.State.VARIABLE:
|
||||
if self.source[self.cursor :].startswith("}}"):
|
||||
self.cursor += 1
|
||||
state = self.State.TEXT
|
||||
self.processVariable()
|
||||
else:
|
||||
self.block += self.source[self.cursor]
|
||||
else:
|
||||
raise Exception("Unknown State")
|
||||
|
||||
self.cursor += 1
|
||||
|
||||
if state != self.State.TEXT:
|
||||
raise Exception("Last self.block was not closed")
|
||||
|
||||
if self.block:
|
||||
self.processText()
|
||||
|
||||
return "\n".join(self.blocks)
|
||||
|
||||
|
||||
class Templite:
|
||||
cache = {}
|
||||
|
||||
def __init__(self, text=None, filename=None, encoding="utf-8", caching=False):
|
||||
"""Loads a template from string or file."""
|
||||
if filename:
|
||||
filename = os.path.abspath(filename)
|
||||
mtime = os.path.getmtime(filename)
|
||||
self.file = key = filename
|
||||
elif text is not None:
|
||||
self.file = mtime = None
|
||||
key = hash(text)
|
||||
else:
|
||||
raise ValueError("either text or filename required")
|
||||
# set attributes
|
||||
self.encoding = encoding
|
||||
self.caching = caching
|
||||
# check cache
|
||||
cache = self.cache
|
||||
if caching and key in cache and cache[key][0] == mtime:
|
||||
self._code = cache[key][1]
|
||||
return
|
||||
# read file
|
||||
if filename:
|
||||
with open(filename) as fh:
|
||||
text = fh.read()
|
||||
# Compile template to executable
|
||||
code = TempliteCompiler(text, self.encoding).compile()
|
||||
self._code = compile(code, self.file or "<string>", "exec")
|
||||
# Cache for future use
|
||||
if caching:
|
||||
cache[key] = (mtime, self._code)
|
||||
|
||||
def render(self, **namespace):
|
||||
"""Renders the template according to the given namespace."""
|
||||
stack = []
|
||||
namespace["__file__"] = self.file
|
||||
# add write method
|
||||
def write(*args):
|
||||
for value in args:
|
||||
stack.append(str(value))
|
||||
|
||||
namespace["write"] = write
|
||||
# add include method
|
||||
def include(file):
|
||||
if not os.path.isabs(file):
|
||||
if self.file:
|
||||
base = os.path.dirname(self.file)
|
||||
else:
|
||||
base = os.path.dirname(sys.argv[0])
|
||||
file = os.path.join(base, file)
|
||||
t = Templite(None, file, self.encoding, self.delimiters, self.caching)
|
||||
stack.append(t.render(**namespace))
|
||||
|
||||
namespace["include"] = include
|
||||
# execute template code
|
||||
exec(self._code, namespace)
|
||||
return "".join(stack)
|
Reference in New Issue
Block a user