cocos2dxandroid运行Luac编译后的lua代码
运行环境 win7 64 cocos2d-2.1rc0-x-2.1.2 lua 5.1 通常我们编写好的lua代码都是明文形式,谁都可以查看修改,为了防止自己的劳动成果不被别人轻易的盗取,可以使用luac(lua库中自带)对其进行加密,转换为二进制文件。这样lua代码就无法直接查看,但是这里会有一个问题:在windows下能够很好的运行,在android上就会黑屏,提示错误:
01.
# if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
02.
CCString* pstrFileContent = CCString::createWithContentsOfFile( "program/main.lua" );
03.
if (pstrFileContent)
04.
{
05.
pEngine->executeString(pstrFileContent->getCString());
06.
}
07.
# else
08.
std::string path = CCFileUtils::sharedFileUtils()->fullPathForFilename( "program/main.lua" );
09.
pEngine->addSearchPath( path.substr( 0 ,path.find_last_of( "/" ) ).c_str() );
10.
pEngine->executeScriptFile( path.c_str() );
11.
#endif
这里executeString()加载lua文件,继续查看源码发现真正干活的是: 1.
LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff,size_t size,
2.
const char *name) {
3.
LoadS ls;
4.
ls.s = buff;
5.
ls.size = size;
6.
return lua_load(L,getS,&ls,name);
7.
}
在回头看看函数executeScriptFile()的执行过程: 01.
LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
02.
LoadF lf;
03.
int status,readstatus;
04.
int c;
05.
int fnameindex = lua_gettop(L) + 1 ; /* index of filename on the stack */
06.
lf.extraline = 0 ;
07.
if (filename == NULL) {
08.
lua_pushliteral(L, "=stdin" );
09.
lf.f = stdin;
10.
}
11.
else {
12.
lua_pushfstring(L, "@%s" ,filename);
13.
lf.f = fopen(filename, "r" );
14.
if (lf.f == NULL) return errfile(L, "open" ,fnameindex);
15.
}
16.
c = getc(lf.f);
17.
if (c == '#' ) { /* Unix exec. file? */
18.
lf.extraline = 1 ;
19.
while ((c = getc(lf.f)) != EOF && c != '
20.
') ; /* skip first line */
21.
if (c == '
22.
') c = getc(lf.f);
23.
}
24.
if (c == LUA_SIGNATURE[ 0 ] && filename) { /* binary file? */
25.
lf.f = freopen(filename, "rb" ,lf.f); /* reopen in binary mode */
26.
if (lf.f == NULL) return errfile(L, "reopen" ,fnameindex);
27.
/* skip eventual `#!...' */
28.
while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[ 0 ]) ;
29.
lf.extraline = 0 ;
30.
}
31.
ungetc(c,lf.f);
32.
status = lua_load(L,getF,&lf,lua_tostring(L,- 1 )); // 关键,与luaL_loadbuffer()中的一样
33.
readstatus = ferror(lf.f);
34.
if (filename) fclose(lf.f); /* close file (even in case of errors) */
35.
if (readstatus) {
36.
lua_settop(L,fnameindex); /* ignore results from `lua_load' */
37.
return errfile(L, "read" ,fnameindex);
38.
}
39.
lua_remove(L,fnameindex);
40.
return status;
41.
}
解决办法 修改scriptingluacocos2dx_supportCocos2dxLuaLoader.cpp中的loader_Android()函数 修改前: 01.
int loader_<a href= "http://www.it165.net/pro/ydad/" target= "_blank" class = "keylink" >Android</a>(lua_State *L)
02.
{
03.
std::string filename(luaL_checkstring(L, 1 ));
04.
filename.append( ".lua" );
05.
06.
CCString* pFileContent = CCString::createWithContentsOfFile(filename.c_str());
07.
08.
if (pFileContent)
09.
{
10.
if (luaL_loadstring(L,pFileContent->getCString()) != 0 )
11.
{
12.
luaL_error(L,"error loading module %s from file %s :
13.
%s",
14.
lua_tostring(L, 1 ),filename.c_str(),- 1 ));
15.
}
16.
}
17.
else
18.
{
19.
CCLog( "can not get file data of %s" ,filename.c_str());
20.
}
21.
22.
return 1 ;
23.
}
修改后: 01.
int loader_Android(lua_State* L)
02.
{
03.
unsigned char * pData = nullptr;
04.
unsigned long size = 0 ;
05.
06.
/* modify for read lua script from bytecode */
07.
std::string filename(luaL_checkstring(L, 1 ));
08.
std::string rel_name = filename.append( ".lua" );
09.
10.
pData = CCFileUtils::sharedFileUtils()->getFileData(rel_name.c_str(),&size);
11.
if (pData) {
12.
if (luaL_loadbuffer(L,( char *)pData,size,rel_name.c_str()) != 0 ) {
13.
luaL_error(L, "error loading module s from file s :s" ,rel_name.c_str(),- 1 ));
15.
}
16.
}
17.
else {
18.
CCLog( "can not get file data of %s" ,filename.c_str());
19.
}
20.
21.
CC_SAFE_DELETE_ARRAY(pData);
22.
23.
return 1 ;
24.
}
另外,由于在cpp中第一次加载lua并没有调用loader_Android(),这是因为uaL_loadbuffer只会把文件加载成为一个chunk,而不会运行该chunk,所以还要在加一条调用语名lua_pcall,如下: 01.
int loader_Android(lua_State* L)
02.
{
03.
unsigned char * pData = nullptr;
04.
unsigned long size = 0 ;
05.
06.
/* modify for read lua script from bytecode */
07.
std::string filename(luaL_checkstring(L,rel_name.c_str()) != 0 || lua_pcall(L, 0 ,LUA_MULTRET, 0 ) ) { // 修改处
13.
luaL_error(L,filename.c_str());
19.
}
20.
21.
CC_SAFE_DELETE_ARRAY(pData);
22.
23.
return 1 ;
24.
}
1.
pEngine->executeString( "require " program/main "" ); // 注意这里的"
最后我们的AppDelegate::applicationDidFinishLaunching()大概就是这样的, 01.
const char * luaFile = "program/main.lua" ;
02.
# if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
03.
std::string path = CCFileUtils::sharedFileUtils()->fullPathForFilename(luaFile);
04.
CCLog( "path = %s" ,path.c_str());
05.
std::string subPath = path.substr( 0 ,path.find_last_of( "/" ));
06.
CCLog( "sub path = %s" ,subPath.c_str());
07.
08.
pEngine->addSearchPath(subPath.c_str());
09.
10.
std::vector<std::string> searchPaths = CCFileUtils::sharedFileUtils()->getSearchPaths();
11.
searchPaths.insert(searchPaths.begin(),subPath);
12.
CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths);
13.
14.
pEngine->executeString( "require " program/main "" ); // 注意这里的"
15.
# else
16.
std::string path = CCFileUtils::sharedFileUtils()->fullPathForFilename(luaFile);
17.
pEngine->addSearchPath( path.substr( 0 ,path.find_last_of( "/" ) ).c_str() );
18.
pEngine->executeScriptFile( path.c_str() );
19.
#endif
注:这里笔者偷鸡了一下。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |