From 9aa97b227dc2dd8c42e175a2df6a77bed5829145 Mon Sep 17 00:00:00 2001 From: dborth Date: Tue, 6 Oct 2009 06:37:53 +0000 Subject: [PATCH] many bugfixes (and rewritten code) --- source/ngc/filebrowser.cpp | 2 +- source/ngc/fileop.cpp | 314 +++++++++++++++++++------------------ source/ngc/menu.cpp | 1 + source/ngc/networkop.cpp | 73 +++++---- source/ngc/preferences.cpp | 4 +- source/ngc/snes9xGX.cpp | 7 +- 6 files changed, 212 insertions(+), 189 deletions(-) diff --git a/source/ngc/filebrowser.cpp b/source/ngc/filebrowser.cpp index f0b8141..584c500 100644 --- a/source/ngc/filebrowser.cpp +++ b/source/ngc/filebrowser.cpp @@ -661,7 +661,7 @@ OpenGameList () { int device = GCSettings.LoadMethod; - if(device == DEVICE_AUTO) + if(device == DEVICE_AUTO && strlen(GCSettings.LoadFolder) > 0) device = autoLoadMethod(); // change current dir to roms directory diff --git a/source/ngc/fileop.cpp b/source/ngc/fileop.cpp index 9dc789b..f4fc8e0 100644 --- a/source/ngc/fileop.cpp +++ b/source/ngc/fileop.cpp @@ -242,7 +242,8 @@ void UnmountAllFAT() static bool MountFAT(int device, int silent) { - bool mounted = true; // assume our disc is already mounted + bool mounted = false; + int retry = 1; char name[10], name2[10]; const DISC_INTERFACE* disc = NULL; @@ -283,20 +284,19 @@ static bool MountFAT(int device, int silent) disc->shutdown(); isMounted[device] = false; } - if(!isMounted[device]) - { - if(!disc->startup()) - mounted = false; - else if(!fatMountSimple(name, disc)) - mounted = false; - } - - if(!mounted && !silent) + + while(retry) { + if(disc->startup() && fatMountSimple(name, disc)) + mounted = true; + + if(mounted || silent) + break; + if(device == DEVICE_SD) - ErrorPrompt("SD card not found!"); + retry = ErrorPromptRetry("SD card not found!"); else - ErrorPrompt("USB drive not found!"); + retry = ErrorPromptRetry("USB drive not found!"); } isMounted[device] = mounted; @@ -321,12 +321,8 @@ void MountAllFAT() ***************************************************************************/ bool MountDVD(bool silent) { - if(isMounted[DEVICE_DVD]) - return true; - - ShowAction("Loading DVD..."); - bool mounted = false; + int retry = 1; if(unmountRequired[DEVICE_DVD]) { @@ -334,19 +330,29 @@ bool MountDVD(bool silent) ISO9660_Unmount(); } - mounted = dvd->isInserted(); - - if(!mounted) + while(retry) { - if(!silent) - ErrorPrompt("No disc inserted!"); - } - else - { - mounted = ISO9660_Mount(); + ShowAction("Loading DVD..."); - if(!mounted && !silent) - ErrorPrompt("Invalid DVD."); + if(!dvd->isInserted()) + { + if(silent) + break; + + retry = ErrorPromptRetry("No disc inserted!"); + } + else if(!ISO9660_Mount()) + { + if(silent) + break; + + retry = ErrorPromptRetry("Invalid DVD."); + } + else + { + mounted = true; + break; + } } CancelAction(); isMounted[DEVICE_DVD] = mounted; @@ -420,8 +426,11 @@ char * StripDevice(char * path) ***************************************************************************/ bool ChangeInterface(int device, bool silent) { + if(isMounted[device]) + return true; + bool mounted = false; - + switch(device) { case DEVICE_SD: @@ -503,26 +512,30 @@ bool ParseDirEntries() i--; continue; } - - if(AddBrowserEntry()) + + if(!AddBrowserEntry()) { - strncpy(browserList[browser.numEntries+i].filename, filename, MAXJOLIET); - browserList[browser.numEntries+i].length = filestat.st_size; - browserList[browser.numEntries+i].mtime = filestat.st_mtime; - browserList[browser.numEntries+i].isdir = (filestat.st_mode & _IFDIR) == 0 ? 0 : 1; // flag this as a dir - - if(browserList[browser.numEntries+i].isdir) - { - if(strcmp(filename, "..") == 0) - sprintf(browserList[browser.numEntries+i].displayname, "Up One Level"); - else - strncpy(browserList[browser.numEntries+i].displayname, browserList[browser.numEntries+i].filename, MAXJOLIET); - browserList[browser.numEntries+i].icon = ICON_FOLDER; - } + i=0; + parseHalt = true; + break; + } + + strncpy(browserList[browser.numEntries+i].filename, filename, MAXJOLIET); + browserList[browser.numEntries+i].length = filestat.st_size; + browserList[browser.numEntries+i].mtime = filestat.st_mtime; + browserList[browser.numEntries+i].isdir = (filestat.st_mode & _IFDIR) == 0 ? 0 : 1; // flag this as a dir + + if(browserList[browser.numEntries+i].isdir) + { + if(strcmp(filename, "..") == 0) + sprintf(browserList[browser.numEntries+i].displayname, "Up One Level"); else - { - StripExt(browserList[browser.numEntries+i].displayname, browserList[browser.numEntries+i].filename); // hide file extension - } + strncpy(browserList[browser.numEntries+i].displayname, browserList[browser.numEntries+i].filename, MAXJOLIET); + browserList[browser.numEntries+i].icon = ICON_FOLDER; + } + else + { + StripExt(browserList[browser.numEntries+i].displayname, browserList[browser.numEntries+i].filename); // hide file extension } } @@ -553,7 +566,7 @@ ParseDirectory(bool waitParse) bool mounted = false; ResetBrowser(); // reset browser - + // open the directory while(dirIter == NULL && retry == 1) { @@ -574,14 +587,17 @@ ParseDirectory(bool waitParse) // if we can't open the dir, try higher levels if (dirIter == NULL) { + char * devEnd = strrchr(browser.dir, '/'); + while(!IsDeviceRoot(browser.dir)) { - char * devEnd = strrchr(browser.dir, '/'); + devEnd[0] = 0; // strip slash + devEnd = strrchr(browser.dir, '/'); if(devEnd == NULL) break; - devEnd[0] = 0; // strip remaining file listing + devEnd[1] = 0; // strip remaining file listing dirIter = diropen(browser.dir); if (dirIter) break; @@ -660,7 +676,7 @@ LoadSzFile(char * filepath, unsigned char * rbuffer) HaltDeviceThread(); // halt parsing - parseHalt = true; + HaltParseThread(); file = fopen (filepath, "rb"); if (file > 0) @@ -686,94 +702,85 @@ size_t LoadFile (char * rbuffer, char *filepath, size_t length, bool silent) { char zipbuffer[2048]; - size_t size = 0; - size_t readsize = 0; + size_t size = 0, offset = 0, readsize = 0; int retry = 1; int device; - + if(!FindDevice(filepath, &device)) return 0; - if(device == DEVICE_MC_SLOTA) - return LoadMCFile (rbuffer, CARD_SLOTA, StripDevice(filepath), silent); - else if(device == DEVICE_MC_SLOTB) - return LoadMCFile (rbuffer, CARD_SLOTB, StripDevice(filepath), silent); - // stop checking if devices were removed/inserted // since we're loading a file HaltDeviceThread(); // halt parsing - parseHalt = true; + HaltParseThread(); - // open the file - while(!size && retry == 1) + if(device == DEVICE_MC_SLOTA) { - if(ChangeInterface(device, silent)) + size = LoadMCFile (rbuffer, CARD_SLOTA, StripDevice(filepath), silent); + } + else if(device == DEVICE_MC_SLOTB) + { + size = LoadMCFile (rbuffer, CARD_SLOTB, StripDevice(filepath), silent); + } + else + { + // open the file + while(!size && retry) { + if(!ChangeInterface(device, silent)) + break; + file = fopen (filepath, "rb"); - if(file > 0) + if(!file) { - if(length > 0 && length <= 2048) // do a partial read (eg: to check file header) - { - size = fread (rbuffer, 1, length, file); - } - else // load whole file - { - readsize = fread (zipbuffer, 1, 2048, file); + if(silent) + break; - if(readsize > 0) + retry = ErrorPromptRetry("Error opening file!"); + continue; + } + + if(length > 0 && length <= 2048) // do a partial read (eg: to check file header) + { + size = fread (rbuffer, 1, length, file); + } + else // load whole file + { + readsize = fread (zipbuffer, 1, 32, file); + + if(!readsize) + { + unmountRequired[device] = true; + retry = ErrorPromptRetry("Error reading file!"); + continue; + } + + if (IsZipFile (zipbuffer)) + { + size = UnZipBuffer ((unsigned char *)rbuffer); // unzip + } + else + { + fseeko(file,0,SEEK_END); + size = ftello(file); + fseeko(file,0,SEEK_SET); + + while(!feof(file)) { - if (IsZipFile (zipbuffer)) - { - size = UnZipBuffer ((unsigned char *)rbuffer); // unzip - } - else - { - struct stat fileinfo; - if(fstat(file->_file, &fileinfo) == 0) - { - size = fileinfo.st_size; + ShowProgress ("Loading...", offset, size); + readsize = fread (rbuffer + offset, 1, 4096, file); // read in next chunk - memcpy (rbuffer, zipbuffer, readsize); // copy what we already read + if(readsize <= 0) + break; // reading finished (or failed) - size_t offset = readsize; - size_t nextread = 0; - while(offset < size) - { - if(size - offset > 4*1024) nextread = 4*1024; - else nextread = size-offset; - ShowProgress ("Loading...", offset, size); - readsize = fread (rbuffer + offset, 1, nextread, file); // read in next chunk - - if(readsize <= 0 || readsize > nextread) - break; // read failure - - if(readsize > 0) - offset += readsize; - } - CancelAction(); - - if(offset != size) // # bytes read doesn't match # expected - size = 0; - } - } + offset += readsize; } + size = offset; + CancelAction(); } - fclose (file); - } - } - if(!size) - { - if(!silent) - { - unmountRequired[device] = true; - retry = ErrorPromptRetry("Error loading file!"); - } - else - { - retry = 0; } } } @@ -797,6 +804,7 @@ size_t SaveFile (char * buffer, char *filepath, size_t datasize, bool silent) { size_t written = 0; + size_t writesize, nextwrite; int retry = 1; int device; @@ -806,46 +814,54 @@ SaveFile (char * buffer, char *filepath, size_t datasize, bool silent) if(datasize == 0) return 0; + // stop checking if devices were removed/inserted + // since we're loading a file + HaltDeviceThread(); + + // halt parsing + HaltParseThread(); + ShowAction("Saving..."); if(device == DEVICE_MC_SLOTA) - return SaveMCFile (buffer, CARD_SLOTA, StripDevice(filepath), datasize, silent); - else if(device == DEVICE_MC_SLOTB) - return SaveMCFile (buffer, CARD_SLOTB, StripDevice(filepath), datasize, silent); - - // stop checking if devices were removed/inserted - // since we're saving a file - HaltDeviceThread(); - - while(!written && retry == 1) { - if(ChangeInterface(device, silent)) + written = SaveMCFile (buffer, CARD_SLOTA, StripDevice(filepath), datasize, silent); + } + else if(device == DEVICE_MC_SLOTB) + { + written = SaveMCFile (buffer, CARD_SLOTB, StripDevice(filepath), datasize, silent); + } + else + { + while(!written && retry == 1) { - file = fopen (filepath, "wb"); - - if (file > 0) + if(ChangeInterface(device, silent)) { - size_t writesize, nextwrite; - while(written < datasize) - { - if(datasize - written > 4*1024) nextwrite=4*1024; - else nextwrite = datasize-written; - writesize = fwrite (buffer+written, 1, nextwrite, file); - if(writesize != nextwrite) break; // write failure - written += writesize; - } + file = fopen (filepath, "wb"); - if(written != datasize) written = 0; - fclose (file); + if (file) + { + while(written < datasize) + { + if(datasize - written > 4096) nextwrite=4096; + else nextwrite = datasize-written; + writesize = fwrite (buffer+written, 1, nextwrite, file); + if(writesize != nextwrite) break; // write failure + written += writesize; + } + + if(written != datasize) written = 0; + fclose (file); + } + } + if(!written) + { + unmountRequired[device] = true; + if(!silent) + retry = ErrorPromptRetry("Error saving file!"); + else + retry = 0; } - } - if(!written) - { - unmountRequired[device] = true; - if(!silent) - retry = ErrorPromptRetry("Error saving file!"); - else - retry = 0; } } diff --git a/source/ngc/menu.cpp b/source/ngc/menu.cpp index 9742437..b4924cf 100644 --- a/source/ngc/menu.cpp +++ b/source/ngc/menu.cpp @@ -1031,6 +1031,7 @@ static int MenuGameSelection() ExitRequested = 1; } + HaltParseThread(); // halt parsing HaltGui(); mainWindow->Remove(&titleTxt); mainWindow->Remove(&buttonWindow); diff --git a/source/ngc/networkop.cpp b/source/ngc/networkop.cpp index 31cfe02..a79c21b 100644 --- a/source/ngc/networkop.cpp +++ b/source/ngc/networkop.cpp @@ -21,6 +21,7 @@ ConnectShare (bool silent) #include #include #include +#include #include "unzip.h" #include "miniunz.h" @@ -183,9 +184,14 @@ void InitializeNetwork(bool silent) { // stop if we're already initialized, or if auto-init has failed before // in which case, manual initialization is required - if(networkInit || !autoNetworkInit) + if(networkInit || (silent && !autoNetworkInit)) return; + int retry = 1; + char ip[16]; + char msg[150]; + s32 initResult; + if(!silent) ShowAction ("Initializing network..."); @@ -196,25 +202,25 @@ void InitializeNetwork(bool silent) { inNetworkInit = true; - char ip[16]; - s32 initResult = if_config(ip, NULL, NULL, true); - - if(initResult == 0) + while(retry) { - networkInit = true; - } - else - { - // do not automatically attempt a reconnection - autoNetworkInit = false; - if(!silent) - { - char msg[150]; - sprintf(msg, "Unable to initialize network (Error #: %i)", initResult); - ErrorPrompt(msg); - } + ShowAction ("Initializing network..."); + + initResult = if_config(ip, NULL, NULL, true); + + if(initResult == 0) + networkInit = true; + + if(networkInit || silent) + break; + + sprintf(msg, "Unable to initialize network (Error #: %i)", initResult); + retry = ErrorPromptRetry(msg); } + + // do not automatically attempt a reconnection + autoNetworkInit = false; inNetworkInit = false; } if(!silent) @@ -226,7 +232,6 @@ void CloseShare() if(networkShareInit) smbClose("smb"); networkShareInit = false; - networkInit = false; // trigger a network reinit } /**************************************************************************** @@ -241,6 +246,7 @@ ConnectShare (bool silent) return false; #endif + int retry = 1; int chkS = (strlen(GCSettings.smbshare) > 0) ? 0:1; int chkI = (strlen(GCSettings.smbip) > 0) ? 0:1; @@ -270,27 +276,26 @@ ConnectShare (bool silent) if(!networkInit) InitializeNetwork(silent); - if(networkInit) + if(!networkInit) + return false; + + while(retry) { - if(!networkShareInit) - { - if(!silent) - ShowAction ("Connecting to network share..."); + if(!silent) + ShowAction ("Connecting to network share..."); + + if(smbInit(GCSettings.smbuser, GCSettings.smbpwd, GCSettings.smbshare, GCSettings.smbip)) + networkShareInit = true; - if(smbInit(GCSettings.smbuser, GCSettings.smbpwd, - GCSettings.smbshare, GCSettings.smbip)) - { - networkShareInit = true; - } + if(networkShareInit || silent) + break; - if(!silent) - CancelAction(); - } - - if(!networkShareInit && !silent) - ErrorPrompt("Failed to connect to network share."); + retry = ErrorPromptRetry("Failed to connect to network share."); } + if(!silent) + CancelAction(); + return networkShareInit; } diff --git a/source/ngc/preferences.cpp b/source/ngc/preferences.cpp index 924c621..aa721be 100644 --- a/source/ngc/preferences.cpp +++ b/source/ngc/preferences.cpp @@ -367,7 +367,7 @@ SavePrefs (bool silent) } else { - device = autoLoadMethod(); + device = autoSaveMethod(silent); if(device == 0) return false; @@ -375,7 +375,7 @@ SavePrefs (bool silent) if(device == DEVICE_MC_SLOTA || device == DEVICE_MC_SLOTB) sprintf(filepath, "%s%s", pathPrefix[device], PREF_FILE_NAME); else - sprintf(filepath, "%ssnes9x/%s", pathPrefix[device], PREF_FILE_NAME); + sprintf(filepath, "%s%s/%s", pathPrefix[device], APPFOLDER, PREF_FILE_NAME); } if(device == 0) diff --git a/source/ngc/snes9xGX.cpp b/source/ngc/snes9xGX.cpp index 5e685d2..ce1997a 100644 --- a/source/ngc/snes9xGX.cpp +++ b/source/ngc/snes9xGX.cpp @@ -74,9 +74,6 @@ extern void __exception_setreload(int t); void ExitCleanup() { -#ifdef HW_RVL - ShutoffRumble(); -#endif ShutdownAudio(); StopGX(); @@ -96,6 +93,10 @@ void ExitCleanup() void ExitApp() { +#ifdef HW_RVL + ShutoffRumble(); +#endif + SavePrefs(SILENT); if (SNESROMSize > 0 && !ConfigRequested && GCSettings.AutoSave == 1)