snes9xgx/source/ngc/fileop.cpp

586 lines
13 KiB
C++
Raw Normal View History

/****************************************************************************
* Snes9x 1.51 Nintendo Wii/Gamecube Port
2008-08-14 00:44:59 +02:00
*
* softdev July 2006
* crunchy2 May 2007
2008-10-14 11:21:34 +02:00
* Michniewski 2008
2009-03-11 18:28:37 +01:00
* Tantric 2008-2009
*
2008-08-07 07:19:17 +02:00
* fileop.cpp
*
* File operations
***************************************************************************/
#include <gccore.h>
#include <stdio.h>
2008-12-18 19:36:30 +01:00
#include <stdlib.h>
#include <string.h>
#include <ogcsys.h>
2008-12-18 19:36:30 +01:00
#include <sys/dir.h>
#include <sys/stat.h>
#include <zlib.h>
#include <malloc.h>
2008-12-18 19:36:30 +01:00
#include <sdcard/wiisd_io.h>
#include <sdcard/gcsd.h>
#include <ogc/usbstorage.h>
2008-12-18 19:36:30 +01:00
#include "snes9xGX.h"
2008-08-07 07:19:17 +02:00
#include "fileop.h"
#include "networkop.h"
2008-12-18 19:36:30 +01:00
#include "dvd.h"
#include "memcardop.h"
#include "gcunzip.h"
#include "video.h"
2009-03-11 18:28:37 +01:00
#include "menu.h"
#include "filebrowser.h"
#include "preferences.h"
unsigned char * savebuffer = NULL;
FILE * file; // file pointer - the only one we should ever use!
2008-12-18 19:36:30 +01:00
bool unmountRequired[9] = { false, false, false, false, false, false, false, false, false };
bool isMounted[9] = { false, false, false, false, false, false, false, false, false };
2008-12-18 19:36:30 +01:00
#ifdef HW_RVL
const DISC_INTERFACE* sd = &__io_wiisd;
const DISC_INTERFACE* usb = &__io_usbstorage;
#else
const DISC_INTERFACE* carda = &__io_gcsda;
const DISC_INTERFACE* cardb = &__io_gcsdb;
#endif
2008-10-28 07:52:38 +01:00
/****************************************************************************
2008-12-18 19:36:30 +01:00
* deviceThreading
2008-10-28 07:52:38 +01:00
***************************************************************************/
2009-02-08 22:59:53 +01:00
lwp_t devicethread = LWP_THREAD_NULL;
2008-12-18 19:36:30 +01:00
/****************************************************************************
* devicecallback
*
* This checks our devices for changes (SD/USB removed) and
* initializes the network in the background
***************************************************************************/
static void *
devicecallback (void *arg)
2008-10-28 07:52:38 +01:00
{
2008-12-18 19:36:30 +01:00
while (1)
{
#ifdef HW_RVL
2009-02-08 22:30:39 +01:00
if(isMounted[METHOD_SD])
2008-12-18 19:36:30 +01:00
{
if(!sd->isInserted()) // check if the device was removed
{
unmountRequired[METHOD_SD] = true;
isMounted[METHOD_SD] = false;
}
}
if(isMounted[METHOD_USB])
{
if(!usb->isInserted()) // check if the device was removed - doesn't work on USB!
{
unmountRequired[METHOD_USB] = true;
isMounted[METHOD_USB] = false;
}
}
2009-02-08 22:30:39 +01:00
2008-12-18 19:36:30 +01:00
InitializeNetwork(SILENT);
UpdateCheck();
2008-12-18 19:36:30 +01:00
#else
2009-02-08 22:30:39 +01:00
if(isMounted[METHOD_SD_SLOTA])
2008-12-18 19:36:30 +01:00
{
if(!carda->isInserted()) // check if the device was removed
{
unmountRequired[METHOD_SD_SLOTA] = true;
isMounted[METHOD_SD_SLOTA] = false;
}
}
if(isMounted[METHOD_SD_SLOTB])
{
if(!cardb->isInserted()) // check if the device was removed
{
unmountRequired[METHOD_SD_SLOTB] = true;
isMounted[METHOD_SD_SLOTB] = false;
}
2009-02-08 22:30:39 +01:00
}
2008-12-18 19:36:30 +01:00
#endif
usleep(500000); // suspend thread for 1/2 sec
}
return NULL;
}
/****************************************************************************
* InitDeviceThread
*
* libOGC provides a nice wrapper for LWP access.
* This function sets up a new local queue and attaches the thread to it.
***************************************************************************/
void
InitDeviceThread()
{
2009-02-07 04:01:10 +01:00
LWP_CreateThread (&devicethread, devicecallback, NULL, NULL, 0, 40);
}
/****************************************************************************
2008-10-28 07:52:38 +01:00
* UnmountAllFAT
* Unmounts all FAT devices
***************************************************************************/
void UnmountAllFAT()
{
#ifdef HW_RVL
2009-03-11 18:28:37 +01:00
fatUnmount("sd:");
fatUnmount("usb:");
2008-12-18 19:36:30 +01:00
#else
2009-03-11 18:28:37 +01:00
fatUnmount("carda:");
fatUnmount("cardb:");
2008-10-28 07:52:38 +01:00
#endif
}
2008-11-12 08:50:39 +01:00
/****************************************************************************
* MountFAT
* Checks if the device needs to be (re)mounted
* If so, unmounts the device
* Attempts to mount the device specified
* Sets libfat to use the device by default
***************************************************************************/
2008-12-18 19:36:30 +01:00
bool MountFAT(int method)
2008-11-12 08:50:39 +01:00
{
2008-12-18 19:36:30 +01:00
bool mounted = true; // assume our disc is already mounted
char name[10];
const DISC_INTERFACE* disc = NULL;
2008-11-12 08:50:39 +01:00
2008-12-18 19:36:30 +01:00
switch(method)
{
#ifdef HW_RVL
case METHOD_SD:
sprintf(name, "sd");
disc = sd;
break;
case METHOD_USB:
sprintf(name, "usb");
disc = usb;
break;
#else
case METHOD_SD_SLOTA:
sprintf(name, "carda");
disc = carda;
break;
case METHOD_SD_SLOTB:
sprintf(name, "cardb");
disc = cardb;
break;
#endif
default:
return false; // unknown device
}
2009-03-11 18:28:37 +01:00
sprintf(rootdir, "%s:", name);
2008-12-18 19:36:30 +01:00
if(unmountRequired[method])
2008-11-12 08:50:39 +01:00
{
2008-12-18 19:36:30 +01:00
unmountRequired[method] = false;
fatUnmount(rootdir);
2008-12-18 19:36:30 +01:00
disc->shutdown();
}
if(!isMounted[method])
{
if(!disc->startup())
mounted = false;
else if(!fatMountSimple(name, disc))
mounted = false;
2009-01-21 07:52:04 +01:00
#ifdef HW_RVL
2009-01-06 07:04:41 +01:00
else
fatSetReadAhead(rootdir, 6, 64);
2009-01-21 07:52:04 +01:00
#endif
2008-11-12 08:50:39 +01:00
}
2008-12-18 19:36:30 +01:00
isMounted[method] = mounted;
2008-11-12 08:50:39 +01:00
return mounted;
}
2008-12-18 19:36:30 +01:00
void MountAllFAT()
{
#ifdef HW_RVL
MountFAT(METHOD_SD);
MountFAT(METHOD_USB);
#else
MountFAT(METHOD_SD_SLOTA);
MountFAT(METHOD_SD_SLOTB);
#endif
}
2008-10-28 07:52:38 +01:00
/****************************************************************************
2008-12-18 19:36:30 +01:00
* ChangeInterface
* Attempts to mount/configure the device specified
***************************************************************************/
2008-12-18 19:36:30 +01:00
bool ChangeInterface(int method, bool silent)
{
2008-10-28 07:52:38 +01:00
bool mounted = false;
if(method == METHOD_SD)
{
#ifdef HW_RVL
2008-12-18 19:36:30 +01:00
mounted = MountFAT(METHOD_SD); // try Wii internal SD
#else
mounted = MountFAT(METHOD_SD_SLOTA); // try SD Gecko on slot A
2008-10-28 07:52:38 +01:00
if(!mounted) // internal SD and SD Gecko (on slot A) not found
2008-12-18 19:36:30 +01:00
mounted = MountFAT(METHOD_SD_SLOTB); // try SD Gecko on slot B
#endif
2008-10-28 07:52:38 +01:00
if(!mounted && !silent) // no SD device found
2009-03-11 18:28:37 +01:00
ErrorPrompt("SD card not found!");
}
else if(method == METHOD_USB)
{
#ifdef HW_RVL
2008-12-18 19:36:30 +01:00
mounted = MountFAT(method);
2008-11-12 08:50:39 +01:00
2008-10-28 07:52:38 +01:00
if(!mounted && !silent)
2009-03-11 18:28:37 +01:00
ErrorPrompt("USB drive not found!");
#endif
}
2008-12-18 19:36:30 +01:00
else if(method == METHOD_DVD)
{
2009-03-11 18:28:37 +01:00
rootdir[0] = 0;
mounted = MountDVD(silent);
}
2009-02-08 22:59:53 +01:00
#ifdef HW_RVL
else if(method == METHOD_SMB)
{
2009-03-11 18:28:37 +01:00
sprintf(rootdir, "smb:");
mounted = ConnectShare(silent);
2008-12-18 19:36:30 +01:00
}
2009-02-08 22:59:53 +01:00
#endif
2009-03-11 18:28:37 +01:00
else if(method == METHOD_MC_SLOTA)
{
rootdir[0] = 0;
mounted = TestMC(CARD_SLOTA, silent);
}
else if(method == METHOD_MC_SLOTB)
{
rootdir[0] = 0;
mounted = TestMC(CARD_SLOTB, silent);
}
2008-10-28 07:52:38 +01:00
return mounted;
}
/***************************************************************************
2008-12-18 19:36:30 +01:00
* Browse subdirectories
**************************************************************************/
int
2008-12-18 19:36:30 +01:00
ParseDirectory()
{
2008-12-18 19:36:30 +01:00
DIR_ITER *dir;
char fulldir[MAXPATHLEN];
char filename[MAXPATHLEN];
2008-11-26 08:40:12 +01:00
char tmpname[MAXPATHLEN];
struct stat filestat;
2009-03-11 18:28:37 +01:00
struct tm * timeinfo;
char msg[128];
2009-03-11 18:28:37 +01:00
ShowAction("Loading...");
// reset browser
ResetBrowser();
2008-08-16 02:02:08 +02:00
2008-12-18 19:36:30 +01:00
// add device to path
2009-03-17 06:09:45 +01:00
sprintf(fulldir, "%s/%s", rootdir, browser.dir);
2008-12-18 19:36:30 +01:00
// open the directory
2008-12-18 19:36:30 +01:00
dir = diropen(fulldir);
if (dir == NULL)
{
2008-12-18 19:36:30 +01:00
sprintf(msg, "Error opening %s", fulldir);
2009-03-11 18:28:37 +01:00
ErrorPrompt(msg);
// if we can't open the dir, open root dir
sprintf(browser.dir,"/");
dir = diropen(rootdir);
2008-12-18 19:36:30 +01:00
if (dir == NULL)
{
sprintf(msg, "Error opening %s", rootdir);
2009-03-11 18:28:37 +01:00
ErrorPrompt(msg);
return -1;
}
}
// index files/folders
int entryNum = 0;
2008-12-18 19:36:30 +01:00
while(dirnext(dir,filename,&filestat) == 0)
{
if(strcmp(filename,".") != 0)
{
BROWSERENTRY * newBrowserList = (BROWSERENTRY *)realloc(browserList, (entryNum+1) * sizeof(BROWSERENTRY));
if(!newBrowserList) // failed to allocate required memory
{
ResetBrowser();
2009-03-11 18:28:37 +01:00
ErrorPrompt("Out of memory: too many files!");
entryNum = -1;
break;
}
else
{
browserList = newBrowserList;
}
memset(&(browserList[entryNum]), 0, sizeof(BROWSERENTRY)); // clear the new entry
strncpy(browserList[entryNum].filename, filename, MAXJOLIET);
2009-03-16 06:16:20 +01:00
if(strcmp(filename,"..") == 0)
{
sprintf(browserList[entryNum].displayname, "Up One Level");
}
else
{
StripExt(tmpname, filename); // hide file extension
strncpy(browserList[entryNum].displayname, tmpname, MAXDISPLAY); // crop name for display
}
browserList[entryNum].length = filestat.st_size;
2009-03-11 18:28:37 +01:00
timeinfo = localtime (&filestat.st_mtime);
memcpy(&browserList[entryNum].mtime, timeinfo, sizeof(tm));
browserList[entryNum].isdir = (filestat.st_mode & _IFDIR) == 0 ? 0 : 1; // flag this as a dir
entryNum++;
}
}
// close directory
2008-12-18 19:36:30 +01:00
dirclose(dir);
// Sort the file list
qsort(browserList, entryNum, sizeof(BROWSERENTRY), FileSortCallback);
2009-03-11 18:28:37 +01:00
CancelAction();
browser.numEntries = entryNum;
return entryNum;
}
/****************************************************************************
* AllocSaveBuffer ()
* Clear and allocate the savebuffer
***************************************************************************/
void
AllocSaveBuffer ()
{
2009-03-12 08:07:52 +01:00
while(savebuffer != NULL) // save buffer is in use
usleep(50); // wait for it to be free
savebuffer = (unsigned char *) memalign(32, SAVEBUFFERSIZE);
memset (savebuffer, 0, SAVEBUFFERSIZE);
}
/****************************************************************************
* FreeSaveBuffer ()
* Free the savebuffer memory
***************************************************************************/
void
FreeSaveBuffer ()
{
if (savebuffer != NULL)
free(savebuffer);
savebuffer = NULL;
}
/****************************************************************************
2008-12-18 19:36:30 +01:00
* LoadSzFile
2008-11-12 08:50:39 +01:00
* Loads the selected file # from the specified 7z into rbuffer
* Returns file size
***************************************************************************/
2008-12-18 19:36:30 +01:00
u32
LoadSzFile(char * filepath, unsigned char * rbuffer)
{
u32 size = 0;
// stop checking if devices were removed/inserted
// since we're loading a file
LWP_SuspendThread (devicethread);
2008-12-18 19:36:30 +01:00
file = fopen (filepath, "rb");
if (file > 0)
{
size = SzExtractFile(browserList[browser.selIndex].offset, rbuffer);
2008-12-18 19:36:30 +01:00
fclose (file);
2008-10-14 11:21:34 +02:00
}
else
{
2009-03-11 18:28:37 +01:00
ErrorPrompt("Error opening file");
2008-10-14 11:21:34 +02:00
}
// go back to checking if devices were inserted/removed
LWP_ResumeThread (devicethread);
return size;
2008-10-14 11:21:34 +02:00
}
/****************************************************************************
2008-12-18 19:36:30 +01:00
* LoadFile
2008-10-14 11:21:34 +02:00
***************************************************************************/
2008-12-18 19:36:30 +01:00
u32
LoadFile (char * rbuffer, char *filepath, u32 length, int method, bool silent)
2008-10-14 11:21:34 +02:00
{
2008-11-12 08:50:39 +01:00
char zipbuffer[2048];
2008-12-18 19:36:30 +01:00
u32 size = 0;
u32 readsize = 0;
2009-03-11 18:28:37 +01:00
if(!ChangeInterface(method, silent))
2008-12-18 19:36:30 +01:00
return 0;
switch(method)
{
case METHOD_DVD:
return LoadDVDFile (rbuffer, filepath, length, silent);
break;
case METHOD_MC_SLOTA:
return LoadMCFile (rbuffer, CARD_SLOTA, filepath, silent);
break;
case METHOD_MC_SLOTB:
return LoadMCFile (rbuffer, CARD_SLOTB, filepath, silent);
break;
}
// stop checking if devices were removed/inserted
// since we're loading a file
LWP_SuspendThread (devicethread);
2008-12-18 19:36:30 +01:00
// add device to filepath
char fullpath[1024];
2009-03-20 08:30:29 +01:00
sprintf(fullpath, "%s/%s", rootdir, filepath);
2008-11-12 08:50:39 +01:00
2008-12-18 19:36:30 +01:00
file = fopen (fullpath, "rb");
2008-11-12 08:50:39 +01:00
2008-12-18 19:36:30 +01:00
if (file > 0)
2008-10-14 11:21:34 +02:00
{
2008-11-12 08:50:39 +01:00
if(length > 0 && length <= 2048) // do a partial read (eg: to check file header)
{
2008-12-18 19:36:30 +01:00
size = fread (rbuffer, 1, length, file);
2008-11-12 08:50:39 +01:00
}
else // load whole file
{
2008-12-18 19:36:30 +01:00
readsize = fread (zipbuffer, 1, 2048, file);
2008-11-12 08:50:39 +01:00
if(readsize > 0)
{
if (IsZipFile (zipbuffer))
{
2008-12-18 19:36:30 +01:00
size = UnZipBuffer ((unsigned char *)rbuffer, method); // unzip
2008-11-12 08:50:39 +01:00
}
else
{
2008-12-18 19:36:30 +01:00
struct stat fileinfo;
fstat(file->_file, &fileinfo);
size = fileinfo.st_size;
2009-01-02 23:32:16 +01:00
memcpy (rbuffer, zipbuffer, readsize); // copy what we already read
2008-11-12 08:50:39 +01:00
2009-01-02 23:32:16 +01:00
u32 offset = readsize;
2009-03-21 03:01:40 +01:00
u32 nextread = 0;
2008-12-18 19:36:30 +01:00
while(offset < size)
2008-11-12 08:50:39 +01:00
{
2009-03-21 03:01:40 +01:00
if(size - offset > 1024*512) nextread = 1024*512;
else nextread = size-offset;
2009-01-02 23:32:16 +01:00
ShowProgress ("Loading...", offset, size);
2009-03-21 03:01:40 +01:00
readsize = fread (rbuffer + offset, 1, nextread, file); // read in 512K chunks
2008-12-18 19:36:30 +01:00
if(readsize <= 0 || readsize > (1024*512))
break; // read failure
if(readsize > 0)
offset += readsize;
2008-11-12 08:50:39 +01:00
}
2009-03-11 18:28:37 +01:00
CancelAction();
2008-12-18 19:36:30 +01:00
if(offset != size) // # bytes read doesn't match # expected
size = 0;
2008-11-12 08:50:39 +01:00
}
}
}
2008-12-18 19:36:30 +01:00
fclose (file);
}
2008-12-18 19:36:30 +01:00
if(!size && !silent)
{
2008-12-18 19:36:30 +01:00
unmountRequired[method] = true;
2009-03-11 18:28:37 +01:00
ErrorPrompt("Error loading file!");
}
2008-12-18 19:36:30 +01:00
// go back to checking if devices were inserted/removed
LWP_ResumeThread (devicethread);
2009-03-11 18:28:37 +01:00
CancelAction();
2008-12-18 19:36:30 +01:00
return size;
}
2009-03-11 18:28:37 +01:00
u32 LoadFile(char * filepath, int method, bool silent)
2008-12-18 19:36:30 +01:00
{
return LoadFile((char *)savebuffer, filepath, 0, method, silent);
}
/****************************************************************************
2008-12-18 19:36:30 +01:00
* SaveFile
* Write buffer to file
***************************************************************************/
2008-12-18 19:36:30 +01:00
u32
SaveFile (char * buffer, char *filepath, u32 datasize, int method, bool silent)
{
2009-03-16 07:58:50 +01:00
if(datasize == 0)
return 0;
2008-12-18 19:36:30 +01:00
u32 written = 0;
2009-03-11 18:28:37 +01:00
if(!ChangeInterface(method, silent))
2008-12-18 19:36:30 +01:00
return 0;
2009-03-11 18:28:37 +01:00
ShowAction("Saving...");
2009-03-16 07:58:50 +01:00
if(method == METHOD_MC_SLOTA || method == METHOD_MC_SLOTB)
2008-12-18 19:36:30 +01:00
{
2009-03-16 07:58:50 +01:00
if(method == METHOD_MC_SLOTA)
written = SaveMCFile (buffer, CARD_SLOTA, filepath, datasize, silent);
else
written = SaveMCFile (buffer, CARD_SLOTB, filepath, datasize, silent);
2008-12-18 19:36:30 +01:00
}
2009-03-16 07:58:50 +01:00
else
{
// stop checking if devices were removed/inserted
// since we're saving a file
LWP_SuspendThread (devicethread);
2008-12-18 19:36:30 +01:00
// add device to filepath
char fullpath[1024];
2009-03-20 08:30:29 +01:00
sprintf(fullpath, "%s/%s", rootdir, filepath);
2008-12-18 19:36:30 +01:00
// open file for writing
2008-12-20 21:32:24 +01:00
file = fopen (fullpath, "wb");
2008-12-18 19:36:30 +01:00
if (file > 0)
{
written = fwrite (savebuffer, 1, datasize, file);
fclose (file);
}
2009-03-16 07:58:50 +01:00
if(!written)
2008-12-18 19:36:30 +01:00
unmountRequired[method] = true;
// go back to checking if devices were inserted/removed
LWP_ResumeThread (devicethread);
}
2009-03-16 07:58:50 +01:00
if(!written && !silent)
ErrorPrompt("Error saving file!");
2009-03-11 18:28:37 +01:00
CancelAction();
2008-12-18 19:36:30 +01:00
return written;
}
2009-03-11 18:28:37 +01:00
u32 SaveFile(char * filepath, u32 datasize, int method, bool silent)
2008-12-18 19:36:30 +01:00
{
return SaveFile((char *)savebuffer, filepath, datasize, method, silent);
}