Unload asset pack icons for external apps

This commit is contained in:
Willy-JL
2023-05-28 17:04:33 +01:00
parent 33b8519202
commit 7051917d36
4 changed files with 54 additions and 11 deletions

View File

@@ -13,6 +13,7 @@
#include <loader/firmware_api/firmware_api.h> #include <loader/firmware_api/firmware_api.h>
#include <storage/storage_processing.h> #include <storage/storage_processing.h>
#include <applications/main/archive/helpers/favorite_timeout.h> #include <applications/main/archive/helpers/favorite_timeout.h>
#include <xtreme/private.h>
#define TAG "FapLoader" #define TAG "FapLoader"
@@ -243,6 +244,9 @@ static void fap_loader_free(FapLoader* loader) {
} }
int32_t fap_loader_app(char* p) { int32_t fap_loader_app(char* p) {
size_t start = furi_get_tick();
XTREME_ASSETS_FREE();
FURI_LOG_I("Assets", "Freed in %ums", (size_t)(furi_get_tick() - start));
FapLoader* loader; FapLoader* loader;
process_favorite_launch(&p); process_favorite_launch(&p);
if(p) { if(p) {
@@ -262,5 +266,9 @@ int32_t fap_loader_app(char* p) {
} }
fap_loader_free(loader); fap_loader_free(loader);
start = furi_get_tick();
XTREME_ASSETS_LOAD();
FURI_LOG_I("Assets", "Loaded in %ums", (size_t)(furi_get_tick() - start));
return 0; return 0;
} }

View File

@@ -12,4 +12,5 @@ struct Icon {
const uint8_t frame_count; const uint8_t frame_count;
const uint8_t frame_rate; const uint8_t frame_rate;
const uint8_t* const* frames; const uint8_t* const* frames;
Icon* original;
}; };

View File

