From 43a5b6f0206fa3be079f8f3a7abab525ed49cbb3 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Tue, 14 Jul 2015 13:40:09 +0800 Subject: [PATCH 1/2] Fix jsb loaders issues --- .../scripting/js-bindings/script/jsb_boot.js | 30 ++++++++++++------- .../js-bindings/script/jsb_loaders.js | 29 +++++++++--------- 2 files changed, 34 insertions(+), 25 deletions(-) diff --git a/cocos/scripting/js-bindings/script/jsb_boot.js b/cocos/scripting/js-bindings/script/jsb_boot.js index 09b4123a9c8f..7f3eaaf8146c 100644 --- a/cocos/scripting/js-bindings/script/jsb_boot.js +++ b/cocos/scripting/js-bindings/script/jsb_boot.js @@ -625,15 +625,18 @@ cc.loader = { } var basePath = loader.getBasePath ? loader.getBasePath() : self.resPath; var realUrl = self.getUrl(basePath, url); - var data = loader.load(realUrl, url); - if (data) { - self.cache[url] = data; - cb(null, data); - } else { - self.cache[url] = null; - delete self.cache[url]; - cb(); - } + + 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); + } + }); }, /** @@ -776,13 +779,18 @@ cc.loader = { * Release the cache of resource by url. * @param url */ - release : function(url){//do nothing in jsb + release : function(url){ + var cache = this.cache; + delete cache[url]; }, /** * Resource cache of all resources. */ - releaseAll : function(){//do nothing in jsb + releaseAll : function(){ + var locCache = this.cache; + for (var key in locCache) + delete locCache[key]; } }; diff --git a/cocos/scripting/js-bindings/script/jsb_loaders.js b/cocos/scripting/js-bindings/script/jsb_loaders.js index 9ef7ec5900b3..2a1e23f7e1ae 100644 --- a/cocos/scripting/js-bindings/script/jsb_loaders.js +++ b/cocos/scripting/js-bindings/script/jsb_loaders.js @@ -27,8 +27,8 @@ // cc._emptyLoader = { - load : function(realUrl, url){ - return null; + load : function(realUrl, url, res, cb){ + cb && cb(null, null); } }; @@ -39,21 +39,22 @@ cc.loader.register([ cc._emptyLoader); cc._txtLoader = { - load : function(realUrl, url){ - return jsb.fileUtils.getStringFromFile(realUrl); + load : function(realUrl, url, res, cb){ + cb && cb(null, jsb.fileUtils.getStringFromFile(realUrl)); } }; cc.loader.register(["txt", "xml", "vsh", "fsh", "tmx", "tsx"], cc._txtLoader); cc._jsonLoader = { - load : function(realUrl, url){ - var data = jsb.fileUtils.getStringFromFile(realUrl); + load : function(realUrl, url, res, cb){ + var data = jsb.fileUtils.getStringFromFile(realUrl), result; try{ - return JSON.parse(data); + result = JSON.parse(data); }catch(e){ cc.error(e); - return null; + result = null; } + cb && cb(null, result); } }; cc.loader.register(["json", "ExportJson"], cc._jsonLoader); @@ -73,16 +74,16 @@ cc._imgLoader = { cc.loader.register(["png", "jpg", "bmp","jpeg","gif"], cc._imgLoader); cc._plistLoader = { - load : function(realUrl, url){ + load : function(realUrl, url, res, cb){ var content = jsb.fileUtils.getStringFromFile(realUrl); - return cc.plistParser.parse(content); + cb && cb(null, cc.plistParser.parse(content)); } }; cc.loader.register(["plist"], cc._plistLoader); cc._binaryLoader = { - load : function(realUrl, url){ - return cc.loader.loadBinarySync(realUrl); + load : function(realUrl, url, res, cb){ + cb && cb(null, cc.loader.loadBinarySync(realUrl)); } }; cc.loader.register(["ccbi"], cc._binaryLoader); @@ -166,9 +167,9 @@ cc._fntLoader = { return fnt; }, - load : function(realUrl, url){ + load : function(realUrl, url, res, cb){ var data = jsb.fileUtils.getStringFromFile(realUrl); - return this.parseFnt(data, url); + cb && cb(null, this.parseFnt(data, url)); } }; cc.loader.register(["fnt"], cc._fntLoader); From 09775d6f2af4aa56e1d705e39a79abec7d0866f3 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Tue, 14 Jul 2015 14:14:36 +0800 Subject: [PATCH 2/2] Ensure backward compatibility --- .../js-bindings/script/jsb_loaders.js | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/cocos/scripting/js-bindings/script/jsb_loaders.js b/cocos/scripting/js-bindings/script/jsb_loaders.js index 2a1e23f7e1ae..1259e1a6c69c 100644 --- a/cocos/scripting/js-bindings/script/jsb_loaders.js +++ b/cocos/scripting/js-bindings/script/jsb_loaders.js @@ -29,6 +29,7 @@ cc._emptyLoader = { load : function(realUrl, url, res, cb){ cb && cb(null, null); + return null; } }; @@ -40,7 +41,9 @@ cc.loader.register([ cc._txtLoader = { load : function(realUrl, url, res, cb){ - cb && cb(null, jsb.fileUtils.getStringFromFile(realUrl)); + var result = jsb.fileUtils.getStringFromFile(realUrl); + cb && cb(null, result); + return result; } }; cc.loader.register(["txt", "xml", "vsh", "fsh", "tmx", "tsx"], cc._txtLoader); @@ -55,6 +58,7 @@ cc._jsonLoader = { result = null; } cb && cb(null, result); + return result; } }; cc.loader.register(["json", "ExportJson"], cc._jsonLoader); @@ -76,14 +80,18 @@ cc.loader.register(["png", "jpg", "bmp","jpeg","gif"], cc._imgLoader); cc._plistLoader = { load : function(realUrl, url, res, cb){ var content = jsb.fileUtils.getStringFromFile(realUrl); - cb && cb(null, cc.plistParser.parse(content)); + var result = cc.plistParser.parse(content); + cb && cb(null, result); + return result; } }; cc.loader.register(["plist"], cc._plistLoader); cc._binaryLoader = { load : function(realUrl, url, res, cb){ - cb && cb(null, cc.loader.loadBinarySync(realUrl)); + var result = cc.loader.loadBinarySync(realUrl); + cb && cb(null, result); + return result; } }; cc.loader.register(["ccbi"], cc._binaryLoader); @@ -168,8 +176,11 @@ cc._fntLoader = { }, load : function(realUrl, url, res, cb){ - var data = jsb.fileUtils.getStringFromFile(realUrl); - cb && cb(null, this.parseFnt(data, url)); + var data = jsb.fileUtils.getStringFromFile(realUrl), result = null; + if (data) + result = this.parseFnt(data, url); + cb && cb(null, result); + return result; } }; cc.loader.register(["fnt"], cc._fntLoader);