fceugx/source/ngc/networkop.cpp

301 lines
6.9 KiB
C++
Raw Normal View History

2008-12-24 08:58:23 +01:00
/****************************************************************************
2009-07-22 04:05:49 +02:00
* FCE Ultra
2008-12-24 08:58:23 +01:00
* Nintendo Wii/Gamecube Port
*
* Tantric December 2008
*
* networkop.cpp
*
* Network and SMB support routines
****************************************************************************/
2009-03-31 09:13:20 +02:00
#ifdef HW_RVL
2008-12-24 08:58:23 +01:00
#include <network.h>
#include <smb.h>
#include <mxml.h>
2009-02-07 01:57:39 +01:00
#include <malloc.h>
2008-12-24 08:58:23 +01:00
#include "unzip.h"
#include "miniunz.h"
#include "fceugx.h"
2009-03-28 18:23:08 +01:00
#include "menu.h"
2008-12-24 08:58:23 +01:00
#include "fileop.h"
#include "http.h"
2009-06-12 09:48:27 +02:00
#include "filebrowser.h"
2008-12-24 08:58:23 +01:00
bool inNetworkInit = false;
static bool networkInit = false;
static bool autoNetworkInit = true;
static bool networkShareInit = false;
static bool updateChecked = false; // true if checked for app update
2008-12-24 08:58:23 +01:00
static char updateURL[128]; // URL of app update
bool updateFound = false; // true if an app update was found
/****************************************************************************
* UpdateCheck
* Checks for an update for the application
***************************************************************************/
void UpdateCheck()
{
// we can only check for the update if we have internet + SD
if(!updateChecked && networkInit && isMounted[METHOD_SD])
{
static char url[128];
int retval;
updateChecked = true;
snprintf(url, 128, "http://fceugc.googlecode.com/svn/trunk/update.xml");
2009-02-07 01:57:39 +01:00
u8 * tmpbuffer = (u8 *)memalign(32,32768);
memset(tmpbuffer, 0, 32768);
retval = http_request(url, NULL, tmpbuffer, 32768);
2008-12-24 08:58:23 +01:00
memset(url, 0, 128);
if (retval)
{
mxml_node_t *xml;
mxml_node_t *item;
xml = mxmlLoadString(NULL, (char *)tmpbuffer, MXML_TEXT_CALLBACK);
2008-12-24 08:58:23 +01:00
if(xml)
2008-12-24 08:58:23 +01:00
{
// check settings version
item = mxmlFindElement(xml, xml, "app", "version", NULL, MXML_DESCEND);
if(item) // a version entry exists
2008-12-24 08:58:23 +01:00
{
2009-03-31 09:13:20 +02:00
const char * version = mxmlElementGetAttr(item, "version");
if(version && strlen(version) == 5)
2008-12-24 08:58:23 +01:00
{
2009-03-31 09:13:20 +02:00
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 ||
2009-04-01 07:48:01 +02:00
(verMajor == curMajor && verMinor > curMinor) ||
(verMajor == curMajor && verMinor == curMinor && verPoint > curPoint)))
{
2009-03-31 09:13:20 +02:00
item = mxmlFindElement(xml, xml, "file", NULL, NULL, MXML_DESCEND);
if(item)
{
2009-03-31 09:13:20 +02:00
const char * tmp = mxmlElementGetAttr(item, "url");
if(tmp)
{
snprintf(updateURL, 128, "%s", tmp);
updateFound = true;
}
}
}
2008-12-24 08:58:23 +01:00
}
}
mxmlDelete(xml);
2008-12-24 08:58:23 +01:00
}
}
free(tmpbuffer);
2008-12-24 08:58:23 +01:00
}
}
static bool unzipArchive(char * zipfilepath, char * unzipfolderpath)
2008-12-24 08:58:23 +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;
if(strlen(updateURL) > 0)
{
// stop checking if devices were removed/inserted
// since we're saving a file
2009-05-06 19:27:20 +02:00
HaltDeviceThread();
2008-12-24 08:58:23 +01:00
FILE * hfile;
char updateFile[50];
sprintf(updateFile, "sd:/%s Update.zip", APPNAME);
hfile = fopen (updateFile, "wb");
if (hfile > 0)
{
int retval;
retval = http_request(updateURL, hfile, NULL, (1024*1024*5));
fclose (hfile);
}
2009-01-25 08:09:11 +01:00
2008-12-24 08:58:23 +01:00
bool unzipResult = unzipArchive(updateFile, (char *)"sd:/");
remove(updateFile); // delete update file
if(unzipResult)
{
result = true;
2009-03-28 18:23:08 +01:00
InfoPrompt("Update successful!");
2008-12-24 08:58:23 +01:00
}
else
{
result = false;
2009-03-31 09:13:20 +02:00
ErrorPrompt("Update failed!");
2008-12-24 08:58:23 +01:00
}
updateFound = false; // updating is finished (successful or not!)
// go back to checking if devices were inserted/removed
2009-05-06 19:27:20 +02:00
ResumeDeviceThread();
2008-12-24 08:58:23 +01:00
}
return result;
}
/****************************************************************************
* InitializeNetwork
* Initializes the Wii/GameCube network interface
***************************************************************************/
void InitializeNetwork(bool silent)
{
// stop if we're already initialized, or if auto-init has failed before
// in which case, manual initialization is required
if(networkInit || !autoNetworkInit)
return;
if(!silent)
ShowAction ("Initializing network...");
2009-03-31 09:13:20 +02:00
while(inNetworkInit) // a network init is already in progress!
usleep(50);
if(!networkInit) // check again if the network was inited
2008-12-24 08:58:23 +01:00
{
inNetworkInit = true;
2008-12-24 08:58:23 +01:00
char ip[16];
s32 initResult = if_config(ip, NULL, NULL, true);
if(initResult == 0)
{
networkInit = true;
}
else
2008-12-24 08:58:23 +01:00
{
// do not automatically attempt a reconnection
autoNetworkInit = false;
if(!silent)
{
char msg[150];
sprintf(msg, "Unable to initialize network (Error #: %i)", initResult);
ErrorPrompt(msg);
}
2008-12-24 08:58:23 +01:00
}
inNetworkInit = false;
2008-12-24 08:58:23 +01:00
}
if(!silent)
CancelAction();
2008-12-24 08:58:23 +01:00
}
void CloseShare()
{
if(networkShareInit)
2009-03-28 18:23:08 +01:00
smbClose("smb");
2008-12-24 08:58:23 +01:00
networkShareInit = false;
networkInit = false; // trigger a network reinit
2008-12-24 08:58:23 +01:00
}
/****************************************************************************
* Mount SMB Share
****************************************************************************/
bool
ConnectShare (bool silent)
{
// Crashes or stalls system in GameCube mode - so disable
#ifndef HW_RVL
return false;
#endif
int chkU = (strlen(GCSettings.smbuser) > 0) ? 0:1;
int chkP = (strlen(GCSettings.smbpwd) > 0) ? 0:1;
int chkS = (strlen(GCSettings.smbshare) > 0) ? 0:1;
int chkI = (strlen(GCSettings.smbip) > 0) ? 0:1;
// check that all parameters have been set
if(chkU + chkP + chkS + chkI > 0)
{
if(!silent)
{
char msg[50];
char msg2[100];
if(chkU + chkP + chkS + chkI > 1) // more than one thing is wrong
sprintf(msg, "Check settings.xml.");
else if(chkU)
sprintf(msg, "Username is blank.");
else if(chkP)
sprintf(msg, "Password is blank.");
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-28 18:23:08 +01:00
ErrorPrompt(msg2);
2008-12-24 08:58:23 +01:00
}
return false;
}
if(unmountRequired[METHOD_SMB])
CloseShare();
2008-12-24 08:58:23 +01:00
if(!networkInit)
InitializeNetwork(silent);
if(networkInit)
{
if(!networkShareInit)
{
if(!silent)
ShowAction ("Connecting to network share...");
if(smbInit(GCSettings.smbuser, GCSettings.smbpwd,
GCSettings.smbshare, GCSettings.smbip))
{
networkShareInit = true;
}
if(!silent)
CancelAction();
2008-12-24 08:58:23 +01:00
}
if(!networkShareInit && !silent)
2009-03-31 09:13:20 +02:00
ErrorPrompt("Failed to connect to network share.");
2008-12-24 08:58:23 +01:00
}
if(networkShareInit)
sprintf(rootdir, "smb:");
2008-12-24 08:58:23 +01:00
return networkShareInit;
}
2009-03-31 09:13:20 +02:00
#endif