2008-08-06 03:09:59 +02:00
|
|
|
/****************************************************************************
|
2010-01-27 23:20:37 +01:00
|
|
|
* Snes9x Nintendo Wii/Gamecube Port
|
2008-08-14 00:44:59 +02:00
|
|
|
*
|
2008-08-06 03:09:59 +02:00
|
|
|
* softdev July 2006
|
|
|
|
* crunchy2 May 2007
|
2008-10-14 11:21:34 +02:00
|
|
|
* Michniewski 2008
|
2010-01-27 23:20:37 +01:00
|
|
|
* Tantric 2008-2010
|
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-12-24 09:02:04 +01:00
|
|
|
* File operations
|
2008-09-12 07:28:40 +02:00
|
|
|
***************************************************************************/
|
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
#include <gccore.h>
|
|
|
|
#include <stdio.h>
|
2008-12-18 19:36:30 +01:00
|
|
|
#include <stdlib.h>
|
2008-08-06 03:09:59 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <ogcsys.h>
|
2011-01-20 03:52:56 +01:00
|
|
|
#include <dirent.h>
|
2008-12-18 19:36:30 +01:00
|
|
|
#include <sys/stat.h>
|
2008-08-10 05:14:39 +02:00
|
|
|
#include <zlib.h>
|
2008-12-30 01:08:17 +01:00
|
|
|
#include <malloc.h>
|
2008-12-18 19:36:30 +01:00
|
|
|
#include <sdcard/wiisd_io.h>
|
|
|
|
#include <sdcard/gcsd.h>
|
|
|
|
#include <ogc/usbstorage.h>
|
2009-10-03 19:02:45 +02:00
|
|
|
#include <di/di.h>
|
|
|
|
#include <ogc/dvd.h>
|
|
|
|
#include <iso9660.h>
|
2008-08-10 05:14:39 +02:00
|
|
|
|
2010-03-22 00:43:54 +01:00
|
|
|
#include "snes9xgx.h"
|
2008-08-07 07:19:17 +02:00
|
|
|
#include "fileop.h"
|
2008-12-24 09:02:04 +01:00
|
|
|
#include "networkop.h"
|
|
|
|
#include "gcunzip.h"
|
2009-03-11 18:28:37 +01:00
|
|
|
#include "menu.h"
|
|
|
|
#include "filebrowser.h"
|
2009-12-23 01:38:11 +01:00
|
|
|
#include "gui/gui.h"
|
2008-08-06 03:09:59 +02:00
|
|
|
|
2009-07-07 08:35:53 +02:00
|
|
|
#define THREAD_SLEEP 100
|
|
|
|
|
2010-06-29 09:48:17 +02:00
|
|
|
unsigned char *savebuffer = NULL;
|
2009-06-01 23:56:31 +02:00
|
|
|
static mutex_t bufferLock = LWP_MUTEX_NULL;
|
2008-12-30 01:08:17 +01:00
|
|
|
FILE * file; // file pointer - the only one we should ever use!
|
2010-04-02 23:02:16 +02:00
|
|
|
bool unmountRequired[7] = { false, false, false, false, false, false, false };
|
|
|
|
bool isMounted[7] = { false, false, false, false, false, false, false };
|
2008-08-06 03:55:59 +02:00
|
|
|
|
2008-12-18 19:36:30 +01:00
|
|
|
#ifdef HW_RVL
|
|
|
|
const DISC_INTERFACE* sd = &__io_wiisd;
|
|
|
|
const DISC_INTERFACE* usb = &__io_usbstorage;
|
2009-10-03 19:02:45 +02:00
|
|
|
const DISC_INTERFACE* dvd = &__io_wiidvd;
|
2008-12-18 19:36:30 +01:00
|
|
|
#else
|
|
|
|
const DISC_INTERFACE* carda = &__io_gcsda;
|
|
|
|
const DISC_INTERFACE* cardb = &__io_gcsdb;
|
2009-10-03 19:02:45 +02:00
|
|
|
const DISC_INTERFACE* dvd = &__io_gcdvd;
|
2008-12-18 19:36:30 +01:00
|
|
|
#endif
|
2008-08-07 05:25:02 +02:00
|
|
|
|
2009-07-07 08:35:53 +02:00
|
|
|
// folder parsing thread
|
|
|
|
static lwp_t parsethread = LWP_THREAD_NULL;
|
2011-01-20 03:52:56 +01:00
|
|
|
static DIR *dir = NULL;
|
2009-07-07 08:35:53 +02:00
|
|
|
static bool parseHalt = true;
|
2010-07-15 08:55:40 +02:00
|
|
|
static bool parseFilter = true;
|
2009-07-07 08:35:53 +02:00
|
|
|
bool ParseDirEntries();
|
2009-12-23 01:38:11 +01:00
|
|
|
int selectLoadedFile = 0;
|
2009-07-07 08:35:53 +02:00
|
|
|
|
|
|
|
// device thread
|
2009-05-06 19:28:06 +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()
|
|
|
|
{
|
2010-04-02 23:02:16 +02:00
|
|
|
#ifdef HW_RVL
|
2009-05-06 19:28:06 +02:00
|
|
|
deviceHalt = true;
|
|
|
|
|
|
|
|
// wait for thread to finish
|
|
|
|
while(!LWP_ThreadIsSuspended(devicethread))
|
2009-07-07 08:35:53 +02:00
|
|
|
usleep(THREAD_SLEEP);
|
2010-04-02 23:02:16 +02:00
|
|
|
#endif
|
2009-05-06 19:28:06 +02:00
|
|
|
}
|
2008-12-18 19:36:30 +01:00
|
|
|
|
2009-10-02 00:35:12 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* HaltParseThread
|
|
|
|
*
|
|
|
|
* Signals the parse thread to stop.
|
|
|
|
***************************************************************************/
|
|
|
|
void
|
|
|
|
HaltParseThread()
|
|
|
|
{
|
|
|
|
parseHalt = true;
|
|
|
|
|
|
|
|
while(!LWP_ThreadIsSuspended(parsethread))
|
|
|
|
usleep(THREAD_SLEEP);
|
|
|
|
}
|
|
|
|
|
2010-04-02 23:02:16 +02:00
|
|
|
|
2008-12-18 19:36:30 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* devicecallback
|
|
|
|
*
|
2010-08-18 19:35:23 +02:00
|
|
|
* This checks our devices for changes (SD/USB/DVD removed)
|
2008-12-18 19:36:30 +01:00
|
|
|
***************************************************************************/
|
2010-04-02 23:02:16 +02:00
|
|
|
#ifdef HW_RVL
|
2010-08-18 02:15:25 +02:00
|
|
|
static int devsleep;
|
2009-05-26 23:48:12 +02:00
|
|
|
|
2008-12-18 19:36:30 +01:00
|
|
|
static void *
|
|
|
|
devicecallback (void *arg)
|
2008-10-28 07:52:38 +01:00
|
|
|
{
|
2008-12-18 19:36:30 +01:00
|
|
|
while (1)
|
|
|
|
{
|
2009-10-02 00:35:12 +02:00
|
|
|
if(isMounted[DEVICE_SD])
|
2008-12-18 19:36:30 +01:00
|
|
|
{
|
|
|
|
if(!sd->isInserted()) // check if the device was removed
|
|
|
|
{
|
2009-10-02 00:35:12 +02:00
|
|
|
unmountRequired[DEVICE_SD] = true;
|
|
|
|
isMounted[DEVICE_SD] = false;
|
2008-12-18 19:36:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-02 00:35:12 +02:00
|
|
|
if(isMounted[DEVICE_USB])
|
2008-12-18 19:36:30 +01:00
|
|
|
{
|
2009-05-06 00:32:58 +02:00
|
|
|
if(!usb->isInserted()) // check if the device was removed
|
2008-12-18 19:36:30 +01:00
|
|
|
{
|
2009-10-02 00:35:12 +02:00
|
|
|
unmountRequired[DEVICE_USB] = true;
|
|
|
|
isMounted[DEVICE_USB] = false;
|
2008-12-18 19:36:30 +01:00
|
|
|
}
|
|
|
|
}
|
2009-02-08 22:30:39 +01:00
|
|
|
|
2009-10-03 19:02:45 +02:00
|
|
|
if(isMounted[DEVICE_DVD])
|
|
|
|
{
|
|
|
|
if(!dvd->isInserted()) // check if the device was removed
|
|
|
|
{
|
|
|
|
unmountRequired[DEVICE_DVD] = true;
|
|
|
|
isMounted[DEVICE_DVD] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-26 23:48:12 +02:00
|
|
|
devsleep = 1000*1000; // 1 sec
|
|
|
|
|
|
|
|
while(devsleep > 0)
|
|
|
|
{
|
|
|
|
if(deviceHalt)
|
|
|
|
LWP_SuspendThread(devicethread);
|
2009-07-07 08:35:53 +02:00
|
|
|
usleep(THREAD_SLEEP);
|
|
|
|
devsleep -= THREAD_SLEEP;
|
2009-05-26 23:48:12 +02:00
|
|
|
}
|
2010-08-18 02:15:25 +02:00
|
|
|
UpdateCheck();
|
2008-12-18 19:36:30 +01:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-04-02 23:02:16 +02:00
|
|
|
#endif
|
2008-12-18 19:36:30 +01:00
|
|
|
|
2009-07-07 08:35:53 +02:00
|
|
|
static void *
|
|
|
|
parsecallback (void *arg)
|
|
|
|
{
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
while(ParseDirEntries())
|
|
|
|
usleep(THREAD_SLEEP);
|
|
|
|
LWP_SuspendThread(parsethread);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-12-18 19:36:30 +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.
|
|
|
|
***************************************************************************/
|
|
|
|
void
|
|
|
|
InitDeviceThread()
|
|
|
|
{
|
2010-04-02 23:02:16 +02:00
|
|
|
#ifdef HW_RVL
|
2009-02-07 04:01:10 +01:00
|
|
|
LWP_CreateThread (&devicethread, devicecallback, NULL, NULL, 0, 40);
|
2010-04-02 23:02:16 +02:00
|
|
|
#endif
|
2009-07-07 08:35:53 +02:00
|
|
|
LWP_CreateThread (&parsethread, parsecallback, NULL, NULL, 0, 80);
|
2008-08-07 05:25:02 +02:00
|
|
|
}
|
|
|
|
|
2008-08-08 09:57:01 +02:00
|
|
|
/****************************************************************************
|
2008-10-28 07:52:38 +01:00
|
|
|
* UnmountAllFAT
|
|
|
|
* Unmounts all FAT devices
|
|
|
|
***************************************************************************/
|
|
|
|
void UnmountAllFAT()
|
|
|
|
{
|
|
|
|
#ifdef HW_RVL
|
2009-06-12 09:47:42 +02:00
|
|
|
fatUnmount("sd:");
|
|
|
|
fatUnmount("usb:");
|
2008-12-18 19:36:30 +01:00
|
|
|
#else
|
2009-06-12 09:47:42 +02: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
|
|
|
|
2009-10-02 00:35:12 +02:00
|
|
|
static bool MountFAT(int device, int silent)
|
2008-11-12 08:50:39 +01:00
|
|
|
{
|
2009-10-06 08:37:53 +02:00
|
|
|
bool mounted = false;
|
|
|
|
int retry = 1;
|
2009-10-02 00:35:12 +02:00
|
|
|
char name[10], name2[10];
|
2008-12-18 19:36:30 +01:00
|
|
|
const DISC_INTERFACE* disc = NULL;
|
2008-11-12 08:50:39 +01:00
|
|
|
|
2009-10-02 00:35:12 +02:00
|
|
|
switch(device)
|
2008-12-18 19:36:30 +01:00
|
|
|
{
|
|
|
|
#ifdef HW_RVL
|
2009-10-02 00:35:12 +02:00
|
|
|
case DEVICE_SD:
|
2008-12-18 19:36:30 +01:00
|
|
|
sprintf(name, "sd");
|
2009-10-02 00:35:12 +02:00
|
|
|
sprintf(name2, "sd:");
|
2008-12-18 19:36:30 +01:00
|
|
|
disc = sd;
|
|
|
|
break;
|
2009-10-02 00:35:12 +02:00
|
|
|
case DEVICE_USB:
|
2008-12-18 19:36:30 +01:00
|
|
|
sprintf(name, "usb");
|
2009-10-02 00:35:12 +02:00
|
|
|
sprintf(name2, "usb:");
|
2008-12-18 19:36:30 +01:00
|
|
|
disc = usb;
|
|
|
|
break;
|
|
|
|
#else
|
2009-10-02 00:35:12 +02:00
|
|
|
case DEVICE_SD_SLOTA:
|
2008-12-18 19:36:30 +01:00
|
|
|
sprintf(name, "carda");
|
2009-10-02 00:35:12 +02:00
|
|
|
sprintf(name2, "carda:");
|
2008-12-18 19:36:30 +01:00
|
|
|
disc = carda;
|
|
|
|
break;
|
|
|
|
|
2009-10-02 00:35:12 +02:00
|
|
|
case DEVICE_SD_SLOTB:
|
2008-12-18 19:36:30 +01:00
|
|
|
sprintf(name, "cardb");
|
2009-10-02 00:35:12 +02:00
|
|
|
sprintf(name2, "cardb:");
|
2008-12-18 19:36:30 +01:00
|
|
|
disc = cardb;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return false; // unknown device
|
|
|
|
}
|
|
|
|
|
2009-10-02 00:35:12 +02:00
|
|
|
if(unmountRequired[device])
|
2008-11-12 08:50:39 +01:00
|
|
|
{
|
2009-10-02 00:35:12 +02:00
|
|
|
unmountRequired[device] = false;
|
|
|
|
fatUnmount(name2);
|
2008-12-18 19:36:30 +01:00
|
|
|
disc->shutdown();
|
2009-10-02 00:35:12 +02:00
|
|
|
isMounted[device] = false;
|
2008-12-18 19:36:30 +01:00
|
|
|
}
|
2009-10-06 08:37:53 +02:00
|
|
|
|
|
|
|
while(retry)
|
2009-10-02 00:35:12 +02:00
|
|
|
{
|
2009-10-06 08:37:53 +02:00
|
|
|
if(disc->startup() && fatMountSimple(name, disc))
|
|
|
|
mounted = true;
|
|
|
|
|
|
|
|
if(mounted || silent)
|
|
|
|
break;
|
|
|
|
|
2010-04-02 23:02:16 +02:00
|
|
|
#ifdef HW_RVL
|
2009-10-02 00:35:12 +02:00
|
|
|
if(device == DEVICE_SD)
|
2009-10-06 08:37:53 +02:00
|
|
|
retry = ErrorPromptRetry("SD card not found!");
|
2009-10-02 00:35:12 +02:00
|
|
|
else
|
2009-10-06 08:37:53 +02:00
|
|
|
retry = ErrorPromptRetry("USB drive not found!");
|
2010-04-02 23:02:16 +02:00
|
|
|
#else
|
|
|
|
retry = ErrorPromptRetry("SD card not found!");
|
|
|
|
#endif
|
2009-10-02 00:35:12 +02:00
|
|
|
}
|
2008-12-18 19:36:30 +01:00
|
|
|
|
2009-10-02 00:35:12 +02:00
|
|
|
isMounted[device] = mounted;
|
2008-11-12 08:50:39 +01:00
|
|
|
return mounted;
|
|
|
|
}
|
|
|
|
|
2008-12-18 19:36:30 +01:00
|
|
|
void MountAllFAT()
|
|
|
|
{
|
|
|
|
#ifdef HW_RVL
|
2009-10-02 00:35:12 +02:00
|
|
|
MountFAT(DEVICE_SD, SILENT);
|
|
|
|
MountFAT(DEVICE_USB, SILENT);
|
2008-12-18 19:36:30 +01:00
|
|
|
#else
|
2009-10-02 00:35:12 +02:00
|
|
|
MountFAT(DEVICE_SD_SLOTA, SILENT);
|
|
|
|
MountFAT(DEVICE_SD_SLOTB, SILENT);
|
2008-12-18 19:36:30 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-10-03 19:02:45 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* MountDVD()
|
|
|
|
*
|
|
|
|
* Tests if a ISO9660 DVD is inserted and available, and mounts it
|
|
|
|
***************************************************************************/
|
|
|
|
bool MountDVD(bool silent)
|
|
|
|
{
|
|
|
|
bool mounted = false;
|
2009-10-06 08:37:53 +02:00
|
|
|
int retry = 1;
|
2009-10-03 19:02:45 +02:00
|
|
|
|
|
|
|
if(unmountRequired[DEVICE_DVD])
|
|
|
|
{
|
|
|
|
unmountRequired[DEVICE_DVD] = false;
|
|
|
|
ISO9660_Unmount();
|
|
|
|
}
|
|
|
|
|
2009-10-06 08:37:53 +02:00
|
|
|
while(retry)
|
2009-10-03 19:02:45 +02:00
|
|
|
{
|
2009-10-06 08:37:53 +02:00
|
|
|
ShowAction("Loading DVD...");
|
|
|
|
|
|
|
|
if(!dvd->isInserted())
|
|
|
|
{
|
|
|
|
if(silent)
|
|
|
|
break;
|
2009-10-03 19:02:45 +02:00
|
|
|
|
2009-10-06 08:37:53 +02:00
|
|
|
retry = ErrorPromptRetry("No disc inserted!");
|
|
|
|
}
|
|
|
|
else if(!ISO9660_Mount())
|
|
|
|
{
|
|
|
|
if(silent)
|
|
|
|
break;
|
|
|
|
|
2010-03-18 00:20:08 +01:00
|
|
|
retry = ErrorPromptRetry("Unrecognized DVD format.");
|
2009-10-06 08:37:53 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mounted = true;
|
|
|
|
break;
|
|
|
|
}
|
2009-10-03 19:02:45 +02:00
|
|
|
}
|
|
|
|
CancelAction();
|
|
|
|
isMounted[DEVICE_DVD] = mounted;
|
|
|
|
return mounted;
|
|
|
|
}
|
|
|
|
|
2009-10-02 00:35:12 +02:00
|
|
|
bool FindDevice(char * filepath, int * device)
|
2008-08-08 09:57:01 +02:00
|
|
|
{
|
2009-10-02 00:35:12 +02:00
|
|
|
if(!filepath || filepath[0] == 0)
|
|
|
|
return false;
|
2010-04-02 23:02:16 +02:00
|
|
|
|
2009-10-02 00:35:12 +02:00
|
|
|
if(strncmp(filepath, "sd:", 3) == 0)
|
2008-08-08 09:57:01 +02:00
|
|
|
{
|
2009-10-02 00:35:12 +02:00
|
|
|
*device = DEVICE_SD;
|
|
|
|
return true;
|
2008-08-08 09:57:01 +02:00
|
|
|
}
|
2009-10-02 00:35:12 +02:00
|
|
|
else if(strncmp(filepath, "usb:", 4) == 0)
|
2008-08-08 09:57:01 +02:00
|
|
|
{
|
2009-10-02 00:35:12 +02:00
|
|
|
*device = DEVICE_USB;
|
|
|
|
return true;
|
2008-08-08 09:57:01 +02:00
|
|
|
}
|
2009-10-02 00:35:12 +02:00
|
|
|
else if(strncmp(filepath, "smb:", 4) == 0)
|
2008-12-22 10:20:35 +01:00
|
|
|
{
|
2009-10-02 00:35:12 +02:00
|
|
|
*device = DEVICE_SMB;
|
|
|
|
return true;
|
2008-12-18 19:36:30 +01:00
|
|
|
}
|
2010-04-02 23:02:16 +02:00
|
|
|
else if(strncmp(filepath, "carda:", 6) == 0)
|
2009-03-11 18:28:37 +01:00
|
|
|
{
|
2009-10-02 00:35:12 +02:00
|
|
|
*device = DEVICE_SD_SLOTA;
|
|
|
|
return true;
|
2009-03-11 18:28:37 +01:00
|
|
|
}
|
2010-04-02 23:02:16 +02:00
|
|
|
else if(strncmp(filepath, "cardb:", 6) == 0)
|
2009-03-11 18:28:37 +01:00
|
|
|
{
|
2009-10-02 00:35:12 +02:00
|
|
|
*device = DEVICE_SD_SLOTB;
|
|
|
|
return true;
|
2009-03-11 18:28:37 +01:00
|
|
|
}
|
2010-04-02 23:02:16 +02:00
|
|
|
else if(strncmp(filepath, "dvd:", 4) == 0)
|
|
|
|
{
|
|
|
|
*device = DEVICE_DVD;
|
|
|
|
return true;
|
|
|
|
}
|
2009-10-02 00:35:12 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char * StripDevice(char * path)
|
|
|
|
{
|
|
|
|
if(path == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
char * newpath = strchr(path,'/');
|
|
|
|
|
|
|
|
if(newpath != NULL)
|
|
|
|
newpath++;
|
|
|
|
|
|
|
|
return newpath;
|
|
|
|
}
|
2008-08-08 09:57:01 +02:00
|
|
|
|
2009-10-02 00:35:12 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* ChangeInterface
|
|
|
|
* Attempts to mount/configure the device specified
|
|
|
|
***************************************************************************/
|
|
|
|
bool ChangeInterface(int device, bool silent)
|
|
|
|
{
|
2009-10-06 08:37:53 +02:00
|
|
|
if(isMounted[device])
|
|
|
|
return true;
|
|
|
|
|
2009-10-02 00:35:12 +02:00
|
|
|
bool mounted = false;
|
2009-10-06 08:37:53 +02:00
|
|
|
|
2009-10-02 00:35:12 +02:00
|
|
|
switch(device)
|
2009-06-12 09:47:42 +02:00
|
|
|
{
|
2010-04-02 23:02:16 +02:00
|
|
|
#ifdef HW_RVL
|
2009-10-02 00:35:12 +02:00
|
|
|
case DEVICE_SD:
|
|
|
|
case DEVICE_USB:
|
2010-04-02 23:02:16 +02:00
|
|
|
#else
|
|
|
|
case DEVICE_SD_SLOTA:
|
|
|
|
case DEVICE_SD_SLOTB:
|
|
|
|
#endif
|
2009-10-02 00:35:12 +02:00
|
|
|
mounted = MountFAT(device, silent);
|
|
|
|
break;
|
|
|
|
case DEVICE_DVD:
|
|
|
|
mounted = MountDVD(silent);
|
|
|
|
break;
|
2010-04-02 23:02:16 +02:00
|
|
|
#ifdef HW_RVL
|
2009-10-02 00:35:12 +02:00
|
|
|
case DEVICE_SMB:
|
|
|
|
mounted = ConnectShare(silent);
|
|
|
|
break;
|
2010-04-02 23:02:16 +02:00
|
|
|
#endif
|
2009-06-12 09:47:42 +02:00
|
|
|
}
|
|
|
|
|
2008-10-28 07:52:38 +01:00
|
|
|
return mounted;
|
2008-08-08 09:57:01 +02:00
|
|
|
}
|
|
|
|
|
2009-10-02 00:35:12 +02:00
|
|
|
bool ChangeInterface(char * filepath, bool silent)
|
|
|
|
{
|
|
|
|
int device = -1;
|
|
|
|
|
|
|
|
if(!FindDevice(filepath, &device))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return ChangeInterface(device, silent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CreateAppPath(char * origpath)
|
|
|
|
{
|
2009-10-13 09:28:58 +02:00
|
|
|
if(!origpath || origpath[0] == 0)
|
|
|
|
return;
|
|
|
|
|
2009-10-02 00:35:12 +02:00
|
|
|
char * path = strdup(origpath); // make a copy so we don't mess up original
|
|
|
|
|
|
|
|
if(!path)
|
|
|
|
return;
|
|
|
|
|
|
|
|
char * loc = strrchr(path,'/');
|
|
|
|
if (loc != NULL)
|
|
|
|
*loc = 0; // strip file name
|
|
|
|
|
|
|
|
int pos = 0;
|
|
|
|
|
|
|
|
// replace fat:/ with sd:/
|
|
|
|
if(strncmp(path, "fat:/", 5) == 0)
|
|
|
|
{
|
|
|
|
pos++;
|
|
|
|
path[1] = 's';
|
|
|
|
path[2] = 'd';
|
|
|
|
}
|
|
|
|
if(ChangeInterface(&path[pos], SILENT))
|
|
|
|
strncpy(appPath, &path[pos], MAXPATHLEN);
|
|
|
|
appPath[MAXPATHLEN-1] = 0;
|
|
|
|
free(path);
|
|
|
|
}
|
|
|
|
|
2010-07-12 08:19:35 +02:00
|
|
|
static char *GetExt(char *file)
|
|
|
|
{
|
|
|
|
if(!file)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
char *ext = strrchr(file,'.');
|
|
|
|
if(ext != NULL)
|
|
|
|
{
|
|
|
|
ext++;
|
|
|
|
int extlen = strlen(ext);
|
|
|
|
if(extlen > 5)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ext;
|
|
|
|
}
|
|
|
|
|
2009-07-07 08:35:53 +02:00
|
|
|
bool ParseDirEntries()
|
|
|
|
{
|
2011-01-20 03:52:56 +01:00
|
|
|
if(!dir)
|
2009-07-07 08:35:53 +02:00
|
|
|
return false;
|
|
|
|
|
2010-07-12 08:19:35 +02:00
|
|
|
char *ext;
|
2011-01-21 03:36:17 +01:00
|
|
|
char path[MAXPATHLEN+1];
|
2011-01-20 03:52:56 +01:00
|
|
|
struct dirent *entry;
|
2009-07-07 08:35:53 +02:00
|
|
|
struct stat filestat;
|
|
|
|
|
2010-07-12 08:19:35 +02:00
|
|
|
int i = 0;
|
2009-07-07 08:35:53 +02:00
|
|
|
|
2010-07-12 08:19:35 +02:00
|
|
|
while(i < 20)
|
2009-07-07 08:35:53 +02:00
|
|
|
{
|
2011-01-20 03:52:56 +01:00
|
|
|
entry = readdir(dir);
|
2009-07-07 08:35:53 +02:00
|
|
|
|
2011-01-20 03:52:56 +01:00
|
|
|
if(entry == NULL)
|
2009-07-07 08:35:53 +02:00
|
|
|
break;
|
|
|
|
|
2011-01-20 03:52:56 +01:00
|
|
|
if(entry->d_name[0] == '.' && entry->d_name[1] != '.')
|
2009-07-07 08:35:53 +02:00
|
|
|
continue;
|
2010-07-12 08:19:35 +02:00
|
|
|
|
2011-01-21 03:36:17 +01:00
|
|
|
if(strcmp(entry->d_name, "..") == 0)
|
2010-07-12 08:19:35 +02:00
|
|
|
{
|
2011-01-21 03:36:17 +01:00
|
|
|
filestat.st_mode = _IFDIR;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ext = GetExt(entry->d_name);
|
|
|
|
snprintf(path, MAXPATHLEN, "%s%s", browser.dir, entry->d_name);
|
2010-07-12 08:19:35 +02:00
|
|
|
|
2011-01-21 03:36:17 +01:00
|
|
|
if(stat(path, &filestat) < 0)
|
2010-07-12 08:19:35 +02:00
|
|
|
continue;
|
2011-01-21 03:36:17 +01:00
|
|
|
|
|
|
|
// don't show the file if it's not a valid ROM
|
|
|
|
if(parseFilter && (filestat.st_mode & _IFDIR) == 0)
|
|
|
|
{
|
|
|
|
if(ext == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( stricmp(ext, "smc") != 0 && stricmp(ext, "fig") != 0 &&
|
|
|
|
stricmp(ext, "sfc") != 0 && stricmp(ext, "swc") != 0 &&
|
|
|
|
stricmp(ext, "zip") != 0 && stricmp(ext, "7z") != 0)
|
|
|
|
continue;
|
|
|
|
}
|
2009-07-07 08:35:53 +02:00
|
|
|
}
|
2009-10-06 08:37:53 +02:00
|
|
|
|
|
|
|
if(!AddBrowserEntry())
|
2009-07-07 08:35:53 +02:00
|
|
|
{
|
2009-10-06 08:37:53 +02:00
|
|
|
parseHalt = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-01-20 03:52:56 +01:00
|
|
|
strncpy(browserList[browser.numEntries+i].filename, entry->d_name, MAXJOLIET);
|
2009-10-06 08:37:53 +02:00
|
|
|
browserList[browser.numEntries+i].length = filestat.st_size;
|
|
|
|
browserList[browser.numEntries+i].mtime = filestat.st_mtime;
|
|
|
|
browserList[browser.numEntries+i].isdir = (filestat.st_mode & _IFDIR) == 0 ? 0 : 1; // flag this as a dir
|
|
|
|
|
|
|
|
if(browserList[browser.numEntries+i].isdir)
|
|
|
|
{
|
2011-01-20 03:52:56 +01:00
|
|
|
if(strcmp(entry->d_name, "..") == 0)
|
2009-10-06 08:37:53 +02:00
|
|
|
sprintf(browserList[browser.numEntries+i].displayname, "Up One Level");
|
2009-07-07 08:35:53 +02:00
|
|
|
else
|
2009-10-06 08:37:53 +02:00
|
|
|
strncpy(browserList[browser.numEntries+i].displayname, browserList[browser.numEntries+i].filename, MAXJOLIET);
|
|
|
|
browserList[browser.numEntries+i].icon = ICON_FOLDER;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
StripExt(browserList[browser.numEntries+i].displayname, browserList[browser.numEntries+i].filename); // hide file extension
|
2009-07-07 08:35:53 +02:00
|
|
|
}
|
2010-07-12 08:19:35 +02:00
|
|
|
i++;
|
2009-07-07 08:35:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the file list
|
|
|
|
if(i >= 0)
|
2009-12-23 01:38:11 +01:00
|
|
|
qsort(browserList, browser.numEntries+i, sizeof(BROWSERENTRY), FileSortCallback);
|
2009-07-07 08:35:53 +02:00
|
|
|
|
2010-08-31 19:08:32 +02:00
|
|
|
browser.numEntries += i;
|
|
|
|
|
2011-01-20 03:52:56 +01:00
|
|
|
if(entry == NULL || parseHalt)
|
2009-12-23 01:38:11 +01:00
|
|
|
{
|
2011-01-20 03:52:56 +01:00
|
|
|
closedir(dir); // close directory
|
|
|
|
dir = NULL;
|
2009-12-23 01:38:11 +01:00
|
|
|
|
2010-08-31 19:08:32 +02:00
|
|
|
// try to find and select the last loaded file
|
|
|
|
if(selectLoadedFile == 1 && parseHalt == 0 && loadedFile[0] != 0 && browser.dir[0] != 0)
|
2009-12-23 01:38:11 +01:00
|
|
|
{
|
2010-08-31 19:08:32 +02:00
|
|
|
int indexFound = -1;
|
|
|
|
|
|
|
|
for(int j=1; j < browser.numEntries; j++)
|
|
|
|
{
|
|
|
|
if(strcmp(browserList[j].filename, loadedFile) == 0)
|
2009-12-23 01:38:11 +01:00
|
|
|
{
|
2010-08-31 19:08:32 +02:00
|
|
|
indexFound = j;
|
|
|
|
break;
|
2009-12-23 01:38:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-31 19:08:32 +02:00
|
|
|
// move to this file
|
|
|
|
if(indexFound > 0)
|
|
|
|
{
|
|
|
|
if(indexFound >= FILE_PAGESIZE)
|
|
|
|
{
|
|
|
|
int newIndex = (floor(indexFound/(float)FILE_PAGESIZE)) * FILE_PAGESIZE;
|
2009-12-23 01:38:11 +01:00
|
|
|
|
2010-08-31 19:08:32 +02:00
|
|
|
if(newIndex + FILE_PAGESIZE > browser.numEntries)
|
|
|
|
newIndex = browser.numEntries - FILE_PAGESIZE;
|
|
|
|
|
|
|
|
if(newIndex < 0)
|
|
|
|
newIndex = 0;
|
|
|
|
|
|
|
|
browser.pageIndex = newIndex;
|
|
|
|
}
|
|
|
|
browser.selIndex = indexFound;
|
|
|
|
}
|
|
|
|
selectLoadedFile = 2; // selecting done
|
|
|
|
}
|
|
|
|
|
2009-07-07 08:35:53 +02:00
|
|
|
return false; // no more entries
|
|
|
|
}
|
|
|
|
return true; // more entries
|
|
|
|
}
|
|
|
|
|
2008-08-06 03:55:59 +02:00
|
|
|
/***************************************************************************
|
2008-12-18 19:36:30 +01:00
|
|
|
* Browse subdirectories
|
2008-09-12 07:28:40 +02:00
|
|
|
**************************************************************************/
|
2008-08-10 05:14:39 +02:00
|
|
|
int
|
2010-07-15 08:55:40 +02:00
|
|
|
ParseDirectory(bool waitParse, bool filter)
|
2008-08-06 03:55:59 +02:00
|
|
|
{
|
2009-03-23 00:16:25 +01:00
|
|
|
int retry = 1;
|
2009-03-23 06:53:21 +01:00
|
|
|
bool mounted = false;
|
2010-07-15 08:55:40 +02:00
|
|
|
parseFilter = filter;
|
2009-10-02 00:35:12 +02:00
|
|
|
|
|
|
|
ResetBrowser(); // reset browser
|
2009-10-06 08:37:53 +02:00
|
|
|
|
2008-08-10 05:14:39 +02:00
|
|
|
// open the directory
|
2011-01-20 03:52:56 +01:00
|
|
|
while(dir == NULL && retry == 1)
|
2009-03-23 00:16:25 +01:00
|
|
|
{
|
2009-10-02 00:35:12 +02:00
|
|
|
mounted = ChangeInterface(browser.dir, NOTSILENT);
|
2009-10-03 19:02:45 +02:00
|
|
|
|
2009-10-02 00:35:12 +02:00
|
|
|
if(mounted)
|
2011-01-20 03:52:56 +01:00
|
|
|
dir = opendir(browser.dir);
|
2009-10-02 00:35:12 +02:00
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
|
2011-01-20 03:52:56 +01:00
|
|
|
if(dir == NULL)
|
2009-03-23 00:16:25 +01:00
|
|
|
{
|
2010-03-18 00:20:08 +01:00
|
|
|
retry = ErrorPromptRetry("Error opening directory!");
|
2009-03-23 00:16:25 +01:00
|
|
|
}
|
|
|
|
}
|
2008-12-18 19:36:30 +01:00
|
|
|
|
2009-10-02 00:35:12 +02:00
|
|
|
// if we can't open the dir, try higher levels
|
2011-01-20 03:52:56 +01:00
|
|
|
if (dir == NULL)
|
2008-08-06 03:55:59 +02:00
|
|
|
{
|
2009-10-06 08:37:53 +02:00
|
|
|
char * devEnd = strrchr(browser.dir, '/');
|
|
|
|
|
2009-10-02 00:35:12 +02:00
|
|
|
while(!IsDeviceRoot(browser.dir))
|
2008-08-07 05:25:02 +02:00
|
|
|
{
|
2009-10-06 08:37:53 +02:00
|
|
|
devEnd[0] = 0; // strip slash
|
|
|
|
devEnd = strrchr(browser.dir, '/');
|
2009-10-02 05:09:36 +02:00
|
|
|
|
|
|
|
if(devEnd == NULL)
|
|
|
|
break;
|
|
|
|
|
2009-10-06 08:37:53 +02:00
|
|
|
devEnd[1] = 0; // strip remaining file listing
|
2011-01-20 03:52:56 +01:00
|
|
|
dir = opendir(browser.dir);
|
|
|
|
if (dir)
|
2009-10-02 00:35:12 +02:00
|
|
|
break;
|
2008-08-07 05:25:02 +02:00
|
|
|
}
|
|
|
|
}
|
2009-10-02 00:35:12 +02:00
|
|
|
|
2011-01-20 03:52:56 +01:00
|
|
|
if(dir == NULL)
|
2009-10-02 00:35:12 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if(IsDeviceRoot(browser.dir))
|
|
|
|
{
|
2010-06-25 01:38:05 +02:00
|
|
|
AddBrowserEntry();
|
2009-10-02 00:35:12 +02:00
|
|
|
sprintf(browserList[0].filename, "..");
|
|
|
|
sprintf(browserList[0].displayname, "Up One Level");
|
|
|
|
browserList[0].length = 0;
|
|
|
|
browserList[0].mtime = 0;
|
|
|
|
browserList[0].isdir = 1; // flag this as a dir
|
2009-10-03 20:44:09 +02:00
|
|
|
browserList[0].icon = ICON_FOLDER;
|
2010-06-25 01:38:05 +02:00
|
|
|
browser.numEntries++;
|
2009-10-02 00:35:12 +02:00
|
|
|
}
|
2008-08-06 03:09:59 +02:00
|
|
|
|
2009-07-07 08:35:53 +02:00
|
|
|
parseHalt = false;
|
|
|
|
ParseDirEntries(); // index first 20 entries
|
2010-04-02 23:02:16 +02:00
|
|
|
|
2009-07-07 08:35:53 +02:00
|
|
|
LWP_ResumeThread(parsethread); // index remaining entries
|
2009-03-11 18:28:37 +01:00
|
|
|
|
2009-07-16 19:47:37 +02:00
|
|
|
if(waitParse) // wait for complete parsing
|
|
|
|
{
|
2010-07-15 08:55:40 +02:00
|
|
|
ShowAction("Loading...");
|
|
|
|
|
2009-07-16 19:47:37 +02:00
|
|
|
while(!LWP_ThreadIsSuspended(parsethread))
|
|
|
|
usleep(THREAD_SLEEP);
|
|
|
|
|
|
|
|
CancelAction();
|
|
|
|
}
|
|
|
|
|
2009-07-07 08:35:53 +02:00
|
|
|
return browser.numEntries;
|
2008-12-30 01:08:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* AllocSaveBuffer ()
|
|
|
|
* Clear and allocate the savebuffer
|
|
|
|
***************************************************************************/
|
|
|
|
void
|
|
|
|
AllocSaveBuffer ()
|
|
|
|
{
|
2009-06-01 23:56:31 +02:00
|
|
|
if(bufferLock == LWP_MUTEX_NULL)
|
|
|
|
LWP_MutexInit(&bufferLock, false);
|
2008-12-30 01:08:17 +01:00
|
|
|
|
2009-06-01 23:56:31 +02:00
|
|
|
if(bufferLock != LWP_MUTEX_NULL)
|
|
|
|
LWP_MutexLock(bufferLock);
|
2008-12-30 01:08:17 +01:00
|
|
|
memset (savebuffer, 0, SAVEBUFFERSIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* FreeSaveBuffer ()
|
|
|
|
* Free the savebuffer memory
|
|
|
|
***************************************************************************/
|
|
|
|
void
|
|
|
|
FreeSaveBuffer ()
|
|
|
|
{
|
2009-06-01 23:56:31 +02:00
|
|
|
if(bufferLock != LWP_MUTEX_NULL)
|
|
|
|
LWP_MutexUnlock(bufferLock);
|
2008-08-06 03:09:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
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-09-12 07:28:40 +02:00
|
|
|
***************************************************************************/
|
2009-10-03 21:10:53 +02:00
|
|
|
size_t
|
2008-12-18 19:36:30 +01:00
|
|
|
LoadSzFile(char * filepath, unsigned char * rbuffer)
|
2008-08-06 03:09:59 +02:00
|
|
|
{
|
2009-10-03 21:10:53 +02:00
|
|
|
size_t size = 0;
|
2008-12-19 00:22:32 +01:00
|
|
|
|
|
|
|
// stop checking if devices were removed/inserted
|
|
|
|
// since we're loading a file
|
2009-05-06 19:28:06 +02:00
|
|
|
HaltDeviceThread();
|
2008-12-19 00:22:32 +01:00
|
|
|
|
2009-07-08 22:25:59 +02:00
|
|
|
// halt parsing
|
2009-10-06 08:37:53 +02:00
|
|
|
HaltParseThread();
|
2009-07-08 22:25:59 +02:00
|
|
|
|
2008-12-18 19:36:30 +01:00
|
|
|
file = fopen (filepath, "rb");
|
|
|
|
if (file > 0)
|
2008-08-10 05:14:39 +02:00
|
|
|
{
|
2009-10-03 21:46:57 +02:00
|
|
|
size = SzExtractFile(browserList[browser.selIndex].filenum, rbuffer);
|
2008-12-18 19:36:30 +01:00
|
|
|
fclose (file);
|
2008-10-14 11:21:34 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-03-18 00:20:08 +01:00
|
|
|
ErrorPrompt("Error opening file!");
|
2008-10-14 11:21:34 +02:00
|
|
|
}
|
2008-12-19 00:22:32 +01:00
|
|
|
|
|
|
|
// go back to checking if devices were inserted/removed
|
2009-05-06 19:28:06 +02:00
|
|
|
ResumeDeviceThread();
|
2008-12-19 00:22:32 +01:00
|
|
|
|
|
|
|
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
|
|
|
***************************************************************************/
|
2009-10-03 21:10:53 +02:00
|
|
|
size_t
|
|
|
|
LoadFile (char * rbuffer, char *filepath, size_t length, bool silent)
|
2008-10-14 11:21:34 +02:00
|
|
|
{
|
2008-11-12 08:50:39 +01:00
|
|
|
char zipbuffer[2048];
|
2009-10-06 08:37:53 +02:00
|
|
|
size_t size = 0, offset = 0, readsize = 0;
|
2009-03-23 00:16:25 +01:00
|
|
|
int retry = 1;
|
2009-10-02 00:35:12 +02:00
|
|
|
int device;
|
2009-10-06 08:37:53 +02:00
|
|
|
|
2009-10-02 00:35:12 +02:00
|
|
|
if(!FindDevice(filepath, &device))
|
|
|
|
return 0;
|
2008-12-18 19:36:30 +01:00
|
|
|
|
2008-12-19 00:22:32 +01:00
|
|
|
// stop checking if devices were removed/inserted
|
|
|
|
// since we're loading a file
|
2009-05-06 19:28:06 +02:00
|
|
|
HaltDeviceThread();
|
2008-12-19 00:22:32 +01:00
|
|
|
|
2009-07-08 22:25:59 +02:00
|
|
|
// halt parsing
|
2009-10-06 08:37:53 +02:00
|
|
|
HaltParseThread();
|
2009-07-08 22:25:59 +02:00
|
|
|
|
2009-11-29 09:12:40 +01:00
|
|
|
// open the file
|
2010-08-18 02:15:25 +02:00
|
|
|
while(retry)
|
2009-10-06 08:37:53 +02:00
|
|
|
{
|
2009-11-29 09:12:40 +01:00
|
|
|
if(!ChangeInterface(device, silent))
|
|
|
|
break;
|
|
|
|
|
|
|
|
file = fopen (filepath, "rb");
|
|
|
|
|
|
|
|
if(!file)
|
2008-11-12 08:50:39 +01:00
|
|
|
{
|
2009-11-29 09:12:40 +01:00
|
|
|
if(silent)
|
2009-10-06 08:37:53 +02:00
|
|
|
break;
|
|
|
|
|
2009-11-29 09:12:40 +01:00
|
|
|
retry = ErrorPromptRetry("Error opening file!");
|
|
|
|
continue;
|
|
|
|
}
|
2008-11-12 08:50:39 +01:00
|
|
|
|
2009-11-29 09:12:40 +01:00
|
|
|
if(length > 0 && length <= 2048) // do a partial read (eg: to check file header)
|
|
|
|
{
|
|
|
|
size = fread (rbuffer, 1, length, file);
|
|
|
|
}
|
|
|
|
else // load whole file
|
|
|
|
{
|
|
|
|
readsize = fread (zipbuffer, 1, 32, file);
|
2009-10-06 08:37:53 +02:00
|
|
|
|
2009-11-29 09:12:40 +01:00
|
|
|
if(!readsize)
|
|
|
|
{
|
|
|
|
unmountRequired[device] = true;
|
|
|
|
retry = ErrorPromptRetry("Error reading file!");
|
2010-08-18 02:15:25 +02:00
|
|
|
fclose (file);
|
2009-10-06 08:37:53 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-11-29 09:12:40 +01:00
|
|
|
if (IsZipFile (zipbuffer))
|
2009-10-06 08:37:53 +02:00
|
|
|
{
|
2009-11-29 09:12:40 +01:00
|
|
|
size = UnZipBuffer ((unsigned char *)rbuffer); // unzip
|
2009-10-06 08:37:53 +02:00
|
|
|
}
|
2009-11-29 09:12:40 +01:00
|
|
|
else
|
2008-11-12 08:50:39 +01:00
|
|
|
{
|
2009-11-29 09:12:40 +01:00
|
|
|
fseeko(file,0,SEEK_END);
|
|
|
|
size = ftello(file);
|
|
|
|
fseeko(file,0,SEEK_SET);
|
2009-10-06 08:37:53 +02:00
|
|
|
|
2009-11-29 09:12:40 +01:00
|
|
|
while(!feof(file))
|
2008-11-12 08:50:39 +01:00
|
|
|
{
|
2009-11-29 09:12:40 +01:00
|
|
|
ShowProgress ("Loading...", offset, size);
|
|
|
|
readsize = fread (rbuffer + offset, 1, 4096, file); // read in next chunk
|
2009-10-06 08:37:53 +02:00
|
|
|
|
2009-11-29 09:12:40 +01:00
|
|
|
if(readsize <= 0)
|
|
|
|
break; // reading finished (or failed)
|
|
|
|
|
|
|
|
offset += readsize;
|
2008-11-12 08:50:39 +01:00
|
|
|
}
|
2009-11-29 09:12:40 +01:00
|
|
|
size = offset;
|
|
|
|
CancelAction();
|
2008-11-12 08:50:39 +01:00
|
|
|
}
|
|
|
|
}
|
2010-08-18 02:15:25 +02:00
|
|
|
retry = 0;
|
2009-11-29 09:12:40 +01:00
|
|
|
fclose (file);
|
2008-08-10 05:14:39 +02:00
|
|
|
}
|
2008-12-18 19:36:30 +01:00
|
|
|
|
2008-12-19 00:22:32 +01:00
|
|
|
// go back to checking if devices were inserted/removed
|
2009-05-06 19:28:06 +02:00
|
|
|
ResumeDeviceThread();
|
2009-03-11 18:28:37 +01:00
|
|
|
CancelAction();
|
2008-12-18 19:36:30 +01:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2009-10-03 21:10:53 +02:00
|
|
|
size_t LoadFile(char * filepath, bool silent)
|
2008-12-18 19:36:30 +01:00
|
|
|
{
|
2009-10-02 00:35:12 +02:00
|
|
|
return LoadFile((char *)savebuffer, filepath, 0, silent);
|
2008-08-06 03:09:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2008-12-18 19:36:30 +01:00
|
|
|
* SaveFile
|
|
|
|
* Write buffer to file
|
2008-09-12 07:28:40 +02:00
|
|
|
***************************************************************************/
|
2009-10-03 21:10:53 +02:00
|
|
|
size_t
|
|
|
|
SaveFile (char * buffer, char *filepath, size_t datasize, bool silent)
|
2008-08-06 03:09:59 +02:00
|
|
|
{
|
2009-10-03 21:10:53 +02:00
|
|
|
size_t written = 0;
|
2009-10-06 08:37:53 +02:00
|
|
|
size_t writesize, nextwrite;
|
2009-03-23 00:16:25 +01:00
|
|
|
int retry = 1;
|
2009-10-02 00:35:12 +02:00
|
|
|
int device;
|
|
|
|
|
|
|
|
if(!FindDevice(filepath, &device))
|
|
|
|
return 0;
|
2008-12-18 19:36:30 +01:00
|
|
|
|
2009-03-23 00:16:25 +01:00
|
|
|
if(datasize == 0)
|
2008-12-18 19:36:30 +01:00
|
|
|
return 0;
|
|
|
|
|
2009-10-06 08:37:53 +02:00
|
|
|
// stop checking if devices were removed/inserted
|
2009-10-06 09:10:40 +02:00
|
|
|
// since we're saving a file
|
2009-10-06 08:37:53 +02:00
|
|
|
HaltDeviceThread();
|
|
|
|
|
|
|
|
// halt parsing
|
|
|
|
HaltParseThread();
|
|
|
|
|
2009-03-11 18:28:37 +01:00
|
|
|
ShowAction("Saving...");
|
|
|
|
|
2009-11-29 09:12:40 +01:00
|
|
|
while(!written && retry == 1)
|
2009-10-06 08:37:53 +02:00
|
|
|
{
|
2009-11-29 09:12:40 +01:00
|
|
|
if(!ChangeInterface(device, silent))
|
|
|
|
break;
|
2009-10-06 09:09:51 +02:00
|
|
|
|
2009-11-29 09:12:40 +01:00
|
|
|
file = fopen (filepath, "wb");
|
2009-10-06 09:09:51 +02:00
|
|
|
|
2009-11-29 09:12:40 +01:00
|
|
|
if(!file)
|
|
|
|
{
|
|
|
|
if(silent)
|
|
|
|
break;
|
2009-10-06 08:37:53 +02:00
|
|
|
|
2009-11-29 09:12:40 +01:00
|
|
|
retry = ErrorPromptRetry("Error creating file!");
|
|
|
|
continue;
|
|
|
|
}
|
2009-05-06 00:32:58 +02:00
|
|
|
|
2009-11-29 09:12:40 +01:00
|
|
|
while(written < datasize)
|
|
|
|
{
|
|
|
|
if(datasize - written > 4096) nextwrite=4096;
|
|
|
|
else nextwrite = datasize-written;
|
|
|
|
writesize = fwrite (buffer+written, 1, nextwrite, file);
|
|
|
|
if(writesize != nextwrite) break; // write failure
|
|
|
|
written += writesize;
|
|
|
|
}
|
|
|
|
fclose (file);
|
2009-10-06 09:09:51 +02:00
|
|
|
|
2009-11-29 09:12:40 +01:00
|
|
|
if(written != datasize) written = 0;
|
2009-10-06 09:09:51 +02:00
|
|
|
|
2009-11-29 09:12:40 +01:00
|
|
|
if(!written)
|
|
|
|
{
|
|
|
|
unmountRequired[device] = true;
|
2010-04-13 07:17:30 +02:00
|
|
|
if(silent) break;
|
2009-11-29 09:12:40 +01:00
|
|
|
retry = ErrorPromptRetry("Error saving file!");
|
2009-03-23 00:16:25 +01:00
|
|
|
}
|
|
|
|
}
|
2008-12-19 00:22:32 +01:00
|
|
|
|
2009-03-23 00:16:25 +01:00
|
|
|
// go back to checking if devices were inserted/removed
|
2009-05-06 19:28:06 +02:00
|
|
|
ResumeDeviceThread();
|
2009-03-16 07:58:50 +01:00
|
|
|
|
2009-03-11 18:28:37 +01:00
|
|
|
CancelAction();
|
2009-06-12 09:47:42 +02:00
|
|
|
return written;
|
2008-12-18 19:36:30 +01:00
|
|
|
}
|
|
|
|
|
2009-10-03 21:10:53 +02:00
|
|
|
size_t SaveFile(char * filepath, size_t datasize, bool silent)
|
2008-12-18 19:36:30 +01:00
|
|
|
{
|
2009-10-02 00:35:12 +02:00
|
|
|
return SaveFile((char *)savebuffer, filepath, datasize, silent);
|
2008-08-06 03:09:59 +02:00
|
|
|
}
|