@@ -9,7 +9,7 @@
#define ICONS_FMT XTREME_ASSETS_PATH "/%s/Icons/%s" #define ICONS_FMT XTREME_ASSETS_PATH "/%s/Icons/%s"
void swap_icon_animated(const Icon* replace, const char* name, FuriString* path, File* file) { void load_icon_animated(const Icon* replace, const char* name, FuriString* path, File* file) {
const char* pack = XTREME_SETTINGS()->asset_pack; const char* pack = XTREME_SETTINGS()->asset_pack;
furi_string_printf(path, ICONS_FMT "/meta", pack, name); furi_string_printf(path, ICONS_FMT "/meta", pack, name);
if(storage_file_open(file, furi_string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING)) { if(storage_file_open(file, furi_string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING)) {
@@ -41,10 +41,13 @@ void swap_icon_animated(const Icon* replace, const char* name, FuriString* path,
} }
if(i == frame_count) { if(i == frame_count) {
FURI_CONST_ASSIGN(replace->frame_count, frame_count); Icon* original = malloc(sizeof(Icon));
FURI_CONST_ASSIGN(replace->frame_rate, frame_rate); memcpy(original, replace, sizeof(Icon));
FURI_CONST_ASSIGN_PTR(replace->original, original);
FURI_CONST_ASSIGN(replace->width, icon_width); FURI_CONST_ASSIGN(replace->width, icon_width);
FURI_CONST_ASSIGN(replace->height, icon_height); FURI_CONST_ASSIGN(replace->height, icon_height);
FURI_CONST_ASSIGN(replace->frame_rate, frame_rate);
FURI_CONST_ASSIGN(replace->frame_count, frame_count);
FURI_CONST_ASSIGN_PTR(replace->frames, frames); FURI_CONST_ASSIGN_PTR(replace->frames, frames);
} else { } else {
for(; i >= 0; i--) { for(; i >= 0; i--) {
@@ -57,7 +60,7 @@ void swap_icon_animated(const Icon* replace, const char* name, FuriString* path,
storage_file_close(file); storage_file_close(file);
} }
void swap_icon_static(const Icon* replace, const char* name, FuriString* path, File* file) { void load_icon_static(const Icon* replace, const char* name, FuriString* path, File* file) {
furi_string_printf(path, ICONS_FMT ".bmx", XTREME_SETTINGS()->asset_pack, name); furi_string_printf(path, ICONS_FMT ".bmx", XTREME_SETTINGS()->asset_pack, name);
if(storage_file_open(file, furi_string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING)) { if(storage_file_open(file, furi_string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING)) {
uint64_t size = storage_file_size(file) - 8; uint64_t size = storage_file_size(file) - 8;
@@ -67,12 +70,16 @@ void swap_icon_static(const Icon* replace, const char* name, FuriString* path, F
if(storage_file_read(file, &icon_width, 4) == 4 && if(storage_file_read(file, &icon_width, 4) == 4 &&
storage_file_read(file, &icon_height, 4) == 4 && storage_file_read(file, &icon_height, 4) == 4 &&
storage_file_read(file, frame, size) == size) { storage_file_read(file, frame, size) == size) {
FURI_CONST_ASSIGN(replace->frame_count, 1); Icon* original = malloc(sizeof(Icon));
memcpy(original, replace, sizeof(Icon));
FURI_CONST_ASSIGN_PTR(replace->original, original);
uint8_t** frames = malloc(sizeof(const uint8_t*));
frames[0] = frame;
FURI_CONST_ASSIGN(replace->frame_rate, 0); FURI_CONST_ASSIGN(replace->frame_rate, 0);
FURI_CONST_ASSIGN(replace->frame_count, 1);
FURI_CONST_ASSIGN(replace->width, icon_width); FURI_CONST_ASSIGN(replace->width, icon_width);
FURI_CONST_ASSIGN(replace->height, icon_height); FURI_CONST_ASSIGN(replace->height, icon_height);
FURI_CONST_ASSIGN_PTR(replace->frames, malloc(sizeof(const uint8_t*))); FURI_CONST_ASSIGN_PTR(replace->frames, frames);
FURI_CONST_ASSIGN_PTR(replace->frames[0], frame);
} else { } else {
free(frame); free(frame);
} }
@@ -80,6 +87,20 @@ void swap_icon_static(const Icon* replace, const char* name, FuriString* path, F
storage_file_close(file); storage_file_close(file);
} }
void free_icon(const Icon* icon) {
uint8_t** frames = (void*)icon->frames;
int32_t frame_count = icon->frame_count;
Icon* original = icon->original;
memcpy((void*)icon, original, sizeof(Icon));
free(original);
for(int32_t i = 0; i < frame_count; i++) {
free(frames[i]);
}
free(frames);
}
void XTREME_ASSETS_LOAD() { void XTREME_ASSETS_LOAD() {
if(!furi_hal_is_normal_boot()) return; if(!furi_hal_is_normal_boot()) return;
@@ -95,10 +116,12 @@ void XTREME_ASSETS_LOAD() {
File* f = storage_file_alloc(storage); File* f = storage_file_alloc(storage);
for(size_t i = 0; i < ICON_PATHS_COUNT; i++) { for(size_t i = 0; i < ICON_PATHS_COUNT; i++) {
if(ICON_PATHS[i].animated) { if(ICON_PATHS[i].icon->original == NULL) {
swap_icon_animated(ICON_PATHS[i].icon, ICON_PATHS[i].path, p, f); if(ICON_PATHS[i].animated) {
} else { load_icon_animated(ICON_PATHS[i].icon, ICON_PATHS[i].path, p, f);
swap_icon_static(ICON_PATHS[i].icon, ICON_PATHS[i].path, p, f); } else {
load_icon_static(ICON_PATHS[i].icon, ICON_PATHS[i].path, p, f);
}
} }
} }
@@ -107,3 +130,13 @@ void XTREME_ASSETS_LOAD() {
furi_string_free(p); furi_string_free(p);
furi_record_close(RECORD_STORAGE); furi_record_close(RECORD_STORAGE);
} }
void XTREME_ASSETS_FREE() {
if(!furi_hal_is_normal_boot()) return;
for(size_t i = 0; i < ICON_PATHS_COUNT; i++) {
if(ICON_PATHS[i].icon->original != NULL) {
free_icon(ICON_PATHS[i].icon);
}
}
}

View File

@@ -3,3 +3,4 @@
void NAMESPOOF_INIT(); void NAMESPOOF_INIT();
void XTREME_SETTINGS_LOAD(); void XTREME_SETTINGS_LOAD();
void XTREME_ASSETS_LOAD(); void XTREME_ASSETS_LOAD();
void XTREME_ASSETS_FREE();