iw5-mod/src/module/scripting.cpp

102 lines
1.9 KiB
C++
Raw Normal View History

2019-01-13 05:16:19 -05:00
#include <std_include.hpp>
2019-01-13 13:03:46 -05:00
#include "scripting.hpp"
2019-01-13 05:16:19 -05:00
2019-01-13 13:03:46 -05:00
utils::hook scripting::start_hook_;
utils::hook scripting::stop_hook_;
std::mutex scripting::mutex_;
std::vector<std::function<void()>> scripting::start_callbacks_;
std::vector<std::function<void()>> scripting::stop_callbacks_;
scripting::variable::variable(game::native::VariableValue value) : value_(value)
2019-01-13 05:16:19 -05:00
{
2019-01-13 13:03:46 -05:00
game::native::AddRefToValue(&value);
}
scripting::variable::~variable()
{
game::native::RemoveRefToValue(this->value_.type, this->value_.u);
}
scripting::variable::operator game::native::VariableValue() const
{
return this->value_;
}
void scripting::post_load()
{
start_hook_.initialize(SELECT_VALUE(0x50C575, 0x50D4F2, 0x48A026), []()
2019-01-13 05:16:19 -05:00
{
2019-01-13 13:03:46 -05:00
start_execution();
static_cast<void(*)()>(start_hook_.get_original())();
}, HOOK_CALL)->install()->quick();
stop_hook_.initialize(SELECT_VALUE(0x528B04, 0x569E46, 0x4F03FA), []()
{
stop_execution();
static_cast<void(*)()>(stop_hook_.get_original())();
}, HOOK_CALL)->install()->quick();
on_start([this]()
{
this->chai_ = std::make_unique<chaiscript::ChaiScript>();
});
on_stop([this]()
{
this->chai_ = {};
});
}
void scripting::pre_destroy()
{
this->chai_ = {};
start_callbacks_.clear();
stop_callbacks_.clear();
}
void scripting::on_start(const std::function<void()>& callback)
{
std::lock_guard _(mutex_);
start_callbacks_.push_back(callback);
}
void scripting::on_stop(const std::function<void()>& callback)
{
std::lock_guard _(mutex_);
stop_callbacks_.push_back(callback);
}
void scripting::start_execution()
{
std::vector<std::function<void()>> copy;
{
std::lock_guard _(mutex_);
copy = start_callbacks_;
}
for (const auto& callback : copy)
{
callback();
2019-01-13 05:16:19 -05:00
}
2019-01-13 13:03:46 -05:00
}
void scripting::stop_execution()
{
std::vector<std::function<void()>> copy;
2019-01-13 06:07:19 -05:00
2019-01-13 13:03:46 -05:00
{
std::lock_guard _(mutex_);
copy = stop_callbacks_;
}
2019-01-13 06:07:19 -05:00
2019-01-13 13:03:46 -05:00
for (const auto& callback : copy)
{
callback();
}
}
2019-01-13 06:07:19 -05:00
2019-01-13 05:16:19 -05:00
REGISTER_MODULE(scripting)