diff --git a/source/fileOps/fileOps.c b/source/fileOps/fileOps.c index 9f0d6625..6e6fafa8 100644 --- a/source/fileOps/fileOps.c +++ b/source/fileOps/fileOps.c @@ -20,6 +20,7 @@ en exposed s_fsop fsop structure can be used by callback to update operation sta #include "fileOps/fileOps.h" #include "gecko/gecko.hpp" #include "loader/utils.h" +#include "memory/mem2.hpp" #define SET(a, b) a = b; DCFlushRange(&a, sizeof(a)); #define STACKSIZE 8192 @@ -366,3 +367,38 @@ void fsop_deleteFile(const char *source) gprintf("Deleting file: %s\n", source); fsop_silentDelete(source); } + +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); + rewind(f); + fread(mem, filesize, 1, f); + fclose(f); + + *(size) = filesize; + return mem; +} + +bool fsop_WriteFile(const char *path, u8 *mem, 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; +} diff --git a/source/fileOps/fileOps.h b/source/fileOps/fileOps.h index c6df8ac3..82aebbd2 100644 --- a/source/fileOps/fileOps.h +++ b/source/fileOps/fileOps.h @@ -19,6 +19,8 @@ bool fsop_CopyFile(const char *source, const char *target, progress_callback_t s bool fsop_CopyFolder(const char *source, const char *target, progress_callback_t spinner, void *spinner_data); void fsop_deleteFile(const char *source); void fsop_deleteFolder(const char *source); +u8 *fsop_ReadFile(const char *path, u32 *size); +bool fsop_WriteFile(const char *path, u8 *mem, u32 size); #endif diff --git a/source/gc/gc.cpp b/source/gc/gc.cpp index 888dd787..b9d76d5d 100644 --- a/source/gc/gc.cpp +++ b/source/gc/gc.cpp @@ -179,20 +179,8 @@ bool DEVO_Installed(const char *path) void DEVO_GetLoader(const char *path) { - //Read in loader.bin - const char *loader_path = fmt("%s/loader.bin", path); - FILE *f = fopen(loader_path, "rb"); - if(f != NULL) - { - gprintf("Devolution: Reading %s\n", loader_path); - fseek(f, 0, SEEK_END); - loader_size = ftell(f); - rewind(f); - tmp_buffer = (u8*)MEM2_alloc(loader_size); - fread(tmp_buffer, 1, loader_size, f); - fclose(f); - } - else + tmp_buffer = fsop_ReadFile(fmt("%s/loader.bin", path), &loader_size); + if(tmp_buffer == NULL) { gprintf("Devolution: Loader not found!\n"); return; diff --git a/source/menu/menu.cpp b/source/menu/menu.cpp index 1f33512b..81f32cc8 100644 --- a/source/menu/menu.cpp +++ b/source/menu/menu.cpp @@ -1283,8 +1283,12 @@ GuiSound *CMenu::_sound(CMenu::SoundSet &soundSet, const char *domain, const cha CMenu::SoundSet::iterator i = soundSet.find(upperCase(name)); if(i == soundSet.end()) { - if(filename != name) - soundSet[upperCase(filename)] = new GuiSound(fmt("%s/%s", m_themeDataDir.c_str(), filename)); + if(filename != name && fsop_FileExist(fmt("%s/%s", m_themeDataDir.c_str(), filename))) + { + u32 size = 0; + u8 *mem = fsop_ReadFile(fmt("%s/%s", m_themeDataDir.c_str(), filename), &size); + soundSet[upperCase(filename)] = new GuiSound(mem, size, filename, true); + } else soundSet[upperCase(filename)] = new GuiSound(snd, len, filename, isAllocated); return soundSet[upperCase(filename)]; @@ -1306,7 +1310,14 @@ GuiSound *CMenu::_sound(CMenu::SoundSet &soundSet, const char *domain, const cha SoundSet::iterator i = soundSet.find(upperCase(filename)); if(i == soundSet.end()) { - soundSet[upperCase(filename)] = new GuiSound(fmt("%s/%s", m_themeDataDir.c_str(), filename)); + if(fsop_FileExist(fmt("%s/%s", m_themeDataDir.c_str(), filename))) + { + u32 size = 0; + u8 *mem = fsop_ReadFile(fmt("%s/%s", m_themeDataDir.c_str(), filename), &size); + soundSet[upperCase(filename)] = new GuiSound(mem, size, filename, true); + } + else + soundSet[upperCase(filename)] = new GuiSound(); return soundSet[upperCase(filename)]; } return i->second; diff --git a/source/menu/menu_game.cpp b/source/menu/menu_game.cpp index 76c29fc8..39eea224 100644 --- a/source/menu/menu_game.cpp +++ b/source/menu/menu_game.cpp @@ -1560,64 +1560,28 @@ void CMenu::_gameSoundThread(CMenu *m) m->m_soundThrdBusy = false; return; } - bool custom = false; u8 *custom_bnr_file = NULL; u32 custom_bnr_size = 0; - bool cached = false; u8 *cached_bnr_file = NULL; u32 cached_bnr_size = 0; char cached_banner[256]; cached_banner[255] = '\0'; strncpy(cached_banner, fmt("%s/%.6s.bnr", m->m_bnrCacheDir.c_str(), GameHdr->id), 255); - FILE *fp = fopen(cached_banner, "rb"); - if(fp) - { - cached = true; - fseek(fp, 0, SEEK_END); - cached_bnr_size = ftell(fp); - fseek(fp, 0, SEEK_SET); - cached_bnr_file = (u8*)MEM2_alloc(cached_bnr_size); - if(cached_bnr_file == NULL) - { - m->m_gameSound.FreeMemory(); - m_banner.DeleteBanner(); - m->m_soundThrdBusy = false; - return; - } - fread(cached_bnr_file, 1, cached_bnr_size, fp); - fclose(fp); - } - else + cached_bnr_file = fsop_ReadFile(cached_banner, &cached_bnr_size); + if(cached_bnr_file == NULL) { char custom_banner[256]; custom_banner[255] = '\0'; strncpy(custom_banner, fmt("%s/%.6s.bnr", m->m_customBnrDir.c_str(), GameHdr->id), 255); - FILE *fp = fopen(custom_banner, "rb"); - if(!fp) + custom_bnr_file = fsop_ReadFile(custom_banner, &custom_bnr_size); + if(custom_bnr_file == NULL) { strncpy(custom_banner, fmt("%s/%.3s.bnr", m->m_customBnrDir.c_str(), GameHdr->id), 255); - fp = fopen(custom_banner, "rb"); + custom_bnr_file = fsop_ReadFile(custom_banner, &custom_bnr_size); } - if(fp) - { - custom = true; - fseek(fp, 0, SEEK_END); - custom_bnr_size = ftell(fp); - fseek(fp, 0, SEEK_SET); - custom_bnr_file = (u8*)MEM2_alloc(custom_bnr_size); - if(custom_bnr_file == NULL) - { - m->m_gameSound.FreeMemory(); - m_banner.DeleteBanner(); - m->m_soundThrdBusy = false; - return; - } - fread(custom_bnr_file, 1, custom_bnr_size, fp); - fclose(fp); - } - if(!fp && GameHdr->type == TYPE_GC_GAME) + if(custom_bnr_file == NULL && GameHdr->type == TYPE_GC_GAME) { GC_Disc disc; disc.init(GameHdr->path); @@ -1636,9 +1600,9 @@ void CMenu::_gameSoundThread(CMenu *m) u32 sndSize = 0; u8 *soundBin = NULL; - if(cached) + if(cached_bnr_file != NULL) CurrentBanner.SetBanner(cached_bnr_file, cached_bnr_size); - else if(custom) + else if(custom_bnr_file != NULL) CurrentBanner.SetBanner(custom_bnr_file, custom_bnr_size, 0, true); else if(GameHdr->type == TYPE_WII_GAME) _extractBnr(GameHdr); @@ -1653,12 +1617,9 @@ void CMenu::_gameSoundThread(CMenu *m) m->m_soundThrdBusy = false; return; } - if(!custom && !cached && CurrentBanner.GetBannerFileSize() > 0) - { - FILE *fp = fopen(cached_banner, "wb"); - fwrite(CurrentBanner.GetBannerFile(), 1, CurrentBanner.GetBannerFileSize(), fp); - fclose(fp); - } + if(cached_bnr_file == NULL && custom_bnr_file == NULL) + fsop_WriteFile(cached_banner, CurrentBanner.GetBannerFile(), CurrentBanner.GetBannerFileSize()); + m_banner.LoadBanner(m->m_wbf1_font, m->m_wbf2_font); soundBin = CurrentBanner.GetFile("sound.bin", &sndSize); CurrentBanner.ClearBanner(); diff --git a/source/music/gui_sound.cpp b/source/music/gui_sound.cpp index 949630e3..51dddc73 100644 --- a/source/music/gui_sound.cpp +++ b/source/music/gui_sound.cpp @@ -190,7 +190,7 @@ bool GuiSound::Load(const char *path) } strncpy(this->filepath, path, 255); - this->filepath[255] = '\0'; SetLoop(loop); + this->filepath[255] = '\0'; SetLoop(loop); return true;