Programist135

Programist135 API

Nov 21st, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.32 KB | None | 0 0
  1. --[[
  2.        
  3.     ********
  4.  
  5.     Библиотека для программ Programist135® Soft™
  6.  
  7.     Создатель: Programist135
  8.  
  9.     ********
  10.  
  11. --]]
  12.  
  13. -- грузим OpenOS'овские либы короч =)
  14. local component = require("component")
  15. local unicode = require("unicode")
  16. local event = require("event")
  17. local serialization = require("serialization")
  18.  
  19. -- подключаем нужные компоненты.
  20. local gpu = component.gpu
  21.  
  22. -- а вот это костыль. Чтобы если люди не имеют этих компонентов, то зачем тратить ресы чтобы запустить мою либу? ;)
  23. if component.isAvailable("modem") then
  24.   modem = component.modem
  25. end
  26. if component.isAvailable("data") then
  27.   data_card = component.data
  28. end
  29.  
  30. -- получаем разрешение экрана.
  31. local resX, resY = gpu.getResolution()
  32.  
  33. -- создаём таблицу, в которой будут храниться функции
  34. p135 = {}
  35.  
  36. -- корректор стартовых координат. Взято из библиотеки пользователя ECS. ФУНКЦИЯ НЕДОСТУПНА ИЗ REQUIRE!!!
  37. local function correctStartCoords(xStart, yStart, xWindowSize, yWindowSize)
  38.   if xStart == "auto" then
  39.     xStart = math.floor(resX/2 - xWindowSize/2)
  40.   end
  41.   if yStart == "auto" then
  42.     yStart = math.ceil(resY/2 - yWindowSize/2)
  43.   end
  44.   return xStart, yStart
  45. end
  46.  
  47. -- создаёт/переделывает шкалу прогресса.
  48. function p135.setProgressBar(x, y, width, currentProgress, backColor, lineColor)
  49.   if width > resX then
  50.     error("index out of bounds")
  51.   end
  52.   gpu.setBackground(backColor)
  53.   gpu.fill(x, y, width, 1, " ")
  54.   if currentProgress > width then
  55.     error("index out of bounds")
  56.   else
  57.     gpu.setBackground(lineColor)
  58.     gpu.fill(x, y, currentProgress, 1, " ")
  59.   end
  60. end
  61.  
  62. -- а теперь окошки.
  63. function p135.addWindow(x, y, width, height, windowColor, topBarColor, topBarlabel)
  64.   local x, y = correctStartCoords(x, y, width, height)
  65.   -- само окошко
  66.   gpu.setBackground(windowColor)
  67.   gpu.fill(x, y, width, height, " ")
  68.   -- топбар
  69.   gpu.setBackground(topBarColor)
  70.   gpu.fill(x, y, width, 1, " ")
  71.   -- надпись на топбаре
  72.   gpu.setForeground(0xFFFFFF)
  73.   gpu.set(x, y, topBarlabel)
  74. end
  75.  
  76. -- текст. Можно и gpu.set'ом обойтись, но тут есть выбор цвета.
  77. function p135.addText(x, y, backColor, txtColor, text)
  78.   gpu.setForeground(txtColor)
  79.   gpu.setBackground(backColor)
  80.   gpu.set(x, y, text)
  81. end
  82.  
  83. -- возвращает всю систему к стандартным параметрам.
  84. function p135.reset()
  85.   gpu.setBackground(0x000000)
  86.   gpu.setForeground(0xFFFFFF)
  87.   require("term").clear()
  88. end
  89.  
  90. -- генератор рандомных символов!
  91. function p135.generate(length)
  92.   local key = ""
  93.   for i = 1, length do
  94.       local number = math.random(65, 126)
  95.       local char = unicode.char(number)
  96.       key = key..char
  97.   end
  98.   return key
  99. end
  100.  
  101. -- безопасность превыше всего.
  102. -- а тут функция может вызвать ошибку, если компонент не подключён. Поэтому сначала:
  103. if component.isAvailable("modem") and component.isAvailable("data") then
  104.   function p135.sendSecuredMessage(...)
  105.     local port = 36813
  106.     modem.open(port)
  107.     local messages = table.pack(...)
  108.     messages = serialization.serialize(messages)
  109.     messages = data_card.encode64(messages)
  110.     modem.broadcast(port, messages)
  111.     modem.close(port)
  112.   end
  113.  
  114.   function p135.getSecuredMessage()
  115.     local port = 36813
  116.     modem.open(port)
  117.     local _, _, _, _, _, messages = event.pull("modem_message")
  118.     local messages = data_card.decode64(messages)
  119.     messages = serialization.unserialize(messages)
  120.     modem.close(port)
  121.     return table.unpack(messages)
  122.   end
  123. end
  124. -- вот такие вот дела ;)
  125.  
  126. -- а это чтобы получить текущее состояние библиотеки, насколько она работает.
  127. function p135.getState()
  128.   if component.isAvailable("modem") and not component.isAvailable("data") then
  129.     print("Модем подключен, но не подключена дата-карта. Функции защищенных сообщений недоступны.")
  130.   elseif component.isAvailable("data") and not component.isAvailable("modem") then
  131.     print("Дата-карта подключена, но не подключен модем. Функции защищенных сообщений недоступны.")
  132.   elseif not component.isAvailable("modem") and not component.isAvailable("data") then
  133.     print("Не подключены ни дата-карта, ни модем. Функции защищенных сообщений недоступны.")
  134.   elseif component.isAvailable("modem") and component.isAvailable("data") then
  135.     print("Дата-карта подключена.")
  136.     print("Модем подключен.")
  137.     print("Вы можете использовать библиотеку на все сто! :)")
  138.   end
  139. end
  140.  
  141. -- всё. В конце выдаём таблицу с функциями после запроса.
  142. return p135
Advertisement
Add Comment
Please, Sign In to add comment