Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 132 additions & 48 deletions CCBoot.js
Original file line number Diff line number Diff line change
Expand Up @@ -1673,6 +1673,118 @@ cc._initSys = function (config, CONFIG_KEY) {

//+++++++++++++++++++++++++something about sys end+++++++++++++++++++++++++++++

//+++++++++++++++++++++++++something about cc.moduleLoader begin+++++++++++++++++++++++++++++
/**
* A loader to load js by moduleConfig.json.
*/
cc.moduleLoader = {
/**
* Get js list of a module configured in configConfig.json
* @param moduleMap map of "module" in configConfig.json
* @param moduleName the module name to load
* @param dir the dir of userModule relative to index.html
* @param jsAddedCache the cache of js and module that has got
* @returns {Array}
* @private
*/
_getJsListOfModule: function (moduleMap, moduleName, dir, jsAddedCache) {
jsAddedCache = jsAddedCache || {};
if (jsAddedCache[moduleName]) return [];//if this module has been loaded
dir = dir || "";
var jsList = [];
var tempList = moduleMap[moduleName];
if (tempList == null) throw "can not find module [" + moduleName + "]";
var ccPath = cc.path;
for (var i = 0, li = tempList.length; i < li; i++) {
var item = tempList[i];
if (jsAddedCache[item]) continue;
var extname = ccPath.extname(item);
if (!extname) {//this is a module
var arr = this._getJsListOfModule(moduleMap, item, dir, jsAddedCache);
if (arr) jsList = jsList.concat(arr);
} else if (extname.toLowerCase() == ".js") jsList.push(ccPath.join(dir, item));
jsAddedCache[item] = 1;//cache
}
return jsList;
},
/**
* Get all js list of a userModule.
* @param moduleRoot the dir of userModule relative to index.html
* @param modules modules of this userModule to be loaded
* @param cb
*/
getModuleJsList : function(moduleRoot, modules, cb){
var self = this;
//get moduleConfig.json first
var moduleConfigPath = cc.path.join(moduleRoot, "moduleConfig.json");
cc.loader.loadJson([moduleConfigPath], function(err, moduleConfig){
if(err) return cb(err);
var moduleMap = moduleConfig["module"];
var resultList = [];
modules = modules || ["default"];//set it default while it is empty
for(var i = 0, li = modules.length; i < li; ++i){
resultList = resultList.concat(self._getJsListOfModule(moduleMap, modules[i], moduleRoot))
}
cb(null, resultList);
});
},
/**
* Get all js list by userModuleCfgList
* @param {Array} moduleCfgList
* @param cb
*/
getJsList : function(moduleCfgList, cb){
var self = this;
var tempList = [];
for(var i = 0, li = moduleCfgList.length; i < li; ++i){
var cfg = moduleCfgList[i];//cfg can be a string or an object
if(typeof cfg == "string"){
tempList.push({moduleRoot:cfg});
}else{
for(var moduleRoot in cfg){
tempList.push({moduleRoot:moduleRoot, modules:cfg[moduleRoot]});
}
}
}
cc.async.map(tempList, function(value, index, cb1){
self.getModuleJsList(value.moduleRoot, value.modules, cb1);
}, function(err, results){
if(err) return cb(err);

var resultList = []
for(var i = 0, li = results.length; i < li; ++i){
resultList = resultList.concat(results[i]);
}
cb(null, resultList);
});
},

/**
* Load all js list by userModuleCfgList.
* @param {Array} moduleCfgList
* @param cb
*/
load : function(moduleCfgList, cb){
this.getJsList(moduleCfgList, function(err, jsList){
if(err) return cb(err);
cc.loader.loadJs(jsList, cb);
});
},

/**
* Load all js list by userModuleCfgList with loading image.
* @param {Array} moduleCfgList
* @param cb
*/
loadWithImg : function(moduleCfgList, cb){
this.getJsList(moduleCfgList, function(err, jsList){
if(err) return cb(err);
cc.loader.loadJsWithImg(jsList, cb);
});
}
}
//+++++++++++++++++++++++++something about cc.moduleLoader end+++++++++++++++++++++++++++

//+++++++++++++++++++++++++something about CCGame begin+++++++++++++++++++++++++++

/**
Expand Down Expand Up @@ -1953,6 +2065,8 @@ cc.game = /** @lends cc.game# */{
frameRate: "frameRate",
id: "id",
renderMode: "renderMode",
modules: "modules",
userModules: "userModules",
jsList: "jsList",
classReleaseMode: "classReleaseMode"
},
Expand Down Expand Up @@ -2092,70 +2206,40 @@ cc.game = /** @lends cc.game# */{
cc._initSys(self.config, CONFIG_KEY);
},

//cache for js and module that has added into jsList to be loaded.
_jsAddedCache: {},
_getJsListOfModule: function (moduleMap, moduleName, dir) {
var jsAddedCache = this._jsAddedCache;
if (jsAddedCache[moduleName]) return null;
dir = dir || "";
var jsList = [];
var tempList = moduleMap[moduleName];
if (!tempList) throw "can not find module [" + moduleName + "]";
var ccPath = cc.path;
for (var i = 0, li = tempList.length; i < li; i++) {
var item = tempList[i];
if (jsAddedCache[item]) continue;
var extname = ccPath.extname(item);
if (!extname) {
var arr = this._getJsListOfModule(moduleMap, item, dir);
if (arr) jsList = jsList.concat(arr);
} else if (extname.toLowerCase() == ".js") jsList.push(ccPath.join(dir, item));
jsAddedCache[item] = 1;
}
return jsList;
},
/**
* Prepare game.
* @param cb
*/
prepare: function (cb) {
var self = this;
var config = self.config, CONFIG_KEY = self.CONFIG_KEY, engineDir = config[CONFIG_KEY.engineDir], loader = cc.loader;
var config = self.config, CONFIG_KEY = self.CONFIG_KEY, engineDir = config[CONFIG_KEY.engineDir], loader = cc.loader,moduleLoader = cc.moduleLoader;
if (!cc._supportRender) {
throw "The renderer doesn't support the renderMode " + config[CONFIG_KEY.renderMode];
}
self._prepareCalled = true;

var cfgList = [];
var jsList = config[CONFIG_KEY.jsList] || [];
if (cc.Class) {//is single file
//load user's jsList only
loader.loadJsWithImg("", jsList, function (err) {
if (err) throw err;
self._prepared = true;
if (cb) cb();
});
} else {
//load cc's jsList first
var ccModulesPath = cc.path.join(engineDir, "moduleConfig.json");
loader.loadJson(ccModulesPath, function (err, modulesJson) {
if (!cc.Class) {
var moduleCfg4Engine = {};//moduleCfg for cocos
var modules = config[CONFIG_KEY.modules] || [];
if (cc._renderType == cc._RENDER_TYPE_WEBGL)
modules.splice(0, 0, "shaders");
else if (modules.indexOf("core") < 0)
modules.splice(0, 0, "core");
moduleCfg4Engine[engineDir] = modules;
cfgList.push(moduleCfg4Engine);
}
moduleLoader.loadWithImg(cfgList, function(err){
if (err) throw err;
moduleLoader.loadWithImg(config[CONFIG_KEY.userModules] || [], function(err){
if (err) throw err;
var modules = config["modules"] || [];
var moduleMap = modulesJson["module"];
var newJsList = [];
if (cc._renderType == cc._RENDER_TYPE_WEBGL) modules.splice(0, 0, "shaders");
else if (modules.indexOf("core") < 0) modules.splice(0, 0, "core");
for (var i = 0, li = modules.length; i < li; i++) {
var arr = self._getJsListOfModule(moduleMap, modules[i], engineDir);
if (arr) newJsList = newJsList.concat(arr);
}
newJsList = newJsList.concat(jsList);
cc.loader.loadJsWithImg(newJsList, function (err) {
loader.loadJsWithImg("", jsList, function (err) {
if (err) throw err;
self._prepared = true;
self._prepareCalled = true;
if (cb) cb();
});
});
}
});
}
};
cc.game._initConfig();
Expand Down