Advertisement
BrunoZockt

SupHa - Startup Handler

Sep 15th, 2018
35,995
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.93 KB | None | 0 0
  1. --[[startup handler by BrunoZockt
  2.  
  3. MIT License
  4.  
  5. Copyright (c) 2018 Bruno Heberle
  6.  
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13.  
  14. The above copyright notice and this permission notice shall be included in all
  15. copies or substantial portions of the Software.
  16.  
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. SOFTWARE.
  24. ]]--
  25.  
  26. local programs = {
  27.   ["standart"] = ""
  28. }
  29.  
  30. local color = term.isColor()
  31.  
  32. name = "SupHa - Startup Handler by BrunoZockt"
  33.  
  34. local function uploadList()
  35.   local file = fs.open("database/startup", "w")
  36.   file.writeLine(textutils.serialize(programs))
  37.   file.close()
  38. end
  39.  
  40. function getList(terminal)
  41.   local function downloadList()
  42.     local programs = {["standart"] = ""}
  43.     if fs.exists("database/startup") then
  44.       local file = fs.open("database/startup", "r")
  45.       programs = textutils.unserialize(file.readAll())
  46.       file.close()
  47.     end
  48.     return programs
  49.   end
  50.  
  51.   local programs = downloadList()
  52.   if terminal then
  53.     print(textutils.serialize(programs))
  54.   else
  55.     return programs
  56.   end
  57. end
  58.  
  59. function set(path, position, terminal)
  60.   programs = getList()
  61.   local result = nil
  62.   if fs.exists(path) == true then
  63.     if position == "standart" then
  64.       programs["standart"] = path
  65.     else
  66.       table.insert(programs, (tonumber(position) or #programs+1), path)
  67.     end
  68.     if terminal then
  69.       print("Success!")
  70.       print(textutils.serialize(programs))
  71.     else
  72.       result = true
  73.     end
  74.   else
  75.     if terminal then
  76.       if color then
  77.         term.setTextColor(colors.red)
  78.       end
  79.       print("Error: could not find path")
  80.       if color then
  81.         term.setTextColor(colors.white)
  82.       end
  83.     else
  84.       result = false
  85.     end
  86.   end
  87.   uploadList()
  88.   return result
  89. end
  90.  
  91. function remove(path, terminal)
  92.   programs = getList()
  93.   for k, v in ipairs(programs) do
  94.     if v == path then
  95.       table.remove(programs, k)
  96.       if terminal then
  97.         print("Success!")
  98.         print(textutils.serialize(programs))
  99.       end
  100.       uploadList()
  101.       return true
  102.     end
  103.   end
  104.   if terminal then
  105.     if color then
  106.       term.setTextColor(colors.red)
  107.     end
  108.     print("Error: "..path.." was not found on the list")
  109.     if color then
  110.       term.setTextColor(colors.white)
  111.     end
  112.   end
  113.   return false
  114. end
  115.  
  116. local function shelly()
  117.   shell.path()
  118. end
  119.  
  120. if pcall(shelly) == true then
  121.   local args = {...}
  122.   if args[1] == "getList" then
  123.     getList(true)
  124.   elseif args[1] == "set" then
  125.     set(args[2], args[3], true)
  126.   elseif args[1] == "remove" then
  127.     remove(args[2], true)
  128.   elseif #args == 0 then
  129.     programs = getList()
  130.     local program = ""
  131.     if fs.exists(programs["standart"]) then
  132.       program = programs["standart"]
  133.     end
  134.  
  135.     for i = 1, #programs do
  136.       if fs.exists(programs[i]) then
  137.         if fs.exists("database/"..programs[i].."/resume") then
  138.           program = programs[i]
  139.           break
  140.         end
  141.       end
  142.     end
  143.  
  144.     shell.run(program)
  145.   else
  146.     if color then
  147.       term.setTextColor(colors.red)
  148.     end
  149.     print("Error: wrong argument: "..args[1])
  150.     if color then
  151.       term.setTextColor(colors.white)
  152.     end
  153.   end
  154. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement