模块支持:
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打包太大,就只上传改动的文件。