PuzzleSDK
AssetsManagerExTest.cpp
浏览该文件的文档.
1/****************************************************************************
2 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
3
4 http://www.cocos2d-x.org
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 ****************************************************************************/
24
25#include "AssetsManagerExTest.h"
26#include "../../testResource.h"
27#include "cocos2d.h"
28
31
32const char* sceneManifests[] = {"Manifests/AMTestScene1/project.manifest", "Manifests/AMTestScene2/project.manifest", "Manifests/AMTestScene3/project.manifest"};
33const char* storagePaths[] = {"CppTests/AssetsManagerExTest/scene1/", "CppTests/AssetsManagerExTest/scene2/", "CppTests/AssetsManagerExTest/scene3"};
34const char* backgroundPaths[] = {"Images/assetMgrBackground1.jpg", "Images/assetMgrBackground2.png", "Images/assetMgrBackground3.png"};
35
36AssetsManagerExTests::AssetsManagerExTests()
37{
38 addTestCase("AssetsManager Test1", [](){ return AssetsManagerExLoaderScene::create(0); });
39 addTestCase("AssetsManager Test2", [](){ return AssetsManagerExLoaderScene::create(1); });
40 addTestCase("AssetsManager Test3", [](){ return AssetsManagerExLoaderScene::create(2); });
41}
42
44{
45 auto scene = new (std::nothrow) AssetsManagerExLoaderScene(testIndex);
46 if (scene && scene->init())
47 {
48 scene->autorelease();
49 }
50 else
51 {
52 delete scene;
53 scene = nullptr;
54 }
55
56 return scene;
57}
58
60: _testIndex(testIndex)
61, _loadLayer(nullptr)
62, _downloadMenu(nullptr)
63, _am(nullptr)
64, _progress(nullptr)
65, _amListener(nullptr)
66{
67
68}
69
71{
72 if (!TestCase::init())
73 {
74 return false;
75 }
76
77 auto downloadLabel = Label::createWithTTF("Start Download", "fonts/arial.ttf", 16);
78 auto downloadItem = MenuItemLabel::create(downloadLabel, CC_CALLBACK_1(AssetsManagerExLoaderScene::startDownloadCallback, this));
79 downloadItem->setPosition(Vec2(VisibleRect::center().x, VisibleRect::bottom().y + 100));
80 _downloadMenu = Menu::create(downloadItem, nullptr);
81 _downloadMenu->setPosition(Vec2::ZERO);
82 this->addChild(_downloadMenu, 1);
83
84 _loadLayer = Layer::create();
85 addChild(_loadLayer);
86
87 auto sprite = Sprite::create("Images/Icon.png");
88 _loadLayer->addChild(sprite);
89 sprite->setPosition( VisibleRect::center() );
90
91 TTFConfig config("fonts/tahoma.ttf", 30);
92 _progress = Label::createWithTTF(config, "0%", TextHAlignment::CENTER);
93 _progress->setPosition( Vec2(VisibleRect::center().x, VisibleRect::center().y + 50) );
94 _loadLayer->addChild(_progress);
95
96 std::string manifestPath = sceneManifests[_testIndex], storagePath = FileUtils::getInstance()->getWritablePath() + storagePaths[_testIndex];
97 CCLOG("Storage path for this test : %s", storagePath.c_str());
98 _am = AssetsManagerEx::create(manifestPath, storagePath);
99 _am->retain();
100
101 return true;
102}
103
105{
106 removeChild(_loadLayer, true);
107 _loadLayer = nullptr;
108
109 auto backgroundSprite = Sprite::create(backgroundPaths[_testIndex]);
110 addChild(backgroundSprite, 1);
111 backgroundSprite->setPosition(VisibleRect::center());
112}
113
115{
116 removeChild(_downloadMenu);
117 _downloadMenu = nullptr;
118
119 if (!_am->getLocalManifest()->isLoaded())
120 {
121 CCLOG("Fail to update assets, step skipped.");
122 onLoadEnd();
123 }
124 else
125 {
126 _amListener = cocos2d::extension::EventListenerAssetsManagerEx::create(_am, [this](EventAssetsManagerEx* event){
127 static int failCount = 0;
128 switch (event->getEventCode())
129 {
130 case EventAssetsManagerEx::EventCode::ERROR_NO_LOCAL_MANIFEST:
131 {
132 CCLOG("No local manifest file found, skip assets update.");
133 this->onLoadEnd();
134 }
135 break;
136 case EventAssetsManagerEx::EventCode::UPDATE_PROGRESSION:
137 {
138 std::string assetId = event->getAssetId();
139 float percent = event->getPercent();
140 std::string str;
141 if (assetId == AssetsManagerEx::VERSION_ID)
142 {
143 str = StringUtils::format("Version file: %.2f", percent) + "%";
144 }
145 else if (assetId == AssetsManagerEx::MANIFEST_ID)
146 {
147 str = StringUtils::format("Manifest file: %.2f", percent) + "%";
148 }
149 else
150 {
151 str = StringUtils::format("%.2f", percent) + "%";
152 CCLOG("%.2f Percent", percent);
153 }
154 if (this->_progress != nullptr)
155 this->_progress->setString(str);
156 }
157 break;
158 case EventAssetsManagerEx::EventCode::ERROR_DOWNLOAD_MANIFEST:
159 case EventAssetsManagerEx::EventCode::ERROR_PARSE_MANIFEST:
160 {
161 CCLOG("Fail to download manifest file, update skipped.");
162 this->onLoadEnd();
163 }
164 break;
165 case EventAssetsManagerEx::EventCode::ALREADY_UP_TO_DATE:
166 case EventAssetsManagerEx::EventCode::UPDATE_FINISHED:
167 {
168 CCLOG("Update finished. %s", event->getMessage().c_str());
169 this->onLoadEnd();
170 }
171 break;
172 case EventAssetsManagerEx::EventCode::UPDATE_FAILED:
173 {
174 CCLOG("Update failed. %s", event->getMessage().c_str());
175
176 failCount++;
177 if (failCount < 5)
178 {
179 _am->downloadFailedAssets();
180 }
181 else
182 {
183 CCLOG("Reach maximum fail count, exit update process");
184 failCount = 0;
185 this->onLoadEnd();
186 }
187 }
188 break;
189 case EventAssetsManagerEx::EventCode::ERROR_UPDATING:
190 {
191 CCLOG("Asset %s : %s", event->getAssetId().c_str(), event->getMessage().c_str());
192 }
193 break;
194 case EventAssetsManagerEx::EventCode::ERROR_DECOMPRESS:
195 {
196 CCLOG("%s", event->getMessage().c_str());
197 }
198 break;
199 default:
200 break;
201 }
202 });
203 Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_amListener, 1);
204
205 _am->update();
206 }
207}
208
210{
211 _eventDispatcher->removeEventListener(_amListener);
212 _am->release();
213 TestCase::onExit();
214}
215
217{
218 return "AssetsManagerExTest";
219}
const char * storagePaths[]
const char * backgroundPaths[]
const char * sceneManifests[]
virtual std::string title() const override
virtual bool init() override
cocos2d::extension::EventListenerAssetsManagerEx * _amListener
void startDownloadCallback(cocos2d::Ref *sender)
cocos2d::extension::AssetsManagerEx * _am
static AssetsManagerExLoaderScene * create(int testIndex)
virtual void onExit() override
static cocos2d::Vec2 center()
Definition: VisibleRect.cpp:69
static cocos2d::Vec2 bottom()
Definition: VisibleRect.cpp:63