2008-08-10 05:14:39 +02:00
|
|
|
/****************************************************************************
|
2008-09-12 07:28:40 +02:00
|
|
|
* Snes9x 1.51 Nintendo Wii/Gamecube Port
|
2008-08-14 00:44:59 +02:00
|
|
|
*
|
2008-08-10 05:14:39 +02:00
|
|
|
* softdev July 2006
|
|
|
|
* crunchy2 May 2007
|
|
|
|
* Tantric August 2008
|
|
|
|
*
|
|
|
|
* smbload.cpp
|
|
|
|
*
|
|
|
|
* SMB support routines
|
|
|
|
****************************************************************************/
|
2008-09-12 07:28:40 +02:00
|
|
|
|
2008-08-10 05:14:39 +02:00
|
|
|
#include <gccore.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ogcsys.h>
|
|
|
|
#include <network.h>
|
|
|
|
#include <smb.h>
|
|
|
|
#include <zlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include "memmap.h"
|
2008-09-09 19:36:48 +02:00
|
|
|
|
2008-08-22 04:47:08 +02:00
|
|
|
#include "smbop.h"
|
2008-08-10 05:14:39 +02:00
|
|
|
#include "unzip.h"
|
|
|
|
#include "video.h"
|
|
|
|
#include "menudraw.h"
|
|
|
|
#include "filesel.h"
|
2008-10-03 07:26:01 +02:00
|
|
|
#include "snes9xGX.h"
|
2008-08-10 05:14:39 +02:00
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
bool networkInit = false;
|
|
|
|
bool networkShareInit = false;
|
|
|
|
unsigned int SMBTimer = 0;
|
|
|
|
#define SMBTIMEOUT ( 3600 ) // Some implementations timeout in 10 minutes
|
|
|
|
|
2008-08-10 05:14:39 +02:00
|
|
|
SMBCONN smbconn;
|
2008-08-12 05:25:16 +02:00
|
|
|
#define ZIPCHUNK 16384
|
2008-08-10 05:14:39 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* InitializeNetwork
|
|
|
|
* Initializes the Wii/GameCube network interface
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
bool InitializeNetwork(bool silent)
|
|
|
|
{
|
|
|
|
ShowAction ((char*) "Initializing network...");
|
|
|
|
s32 result;
|
|
|
|
|
|
|
|
while ((result = net_init()) == -EAGAIN);
|
|
|
|
|
|
|
|
if (result >= 0)
|
|
|
|
{
|
|
|
|
char myIP[16];
|
|
|
|
|
|
|
|
if (if_config(myIP, NULL, NULL, true) < 0)
|
|
|
|
{
|
2008-08-12 05:25:16 +02:00
|
|
|
if(!silent)
|
|
|
|
WaitPrompt((char*) "Error reading IP address.");
|
2008-08-10 05:14:39 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
if(!silent)
|
|
|
|
WaitPrompt((char*) "Unable to initialize network.");
|
2008-08-10 05:14:39 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Mount SMB Share
|
|
|
|
****************************************************************************/
|
2008-08-12 05:25:16 +02:00
|
|
|
|
2008-08-10 05:14:39 +02:00
|
|
|
bool
|
2008-08-12 05:25:16 +02:00
|
|
|
ConnectShare (bool silent)
|
2008-08-10 05:14:39 +02:00
|
|
|
{
|
2008-08-16 02:02:08 +02:00
|
|
|
// Crashes or stalls system in GameCube mode - so disable
|
|
|
|
#ifndef HW_RVL
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
|
2008-09-08 01:50:57 +02:00
|
|
|
// check that all parameter have been set
|
|
|
|
if(strlen(GCSettings.smbuser) == 0 ||
|
|
|
|
strlen(GCSettings.smbpwd) == 0 ||
|
|
|
|
strlen(GCSettings.smbshare) == 0 ||
|
|
|
|
strlen(GCSettings.smbip) == 0)
|
2008-09-10 07:57:37 +02:00
|
|
|
{
|
|
|
|
if(!silent)
|
|
|
|
WaitPrompt((char*) "Invalid network settings. Check SNES9xGX.xml.");
|
2008-09-08 01:50:57 +02:00
|
|
|
return false;
|
2008-09-10 07:57:37 +02:00
|
|
|
}
|
2008-09-08 01:50:57 +02:00
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
if(!networkInit)
|
|
|
|
networkInit = InitializeNetwork(silent);
|
2008-08-10 05:14:39 +02:00
|
|
|
|
|
|
|
if(networkInit)
|
|
|
|
{
|
2008-08-12 05:25:16 +02:00
|
|
|
// connection may have expired
|
|
|
|
if (networkShareInit && SMBTimer > SMBTIMEOUT)
|
|
|
|
{
|
|
|
|
networkShareInit = false;
|
|
|
|
SMBTimer = 0;
|
|
|
|
SMB_Close(smbconn);
|
|
|
|
}
|
2008-08-10 05:14:39 +02:00
|
|
|
|
|
|
|
if(!networkShareInit)
|
2008-08-12 05:25:16 +02:00
|
|
|
{
|
|
|
|
if(!silent)
|
|
|
|
ShowAction ((char*) "Connecting to network share...");
|
|
|
|
|
|
|
|
if(SMB_Connect(&smbconn, GCSettings.smbuser, GCSettings.smbpwd,
|
|
|
|
GCSettings.smbgcid, GCSettings.smbsvid, GCSettings.smbshare, GCSettings.smbip) == SMB_SUCCESS)
|
|
|
|
networkShareInit = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!networkShareInit && !silent)
|
2008-08-10 05:14:39 +02:00
|
|
|
WaitPrompt ((char*) "Failed to connect to network share.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return networkShareInit;
|
|
|
|
}
|
|
|
|
|
2008-08-10 10:09:22 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* SMBPath
|
|
|
|
*
|
|
|
|
* Returns a SMB-style path
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
char * SMBPath(char * path)
|
|
|
|
{
|
|
|
|
// fix path - replace all '/' with '\'
|
|
|
|
for(uint i=0; i < strlen(path); i++)
|
|
|
|
if(path[i] == '/')
|
|
|
|
path[i] = '\\';
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2008-08-10 05:14:39 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* parseSMBDirectory
|
|
|
|
*
|
|
|
|
* Load the directory and put in the filelist array
|
|
|
|
*****************************************************************************/
|
|
|
|
int
|
2008-08-12 05:25:16 +02:00
|
|
|
ParseSMBdirectory ()
|
2008-08-10 05:14:39 +02:00
|
|
|
{
|
2008-08-12 05:25:16 +02:00
|
|
|
if(!ConnectShare (NOTSILENT))
|
|
|
|
return 0;
|
|
|
|
|
2008-08-10 05:14:39 +02:00
|
|
|
int filecount = 0;
|
|
|
|
char searchpath[1024];
|
|
|
|
SMBDIRENTRY smbdir;
|
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
// initialize selection
|
|
|
|
selection = offset = 0;
|
|
|
|
|
2008-08-16 02:02:08 +02:00
|
|
|
// Clear any existing values
|
|
|
|
memset (&filelist, 0, sizeof (FILEENTRIES) * MAXFILES);
|
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
if(strlen(currentdir) <= 1) // root
|
2008-08-10 10:09:22 +02:00
|
|
|
sprintf(searchpath, "*");
|
|
|
|
else
|
|
|
|
sprintf(searchpath, "%s/*", currentdir);
|
2008-08-10 05:14:39 +02:00
|
|
|
|
|
|
|
if (SMB_FindFirst
|
2008-08-10 10:09:22 +02:00
|
|
|
(SMBPath(searchpath), SMB_SRCH_READONLY | SMB_SRCH_DIRECTORY, &smbdir, smbconn) != SMB_SUCCESS)
|
2008-08-10 05:14:39 +02:00
|
|
|
{
|
|
|
|
char msg[200];
|
|
|
|
sprintf(msg, "Could not open %s", currentdir);
|
|
|
|
WaitPrompt (msg);
|
|
|
|
|
|
|
|
// if we can't open the dir, open root dir
|
2008-08-10 10:09:22 +02:00
|
|
|
sprintf(searchpath, "/");
|
2008-08-10 05:14:39 +02:00
|
|
|
sprintf(searchpath,"*");
|
|
|
|
|
|
|
|
if (SMB_FindFirst
|
2008-08-10 10:09:22 +02:00
|
|
|
(SMBPath(searchpath), SMB_SRCH_READONLY | SMB_SRCH_DIRECTORY, &smbdir, smbconn) != SMB_SUCCESS)
|
2008-08-10 05:14:39 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// index files/folders
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if(strcmp(smbdir.name,".") != 0 &&
|
2008-08-12 05:25:16 +02:00
|
|
|
!(strlen(currentdir) <= 1 && strcmp(smbdir.name,"..") == 0))
|
2008-08-10 05:14:39 +02:00
|
|
|
{
|
|
|
|
memset (&filelist[filecount], 0, sizeof (FILEENTRIES));
|
|
|
|
filelist[filecount].length = smbdir.size_low;
|
|
|
|
smbdir.name[MAXJOLIET] = 0;
|
|
|
|
|
|
|
|
if(smbdir.attributes == SMB_SRCH_DIRECTORY)
|
|
|
|
filelist[filecount].flags = 1; // flag this as a dir
|
|
|
|
else
|
|
|
|
filelist[filecount].flags = 0;
|
|
|
|
|
|
|
|
// Update display name
|
|
|
|
memcpy (&filelist[filecount].displayname, smbdir.name, MAXDISPLAY);
|
|
|
|
filelist[filecount].displayname[MAXDISPLAY] = 0;
|
|
|
|
|
|
|
|
strcpy (filelist[filecount].filename, smbdir.name);
|
|
|
|
filecount++;
|
|
|
|
}
|
|
|
|
} while (SMB_FindNext (&smbdir, smbconn) == SMB_SUCCESS);
|
|
|
|
|
|
|
|
// close directory
|
|
|
|
SMB_FindClose (smbconn);
|
|
|
|
|
|
|
|
// Sort the file list
|
|
|
|
qsort(filelist, filecount, sizeof(FILEENTRIES), FileSortCallback);
|
|
|
|
|
|
|
|
return filecount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Load SMB file
|
2008-10-05 23:56:18 +02:00
|
|
|
* rom - pointer to memory where ROM will be stored
|
|
|
|
* length - # bytes to read (0 for all)
|
2008-08-10 05:14:39 +02:00
|
|
|
****************************************************************************/
|
|
|
|
int
|
2008-10-05 23:56:18 +02:00
|
|
|
LoadSMBFile (char * rom, int length)
|
2008-08-10 05:14:39 +02:00
|
|
|
{
|
2008-08-12 05:25:16 +02:00
|
|
|
char filepath[MAXPATHLEN];
|
2008-08-10 05:14:39 +02:00
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
/* Check filename length */
|
|
|
|
if ((strlen(currentdir)+1+strlen(filelist[selection].filename)) < MAXPATHLEN)
|
|
|
|
sprintf(filepath, "%s/%s",currentdir,filelist[selection].filename);
|
2008-08-10 05:14:39 +02:00
|
|
|
else
|
|
|
|
{
|
2008-08-12 05:25:16 +02:00
|
|
|
WaitPrompt((char*) "Maximum filepath length reached!");
|
|
|
|
return -1;
|
|
|
|
}
|
2008-10-05 23:56:18 +02:00
|
|
|
return LoadBufferFromSMB(rom, SMBPath(filepath), length, NOTSILENT);
|
2008-08-10 05:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Write savebuffer to SMB file
|
|
|
|
****************************************************************************/
|
2008-10-04 04:27:05 +02:00
|
|
|
// no buffer specified, use savebuffer
|
2008-08-10 05:14:39 +02:00
|
|
|
int
|
2008-09-27 00:28:57 +02:00
|
|
|
SaveBufferToSMB (char *filepath, int datasize, bool silent)
|
2008-10-04 04:27:05 +02:00
|
|
|
{
|
|
|
|
return SaveBufferToSMB((char *)savebuffer, filepath, datasize, silent);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SaveBufferToSMB (char * sbuffer, char *filepath, int datasize, bool silent)
|
2008-08-10 05:14:39 +02:00
|
|
|
{
|
2008-08-12 05:25:16 +02:00
|
|
|
if(!ConnectShare (NOTSILENT))
|
|
|
|
return 0;
|
|
|
|
|
2008-08-10 05:14:39 +02:00
|
|
|
SMBFILE smbfile;
|
|
|
|
int dsize = datasize;
|
|
|
|
int wrote = 0;
|
2008-08-12 05:25:16 +02:00
|
|
|
int boffset = 0;
|
2008-08-10 05:14:39 +02:00
|
|
|
|
|
|
|
smbfile =
|
2008-08-10 10:09:22 +02:00
|
|
|
SMB_OpenFile (SMBPath(filepath), SMB_OPEN_WRITING | SMB_DENY_NONE,
|
2008-08-10 05:14:39 +02:00
|
|
|
SMB_OF_CREATE | SMB_OF_TRUNCATE, smbconn);
|
|
|
|
|
|
|
|
if (smbfile)
|
|
|
|
{
|
|
|
|
while (dsize > 0)
|
|
|
|
{
|
|
|
|
if (dsize > 1024)
|
|
|
|
wrote =
|
2008-10-04 04:27:05 +02:00
|
|
|
SMB_WriteFile ((char *) sbuffer + boffset, 1024, boffset, smbfile);
|
2008-08-10 05:14:39 +02:00
|
|
|
else
|
|
|
|
wrote =
|
2008-10-04 04:27:05 +02:00
|
|
|
SMB_WriteFile ((char *) sbuffer + boffset, dsize, boffset, smbfile);
|
2008-08-10 05:14:39 +02:00
|
|
|
|
2008-08-12 05:25:16 +02:00
|
|
|
boffset += wrote;
|
2008-08-10 05:14:39 +02:00
|
|
|
dsize -= wrote;
|
|
|
|
}
|
|
|
|
SMB_CloseFile (smbfile);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char msg[100];
|
2008-08-10 10:09:22 +02:00
|
|
|
sprintf(msg, "Couldn't save SMB: %s", SMBPath(filepath));
|
2008-08-10 05:14:39 +02:00
|
|
|
WaitPrompt (msg);
|
|
|
|
}
|
|
|
|
|
2008-08-13 07:47:04 +02:00
|
|
|
return boffset;
|
2008-08-10 05:14:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2008-08-14 00:44:59 +02:00
|
|
|
* Load up a buffer from SMB file
|
2008-08-10 05:14:39 +02:00
|
|
|
****************************************************************************/
|
2008-08-14 00:44:59 +02:00
|
|
|
|
|
|
|
// no buffer is specified - so use savebuffer
|
2008-08-10 05:14:39 +02:00
|
|
|
int
|
2008-09-27 00:28:57 +02:00
|
|
|
LoadBufferFromSMB (char *filepath, bool silent)
|
2008-08-13 07:47:04 +02:00
|
|
|
{
|
2008-10-05 23:56:18 +02:00
|
|
|
return LoadBufferFromSMB((char *)savebuffer, filepath, 0, silent);
|
2008-08-13 07:47:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-10-05 23:56:18 +02:00
|
|
|
LoadBufferFromSMB (char * sbuffer, char *filepath, int length, bool silent)
|
2008-08-10 05:14:39 +02:00
|
|
|
{
|
2008-08-12 05:25:16 +02:00
|
|
|
if(!ConnectShare (NOTSILENT))
|
|
|
|
return 0;
|
|
|
|
|
2008-08-10 05:14:39 +02:00
|
|
|
SMBFILE smbfile;
|
|
|
|
int ret;
|
2008-08-13 07:47:04 +02:00
|
|
|
int boffset = 0;
|
2008-08-10 05:14:39 +02:00
|
|
|
|
|
|
|
smbfile =
|
2008-08-10 10:09:22 +02:00
|
|
|
SMB_OpenFile (SMBPath(filepath), SMB_OPEN_READING, SMB_OF_OPEN, smbconn);
|
2008-08-10 05:14:39 +02:00
|
|
|
|
|
|
|
if (!smbfile)
|
|
|
|
{
|
2008-08-12 05:25:16 +02:00
|
|
|
if(!silent)
|
2008-08-10 05:14:39 +02:00
|
|
|
{
|
|
|
|
char msg[100];
|
2008-08-10 10:09:22 +02:00
|
|
|
sprintf(msg, "Couldn't open SMB: %s", SMBPath(filepath));
|
2008-08-10 05:14:39 +02:00
|
|
|
WaitPrompt (msg);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-05 23:56:18 +02:00
|
|
|
if(length > 0) // do a partial read (eg: to check file header)
|
2008-08-22 04:47:08 +02:00
|
|
|
{
|
2008-10-05 23:56:18 +02:00
|
|
|
boffset = SMB_ReadFile (sbuffer, length, 0, smbfile);
|
2008-08-22 04:47:08 +02:00
|
|
|
}
|
2008-10-05 23:56:18 +02:00
|
|
|
else // load whole file
|
2008-08-22 04:47:08 +02:00
|
|
|
{
|
2008-10-05 23:56:18 +02:00
|
|
|
ret = SMB_ReadFile (sbuffer, 1024, boffset, smbfile);
|
|
|
|
|
|
|
|
if (IsZipFile (sbuffer))
|
|
|
|
{
|
|
|
|
boffset = UnZipFile ((unsigned char *)sbuffer, smbfile); // unzip from SMB
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Just load the file up
|
|
|
|
while ((ret = SMB_ReadFile (sbuffer + boffset, 1024, boffset, smbfile)) > 0)
|
|
|
|
boffset += ret;
|
|
|
|
}
|
2008-08-22 04:47:08 +02:00
|
|
|
}
|
2008-08-10 05:14:39 +02:00
|
|
|
SMB_CloseFile (smbfile);
|
|
|
|
|
2008-08-13 07:47:04 +02:00
|
|
|
return boffset;
|
2008-08-10 05:14:39 +02:00
|
|
|
}
|