// Remove downloaded files
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
string command = "rm -r ";
// Path may include space.
command += "\"" + pathToSave + "\"";
system(command.c_str());
#else
string command = "rd /s /q ";
// Path may include space.
command += "\"" + pathToSave + "\"";
system(command.c_str());
#endif
bool AssetsManager::downLoad()
{
// Create a file to save package.
string outFileName = _storagePath + TEMP_PACKAGE_FILE_NAME;//下载文件的存放位置
FILE *fp = fopen(outFileName.c_str(), "wb");//新建并打来一个文件 以便下载后写入
if (! fp)
{
sendErrorMessage(kCreateFile);
CCLOG("can not create file %s", outFileName.c_str());
return false;
}
// Download pacakge
CURLcode res;
curl_easy_setopt(_curl, CURLOPT_URL, _packageUrl.c_str());//设定curl的链接
curl_easy_setopt(_curl, CURLOPT_WRITEFUNCTION, downLoadPackage);
curl_easy_setopt(_curl, CURLOPT_WRITEDATA, fp);//设置将data写入到文件流fp中
curl_easy_setopt(_curl, CURLOPT_NOPROGRESS, false);//设为false 下面才能设置进度响应函数
curl_easy_setopt(_curl, CURLOPT_PROGRESSFUNCTION, assetsManagerProgressFunc);//进度响应函数
curl_easy_setopt(_curl, CURLOPT_PROGRESSDATA, this);//数据传输的对象
res = curl_easy_perform(_curl);
curl_easy_cleanup(_curl);
if (res != 0)
{
sendErrorMessage(kNetwork);
CCLOG("error when download package");
fclose(fp);
return false;
}
CCLOG("succeed downloading package %s", _packageUrl.c_str());
fclose(fp);
return true;
}