Advertisement
infiniteblock

Untitled

Dec 30th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. -- iOS - A shell for ComputerCraft computers
  2. -- Copyright (C) 2016-2017 Tulir Asokan
  3.  
  4. -- This program is free software: you can redistribute it and/or modify
  5. -- it under the terms of the GNU General Public License as published by
  6. -- the Free Software Foundation, either version 3 of the License, or
  7. -- (at your option) any later
  8.  
  9. -- This program is distributed in the hope that it will be useful,
  10. -- but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. -- GNU General Public License for more details.
  13.  
  14. -- You should have received a copy of the GNU General Public License
  15. -- along with this program. If not, see <http://www.gnu.org/licenses/>
  16.  
  17. -- Define the loadFile function that allows lua source files to be loaded
  18. function fatal(message)
  19. print("[Fatal] ", message)
  20. exit(1)
  21. end
  22.  
  23. function formatPath(base, fileName)
  24. if not base:sub(-1) ~= "/" then
  25. base = base .. "/"
  26. end
  27.  
  28. if fileName:sub(1, 1) == "/" then
  29. fileName = fileName:sub(-2)
  30. end
  31.  
  32. return base .. fileName
  33. end
  34.  
  35. function install(baseURL, directory, data)
  36. local versions = {}
  37. for name, value in pairs(data) do
  38. if type(value) == "number" then
  39. name = name .. ".lua"
  40. url = formatPath(baseURL, formatPath(directory, name))
  41. print("[Trace] ", "Downloading ", url)
  42. local conn = http.get(url)
  43. if conn then
  44. print("[Trace] ", "Reading ", url)
  45. local fileData = conn.readAll()
  46. conn.close()
  47. if fileData then
  48. print("[Trace] ", "Writing ", formatPath(directory, name))
  49. local file = fs.open(formatPath(directory, name), "w")
  50. file.write(fileData)
  51. file.close()
  52. versions[name] = value
  53. print("[Info] ", formatPath(directory, name:sub(1, -5)), " installed.")
  54. else
  55. print("[Error] ", "Failed to read ", url)
  56. end
  57. else
  58. print("[Error] ", "Failed to download ", url)
  59. end
  60. elseif type(value) == "table" then
  61. local path = formatPath(directory, name)
  62. print("[Trace] ", "Creating ", path)
  63. fs.makeDir(path)
  64. print("[Trace] ", "Recursing into ", path)
  65. versions[name] = install(baseURL, path, value)
  66. else
  67. print("[Warning] ", "Unknown file definition value type: ", value)
  68. end
  69. end
  70. end
  71.  
  72. local dataPath = "https://raw.githubusercontent.com/tulir/iOS/blob/master/ios.manifest"
  73.  
  74. local args = {...}
  75. if table.getn(args) > 0 then
  76. dataPath = args[0]
  77. end
  78.  
  79. print("[Debug] ", "Downloading file manifest")
  80. local conn = http.get(dataPath)
  81. if not conn then
  82. fatal("Failed to download file manifest")
  83. end
  84.  
  85. local rawData = conn.readAll()
  86. conn.close()
  87. if not rawData then
  88. fatal("Failed to download read manifest")
  89. end
  90.  
  91. local data = textutils.unserialize(rawData)
  92. if not data then
  93. fatal("Failed to download parse manifest")
  94. end
  95.  
  96. print("[Debug] ", "Installing files")
  97. local installedData = {
  98. files = install(data.baseURL, "", data.files),
  99. baseURL = data.baseURL,
  100. version = data.version,
  101. updatePath = dataPath
  102. }
  103.  
  104. print("[Debug] ", "Writing manifest")
  105. versionFile = fs.open("ios.manifest", "w")
  106. versionFile.write(textutils.serialize(installedData))
  107. versionFile.close()
  108.  
  109. print("[Debug] ", "Creating startup file")
  110. startupFile = fs.open("startup", "w")
  111. startupFile.write("os.run({}, \"ios.lua\")")
  112. startupFile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement