您好,登錄后才能下訂單哦!
熱更新基本思路
得到cocoachina論壇上fysp和akira_cn的幫助,理清了游戲熱更新的思路:
執行AssetsManager后,搜索路徑增加了jsb.fileUtils.getWritablePath()目錄,并且是優先搜索;
需要熱更新js不放在project.json中定義,等AssetsManager更新完了,用cc.loader.load動態加載;
所以在jsb.fileUtils.getWritablePath()目錄下載的資源和js文件,與項目目錄保持一致,那么優先加載新下載的資源 和js文件,再進入游戲,從而實現熱更新。
用cocos new MyGame -l js -d /directory/to/project方式新建一個測試項目,參考sample寫的src/AssetsManager.js:
var __failCount = 0;
var AssetsManagerLoaderScene = cc.Scene.extend({
_am:null,
_progress:null,
_percent:0,
_percentByFile:0,
run:function(){
if (!cc.sys.isNative) {
this.loadGame();
return;
}
var layer = new cc.Layer();
this.addChild(layer);
this._progress = new cc.LabelTTF.create("0%", "Arial", 12);
this._progress.x = cc.winSize.width / 2;
this._progress.y = cc.winSize.height / 2 + 50;
layer.addChild(this._progress);
// android: /data/data/com.huanle.magic/files/
var storagePath = (jsb.fileUtils ? jsb.fileUtils.getWritablePath() : "./");
this._am = new jsb.AssetsManager("res/project.manifest", storagePath);
this._am.retain();
if (!this._am.getLocalManifest().isLoaded())
{
cc.log("Fail to update assets, step skipped.");
this.loadGame();
}
else
{
var that = this;
var listener = new cc.EventListenerAssetsManager(this._am, function(event) {
switch (event.getEventCode()){
case cc.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST:
cc.log("No local manifest file found, skip assets update.");
that.loadGame();
break;
case cc.EventAssetsManager.UPDATE_PROGRESSION:
that._percent = event.getPercent();
that._percentByFile = event.getPercentByFile();
cc.log(that._percent + "%");
var msg = event.getMessage();
if (msg) {
cc.log(msg);
}
break;
case cc.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST:
case cc.EventAssetsManager.ERROR_PARSE_MANIFEST:
cc.log("Fail to download manifest file, update skipped.");
that.loadGame();
break;
case cc.EventAssetsManager.ALREADY_UP_TO_DATE:
case cc.EventAssetsManager.UPDATE_FINISHED:
cc.log("Update finished.");
that.loadGame();
break;
case cc.EventAssetsManager.UPDATE_FAILED:
cc.log("Update failed. " + event.getMessage());
__failCount ++;
if (__failCount < 5)
{
that._am.downloadFailedAssets();
}
else
{
cc.log("Reach maximum fail count, exit update process");
__failCount = 0;
that.loadGame();
}
break;
case cc.EventAssetsManager.ERROR_UPDATING:
cc.log("Asset update error: " + event.getAssetId() + ", " + event.getMessage());
that.loadGame();
break;
case cc.EventAssetsManager.ERROR_DECOMPRESS:
cc.log(event.getMessage());
that.loadGame();
break;
default:
break;
}
});
cc.eventManager.addListener(listener, 1);
this._am.update();
cc.director.runScene(this);
}
this.schedule(this.updateProgress, 0.5);
},
loadGame:function(){
cc.loader.loadJs(["src/files.js"], function(err){
cc.loader.loadJs(jsFiles, function(err){
cc.director.runScene(new HelloWorldScene());
});
});
},
updateProgress:function(dt){
this._progress.string = "" + this._percent;
},
onExit:function(){
cc.log("AssetsManager::onExit");
this._am.release();
this._super();
}
});
修改項目目錄下的main.js:
cc.game.onStart = function(){
cc.view.setDesignResolutionSize(800, 450, cc.ResolutionPolicy.SHOW_ALL);
cc.view.resizeWithBrowserSize(true);
var scene = new AssetsManagerLoaderScene();
scene.run();
};
cc.game.run();
修改項目目錄下的project.json:
{
"project_type": "javascript",
"debugMode" : 1,
"showFPS" : true,
"frameRate" : 60,
"id" : "gameCanvas",
"renderMode" : 0,
"engineDir":"frameworks/cocos2d-html5",
"modules" : ["cocos2d", "extensions"],
"jsList" : [
"src/AssetsManager.js"
]
}
就留一個AssetsManager.js,其他的js都通過它來加載。
增加一個src/files.js,需要動態加載的js文件都寫在jsFiles這個數組里,這樣js文件有增加變化,這個files.js一并更新,方便動態加載:
var jsFiles = [
"src/app.js",
"src/resource.js"
];
項目res目錄增加一個project.manifest文件,AssetsManager.js里會用到:
{
"packageUrl" : "http://10.0.128.219/res",
"remoteManifestUrl" : "http://10.0.128.219/res/project.manifest",
"remoteVersionUrl" : "http://10.0.128.219/res/version.manifest",
"version" : "1.0.0",
"groupVersions" : {
"1" : "1.0.0"
},
"engineVersion" : "3.0 rc0",
"searchPaths" : [
]
}
這里主要配置服務端資源下載地址,具體字段說明,在下面服務端配置里說明。然后用cocos compile -p android編譯打包成一個apk安裝包,等配置好服務端更新資源安裝測試。
{
"packageUrl" : "http://10.0.128.219/res",
"remoteManifestUrl" : "http://10.0.128.219/res/project.manifest",
"remoteVersionUrl" : "http://10.0.128.219/res/version.manifest",
"version" : "1.0.0",
"groupVersions" : {
"1" : "1.0.1"
},
"engineVersion" : "3.0 rc0"
}
測試發現,AssetsManager首先會下載version.manifest文件,如果有更新的版本,那么才會去下載project.manifest,然后下載其中描述的資源文件。
project.manifest如下:
{
"packageUrl" : "http://10.0.128.219/res",
"remoteManifestUrl" : "http://10.0.128.219/res/project.manifest",
"remoteVersionUrl" : "http://10.0.128.219/res/version.manifest",
"version" : "1.0.0",
"groupVersions" : {
"1" : "1.0.1"
},
"engineVersion" : "3.0 rc0",
"assets" : {
"update1" : {
"path" : "src/app.zip",
"md5" : "f6bf54e5a0d42c963cc5ae81bf9dc6c6",
"compressed" : true,
"group" : "1"
}
},
"searchPaths" : [
]
}
寫法和官方文檔里不太一樣,特別是有個groupVersions字段,這個字段來自fysp在cocoachina論壇回答其他網友問題寫的示例,測試發現用來做增量更新很方便,后面再說明。其他字段的說明官方文檔已經很詳細了。
由于客戶端本地project.manifest里groupVersions的版本信息比服務器端的低,所以AssetsManager會下載http://10.0.128.219/res/src/app.zip到手機的/data/data/org.cocos2dx.hellojavascript/files/src/app.zip,并且會自動解壓,但不會刪除壓縮包本身。
建議用root過的android手機測試,否則/data/data是沒有權限查看。運行客戶端測試程序后用adb連接查看:
e:\>adb shell
shell@android:/ $ su
su
shell@android:/ # ls -l /data/data/org.cocos2dx.hellojavascript/files/src/
ls -l /data/data/org.cocos2dx.hellojavascript/files/src/
-rw-rw-rw- app_65 app_65 2228 2014-07-08 14:23 app.js
-rw-rw-rw- app_65 app_65 1552 2014-07-08 14:23 app.zip
shell@android:/ # ls -l /data/data/org.cocos2dx.hellojavascript/files/
ls -l /data/data/org.cocos2dx.hellojavascript/files/
-rw-rw-rw- app_65 app_65 553 2014-07-08 14:23 project.manifest
drwxrwxrwx app_65 app_65 2014-07-08 14:23 src
-rw-rw-rw- app_65 app_65 307 2014-07-08 14:23 version.manifest
用firefox調試連上手機,發現app.js資源地址是/data/data/org.cocos2dx.hellojavascript/files/src/app.js,而不是assets/src/app.js,實現了熱更新:
debug
{
"packageUrl" : "http://10.0.128.219/res",
"remoteManifestUrl" : "http://10.0.128.219/res/project.manifest",
"remoteVersionUrl" : "http://10.0.128.219/res/version.manifest",
"version" : "1.0.0",
"groupVersions" : {
"1" : "1.0.1",
"2" : "1.0.2"
},
"engineVersion" : "3.0 rc0"
}
修改服務端project.manifest:
{
"packageUrl" : "http://10.0.128.219/res",
"remoteManifestUrl" : "http://10.0.128.219/res/project.manifest",
"remoteVersionUrl" : "http://10.0.128.219/res/version.manifest",
"version" : "1.0.0",
"groupVersions" : {
"1" : "1.0.1",
"2" : "1.0.2"
},
"engineVersion" : "3.0 rc0",
"assets" : {
"update1" : {
"path" : "src/app.zip",
"md5" : "f6bf54e5a0d42c963cc5ae81bf9dc6c6",
"compressed" : true,
"group" : "1"
},
"update2" : {
"path" : "src/config.zip",
"md5" : "5d59789090e4143166430b2cf7b313ff",
"compressed" : true,
"group" : "2"
}
},
"searchPaths" : [
]
}
這時在android客戶端測試,已經更新到update1的,只會下載update2的更新,而沒有更新過的,會把update1和update2都下載下來。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。