int WiiSave::Download(std::string ID)
{
	if(ID.empty())
		return -1;
	
	//! Check Network
	if (!IsNetworkInit())
	{
		ManageProgressStop();
		
		ManageButtons * connect = new ManageButtons(tr("No network connection"),
													tr("Do you want to connect?"),
													tr("Yes"),
													tr("Cancel"));
		
		connect->SetAlignment(ALIGN_CENTRE, ALIGN_MIDDLE);
		connect->SetPosition(-6, 75);
		connect->SetEffect(EFFECT_FADE, 20);
		mainWindow->Append(connect);
		ResumeGui();
		
		while (connect->GetEffect()) usleep(50);
		
		while(!connect->GetChoice()) usleep(50);
		
		connect->SetEffect(EFFECT_FADE, -20);
		
		while (connect->GetEffect()) usleep(50);
		
		HaltGui();
		
		mainWindow->Remove(connect);
		
		ResumeGui();
		
		if(connect->GetChoice() == 1)
		{
			StartManageProgress(tr("Initializing Network"));
			
			Initialize_Network();
			
			ManageProgressStop();
		}
		
		delete connect;
		
		if (!IsNetworkInit())
			return -2;
		
		StartManageProgress(tr("Downloading saveslist:"), "www.wiisave.com");
	}

	//! Generate gameUrl
	std:: string WiiSaveListURL("http://wiisave.com/savegamecode_xml.php?gamecode=");
	WiiSaveListURL += ID.substr(0, 3);
	
	//! Check Connection
	if(!CheckConnection(WiiSaveListURL.c_str()))
	{
		ManageProgressStop();
		return -3;
	}
	
	//! Check Region
	std::string Region;
	switch (ID[3])
	{
		case 'E':
			Region = "E - USA / Canada";
			break;
		case 'J':
			Region = "J - Japanese";
			break;
		case 'W':
			Region = "J - Japanese";//Region = "NTSC T";
			break;
		case 'K':
			Region = "J - Japanese";//Region = "NTSC K";
			break;
		default:
		case 'P':
		case 'D':
		case 'F':
		case 'I':
		case 'S':
		case 'H':
		case 'U':
		case 'X':
		case 'Y':
		case 'Z':
			Region = "P - European / Other / PAL";
			break;
	}
	
	//! Get list
	WiiSave_List * SaveList = new WiiSave_List(WiiSaveListURL.c_str());

	ManageProgressStop();
	
	if (!SaveList->GetCount())
	{
		delete SaveList;
		return -4;
	}
	
	if (!SaveList->FilterList(Region))
	{
		delete SaveList;
		return -5;
	}
	
	//! open browser & select save
	WiiSaveBrowser * SaveBrowser = new WiiSaveBrowser(SaveList);
	mainWindow->Append(SaveBrowser);
	
	SaveBrowser->Show();
	
	int selected = SaveBrowser->GetSelectedSave();
	delete SaveBrowser;
	
	if (selected < 0)
	{
		delete SaveList;
		return -10;
	}
	
	//! select download destination
	ManageButtons * path = new ManageButtons(tr("Select a target path."),
											 tr("Be carrefull, present files can be overwritten"),
											 tr("OK"));
	
	path->SetAlignment(ALIGN_CENTRE, ALIGN_MIDDLE);
	path->SetPosition(-6, 75);
	path->SetEffect(EFFECT_FADE, 20);
	mainWindow->Append(path);
	ResumeGui();
	while (path->GetEffect()) usleep(50);
	
	while(!path->GetChoice()) usleep(50);
	
	path->SetEffect(EFFECT_FADE, -20);
	
	while (path->GetEffect()) usleep(50);
	
	HaltGui();
	
	mainWindow->Remove(path);
	delete path;
	
	ResumeGui();
	
	if( selectBrowser(Settings.BrowserPath.c_str(), PATH) != 2 )
	{
		delete SaveList;
		return -10;
	}
	
	//! download file
	std::string filepath = Settings.TmpPath;
	filepath += "/";
	filepath += SaveList->GetFilteredFilename(selected);
	
	if( WiiSave::InternalDownload(SaveList->GetFilteredDownloadLink(selected), filepath) < 0)
	{
		delete SaveList;
		return -6;
	}
	
	delete SaveList;
	
	//! check file type
	char destID[5];
	std::string srcfilepath;
	bool isSgmgxSave = false;
	bool isDataBin = false;
	
	std::string tmpDest = filepath.substr(0, filepath.rfind("/")+1);
	tmpDest += "tmpExtract";
	
	std::string fileext = filepath.substr(filepath.rfind("."));
	if( !Settings.FileExtensions.CompareArchive(fileext.c_str()) ) //! Archive
	{
		StartManageProgress(tr("Extracting files..."));
		
		//! extract files
		ArchiveHandle * Archive = new ArchiveHandle(filepath.c_str());
		
		Archive->ExtractAll(tmpDest.c_str());
		
		delete Archive;
		RemoveFile(filepath.c_str());
		
		//! check save type
		DirList * dirList = new DirList(tmpDest.c_str(), ".bin" , DirList::Files | DirList::Dirs | DirList::CheckSubfolders);
		
		for(int i = 0; i < dirList->GetFilecount(); i++)
		{
			if(!strcmp(dirList->GetFilename(i), "banner.bin"))
			{
				isSgmgxSave = true;
				srcfilepath = dirList->GetFilepath(i);
				break;
			}
			
			std::string name(dirList->GetFilename(i));
			fileext = name.substr(name.rfind("."));
			if( !Settings.FileExtensions.CompareBinaryFiles(fileext.c_str()) )
			{
				SaveInfos * infos = GetSaveInfos(dirList->GetFilepath(i));
				if(infos)
				{
					isDataBin = true;
					srcfilepath = dirList->GetFilepath(i);
					memcpy(destID, infos->ID, 5);
					delete infos;
					break;
				}
			}
		}
		
		delete dirList;
		
		ManageProgressStop();
	}
	else //! check for data.bin file
	{
		StartManageProgress(tr("Verifying file..."));
		
		SaveInfos * infos = GetSaveInfos(filepath);
		if(infos)
		{
			isDataBin = true;
			srcfilepath = filepath;
			memcpy(destID, infos->ID, 5);
			delete infos;
		}
		
		ManageProgressStop();
	}
	
	if(!isDataBin && !isSgmgxSave) //! supported files not found
	{
		RemoveFile(filepath.c_str());
		RemoveDirectory(tmpDest.c_str());
		return -7;
	}
	
	//! write files
	StartManageProgress(tr("Writing files..."));
	if(isSgmgxSave)
	{
		std::string srcfolder = srcfilepath.substr(0, srcfilepath.rfind("/"));
		
		DirList * dirList = new DirList(srcfolder.c_str(), NULL , DirList::Files | DirList::Dirs);
		
		srcfolder = srcfolder.substr(srcfolder.rfind("/"));
		std::string finaldest = Settings.TmpPath;
		finaldest += srcfolder;
		
		CreateSubfolder(finaldest.c_str());
		
		for(int i = 0; i < dirList->GetFilecount(); i++)
		{
			if(IsDir(i))
				CopyDirectory(dirList->GetFilepath(i), finaldest.c_str());
			else
				CopyFile(dirList->GetFilepath(i),  fmt("%s/%s", finaldest.c_str(), dirList->GetFilename(i)));
		}
		
		delete dirList;
	}
	else if(isDataBin)
	{
		std::string finaldest = Settings.TmpPath;
		finaldest += "/";
		finaldest += destID;
		
		CreateSubfolder(finaldest.c_str());
		
		finaldest += "/data.bin";
		CopyFile(srcfilepath.c_str(), finaldest.c_str());
	}
	
	RemoveFile(filepath.c_str());
	RemoveDirectory(tmpDest.c_str());
	ManageProgressStop();
	
	return 1;
}
Exemplo n.º 2
0
const u8 * NetReceiver::UncompressData()
{
	if(!filebuffer)
		return NULL;

	//Zip File
	if (filebuffer[0] == 'P' && filebuffer[1] == 'K' && filebuffer[2] == 0x03 && filebuffer[3] == 0x04)
	{
		char temppath[200];
		char tempfilepath[200];
		snprintf(temppath, sizeof(temppath), "%s/WiiXplorerTmp/", Settings.BootDevice);
		snprintf(tempfilepath, sizeof(tempfilepath), "%s/WiiXplorerTmp/tmp.zip", Settings.BootDevice);

		if(!CreateSubfolder(temppath))
		{
			FreeData();
			return NULL;
		}

		FILE * file = fopen(tempfilepath, "wb");
		if(!file)
		{
			FreeData();
			RemoveDirectory(temppath);
			return NULL;
		}

		fwrite(filebuffer, 1, filesize, file);
		fclose(file);

		FreeData();

		ArchiveHandle * Zip = new ArchiveHandle(tempfilepath);
		if(!Zip->ExtractAll(temppath))
		{
			delete Zip;
			RemoveDirectory(temppath);
			return NULL;
		}

		delete Zip;

		DirList Dir(temppath, ".dol,.elf");
		if(Dir.GetFilecount() <= 0)
		{
			RemoveDirectory(temppath);
			ShowError(tr("No homebrew in the zip."));
			return NULL;
		}

		char newfilepath[300];
		snprintf(newfilepath, sizeof(newfilepath), "%s", Dir.GetFilepath(0));
		snprintf(FileName, sizeof(FileName), "%s", Dir.GetFilename(0));

		u8 * buffer = NULL;
		u32 newfilesize = 0;

		if(LoadFileToMem(newfilepath, &buffer, &newfilesize) < 0)
		{
			RemoveDirectory(temppath);
			return NULL;
		}

		RemoveDirectory(temppath);
		filesize = newfilesize;
		filebuffer = buffer;
	}

	//WiiLoad zlib compression
	else if((wiiloadVersion[0] > 0 || wiiloadVersion[1] > 4) && uncfilesize != 0)
	{
		u8 * unc = (u8 *) malloc(uncfilesize);
		if(!unc)
		{
			FreeData();
			return NULL;
		}

		uLongf f = uncfilesize;
		if(uncompress(unc, &f, filebuffer, filesize) != Z_OK)
		{
			free(unc);
			FreeData();
			return NULL;
		}

		free(filebuffer);

		filebuffer = unc;
		filesize = f;
	}

	return filebuffer;
}