首页 > 代码库 > cocos2dx android运行Luac编译后的lua代码

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

运行环境

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_loadbuffer (lua_State *L, const char *buff, size_t size,
                                const char *name) {
  LoadS ls;
  ls.s = buff;
  ls.size = size;
  return lua_load(L, getS, &ls, name);
}

在回头看看函数executeScriptFile()的执行过程:

LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
  LoadF lf;
  int status, readstatus;
  int c;
  int fnameindex = lua_gettop(L) + 1;  /* index of filename on the stack */
  lf.extraline = 0;
  if (filename == NULL) {
    lua_pushliteral(L, "=stdin");
    lf.f = stdin;
  }
  else {
    lua_pushfstring(L, "@%s", filename);
    lf.f = fopen(filename, "r");
    if (lf.f == NULL) return errfile(L, "open", fnameindex);
  }
  c = getc(lf.f);
  if (c == ‘#‘) {  /* Unix exec. file? */
    lf.extraline = 1;
    while ((c = getc(lf.f)) != EOF && c != ‘\n‘) ;  /* skip first line */
    if (c == ‘\n‘) c = getc(lf.f);
  }
  if (c == LUA_SIGNATURE[0] && filename) {  /* binary file? */
    lf.f = freopen(filename, "rb", lf.f);  /* reopen in binary mode */
    if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
    /* skip eventual `#!...‘ */
   while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ;
    lf.extraline = 0;
  }
  ungetc(c, lf.f);
  status = lua_load(L, getF, &lf, lua_tostring(L, -1)); // 关键,与luaL_loadbuffer()中的一样
  readstatus = ferror(lf.f);
  if (filename) fclose(lf.f);  /* close file (even in case of errors) */
  if (readstatus) {
    lua_settop(L, fnameindex);  /* ignore results from `lua_load‘ */
    return errfile(L, "read", fnameindex);
  }
  lua_remove(L, fnameindex);
  return status;
}
注意看代码的中文注释部分,这里就能解释为什么用luac编译的文件在window下面可以好好地运行(IOS上面也OK),而android上就不能。luaL_loadfile()中通过c == LUA_SIGNATURE[0]判断lua文件是否加密,如果是,则重新以“rb”方式打开。


解决办法

修改scripting\lua\cocos2dx_support\Cocos2dxLuaLoader.cpp中的loader_Android()函数

修改前:

int loader_Android(lua_State *L)
    {
        std::string filename(luaL_checkstring(L, 1));
        filename.append(".lua");

        CCString* pFileContent = CCString::createWithContentsOfFile(filename.c_str());

        if (pFileContent)
        {
            if (luaL_loadstring(L, pFileContent->getCString()) != 0)
            {
                luaL_error(L, "error loading module %s from file %s :\n\t%s",
                    lua_tostring(L, 1), filename.c_str(), lua_tostring(L, -1));
            }
        }
        else
        {
            CCLog("can not get file data of %s", filename.c_str());
        }

        return 1;
    }

修改后:

int loader_Android(lua_State* L)
	{ 
		unsigned char* pData = http://www.mamicode.com/nullptr;>
另外,由于在cpp中第一次加载lua并没有调用loader_Android(),这是因为uaL_loadbuffer只会把文件加载成为一个chunk,而不会运行该chunk,所以还要在加一条调用语名lua_pcall,如下:

int loader_Android(lua_State* L)
	{ 
		unsigned char* pData = http://www.mamicode.com/nullptr;>或者我们可以偷鸡一下,

pEngine->executeString("require \"program/main\""); // 注意这里的\"

最后我们的AppDelegate::applicationDidFinishLaunching()大概就是这样的,

    const char* luaFile = "program/main.lua";
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	std::string path = CCFileUtils::sharedFileUtils()->fullPathForFilename(luaFile);
	CCLog("path = %s", path.c_str());
	std::string subPath = path.substr(0, path.find_last_of("/"));
	CCLog("sub path = %s", subPath.c_str());

	pEngine->addSearchPath(subPath.c_str());

	std::vector<std::string> searchPaths = CCFileUtils::sharedFileUtils()->getSearchPaths();
	searchPaths.insert(searchPaths.begin(), subPath);
	CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths);

	pEngine->executeString("require \"program/main\""); // 注意这里的\"
#else        
    std::string path = CCFileUtils::sharedFileUtils()->fullPathForFilename(luaFile);
    pEngine->addSearchPath( path.substr( 0, path.find_last_of( "/" ) ).c_str() );
    pEngine->executeScriptFile( path.c_str() );
#endif

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


引用参考

http://www.cocos2d-x.org/forums/11/topics/28628

http://www.cnblogs.com/mrblue/archive/2013/06/06/3122543.html