View difference between Paste ID: 1DUrVHN6 and HErbu4yP
SHOW: | | - or go back to the newest paste.
1-
local tX,tY,cX,cY
1+
-- Coz Core functions and utilities API
2
-- Author: theonlycozzy
3
-- July 31 2013
4-
  ver = "0.1"
4+
5-
  return ver
5+
local button = {}
6
7
-- INITIALATION FUNCTIONS
8
9
function getVersion()
10-
function clear(delay)
10+
  local build = 53
11-
  if delay == true then
11+
  return build
12-
    os.sleep(1)
12+
13
14-
  term.clear()
14+
function detectPeripherals()
15-
  term.setCursorPos(1,1)
15+
  for i,side in pairs(rs.getSides()) do
16
    if peripheral.isPresent(side) then
17
      if peripheral.getType(side) == "sensor" then
18-
function newLine(indent)
18+
        if sensor == nil then
19-
  if indent == nil then
19+
          os.loadAPI("ocs/apis/sensor")
20-
    indent = 0
20+
          if sensor == nil then
21
            error("function detectPeripherals: Sensor API not installed")
22
          end
23-
  cX,cY = term.getCursorPos()
23+
          colorText("OCS LOADED",colors.black,colors.lime)
24-
  term.setCursorPos(1+indent,cY+1)
24+
        else
25
          colorText("OCS ALREADY LOADED",colors.black,colors.red)
26
        end
27-
function cPrint(text,line)
27+
        
28-
  if line == nil then
28+
        colorText("hardware found: OCS Sensor",colors.red,nil,true)
29-
    cX,cY = term.getCursorPos()
29+
        SENSOR = sensor.wrap(side)
30-
    line = cY
30+
        colorText("  wrapped",colors.lime)
31
      
32
      elseif peripheral.getType(side) == "monitor" then
33-
  tX,tY = term.getSize()
33+
        colorText("hardware found: Monitor",colors.red,nil,true)
