axmol/extensions/assets-manager/AssetsManagerEx.cpp

1010 lines
31 KiB
C++
Raw Normal View History

2014-10-09 18:30:39 +08:00
/****************************************************************************
Copyright (c) 2014 cocos2d-x.org
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 "AssetsManagerEx.h"
#include "CCEventListenerAssetsManagerEx.h"
#include "deprecated/CCString.h"
#include "base/CCDirector.h"
2014-10-09 18:30:39 +08:00
#include <stdio.h>
#ifdef MINIZIP_FROM_SYSTEM
#include <minizip/unzip.h>
#else // from our embedded sources
2014-10-09 18:30:39 +08:00
#include "unzip.h"
#endif
#include "base/CCAsyncTaskPool.h"
2014-10-09 18:30:39 +08:00
using namespace cocos2d;
using namespace std;
2014-10-09 18:30:39 +08:00
NS_CC_EXT_BEGIN
#define VERSION_FILENAME "version.manifest"
#define TEMP_MANIFEST_FILENAME "project.manifest.temp"
#define MANIFEST_FILENAME "project.manifest"
#define BUFFER_SIZE 8192
#define MAX_FILENAME 512
#define DEFAULT_CONNECTION_TIMEOUT 8
const std::string AssetsManagerEx::VERSION_ID = "@version";
const std::string AssetsManagerEx::MANIFEST_ID = "@manifest";
// Implementation of AssetsManagerEx
AssetsManagerEx::AssetsManagerEx(const std::string& manifestUrl, const std::string& storagePath)
: _updateState(State::UNCHECKED)
, _assets(nullptr)
2014-10-09 18:30:39 +08:00
, _storagePath("")
, _cacheVersionPath("")
, _cacheManifestPath("")
, _tempManifestPath("")
, _manifestUrl(manifestUrl)
2014-10-09 18:30:39 +08:00
, _localManifest(nullptr)
, _tempManifest(nullptr)
, _remoteManifest(nullptr)
, _waitToUpdate(false)
, _percent(0)
, _percentByFile(0)
, _totalToDownload(0)
, _totalWaitToDownload(0)
2014-11-26 14:39:56 +08:00
, _inited(false)
2014-10-09 18:30:39 +08:00
{
// Init variables
_eventDispatcher = Director::getInstance()->getEventDispatcher();
std::string pointer = StringUtils::format("%p", this);
_eventName = EventListenerAssetsManagerEx::LISTENER_ID + pointer;
_fileUtils = FileUtils::getInstance();
_updateState = State::UNCHECKED;
Squashed commit of the following: commit 2c810a8e931e07b06498c07db37cbf20a2bcfc92 Merge: 61788a0 082caaf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu Aug 13 00:13:38 2015 -0700 Merge branch 'v3' into downloader_decouple commit 61788a0b34188fed5367cab121817db97198bcd8 Merge: 8c1e211 5e21580 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Aug 12 20:45:12 2015 -0700 Merge branch 'v3' into downloader_decouple commit 8c1e2119a5c671c00da5ce1fb244029f2bfab09f Merge: 2ea66af 8d73883 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Aug 12 20:42:04 2015 -0700 Merge pull request #6 from minggo/downloader_decouple fix js binding error commit 8d7388302ff1d8da575b277e47a3f8ceb1017e98 Author: minggo <ming@cocos2d-x.org> Date: Thu Aug 13 11:09:58 2015 +0800 fix js binding error commit 2ea66af7684f5ec8a20067b1a49347920dbbbb40 Merge: 8cdb0b6 bc964e8 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Aug 12 09:14:22 2015 -0700 Merge branch 'v3' into downloader_decouple commit 8cdb0b6e4b83210b1f68b4568c74ccb36f504806 Merge: 9272765 08eeca9 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Aug 11 20:11:46 2015 -0700 Merge pull request #5 from minggo/downloader_decouple fix lua-binding error commit 08eeca9d99b5160c48bac1bfecf03b459d05ed15 Author: minggo <ming@cocos2d-x.org> Date: Wed Aug 12 10:34:05 2015 +0800 fix lua-binding error commit 927276589d57432477f8ffc033e19a21a215b246 Merge: 8252464 6f4ed8a Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Aug 11 10:39:22 2015 -0700 Merge branch 'v3' into downloader_decouple commit 8252464020dbe86da4e5e5aa6e4e45f6997f6b9b Merge: 5dddbb6 138df42 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Aug 11 06:53:48 2015 -0700 Merge pull request #4 from minggo/downloader_decouple fix linking error in lua-tests on Android commit 138df42a3acfdcfae29eeab1b03ef5dd062f0145 Author: minggo <ming@cocos2d-x.org> Date: Tue Aug 11 16:23:53 2015 +0800 fix link error in lua-tests commit 5dddbb61a1bcda96aa93bfcbe78f42c639eb6247 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 23:22:50 2015 -0700 works for real on win32 commit ddadaf82231b60c8e343718561c0d78657afe7c2 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 22:26:59 2015 -0700 compiles on win32 commit 733f1d4c2e4f8382ff19f1793f7f09950774fe78 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 21:57:49 2015 -0700 compiles on iOS!! commit f706f97962eb393bf7dfec79739ff8dfdec103d5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 21:27:03 2015 -0700 compiles and runs Ok on windows. Tested all downloadertests + assetmanagertest commit 5e333f4407952d9c2e71e18cb9393d8b6cd170b4 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 20:24:28 2015 -0700 updates windows projects commit e77fd8a278594fefc24ff13cbee038508429294d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 19:56:10 2015 -0700 compiles on Android commit 5f515e3519a0688d11476ea6ef235007e8378c96 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 18:34:33 2015 -0700 trying to fix a compile issue on iOS WIP commit b144ea67dfe7a06e7bfb515adf16f536ba2ead98 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 18:25:11 2015 -0700 make bug fixes adds public `getHeader` commit a228799ba74eb8642c43d215a802f811a576a4aa Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 17:42:19 2015 -0700 some linux fixes commit 2cab23d87acc138823ebe9dcec9d42f827690d99 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 16:58:53 2015 -0700 Squashed commit of the following: commit 86227e9138c1fb523bd28186d355de65cc64c588 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 16:55:17 2015 -0700 works %100 and uses less memory than before commit a9cafa6f2fe7847e279934d2c162287680760dda Merge: 0bbc62e 0cbd71f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 15:49:33 2015 -0700 Merge branch 'v3' into cocos_net commit 0bbc62ee1fbfd107a935131d5d93994e87865afa Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 15:46:55 2015 -0700 more tests... and works but there is huge bug. ProgData <vector> needs the position of DownloadUnits <unordered_map> but <unordered_map> does not guaranty the order. So in my next commit I"ll merge DownloadUnit with ProgressData commit 389ed8639a1942f628773616a8e340bc66a0c1d7 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 13:47:07 2015 -0700 debugging info commit af0dc45bb9c4705934ced2c1d67e5afc06b6f317 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 11:47:31 2015 -0700 sync and async tests working ok commit 999fa7a7bfb28565492f47b39caef7f1a079c183 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 11:38:38 2015 -0700 adding tests commit 91ee294942510718400edc0d00d3c744d00339ee Merge: 76ab703 7f215e9 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 10:03:50 2015 -0700 Merge branch 'v3' into cocos_net commit 76ab7032d3a21156f05395a021391a16649f755a Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Aug 7 00:22:20 2015 -0700 Downloader tests commit b65c4dad7089660bfe697c0dab3e9290d1a6d013 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu Aug 6 21:34:54 2015 -0700 compiles on Xcode 7 commit ec0d66340b8daf0b042d36333dff759cd424f465 Merge: 878e4b5 0d77838 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu Aug 6 11:02:24 2015 -0700 Merge branch 'v3' into cocos_net commit 878e4b5d0f87bc6d5d6e507102ab15e2c24a1c8f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Aug 5 18:07:02 2015 -0700 working, yeah! commit ad1e7637a5ac2e6bff44d35429f5556ea1cb8a4c Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Aug 5 17:49:59 2015 -0700 code is more thread-safe friendly commit 8c6baaee6b898d4b90e5d8988a174d8a33702f60 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Aug 5 16:39:02 2015 -0700 fixes callbacks commit ddab4d753106c6ff11304c1c69739c5611e3f20a Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Aug 5 15:43:51 2015 -0700 cleaner code runs callbacks in cocos2d thread only if needed commit ef75844aaa010633e6e27ae6e69460174460d5c7 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Aug 5 11:58:04 2015 -0700 removed weak_ptr<> from ProgressData that logic of the code is added in the callbacks commit d9c7436cbadc611592b3c80ca2a3a9edeaa00853 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Aug 5 11:54:07 2015 -0700 checks whether or not the callbacks are in the cocos2d thread commit ae6594a6f866f6b5015ba293db23343887063d2f Merge: 4c61069 6831e24 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Aug 5 10:48:34 2015 -0700 Merge branch 'v3' into cocos_net commit 4c61069a7e0e33fa0332df700cc8df4807178caf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Aug 4 21:36:50 2015 -0700 almost there... Downloader has std::vector<FILE*> instead of FileDescriptors code cleaner... commit c34d04a8218d2be5c316e0487e5ac46792bbed76 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Aug 4 20:38:44 2015 -0700 passes progressData to batch no need to alloc memory for each ProgressData commit f7e8bbd8b85f214b919bce6118d4158dd6d26368 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Aug 4 17:47:23 2015 -0700 tidier code and more optimizations commit a486dc78aedcf9c6178e490292edd572f8a7b374 Merge: 4178327 a06421b Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Aug 4 14:03:22 2015 -0700 Merge branch 'v3' into cocos_net commit 417832705910a6ebd16a876cd98064ea16f0f45c Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Aug 3 18:13:13 2015 -0700 not finished yet... ...but trying to have a common inteface with batched and non-batched downloads commit 1d0e4652763c2e1511ac102a62defae6c6a89220 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Aug 3 16:29:39 2015 -0700 ErrorCallback compiles starting the batching code commit 6645a287c47dc01cbf1f2d80e9c0fdd2b16e3e18 Merge: c180425 8d836da Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Aug 3 10:28:51 2015 -0700 Merge branch 'v3' into cocos_net commit c180425d8181b8e6f53be766d8d2b94f908ae168 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Jul 31 15:35:53 2015 -0700 More progress on migrating multiple-file downloader to the implementation file commit 5fa273e265963af3c0cb6338bf49dc5e1b54173b Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Jul 31 14:00:02 2015 -0700 Dowloader moved to `cocos2d::network` commit 14b1d0825106701b5435a266b255f09291b6486b Merge: 13cfc8d f744383 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Jul 31 11:13:55 2015 -0700 Merge branch 'v3' into cocos_net commit 13cfc8de4eb4835ffb8f8d671d3d070c411b0a2e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri Jul 31 11:11:45 2015 -0700 DownloadUnits belong to network commit e6e17564ead1fb7f6f7df343b0627a104cd756f8 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu Jul 30 12:23:36 2015 -0700 adds `check for option` commit 5cb76dfaaed42011a0c536d01b82a0a94487d6aa Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu Jul 30 10:37:51 2015 -0700 Downloader: using URLDownload in one case. 3 more to go. Not tested yet commit 9b26e9cfac425635027189ccab835054b68bc83d Merge: 20fe063 253d9c4 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu Jul 30 08:35:53 2015 -0700 Merge branch 'v3' into cocos_net commit 20fe0636e36ae7f1da048545b3345f526d5b64de Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Jul 29 17:36:07 2015 -0700 URLDownload: initial commit HttpAsyncConnection -> HttpAsyncConnection-apple: follows the cocos2d guidelines Adds `CCIRULDownload` interface. Adds `CCURLDownload` implementation based on libcurl. Not working yet. `Downloader` uses `URLDownload` for some of its functions. WIP commit 3a5d3b83838bee53e3bbea182f3ad09d7bec32d0 Merge: e408fe0 642ccac Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Jul 29 14:00:43 2015 -0700 Merge remote-tracking branch 'cocos2d/v3' into cocos_net commit e408fe0b9303813d20bab2f7bf548f3b7af416a6 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Jul 29 13:59:46 2015 -0700 Adds more files to Xcode project ...even if they are not being compiled. Much easier to add missing functionality to the rest of the platforms commit 11d86acebeb61348e4ccf7e6af9a2eaf1b6bb534 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Jul 29 11:36:57 2015 -0700 removes unneeded headers
2015-08-13 15:14:10 +08:00
_downloader = std::shared_ptr<network::Downloader>(new network::Downloader);
// _downloader->setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT);
2015-09-15 11:23:46 +08:00
_downloader->onTaskError = bind(&AssetsManagerEx::onError, this, placeholders::_1, placeholders::_2, placeholders::_3, placeholders::_4);
_downloader->onTaskProgress = [this](const network::DownloadTask& task,
int64_t bytesReceived,
int64_t totalBytesReceived,
int64_t totalBytesExpected)
{
this->onProgress(totalBytesExpected, totalBytesReceived, task.requestURL, task.identifier);
};
_downloader->onFileTaskSuccess = [this](const network::DownloadTask& task)
{
this->onSuccess(task.requestURL, task.storagePath, task.identifier);
};
2014-10-09 18:30:39 +08:00
setStoragePath(storagePath);
_cacheVersionPath = _storagePath + VERSION_FILENAME;
_cacheManifestPath = _storagePath + MANIFEST_FILENAME;
_tempManifestPath = _storagePath + TEMP_MANIFEST_FILENAME;
2014-11-26 14:39:56 +08:00
initManifests(manifestUrl);
2014-10-09 18:30:39 +08:00
}
AssetsManagerEx::~AssetsManagerEx()
{
2015-09-15 11:23:46 +08:00
_downloader->onTaskError = (nullptr);
_downloader->onFileTaskSuccess = (nullptr);
_downloader->onTaskProgress = (nullptr);
2014-10-09 18:30:39 +08:00
CC_SAFE_RELEASE(_localManifest);
// _tempManifest could share a ptr with _remoteManifest or _localManifest
if (_tempManifest != _localManifest && _tempManifest != _remoteManifest)
CC_SAFE_RELEASE(_tempManifest);
CC_SAFE_RELEASE(_remoteManifest);
}
AssetsManagerEx* AssetsManagerEx::create(const std::string& manifestUrl, const std::string& storagePath)
{
2014-11-26 14:39:56 +08:00
AssetsManagerEx* ret = new (std::nothrow) AssetsManagerEx(manifestUrl, storagePath);
2014-10-09 18:30:39 +08:00
if (ret)
{
ret->autorelease();
}
else
{
CC_SAFE_DELETE(ret);
}
return ret;
}
2014-11-26 14:39:56 +08:00
void AssetsManagerEx::initManifests(const std::string& manifestUrl)
{
_inited = true;
// Init and load local manifest
_localManifest = new (std::nothrow) Manifest();
if (_localManifest)
{
loadLocalManifest(manifestUrl);
// Init and load temporary manifest
_tempManifest = new (std::nothrow) Manifest();
if (_tempManifest)
{
_tempManifest->parse(_tempManifestPath);
if (!_tempManifest->isLoaded() && _fileUtils->isFileExist(_tempManifestPath))
2014-11-26 14:39:56 +08:00
_fileUtils->removeFile(_tempManifestPath);
}
else
{
_inited = false;
}
// Init remote manifest for future usage
_remoteManifest = new (std::nothrow) Manifest();
if (!_remoteManifest)
{
_inited = false;
}
}
else
{
_inited = false;
}
if (!_inited)
{
CC_SAFE_DELETE(_localManifest);
CC_SAFE_DELETE(_tempManifest);
CC_SAFE_DELETE(_remoteManifest);
}
}
2014-10-09 18:30:39 +08:00
void AssetsManagerEx::prepareLocalManifest()
{
// An alias to assets
_assets = &(_localManifest->getAssets());
// Add search paths
_localManifest->prependSearchPaths();
}
void AssetsManagerEx::loadLocalManifest(const std::string& manifestUrl)
{
Manifest *cachedManifest = nullptr;
// Find the cached manifest file
2014-10-09 18:30:39 +08:00
if (_fileUtils->isFileExist(_cacheManifestPath))
{
2014-11-14 10:44:18 +08:00
cachedManifest = new (std::nothrow) Manifest();
2014-11-26 14:39:56 +08:00
if (cachedManifest) {
cachedManifest->parse(_cacheManifestPath);
if (!cachedManifest->isLoaded())
{
_fileUtils->removeFile(_cacheManifestPath);
CC_SAFE_RELEASE(cachedManifest);
cachedManifest = nullptr;
}
}
2014-10-09 18:30:39 +08:00
}
// Load local manifest in app package
_localManifest->parse(_manifestUrl);
if (_localManifest->isLoaded())
2014-10-09 18:30:39 +08:00
{
// Compare with cached manifest to determine which one to use
if (cachedManifest) {
if (strcmp(_localManifest->getVersion().c_str(), cachedManifest->getVersion().c_str()) > 0)
{
// Recreate storage, to empty the content
_fileUtils->removeDirectory(_storagePath);
_fileUtils->createDirectory(_storagePath);
CC_SAFE_RELEASE(cachedManifest);
}
else
{
CC_SAFE_RELEASE(_localManifest);
_localManifest = cachedManifest;
}
}
prepareLocalManifest();
2014-10-09 18:30:39 +08:00
}
// Fail to load local manifest
if (!_localManifest->isLoaded())
{
CCLOG("AssetsManagerEx : No local manifest file found error.\n");
dispatchUpdateEvent(EventAssetsManagerEx::EventCode::ERROR_NO_LOCAL_MANIFEST);
}
}
std::string AssetsManagerEx::basename(const std::string& path) const
{
size_t found = path.find_last_of("/\\");
if (std::string::npos != found)
{
return path.substr(0, found);
}
else
{
return path;
}
}
std::string AssetsManagerEx::get(const std::string& key) const
{
auto it = _assets->find(key);
if (it != _assets->cend()) {
return _storagePath + it->second.path;
}
else return "";
}
const Manifest* AssetsManagerEx::getLocalManifest() const
{
return _localManifest;
}
const Manifest* AssetsManagerEx::getRemoteManifest() const
{
return _remoteManifest;
}
const std::string& AssetsManagerEx::getStoragePath() const
{
return _storagePath;
}
void AssetsManagerEx::setStoragePath(const std::string& storagePath)
{
_storagePath = storagePath;
adjustPath(_storagePath);
_fileUtils->createDirectory(_storagePath);
}
void AssetsManagerEx::adjustPath(std::string &path)
{
if (path.size() > 0 && path[path.size() - 1] != '/')
{
path.append("/");
}
}
bool AssetsManagerEx::decompress(const std::string &zip)
{
// Find root path for zip file
size_t pos = zip.find_last_of("/\\");
if (pos == std::string::npos)
{
CCLOG("AssetsManagerEx : no root path specified for zip file %s\n", zip.c_str());
return false;
}
const std::string rootPath = zip.substr(0, pos+1);
// Open the zip file
unzFile zipfile = unzOpen(FileUtils::getInstance()->getSuitableFOpen(zip).c_str());
2014-10-09 18:30:39 +08:00
if (! zipfile)
{
CCLOG("AssetsManagerEx : can not open downloaded zip file %s\n", zip.c_str());
return false;
}
// Get info about the zip file
unz_global_info global_info;
if (unzGetGlobalInfo(zipfile, &global_info) != UNZ_OK)
{
CCLOG("AssetsManagerEx : can not read file global info of %s\n", zip.c_str());
unzClose(zipfile);
return false;
}
// Buffer to hold data read from the zip file
char readBuffer[BUFFER_SIZE];
// 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,
NULL,
0,
NULL,
0) != UNZ_OK)
{
CCLOG("AssetsManagerEx : can not read compressed file info\n");
unzClose(zipfile);
return false;
}
const std::string fullPath = rootPath + fileName;
// Check if this entry is a directory or a file.
const size_t filenameLength = strlen(fileName);
if (fileName[filenameLength-1] == '/')
{
//There are not directory entry in some case.
//So we need to create directory when decompressing file entry
if ( !_fileUtils->createDirectory(basename(fullPath)) )
{
// Failed to create directory
CCLOG("AssetsManagerEx : can not create directory %s\n", fullPath.c_str());
unzClose(zipfile);
return false;
}
}
else
{
// Entry is a file, so extract it.
// Open current file.
if (unzOpenCurrentFile(zipfile) != UNZ_OK)
{
CCLOG("AssetsManagerEx : can not extract file %s\n", fileName);
unzClose(zipfile);
return false;
}
// Create a file to store current file.
FILE *out = fopen(FileUtils::getInstance()->getSuitableFOpen(fullPath).c_str(), "wb");
2014-10-09 18:30:39 +08:00
if (!out)
{
CCLOG("AssetsManagerEx : can not create decompress destination file %s\n", 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)
{
CCLOG("AssetsManagerEx : can not read zip file %s, error code is %d\n", fileName, error);
fclose(out);
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("AssetsManagerEx : can not read next file for decompressing\n");
unzClose(zipfile);
return false;
}
}
}
unzClose(zipfile);
return true;
}
void AssetsManagerEx::decompressDownloadedZip()
{
// Decompress all compressed files
for (auto it = _compressedFiles.begin(); it != _compressedFiles.end(); ++it) {
std::string zipfile = *it;
if (!decompress(zipfile))
{
dispatchUpdateEvent(EventAssetsManagerEx::EventCode::ERROR_DECOMPRESS, "", "Unable to decompress file " + zipfile);
}
_fileUtils->removeFile(zipfile);
}
_compressedFiles.clear();
}
void AssetsManagerEx::dispatchUpdateEvent(EventAssetsManagerEx::EventCode code, const std::string &assetId/* = ""*/, const std::string &message/* = ""*/, int curle_code/* = CURLE_OK*/, int curlm_code/* = CURLM_OK*/)
{
EventAssetsManagerEx event(_eventName, this, code, _percent, _percentByFile, assetId, message, curle_code, curlm_code);
_eventDispatcher->dispatchEvent(&event);
}
AssetsManagerEx::State AssetsManagerEx::getState() const
{
return _updateState;
}
void AssetsManagerEx::downloadVersion()
{
if (_updateState > State::PREDOWNLOAD_VERSION)
2014-10-09 18:30:39 +08:00
return;
std::string versionUrl = _localManifest->getVersionFileUrl();
if (versionUrl.size() > 0)
{
_updateState = State::DOWNLOADING_VERSION;
// Download version file asynchronously
2015-09-15 11:23:46 +08:00
_downloader->createDownloadFileTask(versionUrl, _cacheVersionPath, VERSION_ID);
2014-10-09 18:30:39 +08:00
}
// No version file found
else
{
CCLOG("AssetsManagerEx : No version file found, step skipped\n");
_updateState = State::PREDOWNLOAD_MANIFEST;
downloadManifest();
}
}
void AssetsManagerEx::parseVersion()
{
if (_updateState != State::VERSION_LOADED)
return;
_remoteManifest->parseVersion(_cacheVersionPath);
2014-10-09 18:30:39 +08:00
if (!_remoteManifest->isVersionLoaded())
{
CCLOG("AssetsManagerEx : Fail to parse version file, step skipped\n");
_updateState = State::PREDOWNLOAD_MANIFEST;
downloadManifest();
}
else
{
if (_localManifest->versionEquals(_remoteManifest))
{
_updateState = State::UP_TO_DATE;
dispatchUpdateEvent(EventAssetsManagerEx::EventCode::ALREADY_UP_TO_DATE);
}
else
{
_updateState = State::NEED_UPDATE;
dispatchUpdateEvent(EventAssetsManagerEx::EventCode::NEW_VERSION_FOUND);
// Wait to update so continue the process
if (_waitToUpdate)
{
_updateState = State::PREDOWNLOAD_MANIFEST;
downloadManifest();
}
}
}
}
void AssetsManagerEx::downloadManifest()
{
if (_updateState != State::PREDOWNLOAD_MANIFEST)
return;
std::string manifestUrl = _localManifest->getManifestFileUrl();
if (manifestUrl.size() > 0)
{
_updateState = State::DOWNLOADING_MANIFEST;
// Download version file asynchronously
2015-09-15 11:23:46 +08:00
_downloader->createDownloadFileTask(manifestUrl, _tempManifestPath, MANIFEST_ID);
2014-10-09 18:30:39 +08:00
}
// No manifest file found
else
{
CCLOG("AssetsManagerEx : No manifest file found, check update failed\n");
dispatchUpdateEvent(EventAssetsManagerEx::EventCode::ERROR_DOWNLOAD_MANIFEST);
_updateState = State::UNCHECKED;
}
}
void AssetsManagerEx::parseManifest()
{
if (_updateState != State::MANIFEST_LOADED)
return;
_remoteManifest->parse(_tempManifestPath);
if (!_remoteManifest->isLoaded())
{
CCLOG("AssetsManagerEx : Error parsing manifest file\n");
dispatchUpdateEvent(EventAssetsManagerEx::EventCode::ERROR_PARSE_MANIFEST);
_updateState = State::UNCHECKED;
}
else
{
if (_localManifest->versionEquals(_remoteManifest))
{
_updateState = State::UP_TO_DATE;
dispatchUpdateEvent(EventAssetsManagerEx::EventCode::ALREADY_UP_TO_DATE);
}
else
{
_updateState = State::NEED_UPDATE;
dispatchUpdateEvent(EventAssetsManagerEx::EventCode::NEW_VERSION_FOUND);
if (_waitToUpdate)
{
startUpdate();
}
}
}
}
void AssetsManagerEx::startUpdate()
{
if (_updateState != State::NEED_UPDATE)
return;
_updateState = State::UPDATING;
// Clean up before update
_failedUnits.clear();
_downloadUnits.clear();
_compressedFiles.clear();
_totalWaitToDownload = _totalToDownload = 0;
_percent = _percentByFile = _sizeCollected = _totalSize = 0;
_downloadedSize.clear();
_totalEnabled = false;
// Temporary manifest exists, resuming previous download
if (_tempManifest->isLoaded() && _tempManifest->versionEquals(_remoteManifest))
{
_tempManifest->genResumeAssetsList(&_downloadUnits);
_totalWaitToDownload = _totalToDownload = (int)_downloadUnits.size();
2015-09-15 11:23:46 +08:00
this->batchDownload();
2014-10-09 18:30:39 +08:00
std::string msg = StringUtils::format("Resuming from previous unfinished update, %d files remains to be finished.", _totalToDownload);
dispatchUpdateEvent(EventAssetsManagerEx::EventCode::UPDATE_PROGRESSION, "", msg);
}
// Check difference
else
{
// Temporary manifest not exists or out of date,
2014-10-09 18:30:39 +08:00
// it will be used to register the download states of each asset,
// in this case, it equals remote manifest.
_tempManifest->release();
_tempManifest = _remoteManifest;
2014-10-09 18:30:39 +08:00
std::unordered_map<std::string, Manifest::AssetDiff> diff_map = _localManifest->genDiff(_remoteManifest);
if (diff_map.size() == 0)
{
updateSucceed();
2014-10-09 18:30:39 +08:00
}
else
{
// Generate download units for all assets that need to be updated or added
std::string packageUrl = _remoteManifest->getPackageUrl();
for (auto it = diff_map.begin(); it != diff_map.end(); ++it)
{
Manifest::AssetDiff diff = it->second;
if (diff.type == Manifest::DiffType::DELETED)
{
_fileUtils->removeFile(_storagePath + diff.asset.path);
}
else
{
std::string path = diff.asset.path;
// Create path
_fileUtils->createDirectory(basename(_storagePath + path));
2015-09-15 11:23:46 +08:00
DownloadUnit unit;
2014-10-09 18:30:39 +08:00
unit.customId = it->first;
unit.srcUrl = packageUrl + path;
unit.storagePath = _storagePath + path;
_downloadUnits.emplace(unit.customId, unit);
}
}
// Set other assets' downloadState to SUCCESSED
auto &assets = _remoteManifest->getAssets();
2014-10-09 18:30:39 +08:00
for (auto it = assets.cbegin(); it != assets.cend(); ++it)
{
const std::string &key = it->first;
auto diffIt = diff_map.find(key);
if (diffIt == diff_map.end())
{
_tempManifest->setAssetDownloadState(key, Manifest::DownloadState::SUCCESSED);
}
}
_totalWaitToDownload = _totalToDownload = (int)_downloadUnits.size();
2015-09-15 11:23:46 +08:00
this->batchDownload();
2014-10-09 18:30:39 +08:00
std::string msg = StringUtils::format("Start to update %d files from remote package.", _totalToDownload);
dispatchUpdateEvent(EventAssetsManagerEx::EventCode::UPDATE_PROGRESSION, "", msg);
}
}
_waitToUpdate = false;
}
void AssetsManagerEx::updateSucceed()
{
// Every thing is correctly downloaded, do the following
// 1. rename temporary manifest to valid manifest
_fileUtils->renameFile(_storagePath, TEMP_MANIFEST_FILENAME, MANIFEST_FILENAME);
// 2. swap the localManifest
if (_localManifest != nullptr)
_localManifest->release();
_localManifest = _remoteManifest;
_remoteManifest = nullptr;
// 3. make local manifest take effect
prepareLocalManifest();
_updateState = State::UNZIPPING;
2014-10-09 18:30:39 +08:00
// 4. decompress all compressed files
//decompressDownloadedZip();
struct AsyncData
{
std::vector<std::string> compressedFiles;
std::string errorCompressedFile;
};
AsyncData* asyncData = new AsyncData;
asyncData->compressedFiles = _compressedFiles;
_compressedFiles.clear();
std::function<void(void*)> mainThread = [this](void* param) {
AsyncData* asyncData = (AsyncData*)param;
if (asyncData->errorCompressedFile.empty())
{
// 5. Set update state
_updateState = State::UP_TO_DATE;
// 6. Notify finished event
dispatchUpdateEvent(EventAssetsManagerEx::EventCode::UPDATE_FINISHED);
}
else
{
_updateState = State::FAIL_TO_UPDATE;
dispatchUpdateEvent(EventAssetsManagerEx::EventCode::ERROR_DECOMPRESS, "", "Unable to decompress file " + asyncData->errorCompressedFile);
}
delete asyncData;
};
AsyncTaskPool::getInstance()->enqueue(AsyncTaskPool::TaskType::TASK_OTHER, mainThread, (void*)asyncData, [this, asyncData]() {
// Decompress all compressed files
for (auto& zipFile : asyncData->compressedFiles) {
if (!decompress(zipFile))
{
asyncData->errorCompressedFile = zipFile;
break;
}
_fileUtils->removeFile(zipFile);
}
});
2014-10-09 18:30:39 +08:00
}
void AssetsManagerEx::checkUpdate()
{
2014-11-26 14:39:56 +08:00
if (!_inited){
CCLOG("AssetsManagerEx : Manifests uninited.\n");
dispatchUpdateEvent(EventAssetsManagerEx::EventCode::ERROR_NO_LOCAL_MANIFEST);
return;
}
2014-10-09 18:30:39 +08:00
if (!_localManifest->isLoaded())
{
CCLOG("AssetsManagerEx : No local manifest file found error.\n");
dispatchUpdateEvent(EventAssetsManagerEx::EventCode::ERROR_NO_LOCAL_MANIFEST);
return;
}
switch (_updateState) {
case State::UNCHECKED:
case State::PREDOWNLOAD_VERSION:
{
downloadVersion();
}
break;
case State::UP_TO_DATE:
{
dispatchUpdateEvent(EventAssetsManagerEx::EventCode::ALREADY_UP_TO_DATE);
}
break;
case State::FAIL_TO_UPDATE:
case State::NEED_UPDATE:
{
dispatchUpdateEvent(EventAssetsManagerEx::EventCode::NEW_VERSION_FOUND);
}
break;
default:
break;
}
}
void AssetsManagerEx::update()
{
2014-11-26 14:39:56 +08:00
if (!_inited){
CCLOG("AssetsManagerEx : Manifests uninited.\n");
dispatchUpdateEvent(EventAssetsManagerEx::EventCode::ERROR_NO_LOCAL_MANIFEST);
return;
}
2014-10-09 18:30:39 +08:00
if (!_localManifest->isLoaded())
{
CCLOG("AssetsManagerEx : No local manifest file found error.\n");
dispatchUpdateEvent(EventAssetsManagerEx::EventCode::ERROR_NO_LOCAL_MANIFEST);
return;
}
_waitToUpdate = true;
switch (_updateState) {
case State::UNCHECKED:
{
_updateState = State::PREDOWNLOAD_VERSION;
}
case State::PREDOWNLOAD_VERSION:
{
downloadVersion();
}
break;
case State::VERSION_LOADED:
{
parseVersion();
}
break;
case State::PREDOWNLOAD_MANIFEST:
{
downloadManifest();
}
break;
case State::MANIFEST_LOADED:
{
parseManifest();
}
break;
case State::FAIL_TO_UPDATE:
case State::NEED_UPDATE:
{
// Manifest not loaded yet
if (!_remoteManifest->isLoaded())
{
_waitToUpdate = true;
_updateState = State::PREDOWNLOAD_MANIFEST;
downloadManifest();
}
else
{
startUpdate();
}
}
break;
case State::UP_TO_DATE:
case State::UPDATING:
case State::UNZIPPING:
2014-10-09 18:30:39 +08:00
_waitToUpdate = false;
break;
default:
break;
}
}
2015-09-15 11:23:46 +08:00
void AssetsManagerEx::updateAssets(const DownloadUnits& assets)
2014-10-09 18:30:39 +08:00
{
2014-11-26 14:39:56 +08:00
if (!_inited){
CCLOG("AssetsManagerEx : Manifests uninited.\n");
dispatchUpdateEvent(EventAssetsManagerEx::EventCode::ERROR_NO_LOCAL_MANIFEST);
return;
}
2014-10-09 18:30:39 +08:00
if (_updateState != State::UPDATING && _localManifest->isLoaded() && _remoteManifest->isLoaded())
{
int size = (int)(assets.size());
if (size > 0)
{
_updateState = State::UPDATING;
_downloadUnits.clear();
_downloadUnits = assets;
_totalWaitToDownload = _totalToDownload = (int)_downloadUnits.size();
2015-09-15 11:23:46 +08:00
this->batchDownload();
2014-10-09 18:30:39 +08:00
}
else if (size == 0 && _totalWaitToDownload == 0)
{
updateSucceed();
}
}
}
2015-09-15 11:23:46 +08:00
const DownloadUnits& AssetsManagerEx::getFailedAssets() const
2014-10-09 18:30:39 +08:00
{
return _failedUnits;
}
void AssetsManagerEx::downloadFailedAssets()
{
2016-04-18 17:52:58 +08:00
CCLOG("AssetsManagerEx : Start update %lu failed assets.\n", static_cast<unsigned long>(_failedUnits.size()));
2014-10-09 18:30:39 +08:00
updateAssets(_failedUnits);
}
2015-09-15 11:23:46 +08:00
void AssetsManagerEx::onError(const network::DownloadTask& task,
int errorCode,
int errorCodeInternal,
const std::string& errorStr)
{
// Skip version error occurred
2015-09-15 11:23:46 +08:00
if (task.identifier == VERSION_ID)
{
CCLOG("AssetsManagerEx : Fail to download version file, step skipped\n");
_updateState = State::PREDOWNLOAD_MANIFEST;
downloadManifest();
}
else if (task.identifier == MANIFEST_ID)
{
dispatchUpdateEvent(EventAssetsManagerEx::EventCode::ERROR_DOWNLOAD_MANIFEST, task.identifier, errorStr, errorCode, errorCodeInternal);
}
else
{
auto unitIt = _downloadUnits.find(task.identifier);
// Found unit and add it to failed units
if (unitIt != _downloadUnits.end())
{
_totalWaitToDownload--;
2015-09-15 11:23:46 +08:00
DownloadUnit unit = unitIt->second;
_failedUnits.emplace(unit.customId, unit);
}
dispatchUpdateEvent(EventAssetsManagerEx::EventCode::ERROR_UPDATING, task.identifier, errorStr, errorCode, errorCodeInternal);
if (_totalWaitToDownload <= 0)
{
this->onDownloadUnitsFinished();
}
2015-09-15 11:23:46 +08:00
}
}
2014-10-09 18:30:39 +08:00
void AssetsManagerEx::onProgress(double total, double downloaded, const std::string &url, const std::string &customId)
{
if (customId == VERSION_ID || customId == MANIFEST_ID)
{
2015-01-23 12:10:32 +08:00
_percent = 100 * downloaded / total;
2014-10-09 18:30:39 +08:00
// Notify progression event
dispatchUpdateEvent(EventAssetsManagerEx::EventCode::UPDATE_PROGRESSION, customId);
return;
}
else
{
// Calcul total downloaded
bool found = false;
double totalDownloaded = 0;
for (auto it = _downloadedSize.begin(); it != _downloadedSize.end(); ++it)
{
if (it->first == customId)
{
it->second = downloaded;
found = true;
}
totalDownloaded += it->second;
}
// Collect information if not registed
if (!found)
{
// Set download state to DOWNLOADING, this will run only once in the download process
_tempManifest->setAssetDownloadState(customId, Manifest::DownloadState::DOWNLOADING);
// Register the download size information
_downloadedSize.emplace(customId, downloaded);
_totalSize += total;
_sizeCollected++;
// All collected, enable total size
if (_sizeCollected == _totalToDownload)
{
_totalEnabled = true;
}
}
if (_totalEnabled && _updateState == State::UPDATING)
{
float currentPercent = 100 * totalDownloaded / _totalSize;
// Notify at integer level change
if ((int)currentPercent != (int)_percent) {
_percent = currentPercent;
// Notify progression event
2015-01-23 12:10:32 +08:00
dispatchUpdateEvent(EventAssetsManagerEx::EventCode::UPDATE_PROGRESSION, customId);
2014-10-09 18:30:39 +08:00
}
}
}
}
void AssetsManagerEx::onSuccess(const std::string &srcUrl, const std::string &storagePath, const std::string &customId)
{
if (customId == VERSION_ID)
{
_updateState = State::VERSION_LOADED;
parseVersion();
}
else if (customId == MANIFEST_ID)
{
_updateState = State::MANIFEST_LOADED;
parseManifest();
}
else
{
auto &assets = _remoteManifest->getAssets();
2014-10-09 18:30:39 +08:00
auto assetIt = assets.find(customId);
if (assetIt != assets.end())
{
// Set download state to SUCCESSED
_tempManifest->setAssetDownloadState(customId, Manifest::DownloadState::SUCCESSED);
// Add file to need decompress list
if (assetIt->second.compressed) {
_compressedFiles.push_back(storagePath);
}
}
auto unitIt = _downloadUnits.find(customId);
if (unitIt != _downloadUnits.end())
{
// Reduce count only when unit found in _downloadUnits
_totalWaitToDownload--;
_percentByFile = 100 * (float)(_totalToDownload - _totalWaitToDownload) / _totalToDownload;
// Notify progression event
dispatchUpdateEvent(EventAssetsManagerEx::EventCode::UPDATE_PROGRESSION, "");
}
// Notify asset updated event
dispatchUpdateEvent(EventAssetsManagerEx::EventCode::ASSET_UPDATED, customId);
unitIt = _failedUnits.find(customId);
// Found unit and delete it
if (unitIt != _failedUnits.end())
{
// Remove from failed units list
_failedUnits.erase(unitIt);
}
2015-09-15 11:23:46 +08:00
if (_totalWaitToDownload <= 0)
{
this->onDownloadUnitsFinished();
2015-09-15 11:23:46 +08:00
}
2014-10-09 18:30:39 +08:00
}
}
void AssetsManagerEx::destroyDownloadedVersion()
{
_fileUtils->removeFile(_cacheVersionPath);
_fileUtils->removeFile(_cacheManifestPath);
}
2015-09-15 11:23:46 +08:00
void AssetsManagerEx::batchDownload()
{
for(auto iter : _downloadUnits)
{
DownloadUnit& unit = iter.second;
_downloader->createDownloadFileTask(unit.srcUrl, unit.storagePath, unit.customId);
}
}
void AssetsManagerEx::onDownloadUnitsFinished()
{
// Finished with error check
if (_failedUnits.size() > 0)
{
// Save current download manifest information for resuming
_tempManifest->saveToFile(_tempManifestPath);
decompressDownloadedZip();
_updateState = State::FAIL_TO_UPDATE;
dispatchUpdateEvent(EventAssetsManagerEx::EventCode::UPDATE_FAILED);
}
else
{
updateSucceed();
}
}
2014-10-09 18:30:39 +08:00
NS_CC_EXT_END