1.创建目录
(1)
void UpdateLayer::createDownloadedDir()
{
pathToSave =CCFileUtils::sharedFileUtils()->getWritablePath();
pathToSave +="tmpdir";
// Create the folder if it doesn't exist
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
DIR *pDir =NULL;
pDir = opendir (pathToSave.c_str());
if (! pDir)
{
mkdir(pathToSave.c_str(),S_IRWXU | S_IRWXG |S_IRWXO); //目录属性设置:S_IRWXU、S_IRWXG、S_IRWXO
}
#else
if ((GetFileAttributesA(pathToSave.c_str())) == INVALID_FILE_ATTRIBUTES) //windows平台
{
CreateDirectoryA(pathToSave.c_str(),0);
}
#endif
}
(2)
bool AssetsManager::createDirectory(constchar *path)
{
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
mode_t processMask =umask(0); //umask:linux系统设置文件权限
int ret =mkdir(path, S_IRWXU |S_IRWXG | S_IRWXO); //
umask(processMask);
if (ret !=0 && (errno !=EEXIST))
{
returnfalse;
}
returntrue;
#else
BOOL ret = CreateDirectoryA(path, NULL);
if (!ret && ERROR_ALREADY_EXISTS != GetLastError())
{
returnfalse;
}
returntrue;
#endif
}
2.删除某个目录下所有文件
voidUpdateLayer::reset(cocos2d::CCObject *pSender)
{
// 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
}
3.AssetsManager类实现下载
(1)下载资源
//文件里写下载内容
static size_t downLoadPackage(void *ptr,size_t size, size_t nmemb,void *userdata)
{
CCLog("static size_t downLoadPackage(void *ptr, size_t size, size_t nmemb, void *userdata)");
FILE *fp = (FILE*)userdata;
size_t written =fwrite(ptr, size, nmemb, fp);
return written;
}
//记录下载进度
static int progressFunc(void *ptr,double totalToDownload,double nowDownloaded, double totalToUpLoad, double nowUpLoaded)
{
CCLOG("downloading... %d%%", (int)(nowDownloaded/totalToDownload*100));
CCLog("-----------");
return0;
}
//#define TEMP_PACKAGE_FILE_NAME "cocos2dx-update-temp-package.zip"
bool AssetsManager::downLoad()
{
string outFileName =_storagePath + TEMP_PACKAGE_FILE_NAME; //_storagePath存储路径
FILE *fp =fopen(outFileName.c_str(),"wb");
if (! fp)
{
CCLOG("can not create file %s", outFileName.c_str());
returnfalse;
}
CURLcode res; //CURLcode枚举成功失败对应的编号
/*下载地址、执行下载函数、写文件指针、??、下载进度查看、执行、清理*/
curl_easy_setopt(_curl,CURLOPT_URL,_packageUrl.c_str());
curl_easy_setopt(_curl,CURLOPT_WRITEFUNCTION, downLoadPackage);
curl_easy_setopt(_curl,CURLOPT_WRITEDATA, fp); //fp指针
curl_easy_setopt(_curl,CURLOPT_NOPROGRESS, false);
curl_easy_setopt(_curl,CURLOPT_PROGRESSFUNCTION,progressFunc);
res = curl_easy_perform(_curl);
curl_easy_cleanup(_curl);
//执行结果
if (res !=0)
{
CCLOG("error when download package");
fclose(fp);
returnfalse;
}
//全部下载完成
CCLOG("succeed downloading package %s",_packageUrl.c_str());
fclose(fp);
returntrue;
}
(2)网络文字下载
//检查版本
static size_t getVersionCode(void *ptr, size_t size, size_t nmemb, void *userdata)
{
string *version = (string*)userdata;
version->append((char*)ptr, size * nmemb);
return (size * nmemb);
}
//网络连接
bool AssetsManager::checkUpdate()
{
if (_versionFileUrl.size() == 0) return false;
_curl = curl_easy_init();
if (! _curl)
{
CCLOG("can not init curl");
return false;
}
_version.clear();
CURLcode res;
curl_easy_setopt(_curl, CURLOPT_URL, _versionFileUrl.c_str());
curl_easy_setopt(_curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(_curl, CURLOPT_WRITEFUNCTION, getVersionCode);
curl_easy_setopt(_curl, CURLOPT_WRITEDATA, &_version); //_version:直接写字符串
res = curl_easy_perform(_curl);
if (res != 0)
{
CCLOG("can not get version file content, error code is %d", res);
curl_easy_cleanup(_curl);
return false;
}
string recordedVersion = CCUserDefault::sharedUserDefault()->getStringForKey(KEY_OF_VERSION);
if (recordedVersion == _version)
{
CCLOG("there is not new version");
// Set resource search path.
setSearchPath();
return false;
}
CCLOG("there is a new version: %s", _version.c_str());
return true;
}