Quick-Cocos2d-x的热更新机制实现
来源:互联网 发布:加内特身材知乎 编辑:程序博客网 时间:2024/05/17 01:15
模块支持:
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, 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文件了。
希望大家在使用中有更好的方案是可以我相关的建议,大家一起学习。
本文已经在论坛中发帖讨论,欢迎大家加入论坛,与开发者们一起研讨学习。
推荐阅读:
Quick-Cocos2d-x的热更新机制实现(终极版)
来源网址:http://www.cocoachina.com/bbs/read.php?tid=213257
- QUICK-COCOS2D-X的热更新机制实现
- quick-cocos2d-x的热更新机制实现
- quick-cocos2d-x的热更新机制实现<一>前言
- quick-cocos2d-x的热更新机制实现<六>后记
- quick-cocos2d-x的热更新机制实现<二>特色
- Quick-Cocos2d-x的热更新机制实现
- quick-cocos2d-x的热更新机制实现
- quick-cocos2d-x的热更新机制实现
- quick-cocos2d-x的热更新机制实现
- quick-cocos2d-x的热更新机制实现<五>对 framework 的修改
- Quick-Cocos2d-x的热更新机制实现(终极版2)
- quick-cocos2d-x的热更新机制实现<三>Updater(C++)
- quick-cocos2d-x的热更新机制实现<四>update包(lua)(上)
- quick-cocos2d-x的热更新机制实现<四>update包(lua)(中)
- quick-cocos2d-x的热更新机制实现<四>update包(lua)(下)
- 基于Quick-cocos2d-x 2.2.3 的动态更新实现
- 基于Quick-cocos2d-x 2.2.3 的动态更新实现
- 【Cocos2d-x】实现资源热更新
- 自动布局之autoresizingMask使用详解(Storyboard&Code)--cocoaChina
- 苹果ProRes422分析
- 获取li标签里的值
- 在 Web 项目中应用 Apache Shiro
- 《编程珠玑》 粗略估算
- Quick-Cocos2d-x的热更新机制实现
- IOS Push Notification 集成笔记
- Java自定义注解
- math-n进制表示的小数
- Java 小型学生管理系统心得
- 友盟分享配置
- Google C++单元测试框架(Gtest)系列教程之六——FAQ节选
- iOS发展历程
- Linux 驱动之中断下半部之工作队列