您的位置:首页 > 移动开发 > Cocos引擎

cocos2dx android运行Luac编译后的lua代码

2014-05-14 09:23 483 查看
环境:

win7 64

cocos2d-2.1rc0-x-2.1.2

lua 5.1

  通常我们编写好的lua代码都是明文形式,谁都可以查看修改,为了防止自己的劳动成果不被别人轻易的盗取,可以使用luac(lua库中自带)对其进行加密,转换为二进制文件。这样lua代码就无法直接查看,但是这里会有一个问题:在windows下能够很好的运行,在android上就会黑屏,提示错误:

[LUA ERROR] binary string: unexpected end in precompiled chunk

追根溯源

  在bool AppDelegate::applicationDidFinishLaunching()中查看lua加载代码:

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
CCString* pstrFileContent = CCString::createWithContentsOfFile( "program/main.lua" );
if (pstrFileContent)
{
pEngine->executeString(pstrFileContent->getCString());
}
#else
std::string path = CCFileUtils::sharedFileUtils()->fullPathForFilename( "program/main.lua" );
pEngine->addSearchPath( path.substr( 0, path.find_last_of( "/" ) ).c_str() );
pEngine->executeScriptFile( path.c_str() );
#endif


  这里用的是executeString()方法来加载lua文件,查看源码发现:

LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s) {
printf("%s", s);
return luaL_loadbuffer(L, s, strlen(s), s);
}


d
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