Skip to content

Commit

Permalink
Refactor cc.loader (cocos#3626)
Browse files Browse the repository at this point in the history
* refactor load-pipeline

add annotations of API

change config to class

add bundle

refine finalizer

add circular reference check, add bundle, add static reference count

add persist node check

refine API annotation

refine deprecation

preprocess md5map

adjust bundle

fix bugs

fixed a lot of bugs

fix bugs

* rename builtinResMgr to builtins

* refine

* adjust unit test

* adjust unit test

* add assetManager unit test

* modified loading priority of asset

* merge

* fix bugs

* adjust autoRelease in unit test

* compatible with cc.game._sceneInfos

* transfer default map as paramter to Cache

* remove dispatch, adjust API:loadBundle
  • Loading branch information
SantyWang authored and jareguo committed Aug 15, 2019
1 parent 6e552ea commit 02dad34
Show file tree
Hide file tree
Showing 91 changed files with 7,775 additions and 5,477 deletions.
6 changes: 1 addition & 5 deletions cocos2d/audio/CCAudio.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,7 @@ Audio.State = {
self._onLoaded();
}
});
cc.loader.load({
url: clip.nativeUrl,
// For audio, we should skip loader otherwise it will load a new audioClip.
skips: ['Loader'],
},
cc.assetManager.loadNativeFile(clip,
function (err, audioNativeAsset) {
if (err) {
cc.error(err);
Expand Down
12 changes: 6 additions & 6 deletions cocos2d/audio/CCAudioEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ var audioEngine = {
* @param {Number} volume - Volume size.
* @return {Number} audioId
* @example
* cc.loader.loadRes(url, cc.AudioClip, function (err, clip) {
* cc.assetManager.loadRes(url, cc.AudioClip, null, function (err, clip) {
* var audioID = cc.audioEngine.play(clip, false, 0.5);
* });
*/
Expand Down Expand Up @@ -517,14 +517,14 @@ var audioEngine = {
* @param {Function} [callback] - The callback of an audio.
* @example
* cc.audioEngine.preload(path);
* @deprecated `cc.audioEngine.preload` is deprecated, use `cc.loader.loadRes(url, cc.AudioClip)` instead please.
* @deprecated `cc.audioEngine.preload` is deprecated, use `cc.assetManager.loadRes(url, cc.AudioClip)` instead please.
*/
preload: function (filePath, callback) {
if (CC_DEBUG) {
cc.warn('`cc.audioEngine.preload` is deprecated, use `cc.loader.loadRes(url, cc.AudioClip)` instead please.');
cc.warn('`cc.audioEngine.preload` is deprecated, use `cc.assetManager.loadRes(url, cc.AudioClip)` instead please.');
}

cc.loader.load(filePath, callback && function (error) {
cc.assetManager.loadRes(filePath, cc.AudioClip, null, callback && function (error) {
if (!error) {
callback();
}
Expand Down Expand Up @@ -591,7 +591,7 @@ var audioEngine = {
* @param {Boolean} loop - Whether the music loop or not.
* @return {Number} audioId
* @example
* cc.loader.loadRes(url, cc.AudioClip, function (err, clip) {
* cc.assetManager.loadRes(url, cc.AudioClip, null, function (err, clip) {
* var audioID = cc.audioEngine.playMusic(clip, false);
* });
*/
Expand Down Expand Up @@ -686,7 +686,7 @@ var audioEngine = {
* @param {Boolean} loop - Whether the music loop or not.
* @return {Number} audioId
* @example
* cc.loader.loadRes(url, cc.AudioClip, function (err, clip) {
* cc.assetManager.loadRes(url, cc.AudioClip, null, function (err, clip) {
* var audioID = cc.audioEngine.playEffect(clip, false);
* });
*/
Expand Down
3 changes: 3 additions & 0 deletions cocos2d/core/3d/CCModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ let Model = cc.Class({

ctor () {
this._rootNode = null;
this.loaded = false;
},

properties: {
Expand Down Expand Up @@ -83,6 +84,8 @@ let Model = cc.Class({
}
}
}
this.loaded = true;
this.emit("load");
}
});

Expand Down
3 changes: 3 additions & 0 deletions cocos2d/core/3d/skeleton/CCSkeleton.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ let Skeleton = cc.Class({
extends: cc.Asset,

ctor () {
this.loaded = false;
this._bindposes = [];
this._uniqueBindPoses = [];
this._jointPaths = [];
Expand Down Expand Up @@ -77,6 +78,8 @@ let Skeleton = cc.Class({
bindposes[i] = node.bindpose[this._skinIndex];
}
}
this.loaded = true;
this.emit("load");
}
});

Expand Down
179 changes: 24 additions & 155 deletions cocos2d/core/CCDirector.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
****************************************************************************/

const EventTarget = require('./event/event-target');
const AutoReleaseUtils = require('./load-pipeline/auto-release-utils');
const ComponentScheduler = require('./component-scheduler');
const NodeActivator = require('./node-activator');
const Obj = require('./platform/CCObject');
Expand Down Expand Up @@ -116,8 +115,8 @@ cc.Director = function () {
this._winSizeInPoints = null;

// scenes
this._loadingScene = '';
this._scene = null;
this._loadingScene = '';

// FPS
this._totalFrames = 0;
Expand Down Expand Up @@ -206,8 +205,6 @@ cc.Director.prototype = {
if (cc._widgetManager) {
cc._widgetManager.init(this);
}

cc.loader.init(this);
},

/**
Expand Down Expand Up @@ -331,7 +328,7 @@ cc.Director.prototype = {
* @deprecated since v2.0
*/
purgeCachedData: function () {
cc.loader.releaseAll();
cc.assetManager.releaseAll(true);
},

/**
Expand All @@ -355,13 +352,13 @@ cc.Director.prototype = {
this._scene = null;

cc.renderer.clear();
cc.AssetLibrary.resetBuiltins();
cc.assetManager.builtins.clear();
}

cc.game.pause();

// Clear all caches
cc.loader.releaseAll();
cc.assetManager.releaseAll(true);
},

/**
Expand Down Expand Up @@ -437,8 +434,7 @@ cc.Director.prototype = {
if (!CC_EDITOR) {
// auto release assets
CC_BUILD && CC_DEBUG && console.time('AutoRelease');
var autoReleaseAssets = oldScene && oldScene.autoReleaseAssets && oldScene.dependAssets;
AutoReleaseUtils.autoRelease(autoReleaseAssets, scene.dependAssets, persistNodeList);
cc.assetManager.finalizer._autoRelease(oldScene, scene, persistNodeList);
CC_BUILD && CC_DEBUG && console.timeEnd('AutoRelease');
}

Expand Down Expand Up @@ -499,39 +495,6 @@ cc.Director.prototype = {
}, this);
},

// @Scene loading section

_getSceneUuid: function (key) {
var scenes = game._sceneInfos;
if (typeof key === 'string') {
if (!key.endsWith('.fire')) {
key += '.fire';
}
if (key[0] !== '/' && !key.startsWith('db://')) {
key = '/' + key; // 使用全名匹配
}
// search scene
for (var i = 0; i < scenes.length; i++) {
var info = scenes[i];
if (info.url.endsWith(key)) {
return info;
}
}
}
else if (typeof key === 'number') {
if (0 <= key && key < scenes.length) {
return scenes[key];
}
else {
cc.errorID(1206, key);
}
}
else {
cc.errorID(1207, key);
}
return null;
},

/**
* !#en Loads the scene by its name.
* !#zh 通过场景名称进行加载场景。
Expand All @@ -546,12 +509,27 @@ cc.Director.prototype = {
cc.errorID(1208, sceneName, this._loadingScene);
return false;
}
var info = this._getSceneUuid(sceneName);
if (info) {
var uuid = info.uuid;
var bundle = cc.assetManager._bundles.find(function (bundle) {
return bundle._config.getSceneInfo(sceneName);
});
if (bundle) {
this.emit(cc.Director.EVENT_BEFORE_SCENE_LOADING, sceneName);
this._loadingScene = sceneName;
this._loadSceneByUuid(uuid, onLaunched, _onUnloaded);
var self = this;
console.time('LoadScene ' + sceneName);
bundle.loadScene(sceneName, function (err, scene) {
console.timeEnd('LoadScene ' + sceneName);
self._loadingScene = '';
if (err) {
err = 'Failed to load scene: ' + err;
cc.error(err);
onLaunched && onLaunched(err);
}
else {
self.runSceneImmediate(scene, _onUnloaded, onLaunched);
return;
}
});
return true;
}
else {
Expand All @@ -560,115 +538,6 @@ cc.Director.prototype = {
}
},

/**
* !#en
* Preloads the scene to reduces loading time. You can call this method at any time you want.
* After calling this method, you still need to launch the scene by `cc.director.loadScene`.
* It will be totally fine to call `cc.director.loadScene` at any time even if the preloading is not
* yet finished, the scene will be launched after loaded automatically.
* !#zh 预加载场景,你可以在任何时候调用这个方法。
* 调用完后,你仍然需要通过 `cc.director.loadScene` 来启动场景,因为这个方法不会执行场景加载操作。
* 就算预加载还没完成,你也可以直接调用 `cc.director.loadScene`,加载完成后场景就会启动。
*
* @method preloadScene
* @param {String} sceneName - The name of the scene to preload.
* @param {Function} [onProgress] - callback, will be called when the load progression change.
* @param {Number} onProgress.completedCount - The number of the items that are already completed
* @param {Number} onProgress.totalCount - The total number of the items
* @param {Object} onProgress.item - The latest item which flow out the pipeline
* @param {Function} [onLoaded] - callback, will be called after scene loaded.
* @param {Error} onLoaded.error - null or the error object.
* @param {cc.SceneAsset} onLoaded.asset - The scene asset itself.
*/
preloadScene: function (sceneName, onProgress, onLoaded) {
if (onLoaded === undefined) {
onLoaded = onProgress;
onProgress = null;
}

var info = this._getSceneUuid(sceneName);
if (info) {
this.emit(cc.Director.EVENT_BEFORE_SCENE_LOADING, sceneName);
cc.loader.load({ uuid: info.uuid, type: 'uuid' },
onProgress,
function (error, asset) {
if (error) {
cc.errorID(1210, sceneName, error.message);
}
if (onLoaded) {
onLoaded(error, asset);
}
});
}
else {
var error = 'Can not preload the scene "' + sceneName + '" because it is not in the build settings.';
onLoaded(new Error(error));
cc.error('preloadScene: ' + error);
}
},

/**
* Loads the scene by its uuid.
* @method _loadSceneByUuid
* @param {String} uuid - the uuid of the scene asset to load
* @param {Function} [onLaunched]
* @param {Function} [onUnloaded]
* @param {Boolean} [dontRunScene] - Just download and initialize the scene but will not launch it,
* only take effect in the Editor.
* @private
*/
_loadSceneByUuid: function (uuid, onLaunched, onUnloaded, dontRunScene) {
if (CC_EDITOR) {
if (typeof onLaunched === 'boolean') {
dontRunScene = onLaunched;
onLaunched = null;
}
if (typeof onUnloaded === 'boolean') {
dontRunScene = onUnloaded;
onUnloaded = null;
}
}
//cc.AssetLibrary.unloadAsset(uuid); // force reload
console.time('LoadScene ' + uuid);
cc.AssetLibrary.loadAsset(uuid, function (error, sceneAsset) {
console.timeEnd('LoadScene ' + uuid);
var self = cc.director;
self._loadingScene = '';
if (error) {
error = 'Failed to load scene: ' + error;
cc.error(error);
}
else {
if (sceneAsset instanceof cc.SceneAsset) {
var scene = sceneAsset.scene;
scene._id = sceneAsset._uuid;
scene._name = sceneAsset._name;
if (CC_EDITOR) {
if (!dontRunScene) {
self.runSceneImmediate(scene, onUnloaded, onLaunched);
}
else {
scene._load();
if (onLaunched) {
onLaunched(null, scene);
}
}
}
else {
self.runSceneImmediate(scene, onUnloaded, onLaunched);
}
return;
}
else {
error = 'The asset ' + uuid + ' is not a scene';
cc.error(error);
}
}
if (onLaunched) {
onLaunched(error);
}
});
},

/**
* !#en Resume game logic execution after pause, if the current scene is not paused, nothing will happen.
Expand Down
Loading

0 comments on commit 02dad34

Please sign in to comment.