axmol/extensions/scripting/lua-bindings/manual/extension/lua_axis_extension_manual.cpp

389 lines
12 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-25 10:04:45 +08:00
https://axis-project.github.io/
2021-12-25 10:04:45 +08:00
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:
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
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-07-12 21:31:54 +08:00
#include "scripting/lua-bindings/manual/extension/lua_axis_extension_manual.h"
#include "scripting/lua-bindings/auto/lua_axis_extension_auto.hpp"
2019-11-23 20:27:39 +08:00
#include "scripting/lua-bindings/manual/tolua_fix.h"
#include "scripting/lua-bindings/manual/LuaBasicConversions.h"
#include "scripting/lua-bindings/manual/CCLuaValue.h"
#include "cocos-ext.h"
#include "scripting/lua-bindings/manual/CCLuaEngine.h"
2022-07-12 21:31:54 +08:00
#include "scripting/lua-bindings/manual/base/LuaScriptHandlerMgr.h"
2019-11-23 20:27:39 +08:00
USING_NS_AX;
USING_NS_AX_EXT;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
class LuaAssetsManagerDelegateProtocol : public Ref, public AssetsManagerDelegateProtocol
2019-11-23 20:27:39 +08:00
{
public:
2021-12-25 10:04:45 +08:00
virtual ~LuaAssetsManagerDelegateProtocol() {}
2019-11-23 20:27:39 +08:00
virtual void onProgress(int percent) override
{
2021-12-25 10:04:45 +08:00
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(
(void*)this, ScriptHandlerMgr::HandlerType::ASSETSMANAGER_PROGRESS);
2019-11-23 20:27:39 +08:00
if (0 != handler)
{
LuaAssetsManagerEventData eventData(percent);
2021-12-25 10:04:45 +08:00
BasicScriptData data((void*)this, &eventData);
2019-11-23 20:27:39 +08:00
LuaEngine::getInstance()->handleEvent(ScriptHandlerMgr::HandlerType::ASSETSMANAGER_PROGRESS, (void*)&data);
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
virtual void onSuccess() override
{
2021-12-25 10:04:45 +08:00
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(
(void*)this, ScriptHandlerMgr::HandlerType::ASSETSMANAGER_SUCCESS);
2019-11-23 20:27:39 +08:00
if (0 != handler)
{
LuaAssetsManagerEventData eventData;
2021-12-25 10:04:45 +08:00
BasicScriptData data((void*)this, &eventData);
2019-11-23 20:27:39 +08:00
LuaEngine::getInstance()->handleEvent(ScriptHandlerMgr::HandlerType::ASSETSMANAGER_SUCCESS, (void*)&data);
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
virtual void onError(AssetsManager::ErrorCode errorCode) override
{
2021-12-25 10:04:45 +08:00
int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(
(void*)this, ScriptHandlerMgr::HandlerType::ASSETSMANAGER_ERROR);
2019-11-23 20:27:39 +08:00
if (0 != handler)
{
LuaAssetsManagerEventData eventData((int)errorCode);
2021-12-25 10:04:45 +08:00
BasicScriptData data((void*)this, &eventData);
2019-11-23 20:27:39 +08:00
LuaEngine::getInstance()->handleEvent(ScriptHandlerMgr::HandlerType::ASSETSMANAGER_ERROR, (void*)&data);
}
}
};
2022-07-12 21:31:54 +08:00
static int lua_axis_AssetsManager_setDelegate(lua_State* L)
2019-11-23 20:27:39 +08:00
{
if (nullptr == L)
return 0;
2021-12-25 10:04:45 +08:00
int argc = 0;
2019-11-23 20:27:39 +08:00
AssetsManager* self = nullptr;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
2022-07-12 21:31:54 +08:00
if (!tolua_isusertype(L, 1, "ax.AssetsManager", 0, &tolua_err))
2021-12-25 10:04:45 +08:00
goto tolua_lerror;
2019-11-23 20:27:39 +08:00
#endif
2021-12-25 10:04:45 +08:00
self = (AssetsManager*)tolua_tousertype(L, 1, 0);
2019-11-23 20:27:39 +08:00
#if COCOS2D_DEBUG >= 1
if (nullptr == self)
{
2022-07-12 21:31:54 +08:00
tolua_error(L, "invalid 'self' in function 'lua_axis_AssetsManager_setDelegate'\n", nullptr);
2021-12-25 10:04:45 +08:00
return 0;
2019-11-23 20:27:39 +08:00
}
#endif
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
argc = lua_gettop(L) - 1;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (2 == argc)
2021-12-25 10:04:45 +08:00
{
2019-11-23 20:27:39 +08:00
#if COCOS2D_DEBUG >= 1
2021-12-25 10:04:45 +08:00
if (!toluafix_isfunction(L, 2, "LUA_FUNCTION", 0, &tolua_err) || !tolua_isnumber(L, 3, 0, &tolua_err))
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
goto tolua_lerror;
2019-11-23 20:27:39 +08:00
}
#endif
2021-12-25 10:04:45 +08:00
LuaAssetsManagerDelegateProtocol* delegate =
dynamic_cast<LuaAssetsManagerDelegateProtocol*>(self->getDelegate());
2019-11-23 20:27:39 +08:00
if (nullptr == delegate)
{
2021-12-08 00:11:53 +08:00
delegate = new LuaAssetsManagerDelegateProtocol();
2019-11-23 20:27:39 +08:00
self->setUserObject(delegate);
self->setDelegate(delegate);
delegate->release();
}
2021-12-25 10:04:45 +08:00
LUA_FUNCTION handler = toluafix_ref_function(L, 2, 0);
ScriptHandlerMgr::HandlerType handlerType = (ScriptHandlerMgr::HandlerType)(
(int)tolua_tonumber(L, 3, 0) + (int)ScriptHandlerMgr::HandlerType::ASSETSMANAGER_PROGRESS);
2019-11-23 20:27:39 +08:00
ScriptHandlerMgr::getInstance()->addObjectHandler((void*)delegate, handler, handlerType);
return 0;
}
2021-12-25 10:04:45 +08:00
2022-07-12 21:31:54 +08:00
luaL_error(L, "%s has wrong number of arguments: %d, was expecting %d\n", "ax.AssetsManager:setDelegate", argc, 2);
2019-11-23 20:27:39 +08:00
return 0;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
#if COCOS2D_DEBUG >= 1
tolua_lerror:
2022-07-12 21:31:54 +08:00
tolua_error(L, "#ferror in function 'lua_axis_AssetsManager_setDelegate'.", &tolua_err);
2019-11-23 20:27:39 +08:00
return 0;
#endif
}
static void extendAssetsManager(lua_State* L)
{
2022-07-12 21:31:54 +08:00
lua_pushstring(L, "ax.AssetsManager");
2019-11-23 20:27:39 +08:00
lua_rawget(L, LUA_REGISTRYINDEX);
2021-12-25 10:04:45 +08:00
if (lua_istable(L, -1))
2019-11-23 20:27:39 +08:00
{
2022-07-12 21:31:54 +08:00
tolua_function(L, "setDelegate", lua_axis_AssetsManager_setDelegate);
2019-11-23 20:27:39 +08:00
}
lua_pop(L, 1);
}
static void extendManifest(lua_State* L)
{
2022-07-12 21:31:54 +08:00
lua_pushstring(L, "ax.Manifest");
2019-11-23 20:27:39 +08:00
lua_rawget(L, LUA_REGISTRYINDEX);
lua_pop(L, 1);
}
2022-07-12 21:31:54 +08:00
static int lua_axis_Extension_EventListenerAssetsManagerEx_create(lua_State* L)
2019-11-23 20:27:39 +08:00
{
if (nullptr == L)
return 0;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
int argc = 0;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
2022-07-12 21:31:54 +08:00
if (!tolua_isusertable(L, 1, "ax.EventListenerAssetsManagerEx", 0, &tolua_err))
2021-12-25 10:04:45 +08:00
goto tolua_lerror;
2019-11-23 20:27:39 +08:00
#endif
2021-12-25 10:04:45 +08:00
argc = lua_gettop(L) - 1;
2019-11-23 20:27:39 +08:00
if (argc == 2)
{
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
#if COCOS2D_DEBUG >= 1
2022-07-12 21:31:54 +08:00
if (!tolua_isusertype(L, 2, "ax.AssetsManagerEx", 0, &tolua_err) ||
2021-12-25 10:04:45 +08:00
!toluafix_isfunction(L, 3, "LUA_FUNCTION", 0, &tolua_err))
2019-11-23 20:27:39 +08:00
goto tolua_lerror;
#endif
axis::extension::AssetsManagerEx* assetManager =
static_cast<axis::extension::AssetsManagerEx*>(tolua_tousertype(L, 2, nullptr));
2021-12-25 10:04:45 +08:00
LUA_FUNCTION handler = toluafix_ref_function(L, 3, 0);
axis::extension::EventListenerAssetsManagerEx* ret =
axis::extension::EventListenerAssetsManagerEx::create(assetManager, [=](EventAssetsManagerEx* event) {
2021-12-25 10:04:45 +08:00
auto stack = LuaEngine::getInstance()->getLuaStack();
int id = event ? (int)event->_ID : -1;
int* luaID = event ? &event->_luaID : nullptr;
toluafix_pushusertype_ccobject(stack->getLuaState(), id, luaID, (void*)event,
2022-07-12 21:31:54 +08:00
"ax.EventAssetsManagerEx");
2021-12-25 10:04:45 +08:00
stack->executeFunctionByHandler(handler, 1);
});
int id = (ret) ? (int)ret->_ID : -1;
2019-11-23 20:27:39 +08:00
int* luaID = (ret) ? &ret->_luaID : nullptr;
2022-07-12 21:31:54 +08:00
toluafix_pushusertype_ccobject(L, id, luaID, (void*)ret, "ax.EventListenerAssetsManagerEx");
2019-11-23 20:27:39 +08:00
return 1;
}
2021-12-25 10:04:45 +08:00
luaL_error(L, "%s has wrong number of arguments: %d, was expecting %d \n", "create", argc, 2);
2019-11-23 20:27:39 +08:00
return 0;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
#if COCOS2D_DEBUG >= 1
tolua_lerror:
2022-07-12 21:31:54 +08:00
tolua_error(L, "#ferror in function 'lua_axis_Extension_EventListenerAssetsManagerEx_create'.", &tolua_err);
2019-11-23 20:27:39 +08:00
return 0;
#endif
}
static void extendEventListenerAssetsManagerEx(lua_State* L)
{
2022-07-12 21:31:54 +08:00
lua_pushstring(L, "ax.EventListenerAssetsManagerEx");
2019-11-23 20:27:39 +08:00
lua_rawget(L, LUA_REGISTRYINDEX);
2021-12-25 10:04:45 +08:00
if (lua_istable(L, -1))
2019-11-23 20:27:39 +08:00
{
2022-07-12 21:31:54 +08:00
tolua_function(L, "create", lua_axis_Extension_EventListenerAssetsManagerEx_create);
2019-11-23 20:27:39 +08:00
}
lua_pop(L, 1);
}
2022-07-12 21:31:54 +08:00
int lua_axis_extension_ParticleSystem3D_getParticlePool(lua_State* tolua_S)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
int argc = 0;
axis::ParticleSystem3D* cobj = nullptr;
2021-12-25 10:04:45 +08:00
bool ok = true;
2019-11-23 20:27:39 +08:00
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
#endif
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
#if COCOS2D_DEBUG >= 1
2022-07-12 21:31:54 +08:00
if (!tolua_isusertype(tolua_S, 1, "ax.ParticleSystem3D", 0, &tolua_err))
2021-12-25 10:04:45 +08:00
goto tolua_lerror;
2019-11-23 20:27:39 +08:00
#endif
2021-12-25 10:04:45 +08:00
cobj = (axis::ParticleSystem3D*)tolua_tousertype(tolua_S, 1, 0);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
#if COCOS2D_DEBUG >= 1
if (!cobj)
{
2022-07-12 21:31:54 +08:00
tolua_error(tolua_S, "invalid 'cobj' in function 'lua_axis_extension_ParticleSystem3D_getParticlePool'",
2021-12-25 10:04:45 +08:00
nullptr);
2019-11-23 20:27:39 +08:00
return 0;
}
#endif
2021-12-25 10:04:45 +08:00
argc = lua_gettop(tolua_S) - 1;
2019-11-23 20:27:39 +08:00
if (argc == 0)
{
2021-12-25 10:04:45 +08:00
if (!ok)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
tolua_error(tolua_S,
2022-07-12 21:31:54 +08:00
"invalid arguments in function 'lua_axis_extension_ParticleSystem3D_getParticlePool'",
2021-12-25 10:04:45 +08:00
nullptr);
2019-11-23 20:27:39 +08:00
return 0;
}
const axis::ParticlePool& ret = cobj->getParticlePool();
2022-07-12 21:31:54 +08:00
tolua_pushusertype(tolua_S, (void*)&ret, "ax.ParticlePool");
2019-11-23 20:27:39 +08:00
return 1;
}
2021-12-25 10:04:45 +08:00
luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d \n",
2022-07-12 21:31:54 +08:00
"ax.ParticleSystem3D:getParticlePool", argc, 0);
2019-11-23 20:27:39 +08:00
return 0;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
#if COCOS2D_DEBUG >= 1
tolua_lerror:
2022-07-12 21:31:54 +08:00
tolua_error(tolua_S, "#ferror in function 'lua_axis_extension_ParticleSystem3D_getParticlePool'.", &tolua_err);
2019-11-23 20:27:39 +08:00
#endif
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return 0;
}
static void extendParticleSystem3D(lua_State* tolua_S)
{
2022-07-12 21:31:54 +08:00
lua_pushstring(tolua_S, "ax.ParticleSystem3D");
2019-11-23 20:27:39 +08:00
lua_rawget(tolua_S, LUA_REGISTRYINDEX);
2021-12-25 10:04:45 +08:00
if (lua_istable(tolua_S, -1))
2019-11-23 20:27:39 +08:00
{
2022-07-12 21:31:54 +08:00
tolua_function(tolua_S, "getParticlePool", lua_axis_extension_ParticleSystem3D_getParticlePool);
2019-11-23 20:27:39 +08:00
}
lua_pop(tolua_S, 1);
}
2022-07-12 21:31:54 +08:00
int lua_axis_extension_ParticlePool_getActiveDataList(lua_State* tolua_S)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
int argc = 0;
axis::ParticlePool* cobj = nullptr;
2021-12-25 10:04:45 +08:00
bool ok = true;
2019-11-23 20:27:39 +08:00
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
#endif
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
#if COCOS2D_DEBUG >= 1
2022-07-12 21:31:54 +08:00
if (!tolua_isusertype(tolua_S, 1, "ax.ParticlePool", 0, &tolua_err))
2021-12-25 10:04:45 +08:00
goto tolua_lerror;
2019-11-23 20:27:39 +08:00
#endif
2021-12-25 10:04:45 +08:00
cobj = (axis::ParticlePool*)tolua_tousertype(tolua_S, 1, 0);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
#if COCOS2D_DEBUG >= 1
if (!cobj)
{
2022-07-12 21:31:54 +08:00
tolua_error(tolua_S, "invalid 'cobj' in function 'lua_axis_extension_ParticlePool_getActiveDataList'",
2021-12-25 10:04:45 +08:00
nullptr);
2019-11-23 20:27:39 +08:00
return 0;
}
#endif
2021-12-25 10:04:45 +08:00
argc = lua_gettop(tolua_S) - 1;
2019-11-23 20:27:39 +08:00
if (argc == 0)
{
2021-12-25 10:04:45 +08:00
if (!ok)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
tolua_error(tolua_S,
2022-07-12 21:31:54 +08:00
"invalid arguments in function 'lua_axis_extension_ParticlePool_getActiveDataList'",
2021-12-25 10:04:45 +08:00
nullptr);
2019-11-23 20:27:39 +08:00
return 0;
}
const ParticlePool::PoolList& ret = cobj->getActiveDataList();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
lua_newtable(tolua_S);
if (ret.empty())
return 1;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
int index = 1;
for (const auto& obj : ret)
{
if (nullptr == obj)
continue;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
lua_pushnumber(tolua_S, (lua_Number)index);
2022-07-12 21:31:54 +08:00
tolua_pushusertype(tolua_S, (void*)&ret, "ax.ParticlePool");
2019-11-23 20:27:39 +08:00
lua_rawset(tolua_S, -3);
++index;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return 1;
}
2021-12-25 10:04:45 +08:00
luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d \n",
2022-07-12 21:31:54 +08:00
"ax.ParticlePool:getActiveParticleList", argc, 0);
2019-11-23 20:27:39 +08:00
return 0;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
#if COCOS2D_DEBUG >= 1
tolua_lerror:
2022-07-12 21:31:54 +08:00
tolua_error(tolua_S, "#ferror in function 'lua_axis_extension_ParticlePool_getActiveParticleList'.",
2021-12-25 10:04:45 +08:00
&tolua_err);
2019-11-23 20:27:39 +08:00
#endif
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return 0;
}
static void extendParticlePool(lua_State* tolua_S)
{
2022-07-12 21:31:54 +08:00
lua_pushstring(tolua_S, "ax.ParticlePool");
2019-11-23 20:27:39 +08:00
lua_rawget(tolua_S, LUA_REGISTRYINDEX);
2021-12-25 10:04:45 +08:00
if (lua_istable(tolua_S, -1))
2019-11-23 20:27:39 +08:00
{
2022-07-12 21:31:54 +08:00
tolua_function(tolua_S, "getActiveDataList", lua_axis_extension_ParticlePool_getActiveDataList);
2019-11-23 20:27:39 +08:00
}
lua_pop(tolua_S, 1);
}
2022-07-12 21:31:54 +08:00
int register_all_axis_extension_manual(lua_State* tolua_S)
2019-11-23 20:27:39 +08:00
{
extendAssetsManager(tolua_S);
extendManifest(tolua_S);
extendEventListenerAssetsManagerEx(tolua_S);
extendParticleSystem3D(tolua_S);
extendParticlePool(tolua_S);
return 0;
}
int register_extension_module(lua_State* tolua_S)
{
lua_getglobal(tolua_S, "_G");
2021-12-25 10:04:45 +08:00
if (lua_istable(tolua_S, -1)) // stack:...,_G,
2019-11-23 20:27:39 +08:00
{
2022-07-12 21:31:54 +08:00
register_all_axis_extension(tolua_S);
register_all_axis_extension_manual(tolua_S);
2019-11-23 20:27:39 +08:00
}
lua_pop(tolua_S, 1);
return 1;
}