2013-02-22 11:04:09 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 cocos2d-x.org
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2013-02-22 11:04:09 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
#include "AssetsManager.h"
|
|
|
|
|
2013-06-24 13:59:41 +08:00
|
|
|
#include <thread>
|
2013-02-22 11:04:09 +08:00
|
|
|
|
2014-08-29 15:39:52 +08:00
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "base/CCScheduler.h"
|
|
|
|
#include "base/CCUserDefault.h"
|
2015-09-17 16:52:31 +08:00
|
|
|
#include "network/CCDownloader.h"
|
2014-08-29 15:39:52 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
2013-08-07 13:36:26 +08:00
|
|
|
|
2014-11-17 03:41:37 +08:00
|
|
|
#ifdef MINIZIP_FROM_SYSTEM
|
|
|
|
#include <minizip/unzip.h>
|
|
|
|
#else // from our embedded sources
|
2013-10-15 18:00:03 +08:00
|
|
|
#include "unzip.h"
|
2014-11-17 03:41:37 +08:00
|
|
|
#endif
|
2013-02-22 11:04:09 +08:00
|
|
|
|
2019-11-24 15:43:28 +08:00
|
|
|
NS_CC_EXT_BEGIN
|
2013-02-26 16:43:46 +08:00
|
|
|
|
2015-09-17 16:52:31 +08:00
|
|
|
using namespace std;
|
|
|
|
using namespace cocos2d;
|
|
|
|
using namespace cocos2d::network;
|
|
|
|
|
|
|
|
#define KEY_OF_VERSION "current-version-code"
|
|
|
|
#define KEY_OF_DOWNLOADED_VERSION "downloaded-version-code"
|
|
|
|
#define TEMP_PACKAGE_FILE_NAME "cocos2dx-update-temp-package.zip"
|
2013-02-22 11:04:09 +08:00
|
|
|
#define BUFFER_SIZE 8192
|
|
|
|
#define MAX_FILENAME 512
|
|
|
|
|
2013-05-15 17:09:28 +08:00
|
|
|
// Implementation of AssetsManager
|
|
|
|
|
2014-07-10 00:45:27 +08:00
|
|
|
AssetsManager::AssetsManager(const char* packageUrl/* =nullptr */, const char* versionFileUrl/* =nullptr */, const char* storagePath/* =nullptr */)
|
2015-05-16 11:48:34 +08:00
|
|
|
: _storagePath(storagePath ? storagePath : "")
|
2013-02-22 11:04:09 +08:00
|
|
|
, _version("")
|
2015-05-16 11:48:34 +08:00
|
|
|
, _packageUrl(packageUrl ? packageUrl : "")
|
|
|
|
, _versionFileUrl(versionFileUrl ? versionFileUrl : "")
|
2013-05-15 17:09:28 +08:00
|
|
|
, _downloadedVersion("")
|
2015-09-17 16:52:31 +08:00
|
|
|
, _downloader(new Downloader())
|
2013-05-15 17:09:28 +08:00
|
|
|
, _connectionTimeout(0)
|
2014-04-09 22:53:59 +08:00
|
|
|
, _delegate(nullptr)
|
2013-06-24 13:59:41 +08:00
|
|
|
, _isDownloading(false)
|
2013-08-08 17:36:56 +08:00
|
|
|
, _shouldDeleteDelegateWhenExit(false)
|
2013-02-22 11:04:09 +08:00
|
|
|
{
|
2016-06-22 14:31:26 +08:00
|
|
|
checkStoragePath();
|
2015-09-17 16:52:31 +08:00
|
|
|
// convert downloader error code to AssetsManager::ErrorCode
|
2016-11-16 09:48:37 +08:00
|
|
|
_downloader->onTaskError = [this](const DownloadTask& /*task*/,
|
2015-09-17 16:52:31 +08:00
|
|
|
int errorCode,
|
2016-11-16 09:48:37 +08:00
|
|
|
int /*errorCodeInternal*/,
|
|
|
|
const std::string& /*errorStr*/)
|
2015-09-17 16:52:31 +08:00
|
|
|
{
|
|
|
|
_isDownloading = false;
|
|
|
|
|
|
|
|
if (nullptr == _delegate)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2019-11-24 15:43:28 +08:00
|
|
|
auto err = (DownloadTask::ERROR_OPEN_FILE_FAILED == errorCode) ? ErrorCode::CREATE_FILE : ErrorCode::NETWORK;
|
2015-09-17 16:52:31 +08:00
|
|
|
_delegate->onError(err);
|
|
|
|
};
|
|
|
|
|
|
|
|
// progress callback
|
2019-11-24 15:43:28 +08:00
|
|
|
_downloader->onTaskProgress = [this](const DownloadTask& task)
|
2015-09-17 16:52:31 +08:00
|
|
|
{
|
|
|
|
if(FileUtils::getInstance()->getFileExtension(task.requestURL) != ".zip")
|
|
|
|
{
|
|
|
|
// get version progress don't report
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nullptr == _delegate)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-24 15:43:28 +08:00
|
|
|
int percent = task.progressInfo.totalBytesExpected ? int(task.progressInfo.totalBytesReceived * 100 / task.progressInfo.totalBytesExpected) : 0;
|
2015-09-17 16:52:31 +08:00
|
|
|
_delegate->onProgress(percent);
|
|
|
|
CCLOG("downloading... %d%%", percent);
|
|
|
|
};
|
|
|
|
|
|
|
|
// get version from version file when get data success
|
2016-11-16 09:48:37 +08:00
|
|
|
_downloader->onDataTaskSuccess = [this](const DownloadTask& /*task*/,
|
2015-09-17 16:52:31 +08:00
|
|
|
std::vector<unsigned char>& data)
|
|
|
|
{
|
|
|
|
// store version info to member _version
|
|
|
|
const char *p = (char *)data.data();
|
|
|
|
_version.insert(_version.end(), p, p + data.size());
|
|
|
|
|
|
|
|
if (getVersion() == _version)
|
|
|
|
{
|
|
|
|
if (_delegate)
|
|
|
|
{
|
|
|
|
_delegate->onError(ErrorCode::NO_NEW_VERSION);
|
|
|
|
}
|
|
|
|
CCLOG("there is not new version");
|
|
|
|
// Set resource search path.
|
|
|
|
setSearchPath();
|
|
|
|
_isDownloading = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// start download new version assets
|
|
|
|
// 1. Urls of package and version should be valid;
|
|
|
|
// 2. Package should be a zip file.
|
|
|
|
if (_versionFileUrl.empty()
|
|
|
|
|| _packageUrl.empty()
|
|
|
|
|| FileUtils::getInstance()->getFileExtension(_packageUrl) != ".zip"
|
|
|
|
)
|
|
|
|
{
|
|
|
|
CCLOG("no version file url, or no package url, or the package is not a zip file");
|
|
|
|
_isDownloading = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is package already downloaded?
|
|
|
|
_downloadedVersion = UserDefault::getInstance()->getStringForKey(keyOfDownloadedVersion().c_str());
|
|
|
|
if (_downloadedVersion == _version)
|
|
|
|
{
|
|
|
|
downloadAndUncompress();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// start download;
|
|
|
|
const string outFileName = _storagePath + TEMP_PACKAGE_FILE_NAME;
|
2016-06-22 14:31:26 +08:00
|
|
|
_downloader->createDownloadFileTask(_packageUrl, outFileName);
|
2015-09-17 16:52:31 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// after download package, do uncompress operation
|
2016-11-16 09:48:37 +08:00
|
|
|
_downloader->onFileTaskSuccess = [this](const DownloadTask& /*task*/)
|
2015-09-17 16:52:31 +08:00
|
|
|
{
|
|
|
|
downloadAndUncompress();
|
|
|
|
};
|
2013-02-22 11:04:09 +08:00
|
|
|
}
|
|
|
|
|
2013-05-15 17:09:28 +08:00
|
|
|
AssetsManager::~AssetsManager()
|
2013-02-22 11:04:09 +08:00
|
|
|
{
|
2013-08-08 17:36:56 +08:00
|
|
|
if (_shouldDeleteDelegateWhenExit)
|
|
|
|
{
|
|
|
|
delete _delegate;
|
|
|
|
}
|
2015-09-17 16:52:31 +08:00
|
|
|
CC_SAFE_DELETE(_downloader);
|
2013-02-22 11:04:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AssetsManager::checkStoragePath()
|
|
|
|
{
|
2019-11-24 15:43:28 +08:00
|
|
|
if (!_storagePath.empty() && _storagePath[_storagePath.size() - 1] != '/')
|
2013-02-22 11:04:09 +08:00
|
|
|
{
|
|
|
|
_storagePath.append("/");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-08 16:29:29 +08:00
|
|
|
// Multiple key names
|
2013-09-06 15:10:11 +08:00
|
|
|
static std::string keyWithHash( const char* prefix, const std::string& url )
|
2013-08-08 16:29:29 +08:00
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
sprintf(buf,"%s%zd",prefix,std::hash<std::string>()(url));
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
// hashed version
|
2013-08-09 13:39:40 +08:00
|
|
|
std::string AssetsManager::keyOfVersion() const
|
2013-08-08 16:29:29 +08:00
|
|
|
{
|
2013-09-06 15:10:11 +08:00
|
|
|
return keyWithHash(KEY_OF_VERSION,_packageUrl);
|
2013-08-08 16:29:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// hashed version
|
2013-08-09 13:39:40 +08:00
|
|
|
std::string AssetsManager::keyOfDownloadedVersion() const
|
2013-08-08 16:29:29 +08:00
|
|
|
{
|
2013-09-06 15:10:11 +08:00
|
|
|
return keyWithHash(KEY_OF_DOWNLOADED_VERSION,_packageUrl);
|
2013-08-08 16:29:29 +08:00
|
|
|
}
|
|
|
|
|
2013-02-22 11:04:09 +08:00
|
|
|
bool AssetsManager::checkUpdate()
|
|
|
|
{
|
2019-11-24 15:43:28 +08:00
|
|
|
if (_versionFileUrl.empty() || _isDownloading) return false;
|
2013-02-22 11:04:09 +08:00
|
|
|
|
2013-02-26 10:29:31 +08:00
|
|
|
// Clear _version before assign new value.
|
|
|
|
_version.clear();
|
2015-09-17 16:52:31 +08:00
|
|
|
_isDownloading = true;
|
|
|
|
_downloader->createDownloadDataTask(_versionFileUrl);
|
2013-02-22 11:04:09 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-24 13:59:41 +08:00
|
|
|
void AssetsManager::downloadAndUncompress()
|
2013-05-15 17:09:28 +08:00
|
|
|
{
|
2015-09-17 16:52:31 +08:00
|
|
|
std::thread([this]()
|
2013-05-15 17:09:28 +08:00
|
|
|
{
|
2015-09-17 16:52:31 +08:00
|
|
|
do
|
2013-05-15 17:09:28 +08:00
|
|
|
{
|
2015-09-17 16:52:31 +08:00
|
|
|
// Uncompress zip file.
|
|
|
|
if (! uncompress())
|
|
|
|
{
|
|
|
|
Director::getInstance()->getScheduler()->performFunctionInCocosThread([&, this]{
|
|
|
|
UserDefault::getInstance()->setStringForKey(this->keyOfDownloadedVersion().c_str(),"");
|
|
|
|
UserDefault::getInstance()->flush();
|
|
|
|
if (this->_delegate)
|
|
|
|
this->_delegate->onError(ErrorCode::UNCOMPRESS);
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
2013-05-15 17:09:28 +08:00
|
|
|
|
2015-09-17 16:52:31 +08:00
|
|
|
Director::getInstance()->getScheduler()->performFunctionInCocosThread([&, this] {
|
|
|
|
|
|
|
|
// Record new version code.
|
2019-11-24 15:43:28 +08:00
|
|
|
UserDefault::getInstance()->setStringForKey(this->keyOfVersion().c_str(), this->_version);
|
2015-09-17 16:52:31 +08:00
|
|
|
|
|
|
|
// Unrecord downloaded version code.
|
|
|
|
UserDefault::getInstance()->setStringForKey(this->keyOfDownloadedVersion().c_str(), "");
|
2014-08-12 20:13:52 +08:00
|
|
|
UserDefault::getInstance()->flush();
|
2015-09-17 16:52:31 +08:00
|
|
|
|
|
|
|
// Set resource search path.
|
|
|
|
this->setSearchPath();
|
|
|
|
|
|
|
|
// Delete unloaded zip file.
|
|
|
|
string zipfileName = this->_storagePath + TEMP_PACKAGE_FILE_NAME;
|
|
|
|
if (remove(zipfileName.c_str()) != 0)
|
|
|
|
{
|
|
|
|
CCLOG("can not remove downloaded zip file %s", zipfileName.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->_delegate) this->_delegate->onSuccess();
|
2013-11-28 11:06:08 +08:00
|
|
|
});
|
|
|
|
|
2015-09-17 16:52:31 +08:00
|
|
|
} while (0);
|
|
|
|
|
|
|
|
_isDownloading = false;
|
|
|
|
|
|
|
|
}).detach();
|
2013-05-15 17:09:28 +08:00
|
|
|
}
|
|
|
|
|
2013-11-11 17:06:32 +08:00
|
|
|
void AssetsManager::update()
|
2013-02-22 11:04:09 +08:00
|
|
|
{
|
2015-09-17 16:52:31 +08:00
|
|
|
// all operation in checkUpdate, nothing need to do
|
2015-12-11 23:37:58 +08:00
|
|
|
// keep this function for compatibility
|
2013-02-22 11:04:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AssetsManager::uncompress()
|
|
|
|
{
|
|
|
|
// Open the zip file
|
|
|
|
string outFileName = _storagePath + TEMP_PACKAGE_FILE_NAME;
|
2019-11-24 15:43:28 +08:00
|
|
|
unzFile zipfile = unzOpen(outFileName.c_str());
|
2013-02-22 11:04:09 +08:00
|
|
|
if (! zipfile)
|
|
|
|
{
|
|
|
|
CCLOG("can not open downloaded zip file %s", outFileName.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get info about the zip file
|
|
|
|
unz_global_info global_info;
|
|
|
|
if (unzGetGlobalInfo(zipfile, &global_info) != UNZ_OK)
|
|
|
|
{
|
|
|
|
CCLOG("can not read file global info of %s", outFileName.c_str());
|
|
|
|
unzClose(zipfile);
|
2013-05-15 17:09:28 +08:00
|
|
|
return false;
|
2013-02-22 11:04:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Buffer to hold data read from the zip file
|
|
|
|
char readBuffer[BUFFER_SIZE];
|
|
|
|
|
2013-02-25 17:50:32 +08:00
|
|
|
CCLOG("start uncompressing");
|
|
|
|
|
2013-02-22 11:04:09 +08:00
|
|
|
// Loop to extract all files.
|
|
|
|
uLong i;
|
|
|
|
for (i = 0; i < global_info.number_entry; ++i)
|
|
|
|
{
|
|
|
|
// Get info about current file.
|
|
|
|
unz_file_info fileInfo;
|
|
|
|
char fileName[MAX_FILENAME];
|
|
|
|
if (unzGetCurrentFileInfo(zipfile,
|
|
|
|
&fileInfo,
|
|
|
|
fileName,
|
|
|
|
MAX_FILENAME,
|
2014-07-10 00:45:27 +08:00
|
|
|
nullptr,
|
2013-02-22 11:04:09 +08:00
|
|
|
0,
|
2014-07-10 00:45:27 +08:00
|
|
|
nullptr,
|
2013-02-22 11:04:09 +08:00
|
|
|
0) != UNZ_OK)
|
|
|
|
{
|
|
|
|
CCLOG("can not read file info");
|
|
|
|
unzClose(zipfile);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-05 06:26:16 +08:00
|
|
|
const string fullPath = _storagePath + fileName;
|
2013-02-22 11:04:09 +08:00
|
|
|
|
|
|
|
// Check if this entry is a directory or a file.
|
|
|
|
const size_t filenameLength = strlen(fileName);
|
|
|
|
if (fileName[filenameLength-1] == '/')
|
|
|
|
{
|
2015-12-11 23:37:58 +08:00
|
|
|
// Entry is a directory, so create it.
|
|
|
|
// If the directory exists, it will failed silently.
|
2015-09-17 16:52:31 +08:00
|
|
|
if (!FileUtils::getInstance()->createDirectory(fullPath))
|
2013-02-22 11:04:09 +08:00
|
|
|
{
|
|
|
|
CCLOG("can not create directory %s", fullPath.c_str());
|
|
|
|
unzClose(zipfile);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-08 11:23:06 +08:00
|
|
|
//There are not directory entry in some case.
|
|
|
|
//So we need to test whether the file directory exists when uncompressing file entry
|
|
|
|
//, if does not exist then create directory
|
2014-01-05 06:26:16 +08:00
|
|
|
const string fileNameStr(fileName);
|
2013-11-08 11:23:06 +08:00
|
|
|
|
|
|
|
size_t startIndex=0;
|
|
|
|
|
2019-04-22 09:44:19 +08:00
|
|
|
size_t index=fileNameStr.find('/',startIndex);
|
2013-11-08 11:23:06 +08:00
|
|
|
|
2014-01-05 06:26:16 +08:00
|
|
|
while(index != std::string::npos)
|
2013-11-08 11:23:06 +08:00
|
|
|
{
|
2014-01-05 06:26:16 +08:00
|
|
|
const string dir=_storagePath+fileNameStr.substr(0,index);
|
2013-11-08 11:23:06 +08:00
|
|
|
|
2019-11-24 15:43:28 +08:00
|
|
|
FILE *out = fopen(dir.c_str(), "r");
|
2013-11-08 11:23:06 +08:00
|
|
|
|
|
|
|
if(!out)
|
|
|
|
{
|
2015-09-17 16:52:31 +08:00
|
|
|
if (!FileUtils::getInstance()->createDirectory(dir))
|
2013-11-08 11:23:06 +08:00
|
|
|
{
|
|
|
|
CCLOG("can not create directory %s", dir.c_str());
|
|
|
|
unzClose(zipfile);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("create directory %s",dir.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fclose(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
startIndex=index+1;
|
|
|
|
|
2019-04-22 09:44:19 +08:00
|
|
|
index=fileNameStr.find('/',startIndex);
|
2013-11-08 11:23:06 +08:00
|
|
|
|
|
|
|
}
|
2015-09-17 16:52:31 +08:00
|
|
|
|
2013-02-22 11:04:09 +08:00
|
|
|
// Entry is a file, so extract it.
|
|
|
|
|
|
|
|
// Open current file.
|
|
|
|
if (unzOpenCurrentFile(zipfile) != UNZ_OK)
|
|
|
|
{
|
|
|
|
CCLOG("can not open file %s", fileName);
|
|
|
|
unzClose(zipfile);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a file to store current file.
|
2019-11-24 15:43:28 +08:00
|
|
|
FILE *out = fopen(fullPath.c_str(), "wb");
|
2013-02-22 11:04:09 +08:00
|
|
|
if (! out)
|
|
|
|
{
|
|
|
|
CCLOG("can not open destination file %s", fullPath.c_str());
|
|
|
|
unzCloseCurrentFile(zipfile);
|
|
|
|
unzClose(zipfile);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write current file content to destinate file.
|
|
|
|
int error = UNZ_OK;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
error = unzReadCurrentFile(zipfile, readBuffer, BUFFER_SIZE);
|
|
|
|
if (error < 0)
|
|
|
|
{
|
2013-02-26 16:29:52 +08:00
|
|
|
CCLOG("can not read zip file %s, error code is %d", fileName, error);
|
2013-02-22 11:04:09 +08:00
|
|
|
unzCloseCurrentFile(zipfile);
|
|
|
|
unzClose(zipfile);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error > 0)
|
|
|
|
{
|
|
|
|
fwrite(readBuffer, error, 1, out);
|
|
|
|
}
|
|
|
|
} while(error > 0);
|
|
|
|
|
|
|
|
fclose(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
unzCloseCurrentFile(zipfile);
|
|
|
|
|
|
|
|
// Goto next entry listed in the zip file.
|
|
|
|
if ((i+1) < global_info.number_entry)
|
|
|
|
{
|
|
|
|
if (unzGoToNextFile(zipfile) != UNZ_OK)
|
|
|
|
{
|
|
|
|
CCLOG("can not read next file");
|
|
|
|
unzClose(zipfile);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-26 16:29:52 +08:00
|
|
|
CCLOG("end uncompressing");
|
2014-02-12 21:48:18 +08:00
|
|
|
unzClose(zipfile);
|
2013-02-26 16:29:52 +08:00
|
|
|
|
2013-02-22 11:04:09 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssetsManager::setSearchPath()
|
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
vector<string> searchPaths = FileUtils::getInstance()->getSearchPaths();
|
2013-02-22 11:04:09 +08:00
|
|
|
vector<string>::iterator iter = searchPaths.begin();
|
|
|
|
searchPaths.insert(iter, _storagePath);
|
2013-07-12 06:24:23 +08:00
|
|
|
FileUtils::getInstance()->setSearchPaths(searchPaths);
|
2013-02-22 11:04:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* AssetsManager::getPackageUrl() const
|
|
|
|
{
|
|
|
|
return _packageUrl.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssetsManager::setPackageUrl(const char *packageUrl)
|
|
|
|
{
|
|
|
|
_packageUrl = packageUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* AssetsManager::getStoragePath() const
|
|
|
|
{
|
|
|
|
return _storagePath.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssetsManager::setStoragePath(const char *storagePath)
|
|
|
|
{
|
|
|
|
_storagePath = storagePath;
|
|
|
|
checkStoragePath();
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* AssetsManager::getVersionFileUrl() const
|
|
|
|
{
|
|
|
|
return _versionFileUrl.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssetsManager::setVersionFileUrl(const char *versionFileUrl)
|
|
|
|
{
|
|
|
|
_versionFileUrl = versionFileUrl;
|
|
|
|
}
|
2013-02-22 16:02:53 +08:00
|
|
|
|
|
|
|
string AssetsManager::getVersion()
|
|
|
|
{
|
2013-08-09 13:39:40 +08:00
|
|
|
return UserDefault::getInstance()->getStringForKey(keyOfVersion().c_str());
|
2013-02-22 16:02:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AssetsManager::deleteVersion()
|
|
|
|
{
|
2013-08-09 13:39:40 +08:00
|
|
|
UserDefault::getInstance()->setStringForKey(keyOfVersion().c_str(), "");
|
2013-02-22 16:02:53 +08:00
|
|
|
}
|
2013-02-26 16:43:46 +08:00
|
|
|
|
2013-05-15 17:09:28 +08:00
|
|
|
void AssetsManager::setDelegate(AssetsManagerDelegateProtocol *delegate)
|
|
|
|
{
|
|
|
|
_delegate = delegate;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssetsManager::setConnectionTimeout(unsigned int timeout)
|
|
|
|
{
|
|
|
|
_connectionTimeout = timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int AssetsManager::getConnectionTimeout()
|
|
|
|
{
|
|
|
|
return _connectionTimeout;
|
|
|
|
}
|
|
|
|
|
2013-08-07 13:07:01 +08:00
|
|
|
AssetsManager* AssetsManager::create(const char* packageUrl, const char* versionFileUrl, const char* storagePath, ErrorCallback errorCallback, ProgressCallback progressCallback, SuccessCallback successCallback )
|
|
|
|
{
|
|
|
|
class DelegateProtocolImpl : public AssetsManagerDelegateProtocol
|
|
|
|
{
|
|
|
|
public :
|
2016-11-22 09:54:04 +08:00
|
|
|
DelegateProtocolImpl(ErrorCallback& aErrorCallback, ProgressCallback& aProgressCallback, SuccessCallback& aSuccessCallback)
|
2013-11-11 12:49:38 +08:00
|
|
|
: errorCallback(aErrorCallback), progressCallback(aProgressCallback), successCallback(aSuccessCallback)
|
2013-08-07 13:07:01 +08:00
|
|
|
{}
|
|
|
|
|
|
|
|
virtual void onError(AssetsManager::ErrorCode errorCode) { errorCallback(int(errorCode)); }
|
|
|
|
virtual void onProgress(int percent) { progressCallback(percent); }
|
|
|
|
virtual void onSuccess() { successCallback(); }
|
|
|
|
|
|
|
|
private :
|
|
|
|
ErrorCallback errorCallback;
|
|
|
|
ProgressCallback progressCallback;
|
|
|
|
SuccessCallback successCallback;
|
|
|
|
};
|
|
|
|
|
2014-08-28 07:31:57 +08:00
|
|
|
auto* manager = new (std::nothrow) AssetsManager(packageUrl,versionFileUrl,storagePath);
|
|
|
|
auto* delegate = new (std::nothrow) DelegateProtocolImpl(errorCallback,progressCallback,successCallback);
|
2013-08-07 13:07:01 +08:00
|
|
|
manager->setDelegate(delegate);
|
2013-08-08 17:36:56 +08:00
|
|
|
manager->_shouldDeleteDelegateWhenExit = true;
|
2013-08-16 18:03:27 +08:00
|
|
|
manager->autorelease();
|
2013-08-07 13:07:01 +08:00
|
|
|
return manager;
|
|
|
|
}
|
|
|
|
|
2019-11-24 15:43:28 +08:00
|
|
|
NS_CC_EXT_END
|