axmol/tests/cpp-tests/Classes/ExtensionsTest/AssetsManagerExTest/AssetsManagerExTest.cpp

224 lines
8.2 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-28 16:06:23 +08:00
https://axis-project.github.io/
2021-12-28 16:06:23 +08:00
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:
2021-12-28 16:06:23 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-28 16:06:23 +08:00
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.
****************************************************************************/
2014-10-09 18:30:39 +08:00
#include "AssetsManagerExTest.h"
#include "../../testResource.h"
#include "cocos2d.h"
USING_NS_AX;
USING_NS_AX_EXT;
2015-04-09 12:23:47 +08:00
2021-12-28 16:06:23 +08:00
const char* sceneManifests[] = {"Manifests/AMTestScene1/project.manifest", "Manifests/AMTestScene2/project.manifest",
"Manifests/AMTestScene3/project.manifest"};
const char* storagePaths[] = {"CppTests/AssetsManagerExTest/scene1/", "CppTests/AssetsManagerExTest/scene2/",
"CppTests/AssetsManagerExTest/scene3"};
const char* backgroundPaths[] = {"Images/assetMgrBackground1.jpg", "Images/assetMgrBackground2.png",
"Images/assetMgrBackground3.png"};
2014-10-09 18:30:39 +08:00
AssetsManagerExTests::AssetsManagerExTests()
2014-10-09 18:30:39 +08:00
{
2021-12-28 16:06:23 +08:00
addTestCase("AssetsManager Test1", []() { return AssetsManagerExLoaderScene::create(0); });
addTestCase("AssetsManager Test2", []() { return AssetsManagerExLoaderScene::create(1); });
addTestCase("AssetsManager Test3", []() { return AssetsManagerExLoaderScene::create(2); });
2014-10-09 18:30:39 +08:00
}
AssetsManagerExLoaderScene* AssetsManagerExLoaderScene::create(int testIndex)
2014-10-09 18:30:39 +08:00
{
2021-12-08 00:11:53 +08:00
auto scene = new AssetsManagerExLoaderScene(testIndex);
if (scene->init())
2014-10-09 18:30:39 +08:00
{
scene->autorelease();
2014-10-09 18:30:39 +08:00
}
else
{
delete scene;
scene = nullptr;
2014-10-09 18:30:39 +08:00
}
return scene;
2014-10-09 18:30:39 +08:00
}
AssetsManagerExLoaderScene::AssetsManagerExLoaderScene(int testIndex)
2021-12-28 16:06:23 +08:00
: _testIndex(testIndex)
, _loadLayer(nullptr)
, _downloadMenu(nullptr)
, _am(nullptr)
, _progress(nullptr)
, _amListener(nullptr)
{}
2014-10-09 18:30:39 +08:00
bool AssetsManagerExLoaderScene::init()
2014-10-09 18:30:39 +08:00
{
if (!TestCase::init())
{
return false;
}
2014-10-09 18:30:39 +08:00
auto downloadLabel = Label::createWithTTF("Start Download", "fonts/arial.ttf", 16);
2021-12-28 16:06:23 +08:00
auto downloadItem =
2022-07-15 19:17:01 +08:00
MenuItemLabel::create(downloadLabel, AX_CALLBACK_1(AssetsManagerExLoaderScene::startDownloadCallback, this));
downloadItem->setPosition(Vec2(VisibleRect::center().x, VisibleRect::bottom().y + 100));
_downloadMenu = Menu::create(downloadItem, nullptr);
_downloadMenu->setPosition(Vec2::ZERO);
this->addChild(_downloadMenu, 1);
_loadLayer = Layer::create();
addChild(_loadLayer);
auto sprite = Sprite::create("Images/Icon.png");
_loadLayer->addChild(sprite);
2021-12-28 16:06:23 +08:00
sprite->setPosition(VisibleRect::center());
2014-10-09 18:30:39 +08:00
TTFConfig config("fonts/tahoma.ttf", 30);
_progress = Label::createWithTTF(config, "0%", TextHAlignment::CENTER);
2021-12-28 16:06:23 +08:00
_progress->setPosition(Vec2(VisibleRect::center().x, VisibleRect::center().y + 50));
_loadLayer->addChild(_progress);
2021-12-28 16:06:23 +08:00
std::string manifestPath = sceneManifests[_testIndex],
storagePath = FileUtils::getInstance()->getWritablePath() + storagePaths[_testIndex];
2022-07-16 10:43:05 +08:00
AXLOG("Storage path for this test : %s", storagePath.c_str());
2014-10-09 18:30:39 +08:00
_am = AssetsManagerEx::create(manifestPath, storagePath);
_am->retain();
return true;
}
void AssetsManagerExLoaderScene::onLoadEnd()
{
removeChild(_loadLayer, true);
_loadLayer = nullptr;
auto backgroundSprite = Sprite::create(backgroundPaths[_testIndex]);
addChild(backgroundSprite, 1);
backgroundSprite->setPosition(VisibleRect::center());
}
void AssetsManagerExLoaderScene::startDownloadCallback(Ref* sender)
{
removeChild(_downloadMenu);
_downloadMenu = nullptr;
2014-10-09 18:30:39 +08:00
if (!_am->getLocalManifest()->isLoaded())
{
2022-07-16 10:43:05 +08:00
AXLOG("Fail to update assets, step skipped.");
onLoadEnd();
2014-10-09 18:30:39 +08:00
}
else
{
2021-12-28 16:06:23 +08:00
_amListener =
axis::extension::EventListenerAssetsManagerEx::create(_am, [this](EventAssetsManagerEx* event) {
2021-12-28 16:06:23 +08:00
static int failCount = 0;
switch (event->getEventCode())
2014-10-09 18:30:39 +08:00
{
2021-12-28 16:06:23 +08:00
case EventAssetsManagerEx::EventCode::ERROR_NO_LOCAL_MANIFEST:
2014-10-09 18:30:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXLOG("No local manifest file found, skip assets update.");
2021-12-28 16:06:23 +08:00
this->onLoadEnd();
2014-10-09 18:30:39 +08:00
}
2021-12-28 16:06:23 +08:00
break;
case EventAssetsManagerEx::EventCode::UPDATE_PROGRESSION:
2014-10-09 18:30:39 +08:00
{
2021-12-28 16:06:23 +08:00
std::string assetId = event->getAssetId();
float percent = event->getPercent();
std::string str;
if (assetId == AssetsManagerEx::VERSION_ID)
{
str = StringUtils::format("Version file: %.2f", percent) + "%";
}
else if (assetId == AssetsManagerEx::MANIFEST_ID)
{
str = StringUtils::format("Manifest file: %.2f", percent) + "%";
}
else
{
str = StringUtils::format("%.2f", percent) + "%";
2022-07-16 10:43:05 +08:00
AXLOG("%.2f Percent", percent);
2021-12-28 16:06:23 +08:00
}
if (this->_progress != nullptr)
this->_progress->setString(str);
2014-10-09 18:30:39 +08:00
}
2021-12-28 16:06:23 +08:00
break;
case EventAssetsManagerEx::EventCode::ERROR_DOWNLOAD_MANIFEST:
case EventAssetsManagerEx::EventCode::ERROR_PARSE_MANIFEST:
2014-10-09 18:30:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXLOG("Fail to download manifest file, update skipped.");
2021-12-28 16:06:23 +08:00
this->onLoadEnd();
2014-10-09 18:30:39 +08:00
}
2021-12-28 16:06:23 +08:00
break;
case EventAssetsManagerEx::EventCode::ALREADY_UP_TO_DATE:
case EventAssetsManagerEx::EventCode::UPDATE_FINISHED:
2014-10-09 18:30:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXLOG("Update finished. %s", event->getMessage().c_str());
this->onLoadEnd();
2014-10-09 18:30:39 +08:00
}
break;
2021-12-28 16:06:23 +08:00
case EventAssetsManagerEx::EventCode::UPDATE_FAILED:
{
2022-07-16 10:43:05 +08:00
AXLOG("Update failed. %s", event->getMessage().c_str());
2021-12-28 16:06:23 +08:00
failCount++;
if (failCount < 5)
{
_am->downloadFailedAssets();
}
else
{
2022-07-16 10:43:05 +08:00
AXLOG("Reach maximum fail count, exit update process");
2021-12-28 16:06:23 +08:00
failCount = 0;
this->onLoadEnd();
}
}
break;
case EventAssetsManagerEx::EventCode::ERROR_UPDATING:
{
2022-07-16 10:43:05 +08:00
AXLOG("Asset %s : %s", event->getAssetId().c_str(), event->getMessage().c_str());
2021-12-28 16:06:23 +08:00
}
break;
case EventAssetsManagerEx::EventCode::ERROR_DECOMPRESS:
{
2022-07-16 10:43:05 +08:00
AXLOG("%s", event->getMessage().c_str());
2021-12-28 16:06:23 +08:00
}
break;
default:
break;
}
});
2014-10-09 18:30:39 +08:00
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_amListener, 1);
2014-10-09 18:30:39 +08:00
_am->update();
}
}
void AssetsManagerExLoaderScene::onExit()
{
_eventDispatcher->removeEventListener(_amListener);
_am->release();
TestCase::onExit();
}
std::string AssetsManagerExLoaderScene::title() const
{
return "AssetsManagerExTest";
2014-10-09 18:30:39 +08:00
}