diff --git a/cocos2d/core/CCDirector.js b/cocos2d/core/CCDirector.js index 96e914cc79..460f0f6fd3 100644 --- a/cocos2d/core/CCDirector.js +++ b/cocos2d/core/CCDirector.js @@ -221,6 +221,8 @@ cc.Director = Class.extend(/** @lends cc.Director# */{ if (cc._widgetManager) { cc._widgetManager.init(this); } + + cc.loader.init(this); }, /** diff --git a/cocos2d/core/load-pipeline/CCLoader.js b/cocos2d/core/load-pipeline/CCLoader.js index 0c24f509fc..7cc9be0305 100644 --- a/cocos2d/core/load-pipeline/CCLoader.js +++ b/cocos2d/core/load-pipeline/CCLoader.js @@ -32,6 +32,8 @@ var Loader = require('./loader'); var AssetTable = require('./asset-table'); var callInNextTick = require('../platform/utils').callInNextTick; var AutoReleaseUtils = require('./auto-release-utils'); +// var pushToMap = require('../utils/misc').pushToMap; +var ReleasedAssetChecker = CC_DEBUG && require('./released-asset-checker'); var resources = new AssetTable(); @@ -121,10 +123,23 @@ function CCLoader () { // assets to release automatically this._autoReleaseSetting = {}; + + if (CC_DEBUG) { + this._releasedAssetChecker_DEBUG = new ReleasedAssetChecker(); + } } JS.extend(CCLoader, Pipeline); var proto = CCLoader.prototype; +proto.init = function (director) { + if (CC_DEBUG) { + var self = this; + director.on(cc.Director.EVENT_BEFORE_VISIT, function () { + self._releasedAssetChecker_DEBUG.checkCouldRelease(self._cache); + }); + } +}; + /** * Gets a new XMLHttpRequest instance. * @method getXMLHttpRequest @@ -763,6 +778,9 @@ proto.release = function (asset) { else if (asset instanceof cc.Texture2D) { cc.textureCache.removeTextureForKey(item.url); } + if (CC_DEBUG && removed) { + this._releasedAssetChecker_DEBUG.setReleased(item, id); + } } } }; diff --git a/cocos2d/core/load-pipeline/asset-table.js b/cocos2d/core/load-pipeline/asset-table.js index 20b734877b..2191350f99 100644 --- a/cocos2d/core/load-pipeline/asset-table.js +++ b/cocos2d/core/load-pipeline/asset-table.js @@ -110,15 +110,15 @@ proto.getUuidArray = function (path, type, out_urls) { return uuids; }; -/** - * !#en Returns all asset paths in the table. - * !#zh 返回表中的所有资源路径。 - * @method getAllPaths - * @return {string[]} - */ -proto.getAllPaths = function () { - return Object.keys(this._pathToUuid); -}; +// /** +// * !#en Returns all asset paths in the table. +// * !#zh 返回表中的所有资源路径。 +// * @method getAllPaths +// * @return {string[]} +// */ +// proto.getAllPaths = function () { +// return Object.keys(this._pathToUuid); +// }; /** * !#en TODO @@ -138,6 +138,31 @@ proto.add = function (path, uuid, type, isMainAsset) { pushToMap(this._pathToUuid, path, newEntry, isMainAsset); }; +proto._getInfo_DEBUG = CC_DEBUG && function (uuid, out_info) { + var path2uuid = this._pathToUuid; + var paths = Object.keys(path2uuid); + for (var p = 0; p < paths.length; ++p) { + var path = paths[p]; + var item = path2uuid[path]; + if (Array.isArray(item)) { + for (var i = 0; i < item.length; i++) { + var entry = item[i]; + if (entry.uuid === uuid) { + out_info.path = path; + out_info.type = entry.type; + return true; + } + } + } + else if (item.uuid === uuid) { + out_info.path = path; + out_info.type = item.type; + return true; + } + } + return false; +}; + proto.reset = function () { this._pathToUuid = {}; }; diff --git a/cocos2d/core/load-pipeline/released-asset-checker.js b/cocos2d/core/load-pipeline/released-asset-checker.js new file mode 100644 index 0000000000..85ce5feb08 --- /dev/null +++ b/cocos2d/core/load-pipeline/released-asset-checker.js @@ -0,0 +1,113 @@ +/**************************************************************************** + Copyright (c) 2013-2017 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. + ****************************************************************************/ + +if (CC_DEBUG) { + +var JS = require('../platform/js'); + +// checks if asset was releasable + +function ReleasedAssetChecker () { + // { dependKey: true } + this._releasedKeys = JS.createMap(true); + this._dirty = false; +} + +// mark as released for further checking dependencies +ReleasedAssetChecker.prototype.setReleased = function (item, releasedKey) { + this._releasedKeys[releasedKey] = true; + this._dirty = true; +}; + +var tmpInfo = null; +function getItemDesc (item) { + if (item.uuid) { + if (!tmpInfo) { + tmpInfo = { path: "", type: null }; + } + if (cc.loader._resources._getInfo_DEBUG(item.uuid, tmpInfo)) { + tmpInfo.path = 'resources/' + tmpInfo.path; + return `"${tmpInfo.path}" (type: ${JS.getClassName(tmpInfo.type)}, uuid: ${item.uuid})`; + } + else { + return `"${item.rawUrl}" (${item.uuid})`; + } + } + else { + return `"${item.rawUrl}"`; + } +} + +function doCheckCouldRelease (releasedKey, refOwnerItem, caches) { + var loadedAgain = caches[releasedKey]; + if (!loadedAgain) { + cc.log(`"${releasedKey}" was released but maybe still referenced by ${getItemDesc(refOwnerItem)}`); + } +} + +// check dependencies +ReleasedAssetChecker.prototype.checkCouldRelease = function (caches) { + if (!this._dirty) { + return; + } + this._dirty = false; + + var released = this._releasedKeys; + + // check loader cache + for (let id in caches) { + var item = caches[id]; + if (item.alias) { + item = item.alias; + } + let depends = item.dependKeys; + if (depends) { + for (let i = 0; i < depends.length; ++i) { + let depend = depends[i]; + if (released[depend]) { + doCheckCouldRelease(depend, item, caches); + delete released[depend]; + } + } + } + } + + // // check current scene + // let depends = cc.director.getScene().dependAssets; + // for (let i = 0; i < depends.length; ++i) { + // let depend = depends[i]; + // if (released[depend]) { + // doCheckCouldRelease(depend, item, caches); + // delete released[depend]; + // } + // } + + // clear released + JS.clear(released); +}; + +module.exports = ReleasedAssetChecker; + +} diff --git a/cocos2d/core/platform/js.js b/cocos2d/core/platform/js.js index 134e0ea604..19f69cbaab 100644 --- a/cocos2d/core/platform/js.js +++ b/cocos2d/core/platform/js.js @@ -633,9 +633,12 @@ js.shiftArguments = function () { js.createMap = function (forceDictMode) { var map = Object.create(null); if (forceDictMode) { - const INVALID_IDENTIFIER = '.'; - map[INVALID_IDENTIFIER] = true; - delete map[INVALID_IDENTIFIER]; + const INVALID_IDENTIFIER_1 = '.'; + const INVALID_IDENTIFIER_2 = '/'; + map[INVALID_IDENTIFIER_1] = true; + map[INVALID_IDENTIFIER_2] = true; + delete map[INVALID_IDENTIFIER_1]; + delete map[INVALID_IDENTIFIER_2]; } return map; }; diff --git a/jsb/jsb-director.js b/jsb/jsb-director.js index 8d555c976a..b26870e0f9 100644 --- a/jsb/jsb-director.js +++ b/jsb/jsb-director.js @@ -73,6 +73,8 @@ cc.js.mixin(cc.director, { // WidgetManager cc._widgetManager.init(this); + + cc.loader.init(this); }, purgeDirector: function () {