cocos2dx android運行Luac編譯後的lua代碼

來源:互聯網
上載者:User

標籤:android   style   blog   class   code   c   

運行環境

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 = nullptr;unsigned long size = 0;/* modify for read lua script from bytecode */std::string filename(luaL_checkstring(L, 1));std::string rel_name = filename.append(".lua");pData = CCFileUtils::sharedFileUtils()->getFileData(rel_name.c_str(), "rb", &size);if (pData) {if (luaL_loadbuffer(L, (char*)pData,size,rel_name.c_str()) != 0 ) {luaL_error(L, "error loading module s from file s :s",lua_tostring(L, 1), rel_name.c_str(), lua_tostring(L, -1));}}else {CCLog("can not get file data of %s", filename.c_str());}CC_SAFE_DELETE_ARRAY(pData);return 1;}

另外,由於在cpp中第一次載入lua並沒有調用loader_Android(),這是因為uaL_loadbuffer只會把檔案載入成為一個chunk,而不會運行該chunk,所以還要在加一條調用語名lua_pcall,如下:

int loader_Android(lua_State* L){ unsigned char* pData = nullptr;unsigned long size = 0;/* modify for read lua script from bytecode */std::string filename(luaL_checkstring(L, 1));std::string rel_name = filename.append(".lua");pData = CCFileUtils::sharedFileUtils()->getFileData(rel_name.c_str(), "rb", &size);if (pData) {if (luaL_loadbuffer(L, (char*)pData,size,rel_name.c_str()) != 0 || lua_pcall(L, 0, LUA_MULTRET, 0) ) { // 修改處luaL_error(L, "error loading module s from file s :s",lua_tostring(L, 1), rel_name.c_str(), lua_tostring(L, -1));}}else {CCLog("can not get file data of %s", filename.c_str());}CC_SAFE_DELETE_ARRAY(pData);return 1;}
或者我們可以偷雞一下,

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




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.