Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.17 KB | None | 0 0
  1. function _OnActivated()
  2. EnablePrimaryMouseButtonEvents(true)
  3. Task_Autoclick = NewTask(fn_AutoclickLoop)
  4. end
  5.  
  6. function _OnEvent(event, arg, family)
  7. if event == "MOUSE_BUTTON_PRESSED" and arg == 1 then
  8. Task_Autoclick.Start()
  9. elseif event == "MOUSE_BUTTON_RELEASED" and arg == 1 then
  10. Task_Autoclick.Stop()
  11. ReleaseMouseButton(1) -- to ensure the LMB doesn't get left down
  12. end
  13. end
  14.  
  15. function fn_AutoclickLoop()
  16. while true do
  17. PressMouseButton(1)
  18. TaskSleep( Random(17, 31) )
  19. ReleaseMouseButton(1)
  20. TaskSleep( Random(15, 36) )
  21. end
  22. end
  23.  
  24. function Random(a, b)
  25. if not _SEED then
  26. _SEED = GetRunningTime()
  27. math.randomseed(_SEED)
  28. end
  29. return math.random(a, b)
  30. end
  31.  
  32. ------------------------------------------------------------------------------------------------------
  33. -- Task Class
  34. ------------------------------------------------------------------------------------------------------
  35. -- NewTask(func, ...) returns a new object, and creates the coroutine with given func and variables
  36. -- .ChangeVars(...) Change vars to new vars for next .Execute()
  37. -- .SetRepeat(boolean) Sets repeat status to true or false. If repeat, when the task ends, it restarts.
  38. -- .GetRepeat() returns if Repeat is set to true or false.
  39. -- .Create() creates the task, but is not running yet.
  40. -- .Start() creates if not already created and at the start, then sets the task to Run.
  41. -- .Stop() stops and finishes the task. It cannot be resumed.
  42. -- .IsCreated() checks if the Task's coroutine exists (self._Co ~= nil)
  43. -- .IsRunning() checks if the Task's is created, if it is running, and not dead (finished running)
  44. -- .IsAtStart() checks if the Task is created but has not yet run.
  45. -- .IsAtEnd() checks if the Task has completed, and still exists.
  46. -- .GetStatus() returns the status of the coroutine, or nil if it doesn't exist
  47. -- .Pause() stops running the task, but does not destroy it.
  48. -- .Resume() resumes the task where it left off.
  49. -- .Execute() run the Task with the given variables. This should be run every event to keep the task running
  50. -- .Destroy() stops and removes the Task. It remove from the TaskHandler and nilled.
  51. -- .Remove() calls .Destroy()
  52. -- TaskSleep(delay) This will yield within a Task function with the given delay. Execution will pick up where it left off after the delay.
  53. ------------------------------------------------------------------------------------------------------
  54.  
  55. -------------------------------------------------
  56. -- The following is for polling. Do not alter.
  57. -------------------------------------------------
  58. _StartUpParameters = {
  59. PollDevice = "mouse",
  60. PollDelay = 10,
  61. AutoTaskSleep = false,
  62. }
  63. function _PreEvent() end
  64. function _PostEvent()
  65. _TaskHandler.Execute()
  66. end
  67. function OnEvent(event, arg, family)
  68. if event == "PROFILE_ACTIVATED" then
  69. _TaskHandler = InitTaskHandler()
  70. Poll = InitPolling(_StartUpParameters.PollDelay, _StartUpParameters.PollDevice, _PreEvent, _PostEvent)
  71. end
  72. Poll.Execute(event, arg, family)
  73. end
  74.  
  75. ----------------------------
  76. -- Polling Class
  77. ----------------------------
  78. function InitPolling(PollDelay, PollDevice, PreOnEventFunc, PostOnEventFunc)
  79. local self = {
  80. PollDelay = PollDelay,
  81. PollDevice = PollDevice,
  82. PreOnEventFunc = PreOnEventFunc,
  83. PostOnEventFunc = PostOnEventFunc,
  84. Sleep = Sleep_hook,
  85. }
  86. local function CreateEvent() SetMKeyState(1, self.PollDevice) end
  87. local function OnEvent(event, arg, family)
  88. if self.PreOnEventFunc then self.PreOnEventFunc() end
  89. _OnEvent(event, arg, family)
  90. if self.PostOnEventFunc then self.PostOnEventFunc() end
  91. end
  92. function self.Execute(event, arg, family)
  93. if event == "PROFILE_ACTIVATED" then
  94. if _OnActivated then _OnActivated(event, arg, family) end
  95. OnEvent(event, arg, family)
  96. CreateEvent() -- initiates the first polling event
  97. elseif event == "M_RELEASED" and family == self.PollDevice then
  98. OnEvent("POLLING", 0, self.PollDevice)
  99. CreateEvent()
  100. self.Sleep(self.PollDelay)
  101. elseif event == "M_PRESSED" and family == self.PollDevice then
  102. OnEvent("POLLING", 0, self.PollDevice)
  103. self.Sleep(self.PollDelay)
  104. elseif event == "PROFILE_DEACTIVATED" then
  105. if _OnDeactivated then _OnDeactivated(event, arg, family) end
  106. else
  107. OnEvent(event, arg, family)
  108. end
  109. end
  110. function self.SetPreOnEventFunc(func) self.PreOnEventFunc = func end
  111. function self.SetPostOnEventFunc(func) self.PosOnEventFunc = func end
  112. return self
  113. end
  114.  
  115. ------------------------
  116. -- Task Class
  117. ------------------------
  118. function TaskSleep(delay) return coroutine.yield(delay) end
  119. function NewTask(func, ...)
  120. local self = {
  121. _Func = func,
  122. _Running = false,
  123. _Co = nil,
  124. _ResumeRunningTime = -1,
  125. _AtStart = false,
  126. _Repeat = false,
  127. _Vars = nil,
  128. _TH = _TaskHandler or nil,
  129. }
  130. function self.ChangeVars(...) self._Vars = { ... } end
  131. function self.SetRepeat(r) self._Repeat = r end
  132. function self.GetRepeat() return self._Repeat end
  133. function self.Create()
  134. self._ResumeRunningTime = -1
  135. self._Running = false
  136. self._Co = coroutine.create(self._Func)
  137. self._AtStart = true
  138. end
  139. function self.Start()
  140. if not self.IsAtStart() or not self.IsCreated() then
  141. self.Create()
  142. end
  143. self._Running = true
  144. end
  145. function self.Stop() self._Running = false; self._Co = nil end
  146. function self.GetStatus()
  147. if self._Co then return coroutine.status(self._Co)
  148. else return nil end
  149. end
  150. function self.IsAtStart() return self._AtStart end
  151. function self.IsAtEnd() return self.IsDead() end
  152. function self.IsCreated()
  153. if self._Co then return true
  154. else return false end
  155. end
  156. function self.IsDead()
  157. if self._Co and self.GetStatus() == "dead" then return true
  158. else return false end
  159. end
  160. function self.IsRunning()
  161. if self.IsCreated() and self._Running and not self.IsDead() then return true
  162. else return false end
  163. end
  164. function self.IsReady()
  165. if self._Running and self.IsCreated() and not self.IsDead()
  166. and self._ResumeRunningTime <= GetRunningTime() then
  167. return true
  168. else return false end
  169. end
  170. function self.Pause() self._Running = false end
  171. function self.Resume() self._Running = true end
  172. function self.Execute()
  173. if self.GetRepeat() and self.IsDead() and self._Running then self.Start() end
  174. if self.IsReady() then
  175. local status, delay = coroutine.resume(self._Co, unpack(self._Vars))
  176. self._AtStart = false
  177. if delay then self._ResumeRunningTime = delay + GetRunningTime()
  178. else self._ResumeRunningTime = -1 end
  179. return status
  180. end
  181. end
  182. function self.Destroy()
  183. if self._TH then self._TH.RemoveTask(self) end
  184. self = nil
  185. return nil
  186. end
  187. function self.Remove() self.Destroy() end
  188. self.ChangeVars(...)
  189. self.Create()
  190. if self._TH then self._TH.AddTask(self) end
  191. return self
  192. end
  193.  
  194. --------------------------
  195. -- TaskHandler
  196. --------------------------
  197. function InitTaskHandler()
  198. local self = { _TaskList = {}, }
  199. function self.AddTask(Task) self._TaskList[Task] = true end
  200. function self.RemoveTask(Task) self._TaskList[Task] = nil end
  201. function self.Execute()
  202. for k,v in pairs(self._TaskList) do k.Execute() end
  203. end
  204. return self
  205. end
  206. coroutine.running_hook = coroutine.running
  207. function coroutine.running()
  208. local v = coroutine.running_hook()
  209. return v
  210. end
  211. Sleep_hook = Sleep
  212. function Sleep(d)
  213. if _StartUpParameters.AutoTaskSleep and coroutine.running() then return TaskSleep(d)
  214. else return Sleep_hook(d) end
  215. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement