Quick-cocos2d-x 在线更新

本文借鉴阳光七月的在线更新机制服务器

一、加密: 调用quick框架里面bin/compile_scripts生成game.zip (脚本加密)框架

compile_scripts -i scripts -o game.zip -e xxtea_zip -ek MYKEY -es XTui

而后新建一个文件夹名字叫Hello,把项目里面的res下须要更新的图片拷贝过来 路径要同样,不然在游戏中会找不到资源加密

好比hello/res/a.png,将game.zip拷贝到hello目录下面,最后将hello打包生成hello.zip。lua

二、变下update.lua:首先去服务器请求一下游戏版本信息,参数为本地游戏版本。服务器返回给你版本号,与须要下载的资源{"version":1.2,"list":{"1.1/hello.zip","1.1/hellp.zip"}}。若是版本号不同,就须要下载资源。url

function UpdateScene:getUpdateFile(url )spa

    print("url",url)orm

    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

三、解压:  咱们把数据从服务器上面取下来以后保存在了本地的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 

四、解压完成以后。

if io.exists(device.writablePath.."hello/game.zip") then

CCLuaLoadChunksFromZIP(device.writablePath.."hello/game.zip")

end

须要CCLuaLoadChunksFromZIP一次。另外 ,咱们设置了加密 ,还须要在AppDelegate里面设置一下

pStack->setXXTEAKeyAndSign("MYKEY", 4, "XT", 2);

相关文章
相关标签/搜索