Improve scripting notifications
This commit is contained in:
parent
7cde029b78
commit
f7384926f0
@ -10,7 +10,8 @@ public:
|
|||||||
public:
|
public:
|
||||||
std::string name;
|
std::string name;
|
||||||
game::native::scr_entref_t entity;
|
game::native::scr_entref_t entity;
|
||||||
std::vector<scripting::variable> arguments;
|
//std::vector<scripting::variable> arguments;
|
||||||
|
std::vector<game::native::VariableValue> arguments;
|
||||||
};
|
};
|
||||||
|
|
||||||
void post_load() override;
|
void post_load() override;
|
||||||
|
@ -12,12 +12,12 @@ std::vector<std::function<void()>> scripting::stop_callbacks_;
|
|||||||
|
|
||||||
scripting::variable::variable(game::native::VariableValue value) : value_(value)
|
scripting::variable::variable(game::native::VariableValue value) : value_(value)
|
||||||
{
|
{
|
||||||
//game::native::AddRefToValue(&value);
|
game::native::AddRefToValue(&value);
|
||||||
}
|
}
|
||||||
|
|
||||||
scripting::variable::~variable()
|
scripting::variable::~variable()
|
||||||
{
|
{
|
||||||
//game::native::RemoveRefToValue(this->value_.type, this->value_.u);
|
game::native::RemoveRefToValue(this->value_.type, this->value_.u);
|
||||||
}
|
}
|
||||||
|
|
||||||
scripting::variable::operator game::native::VariableValue() const
|
scripting::variable::operator game::native::VariableValue() const
|
||||||
@ -76,6 +76,13 @@ void scripting::initialize()
|
|||||||
|
|
||||||
notification::listen([this](notification::event* event)
|
notification::listen([this](notification::event* event)
|
||||||
{
|
{
|
||||||
|
std::vector<chaiscript::Boxed_Value> arguments;
|
||||||
|
|
||||||
|
for (const auto& argument : event->arguments)
|
||||||
|
{
|
||||||
|
arguments.push_back(make_boxed(argument));
|
||||||
|
}
|
||||||
|
|
||||||
decltype(this->callbacks_) copy;
|
decltype(this->callbacks_) copy;
|
||||||
{
|
{
|
||||||
std::lock_guard _(mutex_);
|
std::lock_guard _(mutex_);
|
||||||
@ -84,7 +91,14 @@ void scripting::initialize()
|
|||||||
|
|
||||||
for (const auto& callback : copy)
|
for (const auto& callback : copy)
|
||||||
{
|
{
|
||||||
callback(event->name, {});
|
try
|
||||||
|
{
|
||||||
|
callback(event->name, arguments);
|
||||||
|
}
|
||||||
|
catch (std::exception& e)
|
||||||
|
{
|
||||||
|
printf("Failed to handle event: %s\n", e.what());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -97,11 +111,37 @@ void scripting::load_scripts() const
|
|||||||
{
|
{
|
||||||
if (script.substr(script.find_last_of('.') + 1) == "chai")
|
if (script.substr(script.find_last_of('.') + 1) == "chai")
|
||||||
{
|
{
|
||||||
this->chai_->eval_file(script);
|
try
|
||||||
|
{
|
||||||
|
this->chai_->eval_file(script);
|
||||||
|
}
|
||||||
|
catch (std::exception& e)
|
||||||
|
{
|
||||||
|
printf("Failed to load script %s: %s\n", script.data(), e.what());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
chaiscript::Boxed_Value scripting::make_boxed(const game::native::VariableValue value)
|
||||||
|
{
|
||||||
|
if (value.type == game::native::SCRIPT_STRING)
|
||||||
|
{
|
||||||
|
const std::string string = game::native::SL_ConvertToString(value.u.stringValue);
|
||||||
|
return chaiscript::var(string);
|
||||||
|
}
|
||||||
|
else if (value.type == game::native::SCRIPT_FLOAT)
|
||||||
|
{
|
||||||
|
return chaiscript::var(value.u.floatValue);
|
||||||
|
}
|
||||||
|
else if (value.type == game::native::SCRIPT_INTEGER)
|
||||||
|
{
|
||||||
|
return chaiscript::var(value.u.intValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
void scripting::on_start(const std::function<void()>& callback)
|
void scripting::on_start(const std::function<void()>& callback)
|
||||||
{
|
{
|
||||||
std::lock_guard _(mutex_);
|
std::lock_guard _(mutex_);
|
||||||
|
@ -32,6 +32,8 @@ private:
|
|||||||
void initialize();
|
void initialize();
|
||||||
void load_scripts() const;
|
void load_scripts() const;
|
||||||
|
|
||||||
|
static chaiscript::Boxed_Value make_boxed(game::native::VariableValue value);
|
||||||
|
|
||||||
static utils::hook start_hook_;
|
static utils::hook start_hook_;
|
||||||
static utils::hook stop_hook_;
|
static utils::hook stop_hook_;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user