【quick-cocos2d-x】热更新机制

http://cn.cocos2d-x.org/tutorial/show?id=1094

模块支持:

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/") 便于初始资源读取位置。



// 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

初始化第三方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("X",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,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,".",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"
buf = buf.."}\n\n"
buf = buf.."return flist"
io.writefile(currentFolder.."/flist",buf)
end

return MakeFileList

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

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

本文已经在论坛中发帖讨论,欢迎大家加入论坛,与开发者们一起研讨学习。

推荐阅读:

Quick-Cocos2d-x的热更新机制实现(终极版)

来源网址:http://www.cocoachina.com/bbs/read.php?tid=213257

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...