-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathscript.cpp
More file actions
105 lines (89 loc) · 1.81 KB
/
Copy pathscript.cpp
File metadata and controls
105 lines (89 loc) · 1.81 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
97
98
99
100
101
102
103
104
105
#include "script.hpp"
namespace big
{
script::script(const func_t func, const std::string& name, const bool toggleable, const std::optional<std::size_t> stack_size) :
script(func, stack_size)
{
m_name = name;
m_toggleable = toggleable;
}
script::script(const func_t func, const std::optional<std::size_t> stack_size) :
m_enabled(true),
m_toggleable(false),
m_script_fiber(nullptr),
m_main_fiber(nullptr),
m_func(func),
m_done(false)
{
m_script_fiber = CreateFiber(
stack_size.has_value() ? stack_size.value() : 0,
[](void* param) {
auto this_script = static_cast<script*>(param);
this_script->fiber_func();
},
this);
}
script::~script()
{
if (m_script_fiber)
DeleteFiber(m_script_fiber);
}
const char* script::name() const
{
return m_name.data();
}
bool script::is_enabled() const
{
return m_enabled;
}
void script::set_enabled(const bool toggle)
{
if (m_toggleable)
m_enabled = toggle;
}
bool* script::toggle_ptr()
{
return &m_enabled;
}
bool script::is_toggleable() const
{
return m_toggleable;
}
bool script::is_done() const
{
return m_done;
}
void script::tick()
{
m_main_fiber = GetCurrentFiber();
if (!m_wake_time.has_value() || m_wake_time.value() <= std::chrono::high_resolution_clock::now())
{
SwitchToFiber(m_script_fiber);
}
}
void script::yield(std::optional<std::chrono::high_resolution_clock::duration> time)
{
if (time.has_value())
{
m_wake_time = std::chrono::high_resolution_clock::now() + time.value();
}
else
{
m_wake_time = std::nullopt;
}
SwitchToFiber(m_main_fiber);
}
script* script::get_current()
{
return static_cast<script*>(GetFiberData());
}
void script::fiber_func()
{
m_func();
m_done = true;
while (true)
{
yield();
}
}
}