Refine lua loader

This commit is contained in:
halx99 2021-10-11 22:34:41 +08:00
parent 3474de5263
commit ca7cc4f697
2 changed files with 59 additions and 56 deletions

View File

@ -245,21 +245,21 @@ int LuaStack::executeScriptFile(const char* filename)
{ {
CCAssert(filename, "CCLuaStack::executeScriptFile() - invalid filename"); CCAssert(filename, "CCLuaStack::executeScriptFile() - invalid filename");
std::string buf(filename); std::string chunkPath(filename);
// //
// remove .lua or .luac // remove .lua or .luac
// //
size_t pos = buf.rfind(BYTECODE_FILE_EXT); size_t pos = chunkPath.rfind(BYTECODE_FILE_EXT);
if (pos != std::string::npos) if (pos != std::string::npos)
{ {
buf = buf.substr(0, pos); chunkPath = chunkPath.substr(0, pos);
} }
else else
{ {
pos = buf.rfind(NOT_BYTECODE_FILE_EXT); pos = chunkPath.rfind(NOT_BYTECODE_FILE_EXT);
if (pos == buf.length() - NOT_BYTECODE_FILE_EXT.length()) if (pos == chunkPath.length() - NOT_BYTECODE_FILE_EXT.length())
{ {
buf = buf.substr(0, pos); chunkPath = chunkPath.substr(0, pos);
} }
} }
@ -269,26 +269,27 @@ int LuaStack::executeScriptFile(const char* filename)
// 1. check .luac suffix // 1. check .luac suffix
// 2. check .lua suffix // 2. check .lua suffix
// //
std::string tmpfilename = buf + BYTECODE_FILE_EXT; std::string tmpfilename = chunkPath + BYTECODE_FILE_EXT;
if (utils->isFileExist(tmpfilename)) if (utils->isFileExist(tmpfilename))
{ {
buf = tmpfilename; chunkPath = tmpfilename;
} }
else else
{ {
tmpfilename = buf + NOT_BYTECODE_FILE_EXT; tmpfilename = chunkPath + NOT_BYTECODE_FILE_EXT;
if (utils->isFileExist(tmpfilename)) if (utils->isFileExist(tmpfilename))
{ {
buf = tmpfilename; chunkPath = tmpfilename;
} }
} }
std::string fullPath = utils->fullPathForFilename(buf); std::string fullPath = utils->fullPathForFilename(chunkPath);
Data data = utils->getDataFromFile(fullPath); Data data = utils->getDataFromFile(fullPath);
int rn = 0; int rn = 0;
if (!data.isNull()) if (!data.isNull())
{ {
if (luaLoadBuffer(_state, (const char*)data.getBytes(), (int)data.getSize(), fullPath.c_str()) == 0) chunkPath.insert(chunkPath.begin(), '@'); // lua standard, add file chunck mark '@'
if (luaLoadBuffer(_state, (const char*)data.getBytes(), (int)data.getSize(), chunkPath.c_str()) == 0)
{ {
rn = executeFunction(0); rn = executeFunction(0);
} }

View File

@ -30,42 +30,46 @@ THE SOFTWARE.
#include "scripting/lua-bindings/manual/CCLuaStack.h" #include "scripting/lua-bindings/manual/CCLuaStack.h"
#include "scripting/lua-bindings/manual/CCLuaEngine.h" #include "scripting/lua-bindings/manual/CCLuaEngine.h"
#include "platform/CCFileUtils.h" #include "platform/CCFileUtils.h"
#include "yasio/cxx17/string_view.hpp"
using namespace cocos2d; using namespace cocos2d;
static cxx17::string_view adxelua_tosv(lua_State* L, int arg) {
size_t l = 0;
const char* s = lua_tolstring(L, arg, &l);
return cxx17::string_view{s, l};
}
extern "C" extern "C"
{ {
int cocos2dx_lua_loader(lua_State *L) int cocos2dx_lua_loader(lua_State *L)
{ {
static const std::string BYTECODE_FILE_EXT = ".luac"; using namespace cxx17;
static const std::string NOT_BYTECODE_FILE_EXT = ".lua"; const auto BYTECODE_FILE_EXT = ".luac"_sv;
const auto NOT_BYTECODE_FILE_EXT = ".lua"_sv;
std::string filename(luaL_checkstring(L, 1)); auto path = adxelua_tosv(L, 1);
size_t pos = filename.rfind(BYTECODE_FILE_EXT); if (cxx20::ends_with(path, BYTECODE_FILE_EXT))
if (pos != std::string::npos && pos == filename.length() - BYTECODE_FILE_EXT.length()) path.remove_suffix(BYTECODE_FILE_EXT.length());
filename = filename.substr(0, pos); else if (cxx20::ends_with(path, NOT_BYTECODE_FILE_EXT))
else path.remove_suffix(NOT_BYTECODE_FILE_EXT.length());
{
pos = filename.rfind(NOT_BYTECODE_FILE_EXT);
if (pos != std::string::npos && pos == filename.length() - NOT_BYTECODE_FILE_EXT.length())
filename = filename.substr(0, pos);
}
pos = filename.find_first_of('.'); std::string strPath{path};
size_t pos = strPath.find_first_of('.');
while (pos != std::string::npos) while (pos != std::string::npos)
{ {
filename.replace(pos, 1, "/"); strPath.replace(pos, 1, "/");
pos = filename.find_first_of('.'); pos = strPath.find_first_of('.');
} }
// search file in package.path // search file in package.path
Data chunk; Data chunk;
std::string chunkName; std::string filePath;
FileUtils* utils = FileUtils::getInstance(); FileUtils* utils = FileUtils::getInstance();
lua_getglobal(L, "package"); lua_getglobal(L, "package");
lua_getfield(L, -1, "path"); lua_getfield(L, -1, "path");
std::string searchpath(lua_tostring(L, -1)); auto searchpath = adxelua_tosv(L, -1);
lua_pop(L, 1); lua_pop(L, 1);
size_t begin = 0; size_t begin = 0;
size_t next = searchpath.find_first_of(';', 0); size_t next = searchpath.find_first_of(';', 0);
@ -74,47 +78,43 @@ extern "C"
{ {
if (next == std::string::npos) if (next == std::string::npos)
next = searchpath.length(); next = searchpath.length();
std::string prefix = searchpath.substr(begin, next-begin); auto prefix = searchpath.substr(begin, next - begin);
if (prefix[0] == '.' && prefix[1] == '/') if (prefix[0] == '.' && prefix[1] == '/')
prefix = prefix.substr(2); prefix = prefix.substr(2);
pos = prefix.rfind(BYTECODE_FILE_EXT); if (cxx20::ends_with(prefix, BYTECODE_FILE_EXT))
if (pos != std::string::npos && pos == prefix.length() - BYTECODE_FILE_EXT.length()) prefix.remove_suffix(BYTECODE_FILE_EXT.length());
{ else if (cxx20::ends_with(prefix, NOT_BYTECODE_FILE_EXT))
prefix = prefix.substr(0, pos); prefix.remove_suffix(NOT_BYTECODE_FILE_EXT.length());
}
else filePath.assign(prefix.data(), prefix.length());
{ pos = filePath.find_first_of('?', 0);
pos = prefix.rfind(NOT_BYTECODE_FILE_EXT);
if (pos != std::string::npos && pos == prefix.length() - NOT_BYTECODE_FILE_EXT.length())
prefix = prefix.substr(0, pos);
}
pos = prefix.find_first_of('?', 0);
while (pos != std::string::npos) while (pos != std::string::npos)
{ {
prefix.replace(pos, 1, filename); filePath.replace(pos, 1, strPath);
pos = prefix.find_first_of('?', pos + filename.length() + 1); pos = filePath.find_first_of('?', pos + strPath.length() + 1);
} }
chunkName = prefix + BYTECODE_FILE_EXT; filePath.append(BYTECODE_FILE_EXT.data(), BYTECODE_FILE_EXT.length());
if (utils->isFileExist(chunkName)) // && !utils->isDirectoryExist(chunkName)) if (utils->isFileExist(filePath))
{ {
chunk = utils->getDataFromFile(chunkName); chunk = utils->getDataFromFile(filePath);
break; break;
} }
else else
{ {
chunkName = prefix + NOT_BYTECODE_FILE_EXT; filePath.resize(filePath.length() - BYTECODE_FILE_EXT.length());
if (utils->isFileExist(chunkName) ) //&& !utils->isDirectoryExist(chunkName)) filePath.append(NOT_BYTECODE_FILE_EXT.data(), NOT_BYTECODE_FILE_EXT.length());
if (utils->isFileExist(filePath))
{ {
chunk = utils->getDataFromFile(chunkName); chunk = utils->getDataFromFile(filePath);
break; break;
} }
else else
{ {
chunkName = prefix; filePath.resize(filePath.length() - NOT_BYTECODE_FILE_EXT.length());
if (utils->isFileExist(chunkName)) // && !utils->isDirectoryExist(chunkName)) if (utils->isFileExist(filePath))
{ {
chunk = utils->getDataFromFile(chunkName); chunk = utils->getDataFromFile(filePath);
break; break;
} }
} }
@ -123,15 +123,17 @@ extern "C"
begin = next + 1; begin = next + 1;
next = searchpath.find_first_of(';', begin); next = searchpath.find_first_of(';', begin);
} while (begin < searchpath.length()); } while (begin < searchpath.length());
if (chunk.getSize() > 0) if (chunk.getSize() > 0)
{ {
LuaStack* stack = LuaEngine::getInstance()->getLuaStack(); LuaStack* stack = LuaEngine::getInstance()->getLuaStack();
stack->luaLoadBuffer(L, reinterpret_cast<const char*>(chunk.getBytes()), filePath.insert(filePath.begin(), '@'); // lua standard, add file chunck mark '@'
static_cast<int>(chunk.getSize()), chunkName.c_str()); stack->luaLoadBuffer(L, reinterpret_cast<const char*>(chunk.getBytes()), static_cast<int>(chunk.getSize()),
filePath.c_str());
} }
else else
{ {
CCLOG("can not get file data of %s", chunkName.c_str()); CCLOG("can not get file data of %s", filePath.c_str());
return 0; return 0;
} }