-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfiber.hpp
More file actions
96 lines (79 loc) · 1.55 KB
/
fiber.hpp
File metadata and controls
96 lines (79 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#pragma once
#include <Includes.h>
#include <singeton.hpp>
namespace fiber
{
using script_func_t = void(*)();
class GameFiber_Task
{
public:
explicit GameFiber_Task(HMODULE hmod, script_func_t func) :
m_hmodule(hmod),
m_function(func)
{ }
~GameFiber_Task() noexcept
{
if (m_script_fiber)
{
DeleteFiber(m_script_fiber);
}
}
HMODULE get_hmodule()
{
return m_hmodule;
}
script_func_t get_function()
{
return m_function;
}
void on_tick()
{
if (GetTickCount64() < m_wake_at)
return;
if (!m_main_fiber)
{
m_main_fiber = IsThreadAFiber() ? GetCurrentFiber() : ConvertThreadToFiber(nullptr);
}
if (m_script_fiber)
{
current_fiber_script = this;
SwitchToFiber(m_script_fiber);
current_fiber_script = nullptr;
}
else
{
m_script_fiber = CreateFiber(0, [](PVOID param)
{
auto this_script = static_cast<GameFiber_Task*>(param);
while (true)
{
this_script->m_function();
}
}, this);
}
}
void wait(std::uint32_t time)
{
m_wake_at = GetTickCount64() + time;
SwitchToFiber(m_main_fiber);
}
inline static GameFiber_Task* get_current_script()
{
return current_fiber_script;
}
private:
HMODULE m_hmodule{};
script_func_t m_function{};
std::uint32_t m_wake_at{};
void* m_script_fiber{};
void* m_main_fiber{};
inline static GameFiber_Task* current_fiber_script{};
};
}
class Fiber : public Singleton<Fiber>
{
public:
std::unique_ptr<fiber::GameFiber_Task> GameFiber;
void Call();
void OnNativeThread();
};