1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-17 03:53:07 +01:00
Files
OpenRCT2/src/openrct2/scripting/HookEngine.h
andrewpratt64 62d66ca9d0 Add hook for vehicle crashes to plugin api (#15084)
* Add initial implementation of "vehicle.crash" hook for the scripting api

The hook will fire whenever a vehicle crashes, i.e. an individual car
explodes and it's status becomes "Crashed!"

* Update contributors.md

Add name to contributors list under the "Additional implementation (OpenRCT2)" section.

- If the added line needs to be changed or removed entirely let me know

- I added this based off of the Github wiki: "If it's the first time you're contributing with the project, make sure to update the contributors.md file by appending your name at the end of the respective list."

* Move hook code into function

* Rename hook function

Renamed function, "FireVehicleCrashHook" to "InvokeVehicleCrashHook"

* Wrap InvokeVehicleCrashHook in #ifdef

Move the #ifdef from inside the function body to the outside

* Update changelog and api version

- Added entry to changelog
- Increment API version

* Fix whitespace

Replace tab character with four spaces

* Update src/openrct2/scripting/HookEngine.cpp

Co-authored-by: Tulio Leao <tupaschoal@gmail.com>
2021-07-30 09:03:15 -03:00

101 lines
2.8 KiB
C++

/*****************************************************************************
* Copyright (c) 2014-2020 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
#ifdef ENABLE_SCRIPTING
# include "../common.h"
# include "Duktape.hpp"
# include <any>
# include <memory>
# include <string>
# include <tuple>
# include <vector>
namespace OpenRCT2::Scripting
{
class ScriptEngine;
class ScriptExecutionInfo;
class Plugin;
enum class HOOK_TYPE
{
ACTION_QUERY,
ACTION_EXECUTE,
INTERVAL_TICK,
INTERVAL_DAY,
NETWORK_CHAT,
NETWORK_AUTHENTICATE,
NETWORK_JOIN,
NETWORK_LEAVE,
RIDE_RATINGS_CALCULATE,
ACTION_LOCATION,
GUEST_GENERATION,
VEHICLE_CRASH,
COUNT,
UNDEFINED = -1,
};
constexpr size_t NUM_HOOK_TYPES = static_cast<size_t>(HOOK_TYPE::COUNT);
HOOK_TYPE GetHookType(const std::string& name);
struct Hook
{
uint32_t Cookie;
std::shared_ptr<Plugin> Owner;
DukValue Function;
Hook() = default;
Hook(uint32_t cookie, std::shared_ptr<Plugin> owner, const DukValue& function)
: Cookie(cookie)
, Owner(owner)
, Function(function)
{
}
};
struct HookList
{
HOOK_TYPE Type{};
std::vector<Hook> Hooks;
HookList() = default;
HookList(const HookList&) = delete;
HookList(HookList&& src) = default;
};
class HookEngine
{
private:
ScriptEngine& _scriptEngine;
std::vector<HookList> _hookMap;
uint32_t _nextCookie = 1;
public:
HookEngine(ScriptEngine& scriptEngine);
HookEngine(const HookEngine&) = delete;
uint32_t Subscribe(HOOK_TYPE type, std::shared_ptr<Plugin> owner, const DukValue& function);
void Unsubscribe(HOOK_TYPE type, uint32_t cookie);
void UnsubscribeAll(std::shared_ptr<const Plugin> owner);
void UnsubscribeAll();
bool HasSubscriptions(HOOK_TYPE type) const;
void Call(HOOK_TYPE type, bool isGameStateMutable);
void Call(HOOK_TYPE type, const DukValue& arg, bool isGameStateMutable);
void Call(
HOOK_TYPE type, const std::initializer_list<std::pair<std::string_view, std::any>>& args, bool isGameStateMutable);
private:
HookList& GetHookList(HOOK_TYPE type);
const HookList& GetHookList(HOOK_TYPE type) const;
};
} // namespace OpenRCT2::Scripting
#endif