問題所在:爲什麼要用getStringFromFile 獲取的字符串,getDataFromFile沒有導出給lua,quick以前的HelpFunc:getFileData在3.10上暫時用不了,所以修改這個來處理,getStringFromFile沒有處理 \0 問題,所以在長度上有問題,用 getDataFromFile 然後 push給lua時加一個長度就行了
修改文件: lua_cocos2dx_manual.cpp
修改前
static int tolua_cocos2dx_FileUtils_getStringFromFile(lua_State* tolua_S)
{
if (nullptr == tolua_S)
return 0;
int argc = 0;
FileUtils* self = nullptr;
bool ok = true;
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
if (!tolua_isusertype(tolua_S,1,"cc.FileUtils",0,&tolua_err)) goto tolua_lerror;
#endif
self = static_cast<FileUtils *>(tolua_tousertype(tolua_S,1,0));
#if COCOS2D_DEBUG >= 1
if (nullptr == self)
{
tolua_error(tolua_S,"invalid 'self' in function 'tolua_cocos2dx_FileUtils_getStringFromFile'\n", nullptr);
return 0;
}
#endif
argc = lua_gettop(tolua_S) - 1;
if (1 == argc)
{
const char* arg0;
std::string arg0_tmp; ok &= luaval_to_std_string(tolua_S, 2, &arg0_tmp, "cc.FileUtils:getStringFromFile"); arg0 = arg0_tmp.c_str();
if (ok)
{
std::string fullPathName = FileUtils::getInstance()->fullPathForFilename(arg0);
__String* contentsOfFile = __String::createWithContentsOfFile(fullPathName.c_str());
if (nullptr != contentsOfFile)
{
const char* tolua_ret = contentsOfFile->getCString();
tolua_pushstring(tolua_S, tolua_ret);
}
return 1;
}
}
luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d\n", "cc.FileUtils:getStringFromFile", argc, 1);
return 0;
#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'tolua_cocos2dx_FileUtils_getStringFromFile'.",&tolua_err);
return 0;
#endif
}
修改後
static int tolua_cocos2dx_FileUtils_getStringFromFile(lua_State* tolua_S)
{
if (nullptr == tolua_S)
return 0;
int argc = 0;
FileUtils* self = nullptr;
bool ok = true;
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
if (!tolua_isusertype(tolua_S,1,"cc.FileUtils",0,&tolua_err)) goto tolua_lerror;
#endif
self = static_cast<FileUtils *>(tolua_tousertype(tolua_S,1,0));
#if COCOS2D_DEBUG >= 1
if (nullptr == self)
{
tolua_error(tolua_S,"invalid 'self' in function 'tolua_cocos2dx_FileUtils_getStringFromFile'\n", nullptr);
return 0;
}
#endif
argc = lua_gettop(tolua_S) - 1;
if (1 == argc)
{
const char* arg0;
std::string arg0_tmp; ok &= luaval_to_std_string(tolua_S, 2, &arg0_tmp, "cc.FileUtils:getStringFromFile"); arg0 = arg0_tmp.c_str();
if (ok)
{
std::string fullPathName = FileUtils::getInstance()->fullPathForFilename(arg0);
Data data = FileUtils::getInstance()->getDataFromFile(fullPathName.c_str());
if (!data.isNull()) {
tolua_pushlstring(tolua_S, (const char*)data.getBytes(), data.getSize());
}
return 1;
}
}
luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d\n", "cc.FileUtils:getStringFromFile", argc, 1);
return 0;
#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'tolua_cocos2dx_FileUtils_getStringFromFile'.",&tolua_err);
return 0;
#endif
}
把
__String* contentsOfFile = __String::createWithContentsOfFile(fullPathName.c_str());
if (nullptr != contentsOfFile)
{
const char* tolua_ret = contentsOfFile->getCString();
tolua_pushstring(tolua_S, tolua_ret);
}
替換成
Data data = FileUtils::getInstance()->getDataFromFile(fullPathName.c_str());
if (!data.isNull()) {
tolua_pushlstring(tolua_S, (const char*)data.getBytes(), data.getSize());
}
文件
tolua_push.h添加
TOLUA_API void tolua_pushlstring (lua_State* L, const char* value, size_t length);
tolua_push.c 添加
TOLUA_API void tolua_pushlstring (lua_State* L, const char* value, size_t length)
{
if (value == NULL)
lua_pushnil(L);
else
lua_pushlstring(L,value,length);
}