init.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. --Games runtime API
  2. local rt = {}
  3. function rt.loadResources()
  4. local scripts = fs.getDirectoryItems("C:/Runtime/Resources/")
  5. for id, name in ipairs(scripts) do
  6. scripts[id] = fs.load("C:/Runtime/Resources/"..name)
  7. end
  8. return scripts
  9. end
  10. function rt.loadGlobals()
  11. local scripts = fs.getDirectoryItems("C:/Runtime/Globals/")
  12. for id, name in ipairs(scripts) do
  13. scripts[id] = fs.load("C:/Runtime/Globals/"..name)
  14. end
  15. return scripts
  16. end
  17. function rt.loadGame()
  18. local glob = _FreshGlobals()
  19. glob._G = glob --Magic ;)
  20. local resources = rt.loadResources()
  21. local globals = rt.loadGlobals()
  22. --Execute the resources
  23. for i=1, #resources do
  24. resources[i](glob)
  25. end
  26. --Load the lua code
  27. local chunk, cerr = loadstring(glob._GameCode,"")
  28. if not chunk then
  29. cerr = tostring(cerr)
  30. if cerr:sub(1,12) == '[string ""]:' then
  31. cerr = cerr:sub(13,-1)
  32. end
  33. return false, "Compile ERR :"..cerr
  34. end
  35. --Set the sandbox
  36. setfenv(chunk, glob)
  37. --Create the coroutine
  38. local co = coroutine.create(chunk)
  39. --Execute the globals constructors
  40. for i=1, #globals do
  41. globals[i](glob,co)
  42. end
  43. return glob, co, chunk
  44. end
  45. function rt.resetEnvironment()
  46. pal() palt() cam() clip()
  47. clearEStack()
  48. clearMatrixStack()
  49. colorPalette() --Reset the color palette.
  50. patternFill()
  51. TC.setInput(false)
  52. Audio.stop()
  53. end
  54. function rt.runGame(glob,co,...)
  55. --Enable the touch controls on mobile
  56. if isMobile() then TC.setInput(true) end
  57. textinput(not isMobile()) --And disable the touch keyboard
  58. --The event loop coroutine, created in the 01_Lua.lua script
  59. local evco = glob.__evco; glob.__evco = nil
  60. local pause = glob.pause --Backup the pause function
  61. local lastArgs = {...}
  62. while true do
  63. if coroutine.status(co) == "dead" then
  64. if evco then
  65. co = evco; evco = false --So it doesn't get placed again.
  66. else
  67. break
  68. end
  69. end
  70. local args = {coroutine.resume(co,unpack(lastArgs))}
  71. --Program crashed
  72. if not args[1] then
  73. local err = tostring(args[2])
  74. if err:sub(1,12) == '[string ""]:' then err = err:sub(13,-1) end
  75. rt.resetEnvironment(); print("")
  76. return false, "ERR :"..err
  77. end
  78. --Nope, it's alive :-)
  79. if args[2] then
  80. --Special command for exiting the game
  81. if args[2] == "RUN:exit" then
  82. rt.resetEnvironment(); print("")
  83. if args[3] then
  84. return false, tostring(args[3])
  85. else
  86. return true
  87. end
  88. --Capture the keypressed event
  89. elseif args[2] == "CPU:pullEvent" or args[2] == "CPU:rawPullEvent" then
  90. lastArgs = {coroutine.yield(select(2,unpack(args)))}
  91. local event, a,b,c,d,e,f = select(2,unpack(lastArgs))
  92. --Check for the escape key
  93. if event == "keypressed" and a == "escape" then
  94. rt.resetEnvironment(); print(""); return true
  95. elseif event == "keypressed" and a == "return" then
  96. pause()
  97. end
  98. --Hack the sleep command
  99. elseif args[2] == "CPU:sleep" then
  100. local timer = args[3] --The sleep timer
  101. if type(timer) ~= "number" or timer < 0 then
  102. lastArgs = {coroutine.yield(select(2,unpack(args)))} --Let the original sleep blame the programmer.
  103. else
  104. while timer > 0 do
  105. local event, a,b,c,d,e,f = rawPullEvent()
  106. if event == "update" then
  107. timer = timer - a
  108. elseif event == "keypressed" and a == "escape" then
  109. rt.resetEnvironment(); print(""); return true
  110. elseif event == "keypressed" and a == "return" then
  111. pause()
  112. else
  113. triggerEvent(event, a,b,c,d,e,f)
  114. end
  115. end
  116. lastArgs = {true} --Sleep ran successfully
  117. end
  118. --Hack the flip command
  119. elseif args[2] == "CPU:flip" then
  120. _hasFlipped() --Clear the flip flag
  121. while true do
  122. local event, a,b,c,d,e,f = rawPullEvent()
  123. if event == "keypressed" and a == "escape" then
  124. rt.resetEnvironment(); print(""); return true
  125. elseif event == "keypressed" and a == "return" then
  126. pause()
  127. end
  128. triggerEvent(event, a,b,c,d,e,f)
  129. if _hasFlipped() then break end
  130. end
  131. lastArgs = {true} --Sleep ran successfully
  132. --Run the rest of the commands normally
  133. else
  134. lastArgs = {coroutine.yield(select(2,unpack(args)))}
  135. end
  136. end
  137. end
  138. rt.resetEnvironment()
  139. print("")
  140. return true
  141. end
  142. return rt