2014-03-10 14:04:58 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2011-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
#include "Cocos2dxLuaLoader.h"
|
|
|
|
#include <string>
|
|
|
|
#include <algorithm>
|
|
|
|
|
2014-06-03 09:55:57 +08:00
|
|
|
#include "CCLuaStack.h"
|
|
|
|
#include "CCLuaEngine.h"
|
|
|
|
|
2014-03-10 14:04:58 +08:00
|
|
|
using namespace cocos2d;
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
int cocos2dx_lua_loader(lua_State *L)
|
|
|
|
{
|
2014-06-04 09:59:29 +08:00
|
|
|
static const std::string BYTECODE_FILE_EXT = ".luac";
|
|
|
|
static const std::string NOT_BYTECODE_FILE_EXT = ".lua";
|
2014-06-03 09:55:57 +08:00
|
|
|
|
2014-03-10 14:04:58 +08:00
|
|
|
std::string filename(luaL_checkstring(L, 1));
|
2014-09-30 13:11:39 +08:00
|
|
|
size_t pos = filename.rfind(BYTECODE_FILE_EXT);
|
2014-03-10 14:04:58 +08:00
|
|
|
if (pos != std::string::npos)
|
|
|
|
{
|
|
|
|
filename = filename.substr(0, pos);
|
2014-09-30 13:17:03 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-09-30 13:11:39 +08:00
|
|
|
pos = filename.rfind(NOT_BYTECODE_FILE_EXT);
|
|
|
|
if (pos == filename.length() - NOT_BYTECODE_FILE_EXT.length())
|
|
|
|
{
|
|
|
|
filename = filename.substr(0, pos);
|
|
|
|
}
|
2014-03-10 14:04:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pos = filename.find_first_of(".");
|
|
|
|
while (pos != std::string::npos)
|
|
|
|
{
|
|
|
|
filename.replace(pos, 1, "/");
|
|
|
|
pos = filename.find_first_of(".");
|
|
|
|
}
|
2014-06-03 09:55:57 +08:00
|
|
|
|
|
|
|
// search file in package.path
|
2014-06-04 09:59:29 +08:00
|
|
|
unsigned char* chunk = nullptr;
|
2014-06-03 09:55:57 +08:00
|
|
|
ssize_t chunkSize = 0;
|
|
|
|
std::string chunkName;
|
|
|
|
FileUtils* utils = FileUtils::getInstance();
|
2014-03-10 14:04:58 +08:00
|
|
|
|
2014-06-03 09:55:57 +08:00
|
|
|
lua_getglobal(L, "package");
|
|
|
|
lua_getfield(L, -1, "path");
|
|
|
|
std::string searchpath(lua_tostring(L, -1));
|
|
|
|
lua_pop(L, 1);
|
|
|
|
size_t begin = 0;
|
|
|
|
size_t next = searchpath.find_first_of(";", 0);
|
2014-03-10 14:04:58 +08:00
|
|
|
|
2014-06-03 09:55:57 +08:00
|
|
|
do
|
2014-03-10 14:04:58 +08:00
|
|
|
{
|
2014-06-04 09:59:29 +08:00
|
|
|
if (next == std::string::npos)
|
|
|
|
next = searchpath.length();
|
2014-06-03 09:55:57 +08:00
|
|
|
std::string prefix = searchpath.substr(begin, next);
|
|
|
|
if (prefix[0] == '.' && prefix[1] == '/')
|
|
|
|
{
|
|
|
|
prefix = prefix.substr(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = prefix.find("?.lua");
|
2014-06-04 09:59:29 +08:00
|
|
|
chunkName = prefix.substr(0, pos) + filename + BYTECODE_FILE_EXT;
|
2014-06-03 09:55:57 +08:00
|
|
|
if (utils->isFileExist(chunkName))
|
2014-03-10 14:04:58 +08:00
|
|
|
{
|
2014-06-03 09:55:57 +08:00
|
|
|
chunk = utils->getFileData(chunkName.c_str(), "rb", &chunkSize);
|
|
|
|
break;
|
2014-03-10 14:04:58 +08:00
|
|
|
}
|
2014-06-03 09:55:57 +08:00
|
|
|
else
|
|
|
|
{
|
2014-06-04 09:59:29 +08:00
|
|
|
chunkName = prefix.substr(0, pos) + filename + NOT_BYTECODE_FILE_EXT;
|
2014-06-03 09:55:57 +08:00
|
|
|
if (utils->isFileExist(chunkName))
|
|
|
|
{
|
|
|
|
chunk = utils->getFileData(chunkName.c_str(), "rb", &chunkSize);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
begin = next + 1;
|
|
|
|
next = searchpath.find_first_of(";", begin);
|
|
|
|
} while (begin < (int)searchpath.length());
|
|
|
|
|
|
|
|
if (chunk)
|
|
|
|
{
|
|
|
|
LuaStack* stack = LuaEngine::getInstance()->getLuaStack();
|
2014-06-04 09:59:29 +08:00
|
|
|
stack->luaLoadBuffer(L, (char*)chunk, (int)chunkSize, chunkName.c_str());
|
2014-11-04 09:21:57 +08:00
|
|
|
free(chunk);
|
2014-03-10 14:04:58 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-06-05 14:49:02 +08:00
|
|
|
CCLOG("can not get file data of %s", chunkName.c_str());
|
2014-06-03 09:55:57 +08:00
|
|
|
return 0;
|
2014-03-10 14:04:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|