Advertisement
Reactor_Games

Untitled

Dec 30th, 2019
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.12 KB | None | 0 0
  1. function proxy(componentType) return component.proxy(component.list(componentType)()) end
  2. function clear() gpu.setBackground(0x0a0a0a) gpu.fill(1, 1, 60, 19, " ") end
  3. local eeprom = proxy("eeprom")
  4. gpu = proxy("gpu")
  5. internet = proxy("internet")
  6.  
  7. gpu.bind(component.list("screen")(), true)
  8. gpu.setResolution(60, 19)
  9. gpu.setForeground(0x68f029)
  10. clear()
  11. gpu.set(23, 9, "Инициализация...")
  12.  
  13. local running = false
  14.  
  15. local users = {computer.users()}
  16. admins = {}
  17.  
  18. for admin = 1, #admins do
  19. computer.addUser(admins[admin])
  20. admins[admins[admin]], admins[admin] = true, nil
  21. end
  22. for user = 1, #users do
  23. if not admins[users[user]] then
  24. computer.removeUser(users[user])
  25. end
  26. end
  27. if eeprom.getData() == "true" then
  28. dev = true
  29. else
  30. dev = false
  31. end
  32.  
  33. local function help()
  34. clear()
  35. gpu.set(18, 8, "CTRL+A — режим разработки")
  36. gpu.set(18, 9, "CTRL+S — запуск программы")
  37. gpu.set(16, 10, "CTRL+D — обновление программы")
  38. gpu.set(12, 11, "CTRL+ALT+C — принудительная остановка")
  39. end
  40.  
  41. local function customError(err)
  42. clear()
  43. gpu.setForeground(0xff0000)
  44. gpu.set(23, 1, "Фатальная ошибка!")
  45. gpu.setForeground(0x68f029)
  46.  
  47. local lines, y = {}
  48.  
  49. for line in err:gmatch("[^\r\n]+") do
  50. line = line:gsub("\t", "")
  51. if unicode.len(line) > 60 then
  52. for i = 1, math.ceil(unicode.len(line) / 60) do
  53. local before = i * 60
  54. lines[#lines + 1] = unicode.sub(line, before - 59, before)
  55. end
  56. else
  57. lines[#lines + 1] = line
  58. end
  59. end
  60.  
  61. local y = 11 - #lines / 2
  62.  
  63. for i = 1, #lines do
  64. gpu.set(math.floor(31 - unicode.len(lines[i]) / 2), y, lines[i])
  65. y = y + 1
  66. end
  67. end
  68.  
  69. local function findFilesystem()
  70. for address in component.list("filesystem") do
  71. if address ~= computer.tmpAddress() and not component.invoke(address, "isReadOnly") then
  72. filesystem = component.proxy(address)
  73. return true
  74. end
  75. end
  76.  
  77. if not filesystem then
  78. customError("Файловая система не найдена!")
  79. end
  80. end
  81.  
  82. local function execute(data)
  83. local chunk, err = load(data, "=shop.lua", "t")
  84.  
  85. if not chunk and err then
  86. customError(err)
  87. else
  88. local success, err = xpcall(chunk, debug.traceback)
  89. if not success and err then
  90. customError(err)
  91. end
  92. end
  93. end
  94.  
  95. local function develop()
  96. clear()
  97. if not dev then
  98. gpu.set(16, 9, "Включение режима разработки...")
  99. eeprom.setData("true")
  100. dev = true
  101. else
  102. gpu.set(15, 9, "Выключение режима разработки...")
  103. eeprom.setData("false")
  104. dev = false
  105. end
  106. help()
  107. end
  108.  
  109. local function update()
  110. clear()
  111. gpu.set(20, 9, "Обновление программы...")
  112. write("/shop.lua", "w", request("https://raw.githubusercontent.com/BrightYC/RipMarket/master/terminal.lua"))
  113. end
  114.  
  115. local function run(forcibly)
  116. if not filesystem.exists("/shop.lua") then
  117. update()
  118. end
  119.  
  120. if filesystem.exists("/shop.lua") and not forcibly then
  121. clear()
  122. gpu.set(25, 9, "Загрузка...")
  123. running = true
  124. execute(read("/shop.lua"))
  125. running = false
  126. end
  127. end
  128.  
  129. local pullSignal = computer.pullSignal
  130. computer.pullSignal = function(...)
  131. local signal = {pullSignal(...)}
  132.  
  133. if signal[1] == "key_down" then
  134. if signal[4] == 29 then
  135. isControlDown = true
  136. elseif signal[4] == 42 then
  137. ifShiftDown = true
  138. elseif signal[4] == 56 then
  139. isAltDown = true
  140. elseif isControlDown and admins[signal[5]] and filesystem then
  141. if running then
  142. if signal[4] == 46 and isAltDown then
  143. error("interrupted")
  144. end
  145. else
  146. if signal[4] == 30 then
  147. develop()
  148. elseif signal[4] == 31 then
  149. run()
  150. elseif signal[4] == 32 then
  151. update()
  152. run()
  153. end
  154. end
  155. end
  156. elseif signal[1] == "key_up" then
  157. if signal[4] == 29 then
  158. isControlDown = false
  159. elseif signal[4] == 42 then
  160. ifShiftDown = false
  161. elseif signal[4] == 56 then
  162. isAltDown = false
  163. end
  164. end
  165.  
  166. return table.unpack(signal)
  167. end
  168.  
  169. function read(path)
  170. local handle = filesystem.open(path, "r")
  171. local data = ""
  172.  
  173. while true do
  174. local chunk = filesystem.read(handle, 2048)
  175.  
  176. if chunk then
  177. data = data .. chunk
  178. else
  179. break
  180. end
  181. end
  182.  
  183. filesystem.close(handle)
  184. return data
  185. end
  186.  
  187. function write(path, mode, data)
  188. local handle = filesystem.open(path, mode)
  189. filesystem.write(handle, data)
  190. filesystem.close(handle)
  191. end
  192.  
  193. function request(path)
  194. local handle, data, chunk = internet.request(path), ""
  195.  
  196. while true do
  197. chunk = handle.read(math.huge)
  198.  
  199. if chunk then
  200. data = data .. chunk
  201. else
  202. break
  203. end
  204. end
  205.  
  206. handle.close()
  207. return data
  208. end
  209.  
  210. function sleep(timeout)
  211. local deadline = computer.uptime() + (timeout or math.huge)
  212.  
  213. repeat
  214. computer.pullSignal(deadline - computer.uptime())
  215. until computer.uptime() >= deadline
  216. end
  217.  
  218. findFilesystem()
  219. if filesystem then
  220. help()
  221. end
  222.  
  223. while true do
  224. computer.pullSignal(math.huge)
  225. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement