Merge pull request #810 from jareguo/v1.1

load json from pack
This commit is contained in:
Jare Guo
2016-06-19 22:22:18 +08:00
committed by GitHub
7 changed files with 255 additions and 19 deletions
+14 -14
View File
@@ -25,6 +25,7 @@
var JS = require('../platform/js');
var Path = require('../utils/CCPath');
var Pipeline = require('./pipeline');
var PackDownloader = require('./pack-downloader');
var downloadAudio;
if (!CC_EDITOR || !Editor.isMainProcess) {
@@ -47,7 +48,7 @@ function isUrlCrossOrigin (url) {
var endIndex = url.indexOf('/', startIndex + 3);
var urlOrigin = (endIndex === -1) ? url : url.substring(0, endIndex);
return urlOrigin !== location.origin;
};
}
var _noCacheRex = /\?/;
function urlAppendTimestamp (url) {
@@ -263,28 +264,30 @@ function downloadUuid (item, callback) {
item.url = url;
item.isRawAsset = isRawAsset;
if (isRawAsset) {
self.pipeline._items.map[url] = {
id: url,
url: url,
type: Path.extname(url).toLowerCase().substr(1),
error: null,
alias: item.id,
complete: true
};
var ext = Path.extname(url).toLowerCase();
if (!ext) {
callback(new Error('Download Uuid: can not find type of raw asset[' + uuid + ']: ' + url));
return;
}
ext = ext.substr(1);
self.pipeline._items.map[url] = {
id: url,
url: url,
type: ext,
error: null,
alias: item.id,
complete: true
};
// Dispatch to other raw type downloader
var downloadFunc = self.extMap[ext] || self.extMap['default'];
item.type = ext;
downloadFunc(item, callback);
}
else {
self.extMap['json'](item, callback);
var loadByPack = PackDownloader.load(item, callback);
if (!loadByPack) {
self.extMap['json'](item, callback);
}
}
}
});
@@ -339,9 +342,6 @@ var defaultMap = {
// Deserializer
'uuid' : downloadUuid,
'prefab' : downloadUuid,
'fire' : downloadUuid,
'scene' : downloadUuid,
'default' : downloadText
};
+1
View File
@@ -25,6 +25,7 @@
require('./downloader');
require('./loader');
require('./json-unpacker');
require('./loading-items');
require('./pipeline');
@@ -0,0 +1,54 @@
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
http://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.
Chukong Aipu 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.
****************************************************************************/
function JsonUnpacker () {
this.jsons = {};
}
/**
* @param {String[]} indices
* @param {String} data
*/
JsonUnpacker.prototype.read = function (indices, data) {
var jsons = typeof data === 'string' ? JSON.parse(data) : data;
if (jsons.length !== indices.length) {
cc.error('Pack indices and data do not match in size');
}
for (var i = 0; i < indices.length; i++) {
var key = indices[i];
var json = jsons[i];
this.jsons[key] = json;
}
};
JsonUnpacker.prototype.retrieve = function (key) {
return this.jsons[key] || null;
};
if (CC_TEST) {
cc._Test.JsonUnpacker = JsonUnpacker;
}
module.exports = JsonUnpacker;
@@ -0,0 +1,112 @@
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
http://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.
Chukong Aipu 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 JsonUnpacker = require('./json-unpacker');
// {assetUuid: packUuid}
var uuidToPack = {};
// {packUuid: assetIndices}
var packIndices = {};
// {packUuid: JsonUnpacker}
// We have to cache all packs in global because for now there's no operation context in loader.
var globalUnpackers = {};
function error (callback, uuid, packUuid) {
callback(new Error('Can not retrieve ' + uuid + ' from packer ' + packUuid));
}
module.exports = {
initPacks: function (packs) {
packIndices = packs;
for (var packUuid in packs) {
var uuids = packs[packUuid];
for (var i = 0; i < uuids.length; i++) {
var uuid = uuids[i];
uuidToPack[uuid] = packUuid;
}
}
},
//getPackUuid: function (uuid) {
// return uuidToPack[uuid];
//},
_loadNewPack: function (uuid, packUuid, callback) {
var packUrl = cc.AssetLibrary.getImportedDir(packUuid) + '/' + packUuid + '.json';
cc.loader.load(packUrl, function (err, packJson) {
if (err) {
cc.error('Failed to download package for ' + uuid);
return callback(err);
}
// double check cache after load
var unpacker = globalUnpackers[packUuid];
if (!unpacker) {
console.log('Load pack %s for %s', packUuid, uuid);
unpacker = globalUnpackers[packUuid] = new JsonUnpacker();
}
unpacker.read(packIndices[packUuid], packJson);
var json = unpacker.retrieve(uuid);
if (json) {
callback(null, json);
}
else {
error(callback, uuid, packUuid);
}
});
//var packItem = {
// id: packUrl,
// type: 'json',
// uuid: packUuid
//};
//pipeline.flowInDeps(depends, function (items) {
//});
},
/**
* @returns {Boolean} specify whether loaded by pack
*/
load: function (item, callback) {
var uuid = item.id;
var packUuid = uuidToPack[uuid];
if (!packUuid) {
return false;
}
var unpacker = globalUnpackers[packUuid];
if (unpacker) {
var json = unpacker.retrieve(uuid);
if (json) {
callback(null, json);
}
else {
error(callback, uuid, packUuid);
}
}
else {
this._loadNewPack(uuid, packUuid, callback);
}
return true;
}
};
+13 -4
View File
@@ -26,6 +26,7 @@
var Asset = require('../assets/CCAsset');
var callInNextTick = require('./utils').callInNextTick;
var Loader = require('../load-pipeline/CCLoader');
var PackDownloader = require('../load-pipeline/pack-downloader');
/**
* The asset library which managing loading/unloading assets in project.
@@ -38,7 +39,7 @@ var Loader = require('../load-pipeline/CCLoader');
var _libraryBase = '';
var _rawAssetsBase = ''; // The base dir for raw assets in runtime
var _uuidToRawAssets;
var _uuidToRawAsset = {};
function isScene (asset) {
return asset && (asset.constructor === cc.SceneAsset || asset instanceof cc.Scene);
@@ -107,7 +108,7 @@ var AssetLibrary = {
},
_getAssetInfoInRuntime: function (uuid) {
var info = _uuidToRawAssets[uuid];
var info = _uuidToRawAsset[uuid];
if (info && !cc.isChildClassOf(info.type, cc.Asset)) {
return {
url: _rawAssetsBase + info.url,
@@ -249,6 +250,7 @@ var AssetLibrary = {
* @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) {
@@ -264,7 +266,8 @@ var AssetLibrary = {
_rawAssetsBase = options.rawAssetsBase;
_uuidToRawAssets = {};
// init raw assets
var resources = Loader._resources;
resources.reset();
var rawAssets = options.rawAssets;
@@ -281,7 +284,7 @@ var AssetLibrary = {
cc.error('Cannot get', typeId);
continue;
}
_uuidToRawAssets[uuid] = {
_uuidToRawAsset[uuid] = {
url: mountPoint + '/' + url,
type: type,
};
@@ -309,6 +312,12 @@ var AssetLibrary = {
}
}
if (options.packedAssets) {
PackDownloader.initPacks(options.packedAssets);
}
// init mount paths
var mountPaths = options.mountPaths;
if (!mountPaths) {
mountPaths = {
+1 -1
View File
@@ -167,7 +167,7 @@ cc.eventManager.addCustomListener(cc.game.EVENT_HIDE, function () {
}
});
cc.eventManager.addCustomListener(cc.game.EVENT_SHOW, function () {
cc.game.on(cc.game.EVENT_SHOW, function () {
var list = _ccsg.VideoPlayer.pauseElements;
var node = list.pop();
while(node){
+60
View File
@@ -0,0 +1,60 @@
module('JSON packer/unpacker');
(function () {
var KEY1 = 'amitabha';
var JSON1 = {
__born__: 1985,
$$: 'Amoy'
};
var KEY2 = 'hallelujah';
var JSON2 = [{
text: 'hallelujah\nalujah\\alujah...\uD83D\uDE02'
}];
test('pack nothing', function () {
var packer = new Editor.JsonPacker();
var res = packer.pack();
ok(res.indices, 'should pack empty indices');
ok(res.data && typeof res.data === 'string', 'should pack empty data');
});
test('unpack nothing', function () {
var unpacker = new cc._Test.JsonUnpacker();
var res = unpacker.retrieve('non-exists key');
ok(!res, 'chould not unpack if key is invalid');
});
test('pack something', function () {
var packer = new Editor.JsonPacker();
packer.add(KEY2, JSON2);
packer.add(KEY1, JSON1);
var res = packer.pack();
ok(res.indices.indexOf(KEY1) !== -1 && res.indices.indexOf(KEY2) !== -1, 'should generate index data when packing');
var unpacker = new cc._Test.JsonUnpacker();
unpacker.read(res.indices, res.data);
deepEqual(unpacker.retrieve(KEY1), JSON1, 'should unpack JSON1');
deepEqual(unpacker.retrieve(KEY2), JSON2, 'should unpack JSON2');
});
//test('unpack speed', function () {
// var bigJson = Editor.serialize(new cc.Node());
// var packer = new Editor.JsonPacker();
// for (var i = 0; i < 100; ++i) {
// packer.add('' + i, bigJson);
// }
// var res = packer.pack();
//
// console.time('unpack time');
//
// var unpacker = new cc._Test.JsonUnpacker();
// unpacker.read(res.indices, res.data);
// for (var i = 0; i < 100; ++i) {
// unpacker.retrieve('' + i);
// }
//
// console.timeEnd('unpack time');
// ok(true);
//});
})();