Fix: executeScriptFile can not load .luac file when .lua is not exist

This commit is contained in:
yinjimmy 2015-01-09 04:18:41 +08:00
parent 01798147d9
commit bdac739541
1 changed files with 31 additions and 17 deletions

View File

@ -275,33 +275,47 @@ int LuaStack::executeString(const char *codes)
int LuaStack::executeScriptFile(const char* filename)
{
CCAssert(filename, "CCLuaStack::executeScriptFile() - invalid filename");
static const std::string BYTECODE_FILE_EXT = ".luac";
static const std::string NOT_BYTECODE_FILE_EXT = ".lua";
std::string buf(filename);
//
// remove .lua or .luac
//
size_t pos = buf.rfind(BYTECODE_FILE_EXT);
if (pos != std::string::npos)
{
buf = buf.substr(0, pos);
}
else
{
pos = buf.rfind(NOT_BYTECODE_FILE_EXT);
if (pos == buf.length() - NOT_BYTECODE_FILE_EXT.length())
{
buf = buf.substr(0, pos);
}
}
FileUtils *utils = FileUtils::getInstance();
//
//
// 1. check .lua suffix
// 2. check .luac suffix
//
std::string buf(filename);
if (!utils->isFileExist(buf))
std::string tmpfilename = buf + NOT_BYTECODE_FILE_EXT;
if (utils->isFileExist(tmpfilename))
{
std::string notBytecodeFilename = buf + NOT_BYTECODE_FILE_EXT;
if (utils->isFileExist(notBytecodeFilename))
buf = tmpfilename;
}
else
{
tmpfilename = buf + BYTECODE_FILE_EXT;
if (utils->isFileExist(tmpfilename))
{
buf = notBytecodeFilename;
}
else
{
std::string bytecodeFilename = buf + BYTECODE_FILE_EXT;
if (utils->isFileExist(bytecodeFilename))
{
buf = bytecodeFilename;
}
buf = tmpfilename;
}
}
std::string fullPath = utils->fullPathForFilename(buf);
Data data = utils->getDataFromFile(fullPath);
int rn = 0;