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
|
|
|
* fileop.cpp
|
2008-09-02 03:57:21 +02:00
|
|
|
*
|
|
|
|
* File operations
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <gccore.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2008-12-30 01:05:57 +01:00
|
|
|
#include <malloc.h>
|
2008-09-02 03:57:21 +02:00
|
|
|
#include <ogcsys.h>
|
2008-12-19 23:04:55 +01:00
|
|
|
#include <sys/dir.h>
|
|
|
|
#include <sys/stat.h>
|
2008-09-02 03:57:21 +02:00
|
|
|
#include <zlib.h>
|
2008-12-19 23:04:55 +01:00
|
|
|
#include <sdcard/wiisd_io.h>
|
|
|
|
#include <sdcard/gcsd.h>
|
|
|
|
#include <ogc/usbstorage.h>
|
2008-09-02 03:57:21 +02:00
|
|
|
|
2008-12-19 23:04:55 +01:00
|
|
|
#include "dvd.h"
|
2008-09-02 03:57:21 +02:00
|
|
|
#include "common.h"
|
2008-11-17 01:09:31 +01:00
|
|
|
#include "fceugx.h"
|
2008-09-02 03:57:21 +02:00
|
|
|
#include "fileop.h"
|
2008-12-19 23:04:55 +01:00
|
|
|
#include "memcardop.h"
|
2008-12-24 08:58:23 +01:00
|
|
|
#include "networkop.h"
|
2008-09-02 03:57:21 +02:00
|
|
|
#include "gcunzip.h"
|
2009-03-28 18:23:08 +01:00
|
|
|
#include "menu.h"
|
|
|
|
#include "filebrowser.h"
|
2008-09-02 03:57:21 +02:00
|
|
|
|
2009-06-01 23:56:21 +02:00
|
|
|
unsigned char savebuffer[SAVEBUFFERSIZE] ATTRIBUTE_ALIGN(32);
|
|
|
|
static mutex_t bufferLock = LWP_MUTEX_NULL;
|
2008-12-30 01:05:57 +01:00
|
|
|
FILE * file; // file pointer - the only one we should ever use!
|
2008-12-19 23:04:55 +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 };
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* deviceThreading
|
|
|
|
***************************************************************************/
|
2009-05-06 19:27:20 +02:00
|
|
|
static lwp_t devicethread = LWP_THREAD_NULL;
|
|
|
|
static bool deviceHalt = true;
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* ResumeDeviceThread
|
|
|
|
*
|
|
|
|
* Signals the device thread to start, and resumes the thread.
|
|
|
|
***************************************************************************/
|
|
|
|
void
|
|
|
|
ResumeDeviceThread()
|
|
|
|
{
|
|
|
|
deviceHalt = false;
|
|
|
|
LWP_ResumeThread(devicethread);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* HaltGui
|
|
|
|
*
|
|
|
|
* Signals the device thread to stop.
|
|
|
|
***************************************************************************/
|
|
|
|
void
|
|
|
|
HaltDeviceThread()
|
|
|
|
{
|
|
|
|
deviceHalt = true;
|
|
|
|
|
2009-06-22 22:04:19 +02:00
|
|
|
#ifdef HW_RVL
|
|
|
|
if(inNetworkInit) // don't wait for network to initialize
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2009-05-06 19:27:20 +02:00
|
|
|
// wait for thread to finish
|
|
|
|
while(!LWP_ThreadIsSuspended(devicethread))
|
|
|
|
usleep(100);
|
|
|
|
}
|
2008-12-19 23:04:55 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* devicecallback
|
|
|
|
*
|
|
|
|
* This checks our devices for changes (SD/USB removed) and
|
|
|
|
* initializes the network in the background
|
|
|
|
***************************************************************************/
|
2009-05-30 20:26:02 +02:00
|
|
|
static int devsleep = 1*1000*1000;
|
2009-05-26 23:48:19 +02:00
|
|
|
|
2008-12-19 23:04:55 +01:00
|
|
|
static void *
|
|
|
|
devicecallback (void *arg)
|
|
|
|
{
|
2009-05-26 23:48:19 +02:00
|
|
|
while(devsleep > 0)
|
|
|
|
{
|
|
|
|
if(deviceHalt)
|
|
|
|
LWP_SuspendThread(devicethread);
|
|
|
|
usleep(100);
|
|
|
|
devsleep -= 100;
|
|
|
|
}
|
|
|
|
|
2008-12-19 23:04:55 +01:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
#ifdef HW_RVL
|
|
|
|
if(isMounted[METHOD_SD])
|
|
|
|
{
|
|
|
|
if(!sd->isInserted()) // check if the device was removed
|
|
|
|
{
|
|
|
|
unmountRequired[METHOD_SD] = true;
|
|
|
|
isMounted[METHOD_SD] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isMounted[METHOD_USB])
|
|
|
|
{
|
2009-05-06 08:33:34 +02:00
|
|
|
if(!usb->isInserted()) // check if the device was removed
|
2008-12-19 23:04:55 +01:00
|
|
|
{
|
|
|
|
unmountRequired[METHOD_USB] = true;
|
|
|
|
isMounted[METHOD_USB] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-01 07:48:01 +02:00
|
|
|
UpdateCheck();
|
2009-06-22 22:04:19 +02:00
|
|
|
InitializeNetwork(SILENT);
|
2008-12-19 23:04:55 +01:00
|
|
|
#else
|
|
|
|
if(isMounted[METHOD_SD_SLOTA])
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2009-05-26 23:48:19 +02:00
|
|
|
devsleep = 1000*1000; // 1 sec
|
|
|
|
|
|
|
|
while(devsleep > 0)
|
|
|
|
{
|
|
|
|
if(deviceHalt)
|
|
|
|
LWP_SuspendThread(devicethread);
|
|
|
|
usleep(100);
|
|
|
|
devsleep -= 100;
|
|
|
|
}
|
2008-12-19 23:04:55 +01:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-09-02 03:57:21 +02:00
|
|
|
|
2008-10-28 07:50:07 +01:00
|
|
|
/****************************************************************************
|
2008-12-19 23:04:55 +01:00
|
|
|
* InitDeviceThread
|
|
|
|
*
|
|
|
|
* libOGC provides a nice wrapper for LWP access.
|
|
|
|
* This function sets up a new local queue and attaches the thread to it.
|
2008-10-28 07:50:07 +01:00
|
|
|
***************************************************************************/
|
2008-12-19 23:04:55 +01:00
|
|
|
void
|
|
|
|
InitDeviceThread()
|
2008-10-28 07:50:07 +01:00
|
|
|
{
|
2009-03-28 18:23:08 +01:00
|
|
|
LWP_CreateThread (&devicethread, devicecallback, NULL, NULL, 0, 40);
|
2008-10-28 07:50:07 +01:00
|
|
|
}
|
2008-09-02 03:57:21 +02:00
|
|
|
|
2008-10-28 07:50:07 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* UnmountAllFAT
|
|
|
|
* Unmounts all FAT devices
|
|
|
|
***************************************************************************/
|
|
|
|
void UnmountAllFAT()
|
|
|
|
{
|
|
|
|
#ifdef HW_RVL
|
2009-06-12 09:48:27 +02:00
|
|
|
fatUnmount("sd:");
|
|
|
|
fatUnmount("usb:");
|
2008-12-19 23:04:55 +01:00
|
|
|
#else
|
2009-06-12 09:48:27 +02:00
|
|
|
fatUnmount("carda:");
|
|
|
|
fatUnmount("cardb:");
|
2008-10-28 07:50:07 +01:00
|
|
|
#endif
|
2008-09-02 03:57:21 +02:00
|
|
|
}
|
|
|
|
|
2008-11-12 09:40:09 +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-19 23:04:55 +01:00
|
|
|
|
|
|
|
bool MountFAT(int method)
|
2008-11-12 09:40:09 +01:00
|
|
|
{
|
2008-12-19 23:04:55 +01:00
|
|
|
bool mounted = true; // assume our disc is already mounted
|
|
|
|
char name[10];
|
|
|
|
const DISC_INTERFACE* disc = NULL;
|
2008-11-12 09:40:09 +01:00
|
|
|
|
2008-12-19 23:04:55 +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
|
|
|
|
}
|
2008-11-12 09:40:09 +01:00
|
|
|
|
2009-06-12 09:48:27 +02:00
|
|
|
sprintf(rootdir, "%s:", name);
|
2008-12-19 23:04:55 +01:00
|
|
|
|
|
|
|
if(unmountRequired[method])
|
2008-11-12 09:40:09 +01:00
|
|
|
{
|
2008-12-19 23:04:55 +01:00
|
|
|
unmountRequired[method] = false;
|
2008-12-24 08:58:23 +01:00
|
|
|
fatUnmount(rootdir);
|
2008-12-19 23:04:55 +01:00
|
|
|
disc->shutdown();
|
2009-03-28 18:23:08 +01:00
|
|
|
isMounted[method] = false;
|
2008-11-12 09:40:09 +01:00
|
|
|
}
|
2008-12-19 23:04:55 +01:00
|
|
|
if(!isMounted[method])
|
|
|
|
{
|
|
|
|
if(!disc->startup())
|
|
|
|
mounted = false;
|
|
|
|
else if(!fatMountSimple(name, disc))
|
|
|
|
mounted = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
isMounted[method] = mounted;
|
2008-11-12 09:40:09 +01:00
|
|
|
return mounted;
|
|
|
|
}
|
|
|
|
|
2008-12-19 23:04:55 +01:00
|
|
|
void MountAllFAT()
|
|
|
|
{
|
|
|
|
#ifdef HW_RVL
|
|
|
|
MountFAT(METHOD_SD);
|
|
|
|
MountFAT(METHOD_USB);
|
|
|
|
#else
|
|
|
|
MountFAT(METHOD_SD_SLOTA);
|
|
|
|
MountFAT(METHOD_SD_SLOTB);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-09-02 03:57:21 +02:00
|
|
|
/****************************************************************************
|
2008-12-19 23:04:55 +01:00
|
|
|
* ChangeInterface
|
|
|
|
* Attempts to mount/configure the device specified
|
2008-10-28 07:50:07 +01:00
|
|
|
***************************************************************************/
|
2008-12-19 23:04:55 +01:00
|
|
|
bool ChangeInterface(int method, bool silent)
|
2008-09-02 03:57:21 +02:00
|
|
|
{
|
2008-10-28 07:50:07 +01:00
|
|
|
bool mounted = false;
|
|
|
|
|
2008-09-02 03:57:21 +02:00
|
|
|
if(method == METHOD_SD)
|
|
|
|
{
|
|
|
|
#ifdef HW_RVL
|
2008-12-19 23:04:55 +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:50:07 +01:00
|
|
|
if(!mounted) // internal SD and SD Gecko (on slot A) not found
|
2008-12-19 23:04:55 +01:00
|
|
|
mounted = MountFAT(METHOD_SD_SLOTB); // try SD Gecko on slot B
|
|
|
|
#endif
|
2008-10-28 07:50:07 +01:00
|
|
|
if(!mounted && !silent) // no SD device found
|
2009-03-28 18:23:08 +01:00
|
|
|
ErrorPrompt("SD card not found!");
|
2008-09-02 03:57:21 +02:00
|
|
|
}
|
|
|
|
else if(method == METHOD_USB)
|
|
|
|
{
|
|
|
|
#ifdef HW_RVL
|
2008-12-19 23:04:55 +01:00
|
|
|
mounted = MountFAT(method);
|
2008-11-12 09:40:09 +01:00
|
|
|
|
2008-10-28 07:50:07 +01:00
|
|
|
if(!mounted && !silent)
|
2009-03-28 18:23:08 +01:00
|
|
|
ErrorPrompt("USB drive not found!");
|
2008-09-02 03:57:21 +02:00
|
|
|
#endif
|
|
|
|
}
|
2008-12-19 23:04:55 +01:00
|
|
|
else if(method == METHOD_DVD)
|
|
|
|
{
|
2008-12-22 09:38:49 +01:00
|
|
|
mounted = MountDVD(silent);
|
|
|
|
}
|
2009-03-28 18:23:08 +01:00
|
|
|
#ifdef HW_RVL
|
2008-12-22 09:38:49 +01:00
|
|
|
else if(method == METHOD_SMB)
|
|
|
|
{
|
|
|
|
mounted = ConnectShare(silent);
|
2008-12-19 23:04:55 +01:00
|
|
|
}
|
2009-03-28 18:23:08 +01:00
|
|
|
#endif
|
|
|
|
else if(method == METHOD_MC_SLOTA)
|
|
|
|
{
|
|
|
|
mounted = TestMC(CARD_SLOTA, silent);
|
|
|
|
}
|
|
|
|
else if(method == METHOD_MC_SLOTB)
|
|
|
|
{
|
|
|
|
mounted = TestMC(CARD_SLOTB, silent);
|
|
|
|
}
|
2008-09-02 03:57:21 +02:00
|
|
|
|
2009-06-12 09:48:27 +02:00
|
|
|
if(!mounted)
|
|
|
|
{
|
|
|
|
sprintf(browser.dir,"/");
|
|
|
|
rootdir[0] = 0;
|
|
|
|
}
|
|
|
|
|
2008-10-28 07:50:07 +01:00
|
|
|
return mounted;
|
2008-09-02 03:57:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************************************
|
2008-12-19 23:04:55 +01:00
|
|
|
* Browse subdirectories
|
2008-11-12 09:40:09 +01:00
|
|
|
**************************************************************************/
|
2008-09-02 03:57:21 +02:00
|
|
|
int
|
2009-03-28 18:23:08 +01:00
|
|
|
ParseDirectory(int method)
|
2008-09-02 03:57:21 +02:00
|
|
|
{
|
2009-03-28 18:23:08 +01:00
|
|
|
DIR_ITER *dir = NULL;
|
2008-12-19 23:04:55 +01:00
|
|
|
char fulldir[MAXPATHLEN];
|
2008-09-02 03:57:21 +02:00
|
|
|
char filename[MAXPATHLEN];
|
|
|
|
struct stat filestat;
|
|
|
|
char msg[128];
|
2009-03-28 18:23:08 +01:00
|
|
|
int retry = 1;
|
|
|
|
bool mounted = false;
|
2008-09-02 03:57:21 +02:00
|
|
|
|
2008-12-30 01:05:57 +01:00
|
|
|
// reset browser
|
|
|
|
ResetBrowser();
|
2008-09-02 03:57:21 +02:00
|
|
|
|
2009-03-28 18:23:08 +01:00
|
|
|
ShowAction("Loading...");
|
2008-12-19 23:04:55 +01:00
|
|
|
|
2008-09-02 03:57:21 +02:00
|
|
|
// open the directory
|
2009-03-28 18:23:08 +01:00
|
|
|
while(dir == NULL && retry == 1)
|
|
|
|
{
|
|
|
|
mounted = ChangeInterface(method, NOTSILENT);
|
|
|
|
sprintf(fulldir, "%s%s", rootdir, browser.dir); // add device to path
|
|
|
|
if(mounted) dir = diropen(fulldir);
|
|
|
|
if(dir == NULL)
|
|
|
|
{
|
|
|
|
unmountRequired[method] = true;
|
|
|
|
sprintf(msg, "Error opening %s", fulldir);
|
|
|
|
retry = ErrorPromptRetry(msg);
|
|
|
|
}
|
|
|
|
}
|
2008-12-19 23:04:55 +01:00
|
|
|
|
2009-03-28 18:23:08 +01:00
|
|
|
// if we can't open the dir, try opening the root dir
|
2008-12-19 23:04:55 +01:00
|
|
|
if (dir == NULL)
|
2008-09-02 03:57:21 +02:00
|
|
|
{
|
2009-03-28 18:23:08 +01:00
|
|
|
if(ChangeInterface(method, SILENT))
|
2008-09-02 03:57:21 +02:00
|
|
|
{
|
2009-03-28 18:23:08 +01:00
|
|
|
sprintf(browser.dir,"/");
|
2009-06-12 09:48:27 +02:00
|
|
|
sprintf(fulldir, "%s%s", rootdir, browser.dir);
|
|
|
|
dir = diropen(fulldir);
|
2009-03-28 18:23:08 +01:00
|
|
|
if (dir == NULL)
|
|
|
|
{
|
|
|
|
sprintf(msg, "Error opening %s", rootdir);
|
|
|
|
ErrorPrompt(msg);
|
|
|
|
return -1;
|
|
|
|
}
|
2008-09-02 03:57:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// index files/folders
|
2008-12-30 01:05:57 +01:00
|
|
|
int entryNum = 0;
|
|
|
|
|
2008-12-19 23:04:55 +01:00
|
|
|
while(dirnext(dir,filename,&filestat) == 0)
|
2008-09-02 03:57:21 +02:00
|
|
|
{
|
|
|
|
if(strcmp(filename,".") != 0)
|
|
|
|
{
|
2009-01-05 23:21:27 +01:00
|
|
|
BROWSERENTRY * newBrowserList = (BROWSERENTRY *)realloc(browserList, (entryNum+1) * sizeof(BROWSERENTRY));
|
2008-12-30 01:05:57 +01:00
|
|
|
|
2009-01-05 23:21:27 +01:00
|
|
|
if(!newBrowserList) // failed to allocate required memory
|
2008-12-30 01:05:57 +01:00
|
|
|
{
|
2009-01-05 23:21:27 +01:00
|
|
|
ResetBrowser();
|
2009-03-28 18:23:08 +01:00
|
|
|
ErrorPrompt("Out of memory: too many files!");
|
|
|
|
entryNum = -1;
|
2008-12-30 01:05:57 +01:00
|
|
|
break;
|
|
|
|
}
|
2009-01-05 23:21:27 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
browserList = newBrowserList;
|
|
|
|
}
|
2008-12-30 01:05:57 +01:00
|
|
|
memset(&(browserList[entryNum]), 0, sizeof(BROWSERENTRY)); // clear the new entry
|
|
|
|
|
|
|
|
strncpy(browserList[entryNum].filename, filename, MAXJOLIET);
|
2009-03-28 18:23:08 +01:00
|
|
|
|
|
|
|
if(strcmp(filename,"..") == 0)
|
|
|
|
{
|
|
|
|
sprintf(browserList[entryNum].displayname, "Up One Level");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-06-27 22:05:45 +02:00
|
|
|
StripExt(browserList[entryNum].displayname, browserList[entryNum].filename); // hide file extension
|
2009-03-28 18:23:08 +01:00
|
|
|
}
|
|
|
|
|
2008-12-30 01:05:57 +01:00
|
|
|
browserList[entryNum].length = filestat.st_size;
|
2009-03-28 18:23:08 +01:00
|
|
|
browserList[entryNum].mtime = filestat.st_mtime;
|
2008-12-30 01:05:57 +01:00
|
|
|
browserList[entryNum].isdir = (filestat.st_mode & _IFDIR) == 0 ? 0 : 1; // flag this as a dir
|
|
|
|
|
|
|
|
entryNum++;
|
2008-09-02 03:57:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// close directory
|
2008-12-19 23:04:55 +01:00
|
|
|
dirclose(dir);
|
2008-09-02 03:57:21 +02:00
|
|
|
|
|
|
|
// Sort the file list
|
2008-12-30 01:05:57 +01:00
|
|
|
qsort(browserList, entryNum, sizeof(BROWSERENTRY), FileSortCallback);
|
2008-09-02 03:57:21 +02:00
|
|
|
|
2009-03-28 18:23:08 +01:00
|
|
|
CancelAction();
|
|
|
|
|
|
|
|
browser.numEntries = entryNum;
|
2008-12-30 01:05:57 +01:00
|
|
|
return entryNum;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* AllocSaveBuffer ()
|
|
|
|
* Clear and allocate the savebuffer
|
|
|
|
***************************************************************************/
|
|
|
|
void
|
|
|
|
AllocSaveBuffer ()
|
|
|
|
{
|
2009-06-01 23:56:21 +02:00
|
|
|
if(bufferLock == LWP_MUTEX_NULL)
|
|
|
|
LWP_MutexInit(&bufferLock, false);
|
2008-12-30 01:05:57 +01:00
|
|
|
|
2009-06-01 23:56:21 +02:00
|
|
|
if(bufferLock != LWP_MUTEX_NULL)
|
|
|
|
LWP_MutexLock(bufferLock);
|
2008-12-30 01:05:57 +01:00
|
|
|
memset (savebuffer, 0, SAVEBUFFERSIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* FreeSaveBuffer ()
|
|
|
|
* Free the savebuffer memory
|
|
|
|
***************************************************************************/
|
|
|
|
void
|
|
|
|
FreeSaveBuffer ()
|
|
|
|
{
|
2009-06-01 23:56:21 +02:00
|
|
|
if(bufferLock != LWP_MUTEX_NULL)
|
|
|
|
LWP_MutexUnlock(bufferLock);
|
2008-09-02 03:57:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2008-12-19 23:04:55 +01:00
|
|
|
* LoadSzFile
|
2008-11-12 09:40:09 +01:00
|
|
|
* Loads the selected file # from the specified 7z into rbuffer
|
|
|
|
* Returns file size
|
2008-10-13 20:15:03 +02:00
|
|
|
***************************************************************************/
|
2008-12-19 23:04:55 +01:00
|
|
|
u32
|
|
|
|
LoadSzFile(char * filepath, unsigned char * rbuffer)
|
2008-09-02 03:57:21 +02:00
|
|
|
{
|
2008-12-19 23:04:55 +01:00
|
|
|
u32 size = 0;
|
|
|
|
|
|
|
|
// stop checking if devices were removed/inserted
|
|
|
|
// since we're loading a file
|
2009-05-06 19:27:20 +02:00
|
|
|
HaltDeviceThread();
|
2008-12-19 23:04:55 +01:00
|
|
|
|
|
|
|
file = fopen (filepath, "rb");
|
|
|
|
if (file > 0)
|
2008-09-02 03:57:21 +02:00
|
|
|
{
|
2008-12-30 01:05:57 +01:00
|
|
|
size = SzExtractFile(browserList[browser.selIndex].offset, rbuffer);
|
2008-12-19 23:04:55 +01:00
|
|
|
fclose (file);
|
2008-09-02 03:57:21 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-03-28 18:23:08 +01:00
|
|
|
ErrorPrompt("Error opening file");
|
2008-09-02 03:57:21 +02:00
|
|
|
}
|
2008-12-19 23:04:55 +01:00
|
|
|
|
|
|
|
// go back to checking if devices were inserted/removed
|
2009-05-06 19:27:20 +02:00
|
|
|
ResumeDeviceThread();
|
2008-12-19 23:04:55 +01:00
|
|
|
|
|
|
|
return size;
|
2008-09-02 03:57:21 +02:00
|
|
|
}
|
|
|
|
|
2008-10-13 20:15:03 +02:00
|
|
|
/****************************************************************************
|
2008-12-19 23:04:55 +01:00
|
|
|
* LoadFile
|
2008-10-13 20:15:03 +02:00
|
|
|
***************************************************************************/
|
2008-12-19 23:04:55 +01:00
|
|
|
u32
|
2009-01-25 08:01:50 +01:00
|
|
|
LoadFile (char * rbuffer, char *filepath, u32 length, int method, bool silent)
|
2008-10-13 20:15:03 +02:00
|
|
|
{
|
2008-11-12 09:40:09 +01:00
|
|
|
char zipbuffer[2048];
|
2008-12-19 23:04:55 +01:00
|
|
|
u32 size = 0;
|
|
|
|
u32 readsize = 0;
|
2009-03-28 18:23:08 +01:00
|
|
|
char fullpath[MAXPATHLEN];
|
|
|
|
int retry = 1;
|
2008-12-19 23:04:55 +01:00
|
|
|
|
|
|
|
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
|
2009-05-06 19:27:20 +02:00
|
|
|
HaltDeviceThread();
|
2008-12-19 23:04:55 +01:00
|
|
|
|
2009-03-28 18:23:08 +01:00
|
|
|
// open the file
|
|
|
|
while(!size && retry == 1)
|
2008-10-13 20:15:03 +02:00
|
|
|
{
|
2009-03-28 18:23:08 +01:00
|
|
|
if(ChangeInterface(method, silent))
|
2008-11-12 09:40:09 +01:00
|
|
|
{
|
2009-03-28 18:23:08 +01:00
|
|
|
sprintf(fullpath, "%s%s", rootdir, filepath); // add device to filepath
|
|
|
|
file = fopen (fullpath, "rb");
|
2008-11-12 09:40:09 +01:00
|
|
|
|
2009-03-28 18:23:08 +01:00
|
|
|
if(file > 0)
|
2008-11-12 09:40:09 +01:00
|
|
|
{
|
2009-03-28 18:23:08 +01:00
|
|
|
if(length > 0 && length <= 2048) // do a partial read (eg: to check file header)
|
2008-11-12 09:40:09 +01:00
|
|
|
{
|
2009-03-28 18:23:08 +01:00
|
|
|
size = fread (rbuffer, 1, length, file);
|
2008-11-12 09:40:09 +01:00
|
|
|
}
|
2009-03-28 18:23:08 +01:00
|
|
|
else // load whole file
|
2008-11-12 09:40:09 +01:00
|
|
|
{
|
2009-03-28 18:23:08 +01:00
|
|
|
readsize = fread (zipbuffer, 1, 2048, file);
|
2008-12-19 23:04:55 +01:00
|
|
|
|
2009-03-28 18:23:08 +01:00
|
|
|
if(readsize > 0)
|
2008-12-19 23:04:55 +01:00
|
|
|
{
|
2009-03-28 18:23:08 +01:00
|
|
|
if (IsZipFile (zipbuffer))
|
|
|
|
{
|
|
|
|
size = UnZipBuffer ((unsigned char *)rbuffer, method); // unzip
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct stat fileinfo;
|
2009-05-06 08:33:34 +02:00
|
|
|
if(fstat(file->_file, &fileinfo) == 0)
|
|
|
|
{
|
|
|
|
size = fileinfo.st_size;
|
2009-03-28 18:23:08 +01:00
|
|
|
|
2009-05-06 08:33:34 +02:00
|
|
|
memcpy (rbuffer, zipbuffer, readsize); // copy what we already read
|
2009-03-28 18:23:08 +01:00
|
|
|
|
2009-05-06 08:33:34 +02:00
|
|
|
u32 offset = readsize;
|
|
|
|
u32 nextread = 0;
|
|
|
|
while(offset < size)
|
|
|
|
{
|
2009-05-30 08:28:00 +02:00
|
|
|
if(size - offset > 4*1024) nextread = 4*1024;
|
2009-05-06 08:33:34 +02:00
|
|
|
else nextread = size-offset;
|
|
|
|
ShowProgress ("Loading...", offset, size);
|
|
|
|
readsize = fread (rbuffer + offset, 1, nextread, file); // read in next chunk
|
2009-03-28 18:23:08 +01:00
|
|
|
|
2009-05-06 08:33:34 +02:00
|
|
|
if(readsize <= 0 || readsize > nextread)
|
|
|
|
break; // read failure
|
2009-03-28 18:23:08 +01:00
|
|
|
|
2009-05-06 08:33:34 +02:00
|
|
|
if(readsize > 0)
|
|
|
|
offset += readsize;
|
|
|
|
}
|
|
|
|
CancelAction();
|
2009-03-28 18:23:08 +01:00
|
|
|
|
2009-05-06 08:33:34 +02:00
|
|
|
if(offset != size) // # bytes read doesn't match # expected
|
|
|
|
size = 0;
|
|
|
|
}
|
2009-03-28 18:23:08 +01:00
|
|
|
}
|
2008-12-19 23:04:55 +01:00
|
|
|
}
|
2008-11-12 09:40:09 +01:00
|
|
|
}
|
2009-03-28 18:23:08 +01:00
|
|
|
fclose (file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!size)
|
|
|
|
{
|
|
|
|
if(!silent)
|
|
|
|
{
|
|
|
|
unmountRequired[method] = true;
|
|
|
|
retry = ErrorPromptRetry("Error loading file!");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
retry = 0;
|
2008-11-12 09:40:09 +01:00
|
|
|
}
|
|
|
|
}
|
2008-10-13 20:15:03 +02:00
|
|
|
}
|
2008-12-19 23:04:55 +01:00
|
|
|
|
|
|
|
// go back to checking if devices were inserted/removed
|
2009-05-06 19:27:20 +02:00
|
|
|
ResumeDeviceThread();
|
2009-03-28 18:23:08 +01:00
|
|
|
CancelAction();
|
2008-12-19 23:04:55 +01:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2009-03-28 18:23:08 +01:00
|
|
|
u32 LoadFile(char * filepath, int method, bool silent)
|
2008-12-19 23:04:55 +01:00
|
|
|
{
|
2009-01-25 08:01:50 +01:00
|
|
|
return LoadFile((char *)savebuffer, filepath, 0, method, silent);
|
2008-10-13 20:15:03 +02:00
|
|
|
}
|
|
|
|
|
2008-09-02 03:57:21 +02:00
|
|
|
/****************************************************************************
|
2008-12-19 23:04:55 +01:00
|
|
|
* SaveFile
|
|
|
|
* Write buffer to file
|
2008-10-01 07:25:38 +02:00
|
|
|
***************************************************************************/
|
2008-12-19 23:04:55 +01:00
|
|
|
u32
|
2009-01-25 08:01:50 +01:00
|
|
|
SaveFile (char * buffer, char *filepath, u32 datasize, int method, bool silent)
|
2008-09-02 03:57:21 +02:00
|
|
|
{
|
2009-03-28 18:23:08 +01:00
|
|
|
char fullpath[MAXPATHLEN];
|
2008-12-19 23:04:55 +01:00
|
|
|
u32 written = 0;
|
2009-03-28 18:23:08 +01:00
|
|
|
int retry = 1;
|
2008-12-19 23:04:55 +01:00
|
|
|
|
2009-03-28 18:23:08 +01:00
|
|
|
if(datasize == 0)
|
2008-12-19 23:04:55 +01:00
|
|
|
return 0;
|
|
|
|
|
2009-03-28 18:23:08 +01:00
|
|
|
ShowAction("Saving...");
|
|
|
|
|
|
|
|
if(method == METHOD_MC_SLOTA || method == METHOD_MC_SLOTB)
|
2008-12-19 23:04:55 +01:00
|
|
|
{
|
2009-03-28 18:23:08 +01:00
|
|
|
if(method == METHOD_MC_SLOTA)
|
2008-12-19 23:04:55 +01:00
|
|
|
return SaveMCFile (buffer, CARD_SLOTA, filepath, datasize, silent);
|
2009-03-28 18:23:08 +01:00
|
|
|
else
|
2008-12-19 23:04:55 +01:00
|
|
|
return SaveMCFile (buffer, CARD_SLOTB, filepath, datasize, silent);
|
|
|
|
}
|
|
|
|
|
2009-03-28 18:23:08 +01:00
|
|
|
// stop checking if devices were removed/inserted
|
|
|
|
// since we're saving a file
|
2009-05-06 19:27:20 +02:00
|
|
|
HaltDeviceThread();
|
2008-12-19 23:04:55 +01:00
|
|
|
|
2009-03-28 18:23:08 +01:00
|
|
|
while(!written && retry == 1)
|
|
|
|
{
|
|
|
|
if(ChangeInterface(method, silent))
|
2008-12-19 23:04:55 +01:00
|
|
|
{
|
2009-03-28 18:23:08 +01:00
|
|
|
sprintf(fullpath, "%s%s", rootdir, filepath); // add device to filepath
|
|
|
|
file = fopen (fullpath, "wb");
|
2008-12-19 23:04:55 +01:00
|
|
|
|
2009-03-28 18:23:08 +01:00
|
|
|
if (file > 0)
|
|
|
|
{
|
2009-05-06 08:33:34 +02:00
|
|
|
u32 writesize, nextwrite;
|
|
|
|
while(written < datasize)
|
|
|
|
{
|
2009-05-30 08:28:00 +02:00
|
|
|
if(datasize - written > 4*1024) nextwrite=4*1024;
|
2009-05-06 08:33:34 +02:00
|
|
|
else nextwrite = datasize-written;
|
|
|
|
writesize = fwrite (buffer+written, 1, nextwrite, file);
|
|
|
|
if(writesize != nextwrite) break; // write failure
|
|
|
|
written += writesize;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(written != datasize) written = 0;
|
2009-03-28 18:23:08 +01:00
|
|
|
fclose (file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!written)
|
2008-12-19 23:04:55 +01:00
|
|
|
{
|
|
|
|
unmountRequired[method] = true;
|
2009-03-28 18:23:08 +01:00
|
|
|
if(!silent)
|
|
|
|
retry = ErrorPromptRetry("Error saving file!");
|
|
|
|
else
|
|
|
|
retry = 0;
|
2008-12-19 23:04:55 +01:00
|
|
|
}
|
2009-03-28 18:23:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// go back to checking if devices were inserted/removed
|
2009-05-06 19:27:20 +02:00
|
|
|
ResumeDeviceThread();
|
2008-12-19 23:04:55 +01:00
|
|
|
|
2009-03-28 18:23:08 +01:00
|
|
|
CancelAction();
|
2008-12-19 23:04:55 +01:00
|
|
|
return written;
|
|
|
|
}
|
|
|
|
|
2009-03-28 18:23:08 +01:00
|
|
|
u32 SaveFile(char * filepath, u32 datasize, int method, bool silent)
|
2008-12-19 23:04:55 +01:00
|
|
|
{
|
2009-01-25 08:01:50 +01:00
|
|
|
return SaveFile((char *)savebuffer, filepath, datasize, method, silent);
|
2008-09-02 03:57:21 +02:00
|
|
|
}
|