#include "cocos-ext.h"
bool initUpdate(); void menuUpdateCallback(Ref* sender); void updateEnd(); void onError(int nErrCode); void onProgress(int nProgress); void onSuccess(); private: cocos2d::LabelTTF *m_labelUpdate; cocos2d::extension::AssetsManager *m_pAssetsMgr;
bool HelloWorld::initUpdate() { auto visibleSize = Director::getInstance()->getVisibleSize(); auto origin = Director::getInstance()->getVisibleOrigin(); auto updateItem = MenuItemImage::create( "CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(HelloWorld::menuUpdateCallback, this)); updateItem->setPosition(origin + Vec2(visibleSize / 2)); // create menu, it's an autorelease object auto menu = Menu::create(updateItem, nullptr); menu->setPosition(Vec2::ZERO); this->addChild(menu, 2); m_labelUpdate = LabelTTF::create("Ready", "Arial", 30); // position the label on the center of the screen m_labelUpdate->setPosition(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2 + m_labelUpdate->getContentSize().height); // add the label as a child to this layer this->addChild(m_labelUpdate, 2); return true; } void HelloWorld::menuUpdateCallback(Ref* sender) { m_labelUpdate->setString("Updating..."); std::string packageUrl = "http://xz.cr173.com/soft1/VA_X_Setup.zip"; std::string versionFileUrl = "http://xz.cr173.com/soft1/VA_X_Setup.zip"; // 版本在AssetsManager内部写死,测试用 // 需要自己处理完整路径,所以直接获取一个可写路径 内部不会判断文件夹是否存在,所以需要确认文件夹存在 std::string storegePath = FileUtils::getInstance()->getWritablePath(); // create create之后是autorelease的,所以需要retain m_pAssetsMgr = extension::AssetsManager::create( packageUrl.c_str(), versionFileUrl.c_str(), storegePath.c_str(), CC_CALLBACK_1(HelloWorld::onError, this), CC_CALLBACK_1(HelloWorld::onProgress, this), CC_CALLBACK_0(HelloWorld::onSuccess, this)); m_pAssetsMgr->retain(); // retain // check if (m_pAssetsMgr->checkUpdate()) { // update m_pAssetsMgr->update(); } } void HelloWorld::updateEnd() { m_pAssetsMgr->release(); } void HelloWorld::onError(int nErrCode) { char sTemp[256] = { 0 }; snprintf(sTemp, 256, "Update Error! ErrCode:%d", nErrCode); extension::AssetsManager::ErrorCode errCode = (extension::AssetsManager::ErrorCode)nErrCode; switch (errCode) { case extension::AssetsManager::ErrorCode::NETWORK: snprintf(sTemp, 256, "Update Error! NETWORK"); break; case extension::AssetsManager::ErrorCode::CREATE_FILE: snprintf(sTemp, 256, "Update Error! CREATE_FILE"); break; case extension::AssetsManager::ErrorCode::NO_NEW_VERSION: snprintf(sTemp, 256, "Update Error! NO_NEW_VERSION"); break; case extension::AssetsManager::ErrorCode::UNCOMPRESS: snprintf(sTemp, 256, "Update Error! UNCOMPRESS"); break; default: break; } std::string label = sTemp; m_labelUpdate->setString(label); updateEnd(); } void HelloWorld::onProgress(int nProgress) { char sTemp[256] = { 0 }; snprintf(sTemp, 256, "Progress:%d", nProgress); std::string label = sTemp; m_labelUpdate->setString(label); } void HelloWorld::onSuccess() { m_labelUpdate->setString("Update Succeed!"); updateEnd(); }
initUpdate();
void AssetsManager::setSearchPath() { vector<string> searchPaths = FileUtils::getInstance()->getSearchPaths(); vector<string>::iterator iter = searchPaths.begin(); searchPaths.insert(iter, _storagePath); // 将更新的路径添加到搜索路径的最前面,保证先搜索到更新的文件 FileUtils::getInstance()->setSearchPaths(searchPaths); }
参考:http://www.cnblogs.com/hll2008/p/4227277.html
我们在用AssetsManager做资源更新的时候遇到了一个问题,部分玩家热更新失败。查了好久才查出来,原来是我们的资源文件被宽带提供商(我们发现BUG的是华数网通、部分手机3G)缓存了这个资源文件,并对我们的请求链接做了跳转。解决办法在AssetsManager源代码中curl的设置加上一句
curl_easy_setopt(_curl, CURLOPT_FOLLOWLOCATION, 1); //设置成更随跳转
这个问题在3.3版本已经解决。
参考:http://www.cocoachina.com/bbs/read.php?tid=233044