From bb53a8ac81cae40e5586afc19ce8fff85335c03b Mon Sep 17 00:00:00 2001 From: yellows8 Date: Sun, 28 Oct 2018 22:31:16 -0400 Subject: [PATCH] Load assets from a .zip in romfs (romfs/ dir for pc-build) using minizip, instead of embedding it in data/. Sync Makefile.nx with latest switch-examples Makefile, with romfs. Automatically build the assets .zip in Makefile. Moved data/ to assets/ and moved data/unused/ to assets_unused/. Use #error in nx_audio.c since it's not supported currently. --- Makefile | 12 +- Makefile.nx | 12 +- Makefile.pc | 91 +--------- README.md | 2 +- {data => assets}/battery_icon.bin | Bin {data => assets}/charging_icon.bin | Bin {data => assets}/folder_icon.bin | 0 {data => assets}/hbmenu_logo_dark.bin | Bin {data => assets}/hbmenu_logo_light.bin | Bin {data => assets}/invalid_icon.bin | 0 {data => assets}/theme_icon_dark.bin | 0 {data => assets}/theme_icon_light.bin | 0 .../button_a_dark.bin | Bin .../button_a_light.bin | Bin .../button_b_dark.bin | Bin .../button_b_light.bin | Bin .../interuimedium20.nxfnt | Bin .../interuimedium28.nxfnt | Bin .../interuimedium30.nxfnt | Bin .../interuimedium42.nxfnt | Bin .../interuiregular14.nxfnt | Bin .../interuiregular18.nxfnt | Bin .../interuiregular20.nxfnt | Bin .../interuiregular24.nxfnt | Bin {data/unused => assets_unused}/tahoma12.nxfnt | Bin {data/unused => assets_unused}/tahoma24.nxfnt | Bin common/assets.c | 162 ++++++++++++++++++ common/assets.h | 21 +++ common/common.h | 1 + common/menu.c | 27 ++- common/theme.c | 10 +- nx_main/main.c | 12 +- nx_main/nx_audio.c | 3 +- pc_main/main.cpp | 2 + 34 files changed, 230 insertions(+), 125 deletions(-) rename {data => assets}/battery_icon.bin (100%) rename {data => assets}/charging_icon.bin (100%) rename {data => assets}/folder_icon.bin (100%) rename {data => assets}/hbmenu_logo_dark.bin (100%) rename {data => assets}/hbmenu_logo_light.bin (100%) rename {data => assets}/invalid_icon.bin (100%) rename {data => assets}/theme_icon_dark.bin (100%) rename {data => assets}/theme_icon_light.bin (100%) rename {data/unused => assets_unused}/button_a_dark.bin (100%) rename {data/unused => assets_unused}/button_a_light.bin (100%) rename {data/unused => assets_unused}/button_b_dark.bin (100%) rename {data/unused => assets_unused}/button_b_light.bin (100%) rename {data/unused => assets_unused}/interuimedium20.nxfnt (100%) rename {data/unused => assets_unused}/interuimedium28.nxfnt (100%) rename {data/unused => assets_unused}/interuimedium30.nxfnt (100%) rename {data/unused => assets_unused}/interuimedium42.nxfnt (100%) rename {data/unused => assets_unused}/interuiregular14.nxfnt (100%) rename {data/unused => assets_unused}/interuiregular18.nxfnt (100%) rename {data/unused => assets_unused}/interuiregular20.nxfnt (100%) rename {data/unused => assets_unused}/interuiregular24.nxfnt (100%) rename {data/unused => assets_unused}/tahoma12.nxfnt (100%) rename {data/unused => assets_unused}/tahoma24.nxfnt (100%) create mode 100644 common/assets.c create mode 100644 common/assets.h diff --git a/Makefile b/Makefile index 8d12645..e3f2b7b 100644 --- a/Makefile +++ b/Makefile @@ -8,15 +8,21 @@ endif all: nx pc -dist-bin: +romfs : assets + @mkdir -p romfs + @rm -f romfs/assets.zip + @zip -rj romfs/assets.zip assets + +dist-bin: romfs $(MAKE) -f Makefile.nx dist-bin -nx: +nx: romfs $(MAKE) -f Makefile.nx -pc: +pc: romfs $(MAKE) -f Makefile.pc clean: + @rm -Rf romfs $(MAKE) -f Makefile.pc clean $(MAKE) -f Makefile.nx clean diff --git a/Makefile.nx b/Makefile.nx index 386eea4..59f3955 100644 --- a/Makefile.nx +++ b/Makefile.nx @@ -16,6 +16,7 @@ include $(DEVKITPRO)/libnx/switch_rules # DATA is a list of directories containing data files # INCLUDES is a list of directories containing header files # EXEFS_SRC is the optional input directory containing data copied into exefs, if anything this normally should only contain "main.npdm". +# ROMFS is the directory containing data to be added to RomFS, relative to the Makefile (Optional) # # NO_ICON: if set to anything, do not use icon. # NO_NACP: if set to anything, no .nacp file is generated. @@ -35,6 +36,7 @@ SOURCES := common/ nx_main/ nx_main/loaders/ DATA := data INCLUDES := include EXEFS_SRC := exefs_src +ROMFS := romfs DIST_PATH := $(TARGET)_v$(APP_VERSION) @@ -48,12 +50,12 @@ CFLAGS := -g -Wall -O2 -ffunction-sections \ CFLAGS += $(INCLUDE) -D__SWITCH__ -DVERSION=\"v$(APP_VERSION)\" -CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions -std=gnu++11 +CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions ASFLAGS := -g $(ARCH) LDFLAGS = -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map) -LIBS := `freetype-config --libs` -lconfig -lturbojpeg +LIBS := -lminizip `freetype-config --libs` -lconfig -lturbojpeg #--------------------------------------------------------------------------------- # list of directories containing libraries, this must be the top level containing @@ -95,7 +97,7 @@ else #--------------------------------------------------------------------------------- endif #--------------------------------------------------------------------------------- - + export OFILES_BIN := $(addsuffix .o,$(BINFILES)) export OFILES_SRC := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o) export OFILES := $(OFILES_BIN) $(OFILES_SRC) @@ -134,6 +136,10 @@ ifneq ($(APP_TITLEID),) export NACPFLAGS += --titleid=$(APP_TITLEID) endif +ifneq ($(ROMFS),) + export NROFLAGS += --romfsdir=$(CURDIR)/$(ROMFS) +endif + .PHONY: $(BUILD) clean all dist-bin #--------------------------------------------------------------------------------- diff --git a/Makefile.pc b/Makefile.pc index 736d4ad..dd65b1f 100644 --- a/Makefile.pc +++ b/Makefile.pc @@ -9,97 +9,12 @@ EXTRA_CFLAGS="-D__USE_MINGW_ANSI_STDIO" EXTRA_LDFLAGS="-lws2_32" endif -# canned command sequence for binary data -#--------------------------------------------------------------------------------- -define bin2o - bin2s $(BIN2S_FLAGS) $< | $(AS) -o $(@) - echo "extern const u8" `(echo $( `(echo $(> `(echo $(> `(echo $( + +typedef struct { + u8 *buffer; + size_t size; + const char *filename; +} assetsDataEntry; + +#define GENASSET(x) {.filename = x} + +static bool g_assetsInitialized = 0; +assetsDataEntry g_assetsDataList[AssetId_Max] = { + GENASSET("battery_icon.bin"), + GENASSET("charging_icon.bin"), + GENASSET("folder_icon.bin"), + GENASSET("invalid_icon.bin"), + GENASSET("hbmenu_logo_dark.bin"), + GENASSET("hbmenu_logo_light.bin"), + GENASSET("theme_icon_dark.bin"), + GENASSET("theme_icon_light.bin"), +}; + +static void assetsClearEntry(assetsDataEntry *entry) { + free(entry->buffer); + + entry->size = 0; + entry->buffer = NULL; +} + +static int assetsLoadFile(unzFile zipf, assetsDataEntry *entry) { + int ret; + int filesize=0; + unz_file_info file_info; + u8* buffer = NULL; + + ret = unzLocateFile(zipf, entry->filename, 0); + + if (ret==UNZ_OK) ret = unzOpenCurrentFile(zipf); + + if (ret==UNZ_OK) { + ret = unzGetCurrentFileInfo(zipf, &file_info, NULL, 0, NULL, 0, NULL, 0); + + filesize = file_info.uncompressed_size; + if (filesize == 0) ret = -10; + + if (ret==UNZ_OK) { + buffer = (u8*)malloc(filesize); + if (buffer) { + memset(buffer, 0, filesize); + } else { + ret = -11; + } + } + + if (ret==UNZ_OK) { + ret = unzReadCurrentFile(zipf, buffer, filesize); + if(ret < filesize) { + ret = -12; + } else { + ret = UNZ_OK; + } + } + + if (ret!=UNZ_OK && buffer!=NULL) free(buffer); + + unzCloseCurrentFile(zipf); + } + + if (ret==UNZ_OK) { + entry->buffer = buffer; + entry->size = filesize; + } + + return ret; +} + +Result assetsInit(void) { + int ret=0; + int i, stopi; + unzFile zipf; + assetsDataEntry *entry = NULL; + char tmp_path[PATH_MAX+1]; + + if (g_assetsInitialized) return 0; + + #ifdef __SWITCH__ + Result rc = romfsInit(); + if (R_FAILED(rc)) return rc; + #endif + + memset(tmp_path, 0, sizeof(tmp_path)); + + #ifdef __SWITCH__ + strncpy(tmp_path, "romfs:/assets.zip", sizeof(tmp_path)-1); + #else + snprintf(tmp_path, sizeof(tmp_path)-1, "%s/romfs/assets.zip", menuGetRootBasePath()); + #endif + + zipf = unzOpen(tmp_path); + if(zipf==NULL) { + #ifdef __SWITCH__ + romfsExit(); + #endif + + return 0x80; + } + + for (i=0; i= AssetId_Max) return; + + assetsDataEntry *entry = &g_assetsDataList[id]; + + if (buffer) *buffer = entry->buffer; + if (size) *size = entry->size; +} + +u8 *assetsGetDataBuffer(AssetId id) { + u8 *buffer = NULL; + + assetsGetData(id, &buffer, NULL); + return buffer; +} + diff --git a/common/assets.h b/common/assets.h new file mode 100644 index 0000000..797f957 --- /dev/null +++ b/common/assets.h @@ -0,0 +1,21 @@ +#pragma once +#include "common.h" + +typedef enum { + AssetId_battery_icon, + AssetId_charging_icon, + AssetId_folder_icon, + AssetId_invalid_icon, + AssetId_hbmenu_logo_dark, + AssetId_hbmenu_logo_light, + AssetId_theme_icon_dark, + AssetId_theme_icon_light, + + AssetId_Max +} AssetId; + +Result assetsInit(void); +void assetsExit(void); +void assetsGetData(AssetId id, u8 **buffer, size_t *size); +u8 *assetsGetDataBuffer(AssetId id); + diff --git a/common/common.h b/common/common.h index 528c3ea..a587dd6 100644 --- a/common/common.h +++ b/common/common.h @@ -55,6 +55,7 @@ typedef union { #include "menu.h" #include "text.h" #include "ui.h" +#include "assets.h" #include "launch.h" #include "worker.h" #include diff --git a/common/menu.c b/common/menu.c index 668662e..d7284f4 100644 --- a/common/menu.c +++ b/common/menu.c @@ -6,13 +6,6 @@ #include "switch/runtime/nxlink.h" #endif -#include "invalid_icon_bin.h" -#include "folder_icon_bin.h" -#include "theme_icon_dark_bin.h" -#include "theme_icon_light_bin.h" -#include "charging_icon_bin.h" -#include "battery_icon_bin.h" - char rootPathBase[PATH_MAX]; char rootPath[PATH_MAX+8]; void computeFrontGradient(color_t baseColor, int height); @@ -238,17 +231,17 @@ static void drawEntry(menuEntry_s* me, int off_x, int is_active) { } else if (me->type == ENTRY_TYPE_FOLDER) { smallimg = folder_icon_small; - largeimg = folder_icon_bin; + largeimg = assetsGetDataBuffer(AssetId_folder_icon); } else if (me->type == ENTRY_TYPE_THEME){ smallimg = theme_icon_small; if(themeGlobalPreset == THEME_PRESET_DARK) - largeimg = theme_icon_dark_bin; - else largeimg = theme_icon_light_bin; + largeimg = assetsGetDataBuffer(AssetId_theme_icon_dark); + else largeimg = assetsGetDataBuffer(AssetId_theme_icon_light); } else { smallimg = invalid_icon_small; - largeimg = invalid_icon_bin; + largeimg = assetsGetDataBuffer(AssetId_invalid_icon); } if (smallimg) { @@ -357,12 +350,12 @@ void menuStartup(void) { menuScan(rootPath); - folder_icon_small = downscaleImg(folder_icon_bin, 256, 256, 140, 140, IMAGE_MODE_RGB24); - invalid_icon_small = downscaleImg(invalid_icon_bin, 256, 256, 140, 140, IMAGE_MODE_RGB24); + folder_icon_small = downscaleImg(assetsGetDataBuffer(AssetId_folder_icon), 256, 256, 140, 140, IMAGE_MODE_RGB24); + invalid_icon_small = downscaleImg(assetsGetDataBuffer(AssetId_invalid_icon), 256, 256, 140, 140, IMAGE_MODE_RGB24); if(themeGlobalPreset == THEME_PRESET_DARK) - theme_icon_small = downscaleImg(theme_icon_dark_bin, 256, 256, 140, 140, IMAGE_MODE_RGB24); + theme_icon_small = downscaleImg(assetsGetDataBuffer(AssetId_theme_icon_dark), 256, 256, 140, 140, IMAGE_MODE_RGB24); else - theme_icon_small = downscaleImg(theme_icon_light_bin, 256, 256, 140, 140, IMAGE_MODE_RGB24); + theme_icon_small = downscaleImg(assetsGetDataBuffer(AssetId_theme_icon_light), 256, 256, 140, 140, IMAGE_MODE_RGB24); computeFrontGradient(themeCurrent.frontWaveColor, 280); //menuCreateMsgBox(780, 300, "This is a test"); } @@ -457,9 +450,9 @@ void drawCharge() { int tmpX = GetTextXCoordinate(interuiregular14, 1180, chargeString, 'r'); DrawText(interuiregular14, tmpX - 15, 0 + 47 + 10 + 21, themeCurrent.textColor, chargeString); - drawIcon(1180 - 11, 0 + 47 + 10 + 6, 10, 15, battery_icon_bin, themeCurrent.textColor); + drawIcon(1180 - 11, 0 + 47 + 10 + 6, 10, 15, assetsGetDataBuffer(AssetId_battery_icon), themeCurrent.textColor); if (isCharging) - drawIcon(tmpX - 32, 0 + 47 + 10 + 6, 9, 15, charging_icon_bin, themeCurrent.textColor); + drawIcon(tmpX - 32, 0 + 47 + 10 + 6, 9, 15, assetsGetDataBuffer(AssetId_charging_icon), themeCurrent.textColor); } } diff --git a/common/theme.c b/common/theme.c index ae667b3..c18853b 100644 --- a/common/theme.c +++ b/common/theme.c @@ -1,10 +1,4 @@ #include "theme.h" -/*#include "button_a_light_bin.h" -#include "button_a_dark_bin.h" -#include "button_b_light_bin.h" -#include "button_b_dark_bin.h"*/ -#include "hbmenu_logo_light_bin.h" -#include "hbmenu_logo_dark_bin.h" theme_t themeCurrent; ThemePreset themeGlobalPreset; @@ -35,7 +29,7 @@ void themeStartup(ThemePreset preset) { .buttonYText = "\uE0E3", .buttonPText = "\uE0EF", .buttonMText = "\uE0F0", - .hbmenuLogoImage = hbmenu_logo_light_bin + .hbmenuLogoImage = assetsGetDataBuffer(AssetId_hbmenu_logo_light) }; theme_t themeDark = (theme_t) { @@ -55,7 +49,7 @@ void themeStartup(ThemePreset preset) { .buttonYText = "\uE0A3", .buttonPText = "\uE0B3", .buttonMText = "\uE0B4", - .hbmenuLogoImage = hbmenu_logo_dark_bin + .hbmenuLogoImage = assetsGetDataBuffer(AssetId_hbmenu_logo_dark) }; char themePath[PATH_MAX] = {0}; diff --git a/nx_main/main.c b/nx_main/main.c index c9c6e0e..914a37f 100644 --- a/nx_main/main.c +++ b/nx_main/main.c @@ -57,12 +57,17 @@ int main(int argc, char **argv) } } - if (R_SUCCEEDED(rc)) { - menuStartupPath(); + if (R_SUCCEEDED(rc)) menuStartupPath(); - themeStartup((ThemePreset)theme); + if (R_SUCCEEDED(rc)) { + rc = assetsInit(); + if (R_FAILED(rc)) { + snprintf(errormsg, sizeof(errormsg)-1, "Error: assetsInit() failed: 0x%x.", rc); + } } + if (R_SUCCEEDED(rc)) themeStartup((ThemePreset)theme); + if (R_SUCCEEDED(rc)) powerInit(); if (R_SUCCEEDED(rc)) { @@ -178,6 +183,7 @@ int main(int argc, char **argv) workerExit(); netloaderExit(); powerExit(); + assetsExit(); plExit(); setsysExit(); diff --git a/nx_main/nx_audio.c b/nx_main/nx_audio.c index 3e1ace4..0cc1796 100644 --- a/nx_main/nx_audio.c +++ b/nx_main/nx_audio.c @@ -5,8 +5,7 @@ #include "../common/common.h" #ifdef ENABLE_AUDIO -#include "audio_intro_bin.h"//When using this, you must manually add these under data/ since they're not included in .git. -#include "audio_loop_bin.h" +#error "Audio is not supported currently." #define SAMPLERATE 48000 #define BYTESPERSAMPLE 2 diff --git a/pc_main/main.cpp b/pc_main/main.cpp index 0f4d03b..0ddaa2f 100644 --- a/pc_main/main.cpp +++ b/pc_main/main.cpp @@ -16,6 +16,7 @@ int main() window.setFramerateLimit(60); menuStartupPath(); + assetsInit(); themeStartup(THEME_PRESET_LIGHT); textInit(); fontInitialize(); @@ -58,6 +59,7 @@ int main() workerExit(); netloaderExit(); fontExit(); + assetsExit(); return 0; }