-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
CCAssetLibrary.js
381 lines (343 loc) · 14.1 KB
/
CCAssetLibrary.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
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
/****************************************************************************
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 Asset = require('../assets/CCAsset');
var callInNextTick = require('./utils').callInNextTick;
var Loader = require('../load-pipeline/CCLoader');
var PackDownloader = require('../load-pipeline/pack-downloader');
var AutoReleaseUtils = require('../load-pipeline/auto-release-utils');
var decodeUuid = require('../utils/decode-uuid');
var MD5Pipe = require('../load-pipeline/md5-pipe');
var js = require('./js');
/**
* The asset library which managing loading/unloading assets in project.
*
* @class AssetLibrary
* @static
*/
// configs
var _libraryBase = '';
var _rawAssetsBase = ''; // The base dir for raw assets in runtime
var _uuidToRawAsset = js.createMap(true);
function isScene (asset) {
return asset && (asset.constructor === cc.SceneAsset || asset instanceof cc.Scene);
}
// types
function RawAssetEntry (url, type) {
this.url = url;
this.type = type;
}
// publics
var AssetLibrary = {
/**
* @callback loadCallback
* @param {String} error - null or the error info
* @param {Asset} data - the loaded asset or null
*/
/**
* @method loadAsset
* @param {String} uuid
* @param {loadCallback} callback - the callback function once load finished
* @param {Object} options
* @param {Boolean} options.readMainCache - Default is true. If false, the asset and all its depends assets will reload and create new instances from library.
* @param {Boolean} options.writeMainCache - Default is true. If true, the result will cache to AssetLibrary, and MUST be unload by user manually.
* @param {Asset} options.existingAsset - load to existing asset, this argument is only available in editor
* @private
*/
loadAsset: function (uuid, callback, options) {
if (typeof uuid !== 'string') {
return callInNextTick(callback, new Error('[AssetLibrary] uuid must be string'), null);
}
// var readMainCache = typeof (options && options.readMainCache) !== 'undefined' ? readMainCache : true;
// var writeMainCache = typeof (options && options.writeMainCache) !== 'undefined' ? writeMainCache : true;
var item = {
uuid: uuid,
type: 'uuid'
};
if (options && options.existingAsset) {
item.existingAsset = options.existingAsset;
}
Loader.load(item, function (error, asset) {
if (error || !asset) {
error = new Error('[AssetLibrary] loading JSON or dependencies failed: ' + (error ? error.message : 'Unknown error'));
}
else {
if (asset.constructor === cc.SceneAsset) {
if (CC_EDITOR && !asset.scene) {
Editor.error('Sorry, the scene data of "%s" is corrupted!', uuid);
}
else {
var key = cc.loader._getReferenceKey(uuid);
asset.scene.dependAssets = AutoReleaseUtils.getDependsRecursively(key);
}
}
if (CC_EDITOR || isScene(asset)) {
var id = cc.loader._getReferenceKey(uuid);
Loader.removeItem(id);
}
}
if (callback) {
callback(error, asset);
}
});
},
getLibUrlNoExt: function (uuid, inRawAssetsDir) {
if (CC_BUILD) {
uuid = decodeUuid(uuid);
}
var base = (CC_BUILD && inRawAssetsDir) ? (_rawAssetsBase + 'assets/') : _libraryBase;
return base + uuid.slice(0, 2) + '/' + uuid;
},
_queryAssetInfoInEditor: function (uuid, callback) {
if (CC_EDITOR) {
Editor.Ipc.sendToMain('scene:query-asset-info-by-uuid', uuid, function (err, info) {
if (info) {
Editor.Utils.UuidCache.cache(info.url, uuid);
var ctor = Editor.assets[info.type];
if (ctor) {
var isRawAsset = !js.isChildClassOf(ctor, Asset);
callback(null, info.url, isRawAsset, ctor);
}
else {
callback(new Error('Can not find asset type ' + info.type));
}
}
else {
var error = new Error('Can not get asset url by uuid "' + uuid + '", the asset may be deleted.');
error.errorCode = 'db.NOTFOUND';
callback(error);
}
});
}
},
_getAssetInfoInRuntime: function (uuid, result) {
result = result || {url: null, raw: false};
var info = _uuidToRawAsset[uuid];
if (info && !js.isChildClassOf(info.type, cc.Asset)) {
// backward compatibility since 1.10
result.url = _rawAssetsBase + info.url;
result.raw = true;
}
else {
result.url = this.getLibUrlNoExt(uuid) + '.json';
result.raw = false;
}
return result;
},
_uuidInSettings: function (uuid) {
return uuid in _uuidToRawAsset;
},
/**
* @method queryAssetInfo
* @param {String} uuid
* @param {Function} callback
* @param {Error} callback.error
* @param {String} callback.url - the url of raw asset or imported asset
* @param {Boolean} callback.raw - indicates whether the asset is raw asset
* @param {Function} callback.ctorInEditor - the actual type of asset, used in editor only
*/
queryAssetInfo: function (uuid, callback) {
if (CC_EDITOR && !CC_TEST) {
this._queryAssetInfoInEditor(uuid, callback);
}
else {
var info = this._getAssetInfoInRuntime(uuid);
callback(null, info.url, info.raw);
}
},
// parse uuid out of url
parseUuidInEditor: function (url) {
if (CC_EDITOR) {
var uuid = '';
var isImported = url.startsWith(_libraryBase);
if (isImported) {
var dir = cc.path.dirname(url);
var dirBasename = cc.path.basename(dir);
var isAssetUrl = dirBasename.length === 2;
if (isAssetUrl) {
uuid = cc.path.basename(url);
var index = uuid.indexOf('.');
if (index !== -1) {
uuid = uuid.slice(0, index);
}
}
else {
// raw file url
uuid = dirBasename;
}
}
// If url is not in the library, just return ""
return uuid;
}
},
/**
* @method loadJson
* @param {String} json
* @param {loadCallback} callback
* @return {LoadingHandle}
* @private
*/
loadJson: function (json, callback) {
var randomUuid = '' + ((new Date()).getTime() + Math.random());
var item = {
uuid: randomUuid,
type: 'uuid',
content: json,
skips: [ Loader.assetLoader.id, Loader.downloader.id ]
};
Loader.load(item, function (error, asset) {
if (error) {
error = new Error('[AssetLibrary] loading JSON or dependencies failed: ' + error.message);
}
else {
if (asset.constructor === cc.SceneAsset) {
var key = cc.loader._getReferenceKey(randomUuid);
asset.scene.dependAssets = AutoReleaseUtils.getDependsRecursively(key);
}
if (CC_EDITOR || isScene(asset)) {
var id = cc.loader._getReferenceKey(randomUuid);
Loader.removeItem(id);
}
}
asset._uuid = '';
if (callback) {
callback(error, asset);
}
});
},
/**
* Get the exists asset by uuid.
*
* @method getAssetByUuid
* @param {String} uuid
* @return {Asset} - the existing asset, if not loaded, just returns null.
* @private
*/
getAssetByUuid: function (uuid) {
return AssetLibrary._uuidToAsset[uuid] || null;
},
/**
* init the asset library
*
* @method init
* @param {Object} options
* @param {String} options.libraryPath - 能接收的任意类型的路径,通常在编辑器里使用绝对的,在网页里使用相对的。
* @param {Object} options.mountPaths - mount point of actual urls for raw assets (only used in editor)
* @param {Object} [options.rawAssets] - uuid to raw asset's urls (only used in runtime)
* @param {String} [options.rawAssetsBase] - base of raw asset's urls (only used in runtime)
* @param {String} [options.packedAssets] - packed assets (only used in runtime)
*/
init: function (options) {
if (CC_EDITOR && _libraryBase) {
cc.errorID(6402);
return;
}
// 这里将路径转 url,不使用路径的原因是有的 runtime 不能解析 "\" 符号。
// 不使用 url.format 的原因是 windows 不支持 file:// 和 /// 开头的协议,所以只能用 replace 操作直接把路径转成 URL。
var libraryPath = options.libraryPath;
libraryPath = libraryPath.replace(/\\/g, '/');
_libraryBase = cc.path.stripSep(libraryPath) + '/';
_rawAssetsBase = options.rawAssetsBase;
var md5AssetsMap = options.md5AssetsMap;
if (md5AssetsMap && md5AssetsMap.import) {
// decode uuid
var i = 0, uuid = 0;
var md5ImportMap = js.createMap(true);
var md5Entries = md5AssetsMap.import;
for (i = 0; i < md5Entries.length; i += 2) {
uuid = decodeUuid(md5Entries[i]);
md5ImportMap[uuid] = md5Entries[i + 1];
}
var md5RawAssetsMap = js.createMap(true);
md5Entries = md5AssetsMap['raw-assets'];
for (i = 0; i < md5Entries.length; i += 2) {
uuid = decodeUuid(md5Entries[i]);
md5RawAssetsMap[uuid] = md5Entries[i + 1];
}
var md5Pipe = new MD5Pipe(md5ImportMap, md5RawAssetsMap, _libraryBase);
cc.loader.insertPipeAfter(cc.loader.assetLoader, md5Pipe);
cc.loader.md5Pipe = md5Pipe;
}
// init raw assets
var resources = Loader._resources;
resources.reset();
var rawAssets = options.rawAssets;
if (rawAssets) {
for (var mountPoint in rawAssets) {
var assets = rawAssets[mountPoint];
for (var uuid in assets) {
var info = assets[uuid];
var url = info[0];
var typeId = info[1];
var type = cc.js._getClassById(typeId);
if (!type) {
cc.error('Cannot get', typeId);
continue;
}
// backward compatibility since 1.10
_uuidToRawAsset[uuid] = new RawAssetEntry(mountPoint + '/' + url, type);
// init resources
if (mountPoint === 'assets') {
var ext = cc.path.extname(url);
if (ext) {
// trim base dir and extname
url = url.slice(0, - ext.length);
}
var isSubAsset = info[2] === 1;
// register
resources.add(url, uuid, type, !isSubAsset);
}
}
}
}
if (options.packedAssets) {
PackDownloader.initPacks(options.packedAssets);
}
// init cc.url
cc.url._init((options.mountPaths && options.mountPaths.assets) || _rawAssetsBase + 'assets');
}
};
// unload asset if it is destoryed
/**
* !#en Caches uuid to all loaded assets in scenes.
*
* !#zh 这里保存所有已经加载的场景资源,防止同一个资源在内存中加载出多份拷贝。
*
* 这里用不了WeakMap,在浏览器中所有加载过的资源都只能手工调用 unloadAsset 释放。
*
* 参考:
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap
* https://github.com/TooTallNate/node-weak
*
* @property {object} _uuidToAsset
* @private
*/
AssetLibrary._uuidToAsset = {};
//暂时屏蔽,因为目前没有缓存任何asset
//if (CC_DEV && Asset.prototype._onPreDestroy) {
// cc.error('_onPreDestroy of Asset has already defined');
//}
//Asset.prototype._onPreDestroy = function () {
// if (AssetLibrary._uuidToAsset[this._uuid] === this) {
// AssetLibrary.unloadAsset(this);
// }
//};
module.exports = cc.AssetLibrary = AssetLibrary;