Advertisement
dannysmc95

uAPI

May 4th, 2015
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.89 KB | None | 0 0
  1. -- Written by DannySMc
  2. -- Twitter: @dannysmc95
  3. version = 2.1
  4.  
  5. function getVer()
  6.     return version
  7. end
  8.  
  9. -- Configuration File Protocols
  10. function saveConfig(table, file)
  11.   fConfig = fs.open(file, "w") or error("Cannot open file "..file, 2)
  12.   fConfig.write(textutils.serialize(table))
  13.   fConfig.close()
  14. end
  15.  
  16. function loadConfig(file)
  17.   fConfig = fs.open(file, "r")
  18.   ret = textutils.unserialize(fConfig.readAll())
  19.   return ret
  20. end
  21.  
  22. -- Beaming Technology
  23. function beam(username, xcord, ycord, zcord, blockSide)
  24.     command = peripheral.wrap(blockSide)
  25.     if command == "true" then
  26.         command.setCommand("/tp "..username.." "..xcord.." "..ycord.." "..zcord)
  27.         check = command.runCommand()
  28.         if check == "true" then
  29.             return true
  30.         else
  31.             return false
  32.         end
  33.     else
  34.         return "Syntax Error"
  35.     end
  36. end
  37.  
  38. -- XP Functions
  39. function xpGive(player, amount, blockSide)
  40.     if player and amount and blockSide then
  41.         command = peripheral.wrap(blockSide)
  42.         command.setCommand("/xp "..amount.." "..player)
  43.         check = command.runCommand()
  44.         if check == "true" then
  45.             return true
  46.         else
  47.             return false
  48.         end
  49.     else
  50.         return false
  51.     end
  52.     if not player then
  53.         return "Need player"
  54.     end
  55.     if not amount then
  56.         return "Need amount"
  57.     end
  58.     if not blockSide then
  59.         return "Need side of command block"
  60.     end
  61. end
  62.  
  63. -- Peripheral Functions
  64. function findPeripheral(Perihp) --Returns side of first matching peripheral matching passed string  
  65.   for _,s in ipairs(rs.getSides()) do
  66.     if peripheral.isPresent(s) and peripheral.getType(s) == Perihp then
  67.       return s  
  68.     end
  69.   end
  70.   return false
  71. end
  72.  
  73. -- Common Draw Functions
  74. function cs()
  75.     term.clear()
  76.     term.setCursorPos(1,1)
  77.     return
  78. end
  79.  
  80. function setCol(textColour, backgroundColour)
  81.     if textColour and backgroundColour then
  82.         if term.isColour() then
  83.             term.setTextColour(colours[textColour])
  84.             term.setBackgroundColour(colours[backgroundColour])
  85.             return true
  86.         else
  87.             return false
  88.         end
  89.     else
  90.         return false
  91.     end
  92. end
  93.  
  94.  function resetCol()
  95.     if term.isColour then
  96.         term.setTextColour(colours.white)
  97.         term.setBackgroundColour(colours.black)
  98.         return true
  99.     else
  100.         return false
  101.     end
  102. end
  103.  
  104. -- Print Functions
  105. function printC(Text, Line, NextLine, Color, BkgColor) -- print centered
  106.   local x, y = term.getSize()
  107.   x = x/2 - #Text/2
  108.   term.setCursorPos(x, Line)
  109.   if Color then setCol(Color, BkgColor) end
  110.   term.write(Text)
  111.   if NextLine then
  112.     term.setCursorPos(1, NextLine)
  113.   end
  114.   if Color then resetCol(Color, BkgColor) end
  115.   return true  
  116. end
  117.  
  118. function printL(Text, Line, NextLine, Color, BkgColor) -- print line
  119.  
  120.  
  121.   local x, y = term.getSize()
  122.   if ((term.isColor) and (term.isColor() == false) and (Text == " ")) then Text = "-" end
  123.   for i = 1, x do
  124.     term.setCursorPos(i, Line)
  125.     if Color then setCol(Color, BkgColor) end
  126.     term.write(Text)
  127.   end
  128.   if NextLine then  
  129.     term.setCursorPos(1, NextLine)
  130.   end
  131.   if Color then resetCol(Color, BkgColor) end
  132.   return true  
  133. end
  134.  
  135. function printA(Text, xx, yy, NextLine, Color, BkgColor) -- print anywhere
  136.   term.setCursorPos(xx,yy)
  137.   if Color then setCol(Color, BkgColor) end
  138.   term.write(Text)
  139.   if NextLine then  
  140.     term.setCursorPos(1, NextLine)
  141.   end
  142.   if Color then resetCol(Color, BkgColor) end
  143.   return true  
  144. end
  145.  
  146. function clearLine(Line, NextLine) -- May seem a bit odd, but it may be usefull sometimes
  147.   local x, y = term.getSize()
  148.   for i = 1, x do
  149.     term.setCursorPos(i, Line)
  150.     term.write(" ")
  151.   end  
  152.   if not NextLine then  
  153.     x, y = term.getCursorPos()
  154.     term.setCursorPos(1, y+1)
  155.   end
  156.   return true  
  157. end
  158.  
  159. function drawBox(StartX, lengthX, StartY, lengthY, Text, Color, BkgColor) -- does what is says on the tin.
  160.   local x, y = term.getSize()
  161.   if Color then setCol(Color, BkgColor) end
  162.   if not Text then Text = "*" end
  163.   lengthX = lengthX - 1
  164.   lengthY = lengthY - 1
  165.   EndX = StartX + lengthX  
  166.   EndY = StartY + lengthY
  167.   term.setCursorPos(StartX, StartY)
  168.   term.write(string.rep(Text, lengthX))
  169.   term.setCursorPos(StartX, EndY)
  170.   term.write(string.rep(Text, lengthX))
  171.   for i = StartY, EndY do
  172.     term.setCursorPos(StartX, i)
  173.     term.write(Text)
  174.     term.setCursorPos(EndX, i)    
  175.     term.write(Text)
  176.   end
  177.   resetCol(Color, BkgColor)
  178.   return true  
  179. end
  180.  
  181.  -- Download Functions
  182. function getPBFile(PBCode, uPath) -- pastebin code of the file, and path to save
  183.     local PBFile = http.get("http://pastebin.com/raw.php?i="..textutils.urlEncode(PBCode))
  184.     if PBFile then
  185.         local PBfileToWrite = PBfile.readAll()
  186.         PBfile.close()
  187.  
  188.         local file = fs.open( uPath, "w" )
  189.         file.write(PBfileToWrite)
  190.         file.close()
  191.             return true
  192.         else
  193.             return false
  194.         end
  195. end
  196.  
  197. -- Database Functions
  198. db = {}
  199. db.__index = db
  200.  
  201. function db.delete(Filename)
  202.   if fs.exists(Filename) then
  203.     fs.delete(Filename)
  204.     return true
  205.   end
  206.   return false
  207. end
  208.  
  209. function db.load(Filename)
  210.   if not fs.exists(Filename) then
  211.     local F = fs.open(Filename, "w")
  212.     F.write("{}")
  213.     F.close()
  214.   end
  215.   local F = fs.open(Filename, "r")
  216.   local Data = F.readAll()
  217.   F.close()
  218.   Data = textutils.unserialize(Data)
  219.   return Data
  220. end
  221.  
  222. function db.save(Filename, ATable)
  223.   local Data = textutils.serialize(ATable)
  224.   local F = fs.open(Filename, "w")
  225.   F.write(Data)
  226.   F.close()
  227.   return true
  228. end
  229.  
  230. function db.search(searchstring, ATable)
  231.   for i, V in pairs(ATable) do
  232.     if tostring(ATable[i]) == tostring(searchstring) then
  233.       return i
  234.     end
  235.   end
  236.   return 0
  237. end
  238.  
  239. function db.removeString(Filename, AString)
  240.   local TempT = db.load(Filename)
  241.   if type(TempT) ~= "table" then return false end
  242.   local Pos = db.search(AString, TempT)
  243.   if Pos > 0 then
  244.     table.remove(TempT, Pos)
  245.     db.save(Filename, TempT)
  246.     return true
  247.   else
  248.     return false
  249.   end
  250. end
  251.  
  252. function db.insertString(Filename, AString)
  253.   local TempT = db.load(Filename)
  254.   if type(TempT) ~= "table" then TempT = {} end
  255.   table.insert(TempT, AString)
  256.   db.save(Filename, TempT)
  257.   return true
  258. end
  259.  
  260. -- Serial Generator
  261. function serialGen(digits) -- seems to become unstable above 18 digits long
  262.  
  263.   local serial
  264.   for i = 1, digits do
  265.     if i == 1 then
  266.       serial = math.random(9)
  267.     else
  268.       serial = serial.. math.random(9)
  269.     end
  270.   end
  271.   serial = tonumber(serial)
  272.   return serial
  273. end
  274.  
  275. -- Encryption & Checksum!
  276. local function zfill(N)
  277.     N=string.format("%X",N)
  278.     Zs=""
  279.     if #N==1 then
  280.         Zs="0"
  281.     end
  282.     return Zs..N
  283. end
  284.  
  285. local function serializeImpl(t)
  286.     local sType = type(t)
  287.     if sType == "table" then
  288.         local lstcnt=0
  289.         for k,v in pairs(t) do
  290.             lstcnt = lstcnt + 1
  291.         end
  292.         local result = "{"
  293.         local aset=1
  294.         for k,v in pairs(t) do
  295.             if k==aset then
  296.                 result = result..serializeImpl(v)..","
  297.                 aset=aset+1
  298.             else
  299.                 result = result..("["..serializeImpl(k).."]="..serializeImpl(v)..",")
  300.             end
  301.         end
  302.         result = result.."}"
  303.         return result
  304.     elseif sType == "string" then
  305.         return string.format("%q",t)
  306.     elseif sType == "number" or sType == "boolean" or sType == "nil" then
  307.         return tostring(t)
  308.     elseif sType == "function" then
  309.         local status,data=pcall(string.dump,t)
  310.         if status then
  311.             return 'func('..string.format("%q",data)..')'
  312.         else
  313.             error()
  314.         end
  315.     else
  316.         error()
  317.     end
  318. end
  319.  
  320. local function split(T,func)
  321.     if func then
  322.         T=func(T)
  323.     end
  324.     local Out={}
  325.     if type(T)=="table" then
  326.         for k,v in pairs(T) do
  327.             Out[split(k)]=split(v)
  328.         end
  329.     else
  330.         Out=T
  331.     end
  332.     return Out
  333. end
  334.  
  335. local function serialize( t )
  336.     t=split(t)
  337.     return serializeImpl( t, tTracking )
  338. end
  339.  
  340. local function unserialize( s )
  341.     local func, e = loadstring( "return "..s, "serialize" )
  342.     local funcs={}
  343.     if not func then
  344.         return e
  345.     end
  346.     setfenv( func, {
  347.         func=function(S)
  348.             local new={}
  349.             funcs[new]=S
  350.             return new
  351.         end,
  352.     })
  353.     return split(func(),function(val)
  354.         if funcs[val] then
  355.             return loadstring(funcs[val])
  356.         else
  357.             return val
  358.         end
  359.     end)
  360. end
  361.  
  362. local function sure(N,n)
  363.     if (l2-n)<1 then N="0" end
  364.     return N
  365. end
  366.  
  367. local function splitnum(S)
  368.     Out=""
  369.     for l1=1,#S,2 do
  370.         l2=(#S-l1)+1
  371.         CNum=tonumber("0x"..sure(string.sub(S,l2-1,l2-1),1) .. sure(string.sub(S,l2,l2),0))
  372.         Out=string.char(CNum)..Out
  373.     end
  374.     return Out
  375. end
  376.  
  377. local function wrap(N)
  378.     return N-(math.floor(N/256)*256)
  379. end
  380.  
  381. function checksum(S,num) -- args strInput and intPassNumber
  382.     local sum=0
  383.     for char in string.gmatch(S,".") do
  384.         for l1=1,(num or 1) do
  385.             math.randomseed(string.byte(char)+sum)
  386.             sum=sum+math.random(0,9999)
  387.         end
  388.     end
  389.     math.randomseed(sum)
  390.     return sum
  391. end
  392.  
  393. local function genkey(len,psw)
  394.     checksum(psw)
  395.     local key={}
  396.     local tKeys={}
  397.     for l1=1,len do
  398.         local num=math.random(1,len)
  399.         while tKeys[num] do
  400.             num=math.random(1,len)
  401.         end
  402.         tKeys[num]=true
  403.         key[l1]={num,math.random(0,255)}
  404.     end
  405.     return key
  406. end
  407.  
  408. function encrypt(data,psw) -- args strInput and strPassword
  409.     data=serialize(data)
  410.     local chs=checksum(data)
  411.     local key=genkey(#data,psw)
  412.     local out={}
  413.     local cnt=1
  414.     for char in string.gmatch(data,".") do
  415.         table.insert(out,key[cnt][1],zfill(wrap(string.byte(char)+key[cnt][2])),chars)
  416.         cnt=cnt+1
  417.     end
  418.     return string.sub(serialize({chs,table.concat(out)}),2,-3)
  419. end
  420.  
  421. function decrypt(data,psw) -- args strInput and strPassword
  422.     local oData=data
  423.     data=unserialize("{"..data.."}")
  424.     if type(data)~="table" then
  425.         return oData
  426.     end
  427.     local chs=data[1]
  428.     data=data[2]
  429.     local key=genkey((#data)/2,psw)
  430.     local sKey={}
  431.     for k,v in pairs(key) do
  432.         sKey[v[1]]={k,v[2]}
  433.     end
  434.     local str=splitnum(data)
  435.     local cnt=1
  436.     local out={}
  437.     for char in string.gmatch(str,".") do
  438.         table.insert(out,sKey[cnt][1],string.char(wrap(string.byte(char)-sKey[cnt][2])))
  439.         cnt=cnt+1
  440.     end
  441.     out=table.concat(out)
  442.     if checksum(out or "")==chs then
  443.         return unserialize(out)
  444.     end
  445.     return oData,out,chs
  446. end
  447.  
  448. -- Checks for uAPI update!
  449. function checkUpdate()
  450.   if http then
  451.     local getGit = http.get("https://raw.github.com/dannysmc95/uprograms/master/uVersions")
  452.     local getGit = getGit.readAll()
  453.     NVersion = textutils.unserialize(getGit)
  454.     if NVersion["uapi"].Version > version then
  455.       getGit = http.get(NVersion["uapi"].GitURL)
  456.       getGit = getGit.readAll()
  457.       local file = fs.open("uapi", "w")
  458.       file.write(getGit)
  459.       file.close()
  460.       return true
  461.     end
  462.   else
  463.     return false
  464.   end
  465. end
  466.  
  467. -- File Compression
  468. local function readFile(filename)
  469.   local contents = {}
  470.   if fs.exists(filename) then
  471.     local file = fs.open(filename, "r")
  472.     while true do
  473.       line = file.readLine()
  474.       if line then
  475.         table.insert(contents, line)
  476.       else
  477.         file.close()
  478.         contents.filename = fs.getName(filename)
  479.         return contents
  480.       end
  481.     end
  482.   else
  483.     error("File doesn't exist.")
  484.   end
  485. end
  486.  
  487. local function prepFiles(dir)
  488.   if fs.exists(dir) and fs.isDir(dir) then
  489.     local files = fs.list(dir)
  490.     local contents = {}
  491.     contents.bunch = true
  492.     for _,name in pairs(files) do
  493.       if fs.isDir(name) == false then
  494.         contents[name] = readFile(dir..name)
  495.       end
  496.     end
  497.     return contents
  498.   else
  499.     error("Not a directory.")
  500.   end
  501. end
  502.  
  503. function unpackFiles(m, dir)
  504.   if (fs.exists(dir) == false) and (fs.isDir(dir) == false) then
  505.     error("Passed dir is not a dir or doesn't exist")
  506.   end
  507.   if type(m) == "table" then
  508.     if m.bunch then
  509.       for name,data in pairs(m) do
  510.         if type(data) == "table" then
  511.           file = fs.open(dir.. data.filename, "w")
  512.           for i = 1, #data do
  513.             file.writeLine(data[i])
  514.           end
  515.           file.close()
  516.         end
  517.       end
  518.     else
  519.       file = fs.open(dir.. m.filename, "w")
  520.       for i = 1, #m do
  521.         file.writeLine(m[i])
  522.       end
  523.       file.close()
  524.     end
  525.   else
  526.     error("Not a table.")
  527.   end
  528. end
  529.  
  530. function unpackFile(message, dir)
  531.   unpackFiles(message, dir)
  532. end
  533.  
  534. function packFile(filename)
  535.   local file = readFile(filename)
  536.   return file
  537. end
  538.  
  539. function packDir(dir)
  540.   local filer = prepFiles(dir)
  541.   return filer
  542. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement