OpenRCT2/src/openrct2/scripting/HookEngine.h

97 lines
2.6 KiB
C
Raw Normal View History

2018-03-19 00:35:58 +01:00
/*****************************************************************************
* Copyright (c) 2014-2018 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#pragma once
2020-02-23 16:33:01 +01:00
#ifdef __ENABLE_SCRIPTING__
2020-02-23 16:33:01 +01:00
# include "../common.h"
# include "Duktape.hpp"
# include <any>
# include <memory>
# include <string>
# include <tuple>
# include <vector>
2018-03-19 00:35:58 +01:00
namespace OpenRCT2::Scripting
{
class ScriptExecutionInfo;
class Plugin;
enum class HOOK_TYPE
{
INTERVAL_TICK,
INTERVAL_DAY,
2019-07-25 23:59:26 +02:00
NETWORK_CHAT,
2018-03-19 00:35:58 +01:00
COUNT,
UNDEFINED = -1,
};
constexpr size_t NUM_HOOK_TYPES = static_cast<size_t>(HOOK_TYPE::COUNT);
HOOK_TYPE GetHookType(const std::string& name);
2018-03-19 00:35:58 +01:00
struct Hook
{
2018-07-31 16:25:50 +02:00
uint32_t Cookie;
2018-03-23 23:39:12 +01:00
std::shared_ptr<Plugin> Owner;
2018-03-19 00:35:58 +01:00
DukValue Function;
Hook();
Hook(uint32_t cookie, std::shared_ptr<Plugin> owner, const DukValue& function)
: Cookie(cookie)
, Owner(owner)
, Function(function)
2018-03-19 00:35:58 +01:00
{
}
};
struct HookList
{
2020-02-11 00:33:00 +01:00
HOOK_TYPE Type{};
2018-03-19 00:35:58 +01:00
std::vector<Hook> Hooks;
HookList()
{
}
2018-03-19 00:35:58 +01:00
HookList(const HookList&) = delete;
HookList(HookList&& src)
: Type(std::move(src.Type))
, Hooks(std::move(src.Hooks))
2018-03-19 00:35:58 +01:00
{
}
};
class HookEngine
{
private:
ScriptExecutionInfo& _execInfo;
std::vector<HookList> _hookMap;
uint32_t _nextCookie = 1;
public:
HookEngine(ScriptExecutionInfo& execInfo);
HookEngine(const HookEngine&) = delete;
uint32_t Subscribe(HOOK_TYPE type, std::shared_ptr<Plugin> owner, const DukValue& function);
2018-07-31 16:25:50 +02:00
void Unsubscribe(HOOK_TYPE type, uint32_t cookie);
2018-03-23 23:39:12 +01:00
void UnsubscribeAll(std::shared_ptr<const Plugin> owner);
2020-02-07 00:15:20 +01:00
void UnsubscribeAll();
2020-02-11 00:33:00 +01:00
bool HasSubscriptions(HOOK_TYPE type) const;
2020-02-23 03:00:22 +01:00
void Call(HOOK_TYPE type, bool isGameStateMutable);
void Call(HOOK_TYPE type, DukValue args, bool isGameStateMutable);
2020-02-23 13:22:15 +01:00
void Call(
HOOK_TYPE type, const std::initializer_list<std::pair<std::string_view, std::any>>& args, bool isGameStateMutable);
2018-03-19 00:35:58 +01:00
private:
HookList& GetHookList(HOOK_TYPE type);
2020-02-11 00:33:00 +01:00
const HookList& GetHookList(HOOK_TYPE type) const;
2018-03-19 00:35:58 +01:00
};
} // namespace OpenRCT2::Scripting
2020-02-23 16:33:01 +01:00
#endif