From df9534961e00a074441d76e2d19cc69a471b068b Mon Sep 17 00:00:00 2001 From: "fix94.1" Date: Sat, 19 Oct 2013 18:59:17 +0000 Subject: [PATCH] -reading in cached/custom banners into a 2mb fixed region now to save some memory allocations and gain a bit speed reading them in --- source/channel/banner.cpp | 18 ++++----- source/channel/banner.h | 9 ++--- source/channel/channels.cpp | 2 +- source/fileOps/fileOps.c | 79 ++++++++++++++++++++++++++++++++++++ source/fileOps/fileOps.h | 81 ++++--------------------------------- source/menu/menu_game.cpp | 32 +++++++++++---- 6 files changed, 124 insertions(+), 97 deletions(-) diff --git a/source/channel/banner.cpp b/source/channel/banner.cpp index 03bc73bf..3504b271 100644 --- a/source/channel/banner.cpp +++ b/source/channel/banner.cpp @@ -49,19 +49,19 @@ Banner::Banner() { opening = NULL; opening_size = 0; - title = 0; + allocated = false; imet = NULL; } -void Banner::SetBanner(u8 *bnr, u32 bnr_size, u64 title, bool custom) +void Banner::SetBanner(u8 *bnr, u32 bnr_size, bool custom, bool alloc) { ClearBanner(); if(bnr == NULL || bnr_size == 0) return; - this->title = title; opening = bnr; opening_size = bnr_size; + allocated = alloc; imet = (IMET *)opening; if(imet->sig != IMET_SIGNATURE) imet = (IMET *) (opening + IMET_OFFSET); @@ -80,19 +80,19 @@ void Banner::SetBanner(u8 *bnr, u32 bnr_size, u64 title, bool custom) if(memcmp(imetmd5, md5, 16) == 0 || custom) this->imet = imet; else - gprintf("Invalid md5, banner not valid for title %08x\n", title); + gprintf("Invalid md5, banner not valid\n"); } else - gprintf("Invalid signature found, banner not valid for title %08x\n", title); + gprintf("Invalid signature found, banner not valid\n"); } void Banner::ClearBanner() { - if(opening != NULL) + if(allocated == true && opening != NULL) free(opening); opening = NULL; opening_size = 0; - title = 0; + allocated = false; imet = NULL; } @@ -164,7 +164,7 @@ u8 *Banner::GetFile(const char *name, u32 *size) return file; } -void Banner::GetBanner(u64 title, char *appname, bool imetOnly) +void Banner::GetBanner(char *appname, bool imetOnly) { u8 *buf = NULL; u32 size = 0; @@ -179,5 +179,5 @@ void Banner::GetBanner(u64 title, char *appname, bool imetOnly) free(buf); return; } - SetBanner(buf, size, title); + SetBanner(buf, size, false, true); } diff --git a/source/channel/banner.h b/source/channel/banner.h index 2090c332..955bedae 100644 --- a/source/channel/banner.h +++ b/source/channel/banner.h @@ -61,26 +61,23 @@ class Banner { public: Banner(); - void SetBanner(u8 *bnr, u32 bnr_size, u64 title = 0, bool custom = false); + void SetBanner(u8 *bnr, u32 bnr_size, bool custom = false, bool alloc = false); void ClearBanner(); - bool IsValid(); bool GetName(u8 *name, int language); bool GetName(wchar_t *name, int language); u8 *GetFile(const char *name, u32 *size); - void GetBanner(u64 title, char *appname, bool imetOnly = false); + void GetBanner(char *appname, bool imetOnly = false); u8 *GetBannerFile() { return opening; } u32 GetBannerFileSize() { return opening_size; } protected: u8 *opening; u32 opening_size; - u64 title; + bool allocated; IMET *imet; - u16 *GetName(int language); - static bool GetChannelNameFromApp(u64 title, wchar_t* name, int language); }; extern Banner CurrentBanner; diff --git a/source/channel/channels.cpp b/source/channel/channels.cpp index 3496ef58..b1293c65 100644 --- a/source/channel/channels.cpp +++ b/source/channel/channels.cpp @@ -153,7 +153,7 @@ void Channels::GetBanner(u64 title, bool imetOnly) char app[ISFS_MAXPATH] ATTRIBUTE_ALIGN(32); if(!GetAppNameFromTmd(title, app, &cid)) return; - CurrentBanner.GetBanner(title, app, imetOnly); + CurrentBanner.GetBanner(app, imetOnly); } bool Channels::GetChannelNameFromApp(u64 title, wchar_t* name, int language) diff --git a/source/fileOps/fileOps.c b/source/fileOps/fileOps.c index dfdc1e5a..c0f53347 100644 --- a/source/fileOps/fileOps.c +++ b/source/fileOps/fileOps.c @@ -316,3 +316,82 @@ void fsop_deleteFolder(const char *source) gprintf("Deleting directory: %s\n", source); unlink(source); } + +bool fsop_FileExist(const char *fn) +{ + FILE * f; + f = fopen(fn, "rb"); + if(f) + { + fclose(f); + return true; + } + return false; +} + +void fsop_ReadFileLoc(const char *path, const u32 size, void *loc) +{ + FILE *f = fopen(path, "rb"); + fread(loc, size, 1, f); + fclose(f); +} + +u8 *fsop_ReadFile(const char *path, u32 *size) +{ + *(size) = 0; + u32 filesize = 0; + u8 *mem = NULL; + fsop_GetFileSizeBytes(path, &filesize); + if(filesize > 0) + { + mem = (u8*)MEM2_alloc(filesize); + if(mem != NULL) + { + //gprintf("Reading file: %s\n", path); + fsop_ReadFileLoc(path, filesize, mem); + *(size) = filesize; + } + } + return mem; +} + +bool fsop_WriteFile(const char *path, const void *mem, const u32 size) +{ + if(mem == NULL || size == 0) + return false; + + FILE *f = fopen(path, "wb"); + if(f == NULL) + return false; + //gprintf("Writing file: %s\n", path); + fwrite(mem, size, 1, f); + fclose(f); + return true; +} + +void fsop_deleteFile(const char *source) +{ + if(!fsop_FileExist(source)) + return; + remove(source); +} + +bool fsop_FolderExist(const char *path) +{ + DIR *dir; + dir = opendir(path); + if(dir) + { + closedir(dir); + return true; + } + return false; +} + +void fsop_MakeFolder(const char *path) +{ + if(fsop_FolderExist(path)) + return; + //gprintf("Folder path to create: %s\n", path); + mkdir(path, S_IREAD | S_IWRITE); +} diff --git a/source/fileOps/fileOps.h b/source/fileOps/fileOps.h index c95c7d71..b809ec7c 100644 --- a/source/fileOps/fileOps.h +++ b/source/fileOps/fileOps.h @@ -19,80 +19,13 @@ u32 fsop_GetFreeSpaceKb(const char *path); bool fsop_CopyFile(const char *source, const char *target, progress_callback_t spinner, void *spinner_data); bool fsop_CopyFolder(const char *source, const char *target, progress_callback_t spinner, void *spinner_data); void fsop_deleteFolder(const char *source); - -static inline bool fsop_FileExist(const char *fn) -{ - FILE * f; - f = fopen(fn, "rb"); - if(f) - { - fclose(f); - return true; - } - return false; -} - -static inline u8 *fsop_ReadFile(const char *path, u32 *size) -{ - *(size) = 0; - if(!fsop_FileExist(path)) - return NULL; - //gprintf("Reading file: %s\n", path); - FILE *f = fopen(path, "rb"); - fseek(f, 0, SEEK_END); - u32 filesize = ftell(f); - u8 *mem = (u8*)MEM2_alloc(filesize); - if(mem != NULL) - { - rewind(f); - fread(mem, filesize, 1, f); - *(size) = filesize; - } - fclose(f); - - return mem; -} - -static inline bool fsop_WriteFile(const char *path, const void *mem, const u32 size) -{ - if(mem == NULL || size == 0) - return false; - - FILE *f = fopen(path, "wb"); - if(f == NULL) - return false; - //gprintf("Writing file: %s\n", path); - fwrite(mem, size, 1, f); - fclose(f); - return true; -} - -static inline void fsop_deleteFile(const char *source) -{ - if(!fsop_FileExist(source)) - return; - remove(source); -} - -static inline bool fsop_FolderExist(const char *path) -{ - DIR *dir; - dir = opendir(path); - if(dir) - { - closedir(dir); - return true; - } - return false; -} - -static inline void fsop_MakeFolder(const char *path) -{ - if(fsop_FolderExist(path)) - return; - //gprintf("Folder path to create: %s\n", path); - mkdir(path, S_IREAD | S_IWRITE); -} +bool fsop_FileExist(const char *fn); +u8 *fsop_ReadFile(const char *path, u32 *size); +void fsop_ReadFileLoc(const char *path, const u32 size, void *loc); +bool fsop_WriteFile(const char *path, const void *mem, const u32 size); +void fsop_deleteFile(const char *source); +bool fsop_FolderExist(const char *path); +void fsop_MakeFolder(const char *path); #endif diff --git a/source/menu/menu_game.cpp b/source/menu/menu_game.cpp index 0761f201..234f2cef 100644 --- a/source/menu/menu_game.cpp +++ b/source/menu/menu_game.cpp @@ -251,7 +251,7 @@ static void _extractBnr(const dir_discHdr *hdr) void *bnr = NULL; size = wbfs_extract_file(disc, (char*)"opening.bnr", &bnr); if(size > 0) - CurrentBanner.SetBanner((u8*)bnr, size); + CurrentBanner.SetBanner((u8*)bnr, size, false, true); WBFS_CloseDisc(disc); } WBFS_Close(); @@ -1597,6 +1597,9 @@ struct IMD5Header u8 crypto[16]; } ATTRIBUTE_PACKED; +static const u32 BNR_MAX_SIZE = 0x00200000; /* 2MB */ +static u8 *BNR_LOC = (u8*)0x90000000; + void CMenu::_gameSoundThread(CMenu *m) { m->m_soundThrdBusy = true; @@ -1622,17 +1625,32 @@ void CMenu::_gameSoundThread(CMenu *m) char cached_banner[256]; cached_banner[255] = '\0'; strncpy(cached_banner, fmt("%s/%s.bnr", m->m_bnrCacheDir.c_str(), GameHdr->id), 255); - cached_bnr_file = fsop_ReadFile(cached_banner, &cached_bnr_size); - if(cached_bnr_file == NULL) + fsop_GetFileSizeBytes(cached_banner, &cached_bnr_size); + if(cached_bnr_size > 0 && cached_bnr_size < BNR_MAX_SIZE) + { + cached_bnr_file = BNR_LOC; + fsop_ReadFileLoc(cached_banner, cached_bnr_size, BNR_LOC); + } + else /* no cached or too big */ { char custom_banner[256]; custom_banner[255] = '\0'; strncpy(custom_banner, fmt("%s/%s.bnr", m->m_customBnrDir.c_str(), GameHdr->id), 255); - custom_bnr_file = fsop_ReadFile(custom_banner, &custom_bnr_size); - if(custom_bnr_file == NULL) + fsop_GetFileSizeBytes(custom_banner, &custom_bnr_size); + if(custom_bnr_size > 0 && custom_bnr_size < BNR_MAX_SIZE) + { + custom_bnr_file = BNR_LOC; + fsop_ReadFileLoc(custom_banner, custom_bnr_size, BNR_LOC); + } + else /* no custom ID6 or too big, try ID3 */ { strncpy(custom_banner, fmt("%s/%.3s.bnr", m->m_customBnrDir.c_str(), GameHdr->id), 255); - custom_bnr_file = fsop_ReadFile(custom_banner, &custom_bnr_size); + fsop_GetFileSizeBytes(custom_banner, &custom_bnr_size); + if(custom_bnr_size > 0 && custom_bnr_size < BNR_MAX_SIZE) + { + custom_bnr_file = BNR_LOC; + fsop_ReadFileLoc(custom_banner, custom_bnr_size, BNR_LOC); + } } if(custom_bnr_file == NULL && GameHdr->type == TYPE_GC_GAME) { @@ -1655,7 +1673,7 @@ void CMenu::_gameSoundThread(CMenu *m) if(cached_bnr_file != NULL) CurrentBanner.SetBanner(cached_bnr_file, cached_bnr_size); else if(custom_bnr_file != NULL) - CurrentBanner.SetBanner(custom_bnr_file, custom_bnr_size, 0, true); + CurrentBanner.SetBanner(custom_bnr_file, custom_bnr_size, true); else if(GameHdr->type == TYPE_WII_GAME) _extractBnr(GameHdr); else if(GameHdr->type == TYPE_CHANNEL)