Compiled lua, can not run on android

Compiled lua, can not run on android!

Who are experiencing the same problem??

luac?
There are some issue sometime, so you should use your own way to run the encrypted code.

Shawn Clovie wrote:

luac?
There are some issue sometime, so you should use your own way to run the encrypted code.

thx,I Already use own way!

yeah, on window, when load lua file, it will use lua default io, so compile lua can running. but android is different, when load lua file, cocos2d-x first load the file content in memory, and then call luaL_loadstring function (the code in scripting/lua/cocos2dx_support/Cocos2dxluaLoader.cpp) , luaL_loadstring can only compile string code. so this is the reason why compile lua code can’t run android.

but it easy to change, this is my implement:
@

int loader_Android(lua_State L)
{ unsigned char
pData=NULL;
unsigned long size = 0;
/* modify for read lua script from bytecode */
std::string filename(luaL_checkstring(L, 1));
std::string rel_name;
do{
rel_name=filename*“.lua”;
pData = CCFileUtils::sharedFileUtils->getFileData, “rb”, &size);
}while;
if
{
rel_name=filename*“.luc”;
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;
}
@

hope it can help you

NosicLin F wrote:

yeah, on window, when load lua file, it will use lua default io, so compile lua can running. but android is different, when load lua file, cocos2d-x first load the file content in memory, and then call luaL_loadstring function (the code in scripting/lua/cocos2dx_support/Cocos2dxluaLoader.cpp) , luaL_loadstring can only compile string code. so this is the reason why compile lua code can’t run android.
>
but it easy to change, this is my implement:
@
>
int loader_Android(lua_State L)
{ unsigned char
pData=NULL;
unsigned long size = 0;
/* modify for read lua script from bytecode /
std::string filename);
std::string rel_name;
do{
rel_name=filename
“.lua”;
pData = CCFileUtils::sharedFileUtils->getFileData, “rb”, &size);
}while;
if
{
rel_name=filename
“.luc”;
pData = CCFileUtils::sharedFileUtils~~>getFileData, “rb”, &size);
}
if
{
if pData,size,rel_name.c_str) != 0)
{
luaL_error, rel_name.c_str, lua_tostring);
}
}
else
{
CCLog);
}
CC_SAFE_DELETE_ARRAY;
return 1;
}
@
>
>
hope it can help you

Thank you very much
But, How to use it when I loaded the first time ?
My Code:
<pre>
lua_State * L = pEngine~~>getLuaState;
unsigned char** pData=NULL;
unsigned long size = 0;
/* modify for read lua script from bytecode */
string filename(“scripts/main”);
string rel_name;
do{
CCLog(“load data!”);
rel_name=filename+“.lua”;
pData = CCFileUtils::sharedFileUtils()->getFileData(rel_name.c_str(), “rb”, &size);
}while(0);

CCLog(“rel_name = s,size = d”,rel_name.c_str(),size);
CCLog(“pData = s",pData);
if (pData)
{
if (luaL_loadbuffer(L, (char*)pData,size,rel_name.c_str()) != 0)
{
CCLog( “error loading module s from file s :\n\ts”, lua_tostring(L, 1), rel_name.c_str(), lua_tostring(L, –1));
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());
}

do you mean how to load the script/main.lua on android ?

NosicLin F wrote:

do you mean how to load the script/main.lua on android ?

yes!

there are some lua project in cocos2d-x, it can running on android. the example in “samples\Lua\HelloLua\proj.android”.

NosicLin F wrote:

there are some lua project in cocos2d-x, it can running on android. the example in “samplesLuaHelloLuaproj.android”.

Sorry, can you speak chinese? My English is not good. so,I did not express clearly what my mean.

我的意思是在C这边首先载入的lua文件,没办法使用编译后的。这边并没有调用到 Cocos2dxLuaLoader 的 loader_Android,而在lua文件里面require的文件则可以是编译后!
我不知道要怎么解决这个问题,我尝试过在 C
调用lua文件的时候使用 luaL_loadbuffer 但是行不通!

qing rao wrote:

NosicLin F wrote:
> there are some lua project in cocos2d-x, it can running on android. the example in “samplesLuaHelloLuaproj.android”.
>
Sorry, can you speak chinese? My English is not good. so,I did not express clearly what my mean.
>
我的意思是在C这边首先载入的lua文件,没办法使用编译后的。这边并没有调用到 Cocos2dxLuaLoader 的 loader_Android,而在lua文件里面require的文件则可以是编译后!
我不知道要怎么解决这个问题,我尝试过在 C
调用lua文件的时候使用 luaL_loadbuffer 但是行不通!

哦,因为luaL_loadbuffer只会把文件加载成为一个chunk,而不会运行该chunk,所以还要在加一条调用语名lua_pcall

修改你原来的代码为:

@

if (pData)
{
if (luaL_loadbuffer(L, (char**)pData,size,rel_name.c_str)||lua_pcall) /** 修改后的代码 */
{
CCLog( “error loading module s from file s :s", lua_tostring(L, 1), rel_name.c_str(), lua_tostring(L, -1));
luaL_error(L, "error loading module s from file s :\n\ts”,lua_tostring(L, 1), rel_name.c_str(), lua_tostring(L, ~~1));
}
}
else
{
CCLog);
}
@

或者是用这种方法也行:
pEngine~~>executeString(“require cript/main”);

NosicLin F wrote:

there are some lua project in cocos2d-x, it can running on android. the example in “samplesLuaHelloLuaproj.android”.

pEngine->executeString(“require cript/main”);
是可行的!太感谢拉,这个方法我怎么没想到呢!

另外种方法我也试试!