2017-05-31 09:45:12 -04:00
|
|
|
#include "STDInclude.hpp"
|
|
|
|
|
|
|
|
namespace Components
|
|
|
|
{
|
2017-07-03 09:07:47 -04:00
|
|
|
bool Scheduler::AsyncTerminate;
|
|
|
|
std::thread Scheduler::AsyncThread;
|
|
|
|
|
2017-05-31 09:45:12 -04:00
|
|
|
bool Scheduler::ReadyPassed = false;
|
|
|
|
Utils::Signal<Scheduler::Callback> Scheduler::ReadySignal;
|
|
|
|
Utils::Signal<Scheduler::Callback> Scheduler::ShutdownSignal;
|
|
|
|
|
|
|
|
Utils::Signal<Scheduler::Callback> Scheduler::FrameSignal;
|
|
|
|
Utils::Signal<Scheduler::Callback> Scheduler::FrameOnceSignal;
|
|
|
|
std::vector<Scheduler::DelayedSlot> Scheduler::DelayedSlots;
|
|
|
|
|
2017-07-03 09:07:47 -04:00
|
|
|
Utils::Signal<Scheduler::Callback> Scheduler::AsyncFrameSignal;
|
|
|
|
Utils::Signal<Scheduler::Callback> Scheduler::AsyncFrameOnceSignal;
|
|
|
|
|
2017-06-01 04:25:13 -04:00
|
|
|
void Scheduler::Once(Utils::Slot<Scheduler::Callback> callback, bool clientOnly)
|
2017-05-31 09:45:12 -04:00
|
|
|
{
|
2017-06-01 04:25:13 -04:00
|
|
|
if (clientOnly && (Dedicated::IsEnabled() || ZoneBuilder::IsEnabled())) return;
|
2017-05-31 09:45:12 -04:00
|
|
|
Scheduler::FrameOnceSignal.connect(callback);
|
|
|
|
}
|
|
|
|
|
2017-05-31 10:09:41 -04:00
|
|
|
void Scheduler::OnShutdown(Utils::Slot<Scheduler::Callback> callback)
|
|
|
|
{
|
|
|
|
Scheduler::ShutdownSignal.connect(callback);
|
|
|
|
}
|
|
|
|
|
2017-06-01 04:25:13 -04:00
|
|
|
void Scheduler::OnFrame(Utils::Slot<Scheduler::Callback> callback, bool clientOnly)
|
2017-05-31 09:45:12 -04:00
|
|
|
{
|
2017-06-01 04:25:13 -04:00
|
|
|
if (clientOnly && (Dedicated::IsEnabled() || ZoneBuilder::IsEnabled())) return;
|
2017-05-31 09:45:12 -04:00
|
|
|
Scheduler::FrameSignal.connect(callback);
|
|
|
|
}
|
|
|
|
|
2017-06-01 04:25:13 -04:00
|
|
|
void Scheduler::OnReady(Utils::Slot<Scheduler::Callback> callback, bool clientOnly)
|
2017-05-31 10:09:41 -04:00
|
|
|
{
|
2017-06-01 04:25:13 -04:00
|
|
|
if (clientOnly && (Dedicated::IsEnabled() || ZoneBuilder::IsEnabled())) return;
|
2017-05-31 10:09:41 -04:00
|
|
|
if (Scheduler::ReadyPassed) Scheduler::Once(callback);
|
|
|
|
else Scheduler::ReadySignal.connect(callback);
|
|
|
|
}
|
|
|
|
|
2017-05-31 09:45:12 -04:00
|
|
|
void Scheduler::ReadyHandler()
|
|
|
|
{
|
2017-05-31 10:09:41 -04:00
|
|
|
if (!FastFiles::Ready())
|
|
|
|
{
|
|
|
|
Scheduler::Once(Scheduler::ReadyHandler);
|
|
|
|
}
|
2017-05-31 09:45:12 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Scheduler::ReadyPassed = true;
|
|
|
|
Scheduler::ReadySignal();
|
|
|
|
Scheduler::ReadySignal.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scheduler::FrameHandler()
|
|
|
|
{
|
|
|
|
Scheduler::DelaySignal();
|
|
|
|
Scheduler::FrameSignal();
|
|
|
|
|
|
|
|
Utils::Signal<Scheduler::Callback> copy(Scheduler::FrameOnceSignal);
|
|
|
|
Scheduler::FrameOnceSignal.clear();
|
|
|
|
copy();
|
|
|
|
}
|
|
|
|
|
2017-06-01 04:25:13 -04:00
|
|
|
void Scheduler::OnDelay(Utils::Slot<Scheduler::Callback> callback, std::chrono::nanoseconds delay, bool clientOnly)
|
2017-05-31 09:45:12 -04:00
|
|
|
{
|
2017-06-01 04:25:13 -04:00
|
|
|
if (clientOnly && (Dedicated::IsEnabled() || ZoneBuilder::IsEnabled())) return;
|
|
|
|
|
2017-05-31 09:45:12 -04:00
|
|
|
Scheduler::DelayedSlot slot;
|
|
|
|
slot.callback = callback;
|
|
|
|
slot.delay = delay;
|
|
|
|
|
|
|
|
Scheduler::DelayedSlots.push_back(slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scheduler::DelaySignal()
|
|
|
|
{
|
|
|
|
Utils::Signal<Scheduler::Callback> signal;
|
|
|
|
|
|
|
|
for (auto i = Scheduler::DelayedSlots.begin(); i != Scheduler::DelayedSlots.end();)
|
|
|
|
{
|
|
|
|
if (i->interval.elapsed(i->delay))
|
|
|
|
{
|
|
|
|
signal.connect(i->callback);
|
|
|
|
i = Scheduler::DelayedSlots.erase(i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
signal();
|
|
|
|
}
|
|
|
|
|
2017-05-31 10:09:41 -04:00
|
|
|
void Scheduler::ShutdownStub(int num)
|
|
|
|
{
|
|
|
|
Scheduler::ShutdownSignal();
|
|
|
|
Utils::Hook::Call<void(int)>(0x46B370)(num);
|
|
|
|
}
|
|
|
|
|
2017-07-03 09:07:47 -04:00
|
|
|
void Scheduler::OnFrameAsync(Utils::Slot<Scheduler::Callback> callback)
|
|
|
|
{
|
|
|
|
Scheduler::AsyncFrameSignal.connect(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scheduler::OnceAsync(Utils::Slot<Scheduler::Callback> callback)
|
|
|
|
{
|
|
|
|
Scheduler::AsyncFrameOnceSignal.connect(callback);
|
|
|
|
}
|
2017-05-31 10:09:41 -04:00
|
|
|
|
2017-05-31 09:45:12 -04:00
|
|
|
Scheduler::Scheduler()
|
|
|
|
{
|
|
|
|
Scheduler::ReadyPassed = false;
|
|
|
|
Scheduler::Once(Scheduler::ReadyHandler);
|
2017-05-31 10:09:41 -04:00
|
|
|
|
|
|
|
Utils::Hook(0x4D697A, Scheduler::ShutdownStub, HOOK_CALL).install()->quick();
|
2017-07-03 09:07:47 -04:00
|
|
|
|
2017-07-03 09:22:15 -04:00
|
|
|
if (!Loader::PerformUnitTests())
|
2017-07-03 09:07:47 -04:00
|
|
|
{
|
2017-07-03 09:22:15 -04:00
|
|
|
Scheduler::AsyncTerminate = false;
|
|
|
|
Scheduler::AsyncThread = std::thread([]()
|
2017-07-03 09:07:47 -04:00
|
|
|
{
|
2017-07-03 09:22:15 -04:00
|
|
|
while (!Scheduler::AsyncTerminate)
|
|
|
|
{
|
|
|
|
Scheduler::AsyncFrameSignal();
|
2017-07-03 09:07:47 -04:00
|
|
|
|
2017-07-03 09:22:15 -04:00
|
|
|
Utils::Signal<Scheduler::Callback> copy(Scheduler::AsyncFrameOnceSignal);
|
|
|
|
Scheduler::AsyncFrameOnceSignal.clear();
|
|
|
|
copy();
|
2017-07-03 09:07:47 -04:00
|
|
|
|
2017-07-03 09:22:15 -04:00
|
|
|
std::this_thread::sleep_for(16ms);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-05-31 09:45:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Scheduler::~Scheduler()
|
|
|
|
{
|
2017-05-31 10:09:41 -04:00
|
|
|
Scheduler::ReadySignal.clear();
|
|
|
|
Scheduler::ShutdownSignal.clear();
|
|
|
|
|
2017-05-31 09:45:12 -04:00
|
|
|
Scheduler::FrameSignal.clear();
|
2017-05-31 10:09:41 -04:00
|
|
|
Scheduler::FrameOnceSignal.clear();
|
|
|
|
Scheduler::DelayedSlots.clear();
|
|
|
|
|
2017-07-03 09:07:47 -04:00
|
|
|
Scheduler::AsyncFrameSignal.clear();
|
|
|
|
Scheduler::AsyncFrameOnceSignal.clear();
|
|
|
|
|
2017-05-31 10:09:41 -04:00
|
|
|
Scheduler::ReadyPassed = false;
|
2017-05-31 09:45:12 -04:00
|
|
|
}
|
2017-07-03 09:07:47 -04:00
|
|
|
|
|
|
|
void Scheduler::preDestroy()
|
|
|
|
{
|
|
|
|
Scheduler::AsyncTerminate = true;
|
|
|
|
if (Scheduler::AsyncThread.joinable())
|
|
|
|
{
|
|
|
|
Scheduler::AsyncThread.join();
|
|
|
|
}
|
|
|
|
}
|
2017-05-31 09:45:12 -04:00
|
|
|
}
|