boolAppDelegate::applicationDidFinishLaunching()
{
// register lua engine
CCLuaEngine *pEngine = CCLuaEngine::defaultEngine(); // 创建lua引擎 quick cocos2dx c-lua(2)
CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine);
CCLuaStack *pStack = pEngine->getLuaStack();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
// load framework
pStack->loadChunksFromZIP("res/framework_precompiled.zip"); //framework_precompiled.zip(framework文件夹下得lua2进制文件压缩包)。 在这里执行chunk代码 并把对应2进制lua文件名保存到全局
// set script path
string path =CCFileUtils::sharedFileUtils()->fullPathForFilename("scripts/main.lua");
#else
// load framework
if (m_projectConfig.isLoadPrecompiledFramework())
{
const string precompiledFrameworkPath = SimulatorConfig::sharedDefaults()->getPrecompiledFrameworkPath();
pStack->loadChunksFromZIP(precompiledFrameworkPath.c_str());
}
// set script path
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) //static const size_type npos = -1;
{
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());
}
}
//eg: path = /Users/gnar/Library/Developer/CoreSimulator/Devices/78DFF2AE-C266-4EB3-A708-CE7D79CA814F/data/Containers/Bundle/Application/AE68B13C-7613-4B7C-950B-01650E6A8D85/test.app/scripts/main.lua
string env ="__LUA_STARTUP_FILE__=\"";
env.append(path); //附加
env.append("\"");
pEngine->executeString(env.c_str()); //直接调用chunk : __LUA_STARTUP_FILE__=\xxxxxx
CCLOG("------------------------------------------------");
CCLOG("LOAD LUA FILE: %s", path.c_str());
CCLOG("------------------------------------------------");
pEngine->executeScriptFile(path.c_str()); //调用 xxx路径指向的问件的 chunk
return true;
}