t7x/src/client/component/scheduler.cpp

185 lines
3.8 KiB
C++
Raw Normal View History

2022-05-21 06:04:08 -04:00
#include <std_include.hpp>
#include "loader/component_loader.hpp"
#include "scheduler.hpp"
#include <cassert>
#include <utils/hook.hpp>
#include <utils/concurrency.hpp>
#include <utils/thread.hpp>
namespace scheduler
{
namespace
{
struct task
{
std::function<bool()> handler{};
std::chrono::milliseconds interval{};
std::chrono::high_resolution_clock::time_point last_call{};
};
using task_list = std::vector<task>;
class task_pipeline
{
public:
void add(task&& task)
{
new_callbacks_.access([&task](task_list& tasks)
{
tasks.emplace_back(std::move(task));
});
}
void execute()
{
callbacks_.access([&](task_list& tasks)
{
this->merge_callbacks();
for (auto i = tasks.begin(); i != tasks.end();)
{
const auto now = std::chrono::high_resolution_clock::now();
const auto diff = now - i->last_call;
if (diff < i->interval)
{
++i;
continue;
}
i->last_call = now;
const auto res = i->handler();
if (res == cond_end)
{
i = tasks.erase(i);
}
else
{
++i;
}
}
});
}
private:
utils::concurrency::container<task_list> new_callbacks_;
utils::concurrency::container<task_list, std::recursive_mutex> callbacks_;
void merge_callbacks()
{
callbacks_.access([&](task_list& tasks)
{
new_callbacks_.access([&](task_list& new_tasks)
{
2022-05-23 11:57:45 -04:00
tasks.insert(tasks.end(), std::move_iterator<task_list::iterator>(new_tasks.begin()),
std::move_iterator<task_list::iterator>(new_tasks.end()));
2022-05-21 06:04:08 -04:00
new_tasks = {};
});
});
}
};
volatile bool kill = false;
std::thread thread;
task_pipeline pipelines[pipeline::count];
2022-09-17 08:51:34 -04:00
2022-05-21 06:04:08 -04:00
utils::hook::detour r_end_frame_hook;
utils::hook::detour g_run_frame_hook;
utils::hook::detour main_frame_hook;
void execute(const pipeline type)
{
assert(type >= 0 && type < pipeline::count);
pipelines[type].execute();
}
void r_end_frame_stub()
{
execute(pipeline::renderer);
r_end_frame_hook.invoke<void>();
}
void server_frame_stub()
{
g_run_frame_hook.invoke<void>();
execute(pipeline::server);
}
void main_frame_stub()
{
main_frame_hook.invoke<void>();
execute(pipeline::main);
}
}
void schedule(const std::function<bool()>& callback, const pipeline type,
2022-05-23 11:57:45 -04:00
const std::chrono::milliseconds delay)
2022-05-21 06:04:08 -04:00
{
assert(type >= 0 && type < pipeline::count);
task task;
task.handler = callback;
task.interval = delay;
task.last_call = std::chrono::high_resolution_clock::now();
pipelines[type].add(std::move(task));
}
void loop(const std::function<void()>& callback, const pipeline type,
2022-05-23 11:57:45 -04:00
const std::chrono::milliseconds delay)
2022-05-21 06:04:08 -04:00
{
schedule([callback]()
{
callback();
return cond_continue;
}, type, delay);
}
void once(const std::function<void()>& callback, const pipeline type,
2022-05-23 11:57:45 -04:00
const std::chrono::milliseconds delay)
2022-05-21 06:04:08 -04:00
{
schedule([callback]()
{
callback();
return cond_end;
}, type, delay);
}
class component final : public component_interface
{
public:
void post_load() override
2022-05-21 06:04:08 -04:00
{
thread = utils::thread::create_named_thread("Async Scheduler", []()
{
while (!kill)
{
execute(pipeline::async);
std::this_thread::sleep_for(10ms);
}
});
}
2022-09-17 08:51:34 -04:00
void post_unpack() override
{
2022-09-17 09:11:44 -04:00
r_end_frame_hook.create(0x142273560_g, r_end_frame_stub); // some func called before R_EndFrame, maybe SND_EndFrame?
g_run_frame_hook.create(0x14065C360_g, server_frame_stub); // GlassSv_Update
main_frame_hook.create(0x1420F9860_g, main_frame_stub); // Com_Frame_Try_Block_Function
2022-09-17 08:51:34 -04:00
}
2022-05-21 06:04:08 -04:00
void pre_destroy() override
{
kill = true;
if (thread.joinable())
{
thread.join();
}
}
};
}
REGISTER_COMPONENT(scheduler::component)