axmol/cocos/scripting/js-bindings/script/jsb_boot.js

1860 lines
53 KiB
JavaScript
Raw Normal View History

2015-05-05 10:50:19 +08:00
/*
2015-07-29 20:47:28 +08:00
* Copyright (c) 2015 Chukong Technologies Inc.
2015-05-05 10:50:19 +08:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*/
//
// cocos2d boot
//
//+++++++++++++++++++++++++something about async begin+++++++++++++++++++++++++++++++
/**
* Async Pool class, a helper of cc.async
* @param {Object|Array} srcObj
* @param {Number} limit the limit of parallel number
* @param {function} iterator
* @param {function} onEnd
* @param {object} target
* @constructor
*/
cc.AsyncPool = function(srcObj, limit, iterator, onEnd, target){
var self = this;
self._srcObj = srcObj;
self._limit = limit;
self._pool = [];
self._iterator = iterator;
self._iteratorTarget = target;
self._onEnd = onEnd;
self._onEndTarget = target;
self._results = srcObj instanceof Array ? [] : {};
self._isErr = false;
cc.each(srcObj, function(value, index){
self._pool.push({index : index, value : value});
});
self.size = self._pool.length;
self.finishedSize = 0;
self._workingSize = 0;
self._limit = self._limit || self.size;
self.onIterator = function(iterator, target){
self._iterator = iterator;
self._iteratorTarget = target;
};
self.onEnd = function(endCb, endCbTarget){
self._onEnd = endCb;
self._onEndTarget = endCbTarget;
};
self._handleItem = function(){
var self = this;
if(self._pool.length == 0)
return; //return directly if the array's length = 0
if(self._workingSize >= self._limit)
return; //return directly if the working size great equal limit number
var item = self._pool.shift();
var value = item.value, index = item.index;
self._workingSize++;
self._iterator.call(self._iteratorTarget, value, index, function(err){
if(self._isErr)
return;
self.finishedSize++;
self._workingSize--;
if(err) {
self._isErr = true;
if(self._onEnd)
self._onEnd.call(self._onEndTarget, err);
return
}
var arr = Array.prototype.slice.call(arguments, 1);
self._results[this.index] = arr[0];
2015-10-10 00:32:51 +08:00
if (self.finishedSize === self.size) {
if (self._onEnd)
2015-05-05 10:50:19 +08:00
self._onEnd.call(self._onEndTarget, null, self._results);
return;
}
self._handleItem();
}.bind(item), self);
};
self.flow = function(){
var self = this;
2015-10-10 00:32:51 +08:00
if(self._pool.length === 0) {
2015-05-05 10:50:19 +08:00
if(self._onEnd)
self._onEnd.call(self._onEndTarget, null, []);
return;
}
for(var i = 0; i < self._limit; i++)
self._handleItem();
}
};
2015-10-10 00:32:51 +08:00
/**
* @class
*/
cc.async = /** @lends cc.async# */{
2015-05-05 10:50:19 +08:00
/**
* Do tasks series.
* @param {Array|Object} tasks
* @param {function} [cb] callback
* @param {Object} [target]
* @return {cc.AsyncPool}
*/
series : function(tasks, cb, target){
var asyncPool = new cc.AsyncPool(tasks, 1, function(func, index, cb1){
func.call(target, cb1);
}, cb, target);
asyncPool.flow();
return asyncPool;
},
/**
* Do tasks parallel.
* @param {Array|Object} tasks
* @param {function} cb callback
* @param {Object} [target]
* @return {cc.AsyncPool}
*/
parallel : function(tasks, cb, target){
var asyncPool = new cc.AsyncPool(tasks, 0, function(func, index, cb1){
func.call(target, cb1);
}, cb, target);
asyncPool.flow();
return asyncPool;
},
/**
* Do tasks waterfall.
* @param {Array|Object} tasks
* @param {function} cb callback
* @param {Object} [target]
* @return {cc.AsyncPool}
*/
waterfall : function(tasks, cb, target){
var args = [];
2015-10-10 00:32:51 +08:00
var lastResults = [null];//the array to store the last results
2015-05-05 10:50:19 +08:00
var asyncPool = new cc.AsyncPool(tasks, 1,
function (func, index, cb1) {
args.push(function (err) {
args = Array.prototype.slice.call(arguments, 1);
2015-10-10 00:32:51 +08:00
if(tasks.length - 1 === index) lastResults = lastResults.concat(args);//while the last task
2015-05-05 10:50:19 +08:00
cb1.apply(null, arguments);
});
func.apply(target, args);
2015-10-10 00:32:51 +08:00
}, function (err) {
2015-05-05 10:50:19 +08:00
if (!cb)
return;
if (err)
return cb.call(target, err);
2015-10-10 00:32:51 +08:00
cb.apply(target, lastResults);
2015-05-05 10:50:19 +08:00
});
asyncPool.flow();
return asyncPool;
},
/**
* Do tasks by iterator.
* @param {Array|Object} tasks
* @param {function|Object} iterator
2015-10-10 00:32:51 +08:00
* @param {function} [callback]
2015-05-05 10:50:19 +08:00
* @param {Object} [target]
* @return {cc.AsyncPool}
*/
2015-10-10 00:32:51 +08:00
map : function(tasks, iterator, callback, target){
2015-05-05 10:50:19 +08:00
var locIterator = iterator;
2015-10-10 00:32:51 +08:00
if(typeof(iterator) === "object"){
callback = iterator.cb;
2015-05-05 10:50:19 +08:00
target = iterator.iteratorTarget;
locIterator = iterator.iterator;
}
2015-10-10 00:32:51 +08:00
var asyncPool = new cc.AsyncPool(tasks, 0, locIterator, callback, target);
2015-05-05 10:50:19 +08:00
asyncPool.flow();
return asyncPool;
},
/**
* Do tasks by iterator limit.
* @param {Array|Object} tasks
* @param {Number} limit
* @param {function} iterator
* @param {function} cb callback
* @param {Object} [target]
*/
mapLimit : function(tasks, limit, iterator, cb, target){
var asyncPool = new cc.AsyncPool(tasks, limit, iterator, cb, target);
asyncPool.flow();
return asyncPool;
}
};
//+++++++++++++++++++++++++something about async end+++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++something about path begin++++++++++++++++++++++++++++++++
2015-10-10 00:32:51 +08:00
/**
* @class
*/
cc.path = /** @lends cc.path# */{
normalizeRE: /[^\.\/]+\/\.\.\//,
2015-05-05 10:50:19 +08:00
/**
* Join strings to be a path.
* @example
cc.path.join("a", "b.png");//-->"a/b.png"
cc.path.join("a", "b", "c.png");//-->"a/b/c.png"
cc.path.join("a", "b");//-->"a/b"
cc.path.join("a", "b", "/");//-->"a/b/"
cc.path.join("a", "b/", "/");//-->"a/b/"
* @returns {string}
*/
2015-10-10 00:32:51 +08:00
join: function () {
2015-05-05 10:50:19 +08:00
var l = arguments.length;
var result = "";
2015-10-10 00:32:51 +08:00
for (var i = 0; i < l; i++) {
result = (result + (result === "" ? "" : "/") + arguments[i]).replace(/(\/|\\\\)$/, "");
2015-05-05 10:50:19 +08:00
}
return result;
},
2015-10-10 00:32:51 +08:00
2015-05-05 10:50:19 +08:00
/**
* Get the ext name of a path.
* @example
cc.path.extname("a/b.png");//-->".png"
cc.path.extname("a/b.png?a=1&b=2");//-->".png"
cc.path.extname("a/b");//-->null
cc.path.extname("a/b?a=1&b=2");//-->null
2015-10-10 00:32:51 +08:00
* @param {string} pathStr
2015-05-05 10:50:19 +08:00
* @returns {*}
*/
2015-10-10 00:32:51 +08:00
extname: function (pathStr) {
var temp = /(\.[^\.\/\?\\]*)(\?.*)?$/.exec(pathStr);
return temp ? temp[1] : null;
2015-05-05 10:50:19 +08:00
},
2015-07-14 16:33:38 +08:00
/**
* Get the main name of a file name
* @param {string} fileName
* @returns {string}
*/
mainFileName: function(fileName){
if(fileName){
var idx = fileName.lastIndexOf(".");
if(idx !== -1)
return fileName.substring(0,idx);
}
return fileName;
},
2015-10-10 00:32:51 +08:00
2015-05-05 10:50:19 +08:00
/**
* Get the file name of a file path.
* @example
cc.path.basename("a/b.png");//-->"b.png"
cc.path.basename("a/b.png?a=1&b=2");//-->"b.png"
cc.path.basename("a/b.png", ".png");//-->"b"
cc.path.basename("a/b.png?a=1&b=2", ".png");//-->"b"
cc.path.basename("a/b.png", ".txt");//-->"b.png"
2015-10-10 00:32:51 +08:00
* @param {string} pathStr
* @param {string} [extname]
2015-05-05 10:50:19 +08:00
* @returns {*}
*/
2015-10-10 00:32:51 +08:00
basename: function (pathStr, extname) {
2015-05-05 10:50:19 +08:00
var index = pathStr.indexOf("?");
2015-10-10 00:32:51 +08:00
if (index > 0) pathStr = pathStr.substring(0, index);
2015-05-05 10:50:19 +08:00
var reg = /(\/|\\\\)([^(\/|\\\\)]+)$/g;
var result = reg.exec(pathStr.replace(/(\/|\\\\)$/, ""));
2015-10-10 00:32:51 +08:00
if (!result) return null;
2015-05-05 10:50:19 +08:00
var baseName = result[2];
2015-10-10 00:32:51 +08:00
if (extname && pathStr.substring(pathStr.length - extname.length).toLowerCase() === extname.toLowerCase())
2015-05-05 10:50:19 +08:00
return baseName.substring(0, baseName.length - extname.length);
return baseName;
},
2015-10-10 00:32:51 +08:00
2015-05-05 10:50:19 +08:00
/**
2015-10-10 00:32:51 +08:00
* Get dirname of a file path.
2015-05-05 10:50:19 +08:00
* @example
2015-10-10 00:32:51 +08:00
* unix
2015-05-05 10:50:19 +08:00
cc.path.driname("a/b/c.png");//-->"a/b"
cc.path.driname("a/b/c.png?a=1&b=2");//-->"a/b"
2015-10-10 00:32:51 +08:00
cc.path.dirname("a/b/");//-->"a/b"
cc.path.dirname("c.png");//-->""
* windows
cc.path.driname("a\\b\\c.png");//-->"a\b"
cc.path.driname("a\\b\\c.png?a=1&b=2");//-->"a\b"
* @param {string} pathStr
2015-05-05 10:50:19 +08:00
* @returns {*}
*/
2015-10-10 00:32:51 +08:00
dirname: function (pathStr) {
return pathStr.replace(/((.*)(\/|\\|\\\\))?(.*?\..*$)?/, '$2');
2015-05-05 10:50:19 +08:00
},
2015-10-10 00:32:51 +08:00
2015-05-05 10:50:19 +08:00
/**
* Change extname of a file path.
* @example
cc.path.changeExtname("a/b.png", ".plist");//-->"a/b.plist"
cc.path.changeExtname("a/b.png?a=1&b=2", ".plist");//-->"a/b.plist?a=1&b=2"
2015-10-10 00:32:51 +08:00
* @param {string} pathStr
* @param {string} [extname]
2015-05-05 10:50:19 +08:00
* @returns {string}
*/
2015-10-10 00:32:51 +08:00
changeExtname: function (pathStr, extname) {
2015-05-05 10:50:19 +08:00
extname = extname || "";
var index = pathStr.indexOf("?");
var tempStr = "";
2015-10-10 00:32:51 +08:00
if (index > 0) {
2015-05-05 10:50:19 +08:00
tempStr = pathStr.substring(index);
pathStr = pathStr.substring(0, index);
2015-10-10 00:32:51 +08:00
}
2015-05-05 10:50:19 +08:00
index = pathStr.lastIndexOf(".");
2015-10-10 00:32:51 +08:00
if (index < 0) return pathStr + extname + tempStr;
2015-05-05 10:50:19 +08:00
return pathStr.substring(0, index) + extname + tempStr;
},
/**
* Change file name of a file path.
* @example
cc.path.changeBasename("a/b/c.plist", "b.plist");//-->"a/b/b.plist"
cc.path.changeBasename("a/b/c.plist?a=1&b=2", "b.plist");//-->"a/b/b.plist?a=1&b=2"
cc.path.changeBasename("a/b/c.plist", ".png");//-->"a/b/c.png"
cc.path.changeBasename("a/b/c.plist", "b");//-->"a/b/b"
cc.path.changeBasename("a/b/c.plist", "b", true);//-->"a/b/b.plist"
* @param {String} pathStr
* @param {String} basename
2015-10-10 00:32:51 +08:00
* @param {Boolean} [isSameExt]
2015-05-05 10:50:19 +08:00
* @returns {string}
*/
2015-10-10 00:32:51 +08:00
changeBasename: function (pathStr, basename, isSameExt) {
if (basename.indexOf(".") === 0) return this.changeExtname(pathStr, basename);
2015-05-05 10:50:19 +08:00
var index = pathStr.indexOf("?");
var tempStr = "";
var ext = isSameExt ? this.extname(pathStr) : "";
2015-10-10 00:32:51 +08:00
if (index > 0) {
2015-05-05 10:50:19 +08:00
tempStr = pathStr.substring(index);
pathStr = pathStr.substring(0, index);
2015-10-10 00:32:51 +08:00
}
2015-05-05 10:50:19 +08:00
index = pathStr.lastIndexOf("/");
2015-10-10 00:32:51 +08:00
index = index <= 0 ? 0 : index + 1;
2015-05-05 10:50:19 +08:00
return pathStr.substring(0, index) + basename + ext + tempStr;
2015-10-10 00:32:51 +08:00
},
//todo make public after verification
_normalize: function(url){
var oldUrl = url = String(url);
//removing all ../
do {
oldUrl = url;
url = url.replace(this.normalizeRE, "");
} while(oldUrl.length !== url.length);
return url;
2015-05-05 10:50:19 +08:00
}
};
//+++++++++++++++++++++++++something about path end++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++something about loader start+++++++++++++++++++++++++++
cc.loader = {
_resPath : "",
_audioPath : "",
_register : {},//register of loaders
cache : {},//cache for data loaded
_langPathCache : {},//cache for lang path
/**
* Get XMLHttpRequest.
* @returns {XMLHttpRequest}
*/
getXMLHttpRequest : function () {
return new XMLHttpRequest();
},
//@MODE_BEGIN DEV
_jsCache : {},//cache for js
2015-10-10 00:32:51 +08:00
_getArgs4Js: function (args) {
2015-05-05 10:50:19 +08:00
var a0 = args[0], a1 = args[1], a2 = args[2], results = ["", null, null];
2015-10-10 00:32:51 +08:00
if (args.length === 1) {
2015-05-05 10:50:19 +08:00
results[1] = a0 instanceof Array ? a0 : [a0];
2015-10-10 00:32:51 +08:00
} else if (args.length === 2) {
if (typeof a1 === "function") {
2015-05-05 10:50:19 +08:00
results[1] = a0 instanceof Array ? a0 : [a0];
results[2] = a1;
2015-10-10 00:32:51 +08:00
} else {
2015-05-05 10:50:19 +08:00
results[0] = a0 || "";
results[1] = a1 instanceof Array ? a1 : [a1];
}
2015-10-10 00:32:51 +08:00
} else if (args.length === 3) {
2015-05-05 10:50:19 +08:00
results[0] = a0 || "";
results[1] = a1 instanceof Array ? a1 : [a1];
results[2] = a2;
2015-10-10 00:32:51 +08:00
} else throw new Error("arguments error to load js!");
2015-05-05 10:50:19 +08:00
return results;
},
2015-10-10 00:32:51 +08:00
2015-05-05 10:50:19 +08:00
/**
* Load js files.
* @param {?string=} baseDir The pre path for jsList.
* @param {array.<string>} jsList List of js path.
* @param {function} cb Callback function
*
* If the arguments.length == 2, then the baseDir turns to be "".
* @returns {*}
*/
2015-10-10 00:32:51 +08:00
loadJs: function (baseDir, jsList, cb) {
2015-05-05 10:50:19 +08:00
var self = this, localJsCache = self._jsCache,
2015-10-10 00:32:51 +08:00
args = self._getArgs4Js(arguments);
2015-05-05 10:50:19 +08:00
baseDir = args[0];
jsList = args[1];
cb = args[2];
var ccPath = cc.path;
for(var i = 0, li = jsList.length; i < li; ++i){
require(ccPath.join(baseDir, jsList[i]));
}
if(cb) cb();
},
/**
* Load js width loading image.
2015-10-10 00:32:51 +08:00
*
* @param {string} [baseDir]
2015-05-05 10:50:19 +08:00
* @param {array} jsList
2015-10-10 00:32:51 +08:00
* @param {function} [cb]
2015-05-05 10:50:19 +08:00
*/
loadJsWithImg : function(baseDir, jsList, cb){
this.loadJs.apply(this, arguments);
},
//@MODE_END DEV
/**
* Load a single resource as txt.
* @param {!string} url
* @param {function} cb arguments are : err, txt
*/
loadTxt : function(url, cb){
cb(null, jsb.fileUtils.getStringFromFile(url));
},
loadJson : function(url, cb){
this.loadTxt(url, function(err, txt){
try{
err ? cb(err) : cb(null, JSON.parse(txt));
}catch(e){
throw e;
cb("load json [" + url + "] failed : " + e);
}
});
},
/**
* Load a single image.
* @param {!string} url
2015-10-10 00:32:51 +08:00
* @param {object} [option]
2015-05-05 10:50:19 +08:00
* @param {function} cb
* @returns {Image}
*/
2015-10-10 00:32:51 +08:00
loadImg: function (url, option, cb){
2015-05-05 10:50:19 +08:00
var l = arguments.length;
if(l == 2) cb = option;
var cachedTex = cc.textureCache.getTextureForKey(url);
if (cachedTex) {
cb && cb(null, cachedTex);
}
else if (url.match(jsb.urlRegExp)) {
jsb.loadRemoteImg(url, function(succeed, tex) {
if (succeed) {
cb && cb(null, tex);
}
else {
cb && cb("Load image failed");
}
});
}
else {
cc.textureCache._addImageAsync(url, function (tex){
if (tex instanceof cc.Texture2D)
cb && cb(null, tex);
else cb && cb("Load image failed");
});
}
},
/**
* Load binary data by url.
* @param {String} url
* @param {Function} cb
*/
loadBinary : function(url, cb){
cb(null, jsb.fileUtils.getDataFromFile(url));
},
loadBinarySync : function(url){
return jsb.fileUtils.getDataFromFile(url);
},
/**
* Iterator function to load res
* @param {object} item
* @param {number} index
2015-10-10 00:32:51 +08:00
* @param {function} [cb]
2015-05-05 10:50:19 +08:00
* @returns {*}
* @private
*/
2015-10-10 00:32:51 +08:00
_loadResIterator: function (item, index, cb) {
2015-05-05 10:50:19 +08:00
var self = this, url = null;
var type = item.type;
if (type) {
type = "." + type.toLowerCase();
url = item.src ? item.src : item.name + type;
} else {
url = item;
type = cc.path.extname(url);
}
var obj = self.cache[url];
if (obj)
return cb(null, obj);
var loader = null;
if (type) {
loader = self._register[type.toLowerCase()];
}
if (!loader) {
cc.error("loader for [" + type + "] not exists!");
return cb();
}
var basePath = loader.getBasePath ? loader.getBasePath() : self.resPath;
var realUrl = self.getUrl(basePath, url);
2015-07-14 13:40:09 +08:00
loader.load(realUrl, url, item, function (err, data) {
if (err) {
cc.log(err);
self.cache[url] = null;
delete self.cache[url];
cb();
} else {
self.cache[url] = data;
cb(null, data);
}
});
2015-05-05 10:50:19 +08:00
},
/**
* Get url with basePath.
2015-10-10 00:32:51 +08:00
* @param {string} basePath
* @param {string} [url]
2015-05-05 10:50:19 +08:00
* @returns {*}
*/
2015-10-10 00:32:51 +08:00
getUrl: function (basePath, url) {
2015-05-05 10:50:19 +08:00
var self = this, langPathCache = self._langPathCache, path = cc.path;
2015-10-10 00:32:51 +08:00
if (basePath !== undefined && url === undefined) {
2015-05-05 10:50:19 +08:00
url = basePath;
var type = path.extname(url);
type = type ? type.toLowerCase() : "";
var loader = self._register[type];
2015-10-10 00:32:51 +08:00
if (!loader)
2015-05-05 10:50:19 +08:00
basePath = self.resPath;
else
basePath = loader.getBasePath ? loader.getBasePath() : self.resPath;
}
url = cc.path.join(basePath || "", url);
2015-10-10 00:32:51 +08:00
if (url.match(/[\/(\\\\)]lang[\/(\\\\)]/i)) {
if (langPathCache[url])
2015-05-05 10:50:19 +08:00
return langPathCache[url];
var extname = path.extname(url) || "";
url = langPathCache[url] = url.substring(0, url.length - extname.length) + "_" + cc.sys.language + extname;
}
return url;
},
2015-10-10 00:32:51 +08:00
2015-05-05 10:50:19 +08:00
/**
* Load resources then call the callback.
2015-10-10 00:32:51 +08:00
* @param {string} resources
* @param {function} [option] callback or trigger
* @param {function|Object} [loadCallback]
* @return {cc.AsyncPool}
2015-05-05 10:50:19 +08:00
*/
2015-10-10 00:32:51 +08:00
load : function(resources, option, loadCallback){
2015-05-05 10:50:19 +08:00
var self = this;
var len = arguments.length;
2015-10-10 00:32:51 +08:00
if(len === 0)
throw new Error("arguments error!");
2015-05-05 10:50:19 +08:00
2015-10-10 00:32:51 +08:00
if(len === 3){
if(typeof option === "function"){
if(typeof loadCallback === "function")
option = {trigger : option, cb : loadCallback };
2015-05-05 10:50:19 +08:00
else
2015-10-10 00:32:51 +08:00
option = { cb : option, cbTarget : loadCallback};
2015-05-05 10:50:19 +08:00
}
2015-10-10 00:32:51 +08:00
}else if(len === 2){
if(typeof option === "function")
2015-05-05 10:50:19 +08:00
option = {cb : option};
2015-10-10 00:32:51 +08:00
}else if(len === 1){
2015-05-05 10:50:19 +08:00
option = {};
}
if(!(resources instanceof Array))
resources = [resources];
2015-10-10 00:32:51 +08:00
var asyncPool = new cc.AsyncPool(
resources, 0,
function (value, index, AsyncPoolCallback, aPool) {
self._loadResIterator(value, index, function (err) {
var arr = Array.prototype.slice.call(arguments, 1);
if (option.trigger)
option.trigger.call(option.triggerTarget, arr[0], aPool.size, aPool.finishedSize); //call trigger
AsyncPoolCallback(err, arr[0]);
});
},
option.cb, option.cbTarget);
2015-05-05 10:50:19 +08:00
asyncPool.flow();
return asyncPool;
},
/**
* <p>
* Loads alias map from the contents of a filename. <br/>
* <br/>
* @note The plist file name should follow the format below: <br/>
* <?xml version="1.0" encoding="UTF-8"?> <br/>
* <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <br/>
* <plist version="1.0"> <br/>
* <dict> <br/>
* <key>filenames</key> <br/>
* <dict> <br/>
* <key>sounds/click.wav</key> <br/>
* <string>sounds/click.caf</string> <br/>
* <key>sounds/endgame.wav</key> <br/>
* <string>sounds/endgame.caf</string> <br/>
* <key>sounds/gem-0.wav</key> <br/>
* <string>sounds/gem-0.caf</string> <br/>
* </dict> <br/>
* <key>metadata</key> <br/>
* <dict> <br/>
* <key>version</key> <br/>
* <integer>1</integer> <br/>
* </dict> <br/>
* </dict> <br/>
* </plist> <br/>
* </p>
* @param {String} filename The plist file name.
* @param {Function} cb callback
*/
loadAliases : function(url, cb){
jsb.fileUtils.loadFilenameLookup(url);
if(cb) cb();
},
/**
* Register a resource loader into loader.
2015-10-10 00:32:51 +08:00
* @param {string} extNames
* @param {function} loader
2015-05-05 10:50:19 +08:00
*/
2015-10-10 00:32:51 +08:00
register: function (extNames, loader) {
if (!extNames || !loader) return;
2015-05-05 10:50:19 +08:00
var self = this;
2015-10-10 00:32:51 +08:00
if (typeof extNames === "string")
2015-05-05 10:50:19 +08:00
return this._register[extNames.trim().toLowerCase()] = loader;
2015-10-10 00:32:51 +08:00
for (var i = 0, li = extNames.length; i < li; i++) {
2015-05-05 10:50:19 +08:00
self._register["." + extNames[i].trim().toLowerCase()] = loader;
}
},
2015-10-10 00:32:51 +08:00
2015-05-05 10:50:19 +08:00
/**
* Get resource data by url.
* @param url
* @returns {*}
*/
getRes : function(url){
var cached = this.cache[url];
if (cached)
return cached;
var type = cc.path.extname(url);
if (!type) return cc.log("cc.loader.getRes: Invalid url");
2015-05-05 10:50:19 +08:00
var loader = this._register[type.toLowerCase()];
if (!loader) return cc.log("cc.loader.getRes: loader for [" + type + "] not exists!");
2015-05-05 10:50:19 +08:00
var basePath = loader.getBasePath ? loader.getBasePath() : this.resPath;
var realUrl = this.getUrl(basePath, url);
return loader.load(realUrl, url);
},
2015-10-10 00:32:51 +08:00
2015-05-05 10:50:19 +08:00
/**
* Release the cache of resource by url.
* @param url
*/
2015-10-10 00:32:51 +08:00
release: function (url) {
2015-07-14 13:40:09 +08:00
var cache = this.cache;
delete cache[url];
2015-05-05 10:50:19 +08:00
},
2015-10-10 00:32:51 +08:00
2015-05-05 10:50:19 +08:00
/**
* Resource cache of all resources.
*/
2015-10-10 00:32:51 +08:00
releaseAll: function () {
2015-07-14 13:40:09 +08:00
var locCache = this.cache;
for (var key in locCache)
delete locCache[key];
2015-05-05 10:50:19 +08:00
}
};
cc.defineGetterSetter(cc.loader, "resPath", function(){
return this._resPath;
}, function(resPath){
this._resPath = resPath || "";
jsb.fileUtils.addSearchPath(this._resPath);
});
cc.defineGetterSetter(cc.loader, "audioPath", function(){
return this._audioPath;
}, function(audioPath){
this._audioPath = audioPath || "";
jsb.fileUtils.addSearchPath(this._audioPath);
});
//+++++++++++++++++++++++++something about loader end+++++++++++++++++++++++++++++
/**
* A string tool to construct a string with format string.
2015-10-10 00:32:51 +08:00
* for example:
* cc.formatStr("a: %d, b: %b", a, b);
* cc.formatStr(a, b, c);
2015-05-05 10:50:19 +08:00
* @returns {String}
*/
cc.formatStr = function(){
var args = arguments;
var l = args.length;
if(l < 1)
return "";
var str = args[0];
var needToFormat = true;
2015-10-10 00:32:51 +08:00
if(typeof str === "object"){
2015-05-05 10:50:19 +08:00
needToFormat = false;
}
for(var i = 1; i < l; ++i){
var arg = args[i];
if(needToFormat){
while(true){
var result = null;
2015-10-10 00:32:51 +08:00
if(typeof arg === "number"){
2015-05-05 10:50:19 +08:00
result = str.match(/(%d)|(%s)/);
if(result){
str = str.replace(/(%d)|(%s)/, arg);
break;
}
}
result = str.match(/%s/);
if(result)
str = str.replace(/%s/, arg);
else
str += " " + arg;
break;
}
}else
str += " " + arg;
}
return str;
};
2015-10-10 00:32:51 +08:00
//+++++++++++++++++++++++++Engine initialization function begin+++++++++++++++++++++++++++
2015-05-05 10:50:19 +08:00
// Define singleton objects
cc.director = cc.Director.getInstance();
cc.director._actionManager = cc.director.getActionManager();
cc.director._scheduler = cc.director.getScheduler();
2015-05-05 10:50:19 +08:00
cc.winSize = cc.director.getWinSize();
2015-10-10 00:32:51 +08:00
2015-05-05 10:50:19 +08:00
cc.view = cc.director.getOpenGLView();
2015-08-17 15:30:41 +08:00
cc.view.getDevicePixelRatio = cc.view.getRetinaFactor;
2015-05-05 10:50:19 +08:00
cc.view.convertToLocationInView = function (tx, ty, relatedPos) {
var _devicePixelRatio = cc.view.getDevicePixelRatio();
return {x: _devicePixelRatio * (tx - relatedPos.left), y: _devicePixelRatio * (relatedPos.top + relatedPos.height - ty)};
};
cc.view.enableRetina = function(enabled) {};
cc.view.isRetinaEnabled = function() {
var sys = cc.sys;
return (sys.os == sys.OS_IOS || sys.os == sys.OS_OSX) ? true : false;
};
cc.view.adjustViewPort = function() {};
cc.view.resizeWithBrowserSize = function () {return;};
cc.view.setResizeCallback = function() {return;};
cc.view.enableAutoFullScreen = function () {return;};
cc.view.isAutoFullScreenEnabled = function() {return true;};
cc.view._setDesignResolutionSize = cc.view.setDesignResolutionSize;
cc.view.setDesignResolutionSize = function(width,height,resolutionPolicy){
cc.view._setDesignResolutionSize(width,height,resolutionPolicy);
cc.winSize = cc.director.getWinSize();
cc.visibleRect.init();
};
cc.view.setRealPixelResolution = cc.view.setDesignResolutionSize;
2015-05-05 10:50:19 +08:00
cc.view.setResolutionPolicy = function(resolutionPolicy){
var size = cc.view.getDesignResolutionSize();
cc.view.setDesignResolutionSize(size.width,size.height,resolutionPolicy);
};
2015-10-10 00:32:51 +08:00
cc.view.getCanvasSize = cc.view.getFrameSize;
cc.view.getVisibleSizeInPixel = cc.view.getVisibleSize;
cc.view.getVisibleOriginInPixel = cc.view.getVisibleOrigin;
2015-05-05 10:50:19 +08:00
cc.view.setContentTranslateLeftTop = function(){return;};
cc.view.getContentTranslateLeftTop = function(){return null;};
cc.view.setFrameZoomFactor = function(){return;};
cc.view.setOrientation = function () {};
2015-05-05 10:50:19 +08:00
cc.DENSITYDPI_DEVICE = "device-dpi";
cc.DENSITYDPI_HIGH = "high-dpi";
cc.DENSITYDPI_MEDIUM = "medium-dpi";
cc.DENSITYDPI_LOW = "low-dpi";
cc.view.setTargetDensityDPI = function() {};
cc.view.getTargetDensityDPI = function() {return cc.DENSITYDPI_DEVICE;};
cc.eventManager = cc.director.getEventDispatcher();
cc.audioEngine = cc.AudioEngine.getInstance();
cc.audioEngine.end = function(){
this.stopMusic();
this.stopAllEffects();
};
cc.audioEngine.features = {
MULTI_CHANNEL: true,
AUTOPLAY: true
};
2015-10-10 00:32:51 +08:00
2015-05-05 10:50:19 +08:00
cc.configuration = cc.Configuration.getInstance();
2015-10-10 00:32:51 +08:00
2015-05-05 10:50:19 +08:00
cc.textureCache = cc.director.getTextureCache();
cc.TextureCache.prototype._addImageAsync = cc.TextureCache.prototype.addImageAsync;
cc.TextureCache.prototype.addImageAsync = function(url, cb, target) {
var localTex = null;
cc.loader.loadImg(url, function(err, tex) {
if (err) tex = null;
if (cb) {
cb.call(target, tex);
}
localTex = tex;
});
return localTex;
};
// Fix for compatibility with old APIs
cc.TextureCache.prototype._addImage = cc.TextureCache.prototype.addImage;
cc.TextureCache.prototype.addImage = function(url, cb, target) {
if (typeof cb === "function") {
return this.addImageAsync(url, cb, target);
}
else {
if (cb) {
2015-12-17 21:57:19 +08:00
return this._addImage(url, cb);
2015-05-05 10:50:19 +08:00
}
else {
return this._addImage(url);
}
}
};
2015-10-10 00:32:51 +08:00
2015-05-05 10:50:19 +08:00
cc.shaderCache = cc.ShaderCache.getInstance();
cc.animationCache = cc.AnimationCache.getInstance();
cc.spriteFrameCache = cc.SpriteFrameCache.getInstance();
2015-10-10 00:32:51 +08:00
2015-05-05 10:50:19 +08:00
cc.plistParser = cc.PlistParser.getInstance();
// File utils (Temporary, won't be accessible)
cc.fileUtils = cc.FileUtils.getInstance();
cc.fileUtils.setPopupNotify(false);
cc.screen = {
init: function() {},
fullScreen: function() {
return true;
},
requestFullScreen: function(element, onFullScreenChange) {
onFullScreenChange.call();
},
exitFullScreen: function() {
return false;
},
autoFullScreen: function(element, onFullScreenChange) {
onFullScreenChange.call();
}
};
/**
* @type {Object}
* @name jsb.fileUtils
* jsb.fileUtils is the native file utils singleton object,
* please refer to Cocos2d-x API to know how to use it.
* Only available in JSB
*/
jsb.fileUtils = cc.fileUtils;
delete cc.FileUtils;
delete cc.fileUtils;
/**
* @type {Object}
* @name jsb.reflection
* jsb.reflection is a bridge to let you invoke Java static functions.
* please refer to this document to know how to use it: http://www.cocos2d-x.org/docs/manual/framework/html5/v3/reflection/en
* Only available on Android platform
*/
jsb.reflection = {
callStaticMethod : function(){
cc.log("not supported on current platform");
}
};
2015-10-10 00:32:51 +08:00
var _initSys = function () {
/**
* System variables
* @namespace
* @name cc.sys
*/
cc.sys = window.sys || {};
var sys = cc.sys;
2015-05-05 10:50:19 +08:00
/**
* English language code
2015-10-10 00:32:51 +08:00
* @memberof cc.sys
* @name LANGUAGE_ENGLISH
2015-05-05 10:50:19 +08:00
* @constant
* @type {Number}
*/
2015-10-10 00:32:51 +08:00
sys.LANGUAGE_ENGLISH = "en";
2015-05-05 10:50:19 +08:00
/**
* Chinese language code
2015-10-10 00:32:51 +08:00
* @memberof cc.sys
* @name LANGUAGE_CHINESE
2015-05-05 10:50:19 +08:00
* @constant
* @type {Number}
*/
2015-10-10 00:32:51 +08:00
sys.LANGUAGE_CHINESE = "zh";
2015-05-05 10:50:19 +08:00
/**
* French language code
2015-10-10 00:32:51 +08:00
* @memberof cc.sys
* @name LANGUAGE_FRENCH
2015-05-05 10:50:19 +08:00
* @constant
* @type {Number}
*/
2015-10-10 00:32:51 +08:00
sys.LANGUAGE_FRENCH = "fr";
2015-05-05 10:50:19 +08:00
/**
* Italian language code
2015-10-10 00:32:51 +08:00
* @memberof cc.sys
* @name LANGUAGE_ITALIAN
2015-05-05 10:50:19 +08:00
* @constant
* @type {Number}
*/
2015-10-10 00:32:51 +08:00
sys.LANGUAGE_ITALIAN = "it";
2015-05-05 10:50:19 +08:00
/**
* German language code
2015-10-10 00:32:51 +08:00
* @memberof cc.sys
* @name LANGUAGE_GERMAN
2015-05-05 10:50:19 +08:00
* @constant
* @type {Number}
*/
2015-10-10 00:32:51 +08:00
sys.LANGUAGE_GERMAN = "de";
2015-05-05 10:50:19 +08:00
/**
* Spanish language code
2015-10-10 00:32:51 +08:00
* @memberof cc.sys
* @name LANGUAGE_SPANISH
2015-05-05 10:50:19 +08:00
* @constant
* @type {Number}
*/
2015-10-10 00:32:51 +08:00
sys.LANGUAGE_SPANISH = "es";
2015-05-05 10:50:19 +08:00
/**
2015-10-10 00:32:51 +08:00
* Spanish language code
* @memberof cc.sys
* @name LANGUAGE_DUTCH
2015-05-05 10:50:19 +08:00
* @constant
* @type {Number}
*/
2015-10-10 00:32:51 +08:00
sys.LANGUAGE_DUTCH = "du";
2015-05-05 10:50:19 +08:00
/**
* Russian language code
2015-10-10 00:32:51 +08:00
* @memberof cc.sys
* @name LANGUAGE_RUSSIAN
2015-05-05 10:50:19 +08:00
* @constant
* @type {Number}
*/
2015-10-10 00:32:51 +08:00
sys.LANGUAGE_RUSSIAN = "ru";
2015-05-05 10:50:19 +08:00
/**
* Korean language code
2015-10-10 00:32:51 +08:00
* @memberof cc.sys
* @name LANGUAGE_KOREAN
2015-05-05 10:50:19 +08:00
* @constant
* @type {Number}
*/
2015-10-10 00:32:51 +08:00
sys.LANGUAGE_KOREAN = "ko";
2015-05-05 10:50:19 +08:00
/**
* Japanese language code
2015-10-10 00:32:51 +08:00
* @memberof cc.sys
* @name LANGUAGE_JAPANESE
2015-05-05 10:50:19 +08:00
* @constant
* @type {Number}
*/
2015-10-10 00:32:51 +08:00
sys.LANGUAGE_JAPANESE = "ja";
2015-05-05 10:50:19 +08:00
/**
* Hungarian language code
2015-10-10 00:32:51 +08:00
* @memberof cc.sys
* @name LANGUAGE_HUNGARIAN
2015-05-05 10:50:19 +08:00
* @constant
* @type {Number}
*/
2015-10-10 00:32:51 +08:00
sys.LANGUAGE_HUNGARIAN = "hu";
2015-05-05 10:50:19 +08:00
/**
* Portuguese language code
2015-10-10 00:32:51 +08:00
* @memberof cc.sys
* @name LANGUAGE_PORTUGUESE
2015-05-05 10:50:19 +08:00
* @constant
* @type {Number}
*/
2015-10-10 00:32:51 +08:00
sys.LANGUAGE_PORTUGUESE = "pt";
2015-05-05 10:50:19 +08:00
/**
* Arabic language code
2015-10-10 00:32:51 +08:00
* @memberof cc.sys
* @name LANGUAGE_ARABIC
2015-05-05 10:50:19 +08:00
* @constant
* @type {Number}
*/
2015-10-10 00:32:51 +08:00
sys.LANGUAGE_ARABIC = "ar";
2015-05-05 10:50:19 +08:00
/**
* Norwegian language code
2015-10-10 00:32:51 +08:00
* @memberof cc.sys
* @name LANGUAGE_NORWEGIAN
2015-05-05 10:50:19 +08:00
* @constant
* @type {Number}
*/
2015-10-10 00:32:51 +08:00
sys.LANGUAGE_NORWEGIAN = "no";
2015-05-05 10:50:19 +08:00
/**
* Polish language code
2015-10-10 00:32:51 +08:00
* @memberof cc.sys
* @name LANGUAGE_POLISH
2015-05-05 10:50:19 +08:00
* @constant
* @type {Number}
*/
2015-10-10 00:32:51 +08:00
sys.LANGUAGE_POLISH = "pl";
2015-05-05 10:50:19 +08:00
2016-12-13 09:51:46 +08:00
/**
* Turkish language code
* @constant
* @default
* @type {Number}
*/
sys.LANGUAGE_TURKISH = "tr";
/**
* Ukrainian language code
* @constant
* @default
* @type {Number}
*/
sys.LANGUAGE_UKRAINIAN = "uk";
/**
* Romanian language code
* @constant
* @default
* @type {Number}
*/
sys.LANGUAGE_ROMANIAN = "ro";
/**
* Bulgarian language code
* @constant
* @default
* @type {Number}
*/
sys.LANGUAGE_BULGARIAN = "bg";
/**
* Unknown language code
* @memberof cc.sys
* @name LANGUAGE_UNKNOWN
* @constant
* @type {Number}
*/
sys.LANGUAGE_UNKNOWN = "unknown";
2015-05-05 10:50:19 +08:00
/**
* @memberof cc.sys
* @name OS_IOS
* @constant
* @type {string}
*/
sys.OS_IOS = "iOS";
/**
* @memberof cc.sys
* @name OS_ANDROID
* @constant
* @type {string}
*/
sys.OS_ANDROID = "Android";
/**
* @memberof cc.sys
* @name OS_WINDOWS
* @constant
* @type {string}
*/
sys.OS_WINDOWS = "Windows";
/**
* @memberof cc.sys
* @name OS_MARMALADE
* @constant
* @type {string}
*/
sys.OS_MARMALADE = "Marmalade";
/**
* @memberof cc.sys
* @name OS_LINUX
* @constant
* @type {string}
*/
sys.OS_LINUX = "Linux";
/**
* @memberof cc.sys
* @name OS_BADA
* @constant
* @type {string}
*/
sys.OS_BADA = "Bada";
/**
* @memberof cc.sys
* @name OS_BLACKBERRY
* @constant
* @type {string}
*/
sys.OS_BLACKBERRY = "Blackberry";
/**
* @memberof cc.sys
* @name OS_OSX
* @constant
* @type {string}
*/
sys.OS_OSX = "OS X";
/**
* @memberof cc.sys
* @name OS_WP8
* @constant
* @type {string}
*/
sys.OS_WP8 = "WP8";
/**
* @memberof cc.sys
* @name OS_WINRT
* @constant
* @type {string}
*/
sys.OS_WINRT = "WINRT";
/**
* @memberof cc.sys
* @name OS_UNKNOWN
* @constant
* @type {string}
*/
sys.OS_UNKNOWN = "Unknown";
/**
* @memberof cc.sys
* @name UNKNOWN
* @constant
* @default
* @type {Number}
*/
sys.UNKNOWN = -1;
2015-05-05 10:50:19 +08:00
/**
* @memberof cc.sys
* @name WIN32
2015-05-05 10:50:19 +08:00
* @constant
* @default
* @type {Number}
*/
sys.WIN32 = 0;
2015-05-05 10:50:19 +08:00
/**
* @memberof cc.sys
* @name LINUX
2015-05-05 10:50:19 +08:00
* @constant
* @default
* @type {Number}
*/
sys.LINUX = 1;
2015-05-05 10:50:19 +08:00
/**
* @memberof cc.sys
* @name MACOS
2015-05-05 10:50:19 +08:00
* @constant
* @default
* @type {Number}
*/
sys.MACOS = 2;
2015-05-05 10:50:19 +08:00
/**
* @memberof cc.sys
* @name ANDROID
2015-05-05 10:50:19 +08:00
* @constant
* @default
* @type {Number}
*/
sys.ANDROID = 3;
2015-05-05 10:50:19 +08:00
/**
* @memberof cc.sys
* @name IOS
2015-05-05 10:50:19 +08:00
* @constant
* @default
* @type {Number}
*/
sys.IPHONE = 4;
2015-05-05 10:50:19 +08:00
/**
* @memberof cc.sys
* @name IOS
2015-05-05 10:50:19 +08:00
* @constant
* @default
* @type {Number}
*/
sys.IPAD = 5;
2015-05-05 10:50:19 +08:00
/**
* @memberof cc.sys
* @name BLACKBERRY
* @constant
* @default
* @type {Number}
*/
sys.BLACKBERRY = 6;
2015-05-05 10:50:19 +08:00
/**
* @memberof cc.sys
* @name NACL
* @constant
* @default
* @type {Number}
*/
sys.NACL = 7;
2015-05-05 10:50:19 +08:00
/**
* @memberof cc.sys
* @name EMSCRIPTEN
* @constant
* @default
* @type {Number}
*/
sys.EMSCRIPTEN = 8;
2015-05-05 10:50:19 +08:00
/**
* @memberof cc.sys
* @name TIZEN
* @constant
* @default
* @type {Number}
*/
sys.TIZEN = 9;
2015-05-05 10:50:19 +08:00
/**
* @memberof cc.sys
* @name WINRT
2015-05-05 10:50:19 +08:00
* @constant
* @default
* @type {Number}
*/
sys.WINRT = 10;
2015-05-05 10:50:19 +08:00
/**
* @memberof cc.sys
* @name WP8
* @constant
* @default
* @type {Number}
*/
sys.WP8 = 11;
2015-05-05 10:50:19 +08:00
/**
2015-10-10 00:32:51 +08:00
* @memberof cc.sys
* @name MOBILE_BROWSER
2015-05-05 10:50:19 +08:00
* @constant
* @default
* @type {Number}
*/
2015-10-10 00:32:51 +08:00
sys.MOBILE_BROWSER = 100;
2015-05-05 10:50:19 +08:00
/**
2015-10-10 00:32:51 +08:00
* @memberof cc.sys
* @name DESKTOP_BROWSER
2015-05-05 10:50:19 +08:00
* @constant
* @default
* @type {Number}
*/
2015-10-10 00:32:51 +08:00
sys.DESKTOP_BROWSER = 101;
sys.BROWSER_TYPE_WECHAT = "wechat";
sys.BROWSER_TYPE_ANDROID = "androidbrowser";
sys.BROWSER_TYPE_IE = "ie";
sys.BROWSER_TYPE_QQ = "qqbrowser";
sys.BROWSER_TYPE_MOBILE_QQ = "mqqbrowser";
sys.BROWSER_TYPE_UC = "ucbrowser";
sys.BROWSER_TYPE_360 = "360browser";
sys.BROWSER_TYPE_BAIDU_APP = "baiduboxapp";
sys.BROWSER_TYPE_BAIDU = "baidubrowser";
sys.BROWSER_TYPE_MAXTHON = "maxthon";
sys.BROWSER_TYPE_OPERA = "opera";
sys.BROWSER_TYPE_OUPENG = "oupeng";
sys.BROWSER_TYPE_MIUI = "miuibrowser";
sys.BROWSER_TYPE_FIREFOX = "firefox";
sys.BROWSER_TYPE_SAFARI = "safari";
sys.BROWSER_TYPE_CHROME = "chrome";
sys.BROWSER_TYPE_LIEBAO = "liebao";
sys.BROWSER_TYPE_QZONE = "qzone";
sys.BROWSER_TYPE_SOUGOU = "sogou";
sys.BROWSER_TYPE_UNKNOWN = "unknown";
2015-05-05 10:50:19 +08:00
/**
* Is native ? This is set to be true in jsb auto.
2015-10-10 00:32:51 +08:00
* @memberof cc.sys
* @name isNative
2015-05-05 10:50:19 +08:00
* @type {Boolean}
*/
2015-10-10 00:32:51 +08:00
sys.isNative = true;
2015-05-05 10:50:19 +08:00
2015-10-10 00:32:51 +08:00
/**
* Indicate the running platform
* @memberof cc.sys
* @name platform
* @type {Number}
*/
var platform = sys.platform = __getPlatform();
2015-05-05 10:50:19 +08:00
2015-10-10 00:32:51 +08:00
/**
* Indicate whether system is mobile system
* @memberof cc.sys
* @name isMobile
* @type {Boolean}
*/
sys.isMobile = (platform === sys.ANDROID ||
platform === sys.IPAD ||
platform === sys.IPHONE ||
platform === sys.WP8 ||
platform === sys.TIZEN ||
platform === sys.BLACKBERRY) ? true : false;
2015-12-17 21:57:19 +08:00
sys._application = cc.Application.getInstance();
2015-05-05 10:50:19 +08:00
2015-10-10 00:32:51 +08:00
/**
* Indicate the current language of the running system
* @memberof cc.sys
* @name language
* @type {String}
*/
sys.language = (function(){
2015-12-17 21:57:19 +08:00
var language = sys._application.getCurrentLanguage();
2015-05-05 10:50:19 +08:00
switch(language){
2015-10-10 00:32:51 +08:00
case 0: return sys.LANGUAGE_ENGLISH;
case 1: return sys.LANGUAGE_CHINESE;
case 2: return sys.LANGUAGE_FRENCH;
case 3: return sys.LANGUAGE_ITALIAN;
case 4: return sys.LANGUAGE_GERMAN;
case 5: return sys.LANGUAGE_SPANISH;
case 6: return sys.LANGUAGE_DUTCH;
case 7: return sys.LANGUAGE_RUSSIAN;
case 8: return sys.LANGUAGE_KOREAN;
case 9: return sys.LANGUAGE_JAPANESE;
case 10: return sys.LANGUAGE_HUNGARIAN;
case 11: return sys.LANGUAGE_PORTUGUESE;
case 12: return sys.LANGUAGE_ARABIC;
case 13: return sys.LANGUAGE_NORWEGIAN;
case 14: return sys.LANGUAGE_POLISH;
2016-12-13 09:51:46 +08:00
case 15: return sys.LANGUAGE_TURKISH;
case 16: return sys.LANGUAGE_UKRAINIAN;
case 17: return sys.LANGUAGE_ROMANIAN;
case 18: return sys.LANGUAGE_BULGARIAN;
2015-10-10 00:32:51 +08:00
default : return sys.LANGUAGE_ENGLISH;
2015-05-05 10:50:19 +08:00
}
})();
2015-10-10 00:32:51 +08:00
/**
* Indicate the running os name
* @memberof cc.sys
* @name os
* @type {String}
*/
sys.os = __getOS();
/**
* Indicate the running browser type
* @memberof cc.sys
* @name browserType
* @type {String}
*/
sys.browserType = null; //null in jsb
/**
* Indicate the running browser version
* @memberof cc.sys
* @name browserVersion
* @type {Number}
*/
sys.browserVersion = null; //null in jsb
/**
* Indicate the real pixel resolution of the whole game window
* @memberof cc.sys
* @name windowPixelResolution
* @type {Number}
*/
2015-10-10 00:32:51 +08:00
sys.windowPixelResolution = cc.view.getFrameSize();
2015-10-10 00:32:51 +08:00
/**
* The capabilities of the current platform
* @memberof cc.sys
* @name capabilities
* @type {Object}
*/
var capabilities = sys.capabilities = {
"canvas": false,
"opengl": true
};
if( sys.isMobile ) {
2015-05-05 10:50:19 +08:00
capabilities["accelerometer"] = true;
capabilities["touches"] = true;
2015-10-10 00:32:51 +08:00
if (platform === sys.WINRT || platform === sys.WP8) {
capabilities["keyboard"] = true;
}
2015-05-05 10:50:19 +08:00
} else {
// desktop
capabilities["keyboard"] = true;
capabilities["mouse"] = true;
2015-06-25 17:25:27 +08:00
// winrt can't suppot mouse in current version
2015-10-10 00:32:51 +08:00
if (platform === sys.WINRT || platform === sys.WP8)
2015-06-25 17:25:27 +08:00
{
capabilities["touches"] = true;
2015-06-25 17:25:27 +08:00
capabilities["mouse"] = false;
}
2015-05-05 10:50:19 +08:00
}
2015-10-10 00:32:51 +08:00
/**
* Forces the garbage collection, only available in JSB
* @memberof cc.sys
* @name garbageCollect
* @function
*/
sys.garbageCollect = function() {
__jsc__.garbageCollect();
};
/**
* Dumps rooted objects, only available in JSB
* @memberof cc.sys
* @name dumpRoot
* @function
*/
sys.dumpRoot = function() {
__jsc__.dumpRoot();
};
/**
* Restart the JS VM, only available in JSB
* @memberof cc.sys
* @name restartVM
* @function
*/
sys.restartVM = function() {
__restartVM();
};
/**
* Clean a script in the JS VM, only available in JSB
* @memberof cc.sys
* @name cleanScript
* @param {String} jsfile
* @function
*/
sys.cleanScript = function(jsFile) {
__cleanScript(jsFile);
};
/**
* Check whether an object is valid,
* In web engine, it will return true if the object exist
* In native engine, it will return true if the JS object and the correspond native object are both valid
* @memberof cc.sys
* @name isObjectValid
* @param {Object} obj
* @return {boolean} Validity of the object
* @function
*/
sys.isObjectValid = function(obj) {
return __isObjectValid(obj);
};
/**
* Dump system informations
* @memberof cc.sys
* @name dump
* @function
*/
sys.dump = function () {
var self = this;
var str = "";
str += "isMobile : " + self.isMobile + "\r\n";
str += "language : " + self.language + "\r\n";
str += "browserType : " + self.browserType + "\r\n";
str += "capabilities : " + JSON.stringify(self.capabilities) + "\r\n";
str += "os : " + self.os + "\r\n";
str += "platform : " + self.platform + "\r\n";
cc.log(str);
2015-12-17 21:57:19 +08:00
};
2015-10-10 00:32:51 +08:00
2015-05-05 10:50:19 +08:00
/**
* Open a url in browser
* @memberof cc.sys
* @name openURL
* @param {String} url
*/
2015-10-10 00:32:51 +08:00
sys.openURL = function(url){
2015-12-17 21:57:19 +08:00
sys._application.openURL(url);
};
2015-10-10 00:32:51 +08:00
sys.now = function () {
return Date.now();
};
2015-10-10 00:32:51 +08:00
// JS to Native bridges
if(window.JavascriptJavaBridge && cc.sys.os == cc.sys.OS_ANDROID){
jsb.reflection = new JavascriptJavaBridge();
cc.sys.capabilities["keyboard"] = true;
}
else if(window.JavaScriptObjCBridge && (cc.sys.os == cc.sys.OS_IOS || cc.sys.os == cc.sys.OS_OSX)){
jsb.reflection = new JavaScriptObjCBridge();
}
2015-05-05 10:50:19 +08:00
};
2015-10-10 00:32:51 +08:00
_initSys();
2015-05-05 10:50:19 +08:00
/**
* Init Debug setting.
* @function
*/
cc._initDebugSetting = function (mode) {
var ccGame = cc.game;
var bakLog = cc._cocosplayerLog || console.log || log;
cc.log = cc.warn = cc.error = cc.assert = function () {};
if (mode > ccGame.DEBUG_MODE_NONE) {
console.log = function () {
bakLog(cc.formatStr.apply(null, arguments));
};
console.error = function () {
bakLog("ERROR : " + cc.formatStr.apply(cc, arguments));
2015-05-05 10:50:19 +08:00
};
console.warn = function () {
bakLog("WARN : " + cc.formatStr.apply(cc, arguments));
};
cc.error = console.error;
cc.assert = function (cond, msg) {
2015-05-05 10:50:19 +08:00
if (!cond && msg) {
var args = [];
for (var i = 1; i < arguments.length; i++)
args.push(arguments[i]);
console.log("Assert: " + cc.formatStr.apply(cc, args));
2015-05-05 10:50:19 +08:00
}
};
if (mode != ccGame.DEBUG_MODE_ERROR && mode != ccGame.DEBUG_MODE_ERROR_FOR_WEB_PAGE) {
cc.warn = console.warn;
2015-05-05 10:50:19 +08:00
}
if (mode == ccGame.DEBUG_MODE_INFO || mode == ccGame.DEBUG_MODE_INFO_FOR_WEB_PAGE) {
cc.log = console.log;
2015-05-05 10:50:19 +08:00
}
}
};
2015-10-10 00:32:51 +08:00
cc._engineLoaded = false;
cc.initEngine = function (config, cb) {
require("script/jsb.js");
cc._renderType = cc.game.RENDER_TYPE_OPENGL;
cc._initDebugSetting(config[cc.game.CONFIG_KEY.debugMode]);
cc._engineLoaded = true;
cc.log(cc.ENGINE_VERSION);
if (cb) cb();
};
2015-05-05 10:50:19 +08:00
//+++++++++++++++++++++++++something about CCGame begin+++++++++++++++++++++++++++
/**
* An object to boot the game.
2015-10-10 00:32:51 +08:00
* @class
* @name cc.game
2015-05-05 10:50:19 +08:00
*/
2015-10-10 00:32:51 +08:00
cc.game = /** @lends cc.game# */{
DEBUG_MODE_NONE: 0,
DEBUG_MODE_INFO: 1,
DEBUG_MODE_WARN: 2,
DEBUG_MODE_ERROR: 3,
DEBUG_MODE_INFO_FOR_WEB_PAGE: 4,
DEBUG_MODE_WARN_FOR_WEB_PAGE: 5,
DEBUG_MODE_ERROR_FOR_WEB_PAGE: 6,
2015-05-05 10:50:19 +08:00
EVENT_HIDE: "game_on_hide",
EVENT_SHOW: "game_on_show",
2015-10-10 00:32:51 +08:00
EVENT_RESIZE: "game_on_resize",
RENDER_TYPE_CANVAS: 0,
RENDER_TYPE_WEBGL: 1,
RENDER_TYPE_OPENGL: 2,
_eventHide: null,
_eventShow: null,
2015-05-05 10:50:19 +08:00
/**
* Key of config
* @constant
* @type {Object}
*/
2015-10-10 00:32:51 +08:00
CONFIG_KEY: {
width: "width",
height: "height",
engineDir: "engineDir",
modules: "modules",
debugMode: "debugMode",
showFPS: "showFPS",
frameRate: "frameRate",
id: "id",
renderMode: "renderMode",
jsList: "jsList"
2015-05-05 10:50:19 +08:00
},
2015-10-10 00:32:51 +08:00
// states
_paused: false,//whether the game is paused
2015-10-10 00:32:51 +08:00
_prepareCalled: false,//whether the prepare function has been called
_prepared: false,//whether the engine has prepared
2015-10-10 00:32:51 +08:00
_intervalId: null,//interval target of main
2015-05-05 10:50:19 +08:00
/**
* Config of game
* @type {Object}
*/
2015-10-10 00:32:51 +08:00
config: null,
2015-05-05 10:50:19 +08:00
/**
* Callback when the scripts of engine have been load.
* @type {Function}
*/
2015-10-10 00:32:51 +08:00
onStart: null,
2015-05-05 10:50:19 +08:00
/**
* Callback when game exits.
* @type {Function}
*/
2015-10-10 00:32:51 +08:00
onStop: null,
//@Public Methods
// @Game play control
2015-05-05 10:50:19 +08:00
/**
2015-10-10 00:32:51 +08:00
* Set frameRate of game.
* @param frameRate
2015-05-05 10:50:19 +08:00
*/
2015-10-10 00:32:51 +08:00
setFrameRate: function (frameRate) {
var self = this, config = self.config, CONFIG_KEY = self.CONFIG_KEY;
config[CONFIG_KEY.frameRate] = frameRate;
cc.director.setAnimationInterval(1.0/frameRate);
},
2015-05-05 10:50:19 +08:00
/**
2015-10-10 00:32:51 +08:00
* Run the game frame by frame.
2015-05-05 10:50:19 +08:00
*/
2015-10-10 00:32:51 +08:00
step: function () {
cc.director.mainLoop();
},
2015-05-05 10:50:19 +08:00
/**
2015-10-10 00:32:51 +08:00
* Pause the game.
2015-05-05 10:50:19 +08:00
*/
2015-10-10 00:32:51 +08:00
pause: function () {
this._paused = true;
2015-10-10 00:32:51 +08:00
cc.director.pause();
},
2015-05-05 10:50:19 +08:00
/**
2015-10-10 00:32:51 +08:00
* Resume the game from pause.
2015-05-05 10:50:19 +08:00
*/
2015-10-10 00:32:51 +08:00
resume: function () {
this._paused = false;
2015-10-10 00:32:51 +08:00
cc.director.resume();
2015-05-05 10:50:19 +08:00
},
2015-10-10 00:32:51 +08:00
/**
* Check whether the game is paused.
*/
isPaused: function () {
return this._paused;
},
2015-05-05 10:50:19 +08:00
/**
* Restart game.
*/
restart: function () {
__restartVM();
},
2015-10-10 00:32:51 +08:00
2016-05-07 14:37:36 +08:00
/**
* End game, it will close the game window
*/
end: function () {
close();
},
// @Game loading
2015-05-05 10:50:19 +08:00
/**
2015-10-10 00:32:51 +08:00
* Prepare game.
* @param cb
2015-05-05 10:50:19 +08:00
*/
2015-10-10 00:32:51 +08:00
prepare: function (cb) {
var self = this,
config = self.config,
CONFIG_KEY = self.CONFIG_KEY;
this._loadConfig();
// Already prepared
if (this._prepared) {
if (cb) cb();
return;
}
// Prepare called, but not done yet
if (this._prepareCalled) {
return;
}
// Prepare never called and engine ready
if (cc._engineLoaded) {
this._prepareCalled = true;
// Load game scripts
var jsList = config[CONFIG_KEY.jsList];
if (jsList) {
cc.loader.loadJsWithImg(jsList, function (err) {
if (err) throw new Error(err);
self._prepared = true;
if (cb) cb();
});
}
else {
2015-10-10 00:32:51 +08:00
if (cb) cb();
}
2015-10-10 00:32:51 +08:00
return;
2015-05-05 10:50:19 +08:00
}
2015-10-10 00:32:51 +08:00
// Engine not loaded yet
cc.initEngine(this.config, function () {
self.prepare(cb);
});
2015-05-05 10:50:19 +08:00
},
2015-10-10 00:32:51 +08:00
2015-05-05 10:50:19 +08:00
/**
* Run game with configuration object and onStart function.
* @param {Object|Function} [config] Pass configuration object or onStart function
* @param {onStart} [onStart] onStart function to be executed after game initialized
2015-05-05 10:50:19 +08:00
*/
run: function (config, onStart) {
if (typeof config === 'function') {
cc.game.onStart = config;
}
else {
if (config) {
cc.game.config = config;
}
if (typeof onStart === 'function') {
cc.game.onStart = onStart;
}
}
this.prepare(cc.game.onStart && cc.game.onStart.bind(cc.game));
2015-10-10 00:32:51 +08:00
},
//@Private Methods
2015-10-10 00:32:51 +08:00
_loadConfig: function () {
// Load config
// Already loaded
if (this.config) {
this._initConfig(this.config);
}
// Load from project.json
else {
try {
var txt = jsb.fileUtils.getStringFromFile("project.json");
var data = JSON.parse(txt);
this._initConfig(data || {});
} catch (e) {
console.log("Failed to read or parse project.json");
this._initConfig({});
}
2015-05-05 10:50:19 +08:00
}
},
_initConfig: function (config) {
var CONFIG_KEY = this.CONFIG_KEY;
// Configs adjustment
config[CONFIG_KEY.showFPS] = typeof config[CONFIG_KEY.showFPS] === 'undefined' ? true : config[CONFIG_KEY.showFPS];
config[CONFIG_KEY.engineDir] = config[CONFIG_KEY.engineDir] || "frameworks/cocos2d-html5";
if (config[CONFIG_KEY.debugMode] == null)
config[CONFIG_KEY.debugMode] = 0;
config[CONFIG_KEY.frameRate] = config[CONFIG_KEY.frameRate] || 60;
if (config[CONFIG_KEY.renderMode] == null)
config[CONFIG_KEY.renderMode] = 0;
this.config = config;
2015-05-05 10:50:19 +08:00
cc.director.setDisplayStats(this.config[CONFIG_KEY.showFPS]);
cc.director.setAnimationInterval(1.0/this.config[CONFIG_KEY.frameRate]);
}
};
//+++++++++++++++++++++++++something about CCGame end+++++++++++++++++++++++++++++
jsb.urlRegExp = new RegExp(
"^" +
// protocol identifier
"(?:(?:https?|ftp)://)" +
// user:pass authentication
"(?:\\S+(?::\\S*)?@)?" +
"(?:" +
// IP address dotted notation octets
// excludes loopback network 0.0.0.0
// excludes reserved space >= 224.0.0.0
// excludes network & broacast addresses
// (first & last IP address of each class)
"(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" +
"(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" +
"(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" +
"|" +
// host name
"(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)" +
// domain name
"(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*" +
// TLD identifier
"(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))" +
2015-10-10 00:32:51 +08:00
"|" +
"(?:localhost)" +
2015-05-05 10:50:19 +08:00
")" +
// port number
"(?::\\d{2,5})?" +
// resource path
"(?:/\\S*)?" +
"$", "i"
);