Thunder7102

coreAPI

Jun 10th, 2014
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.66 KB | None | 0 0
  1. --[[
  2.     CoreAPI is designed to provide some of the regularly needed
  3.     features of general types of programs in one place.
  4.  
  5.     It is a mish-mash of functions where I wasn't sure where to place
  6.     elsewhere.
  7. ]]
  8.  
  9. version = "0.0.2"
  10.  
  11. --==Startup Functions==--
  12.  
  13. --Test This
  14. function setStartup(...)
  15.   --Takes any number of arguments and creates
  16.   --a startup script to execute the program (argument 1)
  17.   --with any following parameters (arguments afterwards)
  18.  
  19.   local args = {...}
  20.  
  21.   --Making sure I don't break a previous startup
  22.   if fs.exists("startup") then
  23.     fs.move("startup", "oldStartup")
  24.   end
  25.  
  26.   --Saving the params file
  27.   if not fs.exists("tempFiles/") then
  28.     fs.makeDir("tempFiles")
  29.   end
  30.  
  31.   local tempStartupParams = fs.open("tempFiles/tempStartupParams", "w")
  32.   tempStartupParams.write(textutils.serialize(args))
  33.   tempStartupParams.close()
  34.  
  35.   --Generating the startup file
  36.   local startupFile = fs.open("startup", "w")
  37.   startupFile.writeLine("--If you can read this, remove it")
  38.   startupFile.writeLine("local params = fs.open(\"tempFiles/tempStartupParams\", \"r\")")
  39.   startupFile.writeLine("local args = textutils.unserialize(params.readAll())")
  40.   startupFile.writeLine("params.close()")
  41.   startupFile.writeLine("shell.run(unpack(args))")
  42.   startupFile.close()
  43. end
  44.  
  45. function removeStartup()
  46.     --Removes the startup you created and sets old one
  47.     --(if it exists), back to normal place
  48.  
  49.     --Removes set startup and puts new one back
  50.     if fs.exists("oldStartup") then
  51.         if fs.exists("startup") then
  52.             fs.delete("startup")
  53.         end
  54.         fs.move("oldStartup", "startup")
  55.     end
  56.  
  57.     --Gets rid of the tempFiles Folder
  58.     if fs.exists("tempFiles") then
  59.         fs.delete("tempFiles")
  60.     end
  61. end
  62.  
  63. function writeFile(table, filePath)
  64.     if fs.exists(filePath) then
  65.         return false
  66.     else
  67.         local f = fs.open(filePath, "w")
  68.         f.write(textutils.serialize(table))
  69.         f.close()
  70.         return true
  71.     end
  72. end
  73.  
  74. function readFile(filePath)
  75.     --This assumes it was written to by this API
  76.     if fs.exists(filePath) then
  77.         local f = fs.open(filePath, "r")
  78.         local array = textutils.unserialize(f.readAll())
  79.         f.close()
  80.         return array
  81.     end
  82. end
  83.  
  84. --==Update Functions==--
  85. function pastebin(extension, fileName)
  86.     --This will return JUST the code for HTML
  87.     local pastebin = "http://www.pastebin.com/raw.php?i="..extension
  88.     if fs.exists(fileName) then return false
  89.     else
  90.         local file = fs.open(fileName, "w")
  91.         file.write(assert(http.get(pastebin)).readAll())
  92.         file.close()
  93.         return true
  94.     end
  95. end
  96.  
  97. function writeStartup(...)
  98.     --Generates a startup program which will automatically update
  99.     --programNames with URLs every reboot.
  100.  
  101.     --Work on this--
  102.  
  103. end
Advertisement
Add Comment
Please, Sign In to add comment