2022-03-24 10:44:20 +00:00
|
|
|
#include <std_include.hpp>
|
2022-04-25 15:26:51 +01:00
|
|
|
|
2019-09-24 11:46:47 +02:00
|
|
|
#include "context.hpp"
|
|
|
|
|
2019-09-27 22:35:57 +02:00
|
|
|
namespace game::scripting
|
2019-09-24 11:46:47 +02:00
|
|
|
{
|
2019-09-27 22:35:57 +02:00
|
|
|
scheduler::scheduler(context* context) : context_(context)
|
2019-09-24 11:46:47 +02:00
|
|
|
{
|
2019-09-27 22:35:57 +02:00
|
|
|
const auto chai = this->context_->get_chai();
|
2019-09-24 11:46:47 +02:00
|
|
|
|
2019-09-27 22:35:57 +02:00
|
|
|
chai->add(chaiscript::user_type<task_handle>(), "_task_handle");
|
|
|
|
chai->add(chaiscript::constructor<task_handle()>(), "_task_handle");
|
|
|
|
chai->add(chaiscript::constructor<task_handle(const task_handle&)>(), "_task_handle");
|
2019-09-24 11:46:47 +02:00
|
|
|
|
2019-09-27 22:35:57 +02:00
|
|
|
chai->add(chaiscript::fun([](task_handle& lhs, const task_handle& rhs) -> task_handle&
|
|
|
|
{
|
|
|
|
return lhs = rhs;
|
|
|
|
}), "=");
|
2019-09-24 11:46:47 +02:00
|
|
|
|
2019-09-27 22:35:57 +02:00
|
|
|
chai->add(chaiscript::fun(
|
2022-04-25 15:26:51 +01:00
|
|
|
[this](const std::function<void()>& callback, const long long milliseconds) -> task_handle
|
|
|
|
{
|
|
|
|
return this->add(callback, milliseconds, true);
|
|
|
|
}), "setTimeout");
|
2019-09-24 11:46:47 +02:00
|
|
|
|
2019-09-27 22:35:57 +02:00
|
|
|
chai->add(chaiscript::fun(
|
2022-04-25 15:26:51 +01:00
|
|
|
[this](const std::function<void()>& callback, const long long milliseconds) -> task_handle
|
|
|
|
{
|
|
|
|
return this->add(callback, milliseconds, false);
|
|
|
|
}), "setInterval");
|
2019-09-24 11:46:47 +02:00
|
|
|
|
2019-09-27 22:35:57 +02:00
|
|
|
const auto clear = [this](const task_handle& handle)
|
|
|
|
{
|
|
|
|
this->remove(handle);
|
|
|
|
};
|
2019-09-24 11:46:47 +02:00
|
|
|
|
2019-09-27 22:35:57 +02:00
|
|
|
chai->add(chaiscript::fun(clear), "clear");
|
|
|
|
chai->add(chaiscript::fun(clear), "clearTimeout");
|
|
|
|
chai->add(chaiscript::fun(clear), "clearInterval");
|
|
|
|
}
|
2019-09-24 11:46:47 +02:00
|
|
|
|
2019-09-27 22:35:57 +02:00
|
|
|
void scheduler::run_frame()
|
|
|
|
{
|
2022-04-25 15:26:51 +01:00
|
|
|
this->tasks_.access([&](task_list& tasks)
|
2019-09-24 11:46:47 +02:00
|
|
|
{
|
2022-04-25 15:26:51 +01:00
|
|
|
for (auto i = tasks.begin(); i != tasks.end();)
|
2019-09-24 11:46:47 +02:00
|
|
|
{
|
2022-04-25 15:26:51 +01:00
|
|
|
const auto now = std::chrono::steady_clock::now();
|
|
|
|
const auto diff = now - i->last_execution;
|
|
|
|
|
|
|
|
if (diff < i->delay)
|
2019-09-24 11:46:47 +02:00
|
|
|
{
|
2022-04-25 15:26:51 +01:00
|
|
|
++i;
|
|
|
|
continue;
|
2019-09-24 11:46:47 +02:00
|
|
|
}
|
2019-09-27 22:35:57 +02:00
|
|
|
|
2022-04-25 15:26:51 +01:00
|
|
|
i->last_execution = now;
|
|
|
|
|
|
|
|
if (i->is_volatile)
|
|
|
|
{
|
|
|
|
i = tasks.erase(i);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
i->callback();
|
|
|
|
++i;
|
|
|
|
}
|
2019-09-24 11:46:47 +02:00
|
|
|
}
|
2022-04-25 15:26:51 +01:00
|
|
|
});
|
2019-09-27 22:35:57 +02:00
|
|
|
}
|
2019-09-24 11:46:47 +02:00
|
|
|
|
2019-09-27 22:35:57 +02:00
|
|
|
void scheduler::clear()
|
|
|
|
{
|
2022-04-25 15:26:51 +01:00
|
|
|
this->tasks_.access([&](task_list& tasks)
|
|
|
|
{
|
|
|
|
tasks.clear();
|
|
|
|
});
|
2019-09-27 22:35:57 +02:00
|
|
|
}
|
2019-09-24 11:46:47 +02:00
|
|
|
|
2019-09-27 22:35:57 +02:00
|
|
|
task_handle scheduler::add(const std::function<void()>& callback, const long long milliseconds,
|
|
|
|
const bool is_volatile)
|
|
|
|
{
|
|
|
|
return this->add(callback, std::chrono::milliseconds(milliseconds), is_volatile);
|
|
|
|
}
|
2019-09-24 11:46:47 +02:00
|
|
|
|
2019-09-27 22:35:57 +02:00
|
|
|
task_handle scheduler::add(const std::function<void()>& callback, const std::chrono::milliseconds delay,
|
|
|
|
const bool is_volatile)
|
|
|
|
{
|
|
|
|
task task;
|
|
|
|
task.is_volatile = is_volatile;
|
|
|
|
task.callback = callback;
|
|
|
|
task.delay = delay;
|
|
|
|
task.last_execution = std::chrono::steady_clock::now();
|
|
|
|
task.id = ++this->current_task_id_;
|
2019-09-24 11:46:47 +02:00
|
|
|
|
2022-04-25 15:26:51 +01:00
|
|
|
this->tasks_.access([&task](task_list& tasks)
|
|
|
|
{
|
|
|
|
tasks.emplace_back(std::move(task));
|
|
|
|
});
|
2019-09-24 11:46:47 +02:00
|
|
|
|
2019-09-27 22:35:57 +02:00
|
|
|
return {task.id};
|
|
|
|
}
|
2019-09-24 11:46:47 +02:00
|
|
|
|
2019-09-27 22:35:57 +02:00
|
|
|
void scheduler::remove(const task_handle& handle)
|
|
|
|
{
|
2022-04-25 15:26:51 +01:00
|
|
|
this->tasks_.access([&](task_list& tasks)
|
2019-09-24 11:46:47 +02:00
|
|
|
{
|
2022-04-25 15:26:51 +01:00
|
|
|
for (auto i = tasks.begin(); i != tasks.end();)
|
2019-09-24 11:46:47 +02:00
|
|
|
{
|
2022-04-25 15:26:51 +01:00
|
|
|
if (i->id == handle.id)
|
|
|
|
{
|
|
|
|
i = tasks.erase(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
++i;
|
2019-09-24 11:46:47 +02:00
|
|
|
}
|
2022-04-25 15:26:51 +01:00
|
|
|
});
|
2019-09-24 11:46:47 +02:00
|
|
|
}
|
|
|
|
}
|