Don't try different file ext, load as-is for lua load

This commit is contained in:
halx99 2021-10-11 23:23:30 +08:00
parent dde1956e02
commit 0b12c01019
2 changed files with 13 additions and 54 deletions

View File

@ -243,40 +243,9 @@ int LuaStack::executeScriptFile(const char* filename)
{
CCAssert(filename, "CCLuaStack::executeScriptFile() - invalid filename");
using namespace cxx17;
const auto BYTECODE_FILE_EXT = ".luac"_sv;
const auto NOT_BYTECODE_FILE_EXT = ".lua"_sv;
cxx17::string_view svFilePath(filename);
//
// remove .lua or .luac
//
if (cxx20::ends_with(svFilePath, BYTECODE_FILE_EXT))
{
svFilePath.remove_suffix(BYTECODE_FILE_EXT.length());
}
else if (cxx20::ends_with(svFilePath, NOT_BYTECODE_FILE_EXT))
{
svFilePath.remove_suffix(NOT_BYTECODE_FILE_EXT.length());
}
FileUtils *utils = FileUtils::getInstance();
//
// 1. check .luac suffix
// 2. check .lua suffix
//
std::string filePath{svFilePath};
filePath.append(BYTECODE_FILE_EXT.data(), BYTECODE_FILE_EXT.length());
Data data = utils->getDataFromFile(filePath);
if (data.isNull())
{
filePath.resize(filePath.length() - BYTECODE_FILE_EXT.length());
filePath.append(NOT_BYTECODE_FILE_EXT.data(), NOT_BYTECODE_FILE_EXT.length());
data = utils->getDataFromFile(filePath);
}
int rn = 0;
std::string filePath{filename};
Data data = FileUtils::getInstance()->getDataFromFile(filePath);
int rn = 0;
if (!data.isNull())
{
filePath.insert(filePath.begin(), '@'); // lua standard, add file chunck mark '@'

View File

@ -49,9 +49,13 @@ extern "C"
const auto BYTECODE_FILE_EXT = ".luac"_sv;
const auto NOT_BYTECODE_FILE_EXT = ".lua"_sv;
bool useBCExt = false;
auto path = adxelua_tosv(L, 1);
if (cxx20::ends_with(path, BYTECODE_FILE_EXT))
{
useBCExt = true;
path.remove_suffix(BYTECODE_FILE_EXT.length());
}
else if (cxx20::ends_with(path, NOT_BYTECODE_FILE_EXT))
path.remove_suffix(NOT_BYTECODE_FILE_EXT.length());
@ -63,6 +67,11 @@ extern "C"
pos = strPath.find_first_of('.');
}
if (!useBCExt)
strPath.append(NOT_BYTECODE_FILE_EXT.data(), NOT_BYTECODE_FILE_EXT.length());
else
strPath.append(BYTECODE_FILE_EXT.data(), BYTECODE_FILE_EXT.length());
// search file in package.path
Data chunk;
std::string filePath;
@ -95,31 +104,12 @@ extern "C"
filePath.replace(pos, 1, strPath);
pos = filePath.find_first_of('?', pos + strPath.length() + 1);
}
filePath.append(BYTECODE_FILE_EXT.data(), BYTECODE_FILE_EXT.length());
chunk = utils->getDataFromFile(filePath);
if (!chunk.isNull())
{
break;
}
else
{
filePath.resize(filePath.length() - BYTECODE_FILE_EXT.length());
filePath.append(NOT_BYTECODE_FILE_EXT.data(), NOT_BYTECODE_FILE_EXT.length());
chunk = utils->getDataFromFile(filePath);
if (!chunk.isNull())
{
break;
}
else
{
filePath.resize(filePath.length() - NOT_BYTECODE_FILE_EXT.length());
if (utils->isFileExist(filePath))
{
chunk = utils->getDataFromFile(filePath);
break;
}
}
}
begin = next + 1;
next = searchpath.find_first_of(';', begin);