Quick-cocos2d-x的热更新机制实现(终极版2)(更新3.3版本)

模块支持:
1.游戏热更新。
2.framework的更新。
3.自身更新。
4.平台初始化模块嵌入(现在的游戏除了app store上 不需要集成第三方SDK,其他的基本上都需要集成各种平台SDK, 所以就加上了这个功能)。
5.更新进度显示。
6.纯lua实现。

模块流程如下
第一步修改AppDelegate.cpp 逻辑

#include "AppPlatform.h"


bool AppDelegate::applicationDidFinishLaunching()
{
    // initialize director
    CCDirector *pDirector = CCDirector::sharedDirector();
    pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
    pDirector->setProjection(kCCDirectorProjection2D);


    // set FPS. the default value is 1.0/60 if you don't call this
    pDirector->setAnimationInterval(1.0 / 60);


    initResourcePath();
 
   // register lua engine
   CCLuaEngine *pEngine = CCLuaEngine::defaultEngine();
   CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine);


    CCLuaStack *pStack = pEngine->getLuaStack();


#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
     string path = CCFileUtils::sharedFileUtils()->fullPathForFilename("scripts/main.lua");
#else
     string path = CCFileUtils::sharedFileUtils()->fullPathForFilename(m_projectConfig.getScriptFileRealPath().c_str());
#endif

     size_t pos;
     while ((pos = path.find_first_of("\\")) != std::string::npos)
     {
         path.replace(pos, 1, "/");
     }
     size_t p = path.find_last_of("/\\");
     if (p != path.npos)
     {
         const string dir = path.substr(0, p);
         pStack->addSearchPath(dir.c_str());


         p = dir.find_last_of("/\\");
         if (p != dir.npos)
         {
             pStack->addSearchPath(dir.substr(0, p).c_str());
         }
     }


     string env = "__LUA_STARTUP_FILE__=\"";
     env.append(path);
     env.append("\"");
     pEngine->executeString(env.c_str());


    CCLOG("------------------------------------------------");
    CCLOG("LOAD LUA FILE: %s", path.c_str());
    CCLOG("------------------------------------------------");
    pEngine->executeScriptFile(path.c_str());


    return true;
}

void AppDelegate::initResourcePath()
{
    CCFileUtils* sharedFileUtils = CCFileUtils::sharedFileUtils();    
    //设置SearchPaths
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    std::vector<std::string> oldSearchPaths = sharedFileUtils->getSearchPaths();
    std::vector<std::string> tempPaths(oldSearchPaths);
    std::vector<std::string> searchPaths;
    searchPaths.push_back(sharedFileUtils->getWritablePath() + "upd/");
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    searchPaths.push_back("res/");
#else
    std::string strBasePath = getAppBaseResourcePath();
    searchPaths.push_back(strBasePath);
    searchPaths.push_back(strBasePath + "res/");
#endif
    for (int i = 0; i < tempPaths.size(); ++i) {
        searchPaths.push_back(tempPaths);
    }
 
    sharedFileUtils->setSearchPaths(searchPaths);
#else
    sharedFileUtils->addSearchPath("res/");
#endif

}


说明:
1:删除加载framework_precompiled.zip 函数调用(启动更新模块时不需要)
2:添加的初始化游戏资源路径方法(为什么要写在这里 大多数情况下资源搜索路径应该是固定的),
有人会问如果资源搜索路径有改变怎么办? 如果有修改,只需要把上述逻辑在脚本里面实现即可。
3:对android特定做了一个BasePath,这个路径是用于设置基础资源搜索的(可以是 “assert/”、“/data/data/com.xxoo.xxoo/files/”、"/mnt/sdcard/xxoo/")
便于初始资源读取位置

增加3.3版本的事例代码。 sample.zip (104 KB)
PS:由于整个sample打包太大,就只上传改动的文件。

1赞

//  AppPlatform.h

#ifndef __xxoo__AppPlatform__
#define __xxoo__AppPlatform__

#include <string>

extern "C" {
    std::string getAppBaseResourcePath();
}

#endif /* defined(__xxoo__AppPlatform__) */


//  AppPlatform.cpp

#include "AppPlatform.h"
#include "cocos2d.h"


#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "jni/JniHelper.h"
#include <jni.h>
#define  GAME_CLASS_NAME "com/xxoo/xxoo/luajavabridge/Luajavabridge"
#endif

using namespace cocos2d;
extern "C" {
    std::string getAppBaseResourcePath()
    {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
        JniMethodInfo t;
        std::string strAppBaseResourcePath;
        if (JniHelper::getStaticMethodInfo(t, GAME_CLASS_NAME, "getAppBaseResourcePath",
                                           "()Ljava/lang/String;")) {
        jstring baseResourcePathJStr = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID);
        strAppBaseResourcePath = JniHelper::jstring2string(baseResourcePathJStr);
        t.env->DeleteLocalRef(t.classID);
        t.env->DeleteLocalRef(baseResourcePathJStr);
    }
    return strAppBaseResourcePath;
#else
    return "";
#endif
}
}


说明:平台调用方法

前期准备工作做完 下一步看main.lua实现


function __G__TRACKBACK__(errorMessage)
    print("----------------------------------------")
    print("LUA ERROR: " .. tostring(errorMessage) .. "\n")
    print(debug.traceback("", 2))
    print("----------------------------------------")
end

CCLuaLoadChunksFromZIP("lib/launcher.zip")
package.loaded"launcher.launcher"] = nil
require("launcher.launcher")

launcher 模块 有3个文件: launcher.lua, init.lua, config.lua

