forked from cocos/cocos-engine
/
pack-downloader.js
239 lines (214 loc) · 8.67 KB
/
pack-downloader.js
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
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
https://www.cocos.com/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
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.
****************************************************************************/
var Unpackers = require('./unpackers');
var pushToMap = require('../utils/misc').pushToMap;
// when more than one package contains the required asset,
// choose to load from the package with the largest state value.
var PackState = {
Invalid: 0,
Removed: 1,
Downloading: 2,
Loaded: 3,
};
function UnpackerData () {
this.unpacker = null;
this.state = PackState.Invalid;
this.timeStamp = 0;
}
// {assetUuid: packUuid|[packUuid]}
// If value is array of packUuid, then the first one will be prioritized for download,
// so the smallest pack must be at the beginning of the array.
var uuidToPack = {};
// {packUuid: assetIndices}
var packIndices = {};
// {packUuid: UnpackerData}
// We have to cache all packs in global because for now there's no operation context in loader.
var globalUnpackers = {};
var toBeChecked = {};
var timer = null;
var checkPeriod = 5000;
function error (uuid, packUuid) {
return new Error('Can not retrieve ' + uuid + ' from packer ' + packUuid);
}
module.exports = {
// two minutes
msToRelease: 2 * 60 * 1000,
initPacks: function (packs) {
packIndices = packs;
uuidToPack = {};
for (var packUuid in packs) {
var uuids = packs[packUuid];
for (var i = 0; i < uuids.length; i++) {
var uuid = uuids[i];
// the smallest pack must be at the beginning of the array to download more first
var pushFront = uuids.length === 1;
pushToMap(uuidToPack, uuid, packUuid, pushFront);
}
}
},
_loadNewPack: function (uuid, packUuid, callback) {
var self = this;
var packUrl = cc.AssetLibrary.getLibUrlNoExt(packUuid) + '.json';
cc.loader.load({ url: packUrl, ignoreMaxConcurrency: true }, function (err, packJson) {
if (err) {
cc.errorID(4916, uuid);
return callback(err);
}
globalUnpackers[packUuid].url = packUrl;
var res = self._doLoadNewPack(uuid, packUuid, packJson);
if (res) {
callback(null, res);
}
else {
callback(error(uuid, packUuid));
}
});
},
_doPreload (packUuid, packJson) {
var unpackerData = globalUnpackers[packUuid];
if (!unpackerData) {
unpackerData = globalUnpackers[packUuid] = new UnpackerData();
unpackerData.state = PackState.Downloading;
}
if (unpackerData.state !== PackState.Loaded) {
unpackerData.unpacker = new Unpackers.JsonUnpacker();
unpackerData.unpacker.load(packIndices[packUuid], packJson);
unpackerData.state = PackState.Loaded;
}
},
_doLoadNewPack: function (uuid, packUuid, packedJson) {
var unpackerData = globalUnpackers[packUuid];
// double check cache after load
if (unpackerData.state !== PackState.Loaded) {
// init unpacker
if (typeof packedJson === 'string') {
packedJson = JSON.parse(packedJson);
}
if (Array.isArray(packedJson)) {
unpackerData.unpacker = new Unpackers.JsonUnpacker();
}
else if (packedJson.type === Unpackers.TextureUnpacker.ID) {
unpackerData.unpacker = new Unpackers.TextureUnpacker();
}
unpackerData.unpacker.load(packIndices[packUuid], packedJson);
unpackerData.state = PackState.Loaded;
unpackerData.timeStamp = performance.now();
toBeChecked[packUuid] = unpackerData;
var self = this;
if (!timer) timer = setInterval(function () {
var now = performance.now();
var empty = true;
for (var packUuid in toBeChecked) {
var pack = toBeChecked[packUuid];
if (now - pack.timeStamp > self.msToRelease) {
self.remove(packUuid);
}
else {
empty = false;
}
}
if (empty) {
clearInterval(timer);
timer = null;
}
}, checkPeriod);
}
return unpackerData.unpacker.retrieve(uuid);
},
_selectLoadedPack: function (packUuids) {
var existsPackState = PackState.Invalid;
var existsPackUuid = '';
for (var i = 0; i < packUuids.length; i++) {
var packUuid = packUuids[i];
var unpackerData = globalUnpackers[packUuid];
if (unpackerData) {
var state = unpackerData.state;
if (state === PackState.Loaded) {
return packUuid;
}
else if (state > existsPackState) { // load from the package with the largest state value,
existsPackState = state;
existsPackUuid = packUuid;
}
}
}
// otherwise the first one (smallest one) will be load
return existsPackState !== PackState.Invalid ? existsPackUuid : packUuids[0];
},
/**
* @returns {Object} When returns undefined, the requested item is not in any pack, when returns null, the item is in a loading pack, when item json exists, it will return the result directly.
*/
load: function (item, callback) {
var uuid = item.uuid;
var packUuid = uuidToPack[uuid];
if (!packUuid) {
// Return undefined to let caller know it's not recognized.
// We don't use false here because changing return value type may cause jit fail,
// though return undefined may have the same issue.
return;
}
if (Array.isArray(packUuid)) {
packUuid = this._selectLoadedPack(packUuid);
}
var unpackerData = globalUnpackers[packUuid];
if (unpackerData && unpackerData.state === PackState.Loaded) {
// update timestamp
unpackerData.timeStamp = performance.now();
// ensure async
var json = unpackerData.unpacker.retrieve(uuid);
if (json) {
return json;
}
else {
return error(uuid, packUuid);
}
}
else {
if (!unpackerData) {
if (!CC_TEST) {
console.log('Create unpacker %s for %s', packUuid, uuid);
}
unpackerData = globalUnpackers[packUuid] = new UnpackerData();
unpackerData.state = PackState.Downloading;
}
this._loadNewPack(uuid, packUuid, callback);
}
// Return null to let caller know it's loading asynchronously
return null;
},
remove (packUuid) {
var unpackerData = globalUnpackers[packUuid];
if (unpackerData) {
cc.loader.release(unpackerData.url);
delete globalUnpackers[packUuid];
delete toBeChecked[packUuid];
}
}
};
if (CC_TEST) {
cc._Test.PackDownloader = module.exports;
cc._Test.PackDownloader.reset = function () {
uuidToPack = {};
packIndices = {};
globalUnpackers = {};
};
}