2008-08-06 03:09:59 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Snes9x 1.50
|
|
|
|
*
|
2008-08-10 05:14:39 +02:00
|
|
|
* Nintendo Wii/Gamecube Port
|
2008-08-06 03:09:59 +02:00
|
|
|
* softdev July 2006
|
|
|
|
* crunchy2 May 2007
|
2008-08-10 05:14:39 +02:00
|
|
|
* Tantric August 2008
|
2008-08-06 03:09:59 +02:00
|
|
|
*
|
2008-08-07 07:19:17 +02:00
|
|
|
* fileop.cpp
|
2008-08-06 03:09:59 +02:00
|
|
|
*
|
2008-08-07 07:19:17 +02:00
|
|
|
* File operations
|
2008-08-06 03:09:59 +02:00
|
|
|
****************************************************************************/
|
|
|
|
#include <gccore.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ogcsys.h>
|
2008-08-10 05:14:39 +02:00
|
|
|
#include <zlib.h>
|
|
|
|
#include "memmap.h"
|
|
|
|
|
2008-08-07 07:19:17 +02:00
|
|
|
#include "fileop.h"
|
2008-08-06 03:55:59 +02:00
|
|
|
#include "unzip.h"
|
2008-08-06 03:09:59 +02:00
|
|
|
#include "video.h"
|
2008-08-07 07:19:17 +02:00
|
|
|
#include "menudraw.h"
|
2008-08-06 03:09:59 +02:00
|
|
|
#include "dvd.h"
|
|
|
|
#include "filesel.h"
|
|
|
|
#include "sram.h"
|
|
|
|
#include "preferences.h"
|
2008-08-07 05:25:02 +02:00
|
|
|
#include "snes9xGx.h"
|
2008-08-06 03:09:59 +02:00
|
|
|
|
2008-08-06 03:39:43 +02:00
|
|
|
FILE * filehandle;
|
2008-08-06 03:55:59 +02:00
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
extern unsigned char savebuffer[];
|
|
|
|
extern char output[16384];
|
2008-08-06 03:09:59 +02:00
|
|
|
extern int offset;
|
|
|
|
extern int selection;
|
2008-08-10 05:14:39 +02:00
|
|
|
extern char currentdir[MAXPATHLEN];
|
2008-08-06 03:39:43 +02:00
|
|
|
extern FILEENTRIES filelist[MAXFILES];
|
2008-08-06 03:09:59 +02:00
|
|
|
|
2008-08-07 05:25:02 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* fat_is_mounted
|
|
|
|
* to check whether FAT media are detected.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
bool FatIsMounted(PARTITION_INTERFACE partition) {
|
2008-08-07 05:25:02 +02:00
|
|
|
char prefix[] = "fatX:/";
|
|
|
|
prefix[3] = partition + '0';
|
|
|
|
DIR_ITER *dir = diropen(prefix);
|
|
|
|
if (dir) {
|
|
|
|
dirclose(dir);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-08-08 09:57:01 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* changeFATInterface
|
|
|
|
* Checks if the device (method) specified is available, and
|
|
|
|
* sets libfat to use the device
|
|
|
|
****************************************************************************/
|
2008-08-12 05:25:16 +02:00
|
|
|
bool ChangeFATInterface(int method, bool silent)
|
2008-08-08 09:57:01 +02:00
|
|
|
{
|
|
|
|
bool devFound = false;
|
|
|
|
|
|
|
|
if(method == METHOD_SD)
|
|
|
|
{
|
|
|
|
// check which SD device is loaded
|
|
|
|
|
|
|
|
#ifdef HW_RVL
|
2008-08-12 05:25:16 +02:00
|
|
|
if (FatIsMounted(PI_INTERNAL_SD))
|
2008-08-08 09:57:01 +02:00
|
|
|
{
|
|
|
|
devFound = true;
|
|
|
|
fatSetDefaultInterface(PI_INTERNAL_SD);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
if (!devFound && FatIsMounted(PI_SDGECKO_A))
|
2008-08-08 09:57:01 +02:00
|
|
|
{
|
|
|
|
devFound = true;
|
|
|
|
fatSetDefaultInterface(PI_SDGECKO_A);
|
|
|
|
}
|
2008-08-12 05:25:16 +02:00
|
|
|
if(!devFound && FatIsMounted(PI_SDGECKO_B))
|
2008-08-08 09:57:01 +02:00
|
|
|
{
|
|
|
|
devFound = true;
|
|
|
|
fatSetDefaultInterface(PI_SDGECKO_B);
|
|
|
|
}
|
2008-08-10 05:14:39 +02:00
|
|
|
if(!devFound)
|
|
|
|
{
|
|
|
|
if(!silent)
|
|
|
|
WaitPrompt ((char *)"SD card not found!");
|
|
|
|
}
|
2008-08-08 09:57:01 +02:00
|
|
|
}
|
|
|
|
else if(method == METHOD_USB)
|
|
|
|
{
|
|
|
|
#ifdef HW_RVL
|
2008-08-12 05:25:16 +02:00
|
|
|
if(FatIsMounted(PI_USBSTORAGE))
|
2008-08-08 09:57:01 +02:00
|
|
|
{
|
|
|
|
devFound = true;
|
|
|
|
fatSetDefaultInterface(PI_USBSTORAGE);
|
|
|
|
}
|
2008-08-10 05:14:39 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if(!silent)
|
|
|
|
WaitPrompt ((char *)"USB flash drive not found!");
|
|
|
|
}
|
2008-08-08 09:57:01 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return devFound;
|
|
|
|
}
|
|
|
|
|
2008-08-06 03:55:59 +02:00
|
|
|
/***************************************************************************
|
2008-08-10 05:14:39 +02:00
|
|
|
* Browse FAT subdirectories
|
2008-08-07 05:25:02 +02:00
|
|
|
***************************************************************************/
|
2008-08-10 05:14:39 +02:00
|
|
|
int
|
2008-08-12 05:25:16 +02:00
|
|
|
ParseFATdirectory(int method)
|
2008-08-06 03:55:59 +02:00
|
|
|
{
|
2008-08-10 05:14:39 +02:00
|
|
|
int nbfiles = 0;
|
|
|
|
DIR_ITER *fatdir;
|
|
|
|
char filename[MAXPATHLEN];
|
|
|
|
struct stat filestat;
|
|
|
|
char msg[128];
|
|
|
|
|
|
|
|
// initialize selection
|
|
|
|
selection = offset = 0;
|
|
|
|
|
|
|
|
// open the directory
|
|
|
|
fatdir = diropen(currentdir);
|
|
|
|
if (fatdir == NULL)
|
2008-08-06 03:55:59 +02:00
|
|
|
{
|
2008-08-10 05:14:39 +02:00
|
|
|
sprintf(msg, "Couldn't open %s", currentdir);
|
|
|
|
WaitPrompt(msg);
|
2008-08-07 05:25:02 +02:00
|
|
|
|
2008-08-10 05:14:39 +02:00
|
|
|
// if we can't open the dir, open root dir
|
|
|
|
sprintf(currentdir,"%s",ROOTFATDIR);
|
2008-08-07 05:25:02 +02:00
|
|
|
|
2008-08-10 05:14:39 +02:00
|
|
|
fatdir = diropen(currentdir);
|
2008-08-06 03:55:59 +02:00
|
|
|
|
2008-08-10 05:14:39 +02:00
|
|
|
if (fatdir == NULL)
|
2008-08-07 05:25:02 +02:00
|
|
|
{
|
2008-08-10 05:14:39 +02:00
|
|
|
sprintf(msg, "Error opening %s", currentdir);
|
|
|
|
WaitPrompt(msg);
|
|
|
|
return 0;
|
2008-08-07 05:25:02 +02:00
|
|
|
}
|
|
|
|
}
|
2008-08-06 03:09:59 +02:00
|
|
|
|
2008-08-10 05:14:39 +02:00
|
|
|
// index files/folders
|
|
|
|
while(dirnext(fatdir,filename,&filestat) == 0)
|
2008-08-07 05:25:02 +02:00
|
|
|
{
|
2008-08-10 05:14:39 +02:00
|
|
|
if(strcmp(filename,".") != 0)
|
2008-08-07 05:25:02 +02:00
|
|
|
{
|
2008-08-10 05:14:39 +02:00
|
|
|
memset(&filelist[nbfiles], 0, sizeof(FILEENTRIES));
|
|
|
|
strncpy(filelist[nbfiles].filename, filename, MAXPATHLEN);
|
2008-08-06 03:39:43 +02:00
|
|
|
strncpy(filelist[nbfiles].displayname, filename, MAXDISPLAY+1); // crop name for display
|
2008-08-10 05:14:39 +02:00
|
|
|
filelist[nbfiles].length = filestat.st_size;
|
|
|
|
filelist[nbfiles].flags = (filestat.st_mode & _IFDIR) == 0 ? 0 : 1; // flag this as a dir
|
|
|
|
nbfiles++;
|
|
|
|
}
|
2008-08-06 03:39:43 +02:00
|
|
|
}
|
2008-08-07 05:25:02 +02:00
|
|
|
|
2008-08-10 05:14:39 +02:00
|
|
|
// close directory
|
|
|
|
dirclose(fatdir);
|
2008-08-07 05:25:02 +02:00
|
|
|
|
2008-08-10 05:14:39 +02:00
|
|
|
// Sort the file list
|
2008-08-06 03:55:59 +02:00
|
|
|
qsort(filelist, nbfiles, sizeof(FILEENTRIES), FileSortCallback);
|
2008-08-07 05:25:02 +02:00
|
|
|
|
2008-08-10 05:14:39 +02:00
|
|
|
return nbfiles;
|
2008-08-06 03:09:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2008-08-07 05:25:02 +02:00
|
|
|
* LoadFATFile
|
2008-08-06 03:09:59 +02:00
|
|
|
****************************************************************************/
|
|
|
|
int
|
2008-08-07 05:25:02 +02:00
|
|
|
LoadFATFile (char *filename, int length)
|
2008-08-06 03:09:59 +02:00
|
|
|
{
|
2008-08-10 05:14:39 +02:00
|
|
|
char zipbuffer[2048];
|
|
|
|
char filepath[MAXPATHLEN];
|
|
|
|
FILE *handle;
|
|
|
|
unsigned char *rbuffer;
|
|
|
|
u32 size;
|
|
|
|
|
|
|
|
rbuffer = (unsigned char *) Memory.ROM;
|
2008-08-06 03:09:59 +02:00
|
|
|
|
2008-08-10 05:14:39 +02:00
|
|
|
/* Check filename length */
|
|
|
|
if ((strlen(currentdir)+1+strlen(filelist[selection].filename)) < MAXPATHLEN)
|
|
|
|
sprintf(filepath, "%s/%s",currentdir,filelist[selection].filename);
|
|
|
|
else
|
|
|
|
{
|
2008-08-12 05:25:16 +02:00
|
|
|
WaitPrompt((char*) "Maximum filepath length reached!");
|
2008-08-10 05:14:39 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
handle = fopen (filepath, "rb");
|
|
|
|
if (handle > 0)
|
|
|
|
{
|
|
|
|
fread (zipbuffer, 1, 2048, handle);
|
|
|
|
|
|
|
|
if (IsZipFile (zipbuffer))
|
2008-08-06 03:39:43 +02:00
|
|
|
{
|
2008-08-12 05:25:16 +02:00
|
|
|
// Unzip the ROM
|
2008-08-10 05:14:39 +02:00
|
|
|
size = UnZipBuffer (rbuffer, 0, 0, handle); // unzip from FAT
|
2008-08-06 03:39:43 +02:00
|
|
|
|
2008-08-10 05:14:39 +02:00
|
|
|
fclose (handle);
|
|
|
|
return size;
|
2008-08-06 03:39:43 +02:00
|
|
|
}
|
2008-08-10 05:14:39 +02:00
|
|
|
else
|
2008-08-06 03:39:43 +02:00
|
|
|
{
|
2008-08-12 05:25:16 +02:00
|
|
|
// Just load the file up
|
2008-08-07 05:25:02 +02:00
|
|
|
|
2008-08-10 05:14:39 +02:00
|
|
|
fseek(handle, 0, SEEK_END);
|
|
|
|
length = ftell(handle); // get filesize
|
|
|
|
fseek(handle, 2048, SEEK_SET); // seek back to point where we left off
|
|
|
|
memcpy (rbuffer, zipbuffer, 2048); // copy what we already read
|
|
|
|
fread (rbuffer + 2048, 1, length - 2048, handle);
|
|
|
|
fclose (handle);
|
2008-08-07 05:25:02 +02:00
|
|
|
|
2008-08-10 05:14:39 +02:00
|
|
|
return length;
|
2008-08-06 03:39:43 +02:00
|
|
|
}
|
2008-08-10 05:14:39 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WaitPrompt((char*) "Error opening file");
|
|
|
|
return 0;
|
|
|
|
}
|
2008-08-06 03:09:59 +02:00
|
|
|
|
2008-08-10 05:14:39 +02:00
|
|
|
return 0;
|
2008-08-06 03:09:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2008-08-07 05:25:02 +02:00
|
|
|
* Load savebuffer from FAT file
|
2008-08-06 03:09:59 +02:00
|
|
|
****************************************************************************/
|
|
|
|
int
|
2008-08-07 05:25:02 +02:00
|
|
|
LoadBufferFromFAT (char *filepath, bool silent)
|
2008-08-06 03:09:59 +02:00
|
|
|
{
|
2008-08-07 05:25:02 +02:00
|
|
|
FILE *handle;
|
2008-08-12 05:25:16 +02:00
|
|
|
int boffset = 0;
|
2008-08-06 03:09:59 +02:00
|
|
|
int read = 0;
|
2008-08-07 05:25:02 +02:00
|
|
|
|
2008-08-13 07:47:04 +02:00
|
|
|
ClearSaveBuffer ();
|
|
|
|
|
2008-08-06 03:39:43 +02:00
|
|
|
handle = fopen (filepath, "rb");
|
2008-08-07 05:25:02 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
if (handle <= 0)
|
|
|
|
{
|
|
|
|
if ( !silent )
|
|
|
|
{
|
|
|
|
char msg[100];
|
|
|
|
sprintf(msg, "Couldn't open %s", filepath);
|
|
|
|
WaitPrompt (msg);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2008-08-07 05:25:02 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
/*** This is really nice, just load the file and decode it ***/
|
2008-08-12 05:25:16 +02:00
|
|
|
while ((read = fread (savebuffer + boffset, 1, 1024, handle)) > 0)
|
2008-08-06 03:09:59 +02:00
|
|
|
{
|
2008-08-12 05:25:16 +02:00
|
|
|
boffset += read;
|
2008-08-06 03:09:59 +02:00
|
|
|
}
|
2008-08-07 05:25:02 +02:00
|
|
|
|
2008-08-06 03:39:43 +02:00
|
|
|
fclose (handle);
|
2008-08-07 05:25:02 +02:00
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
return boffset;
|
2008-08-06 03:09:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2008-08-07 05:25:02 +02:00
|
|
|
* Write savebuffer to FAT card file
|
2008-08-06 03:09:59 +02:00
|
|
|
****************************************************************************/
|
|
|
|
int
|
2008-08-07 05:25:02 +02:00
|
|
|
SaveBufferToFAT (char *filepath, int datasize, bool silent)
|
2008-08-06 03:09:59 +02:00
|
|
|
{
|
2008-08-07 05:25:02 +02:00
|
|
|
FILE *handle;
|
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
if (datasize)
|
|
|
|
{
|
2008-08-06 03:39:43 +02:00
|
|
|
handle = fopen (filepath, "wb");
|
2008-08-07 05:25:02 +02:00
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
if (handle <= 0)
|
|
|
|
{
|
|
|
|
char msg[100];
|
|
|
|
sprintf(msg, "Couldn't save %s", filepath);
|
|
|
|
WaitPrompt (msg);
|
|
|
|
return 0;
|
|
|
|
}
|
2008-08-07 05:25:02 +02:00
|
|
|
|
2008-08-06 03:39:43 +02:00
|
|
|
fwrite (savebuffer, 1, datasize, handle);
|
|
|
|
fclose (handle);
|
2008-08-06 03:09:59 +02:00
|
|
|
}
|
|
|
|
|
2008-08-13 07:47:04 +02:00
|
|
|
ClearSaveBuffer ();
|
2008-08-07 05:25:02 +02:00
|
|
|
return datasize;
|
2008-08-06 03:09:59 +02:00
|
|
|
}
|