Cocos2d-x   发布时间:2022-05-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了cocos2dxandroid运行Luac编译后的lua代码大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

运行环境

win7 64

cocos2d-2.1rc0-x-2.1.2

lua 5.1

通常我们编写好的lua代码都是明文形式,谁都可以查看修改,为了防止自己的劳动成果不被别人轻易的盗取,可以使用luac(lua库中自带)对其进行加密,转换为二进制文件。这样lua代码就无法直接查看,但是这里会有一个问题:在windows下能够很好的运行,@R_489_5313@d上就会黑屏,提示错误:
[LUA ERROR] binary String: unexpected end in precompiled 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文件,继续查看源码发现真正干活的是:
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. }
注意看代码的中文注释部分,这里就能解释为什么用luac编译的文件在window下面可以好好地运行(IOS上面也OK),而Android上就不能。luaL_loadfile()中通过c == LUA_SIGNATURE[0]判断lua文件是否加密,如果是,则重新以“rb”方式打开。

解决办法

修改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. @H_616_954@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. @H_616_954@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. @H_616_954@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

注:这里笔者偷鸡了一下。

大佬总结

以上是大佬教程为你收集整理的cocos2dxandroid运行Luac编译后的lua代码全部内容,希望文章能够帮你解决cocos2dxandroid运行Luac编译后的lua代码所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。