/**************************************************************************** * Visual Boy Advance GX * * Tantric September 2008 * * filebrowser.cpp * * Generic file routines - reading, writing, browsing ***************************************************************************/ #include #include #include #include #include #include #include #ifdef HW_RVL extern "C" { #include } #endif #include "vba.h" #include "dvd.h" #include "vbasupport.h" #include "vmmem.h" #include "filebrowser.h" #include "menu.h" #include "video.h" #include "networkop.h" #include "fileop.h" #include "memcardop.h" #include "input.h" #include "gcunzip.h" #include "wiiusbsupport.h" BROWSERINFO browser; BROWSERENTRY * browserList = NULL; // list of files/folders in browser char rootdir[10]; char szpath[MAXPATHLEN]; bool inSz = false; char ROMFilename[512]; bool ROMLoaded = false; /**************************************************************************** * autoLoadMethod() * Auto-determines and sets the load method * Returns method set ****************************************************************************/ int autoLoadMethod() { ShowAction ("Attempting to determine load method..."); int method = METHOD_AUTO; if(ChangeInterface(METHOD_SD, SILENT)) method = METHOD_SD; else if(ChangeInterface(METHOD_USB, SILENT)) method = METHOD_USB; else if(ChangeInterface(METHOD_DVD, SILENT)) method = METHOD_DVD; else if(ChangeInterface(METHOD_SMB, SILENT)) method = METHOD_SMB; else ErrorPrompt("Unable to auto-determine load method!"); if(GCSettings.LoadMethod == METHOD_AUTO) GCSettings.LoadMethod = method; // save method found for later use CancelAction(); return method; } /**************************************************************************** * autoSaveMethod() * Auto-determines and sets the save method * Returns method set ****************************************************************************/ int autoSaveMethod(bool silent) { if(!silent) ShowAction ("Attempting to determine save method..."); int method = METHOD_AUTO; if(ChangeInterface(METHOD_SD, SILENT)) method = METHOD_SD; else if(ChangeInterface(METHOD_USB, SILENT)) method = METHOD_USB; else if(ChangeInterface(METHOD_MC_SLOTA, SILENT)) method = METHOD_MC_SLOTA; else if(ChangeInterface(METHOD_MC_SLOTB, SILENT)) method = METHOD_MC_SLOTB; else if(ChangeInterface(METHOD_SMB, SILENT)) method = METHOD_SMB; else if(!silent) ErrorPrompt("Unable to auto-determine save method!"); if(GCSettings.SaveMethod == METHOD_AUTO) GCSettings.SaveMethod = method; // save method found for later use CancelAction(); return method; } /**************************************************************************** * ResetBrowser() * Clears the file browser memory, and allocates one initial entry ***************************************************************************/ void ResetBrowser() { browser.numEntries = 0; browser.selIndex = 0; browser.pageIndex = 0; // Clear any existing values if(browserList != NULL) { free(browserList); browserList = NULL; } // set aside space for 1 entry browserList = (BROWSERENTRY *)malloc(sizeof(BROWSERENTRY)); memset(browserList, 0, sizeof(BROWSERENTRY)); } /**************************************************************************** * UpdateDirName() * Update curent directory name for file browser ***************************************************************************/ int UpdateDirName(int method) { int size=0; char * test; char temp[1024]; // update DVD directory if(method == METHOD_DVD) SetDVDdirectory(browserList[browser.selIndex].offset, browserList[browser.selIndex].length); /* current directory doesn't change */ if (strcmp(browserList[browser.selIndex].filename,".") == 0) { return 0; } /* go up to parent directory */ else if (strcmp(browserList[browser.selIndex].filename,"..") == 0) { /* determine last subdirectory namelength */ sprintf(temp,"%s",browser.dir); test = strtok(temp,"/"); while (test != NULL) { size = strlen(test); test = strtok(NULL,"/"); } /* remove last subdirectory name */ size = strlen(browser.dir) - size - 1; browser.dir[size] = 0; return 1; } /* Open a directory */ else { /* test new directory namelength */ if ((strlen(browser.dir)+1+strlen(browserList[browser.selIndex].filename)) < MAXPATHLEN) { /* update current directory name */ sprintf(browser.dir, "%s/%s",browser.dir, browserList[browser.selIndex].filename); return 1; } else { ErrorPrompt("Directory name is too long!"); return -1; } } } bool MakeFilePath(char filepath[], int type, int method, char * filename, int filenum) { char file[512]; char folder[1024]; char ext[4]; char temppath[MAXPATHLEN]; if(type == FILE_ROM) { // Check path length if ((strlen(browser.dir)+1+strlen(browserList[browser.selIndex].filename)) >= MAXPATHLEN) { ErrorPrompt("Maximum filepath length reached!"); filepath[0] = 0; return false; } else { sprintf(temppath, "%s/%s",browser.dir,browserList[browser.selIndex].filename); } } else { switch(type) { case FILE_SRAM: case FILE_SNAPSHOT: sprintf(folder, GCSettings.SaveFolder); if(type == FILE_SRAM) sprintf(ext, "sav"); else sprintf(ext, "sgm"); if(filenum >= 0) { if(method == METHOD_MC_SLOTA || method == METHOD_MC_SLOTB) { filename[26] = 0; // truncate filename sprintf(file, "%s%i.%s", filename, filenum, ext); } else { if(filenum == 0) sprintf(file, "%s Auto.%s", filename, ext); else sprintf(file, "%s %i.%s", filename, filenum, ext); } } else { sprintf(file, "%s", filename); } break; case FILE_CHEAT: sprintf(folder, GCSettings.CheatFolder); sprintf(file, "%s.cht", ROMFilename); break; case FILE_PREF: sprintf(folder, appPath); sprintf(file, "%s", PREF_FILE_NAME); break; } switch(method) { case METHOD_MC_SLOTA: case METHOD_MC_SLOTB: sprintf (temppath, "%s", file); temppath[31] = 0; // truncate filename break; default: sprintf (temppath, "%s/%s", folder, file); break; } } strncpy(filepath, temppath, MAXPATHLEN); return true; } /**************************************************************************** * FileSortCallback * * Quick sort callback to sort file entries with the following order: * . * .. * * ***************************************************************************/ int FileSortCallback(const void *f1, const void *f2) { /* Special case for implicit directories */ if(((BROWSERENTRY *)f1)->filename[0] == '.' || ((BROWSERENTRY *)f2)->filename[0] == '.') { if(strcmp(((BROWSERENTRY *)f1)->filename, ".") == 0) { return -1; } if(strcmp(((BROWSERENTRY *)f2)->filename, ".") == 0) { return 1; } if(strcmp(((BROWSERENTRY *)f1)->filename, "..") == 0) { return -1; } if(strcmp(((BROWSERENTRY *)f2)->filename, "..") == 0) { return 1; } } /* If one is a file and one is a directory the directory is first. */ if(((BROWSERENTRY *)f1)->isdir && !(((BROWSERENTRY *)f2)->isdir)) return -1; if(!(((BROWSERENTRY *)f1)->isdir) && ((BROWSERENTRY *)f2)->isdir) return 1; return stricmp(((BROWSERENTRY *)f1)->filename, ((BROWSERENTRY *)f2)->filename); } /**************************************************************************** * IsSz * * Checks if the specified file is a 7z ***************************************************************************/ bool IsSz() { if (strlen(browserList[browser.selIndex].filename) > 4) { char * p = strrchr(browserList[browser.selIndex].filename, '.'); if (p != NULL) if(stricmp(p, ".7z") == 0) return true; } return false; } /**************************************************************************** * StripExt * * Strips an extension from a filename ***************************************************************************/ void StripExt(char* returnstring, char * inputstring) { char* loc_dot; strncpy (returnstring, inputstring, 255); if(inputstring == NULL || strlen(inputstring) < 4) return; loc_dot = strrchr(returnstring,'.'); if (loc_dot != NULL) *loc_dot = 0; // strip file extension } /**************************************************************************** * BrowserLoadSz * * Opens the selected 7z file, and parses a listing of the files within ***************************************************************************/ int BrowserLoadSz(int method) { char filepath[MAXPATHLEN]; memset(filepath, 0, MAXPATHLEN); // we'll store the 7z filepath for extraction later if(!MakeFilePath(szpath, FILE_ROM, method)) return 0; // add device to filepath if(method != METHOD_DVD) { sprintf(filepath, "%s%s", rootdir, szpath); memcpy(szpath, filepath, MAXPATHLEN); } int szfiles = SzParse(szpath, method); if(szfiles) { browser.numEntries = szfiles; inSz = true; } else ErrorPrompt("Error opening archive!"); return szfiles; } /**************************************************************************** * BrowserLoadFile * * Loads the selected ROM ***************************************************************************/ int BrowserLoadFile(int method) { // store the filename (w/o ext) - used for sram/freeze naming StripExt(ROMFilename, browserList[browser.selIndex].filename); ROMLoaded = LoadVBAROM(method); inSz = false; if (!ROMLoaded) { ErrorPrompt("Error loading ROM!"); } else { ResetBrowser(); } CancelAction(); return ROMLoaded; } /**************************************************************************** * BrowserChangeFolder * * Update current directory and set new entry list if directory has changed ***************************************************************************/ int BrowserChangeFolder(int method) { if(inSz && browser.selIndex == 0) // inside a 7z, requesting to leave { if(method == METHOD_DVD) SetDVDdirectory(browserList[0].offset, browserList[0].length); inSz = false; SzClose(); } if(!UpdateDirName(method)) return -1; switch (method) { case METHOD_DVD: ParseDVDdirectory(); break; default: ParseDirectory(method); break; } if (!browser.numEntries) { ErrorPrompt("Error reading directory!"); } return browser.numEntries; } /**************************************************************************** * OpenROM * Displays a list of ROMS on load device ***************************************************************************/ int OpenGameList () { int method = GCSettings.LoadMethod; if(method == METHOD_AUTO) method = autoLoadMethod(); // change current dir to roms directory switch(method) { case METHOD_DVD: browser.dir[0] = 0; if(MountDVD(NOTSILENT)) if(ParseDVDdirectory()) // Parse root directory SwitchDVDFolder(GCSettings.LoadFolder); // switch to ROM folder break; default: sprintf(browser.dir, "/%s", GCSettings.LoadFolder); ParseDirectory(method); // Parse root directory break; } return browser.numEntries; }