[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:
@@ -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;
|
Reference in New Issue
Block a user