2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2011-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2023-12-08 00:13:39 +08:00
|
|
|
Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2019-11-23 20:27:39 +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.
|
|
|
|
****************************************************************************/
|
2022-08-08 18:02:17 +08:00
|
|
|
#include "scripting/lua-bindings/manual/AxluaLoader.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#include <string>
|
|
|
|
#include <algorithm>
|
|
|
|
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "scripting/lua-bindings/manual/LuaStack.h"
|
|
|
|
#include "scripting/lua-bindings/manual/LuaEngine.h"
|
|
|
|
#include "platform/FileUtils.h"
|
2023-06-24 21:18:27 +08:00
|
|
|
#include "yasio/string_view.hpp"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
extern "C" {
|
2022-08-08 18:02:17 +08:00
|
|
|
int axlua_loader(lua_State* L)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-08-08 18:02:17 +08:00
|
|
|
auto relativePath = axlua_tostr(L, 1);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// convert any '.' to '/'
|
2022-06-24 03:09:50 +08:00
|
|
|
std::replace(relativePath.begin(), relativePath.end(), '.', '/');
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// search file in package.path
|
|
|
|
Data chunk;
|
|
|
|
std::string resolvedPath;
|
|
|
|
auto fileUtils = FileUtils::getInstance();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
lua_getglobal(L, "package");
|
|
|
|
lua_getfield(L, -1, "path");
|
2022-08-08 18:02:17 +08:00
|
|
|
auto searchpath = axlua_tosv(L, -1);
|
2021-12-25 10:04:45 +08:00
|
|
|
lua_pop(L, 1);
|
|
|
|
size_t begin = 0;
|
|
|
|
size_t next = searchpath.find_first_of(';', 0);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
|
|
|
{
|
|
|
|
if (next == std::string::npos)
|
|
|
|
next = searchpath.length();
|
|
|
|
auto prefix = searchpath.substr(begin, next - begin);
|
|
|
|
if (prefix[0] == '.' && prefix[1] == '/')
|
|
|
|
prefix = prefix.substr(2);
|
|
|
|
|
|
|
|
// reserve enough for file path to avoid memory realloc when replace ? to strPath
|
|
|
|
resolvedPath.reserve(prefix.length() + relativePath.length());
|
|
|
|
|
|
|
|
resolvedPath.assign(prefix.data(), prefix.length());
|
2022-06-24 03:09:50 +08:00
|
|
|
auto pos = resolvedPath.find_last_of('?');
|
2021-12-25 10:04:45 +08:00
|
|
|
assert(pos != std::string::npos); // package search path should have '?'
|
|
|
|
if (pos != std::string::npos)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
resolvedPath.replace(pos, 1, relativePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fileUtils->isFileExist(resolvedPath))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
chunk = fileUtils->getDataFromFile(resolvedPath);
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
begin = next + 1;
|
|
|
|
next = searchpath.find_first_of(';', begin);
|
|
|
|
} while (begin < searchpath.length());
|
|
|
|
|
|
|
|
int nret = chunk.getSize() > 0 ? 1 : 0;
|
|
|
|
if (nret)
|
|
|
|
{
|
|
|
|
LuaStack* stack = LuaEngine::getInstance()->getLuaStack();
|
|
|
|
resolvedPath.insert(resolvedPath.begin(), '@'); // lua standard, add file chunck mark '@'
|
|
|
|
stack->luaLoadBuffer(L, reinterpret_cast<const char*>(chunk.getBytes()), static_cast<int>(chunk.getSize()),
|
|
|
|
resolvedPath.c_str());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("can not get file data of %s", resolvedPath.c_str());
|
2021-12-25 10:04:45 +08:00
|
|
|
return nret;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|