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.
|
2021-10-12 14:18:02 +08:00
|
|
|
Copyright (c) 2021 Bytedance Inc.
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-10-12 14:18:02 +08:00
|
|
|
https://adxe.org
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "scripting/lua-bindings/manual/CCLuaStack.h"
|
|
|
|
#include "scripting/lua-bindings/manual/tolua_fix.h"
|
|
|
|
#include <string.h>
|
|
|
|
extern "C" {
|
|
|
|
#include "lua.h"
|
|
|
|
#include "tolua++.h"
|
|
|
|
#include "lualib.h"
|
|
|
|
#include "lauxlib.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "scripting/lua-bindings/manual/Cocos2dxLuaLoader.h"
|
|
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
|
2021-12-25 10:04:45 +08:00
|
|
|
# include "scripting/lua-bindings/manual/platform/ios/CCLuaObjcBridge.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
2021-12-25 10:04:45 +08:00
|
|
|
# include "scripting/lua-bindings/manual/platform/android/CCLuaJavaBridge.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "scripting/lua-bindings/manual/cocos2d/LuaScriptHandlerMgr.h"
|
|
|
|
#include "scripting/lua-bindings/auto/lua_cocos2dx_auto.hpp"
|
|
|
|
#include "scripting/lua-bindings/manual/cocos2d/lua_cocos2dx_manual.hpp"
|
|
|
|
#include "scripting/lua-bindings/manual/LuaBasicConversions.h"
|
|
|
|
#include "scripting/lua-bindings/auto/lua_cocos2dx_physics_auto.hpp"
|
|
|
|
#include "scripting/lua-bindings/manual/cocos2d/lua_cocos2dx_physics_manual.hpp"
|
|
|
|
#include "scripting/lua-bindings/auto/lua_cocos2dx_backend_auto.hpp"
|
|
|
|
#include "base/ZipUtils.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
int get_string_for_print(lua_State* L, std::string* out)
|
|
|
|
{
|
|
|
|
int n = lua_gettop(L); /* number of arguments */
|
|
|
|
int i;
|
|
|
|
|
|
|
|
lua_getglobal(L, "tostring");
|
|
|
|
for (i = 1; i <= n; i++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
const char* s;
|
|
|
|
lua_pushvalue(L, -1); /* function to be called */
|
|
|
|
lua_pushvalue(L, i); /* value to print */
|
|
|
|
lua_call(L, 1, 1);
|
|
|
|
size_t sz;
|
|
|
|
s = lua_tolstring(L, -1, &sz); /* get result */
|
|
|
|
if (s == NULL)
|
|
|
|
return luaL_error(L, "'tostring' must return a string to 'print'");
|
|
|
|
if (i > 1)
|
|
|
|
out->append("\t");
|
|
|
|
out->append(s, sz);
|
|
|
|
lua_pop(L, 1); /* pop result */
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int lua_print(lua_State* L)
|
|
|
|
{
|
|
|
|
std::string t;
|
|
|
|
get_string_for_print(L, &t);
|
|
|
|
CCLOG("[LUA-print] %s", t.c_str());
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int lua_release_print(lua_State* L)
|
|
|
|
{
|
|
|
|
std::string t;
|
|
|
|
get_string_for_print(L, &t);
|
|
|
|
log("[LUA-print] %s", t.c_str());
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2021-04-22 22:01:47 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int lua_version(lua_State* L)
|
|
|
|
{
|
|
|
|
lua_pushinteger(L, LUA_VERSION_NUM);
|
|
|
|
return 1;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
} // namespace
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
LuaStack::~LuaStack()
|
|
|
|
{
|
|
|
|
if (nullptr != _state)
|
|
|
|
{
|
|
|
|
lua_close(_state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
LuaStack* LuaStack::create()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
LuaStack* stack = new LuaStack();
|
2019-11-23 20:27:39 +08:00
|
|
|
stack->init();
|
|
|
|
stack->autorelease();
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
LuaStack* LuaStack::attach(lua_State* L)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
LuaStack* stack = new LuaStack();
|
2019-11-23 20:27:39 +08:00
|
|
|
stack->initWithLuaState(L);
|
|
|
|
stack->autorelease();
|
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LuaStack::init()
|
|
|
|
{
|
|
|
|
_state = lua_open();
|
|
|
|
luaL_openlibs(_state);
|
|
|
|
toluafix_open(_state);
|
|
|
|
|
|
|
|
// Register our version of the global "print" function
|
2021-12-25 10:04:45 +08:00
|
|
|
const luaL_Reg global_functions[] = {
|
|
|
|
{"print", lua_print}, {"release_print", lua_release_print}, {"version", lua_version}, {nullptr, nullptr}};
|
2019-11-23 20:27:39 +08:00
|
|
|
luaL_register(_state, "_G", global_functions);
|
|
|
|
|
|
|
|
g_luaType.clear();
|
|
|
|
register_all_cocos2dx(_state);
|
|
|
|
register_all_cocos2dx_backend(_state);
|
|
|
|
register_all_cocos2dx_manual(_state);
|
|
|
|
register_all_cocos2dx_module_manual(_state);
|
|
|
|
register_all_cocos2dx_math_manual(_state);
|
|
|
|
register_all_cocos2dx_shaders_manual(_state);
|
|
|
|
register_all_cocos2dx_bytearray_manual(_state);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
tolua_luanode_open(_state);
|
|
|
|
register_luanode_manual(_state);
|
|
|
|
#if CC_USE_PHYSICS
|
|
|
|
register_all_cocos2dx_physics(_state);
|
|
|
|
register_all_cocos2dx_physics_manual(_state);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
|
|
|
|
LuaObjcBridge::luaopen_luaoc(_state);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
|
|
|
LuaJavaBridge::luaopen_luaj(_state);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
tolua_script_handler_mgr_open(_state);
|
|
|
|
|
|
|
|
// add cocos2dx loader
|
|
|
|
addLuaLoader(cocos2dx_lua_loader);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool LuaStack::initWithLuaState(lua_State* L)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_state = L;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaStack::addSearchPath(const char* path)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
lua_getglobal(_state, "package"); /* L: package */
|
|
|
|
lua_getfield(_state, -1, "path"); /* get package.path, L: package path */
|
|
|
|
const char* cur_path = lua_tostring(_state, -1);
|
|
|
|
lua_pushfstring(_state, "%s/?.lua;%s", path, cur_path); /* L: package path newpath */
|
|
|
|
lua_setfield(_state, -3, "path"); /* package.path = newpath, L: package path */
|
|
|
|
lua_pop(_state, 2); /* L: - */
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LuaStack::addLuaLoader(lua_CFunction func)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!func)
|
|
|
|
return;
|
|
|
|
|
2020-11-18 12:16:36 +08:00
|
|
|
#if LUA_VERSION_NUM >= 504 || (LUA_VERSION_NUM >= 502 && !defined(LUA_COMPAT_LOADERS))
|
|
|
|
const char* realname = "searchers";
|
|
|
|
#else
|
|
|
|
const char* realname = "loaders";
|
|
|
|
#endif
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// stack content after the invoking of the function
|
|
|
|
// get loader table
|
2021-12-25 10:04:45 +08:00
|
|
|
lua_getglobal(_state, "package"); /* L: package */
|
|
|
|
lua_getfield(_state, -1, realname); /* L: package, loaders */
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// insert loader into index 2
|
2021-12-25 10:04:45 +08:00
|
|
|
lua_pushcfunction(_state, func); /* L: package, loaders, func */
|
2019-11-23 20:27:39 +08:00
|
|
|
for (int i = (int)(lua_objlen(_state, -2) + 1); i > 2; --i)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
lua_rawgeti(_state, -2, i - 1); /* L: package, loaders, func, function */
|
2019-11-23 20:27:39 +08:00
|
|
|
// we call lua_rawgeti, so the loader table now is at -3
|
2021-12-25 10:04:45 +08:00
|
|
|
lua_rawseti(_state, -3, i); /* L: package, loaders, func */
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
lua_rawseti(_state, -2, 2); /* L: package, loaders */
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// set loaders into package
|
2021-12-25 10:04:45 +08:00
|
|
|
lua_setfield(_state, -2, realname); /* L: package */
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
lua_pop(_state, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaStack::removeScriptObjectByObject(Ref* pObj)
|
|
|
|
{
|
|
|
|
toluafix_remove_ccobject_by_refid(_state, pObj->_luaID);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaStack::removeScriptHandler(int nHandler)
|
|
|
|
{
|
|
|
|
toluafix_remove_function_by_refid(_state, nHandler);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int LuaStack::executeString(const char* codes)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
luaL_loadstring(_state, codes);
|
|
|
|
return executeFunction(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaStack::executeScriptFile(const char* filename)
|
|
|
|
{
|
|
|
|
CCAssert(filename, "CCLuaStack::executeScriptFile() - invalid filename");
|
|
|
|
|
2021-10-12 14:18:02 +08:00
|
|
|
std::string filePath{filename};
|
|
|
|
Data data = FileUtils::getInstance()->getDataFromFile(filePath);
|
|
|
|
int rn = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
if (!data.isNull())
|
|
|
|
{
|
2021-10-12 14:18:02 +08:00
|
|
|
filePath.insert(filePath.begin(), '@'); // lua standard, add file chunck mark '@'
|
|
|
|
if (luaLoadBuffer(_state, (const char*)data.getBytes(), (int)data.getSize(), filePath.c_str()) == 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
rn = executeFunction(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rn;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaStack::executeGlobalFunction(const char* functionName)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
lua_getglobal(_state, functionName); /* query function by name, stack: function */
|
2019-11-23 20:27:39 +08:00
|
|
|
if (!lua_isfunction(_state, -1))
|
|
|
|
{
|
|
|
|
CCLOG("[LUA ERROR] name '%s' does not represent a Lua function", functionName);
|
|
|
|
lua_pop(_state, 1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return executeFunction(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaStack::clean()
|
|
|
|
{
|
|
|
|
lua_settop(_state, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaStack::pushInt(int intValue)
|
|
|
|
{
|
|
|
|
lua_pushinteger(_state, intValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaStack::pushFloat(float floatValue)
|
|
|
|
{
|
|
|
|
lua_pushnumber(_state, floatValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaStack::pushLong(long longValue)
|
|
|
|
{
|
|
|
|
lua_pushnumber(_state, longValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaStack::pushBoolean(bool boolValue)
|
|
|
|
{
|
|
|
|
lua_pushboolean(_state, boolValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaStack::pushString(const char* stringValue)
|
|
|
|
{
|
|
|
|
lua_pushstring(_state, stringValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaStack::pushString(const char* stringValue, int length)
|
|
|
|
{
|
|
|
|
lua_pushlstring(_state, stringValue, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaStack::pushNil()
|
|
|
|
{
|
|
|
|
lua_pushnil(_state);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaStack::pushObject(Ref* objectValue, const char* typeName)
|
|
|
|
{
|
|
|
|
toluafix_pushusertype_ccobject(_state, objectValue->_ID, &objectValue->_luaID, objectValue, typeName);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaStack::pushLuaValue(const LuaValue& value)
|
|
|
|
{
|
|
|
|
const LuaValueType type = value.getType();
|
|
|
|
if (type == LuaValueTypeInt)
|
|
|
|
{
|
|
|
|
return pushInt(value.intValue());
|
|
|
|
}
|
|
|
|
else if (type == LuaValueTypeFloat)
|
|
|
|
{
|
|
|
|
return pushFloat(value.floatValue());
|
|
|
|
}
|
|
|
|
else if (type == LuaValueTypeBoolean)
|
|
|
|
{
|
|
|
|
return pushBoolean(value.booleanValue());
|
|
|
|
}
|
|
|
|
else if (type == LuaValueTypeString)
|
|
|
|
{
|
|
|
|
return pushString(value.stringValue().c_str());
|
|
|
|
}
|
|
|
|
else if (type == LuaValueTypeDict)
|
|
|
|
{
|
|
|
|
pushLuaValueDict(value.dictValue());
|
|
|
|
}
|
|
|
|
else if (type == LuaValueTypeArray)
|
|
|
|
{
|
|
|
|
pushLuaValueArray(value.arrayValue());
|
|
|
|
}
|
|
|
|
else if (type == LuaValueTypeObject)
|
|
|
|
{
|
|
|
|
pushObject(value.ccobjectValue(), value.getObjectTypename().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaStack::pushLuaValueDict(const LuaValueDict& dict)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
lua_newtable(_state); /* L: table */
|
2019-11-23 20:27:39 +08:00
|
|
|
for (LuaValueDictIterator it = dict.begin(); it != dict.end(); ++it)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
lua_pushstring(_state, it->first.c_str()); /* L: table key */
|
|
|
|
pushLuaValue(it->second); /* L: table key value */
|
|
|
|
lua_rawset(_state, -3); /* table.key = value, L: table */
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaStack::pushLuaValueArray(const LuaValueArray& array)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
lua_newtable(_state); /* L: table */
|
2019-11-23 20:27:39 +08:00
|
|
|
int index = 1;
|
|
|
|
for (LuaValueArrayIterator it = array.begin(); it != array.end(); ++it)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
pushLuaValue(*it); /* L: table value */
|
|
|
|
lua_rawseti(_state, -2, index); /* table[index] = value, L: table */
|
2019-11-23 20:27:39 +08:00
|
|
|
++index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LuaStack::pushFunctionByHandler(int nHandler)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
toluafix_get_function_by_refid(_state, nHandler); /* L: ... func */
|
2019-11-23 20:27:39 +08:00
|
|
|
if (!lua_isfunction(_state, -1))
|
|
|
|
{
|
|
|
|
CCLOG("[LUA ERROR] function refid '%d' does not reference a Lua function", nHandler);
|
|
|
|
lua_pop(_state, 1);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaStack::executeFunction(int numArgs)
|
|
|
|
{
|
|
|
|
int functionIndex = -(numArgs + 1);
|
|
|
|
if (!lua_isfunction(_state, functionIndex))
|
|
|
|
{
|
|
|
|
CCLOG("value at stack [%d] is not function", functionIndex);
|
2021-12-25 10:04:45 +08:00
|
|
|
lua_pop(_state, numArgs + 1); // remove function and arguments
|
2019-11-23 20:27:39 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int traceback = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
lua_getglobal(_state, "__G__TRACKBACK__"); /* L: ... func arg1 arg2 ... G */
|
2019-11-23 20:27:39 +08:00
|
|
|
if (!lua_isfunction(_state, -1))
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
lua_pop(_state, 1); /* L: ... func arg1 arg2 ... */
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
lua_insert(_state, functionIndex - 1); /* L: ... G func arg1 arg2 ... */
|
2019-11-23 20:27:39 +08:00
|
|
|
traceback = functionIndex - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int error = 0;
|
|
|
|
++_callFromLua;
|
2021-12-25 10:04:45 +08:00
|
|
|
error = lua_pcall(_state, numArgs, 1, traceback); /* L: ... [G] ret */
|
2019-11-23 20:27:39 +08:00
|
|
|
--_callFromLua;
|
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
if (traceback == 0)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
CCLOG("[LUA ERROR] %s", lua_tostring(_state, -1)); /* L: ... error */
|
|
|
|
lua_pop(_state, 1); // remove error message from stack
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else /* L: ... G error */
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
lua_pop(_state, 2); // remove __G__TRACKBACK__ and error message from stack
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get return value
|
|
|
|
int ret = 0;
|
|
|
|
if (lua_isnumber(_state, -1))
|
|
|
|
{
|
|
|
|
ret = (int)lua_tointeger(_state, -1);
|
|
|
|
}
|
|
|
|
else if (lua_isboolean(_state, -1))
|
|
|
|
{
|
|
|
|
ret = (int)lua_toboolean(_state, -1);
|
|
|
|
}
|
|
|
|
// remove return value from stack
|
2021-12-25 10:04:45 +08:00
|
|
|
lua_pop(_state, 1); /* L: ... [G] */
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
if (traceback)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
lua_pop(_state, 1); // remove __G__TRACKBACK__ from stack /* L: ... */
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaStack::executeFunctionByHandler(int nHandler, int numArgs)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (pushFunctionByHandler(nHandler)) /* L: ... arg1 arg2 ... func */
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (numArgs > 0)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
lua_insert(_state, -(numArgs + 1)); /* L: ... func arg1 arg2 ... */
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
ret = executeFunction(numArgs);
|
|
|
|
}
|
|
|
|
lua_settop(_state, 0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool LuaStack::handleAssert(const char* msg)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_callFromLua == 0)
|
|
|
|
return false;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
lua_pushfstring(_state, "ASSERT FAILED ON LUA EXECUTE: %s", msg ? msg : "unknown");
|
|
|
|
lua_error(_state);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaStack::reallocateScriptHandler(int nHandler)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
LUA_FUNCTION nNewHandle = -1;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
if (pushFunctionByHandler(nHandler))
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
nNewHandle = toluafix_ref_function(_state, lua_gettop(_state), 0);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
/*
|
|
|
|
toluafix_get_function_by_refid(_state,nNewHandle);
|
|
|
|
if (!lua_isfunction(_state, -1))
|
|
|
|
{
|
|
|
|
CCLOG("Error!");
|
|
|
|
}
|
|
|
|
lua_settop(_state, 0);
|
|
|
|
*/
|
2019-11-23 20:27:39 +08:00
|
|
|
return nNewHandle;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int LuaStack::executeFunction(int handler,
|
|
|
|
int numArgs,
|
|
|
|
int numResults,
|
|
|
|
const std::function<void(lua_State*, int)>& func)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (pushFunctionByHandler(handler)) /* L: ... arg1 arg2 ... func */
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (numArgs > 0)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
lua_insert(_state, -(numArgs + 1)); /* L: ... func arg1 arg2 ... */
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int functionIndex = -(numArgs + 1);
|
|
|
|
|
|
|
|
if (!lua_isfunction(_state, functionIndex))
|
|
|
|
{
|
|
|
|
CCLOG("value at stack [%d] is not function", functionIndex);
|
2021-12-25 10:04:45 +08:00
|
|
|
lua_pop(_state, numArgs + 1); // remove function and arguments
|
2019-11-23 20:27:39 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int traceCallback = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
lua_getglobal(_state, "__G__TRACKBACK__"); /* L: ... func arg1 arg2 ... G */
|
2019-11-23 20:27:39 +08:00
|
|
|
if (!lua_isfunction(_state, -1))
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
lua_pop(_state, 1); /* L: ... func arg1 arg2 ... */
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
lua_insert(_state, functionIndex - 1); /* L: ... G func arg1 arg2 ... */
|
2019-11-23 20:27:39 +08:00
|
|
|
traceCallback = functionIndex - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int error = 0;
|
|
|
|
++_callFromLua;
|
2021-12-25 10:04:45 +08:00
|
|
|
error = lua_pcall(_state, numArgs, numResults, traceCallback); /* L: ... [G] ret1 ret2 ... retResults*/
|
2019-11-23 20:27:39 +08:00
|
|
|
--_callFromLua;
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
if (traceCallback == 0)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
CCLOG("[LUA ERROR] %s", lua_tostring(_state, -1)); /* L: ... error */
|
|
|
|
lua_pop(_state, 1); // remove error message from stack
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else /* L: ... G error */
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
lua_pop(_state, 2); // remove __G__TRACKBACK__ and error message from stack
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get return value,don't pass LUA_MULTRET to numResults,
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
if (numResults <= 0 || nullptr == func)
|
|
|
|
break;
|
|
|
|
|
|
|
|
func(_state, numResults);
|
|
|
|
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
if (traceCallback)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
lua_pop(_state, 1); // remove __G__TRACKBACK__ from stack /* L: ... */
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaStack::reload(const char* moduleFileName)
|
|
|
|
{
|
|
|
|
if (nullptr == moduleFileName || strlen(moduleFileName) == 0)
|
|
|
|
{
|
|
|
|
CCLOG("moudulFileName is null");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
lua_getglobal(_state, "package"); /* L: package */
|
|
|
|
lua_getfield(_state, -1, "loaded"); /* L: package loaded */
|
2019-11-23 20:27:39 +08:00
|
|
|
lua_pushstring(_state, moduleFileName);
|
2021-12-25 10:04:45 +08:00
|
|
|
lua_gettable(_state, -2); /* L:package loaded module */
|
2019-11-23 20:27:39 +08:00
|
|
|
if (!lua_isnil(_state, -1))
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
lua_pushstring(_state, moduleFileName); /* L:package loaded module name */
|
|
|
|
lua_pushnil(_state); /* L:package loaded module name nil*/
|
|
|
|
lua_settable(_state, -4); /* L:package loaded module */
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
lua_pop(_state, 3);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
std::string name = moduleFileName;
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string require = "require \'" + name + "\'";
|
|
|
|
return executeString(require.c_str());
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int LuaStack::loadChunksFromZIP(const char* zipFilePath)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
pushString(zipFilePath);
|
|
|
|
luaLoadChunksFromZIP(_state);
|
|
|
|
int ret = lua_toboolean(_state, -1);
|
|
|
|
lua_pop(_state, 1);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int LuaStack::luaLoadChunksFromZIP(lua_State* L)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (lua_gettop(L) < 1)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
CCLOG("luaLoadChunksFromZIP() - invalid arguments");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-10-12 14:18:02 +08:00
|
|
|
using namespace cxx17;
|
|
|
|
const auto BYTECODE_FILE_EXT = ".luac"_sv;
|
|
|
|
const auto NOT_BYTECODE_FILE_EXT = ".lua"_sv;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
const char* zipFilename = lua_tostring(L, -1);
|
2019-11-23 20:27:39 +08:00
|
|
|
lua_settop(L, 0);
|
2021-12-25 10:04:45 +08:00
|
|
|
FileUtils* utils = FileUtils::getInstance();
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string zipFilePath = utils->fullPathForFilename(zipFilename);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
LuaStack* stack = this;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
|
|
|
{
|
|
|
|
void* buffer = nullptr;
|
|
|
|
ZipFile* zip = nullptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
Data zipFileData(utils->getDataFromFile(zipFilePath));
|
|
|
|
unsigned char* bytes = zipFileData.getBytes();
|
2021-12-25 10:04:45 +08:00
|
|
|
ssize_t size = zipFileData.getSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (size > 0)
|
|
|
|
{
|
2019-11-24 23:16:11 +08:00
|
|
|
zip = ZipFile::createWithBuffer(bytes, (unsigned long)size);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (zip)
|
|
|
|
{
|
2020-02-15 23:48:21 +08:00
|
|
|
CCLOG("lua_loadChunksFromZIP() - load zip file: %s", zipFilePath.c_str());
|
2019-11-23 20:27:39 +08:00
|
|
|
lua_getglobal(L, "package");
|
|
|
|
lua_getfield(L, -1, "preload");
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int count = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
std::string filename = zip->getFirstFilename();
|
2021-12-25 10:04:45 +08:00
|
|
|
while (filename.length())
|
|
|
|
{
|
|
|
|
ssize_t bufferSize = 0;
|
|
|
|
unsigned char* zbuffer = zip->getFileData(filename.c_str(), &bufferSize);
|
|
|
|
if (bufferSize)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
// remove .lua or .luac extension
|
|
|
|
size_t pos = filename.find_last_of('.');
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
{
|
|
|
|
std::string suffix = filename.substr(pos, filename.length());
|
2021-10-12 14:18:02 +08:00
|
|
|
if (cxx17::string_view{suffix} == NOT_BYTECODE_FILE_EXT ||
|
|
|
|
cxx17::string_view{suffix} == BYTECODE_FILE_EXT)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
filename.erase(pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// replace path separator '/' '\' to '.'
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto& character : filename)
|
|
|
|
{
|
|
|
|
if (character == '/' || character == '\\')
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
character = '.';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CCLOG("[luaLoadChunksFromZIP] add %s to preload", filename.c_str());
|
2021-12-25 10:04:45 +08:00
|
|
|
if (stack->luaLoadBuffer(L, (char*)zbuffer, (int)bufferSize, filename.c_str()) == 0)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
lua_setfield(L, -2, filename.c_str());
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
free(zbuffer);
|
|
|
|
}
|
|
|
|
filename = zip->getNextFilename();
|
|
|
|
}
|
|
|
|
CCLOG("lua_loadChunksFromZIP() - loaded chunks count: %d", count);
|
|
|
|
lua_pop(L, 2);
|
|
|
|
lua_pushboolean(L, 1);
|
|
|
|
|
|
|
|
delete zip;
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
CCLOG("lua_loadChunksFromZIP() - not found or invalid zip file: %s", zipFilePath.c_str());
|
|
|
|
lua_pushboolean(L, 0);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (buffer)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
free(buffer);
|
|
|
|
}
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
void skipBOM(const char*& chunk, int& chunkSize)
|
|
|
|
{
|
|
|
|
// UTF-8 BOM? skip
|
2021-12-25 10:04:45 +08:00
|
|
|
if (chunkSize >= 3 && static_cast<unsigned char>(chunk[0]) == 0xEF &&
|
|
|
|
static_cast<unsigned char>(chunk[1]) == 0xBB && static_cast<unsigned char>(chunk[2]) == 0xBF)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
chunk += 3;
|
|
|
|
chunkSize -= 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
} // end anonymous namespace
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int LuaStack::luaLoadBuffer(lua_State* L, const char* chunk, int chunkSize, const char* chunkName)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
int r = 0;
|
|
|
|
|
2019-11-24 23:16:11 +08:00
|
|
|
skipBOM(chunk, chunkSize);
|
|
|
|
r = luaL_loadbuffer(L, chunk, chunkSize, chunkName);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
#if defined(COCOS2D_DEBUG) && COCOS2D_DEBUG > 0
|
|
|
|
if (r)
|
|
|
|
{
|
|
|
|
switch (r)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
case LUA_ERRSYNTAX:
|
|
|
|
CCLOG("[LUA ERROR] load \"%s\", error: syntax error during pre-compilation.", chunkName);
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
case LUA_ERRMEM:
|
|
|
|
CCLOG("[LUA ERROR] load \"%s\", error: memory allocation error.", chunkName);
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
case LUA_ERRFILE:
|
|
|
|
CCLOG("[LUA ERROR] load \"%s\", error: cannot open/read file.", chunkName);
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
default:
|
|
|
|
CCLOG("[LUA ERROR] load \"%s\", error: unknown.", chunkName);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|