2008-12-24 09:02:04 +01:00
|
|
|
/****************************************************************************
|
2010-01-27 23:20:37 +01:00
|
|
|
* Snes9x Nintendo Wii/Gamecube Port
|
2008-12-24 09:02:04 +01:00
|
|
|
*
|
2010-01-27 23:20:37 +01:00
|
|
|
* Tantric 2008-2010
|
2008-12-24 09:02:04 +01:00
|
|
|
*
|
|
|
|
* networkop.cpp
|
|
|
|
*
|
|
|
|
* Network and SMB support routines
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <network.h>
|
2010-08-18 02:15:25 +02:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <ogc/lwp_watchdog.h>
|
2008-12-24 09:02:04 +01:00
|
|
|
#include <smb.h>
|
|
|
|
#include <mxml.h>
|
|
|
|
|
2010-03-22 00:43:54 +01:00
|
|
|
#include "snes9xgx.h"
|
2009-03-11 18:28:37 +01:00
|
|
|
#include "menu.h"
|
2008-12-24 09:02:04 +01:00
|
|
|
#include "fileop.h"
|
2009-06-12 09:47:42 +02:00
|
|
|
#include "filebrowser.h"
|
2010-03-22 00:43:54 +01:00
|
|
|
#include "utils/http.h"
|
|
|
|
#include "utils/unzip/unzip.h"
|
|
|
|
#include "utils/unzip/miniunz.h"
|
2008-12-24 09:02:04 +01:00
|
|
|
|
2010-08-18 02:15:25 +02:00
|
|
|
static int netHalt = 0;
|
2008-12-30 01:08:17 +01:00
|
|
|
static bool networkInit = false;
|
|
|
|
static bool networkShareInit = false;
|
2010-08-18 02:15:25 +02:00
|
|
|
char wiiIP[16] = { 0 };
|
2008-12-30 01:08:17 +01:00
|
|
|
static bool updateChecked = false; // true if checked for app update
|
2008-12-24 09:02:04 +01:00
|
|
|
static char updateURL[128]; // URL of app update
|
|
|
|
bool updateFound = false; // true if an app update was found
|
|
|
|
|
2011-02-03 04:09:49 +01:00
|
|
|
#ifdef HW_RVL
|
|
|
|
|
2008-12-24 09:02:04 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* UpdateCheck
|
|
|
|
* Checks for an update for the application
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
void UpdateCheck()
|
|
|
|
{
|
2010-07-27 19:29:37 +02:00
|
|
|
// we only check for an update if we have internet + SD/USB
|
|
|
|
if(updateChecked || !networkInit)
|
|
|
|
return;
|
2008-12-24 09:02:04 +01:00
|
|
|
|
2010-07-27 19:29:37 +02:00
|
|
|
if(!isMounted[DEVICE_SD] && !isMounted[DEVICE_USB])
|
|
|
|
return;
|
|
|
|
|
|
|
|
updateChecked = true;
|
|
|
|
u8 tmpbuffer[256];
|
|
|
|
|
|
|
|
if (http_request("http://snes9x-gx.googlecode.com/svn/trunk/update.xml", NULL, tmpbuffer, 256, SILENT) <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mxml_node_t *xml;
|
|
|
|
mxml_node_t *item;
|
2008-12-24 09:02:04 +01:00
|
|
|
|
2010-07-27 19:29:37 +02:00
|
|
|
xml = mxmlLoadString(NULL, (char *)tmpbuffer, MXML_TEXT_CALLBACK);
|
2008-12-24 09:02:04 +01:00
|
|
|
|
2010-07-27 19:29:37 +02:00
|
|
|
if(!xml)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// check settings version
|
|
|
|
item = mxmlFindElement(xml, xml, "app", "version", NULL, MXML_DESCEND);
|
|
|
|
if(item) // a version entry exists
|
|
|
|
{
|
|
|
|
const char * version = mxmlElementGetAttr(item, "version");
|
|
|
|
|
|
|
|
if(version && strlen(version) == 5)
|
|
|
|
{
|
|
|
|
int verMajor = version[0] - '0';
|
|
|
|
int verMinor = version[2] - '0';
|
|
|
|
int verPoint = version[4] - '0';
|
|
|
|
int curMajor = APPVERSION[0] - '0';
|
|
|
|
int curMinor = APPVERSION[2] - '0';
|
|
|
|
int curPoint = APPVERSION[4] - '0';
|
|
|
|
|
|
|
|
// check that the versioning is valid and is a newer version
|
|
|
|
if((verMajor >= 0 && verMajor <= 9 &&
|
|
|
|
verMinor >= 0 && verMinor <= 9 &&
|
|
|
|
verPoint >= 0 && verPoint <= 9) &&
|
|
|
|
(verMajor > curMajor ||
|
|
|
|
(verMajor == curMajor && verMinor > curMinor) ||
|
|
|
|
(verMajor == curMajor && verMinor == curMinor && verPoint > curPoint)))
|
2008-12-24 09:02:04 +01:00
|
|
|
{
|
2010-07-27 19:29:37 +02:00
|
|
|
item = mxmlFindElement(xml, xml, "file", NULL, NULL, MXML_DESCEND);
|
|
|
|
if(item)
|
2008-12-24 09:02:04 +01:00
|
|
|
{
|
2010-07-27 19:29:37 +02:00
|
|
|
const char * tmp = mxmlElementGetAttr(item, "url");
|
|
|
|
if(tmp)
|
2008-12-24 09:02:04 +01:00
|
|
|
{
|
2010-07-27 19:29:37 +02:00
|
|
|
snprintf(updateURL, 128, "%s", tmp);
|
|
|
|
updateFound = true;
|
2009-03-28 20:03:35 +01:00
|
|
|
}
|
2008-12-24 09:02:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-07-27 19:29:37 +02:00
|
|
|
mxmlDelete(xml);
|
2008-12-24 09:02:04 +01:00
|
|
|
}
|
|
|
|
|
2008-12-30 01:08:17 +01:00
|
|
|
static bool unzipArchive(char * zipfilepath, char * unzipfolderpath)
|
2008-12-24 09:02:04 +01:00
|
|
|
{
|
|
|
|
unzFile uf = unzOpen(zipfilepath);
|
|
|
|
if (uf==NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(chdir(unzipfolderpath)) // can't access dir
|
|
|
|
{
|
|
|
|
makedir(unzipfolderpath); // attempt to make dir
|
|
|
|
if(chdir(unzipfolderpath)) // still can't access dir
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
extractZip(uf,0,1,0);
|
|
|
|
|
|
|
|
unzCloseCurrentFile(uf);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DownloadUpdate()
|
|
|
|
{
|
|
|
|
bool result = false;
|
|
|
|
|
2010-06-23 19:34:19 +02:00
|
|
|
if(updateURL[0] == 0 || appPath[0] == 0 || !ChangeInterface(appPath, NOTSILENT))
|
|
|
|
{
|
|
|
|
ErrorPrompt("Update failed!");
|
|
|
|
updateFound = false; // updating is finished (successful or not!)
|
|
|
|
return false;
|
|
|
|
}
|
2009-01-25 08:09:37 +01:00
|
|
|
|
2009-12-23 01:38:11 +01:00
|
|
|
// stop checking if devices were removed/inserted
|
|
|
|
// since we're saving a file
|
|
|
|
HaltDeviceThread();
|
2008-12-24 09:02:04 +01:00
|
|
|
|
2009-12-23 01:38:11 +01:00
|
|
|
int device;
|
|
|
|
FindDevice(appPath, &device);
|
2008-12-24 09:02:04 +01:00
|
|
|
|
2009-12-23 01:38:11 +01:00
|
|
|
char updateFile[50];
|
|
|
|
sprintf(updateFile, "%s%s Update.zip", pathPrefix[device], APPNAME);
|
2008-12-24 09:02:04 +01:00
|
|
|
|
2010-06-23 19:34:19 +02:00
|
|
|
FILE * hfile = fopen (updateFile, "wb");
|
|
|
|
|
|
|
|
if (hfile)
|
2009-12-23 01:38:11 +01:00
|
|
|
{
|
2010-06-23 19:34:19 +02:00
|
|
|
if(http_request(updateURL, hfile, NULL, (1024*1024*10), NOTSILENT) > 0)
|
2010-07-22 09:21:40 +02:00
|
|
|
{
|
|
|
|
fclose (hfile);
|
2010-06-23 19:34:19 +02:00
|
|
|
result = unzipArchive(updateFile, (char *)pathPrefix[device]);
|
2010-07-22 09:21:40 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fclose (hfile);
|
|
|
|
}
|
2010-06-23 19:34:19 +02:00
|
|
|
remove(updateFile); // delete update file
|
2008-12-24 09:02:04 +01:00
|
|
|
}
|
2009-12-23 01:38:11 +01:00
|
|
|
|
|
|
|
// go back to checking if devices were inserted/removed
|
|
|
|
ResumeDeviceThread();
|
2010-06-23 19:34:19 +02:00
|
|
|
|
2009-12-23 01:38:11 +01:00
|
|
|
if(result)
|
|
|
|
InfoPrompt("Update successful!");
|
|
|
|
else
|
|
|
|
ErrorPrompt("Update failed!");
|
|
|
|
|
|
|
|
updateFound = false; // updating is finished (successful or not!)
|
2008-12-24 09:02:04 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* InitializeNetwork
|
|
|
|
* Initializes the Wii/GameCube network interface
|
|
|
|
***************************************************************************/
|
|
|
|
|
2010-08-18 02:15:25 +02:00
|
|
|
static lwp_t networkthread = LWP_THREAD_NULL;
|
|
|
|
static u8 netstack[8192] ATTRIBUTE_ALIGN (32);
|
|
|
|
|
|
|
|
static void * netcb (void *arg)
|
|
|
|
{
|
2010-08-21 00:32:41 +02:00
|
|
|
s32 res;
|
2010-08-18 02:15:25 +02:00
|
|
|
int retry;
|
2010-08-21 00:32:41 +02:00
|
|
|
int wait;
|
2010-08-18 02:15:25 +02:00
|
|
|
|
|
|
|
while(netHalt != 2)
|
|
|
|
{
|
|
|
|
retry = 30;
|
|
|
|
|
|
|
|
while (retry)
|
|
|
|
{
|
2010-08-21 00:32:41 +02:00
|
|
|
net_deinit();
|
|
|
|
res = net_init_async(NULL, NULL);
|
|
|
|
|
|
|
|
if(res != 0)
|
2010-08-18 02:15:25 +02:00
|
|
|
break; // failed
|
|
|
|
|
|
|
|
res = net_get_status();
|
2010-08-21 00:32:41 +02:00
|
|
|
wait = 500; // only wait 10 sec
|
|
|
|
while (res == -EBUSY && wait > 0)
|
2010-08-18 02:15:25 +02:00
|
|
|
{
|
|
|
|
usleep(20000);
|
|
|
|
res = net_get_status();
|
2010-08-21 00:32:41 +02:00
|
|
|
wait--;
|
2010-08-18 02:15:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (res != -EAGAIN && res != -ETIMEDOUT)
|
|
|
|
break;
|
|
|
|
|
|
|
|
retry--;
|
|
|
|
usleep(2000);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-08-21 00:32:41 +02:00
|
|
|
if (res == 0)
|
2010-08-18 02:15:25 +02:00
|
|
|
{
|
|
|
|
networkInit = true;
|
|
|
|
|
|
|
|
struct in_addr hostip;
|
|
|
|
hostip.s_addr = net_gethostip();
|
|
|
|
if (hostip.s_addr)
|
2010-08-23 20:59:21 +02:00
|
|
|
{
|
2010-08-18 02:15:25 +02:00
|
|
|
strcpy(wiiIP, inet_ntoa(hostip));
|
2010-08-23 20:59:21 +02:00
|
|
|
networkInit = true;
|
|
|
|
}
|
2010-08-18 02:15:25 +02:00
|
|
|
}
|
|
|
|
LWP_SuspendThread(networkthread);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* StartNetworkThread
|
|
|
|
*
|
|
|
|
* Signals the network thread to resume, or creates a new thread
|
|
|
|
***************************************************************************/
|
|
|
|
void StartNetworkThread()
|
|
|
|
{
|
|
|
|
netHalt = 0;
|
|
|
|
|
|
|
|
if(networkthread == LWP_THREAD_NULL)
|
|
|
|
LWP_CreateThread(&networkthread, netcb, NULL, netstack, 8192, 40);
|
|
|
|
else
|
|
|
|
LWP_ResumeThread(networkthread);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* StopNetworkThread
|
|
|
|
*
|
|
|
|
* Signals the network thread to stop
|
|
|
|
***************************************************************************/
|
|
|
|
void StopNetworkThread()
|
2008-12-24 09:02:04 +01:00
|
|
|
{
|
2010-08-18 02:15:25 +02:00
|
|
|
if(networkthread == LWP_THREAD_NULL)
|
2008-12-24 09:02:04 +01:00
|
|
|
return;
|
|
|
|
|
2010-08-18 02:15:25 +02:00
|
|
|
netHalt = 2;
|
2009-10-06 08:37:53 +02:00
|
|
|
|
2010-08-18 02:15:25 +02:00
|
|
|
if(LWP_ThreadIsSuspended(networkthread))
|
|
|
|
LWP_ResumeThread(networkthread);
|
2008-12-24 09:02:04 +01:00
|
|
|
|
2010-08-18 02:15:25 +02:00
|
|
|
// wait for thread to finish
|
|
|
|
LWP_JoinThread(networkthread, NULL);
|
|
|
|
networkthread = LWP_THREAD_NULL;
|
|
|
|
}
|
2009-03-11 18:28:37 +01:00
|
|
|
|
2011-02-03 04:09:49 +01:00
|
|
|
#endif
|
|
|
|
|
2010-08-18 02:15:25 +02:00
|
|
|
bool InitializeNetwork(bool silent)
|
|
|
|
{
|
2011-02-03 04:09:49 +01:00
|
|
|
#ifdef HW_RVL
|
2011-02-03 04:35:09 +01:00
|
|
|
StopNetworkThread();
|
|
|
|
|
|
|
|
if(networkInit && net_gethostip() > 0)
|
2010-08-18 02:15:25 +02:00
|
|
|
return true;
|
2008-12-24 09:02:04 +01:00
|
|
|
|
2011-02-03 04:35:09 +01:00
|
|
|
networkInit = false;
|
|
|
|
#else
|
|
|
|
if(networkInit)
|
|
|
|
return true;
|
|
|
|
#endif
|
2010-08-18 02:15:25 +02:00
|
|
|
|
|
|
|
int retry = 1;
|
|
|
|
|
|
|
|
while(retry)
|
|
|
|
{
|
|
|
|
u64 start = gettime();
|
2009-10-06 08:37:53 +02:00
|
|
|
|
2010-08-18 02:15:25 +02:00
|
|
|
ShowAction("Initializing network...");
|
2011-02-03 04:09:49 +01:00
|
|
|
|
|
|
|
#ifdef HW_RVL
|
2010-08-18 02:15:25 +02:00
|
|
|
StartNetworkThread();
|
2009-10-06 08:37:53 +02:00
|
|
|
|
2010-08-18 02:15:25 +02:00
|
|
|
while (!LWP_ThreadIsSuspended(networkthread))
|
|
|
|
{
|
|
|
|
usleep(50 * 1000);
|
2009-10-06 08:37:53 +02:00
|
|
|
|
2010-08-18 02:15:25 +02:00
|
|
|
if(diff_sec(start, gettime()) > 10) // wait for 10 seconds max for net init
|
2009-10-06 08:37:53 +02:00
|
|
|
break;
|
2008-12-24 09:02:04 +01:00
|
|
|
}
|
2011-02-03 04:09:49 +01:00
|
|
|
#else
|
|
|
|
networkInit = !(if_config(wiiIP, NULL, NULL, true) < 0);
|
|
|
|
#endif
|
2009-10-06 08:37:53 +02:00
|
|
|
|
2009-05-30 20:26:45 +02:00
|
|
|
CancelAction();
|
2010-08-18 02:15:25 +02:00
|
|
|
|
2011-02-03 04:35:09 +01:00
|
|
|
if(networkInit || silent)
|
2010-08-18 02:15:25 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
retry = ErrorPromptRetry("Unable to initialize network!");
|
|
|
|
}
|
|
|
|
return networkInit;
|
2008-12-24 09:02:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CloseShare()
|
|
|
|
{
|
|
|
|
if(networkShareInit)
|
2009-03-12 08:07:52 +01:00
|
|
|
smbClose("smb");
|
2008-12-24 09:02:04 +01:00
|
|
|
networkShareInit = false;
|
2010-03-15 08:41:40 +01:00
|
|
|
isMounted[DEVICE_SMB] = false;
|
2008-12-24 09:02:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Mount SMB Share
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
bool
|
|
|
|
ConnectShare (bool silent)
|
|
|
|
{
|
2010-08-18 02:15:25 +02:00
|
|
|
if(!InitializeNetwork(silent))
|
|
|
|
return false;
|
|
|
|
|
2010-06-19 19:21:01 +02:00
|
|
|
if(networkShareInit)
|
|
|
|
return true;
|
|
|
|
|
2009-10-06 08:37:53 +02:00
|
|
|
int retry = 1;
|
2008-12-24 09:02:04 +01:00
|
|
|
int chkS = (strlen(GCSettings.smbshare) > 0) ? 0:1;
|
|
|
|
int chkI = (strlen(GCSettings.smbip) > 0) ? 0:1;
|
|
|
|
|
|
|
|
// check that all parameters have been set
|
2009-10-04 21:30:55 +02:00
|
|
|
if(chkS + chkI > 0)
|
2008-12-24 09:02:04 +01:00
|
|
|
{
|
|
|
|
if(!silent)
|
|
|
|
{
|
|
|
|
char msg[50];
|
|
|
|
char msg2[100];
|
2009-10-04 21:30:55 +02:00
|
|
|
if(chkS + chkI > 1) // more than one thing is wrong
|
2008-12-24 09:02:04 +01:00
|
|
|
sprintf(msg, "Check settings.xml.");
|
|
|
|
else if(chkS)
|
|
|
|
sprintf(msg, "Share name is blank.");
|
|
|
|
else if(chkI)
|
|
|
|
sprintf(msg, "Share IP is blank.");
|
|
|
|
|
|
|
|
sprintf(msg2, "Invalid network settings - %s", msg);
|
2009-03-11 18:28:37 +01:00
|
|
|
ErrorPrompt(msg2);
|
2008-12-24 09:02:04 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-10-06 08:37:53 +02:00
|
|
|
while(retry)
|
|
|
|
{
|
|
|
|
if(!silent)
|
|
|
|
ShowAction ("Connecting to network share...");
|
|
|
|
|
|
|
|
if(smbInit(GCSettings.smbuser, GCSettings.smbpwd, GCSettings.smbshare, GCSettings.smbip))
|
|
|
|
networkShareInit = true;
|
2009-05-30 20:26:45 +02:00
|
|
|
|
2009-10-06 08:37:53 +02:00
|
|
|
if(networkShareInit || silent)
|
|
|
|
break;
|
2008-12-24 09:02:04 +01:00
|
|
|
|
2009-10-06 08:37:53 +02:00
|
|
|
retry = ErrorPromptRetry("Failed to connect to network share.");
|
2008-12-24 09:02:04 +01:00
|
|
|
}
|
|
|
|
|
2009-10-06 08:37:53 +02:00
|
|
|
if(!silent)
|
|
|
|
CancelAction();
|
|
|
|
|
2008-12-24 09:02:04 +01:00
|
|
|
return networkShareInit;
|
|
|
|
}
|