Manifest.cpp
15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/****************************************************************************
Copyright (c) 2014 cocos2d-x.org
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
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 "Manifest.h"
#include "json/prettywriter.h"
#include "json/stringbuffer.h"
#include <fstream>
#include <stdio.h>
#define KEY_VERSION "version"
#define KEY_PACKAGE_URL "packageUrl"
#define KEY_MANIFEST_URL "remoteManifestUrl"
#define KEY_VERSION_URL "remoteVersionUrl"
#define KEY_GROUP_VERSIONS "groupVersions"
#define KEY_ENGINE_VERSION "engineVersion"
#define KEY_ASSETS "assets"
#define KEY_COMPRESSED_FILES "compressedFiles"
#define KEY_SEARCH_PATHS "searchPaths"
#define KEY_PATH "path"
#define KEY_MD5 "md5"
#define KEY_GROUP "group"
#define KEY_COMPRESSED "compressed"
#define KEY_SIZE "size"
#define KEY_COMPRESSED_FILE "compressedFile"
#define KEY_DOWNLOAD_STATE "downloadState"
NS_CC_EXT_BEGIN
static int cmpVersion(const std::string& v1, const std::string& v2)
{
int i;
int oct_v1[4] = {0}, oct_v2[4] = {0};
int filled1 = std::sscanf(v1.c_str(), "%d.%d.%d.%d", &oct_v1[0], &oct_v1[1], &oct_v1[2], &oct_v1[3]);
int filled2 = std::sscanf(v2.c_str(), "%d.%d.%d.%d", &oct_v2[0], &oct_v2[1], &oct_v2[2], &oct_v2[3]);
if (filled1 == 0 || filled2 == 0)
{
return strcmp(v1.c_str(), v2.c_str());
}
for (i = 0; i < 4; i++)
{
if (oct_v1[i] > oct_v2[i])
return 1;
else if (oct_v1[i] < oct_v2[i])
return -1;
}
return 0;
}
Manifest::Manifest(const std::string& manifestUrl/* = ""*/)
: _versionLoaded(false)
, _loaded(false)
, _manifestRoot("")
, _remoteManifestUrl("")
, _remoteVersionUrl("")
, _version("")
, _engineVer("")
{
// Init variables
_fileUtils = FileUtils::getInstance();
if (manifestUrl.size() > 0)
parse(manifestUrl);
}
void Manifest::loadJson(const std::string& url)
{
clear();
std::string content;
if (_fileUtils->isFileExist(url))
{
// Load file content
content = _fileUtils->getStringFromFile(url);
if (content.size() == 0)
{
CCLOG("Fail to retrieve local file content: %s\n", url.c_str());
}
else
{
// Parse file with rapid json
_json.Parse<0>(content.c_str());
// Print error
if (_json.HasParseError()) {
size_t offset = _json.GetErrorOffset();
if (offset > 0)
offset--;
std::string errorSnippet = content.substr(offset, 10);
CCLOG("File parse error %d at <%s>\n", _json.GetParseError(), errorSnippet.c_str());
}
}
}
}
void Manifest::parseVersion(const std::string& versionUrl)
{
loadJson(versionUrl);
if (_json.IsObject())
{
loadVersion(_json);
}
}
void Manifest::parse(const std::string& manifestUrl)
{
loadJson(manifestUrl);
if (!_json.HasParseError() && _json.IsObject())
{
// Register the local manifest root
size_t found = manifestUrl.find_last_of("/\\");
if (found != std::string::npos)
{
_manifestRoot = manifestUrl.substr(0, found+1);
}
loadManifest(_json);
}
}
bool Manifest::isVersionLoaded() const
{
return _versionLoaded;
}
bool Manifest::isLoaded() const
{
return _loaded;
}
bool Manifest::versionEquals(const Manifest *b) const
{
// Check manifest version
if (_version != b->getVersion())
{
return false;
}
// Check group versions
else
{
std::vector<std::string> bGroups = b->getGroups();
std::unordered_map<std::string, std::string> bGroupVer = b->getGroupVerions();
// Check group size
if (bGroups.size() != _groups.size())
return false;
// Check groups version
for (unsigned int i = 0; i < _groups.size(); ++i) {
std::string gid =_groups[i];
// Check group name
if (gid != bGroups[i])
return false;
// Check group version
if (_groupVer.at(gid) != bGroupVer.at(gid))
return false;
}
}
return true;
}
bool Manifest::versionGreater(const Manifest *b, const std::function<int(const std::string& versionA, const std::string& versionB)>& handle) const
{
std::string localVersion = getVersion();
std::string bVersion = b->getVersion();
bool greater;
if (handle)
{
greater = handle(localVersion, bVersion) >= 0;
}
else
{
greater = cmpVersion(localVersion, bVersion) >= 0;
}
return greater;
}
std::unordered_map<std::string, Manifest::AssetDiff> Manifest::genDiff(const Manifest *b) const
{
std::unordered_map<std::string, AssetDiff> diff_map;
const std::unordered_map<std::string, Asset> &bAssets = b->getAssets();
std::string key;
Asset valueA;
Asset valueB;
std::unordered_map<std::string, Asset>::const_iterator valueIt, it;
for (it = _assets.begin(); it != _assets.end(); ++it)
{
key = it->first;
valueA = it->second;
// Deleted
valueIt = bAssets.find(key);
if (valueIt == bAssets.cend()) {
AssetDiff diff;
diff.asset = valueA;
diff.type = DiffType::DELETED;
diff_map.emplace(key, diff);
continue;
}
// Modified
valueB = valueIt->second;
if (valueA.md5 != valueB.md5) {
AssetDiff diff;
diff.asset = valueB;
diff.type = DiffType::MODIFIED;
diff_map.emplace(key, diff);
}
}
for (it = bAssets.begin(); it != bAssets.end(); ++it)
{
key = it->first;
valueB = it->second;
// Added
valueIt = _assets.find(key);
if (valueIt == _assets.cend()) {
AssetDiff diff;
diff.asset = valueB;
diff.type = DiffType::ADDED;
diff_map.emplace(key, diff);
}
}
return diff_map;
}
void Manifest::genResumeAssetsList(DownloadUnits *units) const
{
for (auto it = _assets.begin(); it != _assets.end(); ++it)
{
Asset asset = it->second;
if (asset.downloadState != DownloadState::SUCCESSED && asset.downloadState != DownloadState::UNMARKED)
{
DownloadUnit unit;
unit.customId = it->first;
unit.srcUrl = _packageUrl + asset.path;
unit.storagePath = _manifestRoot + asset.path;
unit.size = asset.size;
units->emplace(unit.customId, unit);
}
}
}
std::vector<std::string> Manifest::getSearchPaths() const
{
std::vector<std::string> searchPaths;
searchPaths.push_back(_manifestRoot);
for (int i = (int)_searchPaths.size()-1; i >= 0; i--)
{
std::string path = _searchPaths[i];
if (path.size() > 0 && path[path.size() - 1] != '/')
path.append("/");
path = _manifestRoot + path;
searchPaths.push_back(path);
}
return searchPaths;
}
void Manifest::prependSearchPaths()
{
std::vector<std::string> searchPaths = FileUtils::getInstance()->getSearchPaths();
std::vector<std::string>::iterator iter = searchPaths.begin();
bool needChangeSearchPaths = false;
if (std::find(searchPaths.begin(), searchPaths.end(), _manifestRoot) == searchPaths.end())
{
searchPaths.insert(iter, _manifestRoot);
needChangeSearchPaths = true;
}
for (int i = (int)_searchPaths.size()-1; i >= 0; i--)
{
std::string path = _searchPaths[i];
if (path.size() > 0 && path[path.size() - 1] != '/')
path.append("/");
path = _manifestRoot + path;
iter = searchPaths.begin();
searchPaths.insert(iter, path);
needChangeSearchPaths = true;
}
if (needChangeSearchPaths)
{
FileUtils::getInstance()->setSearchPaths(searchPaths);
}
}
const std::string& Manifest::getPackageUrl() const
{
return _packageUrl;
}
const std::string& Manifest::getManifestFileUrl() const
{
return _remoteManifestUrl;
}
const std::string& Manifest::getVersionFileUrl() const
{
return _remoteVersionUrl;
}
const std::string& Manifest::getVersion() const
{
return _version;
}
const std::vector<std::string>& Manifest::getGroups() const
{
return _groups;
}
const std::unordered_map<std::string, std::string>& Manifest::getGroupVerions() const
{
return _groupVer;
}
const std::string& Manifest::getGroupVersion(const std::string &group) const
{
return _groupVer.at(group);
}
const std::unordered_map<std::string, Manifest::Asset>& Manifest::getAssets() const
{
return _assets;
}
void Manifest::setAssetDownloadState(const std::string &key, const Manifest::DownloadState &state)
{
auto valueIt = _assets.find(key);
if (valueIt != _assets.end())
{
valueIt->second.downloadState = state;
// Update json object
if(_json.IsObject())
{
if ( _json.HasMember(KEY_ASSETS) )
{
rapidjson::Value &assets = _json[KEY_ASSETS];
if (assets.IsObject())
{
if (assets.HasMember(key.c_str()))
{
rapidjson::Value &entry = assets[key.c_str()];
if (entry.HasMember(KEY_DOWNLOAD_STATE) && entry[KEY_DOWNLOAD_STATE].IsInt())
{
entry[KEY_DOWNLOAD_STATE].SetInt((int) state);
}
else
{
entry.AddMember<int>(KEY_DOWNLOAD_STATE, (int)state, _json.GetAllocator());
}
}
}
}
}
}
}
void Manifest::clear()
{
if (_versionLoaded || _loaded)
{
_groups.clear();
_groupVer.clear();
_remoteManifestUrl = "";
_remoteVersionUrl = "";
_version = "";
_engineVer = "";
_versionLoaded = false;
}
if (_loaded)
{
_assets.clear();
_searchPaths.clear();
_loaded = false;
}
}
Manifest::Asset Manifest::parseAsset(const std::string &path, const rapidjson::Value &json)
{
Asset asset;
asset.path = path;
if ( json.HasMember(KEY_MD5) && json[KEY_MD5].IsString() )
{
asset.md5 = json[KEY_MD5].GetString();
}
else asset.md5 = "";
if ( json.HasMember(KEY_PATH) && json[KEY_PATH].IsString() )
{
asset.path = json[KEY_PATH].GetString();
}
if ( json.HasMember(KEY_COMPRESSED) && json[KEY_COMPRESSED].IsBool() )
{
asset.compressed = json[KEY_COMPRESSED].GetBool();
}
else asset.compressed = false;
if ( json.HasMember(KEY_SIZE) && json[KEY_SIZE].IsInt() )
{
asset.size = json[KEY_SIZE].GetInt();
}
else asset.size = 0;
if ( json.HasMember(KEY_DOWNLOAD_STATE) && json[KEY_DOWNLOAD_STATE].IsInt() )
{
asset.downloadState = (json[KEY_DOWNLOAD_STATE].GetInt());
}
else asset.downloadState = DownloadState::UNMARKED;
return asset;
}
void Manifest::loadVersion(const rapidjson::Document &json)
{
// Retrieve remote manifest url
if ( json.HasMember(KEY_MANIFEST_URL) && json[KEY_MANIFEST_URL].IsString() )
{
_remoteManifestUrl = json[KEY_MANIFEST_URL].GetString();
}
// Retrieve remote version url
if ( json.HasMember(KEY_VERSION_URL) && json[KEY_VERSION_URL].IsString() )
{
_remoteVersionUrl = json[KEY_VERSION_URL].GetString();
}
// Retrieve local version
if ( json.HasMember(KEY_VERSION) && json[KEY_VERSION].IsString() )
{
_version = json[KEY_VERSION].GetString();
}
// Retrieve local group version
if ( json.HasMember(KEY_GROUP_VERSIONS) )
{
const rapidjson::Value& groupVers = json[KEY_GROUP_VERSIONS];
if (groupVers.IsObject())
{
for (rapidjson::Value::ConstMemberIterator itr = groupVers.MemberBegin(); itr != groupVers.MemberEnd(); ++itr)
{
std::string group = itr->name.GetString();
std::string version = "0";
if (itr->value.IsString())
{
version = itr->value.GetString();
}
_groups.push_back(group);
_groupVer.emplace(group, version);
}
}
}
// Retrieve local engine version
if ( json.HasMember(KEY_ENGINE_VERSION) && json[KEY_ENGINE_VERSION].IsString() )
{
_engineVer = json[KEY_ENGINE_VERSION].GetString();
}
_versionLoaded = true;
}
void Manifest::loadManifest(const rapidjson::Document &json)
{
loadVersion(json);
// Retrieve package url
if ( json.HasMember(KEY_PACKAGE_URL) && json[KEY_PACKAGE_URL].IsString() )
{
_packageUrl = json[KEY_PACKAGE_URL].GetString();
// Append automatically "/"
if (_packageUrl.size() > 0 && _packageUrl[_packageUrl.size() - 1] != '/')
{
_packageUrl.append("/");
}
}
// Retrieve all assets
if ( json.HasMember(KEY_ASSETS) )
{
const rapidjson::Value& assets = json[KEY_ASSETS];
if (assets.IsObject())
{
for (rapidjson::Value::ConstMemberIterator itr = assets.MemberBegin(); itr != assets.MemberEnd(); ++itr)
{
std::string key = itr->name.GetString();
Asset asset = parseAsset(key, itr->value);
_assets.emplace(key, asset);
}
}
}
// Retrieve all search paths
if ( json.HasMember(KEY_SEARCH_PATHS) )
{
const rapidjson::Value& paths = json[KEY_SEARCH_PATHS];
if (paths.IsArray())
{
for (rapidjson::SizeType i = 0; i < paths.Size(); ++i)
{
if (paths[i].IsString()) {
_searchPaths.push_back(paths[i].GetString());
}
}
}
}
_loaded = true;
}
void Manifest::saveToFile(const std::string &filepath)
{
rapidjson::StringBuffer buffer;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
_json.Accept(writer);
FileUtils::getInstance()->writeStringToFile(buffer.GetString(), filepath);
}
NS_CC_EXT_END