· 7 years ago · Oct 24, 2018, 12:18 AM
1function _OnActivated()
2 EnablePrimaryMouseButtonEvents(true)
3 --Task_G5 = NewTask(fn_MacroMap, "RightAndLeft")
4 Task_G5 = NewTask(fn_RightAndLeft)
5 Task_G4 = NewTask(fn_MacroMap, "UpAndDown")
6end
7
8function _OnEvent(event, arg, family)
9 if (event == "G_PRESSED" and arg == 5) then
10 Task_G5.Start()
11 elseif (event == "G_RELEASED" and arg == 5) then
12 ReleaseAllKeys(Task_G5._Co)
13 Task_G5.Stop()
14 elseif (event == "G_PRESSED" and arg == 4) then
15 Task_G4.Start()
16 elseif (event == "G_RELEASED" and arg == 4) then
17 ReleaseAllKeys(Task_G4._Co)
18 Task_G4.Stop()
19 end
20end
21
22function fn_MacroMap(macro_name)
23 while true do
24 --PlayMacro(macro_name)
25 fn_RightAndLeft()
26 TaskSleep(5)
27 end
28end
29
30function fn_RightAndLeft()
31 while true do
32 Sleep(100)
33 PressKey("d")
34 Sleep(100)
35 ReleaseKey("d")
36 Sleep(100)
37 PressKey("a")
38 Sleep(100)
39 ReleaseKey("a")
40 Sleep(5)
41 end
42end
43
44CO_KeysPressed = {} -- Keys pressed from within a coroutine. CO-KeysPressed[ct][key]
45
46PressKey_hook = PressKey
47ReleaseKey_hook = ReleaseKey
48
49function PressKey(k)
50 local co = coroutine.running()
51
52 if co then -- if PressKey() was called within a coroutine/Task
53 if not CO_KeysPressed[co] then -- If coroutine not found, make a table for it
54 CO_KeysPressed[co] = {}
55 end
56 CO_KeysPressed[co][k] = true -- set the key to true, meaning it is pressed
57 end
58 PressKey_hook(k)
59end
60
61function ReleaseKey(k)
62 ReleaseKey_hook(k)
63
64 for key, value in pairs(CO_KeysPressed) do
65 value[k] = nil
66 end
67end
68
69function ReleaseAllKeys(co)
70 if CO_KeysPressed[co] then
71 for key, value in pairs(CO_KeysPressed[co]) do
72 ReleaseKey(key)
73 end
74 end
75end
76
77------------------------------------------------------------------------------------------------------
78-- Task Class
79------------------------------------------------------------------------------------------------------
80-- NewTask(func, ...) returns a new object, and creates the coroutine with given func and variables
81-- .ChangeVars(...) Change vars to new vars for next .Execute()
82-- .SetRepeat(boolean) Sets repeat status to true or false. If repeat, when the task ends, it restarts.
83-- .GetRepeat() returns if Repeat is set to true or false.
84-- .Create() creates the task, but is not running yet.
85-- .Start() creates if not already created and at the start, then sets the task to Run.
86-- .Stop() stops and finishes the task. It cannot be resumed.
87-- .IsCreated() checks if the Task's coroutine exists (self._Co ~= nil)
88-- .IsRunning() checks if the Task's is created, if it is running, and not dead (finished running)
89-- .IsAtStart() checks if the Task is created but has not yet run.
90-- .IsAtEnd() checks if the Task has completed, and still exists.
91-- .GetStatus() returns the status of the coroutine, or nil if it doesn't exist
92-- .Pause() stops running the task, but does not destroy it.
93-- .Resume() resumes the task where it left off.
94-- .Execute() run the Task with the given variables. This should be run every event to keep the task running
95-- .Destroy() stops and removes the Task. It remove from the TaskHandler and nilled.
96-- .Remove() calls .Destroy()
97-- TaskSleep(delay) This will yield within a Task function with the given delay. Execution will pick up where it left off after the delay.
98------------------------------------------------------------------------------------------------------
99
100-------------------------------------------------
101-- The following is for polling. Do not alter.
102-------------------------------------------------
103_StartUpParameters = {
104 PollDevice = "mouse",
105 PollDelay = 10,
106 AutoTaskSleep = true,
107}
108function _PreEvent() end
109function _PostEvent()
110 _TaskHandler.Execute()
111end
112function OnEvent(event, arg, family)
113 if event == "PROFILE_ACTIVATED" then
114 _TaskHandler = InitTaskHandler()
115 Poll = InitPolling(_StartUpParameters.PollDelay, _StartUpParameters.PollDevice, _PreEvent, _PostEvent)
116 end
117 Poll.Execute(event, arg, family)
118end
119
120----------------------------
121-- Polling Class
122----------------------------
123function InitPolling(PollDelay, PollDevice, PreOnEventFunc, PostOnEventFunc)
124 local self = {
125 PollDelay = PollDelay,
126 PollDevice = PollDevice,
127 PreOnEventFunc = PreOnEventFunc,
128 PostOnEventFunc = PostOnEventFunc,
129 Sleep = Sleep_hook,
130 }
131 local function CreateEvent() SetMKeyState(1, self.PollDevice) end
132 local function OnEvent(event, arg, family)
133 if self.PreOnEventFunc then self.PreOnEventFunc() end
134 _OnEvent(event, arg, family)
135 if self.PostOnEventFunc then self.PostOnEventFunc() end
136 end
137 function self.Execute(event, arg, family)
138 if event == "PROFILE_ACTIVATED" then
139 if _OnActivated then _OnActivated(event, arg, family) end
140 OnEvent(event, arg, family)
141 CreateEvent() -- initiates the first polling event
142 elseif event == "M_RELEASED" and family == self.PollDevice then
143 OnEvent("POLLING", 0, self.PollDevice)
144 CreateEvent()
145 self.Sleep(self.PollDelay)
146 elseif event == "M_PRESSED" and family == self.PollDevice then
147 OnEvent("POLLING", 0, self.PollDevice)
148 self.Sleep(self.PollDelay)
149 elseif event == "PROFILE_DEACTIVATED" then
150 if _OnDeactivated then _OnDeactivated(event, arg, family) end
151 else
152 OnEvent(event, arg, family)
153 end
154 end
155 function self.SetPreOnEventFunc(func) self.PreOnEventFunc = func end
156 function self.SetPostOnEventFunc(func) self.PosOnEventFunc = func end
157 return self
158end
159
160------------------------
161-- Task Class
162------------------------
163function TaskSleep(delay) return coroutine.yield(delay) end
164function NewTask(func, ...)
165 local self = {
166 _Func = func,
167 _Running = false,
168 _Co = nil,
169 _ResumeRunningTime = -1,
170 _AtStart = false,
171 _Repeat = false,
172 _Vars = nil,
173 _TH = _TaskHandler or nil,
174 _PressedKeys = {}
175 }
176 function self.ChangeVars(...) self._Vars = { ... } end
177 function self.SetRepeat(r) self._Repeat = r end
178 function self.GetRepeat() return self._Repeat end
179 function self.Create()
180 self._Running = false
181 self._Co = coroutine.create(self._Func)
182 self._AtStart = true
183 end
184 function self.Start()
185 if not self.IsAtStart() or not self.IsCreated() then
186 self.Create()
187 end
188 self._Running = true
189 end
190 function self.Stop() self._Running = false; self._Co = nil end
191 function self.GetStatus()
192 if self._Co then return coroutine.status(self._Co)
193 else return nil end
194 end
195 function self.IsAtStart() return self._AtStart end
196 function self.IsAtEnd() return self.IsDead() end
197 function self.IsCreated()
198 if self._Co then return true
199 else return false end
200 end
201 function self.IsDead()
202 if self._Co and self.GetStatus() == "dead" then return true
203 else return false end
204 end
205 function self.IsRunning()
206 if self.IsCreated() and self._Running and not self.IsDead() then return true
207 else return false end
208 end
209 function self.IsReady()
210 if self._Running and self.IsCreated() and not self.IsDead()
211 and self._ResumeRunningTime <= GetRunningTime() then
212 return true
213 else return false end
214 end
215 function self.Pause() self._Running = false end
216 function self.Resume() self._Running = true end
217 function self.Execute()
218 if self.GetRepeat() and self.IsDead() and self._Running then self.Start() end
219 if self.IsReady() then
220 local status, delay = coroutine.resume(self._Co, unpack(self._Vars))
221 self._AtStart = false
222 if delay then self._ResumeRunningTime = delay + GetRunningTime()
223 else self._ResumeRunningTime = -1 end
224 return status
225 end
226 end
227 function self.Destroy()
228 if self._TH then self._TH.RemoveTask(self) end
229 self = nil
230 return nil
231 end
232 function self.Remove() self.Destroy() end
233 self.ChangeVars(...)
234 self.Create()
235 if self._TH then self._TH.AddTask(self) end
236 return self
237end
238
239--------------------------
240-- TaskHandler
241--------------------------
242function InitTaskHandler()
243 local self = { _TaskList = {}, }
244 function self.AddTask(Task) self._TaskList[Task] = true end
245 function self.RemoveTask(Task) self._TaskList[Task] = nil end
246 function self.Execute()
247 for k,v in pairs(self._TaskList) do k.Execute() end
248 end
249 return self
250end
251coroutine.running_hook = coroutine.running
252function coroutine.running()
253 local v = coroutine.running_hook()
254 return v
255end
256Sleep_hook = Sleep
257function Sleep(d)
258 if _StartUpParameters.AutoTaskSleep and coroutine.running() then return TaskSleep(d)
259 else return Sleep_hook(d) end
260end