Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Coz Core functions and utilities API
- -- Author: theonlycozzy
- -- July 31 2013
- local button = {}
- -- INITIALATION FUNCTIONS
- function getVersion()
- local build = 53
- return build
- end
- function detectPeripherals()
- for i,side in pairs(rs.getSides()) do
- if peripheral.isPresent(side) then
- if peripheral.getType(side) == "sensor" then
- if sensor == nil then
- os.loadAPI("ocs/apis/sensor")
- if sensor == nil then
- error("function detectPeripherals: Sensor API not installed")
- end
- colorText("OCS LOADED",colors.black,colors.lime)
- else
- colorText("OCS ALREADY LOADED",colors.black,colors.red)
- end
- colorText("hardware found: OCS Sensor",colors.red,nil,true)
- SENSOR = sensor.wrap(side)
- colorText(" wrapped",colors.lime)
- elseif peripheral.getType(side) == "monitor" then
- colorText("hardware found: Monitor",colors.red,nil,true)
- MONITOR = peripheral.wrap(side)
- colorText(" wrapped",colors.lime)
- elseif peripheral.getType(side) == "modem" then
- colorText("hardware found: Modem",colors.red,nil,true)
- MODEM = peripheral.wrap(side)
- colorText(" wrapped",colors.lime)
- elseif peripheral.getType(side) == "drive" then
- colorText("hardware found: Disk Drive",colors.red,nil,true)
- DRIVE = peripheral.wrap(side)
- colorText(" wrapped",colors.lime)
- elseif peripheral.getType(side) == "printer" then
- colorText("hardware found: Printer",colors.red,nil,true)
- PRINTER = peripheral.wrap(side)
- colorText(" wrapped",colors.lime)
- end
- end
- end
- return SENSOR,MONITOR,MODEM,DRIVE,PRINTER
- end
- -- END OF INITIALATION FUNCTIONS
- -- BASIC FUNCTIONS
- function getCursorPos(TYPE)
- if TYPE == "terminal" then
- local X,Y = term.getCursorPos()
- return X,Y
- elseif TYPE == "monitor" then
- if MONITOR == nil then
- SENSOR,MONITOR,MODEM,DRIVE,PRINTER = detectPeripherals()
- else
- local X,Y = MONITOR.getCursorPos()
- return X,Y
- end
- else
- error("function getCursorPos: Type unknown")
- end
- end
- function cPrint(TXT,LINE,TYPE)
- if TYPE == nil then
- TYPE = "terminal"
- end
- if LINE == nil and TYPE == "terminal" then
- local X,Y = term.getCursorPos()
- LINE = Y
- elseif LINE == nil and TYPE == "monitor" then
- local X,Y = MONITOR.getCursorPos()
- LINE = Y
- end
- if TYPE == "terminal" then
- local X,Y = term.getSize()
- term.setCursorPos(((X/2)+1)-(#TXT/2),LINE)
- term.write(TXT)
- elseif TYPE == "monitor" then
- local X,Y = MONITOR.getSize()
- MONITOR.setCursorPos(((X/2)+1)-(#TXT/2),LINE)
- MONITOR.write(TXT)
- else
- error("function cPrint: type is null")
- end
- end
- function colorText(TXT,txtColor,bgColor,CURLINE)
- local Format = print
- if CURLINE == true then
- Format = write
- end
- local isA = term.isColor and term.isColor()
- if isA then
- term.setTextColor(txtColor)
- if bgColor == nil then
- term.setBackgroundColor(colors.black)
- else
- term.setBackgroundColor(bgColor)
- end
- Format(TXT)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- else
- Format(TXT)
- end
- end
- function findDate()
- local _time = textutils.formatTime(os.time())
- local day = os.day()
- local dayofWeek = (day%7)+1
- local days = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}
- local month = {"January","February","March","April","May","June","July","August","September","October","November","December"}
- local daysinMonth = {31,28,31,30,31,30,31,31,30,31,30,31}
- local daysinYear = 365
- local year = math.ceil(day/daysinYear)
- local leap = year%4
- local searching = true
- local curMonth = nil
- local curDayinMonth = nil
- while searching do
- -- if Current year is a Laep year
- if leap == 0 then
- daysinMonth[2] = 29
- daysinYear = 366
- else
- daysinMonth[2] = 28
- daysinYear = 365
- end
- -- Find the current Month the Game is in
- for i,MONTH in pairs(month) do
- if day > daysinMonth[i] then
- day = day - daysinMonth[i]
- else
- curMonth = MONTH
- curDayinMonth = day % daysinMonth[i]
- searching = false
- break
- end
- end
- -- Format the Day in the month to a string
- if curDayinMonth == 1 or curDayinMonth == 21 or curDayinMonth == 31 then
- curDayinMonth = tostring(curDayinMonth).."st"
- elseif curDayinMonth == 2 or curDayinMonth == 22 then
- curDayinMonth = tostring(curDayinMonth).."nd"
- elseif curDayinMonth == 3 or curDayinMonth == 23 then
- curDayinMonth = tostring(curDayinMonth).."rd"
- else
- curDayinMonth = tostring(curDayinMonth).."th"
- end
- end
- -- RETURN Time of Day, Day of week, month, day in month, year
- return _time,days[dayofWeek],curMonth,curDayinMonth,year
- end
- function splitString(STRING)
- local t = {}
- for token in string.gmatch(STRING,"[^%s]+") do
- t[#t+1] = token
- end
- return t
- end
- -- END OF BASIC FUNCTIONS
- -- BEGIN GUI HANDLES
- function createButton(name,func,xPos,yPos,width,height)
- button[name] = {}
- button[name]["func"] = func
- button[name]["xPos"] = xPos
- button[name]["yPos"] = yPos
- button[name]["width"] = width
- button[name]["height"] = height
- end
- function drawButton(text, color, bData)
- if MONITOR == nil then
- SENSOR,MONITOR,MODEM,DRIVE,PRINTER = detectPeripherals()
- end
- MONITOR.setBackgroundColor(color)
- local widthPos = bData["xPos"] + bData["width"]
- local heightPos = bData["yPos"] + bData["height"]
- local xSpot = math.floor((bData["width"]-string.len(text))/2) +1
- for j = bData["yPos"],heightPos do -- height of the button
- MONITOR.setCursorPos(bData["xPos"], j)
- if j == bData["yPos"]+1 then -- if the text needs to go on this line
- for k = 0, widthPos - bData["xPos"] - string.len(text) +1 do
- if k == xSpot then
- MONITOR.write(text)
- else
- MONITOR.write(" ")
- end
- end
- else
- for i = bData["xPos"], widthPos do -- draw a single line of button
- MONITOR.write(" ")
- end
- end
- end
- MONITOR.setBackgroundColor(colors.black)
- end
- function checkButtonHit(X,Y)
- for name,data in pairs(button) do
- local widthPos = data["xPos"] + data["width"]
- local heightPos = data["yPos"] + data["height"]
- if Y >= data["yPos"] and Y <= heightPos then
- if X >= data["xPos"] and X <= widthPos then
- data["func"]()
- end
- end
- end
- end
- function drawAllButtons()
- for name,data in pairs(button) do
- drawButton(name, colors.gray, data)
- end
- end
- function alert(LN1,LN2)
- local H = 6
- local isA = MONITOR.isColor and MONITOR.isColor()
- local X,Y = MONITOR.getSize()
- local yCenter = Y/2
- local xCenter = (X/2)+1
- local W = nil
- if LN2 == nil then
- H = 5
- W = #LN1+4
- else
- if #LN1 > #LN2 then
- W = #LN1+4
- else
- W = #LN2+4
- end
- end
- if isA then
- MONITOR.setBackgroundColor(colors.white)
- MONITOR.setTextColor(colors.black)
- end
- for i=1,H do
- if i%H == 0 or i%H == 1 then
- MONITOR.setCursorPos(xCenter-(W/2),(yCenter-(H/2))+i)
- for x=1,W do
- MONITOR.write("#")
- end
- else
- MONITOR.setCursorPos(xCenter-(W/2),(yCenter-(H/2))+i)
- for x=1,W do
- if x%W == 1 or x%W == 0 then
- MONITOR.write("#")
- else
- MONITOR.write(" ")
- end
- end
- end
- end
- MONITOR.setCursorPos(xCenter-(W/2)+2,yCenter-(H/2)+3)
- MONITOR.write(LN1)
- if LN2 ~= nil then
- MONITOR.setCursorPos(xCenter-(W/2)+2,yCenter-(H/2)+4)
- MONITOR.write(LN2)
- end
- if isA then
- MONITOR.setBackgroundColor(colors.black)
- MONITOR.setTextColor(colors.white)
- end
- end
- -- END OF GUI HANDLES
- -- BEGIN FILE HANDLES
- function saveTable(PATH,TABLE)
- local txt = textutils.serialize(TABLE)
- local file = fs.open(PATH,"w")
- file.write(txt)
- file.close()
- end
- function openTable(PATH)
- if not fs.exists(PATH) then
- print("file not found, creating one")
- local file = fs.open(PATH,"w")
- file.close()
- end
- local file = fs.open(PATH,"r")
- local txt = file.readAll()
- file.close()
- local TAB = textutils.unserialize(txt)
- return TAB
- end
- -- END OF FILE HANDLES
- -- BEGIN REDNET SECURITY
- local contains = function(tbl, val)
- for k,v in pairs(tbl) do
- if v == val then return true end
- end
- return false
- end
- local hash = function(str)
- local out = {}
- for s in str:gmatch(".") do
- if not contains(out, s) then out[#out+1] = s end
- end
- return table.concat(out)
- end
- function encryption(decrypt,txt,key,password)
- local encrypted = ""
- local encryptedTXT = ""
- local base = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789=+[]{}_,." '
- local baseLength = #base
- if key ~= nil and key > baseLength then -- keep the key within bounds of the length of the base
- key = key % baseLength
- end
- if password ~= nil then -- place the password at the front of the encrypted without duplicates
- encrypted = hash(password)
- end
- for i=1,baseLength do -- create the encrypted
- local char = string.sub(base,(key+i)%baseLength,(key+i)%baseLength)
- if password ~= nil then
- for I=1,#password do -- find the same letter in password and remove it
- local pasChar = string.sub(password,I,I)
- if pasChar == char then
- char = ""
- end
- end
- end
- encrypted = encrypted..char
- end
- for i=1,#txt do -- loop through the text to be encrypteded
- local letter = string.sub(txt,i,i)
- for I=1,baseLength do
- local baseKey = string.sub(base,I,I)
- local hashKey = string.sub(encrypted,I,I)
- if decrypt == true then -- if decrypting the flip flop the baseKey and hashKey
- if letter == hashKey then
- encryptedTXT = encryptedTXT..baseKey
- end
- else
- if letter == baseKey then -- encrypted baseKey with hashKey
- encryptedTXT = encryptedTXT..hashKey
- end
- end
- end
- end
- return encryptedTXT
- end
- -- END OF REDNET SECURITY
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement