1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-28 08:48:48 +02:00
vcmi/scripting/lua/api/ServerCb.cpp

108 lines
1.7 KiB
C++
Raw Normal View History

/*
* ServerCb.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "ServerCb.h"
#include "Registry.h"
#include "../LuaStack.h"
#include "../../../lib/NetPacks.h"
2022-08-15 13:38:17 +02:00
VCMI_LIB_NAMESPACE_BEGIN
namespace scripting
{
namespace api
{
VCMI_REGISTER_CORE_SCRIPT_API(ServerCbProxy, "Server");
2021-02-16 16:07:30 +02:00
const std::vector<ServerCbProxy::CustomRegType> ServerCbProxy::REGISTER_CUSTOM =
{
{
"addToBattleLog",
2021-02-16 16:07:30 +02:00
&ServerCbProxy::apply<BattleLogMessage>,
false
},
{
"moveUnit",
2021-02-16 16:07:30 +02:00
&ServerCbProxy::apply<BattleStackMoved>,
false
},
{
"changeUnits",
2021-02-16 16:07:30 +02:00
&ServerCbProxy::apply<BattleUnitsChanged>,
false
},
{
"commitPackage",
2021-02-16 16:07:30 +02:00
&ServerCbProxy::commitPackage,
false
}
};
2021-02-16 16:07:30 +02:00
int ServerCbProxy::commitPackage(lua_State * L)
{
2021-02-16 16:07:30 +02:00
LuaStack S(L);
2021-02-16 16:07:30 +02:00
ServerCallback * object = nullptr;
if(!S.tryGet(1, object))
return S.retNil();
lua_remove(L, 1);
if(lua_isuserdata(L, 1) != 1)
2021-02-16 16:07:30 +02:00
return S.retVoid();
lua_getfield(L, 1, "toNetpackLight");
lua_insert(L, 1);
int ret = lua_pcall(L, 1, 1, 0);
if(ret != 0 || !lua_islightuserdata(L, 1))
2021-02-16 16:07:30 +02:00
return S.retVoid();
2023-02-05 18:24:34 +02:00
auto * pack = static_cast<CPackForClient *>(lua_touserdata(L, 1));
object->apply(pack);
2021-02-16 16:07:30 +02:00
return S.retVoid();
}
template<typename NetPack>
2021-02-16 16:07:30 +02:00
int ServerCbProxy::apply(lua_State * L)
{
LuaStack S(L);
2021-02-16 16:07:30 +02:00
ServerCallback * object = nullptr;
if(!S.tryGet(1, object))
return S.retNil();
lua_remove(L, 1);
std::shared_ptr<NetPack> pack;
if(!S.tryGet(1, pack))
return S.retVoid();
object->apply(pack.get());
return S.retVoid();
}
}
}
2022-08-15 13:38:17 +02:00
VCMI_LIB_NAMESPACE_END