非常有用,原地址如下,拷貝下來,避免自己忘記了。
原地址:http://www.kankanews.com/ICkengine/archives/129102.shtml
本文借鑑陽光七月的在線更新機制
1、加密: 調用quick框架裏面bin/compile_scripts生成game.zip (腳本加密)
compile_scripts -i scripts -o game.zip -e xxtea_zip -ek MYKEY -es XT
然後新建一個文件夾名字叫Hello,把項目裏面的res下需要更新的圖片拷貝過來 路徑要一樣,否則在遊戲中會找不到資源
比如hello/res/a.png,將game.zip拷貝到hello目錄下面,最後將hello打包生成hello.zip。
2、變下update.lua:首先去服務器請求一下游戲版本信息,參數爲本地遊戲版本。服務器返回給你版本號,與需要下載的資源{“version”:1.2,”list”:{“1.1/hello.zip”,”1.1/hellp.zip”}}。如果版本號不一樣,就需要下載資源。
function UpdateScene:getUpdateFile(url )
print(“url”,url)
self.requestCount = self.requestCount + 1
local index = self.requestCount
local request = network.createHTTPRequest(function(event)
self:getUpdateFileComplete(event, index)
end, url, “GET”)
if request then
request:setTimeout(waittime or 30)
request:start()
else
self:endProcess()
end
end
function UpdateScene:getUpdateFileComplete( event, index, dumpResponse )
local request = event.request
printf(“REQUEST %d – event.name = %s”, index, event.name)
if event.name == “completed” then
printf(“REQUEST %d – getResponseStatusCode() = %d”, index, request:getResponseStatusCode())
if request:getResponseStatusCode() ~= 200 then
self:endProcess()
else
io.writefile(self.path..”hello.zip”,request:getResponseData()) — 保存到本地的hello.zip
end
else
printf(“REQUEST %d – getErrorCode() = %d, getErrorMessage() = %s”, index, request:getErrorCode(), request:getErrorMessage())
self:endProcess()
end
end
3、解壓: 我們把數據從服務器上面取下來之後保存在了本地的hello.zip ,那麼我們就需要解壓這個文件。
我是直接複製了assetsmanager裏面的uncompress方法,放到了一個新的C++類裏面 fileUtil裏面,然後在用tolua導出lua可用的接口。具體新的類我放在了extensions/MyClass下面。
a、編寫C++類
b、修改luabinding/extensions/cocos-ext.tolua
c、在luabinding/extensions/MyClass添加tolua文件
d、運行lib/luabinding/build.sh
4、解壓完成之後。
if io.exists(device.writablePath..”hello/game.zip”) then
CCLuaLoadChunksFromZIP(device.writablePath..”hello/game.zip”)
end
需要CCLuaLoadChunksFromZIP一次。另外 ,我們設置了加密 ,還需要在AppDelegate裏面設置一下
pStack->setXXTEAKeyAndSign(“MYKEY“, 4, “XT“, 2);