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:
|
|
|
|
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;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
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();
|
|
|
|
};
|