From 83042038ed6047c833a91e8058f2fef28229ef08 Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Wed, 23 Dec 2015 18:04:43 +0800 Subject: [PATCH] support mounted raw assets in build version --- cocos2d/core/platform/CCAssetLibrary.js | 41 ++++++++++++++++++++----- cocos2d/core/value-types/CCVec2.js | 1 - test/page/index.js | 4 ++- test/qunit/unit/test-asset-library.js | 2 +- test/qunit/unit/test-prefab.js | 2 +- 5 files changed, 38 insertions(+), 12 deletions(-) diff --git a/cocos2d/core/platform/CCAssetLibrary.js b/cocos2d/core/platform/CCAssetLibrary.js index 7240fc1b6a..2c6f0b31b4 100644 --- a/cocos2d/core/platform/CCAssetLibrary.js +++ b/cocos2d/core/platform/CCAssetLibrary.js @@ -14,7 +14,8 @@ var CallbacksInvoker = require('./callbacks-invoker'); // configs var _libraryBase = ''; -var _uuidToRawAssets = {}; +var _rawAssetsBase = ''; // The base dir for raw assets in runtime +var _uuidToRawAssets; // variables @@ -137,7 +138,7 @@ var AssetLibrary = { var info = _uuidToRawAssets[uuid]; if (info) { return { - url: cc.url._rawAssets + info.url, + url: _rawAssetsBase + info.url, raw: info.raw, }; } @@ -510,11 +511,12 @@ var AssetLibrary = { * init the asset library * * @method init - * @param {String} libraryPath - 能接收的任意类型的路径,通常在编辑器里使用绝对的,在网页里使用相对的。 - * @param {Object} mountPaths - mount point of actual urls for raw assets - * @param {Object} [rawAssets] - uuid to raw asset's urls (only used in runtime) + * @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) */ - init: function (libraryPath, mountPaths, rawAssets) { + init: function (options) { if (CC_EDITOR && _libraryBase) { cc.error('AssetLibrary has already been initialized!'); return; @@ -522,11 +524,34 @@ var AssetLibrary = { // 这里将路径转 url,不使用路径的原因是有的 runtime 不能解析 "\" 符号。 // 不使用 url.format 的原因是 windows 不支持 file:// 和 /// 开头的协议,所以只能用 replace 操作直接把路径转成 URL。 + var libraryPath = options.libraryPath; libraryPath = libraryPath.replace(/\\/g, '/'); - _libraryBase = cc.path._setEndWithSep(libraryPath, '/'); - _uuidToRawAssets = rawAssets || {}; + _rawAssetsBase = options.rawAssetsBase; + + _uuidToRawAssets = {}; + var rawAssets = options.rawAssets; + if (rawAssets) { + for (var mountPoint in rawAssets) { + var assets = rawAssets[mountPoint]; + for (var uuid in assets) { + var info = assets[uuid]; + _uuidToRawAssets[uuid] = { + url: mountPoint + '/' + info.url, + raw: !!info.raw, + }; + } + } + } + + var mountPaths = options.mountPaths; + if (!mountPaths) { + mountPaths = { + assets: _rawAssetsBase + 'assets', + internal: _rawAssetsBase + 'internal', + } + } cc.url._init(mountPaths); } }; diff --git a/cocos2d/core/value-types/CCVec2.js b/cocos2d/core/value-types/CCVec2.js index 14749311f5..c19749576c 100644 --- a/cocos2d/core/value-types/CCVec2.js +++ b/cocos2d/core/value-types/CCVec2.js @@ -443,7 +443,6 @@ JS.get(Vec2, 'RIGHT', function () { }); cc.Vec2 = Vec2; -var proto = Vec2.prototype; /** * diff --git a/test/page/index.js b/test/page/index.js index 283188fd18..c09464afea 100644 --- a/test/page/index.js +++ b/test/page/index.js @@ -28,7 +28,9 @@ Editor.require('app://builtin/fire-assets/init'); Editor.require('app://engine'); // init asset library -cc.AssetLibrary.init( Path.resolve(Path.join(__dirname, 'fixtures/library')) ); +cc.AssetLibrary.init({ + libraryPath: Path.resolve(Path.join(__dirname, 'fixtures/library')) +}); describe('test wrappers', function () { // test wrappers diff --git a/test/qunit/unit/test-asset-library.js b/test/qunit/unit/test-asset-library.js index edaf2094a4..d398e2e127 100644 --- a/test/qunit/unit/test-asset-library.js +++ b/test/qunit/unit/test-asset-library.js @@ -9,7 +9,7 @@ largeModule('AssetLibrary', { setup: function () { _resetGame(); - AssetLibrary.init(libPath); + AssetLibrary.init({libraryPath: libPath}); } }); diff --git a/test/qunit/unit/test-prefab.js b/test/qunit/unit/test-prefab.js index 608bed0678..30f3267ff5 100644 --- a/test/qunit/unit/test-prefab.js +++ b/test/qunit/unit/test-prefab.js @@ -22,7 +22,7 @@ largeModule('Prefab', { setup: function () { _resetGame(); - AssetLibrary.init('../assets/library'); + AssetLibrary.init({libraryPath: '../assets/library'}); }, //teardownOnce: function () { // console.log('teardownOnce');