Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- ********
- Библиотека для программ Programist135® Soft™
- Создатель: Programist135
- ********
- --]]
- -- грузим OpenOS'овские либы короч =)
- local component = require("component")
- local unicode = require("unicode")
- local event = require("event")
- local serialization = require("serialization")
- -- подключаем нужные компоненты.
- local gpu = component.gpu
- -- а вот это костыль. Чтобы если люди не имеют этих компонентов, то зачем тратить ресы чтобы запустить мою либу? ;)
- if component.isAvailable("modem") then
- modem = component.modem
- end
- if component.isAvailable("data") then
- data_card = component.data
- end
- -- получаем разрешение экрана.
- local resX, resY = gpu.getResolution()
- -- создаём таблицу, в которой будут храниться функции
- p135 = {}
- -- корректор стартовых координат. Взято из библиотеки пользователя ECS. ФУНКЦИЯ НЕДОСТУПНА ИЗ REQUIRE!!!
- local function correctStartCoords(xStart, yStart, xWindowSize, yWindowSize)
- if xStart == "auto" then
- xStart = math.floor(resX/2 - xWindowSize/2)
- end
- if yStart == "auto" then
- yStart = math.ceil(resY/2 - yWindowSize/2)
- end
- return xStart, yStart
- end
- -- создаёт/переделывает шкалу прогресса.
- function p135.setProgressBar(x, y, width, currentProgress, backColor, lineColor)
- if width > resX then
- error("index out of bounds")
- end
- gpu.setBackground(backColor)
- gpu.fill(x, y, width, 1, " ")
- if currentProgress > width then
- error("index out of bounds")
- else
- gpu.setBackground(lineColor)
- gpu.fill(x, y, currentProgress, 1, " ")
- end
- end
- -- а теперь окошки.
- function p135.addWindow(x, y, width, height, windowColor, topBarColor, topBarlabel)
- local x, y = correctStartCoords(x, y, width, height)
- -- само окошко
- gpu.setBackground(windowColor)
- gpu.fill(x, y, width, height, " ")
- -- топбар
- gpu.setBackground(topBarColor)
- gpu.fill(x, y, width, 1, " ")
- -- надпись на топбаре
- gpu.setForeground(0xFFFFFF)
- gpu.set(x, y, topBarlabel)
- end
- -- текст. Можно и gpu.set'ом обойтись, но тут есть выбор цвета.
- function p135.addText(x, y, backColor, txtColor, text)
- gpu.setForeground(txtColor)
- gpu.setBackground(backColor)
- gpu.set(x, y, text)
- end
- -- возвращает всю систему к стандартным параметрам.
- function p135.reset()
- gpu.setBackground(0x000000)
- gpu.setForeground(0xFFFFFF)
- require("term").clear()
- end
- -- генератор рандомных символов!
- function p135.generate(length)
- local key = ""
- for i = 1, length do
- local number = math.random(65, 126)
- local char = unicode.char(number)
- key = key..char
- end
- return key
- end
- -- безопасность превыше всего.
- -- а тут функция может вызвать ошибку, если компонент не подключён. Поэтому сначала:
- if component.isAvailable("modem") and component.isAvailable("data") then
- function p135.sendSecuredMessage(...)
- local port = 36813
- modem.open(port)
- local messages = table.pack(...)
- messages = serialization.serialize(messages)
- messages = data_card.encode64(messages)
- modem.broadcast(port, messages)
- modem.close(port)
- end
- function p135.getSecuredMessage()
- local port = 36813
- modem.open(port)
- local _, _, _, _, _, messages = event.pull("modem_message")
- local messages = data_card.decode64(messages)
- messages = serialization.unserialize(messages)
- modem.close(port)
- return table.unpack(messages)
- end
- end
- -- вот такие вот дела ;)
- -- а это чтобы получить текущее состояние библиотеки, насколько она работает.
- function p135.getState()
- if component.isAvailable("modem") and not component.isAvailable("data") then
- print("Модем подключен, но не подключена дата-карта. Функции защищенных сообщений недоступны.")
- elseif component.isAvailable("data") and not component.isAvailable("modem") then
- print("Дата-карта подключена, но не подключен модем. Функции защищенных сообщений недоступны.")
- elseif not component.isAvailable("modem") and not component.isAvailable("data") then
- print("Не подключены ни дата-карта, ни модем. Функции защищенных сообщений недоступны.")
- elseif component.isAvailable("modem") and component.isAvailable("data") then
- print("Дата-карта подключена.")
- print("Модем подключен.")
- print("Вы можете использовать библиотеку на все сто! :)")
- end
- end
- -- всё. В конце выдаём таблицу с функциями после запроса.
- return p135
Advertisement
Add Comment
Please, Sign In to add comment