2021-09-06 18:40:37 -04:00
|
|
|
#include <std_include.hpp>
|
2021-04-23 14:57:21 -04:00
|
|
|
#include "loader/component_loader.hpp"
|
|
|
|
|
2021-04-19 18:56:11 -04:00
|
|
|
#include "scheduler.hpp"
|
|
|
|
#include "game/game.hpp"
|
2021-04-23 14:57:21 -04:00
|
|
|
|
2021-09-07 19:21:20 -04:00
|
|
|
#include <utils/thread.hpp>
|
2021-04-19 18:56:11 -04:00
|
|
|
#include <utils/hook.hpp>
|
|
|
|
#include <utils/concurrency.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)
|
|
|
|
{
|
|
|
|
tasks.insert(tasks.end(), std::move_iterator<task_list::iterator>(new_tasks.begin()), std::move_iterator<task_list::iterator>(new_tasks.end()));
|
|
|
|
new_tasks = {};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
volatile bool kill = false;
|
|
|
|
std::thread thread;
|
|
|
|
task_pipeline pipelines[pipeline::count];
|
|
|
|
utils::hook::detour r_end_frame_hook;
|
2021-04-22 21:46:11 -04:00
|
|
|
utils::hook::detour g_run_frame_hook;
|
2021-04-24 10:22:26 -04:00
|
|
|
utils::hook::detour main_frame_hook;
|
2022-04-28 16:41:29 -04:00
|
|
|
utils::hook::detour hks_frame_hook;
|
2021-04-19 18:56:11 -04:00
|
|
|
|
|
|
|
void execute(const pipeline type)
|
|
|
|
{
|
|
|
|
assert(type >= 0 && type < pipeline::count);
|
|
|
|
pipelines[type].execute();
|
|
|
|
}
|
|
|
|
|
2021-12-19 12:38:45 -05:00
|
|
|
void r_end_frame_stub()
|
2021-04-19 18:56:11 -04:00
|
|
|
{
|
|
|
|
execute(pipeline::renderer);
|
2021-12-19 12:38:45 -05:00
|
|
|
r_end_frame_hook.invoke<void>();
|
2021-04-19 18:56:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void server_frame_stub()
|
|
|
|
{
|
2021-04-22 21:46:11 -04:00
|
|
|
g_run_frame_hook.invoke<void>();
|
2021-04-19 18:56:11 -04:00
|
|
|
execute(pipeline::server);
|
|
|
|
}
|
2021-04-24 10:22:26 -04:00
|
|
|
|
|
|
|
void main_frame_stub()
|
|
|
|
{
|
|
|
|
main_frame_hook.invoke<void>();
|
|
|
|
execute(pipeline::main);
|
|
|
|
}
|
2022-04-28 16:41:29 -04:00
|
|
|
|
|
|
|
void hks_frame_stub()
|
|
|
|
{
|
|
|
|
const auto state = *game::hks::lua_state;
|
|
|
|
if (state)
|
|
|
|
{
|
|
|
|
execute(pipeline::lui);
|
|
|
|
}
|
|
|
|
hks_frame_hook.invoke<void>();
|
|
|
|
}
|
2021-04-19 18:56:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void schedule(const std::function<bool()>& callback, const pipeline type,
|
|
|
|
const std::chrono::milliseconds delay)
|
|
|
|
{
|
|
|
|
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,
|
|
|
|
const std::chrono::milliseconds delay)
|
|
|
|
{
|
|
|
|
schedule([callback]()
|
|
|
|
{
|
|
|
|
callback();
|
|
|
|
return cond_continue;
|
|
|
|
}, type, delay);
|
|
|
|
}
|
|
|
|
|
|
|
|
void once(const std::function<void()>& callback, const pipeline type,
|
|
|
|
const std::chrono::milliseconds delay)
|
|
|
|
{
|
|
|
|
schedule([callback]()
|
|
|
|
{
|
|
|
|
callback();
|
|
|
|
return cond_end;
|
|
|
|
}, type, delay);
|
|
|
|
}
|
|
|
|
|
2021-09-06 18:40:37 -04:00
|
|
|
void on_game_initialized(const std::function<void()>& callback, const pipeline type,
|
|
|
|
const std::chrono::milliseconds delay)
|
|
|
|
{
|
|
|
|
schedule([=]()
|
|
|
|
{
|
|
|
|
if (game::Sys_IsDatabaseReady2())
|
|
|
|
{
|
|
|
|
once(callback, type, delay);
|
|
|
|
return cond_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cond_continue;
|
|
|
|
}, pipeline::main);
|
|
|
|
}
|
|
|
|
|
2021-04-23 14:57:21 -04:00
|
|
|
class component final : public component_interface
|
2021-04-19 18:56:11 -04:00
|
|
|
{
|
2021-04-23 14:57:21 -04:00
|
|
|
public:
|
2021-09-07 19:21:20 -04:00
|
|
|
void post_start() override
|
2021-04-19 18:56:11 -04:00
|
|
|
{
|
2021-09-07 19:21:20 -04:00
|
|
|
thread = utils::thread::create_named_thread("Async Scheduler", []()
|
2021-04-19 18:56:11 -04:00
|
|
|
{
|
2021-04-23 14:57:21 -04:00
|
|
|
while (!kill)
|
|
|
|
{
|
|
|
|
execute(pipeline::async);
|
|
|
|
std::this_thread::sleep_for(10ms);
|
|
|
|
}
|
|
|
|
});
|
2021-09-07 19:21:20 -04:00
|
|
|
}
|
2021-04-19 18:56:11 -04:00
|
|
|
|
2021-09-07 19:21:20 -04:00
|
|
|
void post_unpack() override
|
|
|
|
{
|
2022-03-18 17:02:44 -04:00
|
|
|
r_end_frame_hook.create(0x14076D7B0, scheduler::r_end_frame_stub);
|
|
|
|
g_run_frame_hook.create(0x1404CB030, scheduler::server_frame_stub);
|
|
|
|
main_frame_hook.create(0x140417FA0, scheduler::main_frame_stub);
|
2022-04-28 16:41:29 -04:00
|
|
|
hks_frame_hook.create(0x140327880, scheduler::hks_frame_stub);
|
2021-04-23 14:57:21 -04:00
|
|
|
}
|
2021-09-07 19:21:20 -04:00
|
|
|
|
|
|
|
void pre_destroy() override
|
|
|
|
{
|
|
|
|
kill = true;
|
|
|
|
if (thread.joinable())
|
|
|
|
{
|
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
}
|
2021-04-23 14:57:21 -04:00
|
|
|
};
|
2021-04-19 18:56:11 -04:00
|
|
|
}
|
2021-04-23 14:57:21 -04:00
|
|
|
|
|
|
|
REGISTER_COMPONENT(scheduler::component)
|