运行环境
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 @R_11_3013@ chunk
追根溯源
在bool AppDelegate::applicationDIDFinishLaunching()中查看lua加载代码:
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文件,继续查看源码发现真正干活的是:
view source print ?
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()的执行过程: view source print ?
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.
}
注意看代码的中文注释部分,这里就能解释为什么用luac编译的文件在window下面可以好好地运行(IOS上面也OK),而androID上就不能。luaL_loadfile()中通过c == LUA_SIGNATURE[0]判断lua文件是否加密,如果是,则重新以“rb”方式打开。 解决办法
修改scriptingluacocos2dx_supportCocos2dxLuaLoader.cpp中的loader_Android()函数
修改前:
view source print ?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.
}
修改后: view source print ?
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,如下: view source print ?
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.
}
或者我们可以偷鸡一下, view source print ? 1.
pEngine->executeString(
"require "
program/main
""
);
// 注意这里的"
最后我们的AppDelegate::applicationDIDFinishLaunching()大概就是这样的,
view source print ?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
注:这里笔者偷鸡了一下。
总结以上是内存溢出为你收集整理的cocos2dxandroid运行Luac编译后的lua代码全部内容,希望文章能够帮你解决cocos2dxandroid运行Luac编译后的lua代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)