-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcbtools.lua
More file actions
executable file
·190 lines (171 loc) · 3.89 KB
/
cbtools.lua
File metadata and controls
executable file
·190 lines (171 loc) · 3.89 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
--[[
Created by Dimitri Barronmore
https://github.com/DimitriBarronmore/cyf-cbtools
--]]
local utils = {}
local function test_if_function(f)
if (type(f) == "function") or ((getmetatable(f) or {}).__call) then
return true
else
return false
end
end
utils.waitFor = function(time, seconds)
if time == nil then
error("no duration given", 2)
elseif type(time) ~= "number" then
error("duration must be a number", 2)
end
local timer, complete
if seconds == true then
timer = Time.time
else
timer = 0
end
repeat
if seconds == true then
if Time.time - timer >= time then
complete = true
else
coroutine.yield()
end
else
timer = timer + 1
if timer > time then
complete = true
else
coroutine.yield()
end
end
until complete
end
utils.waitUntil = function(cond)
if cond == nil then error("no condition given", 2)
elseif not test_if_function(cond) then
error("the given condition must be a function", 2)
end
while true do
local res = cond()
if res == true then
break
else
coroutine.yield()
end
end
end
utils.waitComplete = function(...)
while true do
local at_least_one_incomplete = false
local argument_list = {...}
for _, routine in ipairs(argument_list) do
if type(routine) ~= "thread" then
error("attempt to wait for non-coroutine")
end
if coroutine.status(routine) ~= "dead" then
at_least_one_incomplete = true
break
end
end
if at_least_one_incomplete then
coroutine.yield()
else
break
end
end
end
utils.doFor = function(iterations, func)
if iterations == nil then error("no count given", 2)
elseif type(iterations) ~= "number" then error("count argument must be a number", 2) end
if func == nil then error("no runner function given", 2)
elseif not test_if_function(func) then
error("the second argument must be a function", 2)
end
local t = 0
while t < iterations do
t = t + 1
func()
coroutine.yield()
end
end
utils.doUntil = function(condition, func)
if condition== nil then
error("no condition provided", 2)
elseif not test_if_function(condition) then
error("provided condition must be a function", 2)
end
if func == nil then
error("must provide a function to be looped", 2)
elseif not test_if_function(func) then
error("the second argument must be a function", 2)
end
while true do
local res = condition()
if res == true then
break
else
func()
coroutine.yield()
end
end
end
local update_queue = {}
local name_queue = {}
utils.queue = function(func, name, ...)
if not test_if_function(func) then
error("attempt to queue object of type " .. type(func))
end
if name == nil then
for k,v in pairs(_ENV) do
if v == func then
name = k
end
end
end
name = name or "<unknown>"
local co = coroutine.create(func)
if ... then
res, err = coroutine.resume(co, ...)
if res == false then
error("error while queueing coroutine " .. name .. ": \n" .. err, 2)
end
end
table.insert(update_queue, co)
table.insert(name_queue, name)
return co
end
utils.update = function()
local num, to_del = #update_queue, {}
for i = 1, num do
local co = update_queue[i]
res, err = coroutine.resume(co)
if res == false then
error("error in queued coroutine " .. name_queue[i] .. ": \n" .. err, 2)
end
if coroutine.status(co) == "dead" then
table.insert(to_del, i)
end
end
for i = #to_del, 1, -1 do
table.remove(update_queue, to_del[i])
table.remove(name_queue, to_del[i])
end
end
local looping_mt = {
__call = function(t, ...)
if coroutine.status(t.routine) == "dead" then
t.routine = coroutine.create(t.method)
end
local res, err = coroutine.resume(t.routine, ...)
if res == false then
error("error with looping coroutine: \n" .. err, 2)
end
end
}
utils.createLooping = function(func)
local functab = {}
functab.method = func
functab.routine = coroutine.create( func )
setmetatable(functab, looping_mt)
return functab
end
return utils