Created
January 12, 2017 03:11
-
-
Save kkabdol/26c66c043dd43439aea5589d7be33873 to your computer and use it in GitHub Desktop.
Code Reading
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void AssetsManager::downloadAndUncompress() | |
| { | |
| std::thread([this]() | |
| { | |
| do | |
| { | |
| // 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; | |
| } | |
| Director::getInstance()->getScheduler()->performFunctionInCocosThread([&, this] { | |
| // Record new version code. | |
| UserDefault::getInstance()->setStringForKey(this->keyOfVersion().c_str(), this->_version.c_str()); | |
| // Unrecord downloaded version code. | |
| UserDefault::getInstance()->setStringForKey(this->keyOfDownloadedVersion().c_str(), ""); | |
| UserDefault::getInstance()->flush(); | |
| // 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(); | |
| }); | |
| } while (0); | |
| _isDownloading = false; | |
| }).detach(); | |
| } | |
| AssetsManager* AssetsManager::create(const char* packageUrl, const char* versionFileUrl, const char* storagePath, ErrorCallback errorCallback, ProgressCallback progressCallback, SuccessCallback successCallback ) | |
| { | |
| class DelegateProtocolImpl : public AssetsManagerDelegateProtocol | |
| { | |
| public : | |
| DelegateProtocolImpl(ErrorCallback aErrorCallback, ProgressCallback aProgressCallback, SuccessCallback aSuccessCallback) | |
| : errorCallback(aErrorCallback), progressCallback(aProgressCallback), successCallback(aSuccessCallback) | |
| {} | |
| 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; | |
| }; | |
| auto* manager = new (std::nothrow) AssetsManager(packageUrl,versionFileUrl,storagePath); | |
| auto* delegate = new (std::nothrow) DelegateProtocolImpl(errorCallback,progressCallback,successCallback); | |
| manager->setDelegate(delegate); | |
| manager->_shouldDeleteDelegateWhenExit = true; | |
| manager->autorelease(); | |
| return manager; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment