From 0b12c01019991d5183d6c570ecaa095983493f8f Mon Sep 17 00:00:00 2001 From: halx99 Date: Mon, 11 Oct 2021 23:23:30 +0800 Subject: [PATCH] Don't try different file ext, load as-is for lua load --- .../lua-bindings/manual/CCLuaStack.cpp | 37 ++----------------- .../lua-bindings/manual/Cocos2dxLuaLoader.cpp | 30 +++++---------- 2 files changed, 13 insertions(+), 54 deletions(-) diff --git a/extensions/scripting/lua-bindings/manual/CCLuaStack.cpp b/extensions/scripting/lua-bindings/manual/CCLuaStack.cpp index c365e0b8b0..947f6e9bdc 100644 --- a/extensions/scripting/lua-bindings/manual/CCLuaStack.cpp +++ b/extensions/scripting/lua-bindings/manual/CCLuaStack.cpp @@ -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 '@' diff --git a/extensions/scripting/lua-bindings/manual/Cocos2dxLuaLoader.cpp b/extensions/scripting/lua-bindings/manual/Cocos2dxLuaLoader.cpp index aff6615cfc..1119efe20b 100644 --- a/extensions/scripting/lua-bindings/manual/Cocos2dxLuaLoader.cpp +++ b/extensions/scripting/lua-bindings/manual/Cocos2dxLuaLoader.cpp @@ -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);