diff --git a/include/fs.h b/include/fs.h index 1716629..186d6ab 100644 --- a/include/fs.h +++ b/include/fs.h @@ -9,5 +9,9 @@ FS_Archive ArchiveThemeExt; Result open_archives(void); int get_number_entries(char*); +u32 file_to_buf(FS_Path path, char* buf); +u32 zip_file_to_buf(char *file_name, u16 *zip_path, char *buf); +u32 buf_to_file(u32 size, char *path, FS_Archive archive, char *buf); +bool check_file_exists(char *path, FS_Archive archive); #endif \ No newline at end of file diff --git a/source/fs.c b/source/fs.c index 79a8c99..9ed49a4 100644 --- a/source/fs.c +++ b/source/fs.c @@ -79,20 +79,21 @@ int get_number_entries(char *path) return count; } -Result file_to_buf(FS_Path path, char* buf) +u32 file_to_buf(FS_Path path, FS_Archive archive, char* buf) { Handle file; - Result res = FSUSER_OpenFile(&file, ArchiveSD, path, FS_OPEN_READ, 0); - if (R_FAILED(res)) return res; + Result res = FSUSER_OpenFile(&file, archive, path, FS_OPEN_READ, 0); + if (R_FAILED(res)) return 0; u32 size; FSFILE_GetSize(file, &size); buf = malloc(size); - res = FSFILE_Read(file, NULL, 0, buf, size); - return res; + FSFILE_Read(file, NULL, 0, buf, size); + FSFILE_Close(file); + return size; } -int zip_file_to_buf(char *file_name, u16 *zip_path, char *buf) +u32 zip_file_to_buf(char *file_name, u16 *zip_path, char *buf) { ssize_t len = strulen(zip_path, 0x106); @@ -101,20 +102,36 @@ int zip_file_to_buf(char *file_name, u16 *zip_path, char *buf) unzFile zip_handle = unzOpen((char*)path); - if (zip_handle == NULL) return 1; + if (zip_handle == NULL) return 0; + u32 file_size = 0; if (unzLocateFile(zip_handle, file_name, 0) == UNZ_OK) { unz_file_info *file_info = malloc(sizeof(unz_file_info)); - ssize_t file_size = file_info->uncompressed_size; + file_size = file_info->uncompressed_size; free(file_info); buf = malloc(file_size); unzOpenCurrentFile(zip_handle); unzReadCurrentFile(zip_handle, buf, file_size); unzCloseCurrentFile(zip_handle); - unzClose(zip_handle); } + unzClose(zip_handle); free(path); - return 0; + return file_size; +} + +u32 buf_to_file(u32 size, char *path, FS_Archive archive, char *buf) +{ + Handle handle; + u32 bytes = 0; + FSUSER_OpenFile(&handle, archive, fsMakePath(PATH_ASCII, path), FS_OPEN_WRITE | FS_OPEN_CREATE, 0); + FSFILE_Write(handle, &bytes, 0, buf, size, FS_WRITE_FLUSH); + FSFILE_Close(handle); + return bytes; +} + +bool check_file_exists(char *path, FS_Archive archive) +{ + return (R_SUMMARY(FSUSER_OpenFile(NULL, archive, fsMakePath(PATH_ASCII, path), FS_OPEN_READ, 0)) == RS_NOTFOUND); } \ No newline at end of file diff --git a/source/themes.c b/source/themes.c index d8cf8bd..206a41e 100644 --- a/source/themes.c +++ b/source/themes.c @@ -6,14 +6,97 @@ Result single_install(theme* theme) { char *body; + char *music; + char *savedata_buf; + char *thememanage_buf; + u32 body_size; + u32 music_size; + u32 savedata_size; + savedata_size = file_to_buf(fsMakePath(PATH_ASCII, "/SaveData.dat"), ArchiveHomeExt, savedata_buf) + savedata_buf[0x141b] = 0; + memset(&savedata_buf[0x13b8], 0, 8); + savedata_buf[0x13bd] = 3; + savedata_buf[0x13b8] = 0xff; + buf_to_file(savedata_size, "/SaveData.dat", ArchiveHomeExt, savedata_buf); + free(savedata_buf); + + // Open body cache file. Test if theme is zipped if (theme->is_zip) { - zip_file_to_buf("body_lz.bin", theme->path, body); + body_size = zip_file_to_buf("body_lz.bin", theme->path, body); } else { u16 path[0x106]; memcpy(path, theme->path, 0x106); struacat(path, "/body_lz.bin"); - file_to_buf(path, body); + body_size = file_to_buf(path, body); } + + if (body_size == 0) + { + free(body); + return MAKERESULT(RL_PERMANENT, RL_CANCELED, RL_APPLICATION, RD_NOT_FOUND); + } + + if (check_file_exists("/BodyCache.bin", ArchiveThemeExt)) FSUSER_DeleteFile(ArchiveThemeExt, fsMakePath("/BodyCache.bin")); + + u32 size = buf_to_file(body_size, "/BodyCache.bin", ArchiveThemeExt, body); // Write body data to file + free(body); + + if (size == 0) return MAKERESULT(RL_PERMANENT, RL_CANCELED, RL_APPLICATION, RD_NOT_FOUND); + + if (theme->is_zip) // Same as above but this time with bgm + { + music_size = zip_file_to_buf("bgm.bcstm", theme->path, music); + } else { + u16 path[0x106]; + memcpy(path, theme->path, 0x106); + struacat(path, "/bgm.bcstm"); + music_size = file_to_buf(path, music); + } + + if (music_size == 0) + { + free(music); + music = calloc(1, 3371008); + } else if (size > 3371008) { + free(music); + return MAKERESULT(RL_PERMANENT, RL_CANCELED, RL_APPLICATION, RD_TOO_LARGE); + } + + if (check_file_exists("/BgmCache.bin", ArchiveThemeExt)) FSUSER_DeleteFile(ArchiveThemeExt, fsMakePath("/BgmCache.bin")); + + size = buf_to_file(music_size, "/BgmCache.bin", ArchiveThemeExt, music); + free(music); + + if (size == 0) return MAKERESULT(RL_PERMANENT, RL_CANCELED, RL_APPLICATION, RD_NOT_FOUND); + + file_to_buf(fsMakePath(PATH_ASCII, "/ThemeManage.bin"), ArchiveThemeExt, thememanage_buf); + thememanage_buf[0x00] = 1; + thememanage_buf[0x01] = 0; + thememanage_buf[0x02] = 0; + thememanage_buf[0x03] = 0; + thememanage_buf[0x04] = 0; + thememanage_buf[0x05] = 0; + thememanage_buf[0x06] = 0; + thememanage_buf[0x07] = 0; + + u32 *body_size_location = (u32*)(&thememanage_buf[0x8]); + u32 *music_size_location = (u32*)(&thememanage_buf[0xC]); + *body_size_location = body_size; + *music_size_location = music_size; + + thememanage_buf[0x10] = 0xFF; + thememanage_buf[0x14] = 0x01; + thememanage_buf[0x18] = 0xFF; + thememanage_buf[0x1D] = 0x02; + + memset(&thememanage_buf[0x338], 0, 4); + memset(&thememanage_buf[0x340], 0, 4); + memset(&thememanage_buf[0x360], 0, 4); + memset(&thememanage_buf[0x368], 0, 4); + buf_to_file(0x800, "/ThemeManage.bin", ArchiveThemeExt, thememanage_buf); + free(thememanage_buf); + + return 0; } \ No newline at end of file