34-
  term.setCursorPos( (tX/2) - (#text/2), line  )
34+
        MONITOR = peripheral.wrap(side)
35
        colorText("  wrapped",colors.lime)
36-
  term.write(text)  
36+
      
37
      elseif peripheral.getType(side) == "modem" then
38
        colorText("hardware found: Modem",colors.red,nil,true)
39
        MODEM = peripheral.wrap(side)
40
        colorText("  wrapped",colors.lime)
41-
function drawAlertBox()
41+
42-
  tX,tY = term.getSize()
42+
      elseif peripheral.getType(side) == "drive" then
43-
  temp = (tY/2)-4
43+
        colorText("hardware found: Disk Drive",colors.red,nil,true)
44
        DRIVE = peripheral.wrap(side)
45-
  term.setCursorPos(1,temp)  
45+
        colorText("  wrapped",colors.lime)
46-
  for i=1,7 do
46+
47-
    if i%7 == 0 or i%7 == 1 then
47+
      elseif peripheral.getType(side) == "printer" then
48-
      cPrint("*******************************") newLine()
48+
        colorText("hardware found: Printer",colors.red,nil,true)
49
        PRINTER = peripheral.wrap(side)
50-
      cPrint("*                             *") newLine()
50+
        colorText("  wrapped",colors.lime)
51
      
52
      end
53
    end
54
  end
55-
function alertBox(info)
55+
  return SENSOR,MONITOR,MODEM,DRIVE,PRINTER
56-
  drawAlertBox()
56+
57-
  term.setCursorPos(1,(tY/2)-2)
57+
58-
  cPrint(info)  
58+
-- END OF INITIALATION FUNCTIONS
59-
  term.setCursorPos(1,tY-1)
59+
60
61
function getCursorPos(TYPE)
62-
-- FILE HANDLES
62+
  if TYPE == "terminal" then
63
    local X,Y = term.getCursorPos()
64-
function addLinetoFile(text,file)
64+
    return X,Y
65-
  temp = {}
65+
  elseif TYPE == "monitor" then
66-
    if not fs.exists(file) then
66+
    if MONITOR == nil then
67-
      print("Creating file")
67+
      SENSOR,MONITOR,MODEM,DRIVE,PRINTER = detectPeripherals()
68-
      clear(true)
68+
69-
      f = io.open(file,"w")
69+
      local X,Y = MONITOR.getCursorPos()
70-
      f:close()
70+
      return X,Y
71
    end
72-
  f = io.open(file,"r")
72+
  else
73-
    if f == nil then
73+
    error("function getCursorPos: Type unknown")
74-
      error("Failed to load")
74+
75-
      return nil
75+
76
77
function cPrint(TXT,LINE,TYPE)
78-
    for line in f:lines() do
78+
  if TYPE == nil then
79-
      temp[#temp+1] = line
79+
    TYPE = "terminal"
80
  end
81-
  temp[#temp+1] = text
81+
  if LINE == nil and TYPE == "terminal" then
82-
  f:close()
82+
    local X,Y = term.getCursorPos()
83-
  f = fs.open(file,"w")
83+
    LINE = Y
84-
    for i=1,#temp do
84+
  elseif LINE == nil and TYPE == "monitor" then
85-
      f.writeLine(temp[i])
85+
    local X,Y = MONITOR.getCursorPos()
86
    LINE = Y
87-
  f:close()
87+
88
  if TYPE == "terminal" then
89
    local X,Y = term.getSize()
90-
function delLinefromFile(text,file)
90+
    term.setCursorPos(((X/2)+1)-(#TXT/2),LINE)
91-
  temp = {}
91+
    term.write(TXT)
92-
  f = io.open(file,"r")
92+
  elseif TYPE == "monitor" then
93-
    if f == nil then
93+
    local X,Y = MONITOR.getSize()
94-
      error("File not Found")
94+
    MONITOR.setCursorPos(((X/2)+1)-(#TXT/2),LINE)
95-
      return nil
95+
    MONITOR.write(TXT)
96
  else
97
    error("function cPrint: type is null")
98-
    for line in f:lines() do
98+
  end  
99-
      if not(string.lower(line) == string.lower(text)) then
99+
100-
        temp[#temp+1] = line
100+
101
function colorText(TXT,txtColor,bgColor,CURLINE)
102
  local Format = print
103-
  f:close()
103+
  if CURLINE == true then
104-
  f = fs.open(file,"w")
104+
    Format = write
105-
    if f == nil then
105+
106-
      return nil
106+
107
  local isA = term.isColor and term.isColor()
108
  if isA then
109-
    for i=1,#temp do
109+
    term.setTextColor(txtColor)
110-
      f.writeLine(temp[i])
110+
      if bgColor == nil then
111
        term.setBackgroundColor(colors.black)
112-
  f.close()
112+
      else
113-
end
113+
        term.setBackgroundColor(bgColor)
114
      end
115
    Format(TXT)
116
    
117
    term.setBackgroundColor(colors.black)
118
    term.setTextColor(colors.white)
119
  else
120
    Format(TXT)
121
  end
122
end
123
124
function findDate()
125
  local _time = textutils.formatTime(os.time())
126
  local day = os.day()
127
  local dayofWeek = (day%7)+1
128
  local days = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}
129
  local month = {"January","February","March","April","May","June","July","August","September","October","November","December"}
130
  local daysinMonth = {31,28,31,30,31,30,31,31,30,31,30,31}
131
  local daysinYear = 365
132
  local year = math.ceil(day/daysinYear)
133
  local leap = year%4
134
  local searching = true
135
  local curMonth = nil
136
  local curDayinMonth = nil
137
  
138
  while searching do
139
    -- if Current year is a Laep year
140
    if leap == 0 then
141
      daysinMonth[2] = 29
142
      daysinYear = 366
143
    else
144
      daysinMonth[2] = 28
145
      daysinYear = 365
146
    end
147
    
148
    -- Find the current Month the Game is in
149
    for i,MONTH in pairs(month) do
150
      if day > daysinMonth[i] then
151
        day = day - daysinMonth[i]
152
      else
153
        curMonth = MONTH
154
        curDayinMonth = day % daysinMonth[i]
155
        searching = false
156
        break
157
      end
158
    end
159
    
160
    -- Format the Day in the month to a string
161
    if curDayinMonth == 1 or curDayinMonth == 21 or curDayinMonth == 31 then
162
      curDayinMonth = tostring(curDayinMonth).."st"
163
    elseif curDayinMonth == 2 or curDayinMonth == 22 then
164
      curDayinMonth = tostring(curDayinMonth).."nd"
165
    elseif curDayinMonth == 3 or curDayinMonth == 23 then
166
      curDayinMonth = tostring(curDayinMonth).."rd"
167
    else
168
      curDayinMonth = tostring(curDayinMonth).."th"
169
    end
170
  end
171
  -- RETURN Time of Day, Day of week, month, day in month, year
172
  return _time,days[dayofWeek],curMonth,curDayinMonth,year
173
end
174
175
function splitString(STRING)
176
  local t = {}
177
  for token in string.gmatch(STRING,"[^%s]+") do
178
    t[#t+1] = token
179
  end
180
  
181
  return t
182
end
183
184
-- END OF BASIC FUNCTIONS
185
-- BEGIN GUI HANDLES
186
187
function createButton(name,func,xPos,yPos,width,height)
188
   button[name] = {}
189
   button[name]["func"] = func
190
   button[name]["xPos"] = xPos
191
   button[name]["yPos"] = yPos
192
   button[name]["width"] = width
193
   button[name]["height"] = height
194
end
195
196
function drawButton(text, color, bData)
197
  if MONITOR == nil then
198
    SENSOR,MONITOR,MODEM,DRIVE,PRINTER = detectPeripherals()
199
  end
200
  MONITOR.setBackgroundColor(color)
201
  local widthPos = bData["xPos"] + bData["width"]
202
  local heightPos = bData["yPos"] + bData["height"]
203
  local xSpot = math.floor((bData["width"]-string.len(text))/2) +1
204
205
  for j = bData["yPos"],heightPos do  -- height of the button
206
    MONITOR.setCursorPos(bData["xPos"], j)
207
    if j == bData["yPos"]+1 then  -- if the text needs to go on this line
208
      for k = 0, widthPos - bData["xPos"] - string.len(text) +1 do
209
        if k == xSpot then
210
          MONITOR.write(text)
211
        else
212
          MONITOR.write(" ")
213
        end
214
      end
215
    else
216
      for i = bData["xPos"], widthPos do -- draw a single line of button
217
        MONITOR.write(" ")
218
      end
219
    end
220
  end
221
  MONITOR.setBackgroundColor(colors.black)
222
end
223
224
function checkButtonHit(X,Y)
225
  for name,data in pairs(button) do
226
    local widthPos = data["xPos"] + data["width"]
227
    local heightPos = data["yPos"] + data["height"]
228
    if Y >= data["yPos"] and Y <= heightPos then
229
      if X >= data["xPos"] and X <= widthPos then
230
        data["func"]()
231
      end
232
    end
233
  end
234
end
235
236
function drawAllButtons()
237
   for name,data in pairs(button) do
238
      drawButton(name, colors.gray, data)
239
   end
240
end
241
242
function alert(LN1,LN2)
243
  local H = 6
244
  local isA = MONITOR.isColor and MONITOR.isColor()
245
  local X,Y = MONITOR.getSize()
246
  local yCenter = Y/2
247
  local xCenter = (X/2)+1
248
  local W = nil
249
  if LN2 == nil then
250
    H = 5
251
    W = #LN1+4
252
  else
253
    if #LN1 > #LN2 then
254
      W = #LN1+4
255
    else
256
      W = #LN2+4
257
    end
258
  end
259
  if isA then
260
    MONITOR.setBackgroundColor(colors.white)
261
    MONITOR.setTextColor(colors.black)
262
  end
263
  for i=1,H do
264
    if i%H == 0 or i%H == 1 then
265
      MONITOR.setCursorPos(xCenter-(W/2),(yCenter-(H/2))+i)
266
      for x=1,W do
267
        MONITOR.write("#")
268
      end
269
    else
270
      MONITOR.setCursorPos(xCenter-(W/2),(yCenter-(H/2))+i)
271
      for x=1,W do
272
        if x%W == 1 or x%W == 0 then
273
          MONITOR.write("#")
274
        else
275
          MONITOR.write(" ")
276
        end
277
      end
278
    end
279
  end
280
  MONITOR.setCursorPos(xCenter-(W/2)+2,yCenter-(H/2)+3)
281
  MONITOR.write(LN1)
282
  if LN2 ~= nil then
283
    MONITOR.setCursorPos(xCenter-(W/2)+2,yCenter-(H/2)+4)
284
    MONITOR.write(LN2)
285
  end
286
  if isA then
287
    MONITOR.setBackgroundColor(colors.black)
288
    MONITOR.setTextColor(colors.white)
289
  end
290
end
291
292
293
-- END OF GUI HANDLES
294
-- BEGIN FILE HANDLES
295
296
function saveTable(PATH,TABLE)
297
  local txt = textutils.serialize(TABLE)
298
  local file = fs.open(PATH,"w")
299
  file.write(txt)
300
  file.close()
301
end
302
303
function openTable(PATH)
304
  if not fs.exists(PATH) then
305
    print("file not found, creating one")
306
    local file = fs.open(PATH,"w")
307
    file.close()
308
  end
309
  local file = fs.open(PATH,"r")
310
  local txt = file.readAll()
311
  file.close()
312
  local TAB = textutils.unserialize(txt)
313
  
314
  return TAB
315
end
316
317
-- END OF FILE HANDLES
318
-- BEGIN REDNET SECURITY
319
320
local contains = function(tbl, val)
321
  for k,v in pairs(tbl) do 
322
    if v == val then return true end
323
  end
324
  return false
325
end
326
327
local hash = function(str)
328
  local out = {}
329
  for s in str:gmatch(".") do
330
    if not contains(out, s) then out[#out+1] = s end
331
  end
332
  return table.concat(out)
333
end
334
335
function encryption(decrypt,txt,key,password)
336
  local encrypted = ""
337
  local encryptedTXT = ""
338
  local base = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789=+[]{}_,." '
339
  local baseLength = #base
340
  
341
  if key ~= nil and key > baseLength then  -- keep the key within bounds of the length of the base
342
    key = key % baseLength
343
  end
344
  
345
  if password ~= nil then  -- place the password at the front of the encrypted without duplicates    
346
    encrypted = hash(password)
347
  end
348
  
349
  for i=1,baseLength do -- create the encrypted
350
    local char = string.sub(base,(key+i)%baseLength,(key+i)%baseLength)
351
  
352
    if password ~= nil then
353
      for I=1,#password do -- find the same letter in password and remove it
354
        local pasChar = string.sub(password,I,I)
355
    
356
        if pasChar == char then
357
          char = ""
358
        end
359
      end
360
    end
361
    encrypted = encrypted..char
362
  end
363
  
364
  for i=1,#txt do -- loop through the text to be encrypteded
365
    local letter = string.sub(txt,i,i)
366
    
367
    for I=1,baseLength do
368
      local baseKey = string.sub(base,I,I)
369
      local hashKey = string.sub(encrypted,I,I)
370
      
371
      if decrypt == true then -- if decrypting the flip flop the baseKey and hashKey
372
        if letter == hashKey then
373
          encryptedTXT = encryptedTXT..baseKey
374
        end
375
      else
376
        if letter == baseKey then -- encrypted baseKey with hashKey
377
          encryptedTXT = encryptedTXT..hashKey
378
        end
379
      end
380
    end
381
  end
382
  return encryptedTXT
383
end
384
385
-- END OF REDNET SECURITY