diff --git a/portlibs/include/fat.h b/portlibs/include/fat.h index b0ffc13a..82c973d3 100644 --- a/portlibs/include/fat.h +++ b/portlibs/include/fat.h @@ -1,7 +1,7 @@ /* fat.h Simple functionality for startup, mounting and unmounting of FAT-based devices. - + Copyright (c) 2006 - 2009 Michael "Chishm" Chisholm Dave "WinterMute" Murphy @@ -71,7 +71,7 @@ extern bool fatInitDefault (void); /* Mount the device pointed to by interface, and set up a devoptab entry for it as "name:". You can then access the filesystem using "name:/". -This will mount the active partition or the first valid partition on the disc, +This will mount the active partition or the first valid partition on the disc, and will use a cache size optimized for the host system. */ extern bool fatMountSimple (const char* name, const DISC_INTERFACE* interface); diff --git a/source/channel/channel_launcher.c b/source/channel/channel_launcher.c index d150167d..aea910b6 100644 --- a/source/channel/channel_launcher.c +++ b/source/channel/channel_launcher.c @@ -14,17 +14,9 @@ #include "gecko.h" #include "mem2.hpp" -#define STACK_ALIGN(type, name, cnt, alignment) \ - u8 _al__##name[((sizeof(type)*(cnt)) + (alignment) + \ - (((sizeof(type)*(cnt))%(alignment)) > 0 ? ((alignment) - \ - ((sizeof(type)*(cnt))%(alignment))) : 0))]; \ - type *name = (type*)(((u32)(_al__##name)) + ((alignment) - (( \ - (u32)(_al__##name))&((alignment)-1)))) - void __Disc_SetLowMem(void); void __Disc_SetTime(void); void _unstub_start(); -u32 entryPoint; extern void __exception_closeall(); @@ -41,6 +33,8 @@ typedef struct _dolheader u32 padding[7]; } __attribute__((packed)) dolheader; +u32 entryPoint; + s32 BootChannel(u32 entry, u64 chantitle, u32 ios, u8 vidMode, bool vipatch, bool countryString, u8 patchVidMode, int aspectRatio) { gprintf("Loading Channel...\n"); diff --git a/source/channel/nand.cpp b/source/channel/nand.cpp index 5892c4ac..c0c55fb9 100644 --- a/source/channel/nand.cpp +++ b/source/channel/nand.cpp @@ -315,7 +315,7 @@ s32 Nand::__configread(void) configloaded = configloaded ? false : true; if(tbdec && configloaded) - return 1; + return 1; return 0; } @@ -422,10 +422,10 @@ u32 Nand::__configsetsetting(const char *item, const char *val) void Nand::__FATify(char *ptr, const char *str) { char ctr; - while ((ctr = *(str++)) != '\0') + while((ctr = *(str++)) != '\0') { const char *esc; - switch (ctr) + switch(ctr) { case '"': esc = "&qt;"; @@ -468,13 +468,20 @@ void Nand::__NANDify(char *str) { if(c == '&') { - if(!strncmp(src, "qt;", 3)) c = '"'; - else if (!strncmp(src, "st;", 3)) c = '*'; - else if (!strncmp(src, "cl;", 3)) c = ':'; - else if (!strncmp(src, "lt;", 3)) c = '<'; - else if (!strncmp(src, "gt;", 3)) c = '>'; - else if (!strncmp(src, "qm;", 3)) c = '?'; - else if (!strncmp(src, "vb;", 3)) c = '|'; + if(!strncmp(src, "qt;", 3)) + c = '"'; + else if(!strncmp(src, "st;", 3)) + c = '*'; + else if(!strncmp(src, "cl;", 3)) + c = ':'; + else if(!strncmp(src, "lt;", 3)) + c = '<'; + else if(!strncmp(src, "gt;", 3)) + c = '>'; + else if(!strncmp(src, "qm;", 3)) + c = '?'; + else if(!strncmp(src, "vb;", 3)) + c = '|'; if (c != '&') src += 3; @@ -558,7 +565,7 @@ s32 Nand::__FlashNandFile(const char *source, const char *dest) const char *file = strrchr(dest, '/')+1; dumper(NandDone, NandSize, fsize, FileDone, FilesDone, FoldersDone, (char *)file, data); } - } + } gprintf(" done!\n"); FilesDone++; if(showprogress) @@ -568,13 +575,13 @@ s32 Nand::__FlashNandFile(const char *source, const char *dest) } ISFS_Close(fd); MEM2_free(buffer); - fclose(file); + fclose(file); return 1; } s32 Nand::__DumpNandFile(const char *source, const char *dest) { - FileDone = 0; + FileDone = 0; s32 fd = ISFS_Open(source, ISFS_OPEN_READ); if (fd < 0) { @@ -667,8 +674,8 @@ s32 Nand::__DumpNandFile(const char *source, const char *dest) dumper(NandDone, NandSize, status->file_length, FileDone, FilesDone, FoldersDone, (char *)file, data); } gprintf(" done!\n"); - fclose(file); - ISFS_Close(fd); + fclose(file); + ISFS_Close(fd); MEM2_free(status); MEM2_free(buffer); @@ -705,16 +712,16 @@ s32 Nand::__FlashNandFolder(const char *source, const char *dest) if(ent->d_type == DT_DIR) { - __NANDify(ndest); + __NANDify(ndest); if(!fake) { ISFS_CreateDir(ndest, 0, 3, 3, 3); FoldersDone++; } - __FlashNandFolder(nsource, ndest); + __FlashNandFolder(nsource, ndest); } else - { + { __NANDify(ndest); __FlashNandFile(nsource, ndest); } @@ -730,18 +737,18 @@ s32 Nand::__DumpNandFolder(const char *source, const char *dest) char ndest[MAX_FAT_PATH]; char tdest[MAX_FAT_PATH]; - __GetNameList(source, &names, &cnt); + __GetNameList(source, &names, &cnt); for(i = 0; i < cnt; i++) { if(source[strlen(source)-1] == '/') snprintf(nsource, sizeof(nsource), "%s%s", source, names[i].name); else - snprintf(nsource, sizeof(nsource), "%s/%s", source, names[i].name); + snprintf(nsource, sizeof(nsource), "%s/%s", source, names[i].name); if(!names[i].type) { - __FATify(tdest, nsource); + __FATify(tdest, nsource); snprintf(ndest, sizeof(ndest), "%s%s", dest, tdest); __DumpNandFile(nsource, ndest); } @@ -749,7 +756,7 @@ s32 Nand::__DumpNandFolder(const char *source, const char *dest) { if(!fake) { - __FATify(tdest, nsource); + __FATify(tdest, nsource); CreatePath("%s%s", dest, tdest); FoldersDone++; } @@ -769,7 +776,7 @@ void Nand::CreatePath(const char *path, ...) if((vasprintf(&folder, path, args) >= 0) && folder) { if(folder[strlen(folder)-1] == '/') - folder[strlen(folder)-1] = 0; + folder[strlen(folder)-1] = 0; char *check = folder; while (true) @@ -840,9 +847,9 @@ s32 Nand::FlashToNAND(const char *source, const char *dest, dump_callback_t i_du data = i_data; dumper = i_dumper; fake = false; - showprogress = true; + showprogress = true; __FlashNandFolder(source, dest); - return 0; + return 0; } s32 Nand::DoNandDump(const char *source, const char *dest, dump_callback_t i_dumper, void *i_data) @@ -851,7 +858,7 @@ s32 Nand::DoNandDump(const char *source, const char *dest, dump_callback_t i_dum dumper = i_dumper; fake = false; showprogress = true; - u32 temp = 0; + u32 temp = 0; s32 ret = ISFS_ReadDir(source, NULL, &temp); if(ret < 0) { @@ -862,10 +869,8 @@ s32 Nand::DoNandDump(const char *source, const char *dest, dump_callback_t i_dum __DumpNandFile(source, ndest); } else - { __DumpNandFolder(source, dest); - } - return 0; + return 0; } s32 Nand::CalcFlashSize(const char *source, dump_callback_t i_dumper, void *i_data) @@ -874,8 +879,8 @@ s32 Nand::CalcFlashSize(const char *source, dump_callback_t i_dumper, void *i_da dumper = i_dumper; fake = true; showprogress = true; - __FlashNandFolder(source, ""); - return NandSize; + __FlashNandFolder(source, ""); + return NandSize; } s32 Nand::CalcDumpSpace(const char *source, dump_callback_t i_dumper, void *i_data) @@ -883,17 +888,17 @@ s32 Nand::CalcDumpSpace(const char *source, dump_callback_t i_dumper, void *i_da data = i_data; dumper = i_dumper; fake = true; - showprogress = true; + showprogress = true; - u32 temp = 0; + u32 temp = 0; s32 ret = ISFS_ReadDir(source, NULL, &temp); - if(ret < 0) + if(ret < 0) __DumpNandFile(source, ""); else __DumpNandFolder(source, ""); - return NandSize; + return NandSize; } void Nand::ResetCounters(void) @@ -1021,5 +1026,5 @@ s32 Nand::Do_Region_Change(string id) } } __configwrite(); - return 1; + return 1; } diff --git a/source/devicemounter/libwbfs/libwbfs.c b/source/devicemounter/libwbfs/libwbfs.c index 8584b517..2378c593 100644 --- a/source/devicemounter/libwbfs/libwbfs.c +++ b/source/devicemounter/libwbfs/libwbfs.c @@ -12,8 +12,6 @@ #define likely(x) __builtin_expect(!!(x), 1) #define unlikely(x) __builtin_expect(!!(x), 0) -#define read_le32_unaligned(x) ((x)[0]|((x)[1]<<8)|((x)[2]<<16)|((x)[3]<<24)) - #define ERROR(x) do {wbfs_error(x);goto error;}while(0) #define ALIGN_LBA(x) (((x)+p->hd_sec_sz-1)&(~(p->hd_sec_sz-1))) @@ -37,6 +35,8 @@ static u8 size_to_shift(u32 size) return ret - 1; } +#define read_le32_unaligned(x) ((x)[0]|((x)[1]<<8)|((x)[2]<<16)|((x)[3]<<24)) + wbfs_t *wbfs_open_hd(rw_sector_callback_t read_hdsector, rw_sector_callback_t write_hdsector, void *callback_data, int hd_sector_size, int num_hd_sector __attribute((unused)), int reset) { int i=num_hd_sector,ret; @@ -64,7 +64,6 @@ wbfs_t *wbfs_open_hd(rw_sector_callback_t read_hdsector, rw_sector_callback_t wr wbfs_iofree(tmp_buffer); if(reset)// XXX make a empty hd partition.. { - } return 0; } diff --git a/source/devicemounter/libwbfs/rijndael.c b/source/devicemounter/libwbfs/rijndael.c index a1277829..de91e96a 100644 --- a/source/devicemounter/libwbfs/rijndael.c +++ b/source/devicemounter/libwbfs/rijndael.c @@ -382,6 +382,7 @@ void aes_decrypt(u8 *iv, u8 *inbuf, u8 *outbuf, unsigned long long len) } else fraction = 16; + // debug_printf("block %d: fraction = %d\n", blockno, fraction); memcpy(block, inbuf + blockno * sizeof(block), fraction); decrypt((char*) block); u8 *ctext_ptr; @@ -391,6 +392,8 @@ void aes_decrypt(u8 *iv, u8 *inbuf, u8 *outbuf, unsigned long long len) for (i = 0; i < fraction; i++) outbuf[blockno * sizeof(block) + i] = ctext_ptr[i] ^ block[i]; + // debug_printf("Block %d output: ", blockno); + // hexdump(outbuf + blockno*sizeof(block), 16); } } @@ -400,6 +403,8 @@ void aes_encrypt(u8 *iv, u8 *inbuf, u8 *outbuf, unsigned long long len) u8 block[16]; unsigned int blockno = 0, i; + // debug_printf("aes_decrypt(%p, %p, %p, %lld)\n", iv, inbuf, outbuf, len); + for (blockno = 0; blockno <= (len / sizeof(block)); blockno++) { unsigned int fraction; @@ -411,6 +416,7 @@ void aes_encrypt(u8 *iv, u8 *inbuf, u8 *outbuf, unsigned long long len) } else fraction = 16; + // debug_printf("block %d: fraction = %d\n", blockno, fraction); memcpy(block, inbuf + blockno * sizeof(block), fraction); for (i = 0; i < fraction; i++) @@ -419,6 +425,8 @@ void aes_encrypt(u8 *iv, u8 *inbuf, u8 *outbuf, unsigned long long len) encrypt((char*) block); memcpy(iv, block, sizeof(block)); memcpy(outbuf + blockno * sizeof(block), block, sizeof(block)); + // debug_printf("Block %d output: ", blockno); + // hexdump(outbuf + blockno*sizeof(block), 16); } } diff --git a/source/gecko/gecko.c b/source/gecko/gecko.c index 1b0ed6e0..d536f157 100644 --- a/source/gecko/gecko.c +++ b/source/gecko/gecko.c @@ -16,7 +16,6 @@ /* init-globals */ bool geckoinit = false; bool textVideoInit = false; -bool geckoDisable = false; bool bufferMessages = true; bool WriteToSD = false; @@ -108,8 +107,6 @@ void WriteToFile(char* tmp) //using the gprintf from crediar because it is smaller than mine void gprintf( const char *format, ... ) { - if(geckoDisable) - return; char *tmp = NULL; va_list va; va_start(va, format); @@ -184,11 +181,6 @@ bool InitGecko() return false; } -void GeckoDisable() -{ - geckoDisable = true; -} - void AllocSDGeckoBuffer() { tmpfilebuffer = (char*)MEM2_alloc(filebuffer + 1 * sizeof(char)); diff --git a/source/gecko/gecko.h b/source/gecko/gecko.h index 1827e751..564229cc 100644 --- a/source/gecko/gecko.h +++ b/source/gecko/gecko.h @@ -1,5 +1,4 @@ - #ifndef _GECKO_H_ #define _GECKO_H_ @@ -7,17 +6,16 @@ extern "C" { #endif - extern bool geckoinit; - extern bool bufferMessages; - extern bool WriteToSD; +extern bool geckoinit; +extern bool bufferMessages; +extern bool WriteToSD; - //use this just like printf(); - void gprintf(const char *format, ...); - void ghexdump(void *d, int len); - bool InitGecko(); - void AllocSDGeckoBuffer(); - void ClearLogBuffer(); - void GeckoDisable(); +//use this just like printf(); +void gprintf(const char *format, ...); +void ghexdump(void *d, int len); +bool InitGecko(); +void AllocSDGeckoBuffer(); +void ClearLogBuffer(); #ifdef __cplusplus } diff --git a/source/gui/GameTDB.cpp b/source/gui/GameTDB.cpp index 743df35c..21e328cf 100644 --- a/source/gui/GameTDB.cpp +++ b/source/gui/GameTDB.cpp @@ -1,25 +1,25 @@ /**************************************************************************** - * Copyright (C) 2010 - * by Dimok + *Copyright (C) 2010 + *by Dimok * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any - * damages arising from the use of this software. + *This software is provided 'as-is', without any express or implied + *warranty. In no event will the authors be held liable for any + *damages arising from the use of this software. * - * Permission is granted to anyone to use this software for any - * purpose, including commercial applications, and to alter it and - * redistribute it freely, subject to the following restrictions: + *Permission is granted to anyone to use this software for any + *purpose, including commercial applications, and to alter it and + *redistribute it freely, subject to the following restrictions: * - * 1. The origin of this software must not be misrepresented; you - * must not claim that you wrote the original software. If you use - * this software in a product, an acknowledgment in the product - * documentation would be appreciated but is not required. + *1. The origin of this software must not be misrepresented; you + *must not claim that you wrote the original software. If you use + *this software in a product, an acknowledgment in the product + *documentation would be appreciated but is not required. * - * 2. Altered source versions must be plainly marked as such, and - * must not be misrepresented as being the original software. + *2. Altered source versions must be plainly marked as such, and + *must not be misrepresented as being the original software. * - * 3. This notice may not be removed or altered from any source - * distribution. + *3. This notice may not be removed or altered from any source + *distribution. ***************************************************************************/ #include #include @@ -32,25 +32,25 @@ #include "defines.h" #include "text.hpp" -#define NAME_OFFSET_DB "gametdb_offsets.bin" -#define MAXREADSIZE 1024*1024 // Cache size only for parsing the offsets: 1MB +#define NAME_OFFSET_DB "gametdb_offsets.bin" +#define MAXREADSIZE 1024*1024 //Cache size only for parsing the offsets: 1MB typedef struct _ReplaceStruct { - const char * orig; - char replace; - short size; + const char *orig; + char replace; + short size; } ReplaceStruct; //! More replacements can be added if needed static const ReplaceStruct Replacements[] = { - { ">", '>', 4 }, - { "<", '<', 4 }, - { """, '\"', 6 }, - { "'", '\'', 6 }, - { "&", '&', 5 }, - { NULL, '\0', 0 } + { ">", '>', 4 }, + { "<", '<', 4 }, + { """, '\"', 6 }, + { "'", '\'', 6 }, + { "&", '&', 5 }, + { NULL, '\0', 0 } }; GameTDB::GameTDB() @@ -58,427 +58,428 @@ GameTDB::GameTDB() { } -GameTDB::GameTDB(const char * filepath) +GameTDB::GameTDB(const char *filepath) : isLoaded(false), isParsed(false), file(0), filepath(0), LangCode("EN"), GameNodeCache(0) { - OpenFile(filepath); + OpenFile(filepath); } GameTDB::~GameTDB() { - CloseFile(); + CloseFile(); } -bool GameTDB::OpenFile(const char * filepath) +bool GameTDB::OpenFile(const char *filepath) { - if(!filepath) return false; + if(!filepath) + return false; gprintf("Trying to open '%s'...", filepath); - file = fopen(filepath, "rb"); - if(file) - { + file = fopen(filepath, "rb"); + if(file) + { this->filepath = filepath; - + gprintf("success\n"); - int pos; - string OffsetsPath = filepath; - if((pos = OffsetsPath.find_last_of('/')) != (int) string::npos) - OffsetsPath[pos] = '\0'; - else - OffsetsPath.clear(); //! Relative path + int pos; + string OffsetsPath = filepath; + if((pos = OffsetsPath.find_last_of('/')) != (int) string::npos) + OffsetsPath[pos] = '\0'; + else + OffsetsPath.clear(); //! Relative path gprintf("Checking game offsets\n"); - LoadGameOffsets(OffsetsPath.c_str()); + LoadGameOffsets(OffsetsPath.c_str()); /*if (!isParsed) { - gprintf("Checking titles.ini\n"); - CheckTitlesIni(OffsetsPath.c_str()); + gprintf("Checking titles.ini\n"); + CheckTitlesIni(OffsetsPath.c_str()); }*/ - } + } else gprintf("failed\n"); - isLoaded = (file != NULL); + isLoaded = (file != NULL); return isLoaded; } void GameTDB::CloseFile() { - OffsetMap.clear(); + OffsetMap.clear(); - if(GameNodeCache) - delete [] GameNodeCache; - GameNodeCache = NULL; + if(GameNodeCache) + delete [] GameNodeCache; + GameNodeCache = NULL; - if(file) fclose(file); - file = NULL; + if(file) + fclose(file); + file = NULL; } void GameTDB::Refresh() { gprintf("Refreshing file '%s'\n", filepath); CloseFile(); - - if (filepath == NULL) + + if(filepath == NULL) return; - + OpenFile(filepath); } -bool GameTDB::LoadGameOffsets(const char * path) +bool GameTDB::LoadGameOffsets(const char *path) { - if(!path) return false; + if(!path) + return false; - string OffsetDBPath = path; - if(strlen(path) > 0 && path[strlen(path)-1] != '/') - OffsetDBPath += '/'; - OffsetDBPath += NAME_OFFSET_DB; + string OffsetDBPath = path; + if(strlen(path) > 0 && path[strlen(path)-1] != '/') + OffsetDBPath += '/'; + OffsetDBPath += NAME_OFFSET_DB; - FILE * fp = fopen(OffsetDBPath.c_str(), "rb"); - if(!fp) - { - bool result = ParseFile(); - if(result) - SaveGameOffsets(OffsetDBPath.c_str()); + FILE *fp = fopen(OffsetDBPath.c_str(), "rb"); + if(!fp) + { + bool result = ParseFile(); + if(result) + SaveGameOffsets(OffsetDBPath.c_str()); - return result; - } + return result; + } - unsigned long long ExistingVersion = GetGameTDBVersion(); - unsigned long long Version = 0; - unsigned int NodeCount = 0; + u64 ExistingVersion = GetGameTDBVersion(); + u64 Version = 0; + u32 NodeCount = 0; - fread(&Version, 1, sizeof(Version), fp); + fread(&Version, 1, sizeof(Version), fp); - if(ExistingVersion != Version) - { - fclose(fp); - bool result = ParseFile(); - if(result) - SaveGameOffsets(OffsetDBPath.c_str()); + if(ExistingVersion != Version) + { + fclose(fp); + bool result = ParseFile(); + if(result) + SaveGameOffsets(OffsetDBPath.c_str()); - return result; - } + return result; + } - fread(&NodeCount, 1, sizeof(NodeCount), fp); + fread(&NodeCount, 1, sizeof(NodeCount), fp); - if(NodeCount == 0) - { - fclose(fp); - bool result = ParseFile(); - if(result) - SaveGameOffsets(OffsetDBPath.c_str()); + if(NodeCount == 0) + { + fclose(fp); + bool result = ParseFile(); + if(result) + SaveGameOffsets(OffsetDBPath.c_str()); - return result; - } + return result; + } - OffsetMap.resize(NodeCount); + OffsetMap.resize(NodeCount); - if(fread(&OffsetMap[0], 1, NodeCount*sizeof(GameOffsets), fp) != NodeCount*sizeof(GameOffsets)) - { - fclose(fp); - bool result = ParseFile(); - if(result) - SaveGameOffsets(OffsetDBPath.c_str()); + if(fread(&OffsetMap[0], 1, NodeCount*sizeof(GameOffsets), fp) != NodeCount*sizeof(GameOffsets)) + { + fclose(fp); + bool result = ParseFile(); + if(result) + SaveGameOffsets(OffsetDBPath.c_str()); - return result; - } + return result; + } - fclose(fp); + fclose(fp); - return true; + return true; } -bool GameTDB::SaveGameOffsets(const char * path) +bool GameTDB::SaveGameOffsets(const char *path) { - if(OffsetMap.size() == 0 || !path) - return false; + if(OffsetMap.size() == 0 || !path) + return false; - FILE * fp = fopen(path, "wb"); - if(!fp) return false; + FILE *fp = fopen(path, "wb"); + if(!fp) + return false; - unsigned long long ExistingVersion = GetGameTDBVersion(); - unsigned int NodeCount = OffsetMap.size(); + u64 ExistingVersion = GetGameTDBVersion(); + u32 NodeCount = OffsetMap.size(); - if(fwrite(&ExistingVersion, 1, sizeof(ExistingVersion), fp) != sizeof(ExistingVersion)) - { - fclose(fp); - return false; - } + if(fwrite(&ExistingVersion, 1, sizeof(ExistingVersion), fp) != sizeof(ExistingVersion)) + { + fclose(fp); + return false; + } - if(fwrite(&NodeCount, 1, sizeof(NodeCount), fp) != sizeof(NodeCount)) - { - fclose(fp); - return false; - } + if(fwrite(&NodeCount, 1, sizeof(NodeCount), fp) != sizeof(NodeCount)) + { + fclose(fp); + return false; + } - if(fwrite(&OffsetMap[0], 1, NodeCount*sizeof(GameOffsets), fp) != NodeCount*sizeof(GameOffsets)) - { - fclose(fp); - return false; - } + if(fwrite(&OffsetMap[0], 1, NodeCount*sizeof(GameOffsets), fp) != NodeCount*sizeof(GameOffsets)) + { + fclose(fp); + return false; + } - fclose(fp); + fclose(fp); - return true; + return true; } -unsigned long long GameTDB::GetGameTDBVersion() +u64 GameTDB::GetGameTDBVersion() { - if(!file) - return 0; + if(!file) + return 0; - char TmpText[1024]; + char TmpText[1024]; - if(GetData(TmpText, 0, sizeof(TmpText)) < 0) - return 0; + if(GetData(TmpText, 0, sizeof(TmpText)) < 0) + return 0; - char * VersionText = GetNodeText(TmpText, ""); - if(!VersionText) - return 0; + char *VersionText = GetNodeText(TmpText, ""); + if(!VersionText) + return 0; - return strtoull(VersionText, NULL, 10); + return strtoull(VersionText, NULL, 10); } -int GameTDB::GetData(char * data, int offset, int size) +int GameTDB::GetData(char *data, int offset, int size) { - if(!file || !data) - return -1; + if(!file || !data) + return -1; - fseek(file, offset, SEEK_SET); + fseek(file, offset, SEEK_SET); - return fread(data, 1, size, file); + return fread(data, 1, size, file); } -char * GameTDB::LoadGameNode(const char * id) +char *GameTDB::LoadGameNode(const char *id) { - unsigned int read = 0; + u32 read = 0; - GameOffsets * offset = this->GetGameOffset(id); - if(!offset) - return NULL; + GameOffsets *offset = this->GetGameOffset(id); + if(!offset) + return NULL; - char * data = new (std::nothrow) char[offset->nodesize+1]; - if(!data) - return NULL; + char *data = new (std::nothrow) char[offset->nodesize+1]; + if(!data) + return NULL; - if((read = GetData(data, offset->gamenode, offset->nodesize)) != offset->nodesize) - { - delete [] data; - return NULL; - } + if((read = GetData(data, offset->gamenode, offset->nodesize)) != offset->nodesize) + { + delete [] data; + return NULL; + } - data[read] = '\0'; + data[read] = '\0'; - return data; + return data; } -char * GameTDB::GetGameNode(const char * id) +char *GameTDB::GetGameNode(const char *id) { - char * data = NULL; + char *data = NULL; - if(GameNodeCache != 0 && strncmp(id, GameIDCache, strlen(GameIDCache)) == 0) - { - data = new (std::nothrow) char[strlen(GameNodeCache)+1]; - if(data) - strcpy(data, GameNodeCache); - } - else - { - if(GameNodeCache) - delete [] GameNodeCache; + if(GameNodeCache != 0 && strncmp(id, GameIDCache, strlen(GameIDCache)) == 0) + { + data = new (std::nothrow) char[strlen(GameNodeCache)+1]; + if(data) + strcpy(data, GameNodeCache); + } + else + { + if(GameNodeCache) + delete [] GameNodeCache; - GameNodeCache = LoadGameNode(id); + GameNodeCache = LoadGameNode(id); - if(GameNodeCache) - { - snprintf(GameIDCache, sizeof(GameIDCache), id); - data = new (std::nothrow) char[strlen(GameNodeCache)+1]; - if(data) - strcpy(data, GameNodeCache); - } - } + if(GameNodeCache) + { + snprintf(GameIDCache, sizeof(GameIDCache), id); + data = new (std::nothrow) char[strlen(GameNodeCache)+1]; + if(data) + strcpy(data, GameNodeCache); + } + } - return data; + return data; } -GameOffsets * GameTDB::GetGameOffset(const char * gameID) +GameOffsets *GameTDB::GetGameOffset(const char *gameID) { - for(unsigned int i = 0; i < OffsetMap.size(); ++i) - { - if(strncmp(gameID, OffsetMap[i].gameID, strlen(OffsetMap[i].gameID)) == 0) - return &OffsetMap[i]; - } + for(u32 i = 0; i < OffsetMap.size(); ++i) + { + if(strncmp(gameID, OffsetMap[i].gameID, strlen(OffsetMap[i].gameID)) == 0) + return &OffsetMap[i]; + } - return 0; + return 0; } -static inline char * CleanText(char * in_text) +static inline char *CleanText(char *in_text) { - if(!in_text) - return NULL; + if(!in_text) + return NULL; - const char * ptr = in_text; - char * text = in_text; + const char *ptr = in_text; + char *text = in_text; - while(*ptr != '\0') - { - for(int i = 0; Replacements[i].orig != 0; ++i) - { - if(strncmp(ptr, Replacements[i].orig, Replacements[i].size) == 0) - { - ptr += Replacements[i].size; - *text = Replacements[i].replace; - ++text; - i = 0; - continue; - } - } + while(*ptr != '\0') + { + for(int i = 0; Replacements[i].orig != 0; ++i) + { + if(strncmp(ptr, Replacements[i].orig, Replacements[i].size) == 0) + { + ptr += Replacements[i].size; + *text = Replacements[i].replace; + ++text; + i = 0; + continue; + } + } - if(*ptr == '\r') - { - ++ptr; - continue; - } + if(*ptr == '\r') + { + ++ptr; + continue; + } - *text = *ptr; - ++ptr; - ++text; - } + *text = *ptr; + ++ptr; + ++text; + } - *text = '\0'; + *text = '\0'; - return in_text; + return in_text; } -char * GameTDB::GetNodeText(char * data, const char * nodestart, const char * nodeend) +char *GameTDB::GetNodeText(char *data, const char *nodestart, const char *nodeend) { - if(!data || !nodestart || !nodeend) - return NULL; + if(!data || !nodestart || !nodeend) + return NULL; - char * position = strstr(data, nodestart); - if(!position) - return NULL; + char *position = strstr(data, nodestart); + if(!position) + return NULL; - position += strlen(nodestart); + position += strlen(nodestart); - char * end = strstr(position, nodeend); - if(!end) - return NULL; + char *end = strstr(position, nodeend); + if(!end) + return NULL; - *end = '\0'; + *end = '\0'; - return CleanText(position); + return CleanText(position); } -char * GameTDB::SeekLang(char * text, const char * langcode) +char *GameTDB::SeekLang(char *text, const char *langcode) { - if(!text || !langcode) return NULL; + if(!text || !langcode) + return NULL; - char * ptr = text; - while((ptr = strstr(ptr, ""); - if(!end) - return NULL; + if(strncmp(ptr, langcode, strlen(langcode)) == 0) + { + //! Cut off all the other languages + char *end = strstr(ptr, ""); + if(!end) + return NULL; - end += strlen(""); - *end = '\0'; + end += strlen(""); + *end = '\0'; - return ptr; - } - } + return ptr; + } + } - return NULL; + return NULL; } bool GameTDB::ParseFile() { - OffsetMap.clear(); + OffsetMap.clear(); - if(!file) - return false; + if(!file) + return false; - char * Line = new (std::nothrow) char[MAXREADSIZE+1]; - if(!Line) - return false; + char *Line = new (std::nothrow) char[MAXREADSIZE+1]; + if(!Line) + return false; - bool readnew = false; - int i, currentPos = 0; - int read = 0; - const char * gameNode = NULL; - const char * idNode = NULL; - const char * gameEndNode = NULL; + bool readnew = false; + int i, currentPos = 0; + int read = 0; + const char *gameNode = NULL; + const char *idNode = NULL; + const char *gameEndNode = NULL; - while((read = GetData(Line, currentPos, MAXREADSIZE)) > 0) - { - gameNode = Line; - readnew = false; + while((read = GetData(Line, currentPos, MAXREADSIZE)) > 0) + { + gameNode = Line; + readnew = false; - //! Ensure the null termination at the end - Line[read] = '\0'; + //! Ensure the null termination at the end + Line[read] = '\0'; - while((gameNode = strstr(gameNode, ""); - gameEndNode = strstr(gameNode, ""); - if(!idNode || !gameEndNode) - { - //! We are in the middle of the game node, reread complete node and more - currentPos += (gameNode-Line); - fseek(file, currentPos, SEEK_SET); - readnew = true; - break; - } + while((gameNode = strstr(gameNode, ""); + gameEndNode = strstr(gameNode, ""); + if(!idNode || !gameEndNode) + { + //! We are in the middle of the game node, reread complete node and more + currentPos += (gameNode-Line); + fseek(file, currentPos, SEEK_SET); + readnew = true; + break; + } - idNode += strlen(""); - gameEndNode += strlen(""); + idNode += strlen(""); + gameEndNode += strlen(""); - int size = OffsetMap.size(); - OffsetMap.resize(size+1); + int size = OffsetMap.size(); + OffsetMap.resize(size+1); - for(i = 0; i < 7 && *idNode != '<'; ++i, ++idNode) - OffsetMap[size].gameID[i] = *idNode; - OffsetMap[size].gameID[i] = '\0'; - OffsetMap[size].gamenode = currentPos+(gameNode-Line); - OffsetMap[size].nodesize = (gameEndNode-gameNode); - gameNode = gameEndNode; - } + for(i = 0; i < 7 && *idNode != '<'; ++i, ++idNode) + OffsetMap[size].gameID[i] = *idNode; + OffsetMap[size].gameID[i] = '\0'; + OffsetMap[size].gamenode = currentPos+(gameNode-Line); + OffsetMap[size].nodesize = (gameEndNode-gameNode); + gameNode = gameEndNode; + } - if(readnew) - continue; + if(readnew) + continue; - currentPos += read; - } + currentPos += read; + } - delete [] Line; + delete [] Line; - return true; + return true; } -bool GameTDB::FindTitle(char * data, string & title, string langCode) +bool GameTDB::FindTitle(char *data, string & title, string langCode) { - char * language = SeekLang(data, langCode.c_str()); - if(!language) - { - language = SeekLang(data, "EN"); - if(!language) - { - return false; - } - } + char *language = SeekLang(data, langCode.c_str()); + if(!language) + { + language = SeekLang(data, "EN"); + if(!language) + return false; + } + + char *the_title = GetNodeText(language, "", ""); + if(!the_title) + return false; - char * the_title = GetNodeText(language, "", ""); - if(!the_title) - { - return false; - } - char tmp[64]; strncpy(tmp, the_title, sizeof(tmp) - 1); tmp[sizeof(tmp) - 1] = '\0'; @@ -486,572 +487,583 @@ bool GameTDB::FindTitle(char * data, string & title, string langCode) return true; } -bool GameTDB::GetTitle(const char * id, string & title) +bool GameTDB::GetTitle(const char *id, string & title) { title = ""; - if(!id) return false; + if(!id) + return false; - char * data = GetGameNode(id); - if(!data) return false; + char *data = GetGameNode(id); + if(!data) + return false; bool retval = FindTitle(data, title, LangCode); - delete [] data; + delete [] data; - return retval; + return retval; } -bool GameTDB::GetSynopsis(const char * id, string & synopsis) +bool GameTDB::GetSynopsis(const char *id, string & synopsis) { synopsis = ""; - if(!id) return false; + if(!id) + return false; - char * data = GetGameNode(id); - if(!data) return false; + char *data = GetGameNode(id); + if(!data) + return false; - char * language = SeekLang(data, LangCode.c_str()); - if(!language) - { - language = SeekLang(data, "EN"); - if(!language) - { - delete [] data; - return false; - } - } + char *language = SeekLang(data, LangCode.c_str()); + if(!language) + { + language = SeekLang(data, "EN"); + if(!language) + { + delete [] data; + return false; + } + } - char * the_synopsis = GetNodeText(language, "", ""); - if(!the_synopsis) - { - delete [] data; - return false; - } + char *the_synopsis = GetNodeText(language, "", ""); + if(!the_synopsis) + { + delete [] data; + return false; + } - synopsis = the_synopsis; + synopsis = the_synopsis; - delete [] data; + delete [] data; - return true; + return true; } -bool GameTDB::GetRegion(const char * id, string & region) +bool GameTDB::GetRegion(const char *id, string & region) { region = ""; - if(!id) return false; + if(!id) + return false; - char * data = GetGameNode(id); - if(!data) return false; + char *data = GetGameNode(id); + if(!data) + return false; - char * the_region = GetNodeText(data, "", ""); - if(!the_region) - { - delete [] data; - return false; - } + char *the_region = GetNodeText(data, "", ""); + if(!the_region) + { + delete [] data; + return false; + } - region = the_region; + region = the_region; - delete [] data; + delete [] data; - return true; + return true; } -bool GameTDB::GetDeveloper(const char * id, string & dev) +bool GameTDB::GetDeveloper(const char *id, string & dev) { dev = ""; - if(!id) return false; + if(!id) + return false; - char * data = GetGameNode(id); - if(!data) return false; + char *data = GetGameNode(id); + if(!data) + return false; - char * the_dev = GetNodeText(data, "", ""); - if(!the_dev) - { - delete [] data; - return false; - } + char *the_dev = GetNodeText(data, "", ""); + if(!the_dev) + { + delete [] data; + return false; + } - dev = the_dev; + dev = the_dev; - delete [] data; + delete [] data; - return true; + return true; } -bool GameTDB::GetPublisher(const char * id, string & pub) +bool GameTDB::GetPublisher(const char *id, string & pub) { pub = ""; - if(!id) return false; + if(!id) + return false; - char * data = GetGameNode(id); - if(!data) return false; + char *data = GetGameNode(id); + if(!data) + return false; - char * the_pub = GetNodeText(data, "", ""); - if(!the_pub) - { - delete [] data; - return false; - } + char *the_pub = GetNodeText(data, "", ""); + if(!the_pub) + { + delete [] data; + return false; + } - pub = the_pub; + pub = the_pub; - delete [] data; + delete [] data; - return true; + return true; } -unsigned int GameTDB::GetPublishDate(const char * id) +u32 GameTDB::GetPublishDate(const char *id) { - if(!id) return 0; + if(!id) + return 0; - char * data = GetGameNode(id); - if(!data) return 0; + char *data = GetGameNode(id); + if(!data) + return 0; - char * year_string = GetNodeText(data, ""); - if(!year_string) - { - delete [] data; - return 0; - } + char *year_string = GetNodeText(data, ""); + if(!year_string) + { + delete [] data; + return 0; + } - unsigned int year, day, month; + u32 year, day, month; - year = atoi(year_string); + year = atoi(year_string); - char * month_string = strstr(year_string, "month=\""); - if(!month_string) - { - delete [] data; - return 0; - } + char *month_string = strstr(year_string, "month=\""); + if(!month_string) + { + delete [] data; + return 0; + } - month_string += strlen("month=\""); + month_string += strlen("month=\""); - month = atoi(month_string); + month = atoi(month_string); - char * day_string = strstr(month_string, "day=\""); - if(!day_string) - { - delete [] data; - return 0; - } + char *day_string = strstr(month_string, "day=\""); + if(!day_string) + { + delete [] data; + return 0; + } - day_string += strlen("day=\""); + day_string += strlen("day=\""); - day = atoi(day_string); + day = atoi(day_string); - delete [] data; + delete [] data; - return ((year & 0xFFFF) << 16 | (month & 0xFF) << 8 | (day & 0xFF)); + return ((year & 0xFFFF) << 16 | (month & 0xFF) << 8 | (day & 0xFF)); } -bool GameTDB::GetGenres(const char * id, string & gen) +bool GameTDB::GetGenres(const char *id, string & gen) { vector genre; - gen = ""; - if(!id) return false; + gen = ""; + if(!id) + return false; - char * data = GetGameNode(id); - if(!data) return false; + char *data = GetGameNode(id); + if(!data) + return false; - char * the_genre = GetNodeText(data, "", ""); - if(!the_genre) - { - delete [] data; - return false; - } + char *the_genre = GetNodeText(data, "", ""); + if(!the_genre) + { + delete [] data; + return false; + } - unsigned int genre_num = 0; - const char * ptr = the_genre; + u32 genre_num = 0; + const char *ptr = the_genre; - while(*ptr != '\0') - { - if(genre_num >= genre.size()) - genre.resize(genre_num+1); + while(*ptr != '\0') + { + if(genre_num >= genre.size()) + genre.resize(genre_num+1); - if(*ptr == ',' || *ptr == '/' || *ptr == ';') - { - ptr++; - while(*ptr == ' ') ptr++; + if(*ptr == ',' || *ptr == '/' || *ptr == ';') + { + ptr++; + while(*ptr == ' ') + ptr++; genre_num++; - continue; - } + continue; + } - if(genre[genre_num].size() == 0) - genre[genre_num].push_back(toupper((int)*ptr)); - else - genre[genre_num].push_back(*ptr); + if(genre[genre_num].size() == 0) + genre[genre_num].push_back(toupper((int)*ptr)); + else + genre[genre_num].push_back(*ptr); - ++ptr; - } - genre[genre_num].push_back('\0'); + ++ptr; + } + genre[genre_num].push_back('\0'); + + delete [] data; - delete [] data; - gen = vectorToString(genre, ", "); - return true; + return true; } -const char * GameTDB::RatingToString(int rating) +const char *GameTDB::RatingToString(int rating) { - switch(rating) - { - case GAMETDB_RATING_TYPE_CERO: - return "CERO"; - case GAMETDB_RATING_TYPE_ESRB: - return "ESRB"; - case GAMETDB_RATING_TYPE_PEGI: - return "PEGI"; - case GAMETDB_RATING_TYPE_GRB: - return "GRB"; - default: - break; - } + switch(rating) + { + case GAMETDB_RATING_TYPE_CERO: + return "CERO"; + case GAMETDB_RATING_TYPE_ESRB: + return "ESRB"; + case GAMETDB_RATING_TYPE_PEGI: + return "PEGI"; + case GAMETDB_RATING_TYPE_GRB: + return "GRB"; + default: + break; + } return NULL; } -int GameTDB::GetRating(const char * id) +int GameTDB::GetRating(const char *id) { - int rating = -1; + int rating = -1; - if(!id) return rating; + if(!id) + return rating; - char * data = GetGameNode(id); - if(!data) return rating; + char *data = GetGameNode(id); + if(!data) + return rating; - char * rating_text = GetNodeText(data, ""); - if(!rating_text) - { - delete [] data; - return rating; - } + char *rating_text = GetNodeText(data, ""); + if(!rating_text) + { + delete [] data; + return rating; + } - if(strncmp(rating_text, "CERO", 4) == 0) - rating = GAMETDB_RATING_TYPE_CERO; + if(strncmp(rating_text, "CERO", 4) == 0) + rating = GAMETDB_RATING_TYPE_CERO; + else if(strncmp(rating_text, "ESRB", 4) == 0) + rating = GAMETDB_RATING_TYPE_ESRB; + else if(strncmp(rating_text, "PEGI", 4) == 0) + rating = GAMETDB_RATING_TYPE_PEGI; + else if(strncmp(rating_text, "GRB", 4) == 0) + rating = GAMETDB_RATING_TYPE_GRB; - else if(strncmp(rating_text, "ESRB", 4) == 0) - rating = GAMETDB_RATING_TYPE_ESRB; + delete [] data; - else if(strncmp(rating_text, "PEGI", 4) == 0) - rating = GAMETDB_RATING_TYPE_PEGI; - - else if(strncmp(rating_text, "GRB", 4) == 0) - rating = GAMETDB_RATING_TYPE_GRB; - - delete [] data; - - return rating; + return rating; } -bool GameTDB::GetRatingValue(const char * id, string & rating_value) +bool GameTDB::GetRatingValue(const char *id, string & rating_value) { rating_value = ""; - if(!id) return false; + if(!id) + return false; - char * data = GetGameNode(id); - if(!data) return false; + char *data = GetGameNode(id); + if(!data) + return false; - char * rating_text = GetNodeText(data, ""); - if(!rating_text) - { - delete [] data; - return false; - } + char *rating_text = GetNodeText(data, ""); + if(!rating_text) + { + delete [] data; + return false; + } - char * value_text = GetNodeText(rating_text, "value=\"", "\""); - if(!value_text) - { - delete [] data; - return false; - } + char *value_text = GetNodeText(rating_text, "value=\"", "\""); + if(!value_text) + { + delete [] data; + return false; + } - rating_value = value_text; + rating_value = value_text; - delete [] data; + delete [] data; - return true; + return true; } -int GameTDB::GetRatingDescriptors(const char * id, vector & desc_list) +int GameTDB::GetRatingDescriptors(const char *id, vector & desc_list) { - desc_list.clear(); - if(!id) - return -1; + desc_list.clear(); + if(!id) + return -1; - char * data = GetGameNode(id); - if(!data) - return -1; + char *data = GetGameNode(id); + if(!data) + return -1; - char * descriptor_text = GetNodeText(data, "", ""); - if(!descriptor_text) - { - delete [] data; - return -1; - } + char *descriptor_text = GetNodeText(data, "", ""); + if(!descriptor_text) + { + delete [] data; + return -1; + } - unsigned int list_num = 0; + u32 list_num = 0; - while(*descriptor_text != '\0') - { - if(strncmp(descriptor_text, "", strlen("")) == 0) - { - desc_list[list_num].push_back('\0'); - descriptor_text = strstr(descriptor_text, ""); - if(!descriptor_text) - break; + while(*descriptor_text != '\0') + { + if(strncmp(descriptor_text, "", strlen("")) == 0) + { + desc_list[list_num].push_back('\0'); + descriptor_text = strstr(descriptor_text, ""); + if(!descriptor_text) + break; - descriptor_text += strlen(""); - list_num++; - } + descriptor_text += strlen(""); + list_num++; + } - if(list_num >= desc_list.size()) - desc_list.resize(list_num+1); + if(list_num >= desc_list.size()) + desc_list.resize(list_num+1); - desc_list[list_num].push_back(*descriptor_text); - ++descriptor_text; - } + desc_list[list_num].push_back(*descriptor_text); + ++descriptor_text; + } - delete [] data; + delete [] data; - return desc_list.size(); + return desc_list.size(); } -int GameTDB::GetWifiPlayers(const char * id) +int GameTDB::GetWifiPlayers(const char *id) { - int players = -1; + int players = -1; - if(!id) - return players; + if(!id) + return players; - char * data = GetGameNode(id); - if(!data) - return players; + char *data = GetGameNode(id); + if(!data) + return players; - char * PlayersNode = GetNodeText(data, ""); - if(!PlayersNode) - { - delete [] data; - return players; - } + char *PlayersNode = GetNodeText(data, ""); + if(!PlayersNode) + { + delete [] data; + return players; + } - players = atoi(PlayersNode); + players = atoi(PlayersNode); - return players; + return players; } -int GameTDB::GetWifiFeatures(const char * id, vector & feat_list) +int GameTDB::GetWifiFeatures(const char *id, vector & feat_list) { - feat_list.clear(); - if(!id) - return -1; + feat_list.clear(); + if(!id) + return -1; - char * data = GetGameNode(id); - if(!data) - return -1; + char *data = GetGameNode(id); + if(!data) + return -1; - char * feature_text = GetNodeText(data, "", ""); - if(!feature_text) - { - delete [] data; - return -1; - } + char *feature_text = GetNodeText(data, "", ""); + if(!feature_text) + { + delete [] data; + return -1; + } - unsigned int list_num = 0; + u32 list_num = 0; - while(*feature_text != '\0') - { - if(strncmp(feature_text, "", strlen("")) == 0) - { - feat_list[list_num].push_back('\0'); - feature_text = strstr(feature_text, ""); - if(!feature_text) - break; + while(*feature_text != '\0') + { + if(strncmp(feature_text, "", strlen("")) == 0) + { + feat_list[list_num].push_back('\0'); + feature_text = strstr(feature_text, ""); + if(!feature_text) + break; - feature_text += strlen(""); - list_num++; - } + feature_text += strlen(""); + list_num++; + } - if(list_num >= feat_list.size()) - feat_list.resize(list_num+1); + if(list_num >= feat_list.size()) + feat_list.resize(list_num+1); + if(feat_list[list_num].size() == 0) + feat_list[list_num].push_back(toupper((int)*feature_text)); + else + feat_list[list_num].push_back(*feature_text); - if(feat_list[list_num].size() == 0) - feat_list[list_num].push_back(toupper((int)*feature_text)); - else - feat_list[list_num].push_back(*feature_text); + ++feature_text; + } - ++feature_text; - } + delete [] data; - delete [] data; - - return feat_list.size(); + return feat_list.size(); } -int GameTDB::GetPlayers(const char * id) +int GameTDB::GetPlayers(const char *id) { - int players = -1; + int players = -1; - if(!id) - return players; + if(!id) + return players; - char * data = GetGameNode(id); - if(!data) - return players; + char *data = GetGameNode(id); + if(!data) + return players; - char * PlayersNode = GetNodeText(data, ""); - if(!PlayersNode) - { - delete [] data; - return players; - } + char *PlayersNode = GetNodeText(data, ""); + if(!PlayersNode) + { + delete [] data; + return players; + } - players = atoi(PlayersNode); + players = atoi(PlayersNode); - return players; + return players; } -int GameTDB::GetAccessories(const char * id, vector & acc_list) +int GameTDB::GetAccessories(const char *id, vector & acc_list) { - acc_list.clear(); - if(!id) - return -1; + acc_list.clear(); + if(!id) + return -1; - char * data = GetGameNode(id); - if(!data) - return -1; + char *data = GetGameNode(id); + if(!data) + return -1; - char * ControlsNode = GetNodeText(data, ""); - if(!ControlsNode) - { - delete [] data; - return -1; - } + char *ControlsNode = GetNodeText(data, ""); + if(!ControlsNode) + { + delete [] data; + return -1; + } - unsigned int list_num = 0; + u32 list_num = 0; - while(ControlsNode && *ControlsNode != '\0') - { - if(list_num >= acc_list.size()) - acc_list.resize(list_num+1); + while(ControlsNode && *ControlsNode != '\0') + { + if(list_num >= acc_list.size()) + acc_list.resize(list_num+1); - for(const char * ptr = ControlsNode; *ptr != '"' && *ptr != '\0'; ptr++) - acc_list[list_num].Name.push_back(*ptr); + for(const char *ptr = ControlsNode; *ptr != '"' && *ptr != '\0'; ptr++) + acc_list[list_num].Name.push_back(*ptr); + acc_list[list_num].Name.push_back('\0'); - acc_list[list_num].Name.push_back('\0'); + char *requiredField = strstr(ControlsNode, "required=\""); + if(!requiredField) + { + delete [] data; + return -1; + } - char * requiredField = strstr(ControlsNode, "required=\""); - if(!requiredField) - { - delete [] data; - return -1; - } + requiredField += strlen("required=\""); - requiredField += strlen("required=\""); + acc_list[list_num].Required = strncmp(requiredField, "true", 4) == 0; - acc_list[list_num].Required = strncmp(requiredField, "true", 4) == 0; + ControlsNode = strstr(requiredField, "GameID = id; - GetTitle(id, gameInfo->Title); - GetSynopsis(id, gameInfo->Synopsis); - GetRegion(id, gameInfo->Region); - GetDeveloper(id, gameInfo->Developer); - GetPublisher(id, gameInfo->Publisher); - gameInfo->PublishDate = GetPublishDate(id); - GetGenres(id, gameInfo->Genres); - gameInfo->RatingType = GetRating(id); - GetRatingValue(id, gameInfo->RatingValue); - GetRatingDescriptors(id, gameInfo->RatingDescriptors); - gameInfo->WifiPlayers = GetWifiPlayers(id); - GetWifiFeatures(id, gameInfo->WifiFeatures); - gameInfo->Players = GetPlayers(id); - GetAccessories(id, gameInfo->Accessories); - gameInfo->CaseColor = GetCaseColor(id); + GetTitle(id, gameInfo->Title); + GetSynopsis(id, gameInfo->Synopsis); + GetRegion(id, gameInfo->Region); + GetDeveloper(id, gameInfo->Developer); + GetPublisher(id, gameInfo->Publisher); + gameInfo->PublishDate = GetPublishDate(id); + GetGenres(id, gameInfo->Genres); + gameInfo->RatingType = GetRating(id); + GetRatingValue(id, gameInfo->RatingValue); + GetRatingDescriptors(id, gameInfo->RatingDescriptors); + gameInfo->WifiPlayers = GetWifiPlayers(id); + GetWifiFeatures(id, gameInfo->WifiFeatures); + gameInfo->Players = GetPlayers(id); + GetAccessories(id, gameInfo->Accessories); + gameInfo->CaseColor = GetCaseColor(id); - return true; + return true; } bool GameTDB::IsLoaded() diff --git a/source/gui/video.cpp b/source/gui/video.cpp index d84ad3c2..3fa1ea38 100644 --- a/source/gui/video.cpp +++ b/source/gui/video.cpp @@ -489,7 +489,7 @@ void CVideo::_showWaitMessages(CVideo *m) m->m_showingWaitMessages = true; u32 frames = m->m_waitMessageDelay * 50; u32 waitFrames = frames; - + u8 fadeStep = 2 * (u32) (255.f / (waitFrames * m->m_waitMessages.size())); s8 fadeDirection = 1; s8 PNGfadeDirection = 1; @@ -497,10 +497,11 @@ void CVideo::_showWaitMessages(CVideo *m) vector::iterator waitItr = m->m_waitMessages.begin(); gprintf("Going to show a wait message screen, delay: %d, # images: %d\n", waitFrames, m->m_waitMessages.size()); + m->_clearScreen(); - m->waitMessage(*waitItr); - waitItr += PNGfadeDirection; - + m->prepare(); + m->setup2DProjection(); + GX_SetNumChans(0); wiiLightSetLevel(0); wiiLightOn(); @@ -523,16 +524,15 @@ void CVideo::_showWaitMessages(CVideo *m) { m->waitMessage(*waitItr); waitItr += PNGfadeDirection; - if(waitItr == m->m_waitMessages.end()) waitItr = m->m_waitMessages.begin(); - waitFrames = frames; } - waitFrames--; VIDEO_WaitVSync(); + waitFrames--; } wiiLightOff(); + GX_SetNumChans(1); m->m_showingWaitMessages = false; gprintf("Stop showing images\n"); } @@ -584,26 +584,23 @@ void CVideo::waitMessage(float delay) void CVideo::waitMessage(const vector &tex, float delay) { hideWaitMessage(); - if(tex.size() == 0) { m_waitMessages = m_defaultWaitMessages; - m_waitMessageDelay = 0.2f; + m_waitMessageDelay = 0.1f; } else { m_waitMessages = tex; m_waitMessageDelay = delay; } - _clearScreen(); - if (m_waitMessages.size() == 1) + if(m_waitMessages.size() == 1) waitMessage(m_waitMessages[0]); else if(m_waitMessages.size() > 1) { - CheckWaitThread(); m_showWaitMessage = true; - unsigned int stack_size = (unsigned int)32768; + u32 stack_size = (u32)32768; waitThreadStack = smartMem2Alloc(stack_size); LWP_CreateThread(&waitThread, (void *(*)(void *))CVideo::_showWaitMessages, (void *)this, waitThreadStack.get(), stack_size, LWP_PRIO_IDLE); } @@ -614,9 +611,6 @@ void CVideo::waitMessage(const STexture &tex) Mtx modelViewMtx; GXTexObj texObj; - prepare(); - setup2DProjection(); - GX_SetNumChans(0); GX_ClearVtxDesc(); GX_SetVtxDesc(GX_VA_POS, GX_DIRECT); GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_F32, 0); @@ -648,7 +642,6 @@ void CVideo::waitMessage(const STexture &tex) GX_TexCoord2f32(0.f, 1.f); GX_End(); render(); - GX_SetNumChans(1); } s32 CVideo::TakeScreenshot(const char *path) diff --git a/source/gui/video.hpp b/source/gui/video.hpp index ff467c4d..d166afa4 100644 --- a/source/gui/video.hpp +++ b/source/gui/video.hpp @@ -64,6 +64,7 @@ public: bool wide(void) const { return m_wide; } bool vid_50hz(void) const { return m_50hz; } u8 getAA(void) const { return m_aa; } + bool showingWaitMessage() { return m_showingWaitMessages; } void set2DViewport(u32 w, u32 h, int x, int y); void prepareStencil(void); void renderStencil(void); diff --git a/source/list/cachedlist.cpp b/source/list/cachedlist.cpp index 8be39cf2..f1548cc7 100644 --- a/source/list/cachedlist.cpp +++ b/source/list/cachedlist.cpp @@ -38,13 +38,13 @@ void CachedList::Load(string path, string containing, string m_lastLanguage, struct stat filestat, discinfo, cache; gprintf("%s\n", path.c_str()); if(stat(path.c_str(), &filestat) == -1) - return; + return; bool update_lang = m_lastLanguage != m_curLanguage; bool noDB = stat(m_database.c_str(), &cache) == -1; bool mtimes = filestat.st_mtime > cache.st_mtime; if(strcasestr(m_discinf.c_str(), "wbfs") != NULL && stat(m_discinf.c_str(), &discinfo) != -1) - ditimes = discinfo.st_mtime > cache.st_mtime; + ditimes = discinfo.st_mtime > cache.st_mtime; m_update = update_lang || noDB || mtimes || ditimes; if(m_update) @@ -94,7 +94,7 @@ void CachedList::Load(string path, string containing, string m_lastLanguage, remove(path.c_str()); m_loaded = true; - m_update = false; + m_update = false; if(!music && pathlist.size() > 0) { @@ -103,7 +103,7 @@ void CachedList::Load(string path, string containing, string m_lastLanguage, } } else - { + { CCache(*this, m_database, LOAD); m_loaded = true; } diff --git a/source/loader/apploader.c b/source/loader/apploader.c index a898aa3d..95af22a7 100644 --- a/source/loader/apploader.c +++ b/source/loader/apploader.c @@ -22,10 +22,10 @@ typedef void *(*app_final)(); typedef void (*app_entry)(void (**init)(void (*report)(const char *fmt, ...)), int (**main)(), void *(**final)()); /* Apploader pointers */ -static u8 *appldr = (u8 *) 0x81200000; +static u8 *appldr = (u8 *)0x81200000; /* Constants */ -#define APPLDR_OFFSET 0x2440 +#define APPLDR_OFFSET 0x2440 /* Variables */ static u32 buffer[0x20] ATTRIBUTE_ALIGN(32); @@ -57,11 +57,15 @@ s32 Apploader_Run(entry_point *entry, u8 vidMode, GXRModeObj *vmode, bool vipatc /* Calculate apploader length */ appldr_len = buffer[5] + buffer[6]; + SYS_SetArena1Hi((void *)0x816FFFF0); //Kills the possibility of codedumps with gprintf + /* Read apploader code */ ret = WDVD_Read(appldr, appldr_len, APPLDR_OFFSET + 0x20); - if (ret < 0) + if(ret < 0) return ret; + DCFlushRange(appldr, appldr_len); + /* Set apploader entry function */ app_entry appldr_entry = (app_entry)buffer[4]; diff --git a/source/loader/frag.c b/source/loader/frag.c index 63dc6803..18dcf513 100644 --- a/source/loader/frag.c +++ b/source/loader/frag.c @@ -241,7 +241,7 @@ int get_frag_list(u8 *id, char *path, const u32 hdd_sector_size) frag_concat(fa, fs); } - frag_list = MEM1_memalign(32, sizeof(FragList)); + frag_list = MEM1_alloc(ALIGN32(sizeof(FragList))); if(frag_list == NULL) goto out; @@ -307,10 +307,12 @@ int set_frag_list(u8 *id) DCFlushRange(frag_list, size); gprintf("Calling WDVD_SetFragList, frag list size %d\n", size); +/* if (size > 400) ghexdump(frag_list, 400); + else ghexdump(frag_list, size); */ int ret = WDVD_SetFragList(wbfsDev, frag_list, size); - MEM1_free(frag_list); + free(frag_list); frag_list = NULL; if(ret) diff --git a/source/loader/splits.c b/source/loader/splits.c index a9b4da5f..4593f57c 100644 --- a/source/loader/splits.c +++ b/source/loader/splits.c @@ -12,7 +12,7 @@ #include #include "splits.h" -#include "gecko/gecko.h" +#include "gecko.h" #define off64_t off_t #define FMT_llu "%llu" @@ -21,7 +21,7 @@ #define split_error(x) do { gprintf("\nsplit error: %s\n\n",x); } while(0) // 1 cluster less than 4gb -u64 OPT_split_size = (u64) 4LL * 1024 * 1024 * 1024 - 32 * 1024; +u64 OPT_split_size = (u64)4LL * 1024 * 1024 * 1024 - 32 * 1024; // 1 cluster less than 2gb //u64 OPT_split_size = (u64)2LL * 1024 * 1024 * 1024 - 32 * 1024; @@ -88,7 +88,7 @@ int split_fill(split_info_t *s, int idx, u64 size) int fd = split_open_file(s, idx); off64_t fsize = lseek(fd, 0, SEEK_END); - if ((u64)fsize < size) + if((u64)fsize < size) { // gprintf("TRUNC %d "FMT_lld" "FMT_lld"\n", idx, size, fsize); // Wpad_WaitButtons(); ftruncate(fd, size); @@ -149,7 +149,7 @@ int split_get_file(split_info_t *s, u32 lba, u32 *sec_count, int fill) int split_read_sector(void *_fp, u32 lba, u32 count, void *buf) { split_info_t *s = _fp; - int fd; + int fd; u64 off = lba; off *= 512ULL; int i; diff --git a/source/loader/wbfs_ext.c b/source/loader/wbfs_ext.c index 774a5168..ab28b976 100644 --- a/source/loader/wbfs_ext.c +++ b/source/loader/wbfs_ext.c @@ -53,6 +53,8 @@ wbfs_disc_t* WBFS_Ext_OpenDisc(u8 *discid, char *fname) if (fd == -1) return NULL; wbfs_disc_t *iso_file = MEM2_alloc(sizeof(wbfs_disc_t)); + memset(iso_file, 0, sizeof(wbfs_disc_t)); + if (iso_file == NULL) return NULL; diff --git a/source/loader/wip.c b/source/loader/wip.c index b08f08ec..1b870d0a 100644 --- a/source/loader/wip.c +++ b/source/loader/wip.c @@ -14,42 +14,42 @@ static u32 Counter = 0; void do_wip_code(u8 * dst, u32 len) { - if(!CodeList) - return; + if(!CodeList) + return; - if(Counter < 3) - { - Counter++; - return; - } + if(Counter < 3) + { + Counter++; + return; + } - u32 i = 0; - s32 n = 0; - s32 offset = 0; + u32 i = 0; + s32 n = 0; + s32 offset = 0; - for(i = 0; i < CodesCount; i++) - { - for(n = 0; n < 4; n++) - { - offset = CodeList[i].offset+n-ProcessedLength; + for(i = 0; i < CodesCount; i++) + { + for(n = 0; n < 4; n++) + { + offset = CodeList[i].offset+n-ProcessedLength; - if(offset < 0 || (u32)offset >= len) - continue; + if(offset < 0 || (u32)offset >= len) + continue; - if(dst[offset] == ((u8 *)&CodeList[i].srcaddress)[n]) - { - dst[offset] = ((u8 *)&CodeList[i].dstaddress)[n]; - gprintf("WIP: %08X Address Patched.\n", CodeList[i].offset + n); - } - else - { - gprintf("WIP: %08X Address does not match with WIP entry.\n", CodeList[i].offset+n); - gprintf("Destination: %02X | Should be: %02X.\n", dst[offset], ((u8 *)&CodeList[i].srcaddress)[n]); - } - } - } - ProcessedLength += len; - Counter++; + if(dst[offset] == ((u8 *)&CodeList[i].srcaddress)[n]) + { + dst[offset] = ((u8 *)&CodeList[i].dstaddress)[n]; + gprintf("WIP: %08X Address Patched.\n", CodeList[i].offset + n); + } + else + { + gprintf("WIP: %08X Address does not match with WIP entry.\n", CodeList[i].offset+n); + gprintf("Destination: %02X | Should be: %02X.\n", dst[offset], ((u8 *)&CodeList[i].srcaddress)[n]); + } + } + } + ProcessedLength += len; + Counter++; } //! for internal patches only @@ -58,7 +58,7 @@ void do_wip_code(u8 * dst, u32 len) //! if set was successful the codelist will be freed when it's done bool set_wip_list(WIP_Code * list, int size) { - if (!CodeList && size > 0) + if(!CodeList && size > 0) { CodeList = list; CodesCount = size; @@ -70,18 +70,18 @@ bool set_wip_list(WIP_Code * list, int size) void wip_reset_counter() { - ProcessedLength = 0; - //alternative dols don't need a skip. only main.dol. - Counter = 3; + ProcessedLength = 0; + //alternative dols don't need a skip. only main.dol. + Counter = 3; } void free_wip() { if(CodeList) - MEM2_free(CodeList); + MEM2_free(CodeList); - CodesCount = 0; - ProcessedLength = 0; + CodesCount = 0; + ProcessedLength = 0; } int load_wip_patches(u8 *dir, u8 *gameid) @@ -89,54 +89,53 @@ int load_wip_patches(u8 *dir, u8 *gameid) char filepath[150]; char GameID[8]; memset(GameID, 0, sizeof(GameID)); - memcpy(GameID, gameid, 6); + memcpy(GameID, gameid, 6); snprintf(filepath, sizeof(filepath), "%s/%s.wip", dir, GameID); - FILE * fp = fopen(filepath, "rb"); - if (!fp) + FILE *fp = fopen(filepath, "rb"); + if(!fp) { - memset(GameID, 0, sizeof(GameID)); - memcpy(GameID, gameid, 3); + memset(GameID, 0, sizeof(GameID)); + memcpy(GameID, gameid, 3); snprintf(filepath, sizeof(filepath), "%s/%s.wip", dir, GameID); fp = fopen(filepath, "rb"); } - if (!fp) - return -1; + if(!fp) + return -1; - char line[255]; - gprintf("\nLoading WIP code from %s.\n", filepath); + char line[255]; + gprintf("\nLoading WIP code from %s.\n", filepath); - while (fgets(line, sizeof(line), fp)) - { - if (line[0] == '#') continue; + while(fgets(line, sizeof(line), fp)) + { + if(line[0] == '#' || strlen(line) < 26) + continue; - if(strlen(line) < 26) continue; + u32 offset = (u32) strtoul(line, NULL, 16); + u32 srcaddress = (u32) strtoul(line+9, NULL, 16); + u32 dstaddress = (u32) strtoul(line+18, NULL, 16); - u32 offset = (u32) strtoul(line, NULL, 16); - u32 srcaddress = (u32) strtoul(line+9, NULL, 16); - u32 dstaddress = (u32) strtoul(line+18, NULL, 16); - - if(!CodeList) + if(!CodeList) CodeList = MEM2_alloc(sizeof(WIP_Code)); - WIP_Code *tmp = MEM2_realloc(CodeList, (CodesCount+1)*sizeof(WIP_Code)); - if(!tmp) - { - MEM2_free(CodeList); - fclose(fp); - return -1; - } + WIP_Code *tmp = MEM2_realloc(CodeList, (CodesCount+1)*sizeof(WIP_Code)); + if(!tmp) + { + MEM2_free(CodeList); + fclose(fp); + return -1; + } - CodeList = tmp; + CodeList = tmp; - CodeList[CodesCount].offset = offset; - CodeList[CodesCount].srcaddress = srcaddress; - CodeList[CodesCount].dstaddress = dstaddress; - CodesCount++; - } - fclose(fp); - gprintf("\n"); + CodeList[CodesCount].offset = offset; + CodeList[CodesCount].srcaddress = srcaddress; + CodeList[CodesCount].dstaddress = dstaddress; + CodesCount++; + } + fclose(fp); + gprintf("\n"); return 0; } diff --git a/source/main.cpp b/source/main.cpp index 5a653150..97b74005 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -37,7 +37,7 @@ int main(int argc, char **argv) vid.init(); MEM2_init(47); //Should be safe to use - vid.waitMessage(0.2f); + vid.waitMessage(0.1f); AllocSDGeckoBuffer(); gprintf(" \nWelcome to %s (%s-r%s)!\nThis is the debug output.\n", APP_NAME, APP_VERSION, SVN_REV); diff --git a/source/menu/menu.cpp b/source/menu/menu.cpp index 4840bf3f..28c8a2e2 100644 --- a/source/menu/menu.cpp +++ b/source/menu/menu.cpp @@ -1812,8 +1812,9 @@ void CMenu::_mainLoopCommon(bool withCF, bool blockReboot, bool adjusting) m_btnMgr.draw(); ScanInput(); + if(!m_vid.showingWaitMessage()) + m_vid.render(); - m_vid.render(); if(!blockReboot) { if(withCF && Sys_Exiting()) diff --git a/source/menu/menu_game.cpp b/source/menu/menu_game.cpp index ba13f9e4..af2bf3a9 100644 --- a/source/menu/menu_game.cpp +++ b/source/menu/menu_game.cpp @@ -1427,7 +1427,6 @@ void CMenu::_launchGame(dir_discHdr *hdr, bool dvd) if(currentPartition == 0) SDHC_Init(); - GeckoDisable(); #endif RunApploader(offset, videoMode, vipatch, countryPatch, patchVidMode, aspectRatio, returnTo); diff --git a/source/menu/menu_main.cpp b/source/menu/menu_main.cpp index 9742d610..f39948e9 100644 --- a/source/menu/menu_main.cpp +++ b/source/menu/menu_main.cpp @@ -188,11 +188,12 @@ int CMenu::GetCoverStatusAsync(CMenu *m) void CMenu::LoadView(void) { - _showWaitMessage(); - _hideMain(); - m_curGameId = m_cf.getId(); + _hideMain(true); + m_cf.clear(); + _showWaitMessage(); + _loadList(); _showMain(); _initCF(); @@ -418,8 +419,10 @@ int CMenu::main(void) } else if(m_btnMgr.selected(m_mainBtnDVD)) { - _showWaitMessage(); _hideMain(true); + m_cf.clear(); + _showWaitMessage(); + dir_discHdr hdr; memset(&hdr, 0, sizeof(dir_discHdr)); memcpy(&hdr.id, "dvddvd", 6); diff --git a/source/music/AifDecoder.cpp b/source/music/AifDecoder.cpp index 19c10aa1..b900b3d1 100644 --- a/source/music/AifDecoder.cpp +++ b/source/music/AifDecoder.cpp @@ -53,37 +53,39 @@ typedef struct # define UnsignedToFloat(u) (((double)((long)(u - 2147483647L - 1))) + 2147483648.0) -static double ConvertFromIeeeExtended(const unsigned char* bytes) +static double ConvertFromIeeeExtended(const u8* bytes) { double f; int expon; - unsigned long hiMant, loMant; + u64 hiMant, loMant; expon = ((bytes[0] & 0x7F) << 8) | (bytes[1] & 0xFF); - hiMant = ((unsigned long)(bytes[2] & 0xFF) << 24) - | ((unsigned long)(bytes[3] & 0xFF) << 16) - | ((unsigned long)(bytes[4] & 0xFF) << 8) - | ((unsigned long)(bytes[5] & 0xFF)); - loMant = ((unsigned long)(bytes[6] & 0xFF) << 24) - | ((unsigned long)(bytes[7] & 0xFF) << 16) - | ((unsigned long)(bytes[8] & 0xFF) << 8) - | ((unsigned long)(bytes[9] & 0xFF)); - if (expon == 0 && hiMant == 0 && loMant == 0) { + hiMant = ((u64)(bytes[2] & 0xFF) << 24) + | ((u64)(bytes[3] & 0xFF) << 16) + | ((u64)(bytes[4] & 0xFF) << 8) + | ((u64)(bytes[5] & 0xFF)); + + loMant = ((u64)(bytes[6] & 0xFF) << 24) + | ((u64)(bytes[7] & 0xFF) << 16) + | ((u64)(bytes[8] & 0xFF) << 8) + | ((u64)(bytes[9] & 0xFF)); + + if(expon == 0 && hiMant == 0 && loMant == 0) f = 0; - } - else { - if (expon == 0x7FFF) { + else + { + if(expon == 0x7FFF) f = HUGE_VAL; - } - else { + else + { expon -= 16383; - f = ldexp(UnsignedToFloat(hiMant), expon-=31); + f = ldexp(UnsignedToFloat(hiMant), expon-=31); f += ldexp(UnsignedToFloat(loMant), expon-=32); } } - if (bytes[0] & 0x80) + if(bytes[0] & 0x80) return -f; else return f; @@ -227,9 +229,7 @@ int AifDecoder::Read(u8 * buffer, int buffer_size, int) int read = file_fd->read(buffer, buffer_size); if(read > 0) - { CurPos += read; - } return read; } diff --git a/source/music/AifDecoder.hpp b/source/music/AifDecoder.hpp index f9c6fd91..629c84f3 100644 --- a/source/music/AifDecoder.hpp +++ b/source/music/AifDecoder.hpp @@ -31,20 +31,20 @@ class AifDecoder : public SoundDecoder { - public: - AifDecoder(const char * filepath); - AifDecoder(const u8 * snd, int len); - ~AifDecoder(); - int GetFormat() { return Format; }; - int GetSampleRate() { return SampleRate; }; - int Read(u8 * buffer, int buffer_size, int pos); - protected: - void OpenFile(); - void CloseFile(); - u32 DataOffset; - u32 DataSize; - u32 SampleRate; - u8 Format; +public: + AifDecoder(const char *filepath); + AifDecoder(const u8 *snd, int len); + ~AifDecoder(); + int GetFormat() { return Format; }; + int GetSampleRate() { return SampleRate; }; + int Read(u8 *buffer, int buffer_size, int pos); +protected: + void OpenFile(); + void CloseFile(); + u32 DataOffset; + u32 DataSize; + u32 SampleRate; + u8 Format; }; #endif diff --git a/source/music/BNSDecoder.cpp b/source/music/BNSDecoder.cpp index de37e883..a8cf1e3d 100644 --- a/source/music/BNSDecoder.cpp +++ b/source/music/BNSDecoder.cpp @@ -32,109 +32,109 @@ #include "BNSDecoder.hpp" BNSDecoder::BNSDecoder(const char * filepath) - : SoundDecoder(filepath) + : SoundDecoder(filepath) { - SoundType = SOUND_BNS; - memset(&SoundData, 0, sizeof(SoundBlock)); + SoundType = SOUND_BNS; + memset(&SoundData, 0, sizeof(SoundBlock)); - if(!file_fd) - return; + if(!file_fd) + return; - OpenFile(); + OpenFile(); } BNSDecoder::BNSDecoder(const u8 * snd, int len) - : SoundDecoder(snd, len) + : SoundDecoder(snd, len) { - SoundType = SOUND_BNS; - memset(&SoundData, 0, sizeof(SoundBlock)); + SoundType = SOUND_BNS; + memset(&SoundData, 0, sizeof(SoundBlock)); - if(!file_fd) - return; + if(!file_fd) + return; - OpenFile(); + OpenFile(); } BNSDecoder::~BNSDecoder() { - ExitRequested = true; - while(Decoding) - usleep(100); + ExitRequested = true; + while(Decoding) + usleep(100); - MEM2_free(SoundData.buffer); + MEM2_free(SoundData.buffer); } void BNSDecoder::OpenFile() { - u8 * tempbuff = new (std::nothrow) u8[file_fd->size()]; - if(!tempbuff) - { - CloseFile(); - return; - } + u8 *tempbuff = new (std::nothrow) u8[file_fd->size()]; + if(!tempbuff) + { + CloseFile(); + return; + } - int done = 0; + int done = 0; - while(done < file_fd->size()) - { - int read = file_fd->read(tempbuff, file_fd->size()); - if(read > 0) - done += read; - else - { - CloseFile(); - return; - } - } + while(done < file_fd->size()) + { + int read = file_fd->read(tempbuff, file_fd->size()); + if(read > 0) + done += read; + else + { + CloseFile(); + return; + } + } - SoundData = DecodefromBNS(tempbuff, done); - if(SoundData.buffer == NULL) - { - CloseFile(); - return; - } + SoundData = DecodefromBNS(tempbuff, done); + if(SoundData.buffer == NULL) + { + CloseFile(); + return; + } - delete [] tempbuff; - tempbuff = NULL; + delete [] tempbuff; + tempbuff = NULL; - Decode(); + Decode(); } void BNSDecoder::CloseFile() { - if(file_fd) - delete file_fd; + if(file_fd) + delete file_fd; - file_fd = NULL; + file_fd = NULL; } int BNSDecoder::Read(u8 * buffer, int buffer_size, int) { - if(!SoundData.buffer) - return -1; + if(!SoundData.buffer) + return -1; - if(SoundData.loopFlag) - { - int factor = SoundData.format == VOICE_STEREO_16BIT ? 4 : 2; - if(CurPos >= (int) SoundData.loopEnd*factor) - CurPos = SoundData.loopStart*factor; + if(SoundData.loopFlag) + { + int factor = SoundData.format == VOICE_STEREO_16BIT ? 4 : 2; + if(CurPos >= (int) SoundData.loopEnd*factor) + CurPos = SoundData.loopStart*factor; - if(buffer_size > (int) SoundData.loopEnd*factor-CurPos) - buffer_size = SoundData.loopEnd*factor-CurPos; - } - else - { - if(CurPos >= (int) SoundData.size) - return 0; + if(buffer_size > (int) SoundData.loopEnd*factor-CurPos) + buffer_size = SoundData.loopEnd*factor-CurPos; + } + else + { + if(CurPos >= (int) SoundData.size) + return 0; - if(buffer_size > (int) SoundData.size-CurPos) - buffer_size = SoundData.size-CurPos; - } + if(buffer_size > (int) SoundData.size-CurPos) + buffer_size = SoundData.size-CurPos; + } - memcpy(buffer, SoundData.buffer+CurPos, buffer_size); - CurPos += buffer_size; + memcpy(buffer, SoundData.buffer+CurPos, buffer_size); + CurPos += buffer_size; - return buffer_size; + return buffer_size; } struct BNSHeader @@ -306,8 +306,8 @@ static u8 * decodeBNS(u32 &size, const BNSInfo &bnsInfo, const BNSData &bnsData) SoundBlock DecodefromBNS(const u8 *buffer, u32 size) { - SoundBlock OutBlock; - memset(&OutBlock, 0, sizeof(SoundBlock)); + SoundBlock OutBlock; + memset(&OutBlock, 0, sizeof(SoundBlock)); const BNSHeader &hdr = *(BNSHeader *)buffer; if (size < sizeof hdr) diff --git a/source/music/BNSDecoder.hpp b/source/music/BNSDecoder.hpp index 4c9d76c3..dda40ad6 100644 --- a/source/music/BNSDecoder.hpp +++ b/source/music/BNSDecoder.hpp @@ -30,7 +30,7 @@ typedef struct _SoundBlock { - u8 * buffer; + u8 *buffer; u32 size; u8 format; u32 frequency; @@ -41,17 +41,17 @@ typedef struct _SoundBlock class BNSDecoder : public SoundDecoder { - public: - BNSDecoder(const char * filepath); - BNSDecoder(const u8 * snd, int len); - ~BNSDecoder(); - int GetFormat() { return SoundData.format; }; - int GetSampleRate() { return SoundData.frequency; }; - int Read(u8 * buffer, int buffer_size, int pos); - protected: - void OpenFile(); - void CloseFile(); - SoundBlock SoundData; +public: + BNSDecoder(const char * filepath); + BNSDecoder(const u8 * snd, int len); + ~BNSDecoder(); + int GetFormat() { return SoundData.format; }; + int GetSampleRate() { return SoundData.frequency; }; + int Read(u8 * buffer, int buffer_size, int pos); +protected: + void OpenFile(); + void CloseFile(); + SoundBlock SoundData; }; SoundBlock DecodefromBNS(const u8 *buffer, u32 size); diff --git a/source/music/BufferCircle.hpp b/source/music/BufferCircle.hpp index 6eec4b6e..ca805a0c 100644 --- a/source/music/BufferCircle.hpp +++ b/source/music/BufferCircle.hpp @@ -32,62 +32,62 @@ class BufferCircle { - public: - //!> Constructor - BufferCircle(); - //!> Destructor - ~BufferCircle(); - //!> Set circle size - void Resize(int size); - //!> Get the circle size - int Size() { return SoundBuffer.size(); }; - //!> Set/resize the buffer size - void SetBufferBlockSize(int size); - //!> Remove a buffer - void RemoveBuffer(int pos); - //!> Set all buffers clear - void ClearBuffer(); - //!> Free all buffers - void FreeBuffer(); - //!> Switch to next buffer - void LoadNext(); - //!> Get the current buffer - u8 * GetBuffer() { if(!Valid(which)) return 0; return SoundBuffer[which]; }; - //!> Get a buffer at a position - u8 * GetBuffer(int pos) { if(!Valid(pos)) return NULL; else return SoundBuffer[pos]; }; - //!> Get next buffer - u8 * GetNextBuffer() { if(Size() <= 0) return 0; else return SoundBuffer[(which+1) % Size()]; }; - //!> Get previous buffer - u8 * GetLastBuffer() { if(Size() <= 0) return 0; else return SoundBuffer[(which+Size()-1) % Size()]; }; - //!> Get current buffer size - u32 GetBufferSize() { if(!Valid(which)) return 0; else return BufferSize[which]; }; - //!> Get buffer size at position - u32 GetBufferSize(int pos) { if(!Valid(pos)) return 0; else return BufferSize[pos]; }; - //!> Get previous buffer size - u32 GetLastBufferSize() { if(Size() <= 0) return 0; else return BufferSize[(which+Size()-1) % Size()]; }; - //!> Is current buffer ready - bool IsBufferReady() { if(!Valid(which)) return false; else return BufferReady[which]; }; - //!> Is a buffer at a position ready - bool IsBufferReady(int pos) { if(!Valid(pos)) return false; else return BufferReady[pos]; }; - //!> Is next buffer ready - bool IsNextBufferReady() { if(Size() <= 0) return false; else return BufferReady[(which+1) % Size()]; }; - //!> Is last buffer ready - bool IsLastBufferReady() { if(Size() <= 0) return false; else return BufferReady[(which+Size()-1) % Size()]; }; - //!> Set a buffer at a position to a ready state - void SetBufferReady(int pos, bool st); - //!> Set the buffersize at a position - void SetBufferSize(int pos, int size); - //!> Get the current position in the circle - u16 Which() { return which; }; - protected: - //!> Check if the position is a valid position in the vector - bool Valid(int pos) { return !(pos < 0 || pos >= Size()); }; +public: + //!> Constructor + BufferCircle(); + //!> Destructor + ~BufferCircle(); + //!> Set circle size + void Resize(int size); + //!> Get the circle size + int Size() { return SoundBuffer.size(); }; + //!> Set/resize the buffer size + void SetBufferBlockSize(int size); + //!> Remove a buffer + void RemoveBuffer(int pos); + //!> Set all buffers clear + void ClearBuffer(); + //!> Free all buffers + void FreeBuffer(); + //!> Switch to next buffer + void LoadNext(); + //!> Get the current buffer + u8 * GetBuffer() { if(!Valid(which)) return 0; return SoundBuffer[which]; }; + //!> Get a buffer at a position + u8 * GetBuffer(int pos) { if(!Valid(pos)) return NULL; else return SoundBuffer[pos]; }; + //!> Get next buffer + u8 * GetNextBuffer() { if(Size() <= 0) return 0; else return SoundBuffer[(which+1) % Size()]; }; + //!> Get previous buffer + u8 * GetLastBuffer() { if(Size() <= 0) return 0; else return SoundBuffer[(which+Size()-1) % Size()]; }; + //!> Get current buffer size + u32 GetBufferSize() { if(!Valid(which)) return 0; else return BufferSize[which]; }; + //!> Get buffer size at position + u32 GetBufferSize(int pos) { if(!Valid(pos)) return 0; else return BufferSize[pos]; }; + //!> Get previous buffer size + u32 GetLastBufferSize() { if(Size() <= 0) return 0; else return BufferSize[(which+Size()-1) % Size()]; }; + //!> Is current buffer ready + bool IsBufferReady() { if(!Valid(which)) return false; else return BufferReady[which]; }; + //!> Is a buffer at a position ready + bool IsBufferReady(int pos) { if(!Valid(pos)) return false; else return BufferReady[pos]; }; + //!> Is next buffer ready + bool IsNextBufferReady() { if(Size() <= 0) return false; else return BufferReady[(which+1) % Size()]; }; + //!> Is last buffer ready + bool IsLastBufferReady() { if(Size() <= 0) return false; else return BufferReady[(which+Size()-1) % Size()]; }; + //!> Set a buffer at a position to a ready state + void SetBufferReady(int pos, bool st); + //!> Set the buffersize at a position + void SetBufferSize(int pos, int size); + //!> Get the current position in the circle + u16 Which() { return which; }; +protected: + //!> Check if the position is a valid position in the vector + bool Valid(int pos) { return !(pos < 0 || pos >= Size()); }; - u16 which; - u32 BufferBlockSize; - std::vector SoundBuffer; - std::vector BufferSize; - std::vector BufferReady; + u16 which; + u32 BufferBlockSize; + std::vector SoundBuffer; + std::vector BufferSize; + std::vector BufferReady; }; #endif diff --git a/source/music/File.cpp b/source/music/File.cpp index a2dd043d..07735be7 100644 --- a/source/music/File.cpp +++ b/source/music/File.cpp @@ -3,143 +3,134 @@ CFile::CFile() { - file_fd = NULL; - mem_file = NULL; - filesize = 0; - Pos = 0; + file_fd = NULL; + mem_file = NULL; + filesize = 0; + Pos = 0; } CFile::CFile(const char * filepath, const char * mode) { - file_fd = NULL; - open(filepath, mode); + file_fd = NULL; + open(filepath, mode); } CFile::CFile(const u8 * mem, int size) { - file_fd = NULL; - open(mem, size); + file_fd = NULL; + open(mem, size); } CFile::~CFile() { - close(); + close(); } int CFile::open(const char * filepath, const char * mode) { - close(); + close(); - file_fd = fopen(filepath, mode); - if(!file_fd) - return -1; + file_fd = fopen(filepath, mode); + if(!file_fd) + return -1; - fseek(file_fd, 0, SEEK_END); - filesize = ftell(file_fd); - rewind(); + fseek(file_fd, 0, SEEK_END); + filesize = ftell(file_fd); + rewind(); - return 0; + return 0; } int CFile::open(const u8 * mem, int size) { - close(); + close(); - mem_file = mem; - filesize = size; + mem_file = mem; + filesize = size; - return 0; + return 0; } void CFile::close() { - if(file_fd) - fclose(file_fd); + if(file_fd) + fclose(file_fd); - file_fd = NULL; - mem_file = NULL; - filesize = 0; - Pos = 0; + file_fd = NULL; + mem_file = NULL; + filesize = 0; + Pos = 0; } int CFile::read(u8 * ptr, size_t size) { - if(file_fd) - { - int ret = fread(ptr, 1, size, file_fd); - if(ret > 0) - Pos += ret; - return ret; - } + if(file_fd) + { + int ret = fread(ptr, 1, size, file_fd); + if(ret > 0) + Pos += ret; + return ret; + } - int readsize = size; + int readsize = size; - if(readsize > (long int) filesize-Pos) - readsize = filesize-Pos; + if(readsize > (long int) filesize-Pos) + readsize = filesize-Pos; - if(readsize <= 0) - return readsize; + if(readsize <= 0) + return readsize; - if(mem_file != NULL) - { - memcpy(ptr, mem_file+Pos, readsize); - Pos += readsize; - return readsize; - } + if(mem_file != NULL) + { + memcpy(ptr, mem_file+Pos, readsize); + Pos += readsize; + return readsize; + } - return -1; + return -1; } int CFile::write(const u8 * ptr, size_t size) { /* - if(size < 0) - return size; + if(size < 0) + return size; */ - if(file_fd) - { - int ret = fwrite(ptr, 1, size, file_fd); - if(ret > 0) - Pos += ret; - return ret; - } + if(file_fd) + { + int ret = fwrite(ptr, 1, size, file_fd); + if(ret > 0) + Pos += ret; + return ret; + } - return -1; + return -1; } int CFile::seek(long int offset, int origin) { int ret = 0; - if(origin == SEEK_SET) - { - Pos = offset; - } - else if(origin == SEEK_CUR) - { - Pos += offset; - } - else if(origin == SEEK_END) - { - Pos = filesize+offset; - } - if(Pos < 0) - { - Pos = 0; - return -1; - } + if(origin == SEEK_SET) + Pos = offset; + else if(origin == SEEK_CUR) + Pos += offset; + else if(origin == SEEK_END) + Pos = filesize+offset; + if(Pos < 0) + { + Pos = 0; + return -1; + } - if(file_fd) - ret = fseek(file_fd, Pos, SEEK_SET); + if(file_fd) + ret = fseek(file_fd, Pos, SEEK_SET); - if(mem_file != NULL) - { - if(Pos > (long int) filesize) - { - Pos = filesize; - return -1; - } - } + if(mem_file != NULL && Pos > (long int)filesize) + { + Pos = filesize; + return -1; + } return ret; } diff --git a/source/music/File.hpp b/source/music/File.hpp index b5e1af75..0ea38ccb 100644 --- a/source/music/File.hpp +++ b/source/music/File.hpp @@ -6,25 +6,25 @@ class CFile { - public: - CFile(); - CFile(const char * filepath, const char * mode); - CFile(const u8 * memory, int memsize); - ~CFile(); - int open(const char * filepath, const char * mode); - int open(const u8 * memory, int memsize); - void close(); - int read(u8 * ptr, size_t size); - int write(const u8 * ptr, size_t size); - int seek(long int offset, int origin); - long int tell() { return Pos; }; - long int size() { return filesize; }; - void rewind() { seek(0, SEEK_SET); }; - protected: - FILE * file_fd; - const u8 * mem_file; - u64 filesize; - long int Pos; +public: + CFile(); + CFile(const char * filepath, const char * mode); + CFile(const u8 * memory, int memsize); + ~CFile(); + int open(const char * filepath, const char * mode); + int open(const u8 * memory, int memsize); + void close(); + int read(u8 * ptr, size_t size); + int write(const u8 * ptr, size_t size); + int seek(long int offset, int origin); + long int tell() { return Pos; }; + long int size() { return filesize; }; + void rewind() { seek(0, SEEK_SET); }; +protected: + FILE * file_fd; + const u8 * mem_file; + u64 filesize; + long int Pos; }; #endif diff --git a/source/music/Mp3Decoder.cpp b/source/music/Mp3Decoder.cpp index 8edf90fb..d5bd2c08 100644 --- a/source/music/Mp3Decoder.cpp +++ b/source/music/Mp3Decoder.cpp @@ -33,94 +33,94 @@ #include "mem2.hpp" Mp3Decoder::Mp3Decoder(const char * filepath) - : SoundDecoder(filepath) + : SoundDecoder(filepath) { - SoundType = SOUND_MP3; + SoundType = SOUND_MP3; ReadBuffer = NULL; - mad_timer_reset(&Timer); + mad_timer_reset(&Timer); mad_stream_init(&Stream); mad_frame_init(&Frame); mad_synth_init(&Synth); if(!file_fd) - return; + return; - OpenFile(); + OpenFile(); } Mp3Decoder::Mp3Decoder(const u8 * snd, int len) - : SoundDecoder(snd, len) + : SoundDecoder(snd, len) { - SoundType = SOUND_MP3; + SoundType = SOUND_MP3; ReadBuffer = NULL; - mad_timer_reset(&Timer); + mad_timer_reset(&Timer); mad_stream_init(&Stream); mad_frame_init(&Frame); mad_synth_init(&Synth); if(!file_fd) - return; + return; - OpenFile(); + OpenFile(); } Mp3Decoder::~Mp3Decoder() { - ExitRequested = true; - while(Decoding) - usleep(100); + ExitRequested = true; + while(Decoding) + usleep(100); - mad_synth_finish(&Synth); - mad_frame_finish(&Frame); - mad_stream_finish(&Stream); + mad_synth_finish(&Synth); + mad_frame_finish(&Frame); + mad_stream_finish(&Stream); - MEM2_free(ReadBuffer); + MEM2_free(ReadBuffer); } void Mp3Decoder::OpenFile() { - GuardPtr = NULL; - ReadBuffer = (u8 *)MEM2_alloc(SoundBlockSize * SoundBlocks); - if(!ReadBuffer) - { - if(file_fd) - delete file_fd; - file_fd = NULL; - return; - } + GuardPtr = NULL; + ReadBuffer = (u8 *)MEM2_alloc(SoundBlockSize * SoundBlocks); + if(!ReadBuffer) + { + if(file_fd) + delete file_fd; + file_fd = NULL; + return; + } - u8 dummybuff[4096]; - int ret = Read((u8 *) &dummybuff, 4096, 0); - if(ret <= 0) - { - if(file_fd) - delete file_fd; - file_fd = NULL; - return; - } + u8 dummybuff[4096]; + int ret = Read((u8 *) &dummybuff, 4096, 0); + if(ret <= 0) + { + if(file_fd) + delete file_fd; + file_fd = NULL; + return; + } - SampleRate = (u32) Frame.header.samplerate; - Format = ((MAD_NCHANNELS(&Frame.header) == 2) ? VOICE_STEREO_16BIT : VOICE_MONO_16BIT); - Rewind(); - Decode(); + SampleRate = (u32) Frame.header.samplerate; + Format = ((MAD_NCHANNELS(&Frame.header) == 2) ? VOICE_STEREO_16BIT : VOICE_MONO_16BIT); + Rewind(); + Decode(); } int Mp3Decoder::Rewind() { - mad_synth_finish(&Synth); - mad_frame_finish(&Frame); - mad_stream_finish(&Stream); - mad_timer_reset(&Timer); + mad_synth_finish(&Synth); + mad_frame_finish(&Frame); + mad_stream_finish(&Stream); + mad_timer_reset(&Timer); mad_stream_init(&Stream); mad_frame_init(&Frame); mad_synth_init(&Synth); - SynthPos = 0; - GuardPtr = NULL; + SynthPos = 0; + GuardPtr = NULL; if(!file_fd) - return -1; + return -1; - return SoundDecoder::Rewind(); + return SoundDecoder::Rewind(); } static inline s16 FixedToShort(mad_fixed_t Fixed) @@ -137,38 +137,38 @@ static inline s16 FixedToShort(mad_fixed_t Fixed) int Mp3Decoder::Read(u8 * buffer, int buffer_size, int) { - if(!file_fd) - return -1; + if(!file_fd) + return -1; - if(Format == VOICE_STEREO_16BIT) - buffer_size &= ~0x0003; - else - buffer_size &= ~0x0001; + if(Format == VOICE_STEREO_16BIT) + buffer_size &= ~0x0003; + else + buffer_size &= ~0x0001; - u8 * write_pos = buffer; - u8 * write_end = buffer+buffer_size; + u8 * write_pos = buffer; + u8 * write_end = buffer+buffer_size; - while(1) - { - while(SynthPos < Synth.pcm.length) - { - if(write_pos >= write_end) - return write_pos-buffer; + while(1) + { + while(SynthPos < Synth.pcm.length) + { + if(write_pos >= write_end) + return write_pos-buffer; - *((s16 *) write_pos) = FixedToShort(Synth.pcm.samples[0][SynthPos]); - write_pos += 2; + *((s16 *)write_pos) = FixedToShort(Synth.pcm.samples[0][SynthPos]); + write_pos += 2; - if(MAD_NCHANNELS(&Frame.header) == 2) - { - *((s16 *) write_pos) = FixedToShort(Synth.pcm.samples[1][SynthPos]); - write_pos += 2; - } - SynthPos++; - } + if(MAD_NCHANNELS(&Frame.header) == 2) + { + *((s16 *)write_pos) = FixedToShort(Synth.pcm.samples[1][SynthPos]); + write_pos += 2; + } + SynthPos++; + } - if(Stream.buffer == NULL || Stream.error == MAD_ERROR_BUFLEN) - { - u8 * ReadStart = ReadBuffer; + if(Stream.buffer == NULL || Stream.error == MAD_ERROR_BUFLEN) + { + u8 *ReadStart = ReadBuffer; int ReadSize = SoundBlockSize*SoundBlocks; int Remaining = 0; @@ -181,14 +181,14 @@ int Mp3Decoder::Read(u8 * buffer, int buffer_size, int) } ReadSize = file_fd->read(ReadStart, ReadSize); - if(ReadSize <= 0) - { + if(ReadSize <= 0) + { GuardPtr = ReadStart; memset(GuardPtr, 0, MAD_BUFFER_GUARD); ReadSize = MAD_BUFFER_GUARD; - } + } - CurPos += ReadSize; + CurPos += ReadSize; mad_stream_buffer(&Stream, ReadBuffer, Remaining+ReadSize); } @@ -196,20 +196,20 @@ int Mp3Decoder::Read(u8 * buffer, int buffer_size, int) { if(MAD_RECOVERABLE(Stream.error)) { - if(Stream.error != MAD_ERROR_LOSTSYNC || !GuardPtr) - continue; + if(Stream.error != MAD_ERROR_LOSTSYNC || !GuardPtr) + continue; } else { if(Stream.error != MAD_ERROR_BUFLEN) - return -1; - else if(Stream.error == MAD_ERROR_BUFLEN && GuardPtr) - return -1; + return -1; + else if(Stream.error == MAD_ERROR_BUFLEN && GuardPtr) + return -1; } } - mad_timer_add(&Timer,Frame.header.duration); - mad_synth_frame(&Synth,&Frame); - SynthPos = 0; - } + mad_timer_add(&Timer,Frame.header.duration); + mad_synth_frame(&Synth,&Frame); + SynthPos = 0; + } } diff --git a/source/music/Mp3Decoder.hpp b/source/music/Mp3Decoder.hpp index a622f1f3..b7bff3c8 100644 --- a/source/music/Mp3Decoder.hpp +++ b/source/music/Mp3Decoder.hpp @@ -29,23 +29,23 @@ class Mp3Decoder : public SoundDecoder { - public: - Mp3Decoder(const char * filepath); - Mp3Decoder(const u8 * sound, int len); - ~Mp3Decoder(); - int GetFormat() { return Format; }; - int GetSampleRate() { return SampleRate; }; - int Rewind(); - int Read(u8 * buffer, int buffer_size, int pos); - protected: - void OpenFile(); - struct mad_stream Stream; - struct mad_frame Frame; - struct mad_synth Synth; - mad_timer_t Timer; - u8 * GuardPtr; - u8 * ReadBuffer; - u8 Format; - u32 SampleRate; - u32 SynthPos; +public: + Mp3Decoder(const char * filepath); + Mp3Decoder(const u8 * sound, int len); + ~Mp3Decoder(); + int GetFormat() { return Format; }; + int GetSampleRate() { return SampleRate; }; + int Rewind(); + int Read(u8 * buffer, int buffer_size, int pos); +protected: + void OpenFile(); + struct mad_stream Stream; + struct mad_frame Frame; + struct mad_synth Synth; + mad_timer_t Timer; + u8 * GuardPtr; + u8 * ReadBuffer; + u8 Format; + u32 SampleRate; + u32 SynthPos; }; diff --git a/source/music/OggDecoder.cpp b/source/music/OggDecoder.cpp index bbcec771..e102f131 100644 --- a/source/music/OggDecoder.cpp +++ b/source/music/OggDecoder.cpp @@ -28,7 +28,7 @@ extern "C" int ogg_read(void * punt, int bytes, int blocks, int *f) { - return ((CFile *) f)->read((u8 *) punt, bytes*blocks); + return ((CFile *) f)->read((u8 *) punt, bytes*blocks); } extern "C" int ogg_seek(int *f, ogg_int64_t offset, int mode) @@ -38,106 +38,106 @@ extern "C" int ogg_seek(int *f, ogg_int64_t offset, int mode) extern "C" int ogg_close(int *f) { - ((CFile *) f)->close(); + ((CFile*)f)->close(); return 0; } extern "C" long ogg_tell(int *f) { - return (long) ((CFile *) f)->tell(); + return (long)((CFile *)f)->tell(); } static ov_callbacks callbacks = { - (size_t (*)(void *, size_t, size_t, void *)) ogg_read, - (int (*)(void *, ogg_int64_t, int)) ogg_seek, - (int (*)(void *)) ogg_close, - (long (*)(void *)) ogg_tell + (size_t (*)(void *, size_t, size_t, void *)) ogg_read, + (int (*)(void *, ogg_int64_t, int)) ogg_seek, + (int (*)(void *)) ogg_close, + (long (*)(void *)) ogg_tell }; OggDecoder::OggDecoder(const char * filepath) - : SoundDecoder(filepath) + : SoundDecoder(filepath) { - SoundType = SOUND_OGG; + SoundType = SOUND_OGG; - if(!file_fd) - return; + if(!file_fd) + return; - OpenFile(); + OpenFile(); } OggDecoder::OggDecoder(const u8 * snd, int len) - : SoundDecoder(snd, len) + : SoundDecoder(snd, len) { - SoundType = SOUND_OGG; + SoundType = SOUND_OGG; - if(!file_fd) - return; + if(!file_fd) + return; - OpenFile(); + OpenFile(); } OggDecoder::~OggDecoder() { - ExitRequested = true; - while(Decoding) - usleep(100); + ExitRequested = true; + while(Decoding) + usleep(100); - if(file_fd) - ov_clear(&ogg_file); + if(file_fd) + ov_clear(&ogg_file); } void OggDecoder::OpenFile() { - if (ov_open_callbacks(file_fd, &ogg_file, NULL, 0, callbacks) < 0) - { - delete file_fd; - file_fd = NULL; - return; - } + if(ov_open_callbacks(file_fd, &ogg_file, NULL, 0, callbacks) < 0) + { + delete file_fd; + file_fd = NULL; + return; + } - ogg_info = ov_info(&ogg_file, -1); - Decode(); + ogg_info = ov_info(&ogg_file, -1); + Decode(); } int OggDecoder::GetFormat() { - if(!file_fd) - return VOICE_STEREO_16BIT; + if(!file_fd) + return VOICE_STEREO_16BIT; - return ((ogg_info->channels == 2) ? VOICE_STEREO_16BIT : VOICE_MONO_16BIT); + return ((ogg_info->channels == 2) ? VOICE_STEREO_16BIT : VOICE_MONO_16BIT); } int OggDecoder::GetSampleRate() { - if(!file_fd) - return 0; + if(!file_fd) + return 0; - return (int) ogg_info->rate; + return (int) ogg_info->rate; } int OggDecoder::Rewind() { - if(!file_fd) - return -1; + if(!file_fd) + return -1; - int ret = ov_time_seek(&ogg_file, 0); - CurPos = 0; - EndOfFile = false; + int ret = ov_time_seek(&ogg_file, 0); + CurPos = 0; + EndOfFile = false; - return ret; + return ret; } int OggDecoder::Read(u8 * buffer, int buffer_size, int) { - if(!file_fd) - return -1; + if(!file_fd) + return -1; - int bitstream = 0; + int bitstream = 0; - int read = ov_read(&ogg_file, (char *) buffer, buffer_size, &bitstream); + int read = ov_read(&ogg_file, (char *) buffer, buffer_size, &bitstream); - if(read > 0) - CurPos += read; + if(read > 0) + CurPos += read; - return read; + return read; } diff --git a/source/music/OggDecoder.hpp b/source/music/OggDecoder.hpp index 49de225e..32efde35 100644 --- a/source/music/OggDecoder.hpp +++ b/source/music/OggDecoder.hpp @@ -30,16 +30,16 @@ class OggDecoder : public SoundDecoder { - public: - OggDecoder(const char * filepath); - OggDecoder(const u8 * snd, int len); - ~OggDecoder(); - int GetFormat(); - int GetSampleRate(); - int Rewind(); - int Read(u8 * buffer, int buffer_size, int pos); - protected: - void OpenFile(); - OggVorbis_File ogg_file; - vorbis_info *ogg_info; +public: + OggDecoder(const char * filepath); + OggDecoder(const u8 * snd, int len); + ~OggDecoder(); + int GetFormat(); + int GetSampleRate(); + int Rewind(); + int Read(u8 * buffer, int buffer_size, int pos); +protected: + void OpenFile(); + OggVorbis_File ogg_file; + vorbis_info *ogg_info; }; diff --git a/source/music/SoundDecoder.cpp b/source/music/SoundDecoder.cpp index 91b65b10..46ecff88 100644 --- a/source/music/SoundDecoder.cpp +++ b/source/music/SoundDecoder.cpp @@ -32,123 +32,123 @@ SoundDecoder::SoundDecoder() { - file_fd = NULL; - Init(); + file_fd = NULL; + Init(); } SoundDecoder::SoundDecoder(const char * filepath) { - file_fd = new CFile(filepath, "rb"); - Init(); + file_fd = new CFile(filepath, "rb"); + Init(); } SoundDecoder::SoundDecoder(const u8 * buffer, int size) { - file_fd = new CFile(buffer, size); - Init(); + file_fd = new CFile(buffer, size); + Init(); } SoundDecoder::~SoundDecoder() { - ExitRequested = true; - while(Decoding) - usleep(100); + ExitRequested = true; + while(Decoding) + usleep(100); - if(file_fd) - delete file_fd; - file_fd = NULL; + if(file_fd) + delete file_fd; + file_fd = NULL; } void SoundDecoder::Init() { - SoundType = SOUND_RAW; - SoundBlocks = 8; //Settings.SoundblockCount; - SoundBlockSize = 8092; //Settings.SoundblockSize; - CurPos = 0; - Loop = false; - EndOfFile = false; - Decoding = false; - ExitRequested = false; - SoundBuffer.SetBufferBlockSize(SoundBlockSize); - SoundBuffer.Resize(SoundBlocks); + SoundType = SOUND_RAW; + SoundBlocks = 8; //Settings.SoundblockCount; + SoundBlockSize = 8092; //Settings.SoundblockSize; + CurPos = 0; + Loop = false; + EndOfFile = false; + Decoding = false; + ExitRequested = false; + SoundBuffer.SetBufferBlockSize(SoundBlockSize); + SoundBuffer.Resize(SoundBlocks); } int SoundDecoder::Rewind() { - CurPos = 0; - EndOfFile = false; - file_fd->rewind(); + CurPos = 0; + EndOfFile = false; + file_fd->rewind(); - return 0; + return 0; } int SoundDecoder::Read(u8 * buffer, int buffer_size, int) { - int ret = file_fd->read(buffer, buffer_size); - CurPos += ret; + int ret = file_fd->read(buffer, buffer_size); + CurPos += ret; - return ret; + return ret; } void SoundDecoder::Decode() { - if(!file_fd || ExitRequested || EndOfFile) - return; + if(!file_fd || ExitRequested || EndOfFile) + return; - u16 newWhich = SoundBuffer.Which(); - u16 i = 0; - for (i = 0; i < SoundBuffer.Size()-2; i++) - { - if(!SoundBuffer.IsBufferReady(newWhich)) - break; + u16 newWhich = SoundBuffer.Which(); + u16 i = 0; + for(i = 0; i < SoundBuffer.Size()-2; i++) + { + if(!SoundBuffer.IsBufferReady(newWhich)) + break; - newWhich = (newWhich+1) % SoundBuffer.Size(); - } + newWhich = (newWhich+1) % SoundBuffer.Size(); + } - if(i == SoundBuffer.Size()-2) - return; + if(i == SoundBuffer.Size()-2) + return; - Decoding = true; + Decoding = true; - int done = 0; - u8 * write_buf = SoundBuffer.GetBuffer(newWhich); - if(!write_buf) - { - ExitRequested = true; - Decoding = false; - return; - } + int done = 0; + u8 *write_buf = SoundBuffer.GetBuffer(newWhich); + if(!write_buf) + { + ExitRequested = true; + Decoding = false; + return; + } - while(done < SoundBlockSize) - { - int ret = Read(&write_buf[done], SoundBlockSize-done, Tell()); + while(done < SoundBlockSize) + { + int ret = Read(&write_buf[done], SoundBlockSize-done, Tell()); - if(ret <= 0) - { - if(Loop) - { - Rewind(); - continue; - } - else - { - EndOfFile = true; - break; - } - } + if(ret <= 0) + { + if(Loop) + { + Rewind(); + continue; + } + else + { + EndOfFile = true; + break; + } + } - done += ret; - } + done += ret; + } - if(done > 0) - { - SoundBuffer.SetBufferSize(newWhich, done); - SoundBuffer.SetBufferReady(newWhich, true); - } + if(done > 0) + { + SoundBuffer.SetBufferSize(newWhich, done); + SoundBuffer.SetBufferReady(newWhich, true); + } if(!SoundBuffer.IsBufferReady((newWhich+1) % SoundBuffer.Size())) - Decode(); + Decode(); - Decoding = false; + Decoding = false; } diff --git a/source/music/SoundHandler.hpp b/source/music/SoundHandler.hpp index b10953ec..664d181f 100644 --- a/source/music/SoundHandler.hpp +++ b/source/music/SoundHandler.hpp @@ -34,35 +34,35 @@ class SoundHandler { - public: - static SoundHandler * Instance(); - static void DestroyInstance(); +public: + static SoundHandler * Instance(); + static void DestroyInstance(); - void AddDecoder(int voice, const char * filepath); - void AddDecoder(int voice, const u8 * snd, int len); - void RemoveDecoder(int voice); - void DestroyDecoder(SoundDecoder * decoder); + void AddDecoder(int voice, const char * filepath); + void AddDecoder(int voice, const u8 * snd, int len); + void RemoveDecoder(int voice); + void DestroyDecoder(SoundDecoder * decoder); - SoundDecoder * Decoder(int i) { return ((i < 0 || i >= MAX_DECODERS) ? NULL : DecoderList[i]); }; - void ThreadSignal() { LWP_ThreadSignal(ThreadQueue); }; - bool IsDecoding() { return Decoding; }; - protected: - SoundHandler(); - ~SoundHandler(); - static void * UpdateThread(void *arg); - void InternalSoundUpdates(); - void ClearDecoderList(); - SoundDecoder * GetSoundDecoder(const char * filepath); - SoundDecoder * GetSoundDecoder(const u8 * sound, int length); + SoundDecoder * Decoder(int i) { return ((i < 0 || i >= MAX_DECODERS) ? NULL : DecoderList[i]); }; + void ThreadSignal() { LWP_ThreadSignal(ThreadQueue); }; + bool IsDecoding() { return Decoding; }; +protected: + SoundHandler(); + ~SoundHandler(); + static void * UpdateThread(void *arg); + void InternalSoundUpdates(); + void ClearDecoderList(); + SoundDecoder * GetSoundDecoder(const char * filepath); + SoundDecoder * GetSoundDecoder(const u8 * sound, int length); - static SoundHandler * instance; - u8 * ThreadStack; - lwp_t SoundThread; - lwpq_t ThreadQueue; - bool Decoding; - bool ExitRequested; + static SoundHandler * instance; + u8 * ThreadStack; + lwp_t SoundThread; + lwpq_t ThreadQueue; + bool Decoding; + bool ExitRequested; - SoundDecoder * DecoderList[MAX_DECODERS]; + SoundDecoder * DecoderList[MAX_DECODERS]; }; #endif diff --git a/source/music/WavDecoder.cpp b/source/music/WavDecoder.cpp index 8a5c585b..0d503274 100644 --- a/source/music/WavDecoder.cpp +++ b/source/music/WavDecoder.cpp @@ -27,46 +27,47 @@ #include "WavDecoder.hpp" WavDecoder::WavDecoder(const char * filepath) - : SoundDecoder(filepath) + : SoundDecoder(filepath) { - SoundType = SOUND_WAV; - SampleRate = 48000; - Format = VOICE_STEREO_16BIT; + SoundType = SOUND_WAV; + SampleRate = 48000; + Format = VOICE_STEREO_16BIT; - if(!file_fd) - return; + if(!file_fd) + return; - OpenFile(); + OpenFile(); } WavDecoder::WavDecoder(const u8 * snd, int len) - : SoundDecoder(snd, len) + : SoundDecoder(snd, len) { - SoundType = SOUND_WAV; - SampleRate = 48000; - Format = VOICE_STEREO_16BIT; + SoundType = SOUND_WAV; + SampleRate = 48000; + Format = VOICE_STEREO_16BIT; - if(!file_fd) - return; + if(!file_fd) + return; - OpenFile(); + OpenFile(); } WavDecoder::~WavDecoder() { + CloseFile(); } void WavDecoder::OpenFile() { - SWaveHdr Header; - SWaveFmtChunk FmtChunk; - memset(&Header, 0, sizeof(SWaveHdr)); - memset(&FmtChunk, 0, sizeof(SWaveFmtChunk)); + SWaveHdr Header; + SWaveFmtChunk FmtChunk; + memset(&Header, 0, sizeof(SWaveHdr)); + memset(&FmtChunk, 0, sizeof(SWaveFmtChunk)); - file_fd->read((u8 *) &Header, sizeof(SWaveHdr)); - file_fd->read((u8 *) &FmtChunk, sizeof(SWaveFmtChunk)); + file_fd->read((u8 *) &Header, sizeof(SWaveHdr)); + file_fd->read((u8 *) &FmtChunk, sizeof(SWaveFmtChunk)); - if (Header.magicRIFF != 'RIFF') + if(Header.magicRIFF != 'RIFF') { CloseFile(); return; @@ -82,10 +83,10 @@ void WavDecoder::OpenFile() return; } - DataOffset = sizeof(SWaveHdr)+le32(FmtChunk.size)+8; - file_fd->seek(DataOffset, SEEK_SET); - SWaveChunk DataChunk; - file_fd->read((u8 *) &DataChunk, sizeof(SWaveChunk)); + DataOffset = sizeof(SWaveHdr)+le32(FmtChunk.size)+8; + file_fd->seek(DataOffset, SEEK_SET); + SWaveChunk DataChunk; + file_fd->read((u8 *) &DataChunk, sizeof(SWaveChunk)); while(DataChunk.magicDATA != 'data') { @@ -99,56 +100,56 @@ void WavDecoder::OpenFile() } } - DataOffset += 8; - DataSize = le32(DataChunk.size); - Is16Bit = (le16(FmtChunk.bps) == 16); - SampleRate = le32(FmtChunk.freq); + DataOffset += 8; + DataSize = le32(DataChunk.size); + Is16Bit = (le16(FmtChunk.bps) == 16); + SampleRate = le32(FmtChunk.freq); - if (le16(FmtChunk.channels) == 1 && le16(FmtChunk.bps) == 8 && le16(FmtChunk.alignment) <= 1) + if(le16(FmtChunk.channels) == 1 && le16(FmtChunk.bps) == 8 && le16(FmtChunk.alignment) <= 1) Format = VOICE_MONO_8BIT; - else if (le16(FmtChunk.channels) == 1 && le16(FmtChunk.bps) == 16 && le16(FmtChunk.alignment) <= 2) + else if(le16(FmtChunk.channels) == 1 && le16(FmtChunk.bps) == 16 && le16(FmtChunk.alignment) <= 2) Format = VOICE_MONO_16BIT; - else if (le16(FmtChunk.channels) == 2 && le16(FmtChunk.bps) == 8 && le16(FmtChunk.alignment) <= 2) + else if(le16(FmtChunk.channels) == 2 && le16(FmtChunk.bps) == 8 && le16(FmtChunk.alignment) <= 2) Format = VOICE_STEREO_8BIT; - else if (le16(FmtChunk.channels) == 2 && le16(FmtChunk.bps) == 16 && le16(FmtChunk.alignment) <= 4) + else if(le16(FmtChunk.channels) == 2 && le16(FmtChunk.bps) == 16 && le16(FmtChunk.alignment) <= 4) Format = VOICE_STEREO_16BIT; - Decode(); + Decode(); } void WavDecoder::CloseFile() { - if(file_fd) - delete file_fd; + if(file_fd) + delete file_fd; - file_fd = NULL; + file_fd = NULL; } int WavDecoder::Read(u8 * buffer, int buffer_size, int) { - if(!file_fd) - return -1; + if(!file_fd) + return -1; - if(CurPos >= (int) DataSize) - return 0; + if(CurPos >= (int) DataSize) + return 0; - file_fd->seek(DataOffset+CurPos, SEEK_SET); + file_fd->seek(DataOffset+CurPos, SEEK_SET); - if(buffer_size > (int) DataSize-CurPos) - buffer_size = DataSize-CurPos; + if(buffer_size > (int) DataSize-CurPos) + buffer_size = DataSize-CurPos; - int read = file_fd->read(buffer, buffer_size); - if(read > 0) - { - if (Is16Bit) - { - read &= ~0x0001; + int read = file_fd->read(buffer, buffer_size); + if(read > 0) + { + if(Is16Bit) + { + read &= ~0x0001; - for (u32 i = 0; i < (u32) (read / sizeof (u16)); ++i) - ((u16 *) buffer)[i] = le16(((u16 *) buffer)[i]); - } - CurPos += read; - } + for(u32 i = 0; i < (u32) (read / sizeof (u16)); ++i) + ((u16 *)buffer)[i] = le16(((u16 *)buffer)[i]); + } + CurPos += read; + } - return read; + return read; } diff --git a/source/music/WavDecoder.hpp b/source/music/WavDecoder.hpp index 4681bf2b..a86a3263 100644 --- a/source/music/WavDecoder.hpp +++ b/source/music/WavDecoder.hpp @@ -55,21 +55,21 @@ typedef struct class WavDecoder : public SoundDecoder { - public: - WavDecoder(const char * filepath); - WavDecoder(const u8 * snd, int len); - ~WavDecoder(); - int GetFormat() { return Format; }; - int GetSampleRate() { return SampleRate; }; - int Read(u8 * buffer, int buffer_size, int pos); - protected: - void OpenFile(); - void CloseFile(); - u32 DataOffset; - u32 DataSize; - u32 SampleRate; - u8 Format; - bool Is16Bit; +public: + WavDecoder(const char * filepath); + WavDecoder(const u8 * snd, int len); + ~WavDecoder(); + int GetFormat() { return Format; }; + int GetSampleRate() { return SampleRate; }; + int Read(u8 * buffer, int buffer_size, int pos); +protected: + void OpenFile(); + void CloseFile(); + u32 DataOffset; + u32 DataSize; + u32 SampleRate; + u8 Format; + bool Is16Bit; }; #endif diff --git a/source/music/gui_sound.cpp b/source/music/gui_sound.cpp index 8dc3fd2f..f32e6495 100644 --- a/source/music/gui_sound.cpp +++ b/source/music/gui_sound.cpp @@ -55,8 +55,9 @@ static inline int GetFirstUnusedVoice() extern "C" void SoundCallback(s32 voice) { - SoundDecoder * decoder = SoundHandler::Instance()->Decoder(voice); - if(!decoder) return; + SoundDecoder *decoder = SoundHandler::Instance()->Decoder(voice); + if(!decoder) + return; if(decoder->IsBufferReady()) { @@ -98,11 +99,12 @@ GuiSound::GuiSound(GuiSound *g) voice = -1; Init(); - if (g == NULL) return; - - if (g->sound != NULL) + if(g == NULL) + return; + + if(g->sound != NULL) { - u8 * snd = (u8 *) malloc(g->length); + u8 *snd = (u8 *) malloc(g->length); memcpy(snd, g->sound, g->length); Load(snd, g->length, true); } @@ -125,7 +127,7 @@ void GuiSound::Init() voice = GetFirstUnusedVoice(); if(voice > 0) VoiceUsed[voice] = true; - + volume = 255; SoundEffectLength = 0; loop = false; @@ -161,7 +163,8 @@ bool GuiSound::Load(const char * filepath) FILE * f = fopen(filepath, "rb"); if(!f) - { gprintf("Failed to load file %s!!\n", filepath); + { + gprintf("Failed to load file %s!!\n", filepath); return false; } @@ -173,12 +176,14 @@ bool GuiSound::Load(const char * filepath) gprintf("Loading %s using voice %d\n", filepath, voice); SoundDecoder * decoder = SoundHandler::Instance()->Decoder(voice); if(!decoder) - { gprintf("No Decoder!!!\n"); + { + gprintf("No Decoder!!!\n"); return false; } - + if(!decoder->IsBufferReady()) - { gprintf("Buffer not ready!!n"); + { + gprintf("Buffer not ready!!\n"); SoundHandler::Instance()->RemoveDecoder(voice); return false; } @@ -194,29 +199,26 @@ bool GuiSound::Load(const u8 * snd, u32 len, bool isallocated) FreeMemory(); this->voice = voice; - if(!snd) return false; + if(!snd) + return false; if(!isallocated && *((u32 *) snd) == 'RIFF') return LoadSoundEffect(snd, len); if(*((u32 *) snd) == 'IMD5') - { UncompressSoundbin(snd, len, isallocated); - } else { sound = (u8 *) snd; length = len; allocated = isallocated; } - + SoundHandler::Instance()->AddDecoder(this->voice, sound, length); SoundDecoder * decoder = SoundHandler::Instance()->Decoder(voice); if(!decoder) - { return false; - } if(!decoder->IsBufferReady()) { @@ -274,10 +276,12 @@ void GuiSound::Play(int vol, bool restart) return; } - if((IsPlaying() && !restart) || voice < 0 || voice >= 16) return; + if((IsPlaying() && !restart) || voice < 0 || voice >= 16) + return; - SoundDecoder * decoder = SoundHandler::Instance()->Decoder(voice); - if(!decoder) return; + SoundDecoder *decoder = SoundHandler::Instance()->Decoder(voice); + if(!decoder) + return; ASND_StopVoice(voice); if(decoder->IsEOF()) @@ -303,17 +307,18 @@ void GuiSound::Play() void GuiSound::Stop() { volume = 0; - if (!IsPlaying() || voice < 0 || voice >= 16) + if(!IsPlaying() || voice < 0 || voice >= 16) return; ASND_StopVoice(voice); - SoundDecoder * decoder = SoundHandler::Instance()->Decoder(voice); - if(!decoder) return; + SoundDecoder *decoder = SoundHandler::Instance()->Decoder(voice); + if(!decoder) + return; decoder->ClearBuffer(); Rewind(); - + SoundHandler::Instance()->ThreadSignal(); } @@ -361,24 +366,26 @@ void GuiSound::SetLoop(u8 l) { loop = l; - SoundDecoder * decoder = SoundHandler::Instance()->Decoder(voice); - if(!decoder) return; + SoundDecoder *decoder = SoundHandler::Instance()->Decoder(voice); + if(!decoder) + return; decoder->SetLoop(l == 1); } void GuiSound::Rewind() { - SoundDecoder * decoder = SoundHandler::Instance()->Decoder(voice); - if(!decoder) return; + SoundDecoder *decoder = SoundHandler::Instance()->Decoder(voice); + if(!decoder) + return; decoder->Rewind(); } struct _LZ77Info { - u16 length : 4; - u16 offset : 12; + u16 length : 4; + u16 offset : 12; } __attribute__((packed)); typedef struct _LZ77Info LZ77Info; @@ -397,24 +404,25 @@ u8 * uncompressLZ77(const u8 *inBuf, u32 inLength, u32 * size) buffer = (u8 *)malloc(uncSize); - if (!buffer) return buffer; + if(!buffer) + return buffer; u8 *bufCur = buffer; u8 *bufEnd = buffer + uncSize; - while (bufCur < bufEnd && inBuf < inBufEnd) + while(bufCur < bufEnd && inBuf < inBufEnd) { u8 flags = *inBuf; ++inBuf; int i = 0; - for (i = 0; i < 8 && bufCur < bufEnd && inBuf < inBufEnd; ++i) + for(i = 0; i < 8 && bufCur < bufEnd && inBuf < inBufEnd; ++i) { - if ((flags & 0x80) != 0) + if((flags & 0x80) != 0) { const LZ77Info * info = (const LZ77Info *)inBuf; inBuf += sizeof (LZ77Info); int length = info->length + 3; - if (bufCur - info->offset - 1 < buffer || bufCur + length > bufEnd) + if(bufCur - info->offset - 1 < buffer || bufCur + length > bufEnd) return buffer; memcpy(bufCur, bufCur - info->offset - 1, length); bufCur += length; @@ -439,7 +447,8 @@ void GuiSound::UncompressSoundbin(const u8 * snd, u32 len, bool isallocated) const u8 * file = snd+32; length = len-32; - if (length <= 0) return; + if(length <= 0) + return; if(*((u32 *) file) == 'LZ77') { diff --git a/source/music/gui_sound.h b/source/music/gui_sound.h index 652efacd..eb6d8d6d 100644 --- a/source/music/gui_sound.h +++ b/source/music/gui_sound.h @@ -32,69 +32,69 @@ //!Sound conversion and playback. A wrapper for other sound libraries - ASND, libmad, ltremor, etc class GuiSound { - public: - //!Constructor - GuiSound(); - //!Copy Constructor - GuiSound(GuiSound *g); - //!Constructor - //!\param sound Pointer to the sound data - //!\param filesize Length of sound data - GuiSound(std::string filepath, int voice = -1); - GuiSound(const u8 * snd, u32 len, std::string name, bool allocated = false, int voice = -1); - //!Stops sound and frees all memory/closes files - void FreeMemory(); - //!Destructor - ~GuiSound(); - //!Load a file and replace the old one - bool Load(const char * filepath); - //!Load a file and replace the old one - bool Load(const u8 * snd, u32 len, bool allocated = true); - //!For quick playback of the internal soundeffects - bool LoadSoundEffect(const u8 * snd, u32 len); - //!Start sound playback - void Play(); - //!Start sound playback - void Play(int vol, bool restart = false); - //!Stop sound playback - void Stop(); - //!Pause sound playback - void Pause(); - //!Resume sound playback - void Resume(); - //!Checks if a sound is currently loaded - //!\return true if sound is loaded, false otherwise - bool IsLoaded() { return sound != NULL; }; - //!Get the filepath for finding sounds which already have an instance. - //!\return the current instance's filepath - std::string GetName() { return filepath; }; - //!Checks if the sound is currently playing - //!\return true if sound is playing, false otherwise - bool IsPlaying(); - //!Rewind the music - void Rewind(); - //!Get sound volume - //!\returns the current sound volume - int GetVolume(); - //!Set sound volume - //!\param v Sound volume (0-100) - void SetVolume(int v); - //!\param l Loop (true to loop) - void SetLoop(u8 l); - private: - //!Initializes the GuiSound object by setting the default values - void Init(); - //!Special sound case for sound.bin - void UncompressSoundbin(const u8 * snd, u32 len, bool isallocated); - protected: - std::string filepath; - u8 *sound; //!< Pointer to the sound data - u32 length; //!< Length of sound data - s32 voice; //!< Currently assigned ASND voice channel - int volume; //!< Sound volume (0-100) - u8 loop; //!< Loop sound playback - u32 SoundEffectLength; //!< Check if it is an app soundeffect for faster playback - bool allocated; //!< Is the file allocated or not +public: + //!Constructor + GuiSound(); + //!Copy Constructor + GuiSound(GuiSound *g); + //!Constructor + //!\param sound Pointer to the sound data + //!\param filesize Length of sound data + GuiSound(std::string filepath, int voice = -1); + GuiSound(const u8 * snd, u32 len, std::string name, bool allocated = false, int voice = -1); + //!Stops sound and frees all memory/closes files + void FreeMemory(); + //!Destructor + ~GuiSound(); + //!Load a file and replace the old one + bool Load(const char * filepath); + //!Load a file and replace the old one + bool Load(const u8 * snd, u32 len, bool allocated = true); + //!For quick playback of the internal soundeffects + bool LoadSoundEffect(const u8 * snd, u32 len); + //!Start sound playback + void Play(); + //!Start sound playback + void Play(int vol, bool restart = false); + //!Stop sound playback + void Stop(); + //!Pause sound playback + void Pause(); + //!Resume sound playback + void Resume(); + //!Checks if a sound is currently loaded + //!\return true if sound is loaded, false otherwise + bool IsLoaded() { return sound != NULL; }; + //!Get the filepath for finding sounds which already have an instance. + //!\return the current instance's filepath + std::string GetName() { return filepath; }; + //!Checks if the sound is currently playing + //!\return true if sound is playing, false otherwise + bool IsPlaying(); + //!Rewind the music + void Rewind(); + //!Get sound volume + //!\returns the current sound volume + int GetVolume(); + //!Set sound volume + //!\param v Sound volume (0-100) + void SetVolume(int v); + //!\param l Loop (true to loop) + void SetLoop(u8 l); +private: + //!Initializes the GuiSound object by setting the default values + void Init(); + //!Special sound case for sound.bin + void UncompressSoundbin(const u8 * snd, u32 len, bool isallocated); +protected: + std::string filepath; + u8 *sound; //!< Pointer to the sound data + u32 length; //!< Length of sound data + s32 voice; //!< Currently assigned ASND voice channel + int volume; //!< Sound volume (0-100) + u8 loop; //!< Loop sound playback + u32 SoundEffectLength; //!< Check if it is an app soundeffect for faster playback + bool allocated; //!< Is the file allocated or not }; void soundInit(void); diff --git a/source/unzip/ZipFile.cpp b/source/unzip/ZipFile.cpp index 09e05305..26df43ce 100644 --- a/source/unzip/ZipFile.cpp +++ b/source/unzip/ZipFile.cpp @@ -39,140 +39,96 @@ ZipFile::ZipFile(const char *filepath) { - File = unzOpen(filepath); - if (File) this->LoadList(); + File = unzOpen(filepath); + if(File) + this->LoadList(); } ZipFile::~ZipFile() { - unzClose(File); + unzClose(File); } bool ZipFile::LoadList() { - return true; -} -/* -bool ZipFile::FindFile(const char *file) -{ - if (!File) return false; - - char filename[MAXPATHLEN]; - - int ret = unzGoToFirstFile(File); - if (ret != UNZ_OK) return false; - - do - { - if(unzGetCurrentFileInfo(File, &cur_file_info, filename, sizeof(filename), NULL, 0, NULL, 0) != UNZ_OK) - continue; - - const char *realfilename = strrchr(filename, '/'); - if(!realfilename || strlen(realfilename) == 0) - realfilename = filename; - - if(strcasecmp(realfilename, file) == 0) - return true; - } - while(unzGoToNextFile(File) == UNZ_OK); - - return false; + return true; } -bool ZipFile::FindFilePart(const char *partfilename, std::string &realname) -{ - if (!File) return false; - - char filename[MAXPATHLEN]; - - int ret = unzGoToFirstFile(File); - if (ret != UNZ_OK) return false; - - do - { - if(unzGetCurrentFileInfo(File, &cur_file_info, filename, sizeof(filename), NULL, 0, NULL, 0) != UNZ_OK) - continue; - - if(strcasestr(filename, partfilename) != 0) - { - realname = filename; - return true; - } - } - while(unzGoToNextFile(File) == UNZ_OK); - - return false; -} -*/ bool ZipFile::ExtractAll(const char *dest) { - if (!File) return false; + if(!File) + return false; - bool Stop = false; + bool Stop = false; - u32 blocksize = 1024 * 50; - u8 *buffer = new u8[blocksize]; + u32 blocksize = 1024 * 50; + u8 *buffer = new u8[blocksize]; - if (!buffer) return false; + if(!buffer) + return false; - char writepath[MAXPATHLEN]; - char filename[MAXPATHLEN]; - memset(filename, 0, sizeof(filename)); + char writepath[MAXPATHLEN]; + char filename[MAXPATHLEN]; + memset(filename, 0, sizeof(filename)); - int ret = unzGoToFirstFile(File); - if (ret != UNZ_OK) Stop = true; + int ret = unzGoToFirstFile(File); + if(ret != UNZ_OK) + Stop = true; - while (!Stop) - { - if (unzGetCurrentFileInfo(File, &cur_file_info, filename, sizeof(filename), NULL, 0, NULL, 0) != UNZ_OK) + while(!Stop) + { + if(unzGetCurrentFileInfo(File, &cur_file_info, filename, sizeof(filename), NULL, 0, NULL, 0) != UNZ_OK) Stop = true; - if (!Stop && filename[strlen(filename) - 1] != '/') - { - u32 uncompressed_size = cur_file_info.uncompressed_size; + if(!Stop && filename[strlen(filename) - 1] != '/') + { + u32 uncompressed_size = cur_file_info.uncompressed_size; - u32 done = 0; - char *pointer = NULL; + u32 done = 0; + char *pointer = NULL; - ret = unzOpenCurrentFile(File); + ret = unzOpenCurrentFile(File); - snprintf(writepath, sizeof(writepath), "%s/%s", dest, filename); + snprintf(writepath, sizeof(writepath), "%s/%s", dest, filename); - pointer = strrchr(writepath, '/'); - int position = pointer - writepath + 2; + pointer = strrchr(writepath, '/'); + int position = pointer - writepath + 2; - char temppath[strlen(writepath)]; - snprintf(temppath, position, "%s", writepath); + char temppath[strlen(writepath)]; + snprintf(temppath, position, "%s", writepath); - fsop_MakeFolder(temppath); + fsop_MakeFolder(temppath); - if (ret == UNZ_OK) - { - FILE *pfile = fopen(writepath, "wb"); + if(ret == UNZ_OK) + { + FILE *pfile = fopen(writepath, "wb"); - do - { - if (uncompressed_size - done < blocksize) blocksize = uncompressed_size - done; + do + { + if(uncompressed_size - done < blocksize) + blocksize = uncompressed_size - done; - ret = unzReadCurrentFile(File, buffer, blocksize); + ret = unzReadCurrentFile(File, buffer, blocksize); - if (ret == 0) break; + if(ret == 0) + break; - fwrite(buffer, 1, blocksize, pfile); + fwrite(buffer, 1, blocksize, pfile); - done += ret; + done += ret; - } while (done < uncompressed_size); + } while(done < uncompressed_size); - fclose(pfile); - unzCloseCurrentFile(File); - } - } - if (unzGoToNextFile(File) != UNZ_OK) Stop = true; - } + fclose(pfile); + unzCloseCurrentFile(File); + } + } + if(unzGoToNextFile(File) != UNZ_OK) + Stop = true; + } - delete[] buffer; - buffer = NULL; + delete[] buffer; + buffer = NULL; - return true; + return true; } diff --git a/source/unzip/ZipFile.h b/source/unzip/ZipFile.h index 3c89da8c..ecbacec1 100644 --- a/source/unzip/ZipFile.h +++ b/source/unzip/ZipFile.h @@ -32,27 +32,27 @@ typedef struct { - u64 offset; // ZipFile offset - u64 length; // uncompressed file length in 64 bytes for sizes higher than 4GB - bool isdir; // 0 - file, 1 - directory - char filename[256]; // full filename + u64 offset; // ZipFile offset + u64 length; // uncompressed file length in 64 bytes for sizes higher than 4GB + bool isdir; // 0 - file, 1 - directory + char filename[256]; // full filename } FileStructure; class ZipFile { - public: - //!Constructor - ZipFile(const char *filepath); - //!Destructor - ~ZipFile(); - //!Extract all files from a zip file to a directory - //!\param dest Destination path to where to extract - bool ExtractAll(const char *dest); - protected: - bool LoadList(); - unzFile File; - unz_file_info cur_file_info; - FileStructure *FileList; +public: + //!Constructor + ZipFile(const char *filepath); + //!Destructor + ~ZipFile(); + //!Extract all files from a zip file to a directory + //!\param dest Destination path to where to extract + bool ExtractAll(const char *dest); +protected: + bool LoadList(); + unzFile File; + unz_file_info cur_file_info; + FileStructure *FileList; }; #endif