iw5-mod/src/module/scripting.hpp

86 lines
2.2 KiB
C++
Raw Normal View History

2019-01-13 13:03:46 -05:00
#pragma once
#include "loader/module_loader.hpp"
#include "game/game.hpp"
#include "utils/hook.hpp"
2019-01-16 10:19:21 -05:00
#include "utils/chain.hpp"
2019-01-13 13:03:46 -05:00
class scripting final : public module
{
public:
2019-01-16 10:19:21 -05:00
class entity final
{
public:
2019-01-16 16:44:50 -05:00
entity();
entity(const entity& other) = default;
2019-01-16 10:19:21 -05:00
entity(scripting* environment, unsigned int entity_id);
void on_notify(const std::string& event,
const std::function<void(const std::vector<chaiscript::Boxed_Value>&)>& callback,
bool is_volatile) const;
unsigned int get_entity_id() const;
game::native::scr_entref_t get_entity_reference() const;
2019-01-16 16:44:50 -05:00
void call(const std::string& function, const std::vector<chaiscript::Boxed_Value>& arguments);
2019-01-16 10:19:21 -05:00
private:
scripting* environment_;
unsigned int entity_id_;
};
2019-01-13 13:03:46 -05:00
class variable final
{
public:
variable(game::native::VariableValue value);
~variable();
operator game::native::VariableValue() const;
private:
game::native::VariableValue value_;
};
2019-01-16 10:19:21 -05:00
class event_listener final
{
public:
std::string event = {};
unsigned int entity_id = 0;
std::function<void(const std::vector<chaiscript::Boxed_Value>&)> callback = {};
bool is_volatile = false;
};
2019-01-13 15:28:05 -05:00
void post_start() override;
2019-01-13 13:03:46 -05:00
void post_load() override;
void pre_destroy() override;
static void on_start(const std::function<void()>& callback);
static void on_stop(const std::function<void()>& callback);
2019-01-17 15:05:48 -05:00
static void propagate_error(const std::exception& e);
2019-01-16 16:44:50 -05:00
2019-01-13 13:03:46 -05:00
private:
std::unique_ptr<chaiscript::ChaiScript> chai_;
2019-01-16 10:19:21 -05:00
utils::chain<event_listener> event_listeners_;
void add_event_listener(const event_listener& listener);
2019-01-13 15:28:05 -05:00
void initialize();
void load_scripts() const;
2019-01-13 13:03:46 -05:00
2019-01-16 10:19:21 -05:00
chaiscript::Boxed_Value make_boxed(game::native::VariableValue value);
2019-01-13 15:53:52 -05:00
2019-01-13 13:03:46 -05:00
static utils::hook start_hook_;
static utils::hook stop_hook_;
static std::mutex mutex_;
static std::vector<std::function<void()>> start_callbacks_;
static std::vector<std::function<void()>> stop_callbacks_;
static void start_execution();
static void stop_execution();
2019-01-16 16:44:50 -05:00
static void call(const std::string& function, unsigned int entity_id, const std::vector<chaiscript::Boxed_Value>& arguments);
2019-01-17 15:05:48 -05:00
static bool call_safe(game::native::scr_call_t function, game::native::scr_entref_t entref);
2019-01-16 16:44:50 -05:00
static int find_function_index(const std::string& function);
2019-01-13 13:03:46 -05:00
};