fceugx/source/ngc/filebrowser.cpp

542 lines
13 KiB
C++
Raw Normal View History

2008-09-02 03:57:21 +02:00
/****************************************************************************
* FCE Ultra 0.98.12
* Nintendo Wii/Gamecube Port
*
2009-03-28 18:23:08 +01:00
* Tantric 2008-2009
2008-09-02 03:57:21 +02:00
*
2009-03-28 18:23:08 +01:00
* filebrowser.cpp
2008-09-02 03:57:21 +02:00
*
* Generic file routines - reading, writing, browsing
****************************************************************************/
#include <gccore.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wiiuse/wpad.h>
#include <sys/dir.h>
#ifdef HW_RVL
2008-09-02 03:57:21 +02:00
#include <di/di.h>
#endif
#include "common.h"
#include "fceugx.h"
2008-09-02 03:57:21 +02:00
#include "dvd.h"
2009-03-28 18:23:08 +01:00
#include "menu.h"
#include "filebrowser.h"
2008-12-24 08:58:23 +01:00
#include "networkop.h"
2008-09-02 03:57:21 +02:00
#include "fileop.h"
#include "memcardop.h"
#include "pad.h"
#include "fceuload.h"
#include "gcunzip.h"
2009-03-28 18:23:08 +01:00
#include "fceuram.h"
#include "fceustate.h"
2008-09-02 03:57:21 +02:00
BROWSERINFO browser;
BROWSERENTRY * browserList = NULL; // list of files/folders in browser
char rootdir[10];
static char szpath[MAXPATHLEN];
static bool inSz = false;
2009-03-30 10:22:55 +02:00
char romFilename[256];
int nesGameType;
2008-09-02 03:57:21 +02:00
/****************************************************************************
* autoLoadMethod()
* Auto-determines and sets the load method
* Returns method set
****************************************************************************/
int autoLoadMethod()
{
ShowAction ("Attempting to determine load method...");
2008-09-02 03:57:21 +02:00
2009-03-28 18:23:08 +01:00
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;
2008-09-02 03:57:21 +02:00
else
2009-03-28 18:23:08 +01:00
ErrorPrompt("Unable to auto-determine load method!");
2008-12-24 08:58:23 +01:00
if(GCSettings.LoadMethod == METHOD_AUTO)
GCSettings.LoadMethod = method; // save method found for later use
2009-03-28 18:23:08 +01:00
CancelAction();
return method;
2008-09-02 03:57:21 +02:00
}
/****************************************************************************
* autoSaveMethod()
* Auto-determines and sets the save method
* Returns method set
****************************************************************************/
int autoSaveMethod(bool silent)
2008-09-02 03:57:21 +02:00
{
if(!silent)
ShowAction ("Attempting to determine save method...");
2009-03-28 18:23:08 +01:00
int method = METHOD_AUTO;
2008-09-02 03:57:21 +02:00
if(ChangeInterface(METHOD_SD, SILENT))
method = METHOD_SD;
else if(ChangeInterface(METHOD_USB, SILENT))
method = METHOD_USB;
2009-03-28 18:23:08 +01:00
else if(ChangeInterface(METHOD_MC_SLOTA, SILENT))
method = METHOD_MC_SLOTA;
2009-03-28 18:23:08 +01:00
else if(ChangeInterface(METHOD_MC_SLOTB, SILENT))
method = METHOD_MC_SLOTB;
else if(ChangeInterface(METHOD_SMB, SILENT))
method = METHOD_SMB;
else if(!silent)
2009-03-28 18:23:08 +01:00
ErrorPrompt("Unable to auto-determine save method!");
2008-12-24 08:58:23 +01:00
if(GCSettings.SaveMethod == METHOD_AUTO)
GCSettings.SaveMethod = method; // save method found for later use
2009-03-28 18:23:08 +01:00
CancelAction();
return method;
2008-09-02 03:57:21 +02:00
}
/****************************************************************************
* ResetBrowser()
* Clears the file browser memory, and allocates one initial entry
***************************************************************************/
void ResetBrowser()
{
2009-03-28 18:23:08 +01:00
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
2008-09-02 03:57:21 +02:00
***************************************************************************/
int UpdateDirName(int method)
{
int size=0;
2008-11-12 09:40:09 +01:00
char * test;
2008-09-02 03:57:21 +02:00
char temp[1024];
2008-11-12 09:40:09 +01:00
// update DVD directory
2008-09-02 03:57:21 +02:00
if(method == METHOD_DVD)
SetDVDdirectory(browserList[browser.selIndex].offset, browserList[browser.selIndex].length);
2008-09-02 03:57:21 +02:00
/* current directory doesn't change */
if (strcmp(browserList[browser.selIndex].filename,".") == 0)
2008-09-02 03:57:21 +02:00
{
return 0;
}
/* go up to parent directory */
else if (strcmp(browserList[browser.selIndex].filename,"..") == 0)
2008-09-02 03:57:21 +02:00
{
/* determine last subdirectory namelength */
sprintf(temp,"%s",browser.dir);
2008-09-02 03:57:21 +02:00
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;
2008-09-02 03:57:21 +02:00
return 1;
}
/* Open a directory */
else
{
/* test new directory namelength */
if ((strlen(browser.dir)+1+strlen(browserList[browser.selIndex].filename)) < MAXPATHLEN)
2008-09-02 03:57:21 +02:00
{
/* update current directory name */
sprintf(browser.dir, "%s/%s",browser.dir, browserList[browser.selIndex].filename);
2008-09-02 03:57:21 +02:00
return 1;
}
else
{
2009-03-28 18:23:08 +01:00
ErrorPrompt("Directory name is too long!");
2008-09-02 03:57:21 +02:00
return -1;
}
}
}
2009-03-28 18:23:08 +01:00
bool MakeFilePath(char filepath[], int type, int method, char * filename, int filenum)
2008-10-13 20:15:03 +02:00
{
2008-11-12 09:40:09 +01:00
char file[512];
char folder[1024];
2009-03-28 18:23:08 +01:00
char ext[4];
2008-10-13 20:15:03 +02:00
char temppath[MAXPATHLEN];
2008-11-12 09:40:09 +01:00
if(type == FILE_ROM)
2008-10-13 20:15:03 +02:00
{
2008-11-12 09:40:09 +01:00
// Check path length
if ((strlen(browser.dir)+1+strlen(browserList[browser.selIndex].filename)) >= MAXPATHLEN)
2008-11-12 09:40:09 +01:00
{
2009-03-28 18:23:08 +01:00
ErrorPrompt("Maximum filepath length reached!");
2008-11-12 09:40:09 +01:00
filepath[0] = 0;
return false;
}
2008-10-13 20:15:03 +02:00
else
2008-11-12 09:40:09 +01:00
{
sprintf(temppath, "%s/%s",browser.dir,browserList[browser.selIndex].filename);
2008-11-12 09:40:09 +01:00
}
2008-10-13 20:15:03 +02:00
}
else
{
2008-11-12 09:40:09 +01:00
switch(type)
{
case FILE_RAM:
case FILE_STATE:
sprintf(folder, GCSettings.SaveFolder);
2009-03-28 18:23:08 +01:00
if(type == FILE_RAM) sprintf(ext, "sav");
else sprintf(ext, "fcs");
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);
}
2008-11-12 09:40:09 +01:00
break;
2009-03-28 18:23:08 +01:00
2008-11-12 09:40:09 +01:00
case FILE_FDSBIOS:
sprintf(folder, GCSettings.LoadFolder);
sprintf(file, "disksys.rom");
break;
case FILE_PREF:
sprintf(folder, appPath);
2008-11-12 09:40:09 +01:00
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;
2008-11-12 09:40:09 +01:00
}
}
2009-03-28 18:23:08 +01:00
strncpy(filepath, temppath, MAXPATHLEN);
2008-11-12 09:40:09 +01:00
return true;
}
2008-09-02 03:57:21 +02:00
/***************************************************************************
* FileSortCallback
*
* Quick sort callback to sort file entries with the following order:
* .
* ..
* <dirs>
* <files>
***************************************************************************/
int FileSortCallback(const void *f1, const void *f2)
{
/* Special case for implicit directories */
if(((BROWSERENTRY *)f1)->filename[0] == '.' || ((BROWSERENTRY *)f2)->filename[0] == '.')
2008-09-02 03:57:21 +02:00
{
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; }
2008-09-02 03:57:21 +02:00
}
/* 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;
2008-09-02 03:57:21 +02:00
return stricmp(((BROWSERENTRY *)f1)->filename, ((BROWSERENTRY *)f2)->filename);
2008-09-02 03:57:21 +02:00
}
/****************************************************************************
* IsValidROM
*
* Checks if the specified file is a valid ROM
* For now we will just check the file extension and file size
* If the file is a zip, we will check the file extension / file size of the
* first file inside
***************************************************************************/
bool IsValidROM(int method)
{
2009-01-25 08:38:30 +01:00
// file size should be between 8K and 3MB
if(browserList[browser.selIndex].length < (1024*8) ||
browserList[browser.selIndex].length > (1024*1024*3))
{
2009-03-28 18:23:08 +01:00
ErrorPrompt("Invalid file size!");
return false;
}
if (strlen(browserList[browser.selIndex].filename) > 4)
{
char * p = strrchr(browserList[browser.selIndex].filename, '.');
if (p != NULL)
{
2008-10-13 20:15:03 +02:00
if(stricmp(p, ".zip") == 0 && !inSz)
{
// we need to check the file extension of the first file in the archive
char * zippedFilename = GetFirstZipFilename (method);
2008-10-13 20:15:03 +02:00
if(zippedFilename == NULL) // we don't want to run strlen on NULL
p = NULL;
else if(strlen(zippedFilename) > 4)
p = strrchr(zippedFilename, '.');
else
p = NULL;
}
if(p != NULL)
{
if (
stricmp(p, ".nes") == 0 ||
stricmp(p, ".fds") == 0 ||
stricmp(p, ".nsf") == 0 ||
stricmp(p, ".unf") == 0 ||
stricmp(p, ".nez") == 0 ||
stricmp(p, ".unif") == 0
)
{
return true;
}
}
}
}
2009-03-28 18:23:08 +01:00
ErrorPrompt("Unknown file type!");
return false;
}
2008-10-13 20:15:03 +02:00
/****************************************************************************
* IsSz
*
* Checks if the specified file is a 7z
***************************************************************************/
bool IsSz()
{
if (strlen(browserList[browser.selIndex].filename) > 4)
2008-10-13 20:15:03 +02:00
{
char * p = strrchr(browserList[browser.selIndex].filename, '.');
2008-10-13 20:15:03 +02:00
if (p != NULL)
if(stricmp(p, ".7z") == 0)
return true;
}
return false;
}
2008-09-02 03:57:21 +02:00
/****************************************************************************
* StripExt
*
* Strips an extension from a filename
2009-03-28 18:23:08 +01:00
***************************************************************************/
2008-09-02 03:57:21 +02:00
void StripExt(char* returnstring, char * inputstring)
{
char* loc_dot;
2009-03-28 18:23:08 +01:00
strncpy (returnstring, inputstring, 255);
if(inputstring == NULL || strlen(inputstring) < 4)
return;
2008-09-02 03:57:21 +02:00
loc_dot = strrchr(returnstring,'.');
if (loc_dot != NULL)
2009-03-28 18:23:08 +01:00
*loc_dot = 0; // strip file extension
2008-09-02 03:57:21 +02:00
}
/****************************************************************************
2009-03-28 18:23:08 +01:00
* BrowserLoadSz
2008-09-02 03:57:21 +02:00
*
2009-03-28 18:23:08 +01:00
* Opens the selected 7z file, and parses a listing of the files within
***************************************************************************/
int BrowserLoadSz(int method)
2008-09-02 03:57:21 +02:00
{
2009-03-28 18:23:08 +01:00
char filepath[MAXPATHLEN];
memset(filepath, 0, MAXPATHLEN);
2008-09-02 03:57:21 +02:00
2009-03-28 18:23:08 +01:00
// we'll store the 7z filepath for extraction later
if(!MakeFilePath(szpath, FILE_ROM, method))
return 0;
2008-10-13 20:15:03 +02:00
2009-03-28 18:23:08 +01:00
// add device to filepath
sprintf(filepath, "%s%s", rootdir, szpath);
memcpy(szpath, filepath, MAXPATHLEN);
2008-10-13 20:15:03 +02:00
2009-03-28 18:23:08 +01:00
int szfiles = SzParse(szpath, method);
if(szfiles)
{
browser.numEntries = szfiles;
inSz = true;
}
else
ErrorPrompt("Error opening archive!");
2008-10-13 20:15:03 +02:00
2009-03-28 18:23:08 +01:00
return szfiles;
}
2008-09-02 03:57:21 +02:00
2009-03-28 18:23:08 +01:00
/****************************************************************************
* BrowserLoadFile
*
* Loads the selected ROM
***************************************************************************/
int BrowserLoadFile(int method)
{
char filepath[1024];
int filesize = 0;
romLoaded = false;
2008-09-02 03:57:21 +02:00
2009-03-28 18:23:08 +01:00
// check that this is a valid ROM
if(!IsValidROM(method))
goto done;
2008-11-12 09:40:09 +01:00
2009-03-28 18:23:08 +01:00
// store the filename (w/o ext) - used for ram/state naming
StripExt(romFilename, browserList[browser.selIndex].filename);
2009-03-28 18:23:08 +01:00
if(!inSz)
{
if(!MakeFilePath(filepath, FILE_ROM, method))
goto done;
2008-10-06 00:46:15 +02:00
2009-03-28 18:23:08 +01:00
filesize = LoadFile ((char *)nesrom, filepath, browserList[browser.selIndex].length, method, NOTSILENT);
}
else
{
switch (method)
{
case METHOD_DVD:
filesize = SzExtractFile(browserList[browser.selIndex].offset, nesrom);
break;
default:
filesize = LoadSzFile(szpath, nesrom);
break;
}
}
inSz = false;
2008-09-02 03:57:21 +02:00
2009-03-28 18:23:08 +01:00
if (filesize <= 0)
{
ErrorPrompt("Error loading ROM!");
}
else
{
if(GCMemROM(method, filesize) > 0)
{
romLoaded = true;
2008-10-02 07:28:01 +02:00
2009-03-28 18:23:08 +01:00
// load RAM or state
if (GCSettings.AutoLoad == 1)
LoadRAMAuto(GCSettings.SaveMethod, SILENT);
else if (GCSettings.AutoLoad == 2)
LoadStateAuto(GCSettings.SaveMethod, SILENT);
2008-09-02 03:57:21 +02:00
2009-03-28 18:23:08 +01:00
ResetNES();
ResetBrowser();
}
}
done:
CancelAction();
return romLoaded;
}
2008-09-02 03:57:21 +02:00
2009-03-28 18:23:08 +01:00
/****************************************************************************
* 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);
2008-10-13 20:15:03 +02:00
2009-03-28 18:23:08 +01:00
inSz = false;
SzClose();
}
2008-11-12 09:40:09 +01:00
2009-03-28 18:23:08 +01:00
if(!UpdateDirName(method))
return -1;
2008-10-13 20:15:03 +02:00
2009-03-28 18:23:08 +01:00
switch (method)
{
case METHOD_DVD:
ParseDVDdirectory();
break;
2008-09-02 03:57:21 +02:00
2009-03-28 18:23:08 +01:00
default:
ParseDirectory(method);
break;
}
2008-09-02 03:57:21 +02:00
2009-03-28 18:23:08 +01:00
if (!browser.numEntries)
{
ErrorPrompt("Error reading directory!");
}
2009-03-28 18:23:08 +01:00
return browser.numEntries;
2008-09-02 03:57:21 +02:00
}
/****************************************************************************
* OpenROM
2009-03-28 18:23:08 +01:00
* Displays a list of ROMS on load device
***************************************************************************/
2008-09-02 03:57:21 +02:00
int
2009-03-28 18:23:08 +01:00
OpenGameList ()
2008-09-02 03:57:21 +02:00
{
2009-03-28 18:23:08 +01:00
int method = GCSettings.LoadMethod;
if(method == METHOD_AUTO)
method = autoLoadMethod();
2008-09-02 03:57:21 +02:00
2009-03-28 18:23:08 +01:00
// change current dir to roms directory
switch(method)
{
2009-03-28 18:23:08 +01:00
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;
2008-09-02 03:57:21 +02:00
}
2009-03-28 18:23:08 +01:00
return browser.numEntries;
2008-09-02 03:57:21 +02:00
}