Major rewrite: less repetition (#101)

* first step of rewriting: at least it compiles™

* fix tabs/spaces

* fix dabort on load

* fix preview crash

* sorting isnt done outside of loading

* step one of making remake_file useless

* camera/qr code cleanup

* fix dabort when folder is empty, and bring back preview optimization

* fix button for switching modes and show mode when folder is empty

* fix scanning qrs

* no more splash discrimination

* add debug helpers

* fix theme installing
turns out that wasnt such a good idea

* fix battery icon

* mistake

* themeplaza compatibility

* make use of load_data

* don't drink and copy-paste, kids

* fix user-agent

* remove useless

* cleanup includes

* not even used

* add splash buttons

* upgrade buttons drawing

* fix controls while preview is up

* improve positions
This commit is contained in:
LiquidFenrir
2017-12-02 16:09:38 +01:00
committed by Alex Taber
parent ab4ead03b5
commit d937ae1716
15 changed files with 803 additions and 1149 deletions

View File

@@ -27,209 +27,25 @@
#include "themes.h"
#include "unicode.h"
#include "fs.h"
#include <time.h>
#include "draw.h"
#include "pp2d/pp2d/pp2d.h"
#include "pp2d/pp2d/lodepng.h"
#define BGM_MAX_SIZE 3371008
void load_theme_preview(Theme_s *theme)
{
//free the previously loaded preview. wont do anything if there wasnt one
pp2d_free_texture(TEXTURE_PREVIEW);
char *preview_buffer = NULL;
u64 size = 0;
if (!(theme->is_zip))
{
u16 path_to_preview[0x106] = {0};
strucat(path_to_preview, theme->path);
struacat(path_to_preview, "/preview.png");
size = file_to_buf(fsMakePath(PATH_UTF16, path_to_preview), ArchiveSD, &preview_buffer);
} else {
size = zip_file_to_buf("preview.png", theme->path, &preview_buffer);
}
if (!size)
{
free(preview_buffer);
return;
}
u8 * image = NULL;
unsigned int width = 0, height = 0;
int result = lodepng_decode32(&image, &width, &height, (u8*)preview_buffer, size);
if (result == 0) // no error
{
for (u32 i = 0; i < width; i++)
{
for (u32 j = 0; j < height; j++)
{
u32 p = (i + j*width) * 4;
u8 r = *(u8*)(image + p);
u8 g = *(u8*)(image + p + 1);
u8 b = *(u8*)(image + p + 2);
u8 a = *(u8*)(image + p + 3);
*(image + p) = a;
*(image + p + 1) = b;
*(image + p + 2) = g;
*(image + p + 3) = r;
}
}
theme->has_preview = true;
pp2d_load_texture_memory(TEXTURE_PREVIEW, image, (u32)width, (u32)height);
theme->preview_offset = (width-400)/2;
}
free(image);
free(preview_buffer);
}
static void parse_smdh(Theme_s *theme, ssize_t textureID, u16 *dir_name)
{
char *info_buffer = NULL;
u64 size = 0;
if (!(theme->is_zip))
{
u16 path_to_smdh[0x106] = {0};
strucat(path_to_smdh, theme->path);
struacat(path_to_smdh, "/info.smdh");
size = file_to_buf(fsMakePath(PATH_UTF16, path_to_smdh), ArchiveSD, &info_buffer);
} else {
size = zip_file_to_buf("info.smdh", theme->path, &info_buffer);
}
if (!size)
{
free(info_buffer);
memset(theme->name, 0, 0x80);
memset(theme->desc, 0, 0x100);
memset(theme->author, 0, 0x80);
memcpy(theme->name, dir_name, 0x80);
utf8_to_utf16(theme->desc, (u8*)"No description", 0x100);
utf8_to_utf16(theme->author, (u8*)"Unknown author", 0x80);
theme->placeholder_color = RGBA8(rand() % 255, rand() % 255, rand() % 255, 255);
return;
}
memcpy(theme->name, info_buffer + 0x08, 0x80);
memcpy(theme->desc, info_buffer + 0x88, 0x100);
memcpy(theme->author, info_buffer + 0x188, 0x80);
u16 *icon_data = malloc(0x1200);
memcpy(icon_data, info_buffer + 0x24C0, 0x1200);
const u32 width = 48, height = 48;
u32 *image = malloc(width*height*sizeof(u32));
for (u32 x = 0; x < width; x++)
{
for (u32 y = 0; y < height; y++)
{
unsigned int dest_pixel = (x + y*width);
unsigned int source_pixel = (((y >> 3) * (width >> 3) + (x >> 3)) << 6) + ((x & 1) | ((y & 1) << 1) | ((x & 2) << 1) | ((y & 2) << 2) | ((x & 4) << 2) | ((y & 4) << 3));
u8 r = ((icon_data[source_pixel] >> 11) & 0b11111) << 3;
u8 g = ((icon_data[source_pixel] >> 5) & 0b111111) << 2;
u8 b = (icon_data[source_pixel] & 0b11111) << 3;
u8 a = 0xFF;
image[dest_pixel] = (r << 24) | (g << 16) | (b << 8) | a;
}
}
pp2d_load_texture_memory(textureID, (u8*)image, (u32)width, (u32)height);
theme->icon_id = textureID;
free(image);
free(icon_data);
free(info_buffer);
}
Result get_themes(Theme_s **themes_list, int *theme_count)
{
shuffle_theme_count = 0;
Result res = 0;
Handle dir_handle;
res = FSUSER_OpenDirectory(&dir_handle, ArchiveSD, fsMakePath(PATH_ASCII, THEMES_PATH));
if (R_FAILED(res))
return res;
if (*themes_list != NULL) //used for QR reading and also for theme deletion
{
free(*themes_list);
*themes_list = NULL;
*theme_count = 0;
}
u32 entries_read = 1;
while (entries_read)
{
FS_DirectoryEntry entry = {0};
res = FSDIR_Read(dir_handle, &entries_read, 1, &entry);
if (R_FAILED(res) || entries_read == 0)
break;
if (!(entry.attributes & FS_ATTRIBUTE_DIRECTORY) && strcmp(entry.shortExt, "ZIP"))
continue;
*theme_count += entries_read;
*themes_list = realloc(*themes_list, (*theme_count) * sizeof(Theme_s));
if (*themes_list == NULL)
break;
Theme_s* current_theme = &(*themes_list)[*theme_count-1];
memset(current_theme, 0, sizeof(Theme_s));
u16 theme_path[0x106] = {0};
struacat(theme_path, THEMES_PATH);
strucat(theme_path, entry.name);
char pathchar[0x106] = {0};
utf16_to_utf8((u8*)pathchar, theme_path, 0x106);
memcpy(current_theme->path, theme_path, 0x106 * sizeof(u16));
current_theme->is_zip = !strcmp(entry.shortExt, "ZIP");
ssize_t iconID = TEXTURE_PREVIEW + *theme_count;
parse_smdh(current_theme, iconID, entry.name);
}
FSDIR_Close(dir_handle);
qsort(*themes_list, (long)*theme_count, sizeof(Theme_s), themecmp); //alphabet sort
return res;
}
int themecmp(const void* a, const void* b) //essentially a memcmp alias, so that it can be used properly with qsort
{
Theme_s *theme_a = (Theme_s *)a;
Theme_s *theme_b = (Theme_s *)b;
return memcmp(theme_a, theme_b, 0x40*sizeof(u16));
}
void del_theme(u16 *path)
void delete_theme(Entry_s theme)
{
Handle dir_handle;
Result res = FSUSER_OpenDirectory(&dir_handle, ArchiveSD, fsMakePath(PATH_UTF16, path));
if (R_SUCCEEDED(res))
Result res = FSUSER_OpenDirectory(&dir_handle, ArchiveSD, fsMakePath(PATH_UTF16, theme.path));
if(R_SUCCEEDED(res))
{
FSDIR_Close(dir_handle);
FSUSER_DeleteDirectoryRecursively(ArchiveSD, fsMakePath(PATH_UTF16, path));
FSUSER_DeleteDirectoryRecursively(ArchiveSD, fsMakePath(PATH_UTF16, theme.path));
} else
{
FSUSER_DeleteFile(ArchiveSD, fsMakePath(PATH_UTF16, path));
FSUSER_DeleteFile(ArchiveSD, fsMakePath(PATH_UTF16, theme.path));
}
}
Result bgm_install(Theme_s bgm_to_install)
Result bgm_install(Entry_s bgm_to_install)
{
char *savedata_buf;
char *thememanage_buf;
@@ -245,31 +61,23 @@ Result bgm_install(Theme_s bgm_to_install)
Result result = buf_to_file(savedata_size, "/SaveData.dat", ArchiveHomeExt, savedata_buf);
free(savedata_buf);
if (!R_SUCCEEDED(result)) return result;
if(R_FAILED(result)) return result;
if (bgm_to_install.is_zip) // Same as above but this time with bgm
{
music_size = zip_file_to_buf("bgm.bcstm", bgm_to_install.path, &music);
} else {
u16 path[0x106] = {0};
memcpy(path, bgm_to_install.path, 0x106 * sizeof(u16));
struacat(path, "/bgm.bcstm");
music_size = file_to_buf(fsMakePath(PATH_UTF16, path), ArchiveSD, &music);
}
music_size = load_data("/bgm.bcstm", bgm_to_install, &music);
if (music_size == 0)
if(music_size == 0)
{
music = calloc(1, 3371008);
} else if (music_size > 3371008) {
music = calloc(1, BGM_MAX_SIZE);
} else if(music_size > BGM_MAX_SIZE) {
free(music);
puts("musicrip");
return MAKERESULT(RL_PERMANENT, RS_CANCELED, RM_APPLICATION, RD_TOO_LARGE);
}
result = buf_to_file(music_size == 0 ? 3371008 : music_size, "/BgmCache.bin", ArchiveThemeExt, music);
result = buf_to_file(music_size == 0 ? BGM_MAX_SIZE : music_size, "/BgmCache.bin", ArchiveThemeExt, music);
free(music);
if (!R_SUCCEEDED(result)) return result;
if(R_FAILED(result)) return result;
file_to_buf(fsMakePath(PATH_ASCII, "/ThemeManage.bin"), ArchiveThemeExt, &thememanage_buf);
thememanage_buf[0x00] = 1;
@@ -296,23 +104,24 @@ Result bgm_install(Theme_s bgm_to_install)
result = buf_to_file(0x800, "/ThemeManage.bin", ArchiveThemeExt, thememanage_buf);
free(thememanage_buf);
if (!R_SUCCEEDED(result)) return result;
if(R_FAILED(result)) return result;
return 0;
}
// Install a single theme
Result single_install(Theme_s theme_to_install)
Result theme_install(Entry_s theme)
{
char *body;
char *music;
char *savedata_buf;
char *thememanage_buf;
u32 body_size;
u32 music_size;
u32 savedata_size;
char *body = NULL;
char *music = NULL;
char *savedata_buf = NULL;
char *thememanage_buf = NULL;
u32 body_size = 0;
u32 music_size = 0;
u32 savedata_size = 0;
savedata_size = file_to_buf(fsMakePath(PATH_ASCII, "/SaveData.dat"), ArchiveHomeExt, &savedata_buf);
DEBUGPOS("savedata: %p, %lx\n", savedata_buf, savedata_size);
savedata_buf[0x141b] = 0;
memset(&savedata_buf[0x13b8], 0, 8);
savedata_buf[0x13bd] = 3;
@@ -320,56 +129,38 @@ Result single_install(Theme_s theme_to_install)
Result result = buf_to_file(savedata_size, "/SaveData.dat", ArchiveHomeExt, savedata_buf);
free(savedata_buf);
if (!R_SUCCEEDED(result)) return result;
if(R_FAILED(result)) return result;
// Open body cache file. Test if theme is zipped
if (theme_to_install.is_zip)
{
body_size = zip_file_to_buf("body_LZ.bin", theme_to_install.path, &body);
}
else
{
u16 path[0x106] = {0};
memcpy(path, theme_to_install.path, 0x106 * sizeof(u16));
struacat(path, "/body_lz.bin");
body_size = file_to_buf(fsMakePath(PATH_UTF16, path), ArchiveSD, &body);
}
// Open body cache file.
body_size = load_data("/body_LZ.bin", theme, &body);
if (body_size == 0)
if(body_size == 0)
{
free(body);
puts("bodyrip");
DEBUGPOS("bodyrip");
return MAKERESULT(RL_PERMANENT, RS_CANCELED, RM_APPLICATION, RD_NOT_FOUND);
}
result = buf_to_file(body_size, "/BodyCache.bin", ArchiveThemeExt, body); // Write body data to file
free(body);
if (!R_SUCCEEDED(result)) return result;
if(R_FAILED(result)) return result;
if (theme_to_install.is_zip) // Same as above but this time with bgm
{
music_size = zip_file_to_buf("bgm.bcstm", theme_to_install.path, &music);
} else {
u16 path[0x106] = {0};
memcpy(path, theme_to_install.path, 0x106 * sizeof(u16));
struacat(path, "/bgm.bcstm");
music_size = file_to_buf(fsMakePath(PATH_UTF16, path), ArchiveSD, &music);
}
music_size = load_data("/bgm.bcstm", theme, &music);
if (music_size == 0)
if(music_size == 0)
{
music = calloc(1, 3371008);
} else if (music_size > 3371008) {
music = calloc(1, BGM_MAX_SIZE);
} else if(music_size > BGM_MAX_SIZE) {
free(music);
puts("musicrip");
DEBUGPOS("musicrip");
return MAKERESULT(RL_PERMANENT, RS_CANCELED, RM_APPLICATION, RD_TOO_LARGE);
}
result = buf_to_file(music_size == 0 ? 3371008 : music_size, "/BgmCache.bin", ArchiveThemeExt, music);
result = buf_to_file(music_size == 0 ? BGM_MAX_SIZE : music_size, "/BgmCache.bin", ArchiveThemeExt, music);
free(music);
if (!R_SUCCEEDED(result)) return result;
if(R_FAILED(result)) return result;
file_to_buf(fsMakePath(PATH_ASCII, "/ThemeManage.bin"), ArchiveThemeExt, &thememanage_buf);
thememanage_buf[0x00] = 1;
@@ -398,18 +189,18 @@ Result single_install(Theme_s theme_to_install)
result = buf_to_file(0x800, "/ThemeManage.bin", ArchiveThemeExt, thememanage_buf);
free(thememanage_buf);
if (!R_SUCCEEDED(result)) return result;
if(R_FAILED(result)) return result;
return 0;
}
Result shuffle_install(Theme_s *themes_list, int theme_count)
Result shuffle_install(Entry_s* themes_list, int themes_count)
{
u8 count = 0;
Theme_s *shuffle_themes[10] = {0};
Entry_s *shuffle_themes[10] = {0};
u32 body_sizes[10] = {0};
u32 bgm_sizes[10] = {0};
for (int i = 0; i < theme_count; i++)
for (int i = 0; i < themes_count; i++)
{
if (count > 10) return MAKERESULT(RL_USAGE, RS_INVALIDARG, RM_COMMON, RD_INVALID_SELECTION);
if (themes_list[i].in_shuffle)
@@ -435,10 +226,10 @@ Result shuffle_install(Theme_s *themes_list, int theme_count)
savedata_buf[0x13bd] = 3;
savedata_buf[0x13b8] = 0xff;
for (int i = 0; i < 10; i++)
for(int i = 0; i < 10; i++)
{
memset(&savedata_buf[0x13c0 + 0x8 * i], 0, 8); // clear any existing theme structure. 8 is the length of the theme structure, so 8 * i is the pos of the current one
if (count > i) // if we are still installing themes...
if(count > i) // if we are still installing themes...
{
savedata_buf[0x13c0 + (8 * i)] = i; // index
savedata_buf[0x13c0 + (8 * i) + 5] = 3; // persistence (?)
@@ -448,61 +239,39 @@ Result shuffle_install(Theme_s *themes_list, int theme_count)
Result result = buf_to_file(size, "/SaveData.dat", ArchiveHomeExt, savedata_buf);
free(savedata_buf);
if (!R_SUCCEEDED(result)) return result;
if(R_FAILED(result)) return result;
remake_file("/BodyCache_rd.bin", ArchiveThemeExt, 0x150000 * 10); // Enough space for 10 theme files
Handle body_cache_handle;
FSUSER_OpenFile(&body_cache_handle, ArchiveThemeExt, fsMakePath(PATH_ASCII, "/BodyCache_rd.bin"), FS_OPEN_WRITE, 0);
for (int i = 0; i < 10; i++)
for(int i = 0; i < count; i++)
{
if (count > i)
{
if (shuffle_themes[i]->is_zip)
{
char *body_buf;
u32 body_size = zip_file_to_buf("body_LZ.bin", shuffle_themes[i]->path, &body_buf);
body_sizes[i] = body_size;
FSFILE_Write(body_cache_handle, NULL, 0x150000 * i, body_buf, body_size, FS_WRITE_FLUSH);
free(body_buf);
} else {
u16 path[0x106] = {0};
strucat(path, shuffle_themes[i]->path);
struacat(path, "/body_LZ.bin");
char *body_buf;
u32 body_size = file_to_buf(fsMakePath(PATH_UTF16, path), ArchiveSD, &body_buf);
body_sizes[i] = body_size;
FSFILE_Write(body_cache_handle, NULL, 0x150000 * i, body_buf, body_size, FS_WRITE_FLUSH);
free(body_buf);
}
}
Entry_s * current_theme = shuffle_themes[i];
char * body_buf = NULL;
u32 body_size = load_data("/body_LZ.bin", *current_theme, &body_buf);
body_sizes[i] = body_size;
FSFILE_Write(body_cache_handle, NULL, 0x150000 * i, body_buf, body_size, FS_WRITE_FLUSH);
free(body_buf);
}
FSFILE_Close(body_cache_handle);
for (int i = 0; i < 10; i++)
for(int i = 0; i < 10; i++)
{
char bgm_cache_path[17] = {0};
sprintf(bgm_cache_path, "/BgmCache_%.2i.bin", i);
remake_file(bgm_cache_path, ArchiveThemeExt, 3371008);
if (count > i)
remake_file(bgm_cache_path, ArchiveThemeExt, BGM_MAX_SIZE);
if(count > i)
{
char *music_buf;
u32 music_size;
Entry_s * current_theme = shuffle_themes[i];
char *music_buf = NULL;
u32 music_size = music_size = load_data("/bgm.bcstm", *current_theme, &music_buf);
if (shuffle_themes[i]->is_zip)
if(!music_size)
{
music_size = zip_file_to_buf("bgm.bcstm", shuffle_themes[i]->path, &music_buf);
} else {
u16 path[0x106] = {0};
strucat(path, shuffle_themes[i]->path);
struacat(path, "/bgm.bcstm");
music_size = file_to_buf(fsMakePath(PATH_UTF16, path), ArchiveSD, &music_buf);
}
if (!music_size)
{
char *empty = calloc(1, 3371008);
buf_to_file(3371008, bgm_cache_path, ArchiveThemeExt, empty);
char *empty = calloc(1, BGM_MAX_SIZE);
buf_to_file(BGM_MAX_SIZE, bgm_cache_path, ArchiveThemeExt, empty);
bgm_sizes[i] = 0;
free(empty);
continue;
@@ -511,8 +280,8 @@ Result shuffle_install(Theme_s *themes_list, int theme_count)
buf_to_file(music_size, bgm_cache_path, ArchiveThemeExt, music_buf);
free(music_buf);
} else {
char *empty = calloc(1, 3371008);
buf_to_file(3371008, bgm_cache_path, ArchiveThemeExt, empty);
char *empty = calloc(1, BGM_MAX_SIZE);
buf_to_file(BGM_MAX_SIZE, bgm_cache_path, ArchiveThemeExt, empty);
bgm_sizes[i] = 0;
free(empty);
}
@@ -538,7 +307,7 @@ Result shuffle_install(Theme_s *themes_list, int theme_count)
*bodysizeloc = (u32) 0;
*bgmsizeloc = (u32) 0;
for (int i = 0; i < 10; i++)
for(int i = 0; i < 10; i++)
{
bodysizeloc = (u32*) (&thememanage_buf[0x338 + (4 * i)]); // body size info for shuffle themes starts at 0x338 and is 4 bytes for each theme
bgmsizeloc = (u32*) (&thememanage_buf[0x360 + (4 * i)]); // same thing for bgm but starting at 0x360
@@ -549,7 +318,7 @@ Result shuffle_install(Theme_s *themes_list, int theme_count)
result = buf_to_file(0x800, "/ThemeManage.bin", ArchiveThemeExt, thememanage_buf);
free(thememanage_buf);
if (!R_SUCCEEDED(result)) return result;
if(R_FAILED(result)) return result;
return MAKERESULT(RL_SUCCESS, RS_SUCCESS, RM_COMMON, RD_SUCCESS);
}