2014-03-10 14:04:58 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2011-2012 cocos2d-x.org
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-10-11 22:57:21 +08:00
|
|
|
Copyright (c) 2021 Bytedance Inc.
|
2014-03-10 14:04:58 +08:00
|
|
|
|
2021-10-11 22:57:21 +08:00
|
|
|
https://adxe.org
|
2014-03-10 14:04:58 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "scripting/lua-bindings/manual/Cocos2dxLuaLoader.h"
|
2014-03-10 14:04:58 +08:00
|
|
|
#include <string>
|
|
|
|
#include <algorithm>
|
|
|
|
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "scripting/lua-bindings/manual/CCLuaStack.h"
|
|
|
|
#include "scripting/lua-bindings/manual/CCLuaEngine.h"
|
2016-04-18 15:09:21 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
2021-10-11 22:34:41 +08:00
|
|
|
#include "yasio/cxx17/string_view.hpp"
|
2014-06-03 09:55:57 +08:00
|
|
|
|
2014-03-10 14:04:58 +08:00
|
|
|
using namespace cocos2d;
|
|
|
|
|
2021-10-11 22:34:41 +08:00
|
|
|
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};
|
|
|
|
}
|
|
|
|
|
2014-03-10 14:04:58 +08:00
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
int cocos2dx_lua_loader(lua_State *L)
|
|
|
|
{
|
2021-10-11 22:34:41 +08:00
|
|
|
auto path = adxelua_tosv(L, 1);
|
2016-04-18 16:45:46 +08:00
|
|
|
|
2021-10-11 22:34:41 +08:00
|
|
|
std::string strPath{path};
|
|
|
|
size_t pos = strPath.find_first_of('.');
|
2014-03-10 14:04:58 +08:00
|
|
|
while (pos != std::string::npos)
|
|
|
|
{
|
2021-10-11 22:34:41 +08:00
|
|
|
strPath.replace(pos, 1, "/");
|
|
|
|
pos = strPath.find_first_of('.');
|
2014-03-10 14:04:58 +08:00
|
|
|
}
|
2016-04-18 16:45:46 +08:00
|
|
|
|
2014-06-03 09:55:57 +08:00
|
|
|
// search file in package.path
|
2016-04-18 16:45:46 +08:00
|
|
|
Data chunk;
|
2021-10-11 22:34:41 +08:00
|
|
|
std::string filePath;
|
2021-10-12 12:11:48 +08:00
|
|
|
auto fileUtils = FileUtils::getInstance();
|
2016-04-18 16:45:46 +08:00
|
|
|
|
2014-06-03 09:55:57 +08:00
|
|
|
lua_getglobal(L, "package");
|
|
|
|
lua_getfield(L, -1, "path");
|
2021-10-11 22:34:41 +08:00
|
|
|
auto searchpath = adxelua_tosv(L, -1);
|
2014-06-03 09:55:57 +08:00
|
|
|
lua_pop(L, 1);
|
|
|
|
size_t begin = 0;
|
2019-04-22 09:44:19 +08:00
|
|
|
size_t next = searchpath.find_first_of(';', 0);
|
2016-04-18 16:45:46 +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();
|
2021-10-11 22:34:41 +08:00
|
|
|
auto prefix = searchpath.substr(begin, next - begin);
|
2014-06-03 09:55:57 +08:00
|
|
|
if (prefix[0] == '.' && prefix[1] == '/')
|
|
|
|
prefix = prefix.substr(2);
|
2016-04-18 16:45:46 +08:00
|
|
|
|
2021-10-12 12:35:03 +08:00
|
|
|
// reserve enough for file path to avoid memory realloc when replace ? to strPath
|
|
|
|
filePath.reserve(prefix.length() + strPath.length());
|
|
|
|
|
2021-10-11 22:34:41 +08:00
|
|
|
filePath.assign(prefix.data(), prefix.length());
|
2021-10-12 12:35:03 +08:00
|
|
|
pos = filePath.find_last_of('?');
|
|
|
|
assert(pos != std::string::npos); // package search path should have '?'
|
|
|
|
if (pos != std::string::npos)
|
2017-08-25 09:32:02 +08:00
|
|
|
{
|
2021-10-11 22:34:41 +08:00
|
|
|
filePath.replace(pos, 1, strPath);
|
2017-08-25 09:32:02 +08:00
|
|
|
}
|
2021-10-11 23:23:30 +08:00
|
|
|
|
2021-10-12 12:11:48 +08:00
|
|
|
if (fileUtils->isFileExist(filePath))
|
2014-03-10 14:04:58 +08:00
|
|
|
{
|
2021-10-12 12:11:48 +08:00
|
|
|
chunk = fileUtils->getDataFromFile(filePath);
|
2014-06-03 09:55:57 +08:00
|
|
|
break;
|
2014-03-10 14:04:58 +08:00
|
|
|
}
|
2016-04-18 16:45:46 +08:00
|
|
|
|
2014-06-03 09:55:57 +08:00
|
|
|
begin = next + 1;
|
2019-04-22 09:44:19 +08:00
|
|
|
next = searchpath.find_first_of(';', begin);
|
2017-08-25 09:32:02 +08:00
|
|
|
} while (begin < searchpath.length());
|
2021-10-11 22:34:41 +08:00
|
|
|
|
2016-04-18 16:45:46 +08:00
|
|
|
if (chunk.getSize() > 0)
|
2014-06-03 09:55:57 +08:00
|
|
|
{
|
|
|
|
LuaStack* stack = LuaEngine::getInstance()->getLuaStack();
|
2021-10-11 22:34:41 +08:00
|
|
|
filePath.insert(filePath.begin(), '@'); // lua standard, add file chunck mark '@'
|
|
|
|
stack->luaLoadBuffer(L, reinterpret_cast<const char*>(chunk.getBytes()), static_cast<int>(chunk.getSize()),
|
|
|
|
filePath.c_str());
|
2014-03-10 14:04:58 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-10-11 22:34:41 +08:00
|
|
|
CCLOG("can not get file data of %s", filePath.c_str());
|
2014-06-03 09:55:57 +08:00
|
|
|
return 0;
|
2014-03-10 14:04:58 +08:00
|
|
|
}
|
2016-04-18 16:45:46 +08:00
|
|
|
|
2014-03-10 14:04:58 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|