CPU.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. local events = require("Engine.events")
  2. local coreg = require("Engine.coreg")
  3. return function(config) --A function that creates a new CPU peripheral.
  4. local EventStack = {}
  5. local Instant = false
  6. local RawPull = false
  7. local sleepTimer
  8. local devkit = {}
  9. function devkit.triggerEvent(name,...)
  10. if Instant or RawPull then
  11. Instant, RawPull = false, false
  12. coreg:resumeCoroutine(true,name,...)
  13. else
  14. table.insert(EventStack,{name,...})
  15. end
  16. end
  17. events:register("love:update", function(...) --Update event
  18. if not sleepTimer then devkit.triggerEvent("update",...) end
  19. end)
  20. events:register("love:update",function(dt) --Sleep Timer
  21. if sleepTimer then
  22. sleepTimer = sleepTimer-dt
  23. if sleepTimer <=0 then
  24. sleepTimer = nil
  25. coreg:resumeCoroutine(true)
  26. end
  27. end
  28. end)
  29. local function Verify(value,name,etype,allowNil)
  30. if type(value) ~= etype then
  31. if allowNil then
  32. error(name.." should be a "..etype.." or a nil, provided: "..type(value),3)
  33. else
  34. error(name.." should be a "..etype..", provided: "..type(value),3)
  35. end
  36. end
  37. if etype == "number" then
  38. return math.floor(value)
  39. end
  40. end
  41. --The api starts here--
  42. local CPU, yCPU = {}, {}
  43. function yCPU.pullEvent()
  44. if #EventStack == 0 then
  45. Instant = true
  46. return 2 --To quit the coroutine resuming loop
  47. else
  48. local lastEvent = EventStack[1]
  49. local newEventStack = {}
  50. for k,v in ipairs(EventStack) do
  51. if k > 1 then table.insert(newEventStack,v) end --Remove the last event.
  52. end
  53. EventStack = newEventStack
  54. return true, unpack(lastEvent)
  55. end
  56. end
  57. function yCPU.rawPullEvent()
  58. RawPull = true
  59. return 2
  60. end
  61. function CPU.triggerEvent(name,...)
  62. Verify(name,"The event name","string")
  63. table.insert(EventStack,{name,...})
  64. end
  65. function CPU.clearEStack()
  66. EventStack = {}
  67. end
  68. function CPU.getHostOS()
  69. return love.system.getOS()
  70. end
  71. function CPU.isMobile()
  72. return (love.system.getOS() == "Android" or love.system.getOS() == "iOS")
  73. end
  74. function CPU.clipboard(text)
  75. if text then
  76. love.system.setClipboardText(tostring(text))
  77. else
  78. return love.system.getClipboardText()
  79. end
  80. end
  81. function CPU.clearClipboard()
  82. love.system.setClipboardText()
  83. end
  84. function yCPU.sleep(t)
  85. if type(t) ~= "number" then return false, "Time must be a number, provided: "..t end
  86. if t < 0 then return false, "Time must be a positive number" end
  87. sleepTimer = t
  88. return 2
  89. end
  90. function yCPU.shutdown()
  91. love.event.quit()
  92. return 2 --I don't want the coroutine to resume while rebooting
  93. end
  94. function yCPU.reboot(hard)
  95. if hard then
  96. love.event.quit( "restart" )
  97. else
  98. events:trigger("love:reboot") --Tell main.lua that we have to soft reboot.
  99. end
  100. return 2 --I don't want the coroutine to resume while rebooting
  101. end
  102. function CPU.openURL(url)
  103. Verify(url,"URL","string")
  104. love.system.openURL(url)
  105. end
  106. function CPU.openAppData(tar)
  107. local tar = tar or "/"
  108. if tar:sub(0,1) ~= "/" then tar = "/"..tar end
  109. love.system.openURL("file://"..love.filesystem.getSaveDirectory()..tar)
  110. end
  111. function CPU.getSaveDirectory()
  112. return love.filesystem.getSaveDirectory()
  113. end
  114. --Prints to developer console.
  115. function CPU.cprint(...)
  116. print(...)
  117. end
  118. CPU.getFPS = love.timer.getFPS
  119. devkit.CPU = CPU
  120. devkit.yCPU = yCPU
  121. return CPU, yCPU, devkit
  122. end