mirror of
https://github.com/vcmi/vcmi.git
synced 2025-06-15 00:05:02 +02:00
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there. * Declared existing Entity APIs. * Added basic script context caching * Started Lua script module * Started Lua spell effect API * Started script state persistence * Started battle info callback binding * CommitPackage removed * Extracted spells::Caster to own header; Expanded Spell API. * implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C * !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented * Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key) * Re-enabled VERM macros. * !?GM0 added * !?TM implemented * Added !!MF:N * Started !?OB, !!BM, !!HE, !!OW, !!UN * Added basic support of w-variables * Added support for ERM indirect variables * Made !?FU regular trigger * !!re (ERA loop receiver) implemented * Fixed ERM receivers with zero args.
This commit is contained in:
131
scripting/lua/api/events/SubscriptionRegistryProxy.h
Normal file
131
scripting/lua/api/events/SubscriptionRegistryProxy.h
Normal file
@ -0,0 +1,131 @@
|
||||
/*
|
||||
* SubscriptionRegistryProxy.h, 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
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vcmi/events/Event.h>
|
||||
#include <vcmi/events/EventBus.h>
|
||||
#include <vcmi/events/SubscriptionRegistry.h>
|
||||
|
||||
#include "../../LuaWrapper.h"
|
||||
#include "../../LuaStack.h"
|
||||
#include "../../LuaReference.h"
|
||||
|
||||
namespace scripting
|
||||
{
|
||||
namespace api
|
||||
{
|
||||
namespace events
|
||||
{
|
||||
|
||||
|
||||
class EventSubscriptionProxy : public UniqueOpaqueWrapper<::events::EventSubscription, EventSubscriptionProxy>
|
||||
{
|
||||
public:
|
||||
using Wrapper = UniqueOpaqueWrapper<::events::EventSubscription, EventSubscriptionProxy>;
|
||||
static const std::vector<typename Wrapper::RegType> REGISTER;
|
||||
static const std::vector<typename Wrapper::CustomRegType> REGISTER_CUSTOM;
|
||||
};
|
||||
|
||||
template <typename EventProxy>
|
||||
class SubscriptionRegistryProxy
|
||||
{
|
||||
public:
|
||||
using EventType = typename EventProxy::ObjectType;
|
||||
using RegistryType = ::events::SubscriptionRegistry<EventType>;
|
||||
|
||||
static_assert(std::is_base_of<::events::Event, EventType>::value, "Invalid template parameter");
|
||||
|
||||
static int subscribeBefore(lua_State * L)
|
||||
{
|
||||
LuaStack S(L);
|
||||
// subscription = subscribeBefore(eventBus, callback)
|
||||
|
||||
//TODO: use capture by move from c++14
|
||||
auto callbackRef = std::make_shared<LuaReference>(L);
|
||||
|
||||
::events::EventBus * eventBus = nullptr;
|
||||
|
||||
if(!S.tryGet(1, eventBus))
|
||||
{
|
||||
S.push("No event bus");
|
||||
return 1;
|
||||
}
|
||||
|
||||
S.clear();
|
||||
|
||||
RegistryType * registry = EventType::getRegistry();
|
||||
|
||||
typename EventType::PreHandler callback = [=](EventType & event)
|
||||
{
|
||||
LuaStack S(L);
|
||||
callbackRef->push();
|
||||
S.push(&event);
|
||||
|
||||
if(lua_pcall(L, 1, 0, 0) != 0)
|
||||
{
|
||||
std::string msg;
|
||||
S.tryGet(1, msg);
|
||||
logMod->error("Script callback error: %s", msg);
|
||||
}
|
||||
|
||||
S.clear();
|
||||
};
|
||||
|
||||
std::unique_ptr<::events::EventSubscription> subscription = registry->subscribeBefore(eventBus, std::move(callback));
|
||||
S.push(std::move(subscription));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int subscribeAfter(lua_State * L)
|
||||
{
|
||||
LuaStack S(L);
|
||||
|
||||
//TODO: use capture by move from c++14
|
||||
auto callbackRef = std::make_shared<LuaReference>(L);
|
||||
|
||||
::events::EventBus * eventBus = nullptr;
|
||||
|
||||
if(!S.tryGet(1, eventBus))
|
||||
{
|
||||
S.push("No event bus");
|
||||
return 1;
|
||||
}
|
||||
|
||||
S.clear();
|
||||
|
||||
RegistryType * registry = EventType::getRegistry();
|
||||
|
||||
typename EventType::PostHandler callback = [=](const EventType & event)
|
||||
{
|
||||
LuaStack S(L);
|
||||
callbackRef->push();
|
||||
S.push(const_cast<EventType *>(&event)); //FIXME:
|
||||
|
||||
if(lua_pcall(L, 1, 0, 0) != 0)
|
||||
{
|
||||
std::string msg;
|
||||
S.tryGet(1, msg);
|
||||
logMod->error("Script callback error: %s", msg);
|
||||
}
|
||||
|
||||
S.clear();
|
||||
};
|
||||
|
||||
std::unique_ptr<::events::EventSubscription> subscription = registry->subscribeAfter(eventBus, std::move(callback));
|
||||
S.push(std::move(subscription));
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user