From 5aec5d4a839faf0a8e7a0797875b45ab9ea92857 Mon Sep 17 00:00:00 2001 From: samuele3hu Date: Tue, 10 Jun 2014 16:47:32 +0800 Subject: [PATCH] Update `LuaStack::executeScriptFile` to support template project loading lua or luac files --- .../lua-bindings/manual/CCLuaStack.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/cocos/scripting/lua-bindings/manual/CCLuaStack.cpp b/cocos/scripting/lua-bindings/manual/CCLuaStack.cpp index a5ee2a4b51..9b80dd656d 100644 --- a/cocos/scripting/lua-bindings/manual/CCLuaStack.cpp +++ b/cocos/scripting/lua-bindings/manual/CCLuaStack.cpp @@ -262,6 +262,32 @@ int LuaStack::executeScriptFile(const char* filename) return executeString(code.c_str()); #else std::string fullPath = FileUtils::getInstance()->fullPathForFilename(filename); + if (!FileUtils::getInstance()->isFileExist(fullPath)) + { + const std::string BYTECODE_FILE_EXT = ".luac"; + const std::string NOT_BYTECODE_FILE_EXT = ".lua"; + + std::string restructuringFileName = filename; + size_t pos = restructuringFileName.rfind(BYTECODE_FILE_EXT); + if (pos != std::string::npos) + { + restructuringFileName = restructuringFileName.substr(0, pos) + NOT_BYTECODE_FILE_EXT; + } + else + { + pos = restructuringFileName.rfind(NOT_BYTECODE_FILE_EXT); + if (pos != std::string::npos) + { + restructuringFileName = restructuringFileName.substr(0, pos) + BYTECODE_FILE_EXT; + } + } + + fullPath = FileUtils::getInstance()->fullPathForFilename(restructuringFileName); + if (!FileUtils::getInstance()->isFileExist(fullPath)) + { + return 1; + } + } ++_callFromLua; int nRet = luaL_dofile(_state, fullPath.c_str()); --_callFromLua;