文件说明:config.lua 延用quick里面的属性设置
init.lua 为了使当前模块不引用framework中的方法,把launcher需要的方法都封装到init(借鉴framework)
launcher.lua 实现初始化平台SDK,更新资源,自更新,更新界面逻辑。
launcher.zip (7 KB)

初始化第三方SDK成功后,先去下载launcher模块;接着,下载下来的launcher模块和本地的launcher模块做内容md5比较。 如果二者md5值不同就保存新的launcher到upd/lib/目录下,再次加载main.lua;如果二者md5值相同则开始判断是否有新资源更新逻辑。

资源更新逻辑

flist 样板先贴上来


local flist = {
    appVersion = 1,
    version = "1.0.1",
    dirPaths = {
        {name = "common"},
        {name = "common/test"},
        {name = "lib"},
        {name = "sound"},
    },
    fileInfoList = {
        {name = "lib/framework_precompiled.zip", code = "b126279331bd68cc3c5c63e6fe0f2156", size = 101677},
    },
}

return flist

说明:

  1. appVersion:控制app打包的版本是否删除旧资源 (更新整包后upd/目录旧资源需要删除)
  2. version:资源文件跟新版本(控制资源是否更新)
  3. dirPaths: 当前资源目录下所有子目录(便于创建依次创建文件夹)
  4. fileInfoList :资源文件信息;相对路径、文件内容的md5值、文件size

更新资源逻辑:通过服务器上下载下来的flist文件内容和本地的flist文件内容比较差异化
另我是使用脚本生成flist 这个工具是从网上找的,做了一些修改下面贴上关键代码



local currentFolder = "/Users/xxx/Documents/quick-cocos2d-x/projects/xxoo/res"

local function hex(s)
 s=string.gsub(s,"(.)",function (x) return string.format("%02X",string.byte(x)) end)
 return s
end

local function readFile(path)
    local file = io.open(path, "rb")
    if file then
        local content = file:read("*all")
        io.close(file)
        return content
    end
    return nil
end

require "lfs"

local function findindir(path, wefind, dir_table, r_table, intofolder)
    for file in lfs.dir(path) do
        if file ~= "." and file ~= ".." and file ~= ".DS_Store" and file ~= "flist" and file ~= "launcher.zip" then 
            local f = path.."/"..file 
            
            local attr = lfs.attributes (f) 
            assert (type(attr) == "table") 

            if attr.mode == "directory" and intofolder then
                table.insert(dir_table, f)
                findindir(f, wefind, dir_table, r_table, intofolder) 
            else
                table.insert(r_table, {name = f, size = attr.size})
            end   
        end 
    end
end

    MakeFileList = {}

    function MakeFileList:run(path)
        
        local dir_table = {}
        local input_table = {}

        findindir(currentFolder, ".", dir_table, input_table, true)
        local pthlen = string.len(currentFolder)+2

        local buf = "local flist = {\n"
        buf = buf.."\tappVersion = 1,\n"
        buf = buf.."\tversion = \"1.0.2\",\n"
        buf = buf.."\tdirPaths = {\n"
        for i,v in ipairs(dir_table) do
            --print(i,v)
            local fn = string.sub(v,pthlen)
            buf = buf.."\t\t{name = \""..fn.."\"},\n"
        end
        buf = buf.."\t},\n"
        buf = buf.."\tfileInfoList = {\n"
        for i,v in ipairs(input_table) do
            --print(i,v)
            local fn = string.sub(v.name, pthlen)
            buf = buf.."\t\t{name = \""..fn.."\", code = \""
            local data=readFile(v.name)
            local ms = crypto.md5(hex(data or "")) or ""
            buf = buf..ms.."\", size = ".. v.size .."},\n"
        end
        buf = buf.."\t},\n"
        buf = buf.."}\n\n"
        buf = buf.."return flist"
        io.writefile(currentFolder.."/flist", buf)
    end

    return MakeFileList

此代码依赖quick ,使用player 跑一下 上面code就可以生成相关flist文件了。

PS:此模块的完成是借助 阳光、麒麟、总结后最后完善成这样的。

希望大家在使用中有更好的方案是可以我相关的建议,大家一起学习。

麒麟子,就等你写3了。

晚上回去我再补充后续实现逻辑。

哇哈哈哈……

为啥你删除根目录的时候我觉得很开心:877:

现在开始写了:2::2::2:

我也想来一个终极实现版:11::11:

:2: 最近热更新好火, 支持+10086

来一个吧,这个quick的热更新将会越来越完善。:2::2::2::2:

求最终实现版啊 最好能合进framework的

如果update模块最后完善了,完全可以作为quick 附带的一个插件, 需要就加载这个模块, 不需要就不加载。

太棒了,需要这样的文章!!!!

求完善,求合并

完善中! 同时也需要大家给出自己的需求和意见。

朋友,新手不懂啊,比如第二步的c++文件,我怎么处理啊?
有完整的例子吗?

第二步C++就只有一个函数,按照对应模式添加一个.h 和.cpp文件就可以了

对我添加好了,把这两个文件通过tolua导出之后放置在项目中的sources目录下,
然后按照你的步骤一直下去,跑测试的时候
发现可以载入launcher包,提示有3个文件
但是提示错误
module launcher.launcher没有找到,

朋友你看下我的路径,正确吗?
如果你有可以使用的测试包 发我邮箱xiaobadev@126.com就更好了。。
新手感激不尽

问题1 AppPlatform 不需要通过tolua导出,这个方法只在AppDelegate.cpp里面调用,其二 lib/launcher.png 应该放入 res目录下