Advertisement
Saldor010

rmplane Installer

Apr 26th, 2019
1,479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.85 KB | None | 0 0
  1. -- ElvishJerricco's JSON parser
  2. -- http://www.computercraft.info/forums2/index.php?/topic/5854-json-api-v201-for-computercraft/
  3. ------------------------------------------------------------------ utils
  4. local controls = {["\n"]="\\n", ["\r"]="\\r", ["\t"]="\\t", ["\b"]="\\b", ["\f"]="\\f", ["\""]="\\\"", ["\\"]="\\\\"}
  5.  
  6. local function isArray(t)
  7.     local max = 0
  8.     for k,v in pairs(t) do
  9.         if type(k) ~= "number" then
  10.             return false
  11.         elseif k > max then
  12.             max = k
  13.         end
  14.     end
  15.     return max == #t
  16. end
  17.  
  18. local whites = {['\n']=true; ['\r']=true; ['\t']=true; [' ']=true; [',']=true; [':']=true}
  19. function removeWhite(str)
  20.     while whites[str:sub(1, 1)] do
  21.         str = str:sub(2)
  22.     end
  23.     return str
  24. end
  25.  
  26. ------------------------------------------------------------------ encoding
  27.  
  28. local function encodeCommon(val, pretty, tabLevel, tTracking)
  29.     local str = ""
  30.  
  31.     -- Tabbing util
  32.     local function tab(s)
  33.         str = str .. ("\t"):rep(tabLevel) .. s
  34.     end
  35.  
  36.     local function arrEncoding(val, bracket, closeBracket, iterator, loopFunc)
  37.         str = str .. bracket
  38.         if pretty then
  39.             str = str .. "\n"
  40.             tabLevel = tabLevel + 1
  41.         end
  42.         for k,v in iterator(val) do
  43.             tab("")
  44.             loopFunc(k,v)
  45.             str = str .. ","
  46.             if pretty then str = str .. "\n" end
  47.         end
  48.         if pretty then
  49.             tabLevel = tabLevel - 1
  50.         end
  51.         if str:sub(-2) == ",\n" then
  52.             str = str:sub(1, -3) .. "\n"
  53.         elseif str:sub(-1) == "," then
  54.             str = str:sub(1, -2)
  55.         end
  56.         tab(closeBracket)
  57.     end
  58.  
  59.     -- Table encoding
  60.     if type(val) == "table" then
  61.         assert(not tTracking[val], "Cannot encode a table holding itself recursively")
  62.         tTracking[val] = true
  63.         if isArray(val) then
  64.             arrEncoding(val, "[", "]", ipairs, function(k,v)
  65.                 str = str .. encodeCommon(v, pretty, tabLevel, tTracking)
  66.             end)
  67.         else
  68.             arrEncoding(val, "{", "}", pairs, function(k,v)
  69.                 assert(type(k) == "string", "JSON object keys must be strings", 2)
  70.                 str = str .. encodeCommon(k, pretty, tabLevel, tTracking)
  71.                 str = str .. (pretty and ": " or ":") .. encodeCommon(v, pretty, tabLevel, tTracking)
  72.             end)
  73.         end
  74.     -- String encoding
  75.     elseif type(val) == "string" then
  76.         str = '"' .. val:gsub("[%c\"\\]", controls) .. '"'
  77.     -- Number encoding
  78.     elseif type(val) == "number" or type(val) == "boolean" then
  79.         str = tostring(val)
  80.     else
  81.         error("JSON only supports arrays, objects, numbers, booleans, and strings", 2)
  82.     end
  83.     return str
  84. end
  85.  
  86. function encode(val)
  87.     return encodeCommon(val, false, 0, {})
  88. end
  89.  
  90. function encodePretty(val)
  91.     return encodeCommon(val, true, 0, {})
  92. end
  93.  
  94. ------------------------------------------------------------------ decoding
  95.  
  96. local decodeControls = {}
  97. for k,v in pairs(controls) do
  98.     decodeControls[v] = k
  99. end
  100.  
  101. function parseBoolean(str)
  102.     if str:sub(1, 4) == "true" then
  103.         return true, removeWhite(str:sub(5))
  104.     else
  105.         return false, removeWhite(str:sub(6))
  106.     end
  107. end
  108.  
  109. function parseNull(str)
  110.     return nil, removeWhite(str:sub(5))
  111. end
  112.  
  113. local numChars = {['e']=true; ['E']=true; ['+']=true; ['-']=true; ['.']=true}
  114. function parseNumber(str)
  115.     local i = 1
  116.     while numChars[str:sub(i, i)] or tonumber(str:sub(i, i)) do
  117.         i = i + 1
  118.     end
  119.     local val = tonumber(str:sub(1, i - 1))
  120.     str = removeWhite(str:sub(i))
  121.     return val, str
  122. end
  123.  
  124. function parseString(str)
  125.     str = str:sub(2)
  126.     local s = ""
  127.     while str:sub(1,1) ~= "\"" do
  128.         local next = str:sub(1,1)
  129.         str = str:sub(2)
  130.         assert(next ~= "\n", "Unclosed string")
  131.  
  132.         if next == "\\" then
  133.             local escape = str:sub(1,1)
  134.             str = str:sub(2)
  135.  
  136.             next = assert(decodeControls[next..escape], "Invalid escape character")
  137.         end
  138.  
  139.         s = s .. next
  140.     end
  141.     return s, removeWhite(str:sub(2))
  142. end
  143.  
  144. function parseArray(str)
  145.     str = removeWhite(str:sub(2))
  146.  
  147.     local val = {}
  148.     local i = 1
  149.     while str:sub(1, 1) ~= "]" do
  150.         local v = nil
  151.         v, str = parseValue(str)
  152.         val[i] = v
  153.         i = i + 1
  154.         str = removeWhite(str)
  155.     end
  156.     str = removeWhite(str:sub(2))
  157.     return val, str
  158. end
  159.  
  160. function parseObject(str)
  161.     str = removeWhite(str:sub(2))
  162.  
  163.     local val = {}
  164.     while str:sub(1, 1) ~= "}" do
  165.         local k, v = nil, nil
  166.         k, v, str = parseMember(str)
  167.         val[k] = v
  168.         str = removeWhite(str)
  169.     end
  170.     str = removeWhite(str:sub(2))
  171.     return val, str
  172. end
  173.  
  174. function parseMember(str)
  175.     local k = nil
  176.     k, str = parseValue(str)
  177.     local val = nil
  178.     val, str = parseValue(str)
  179.     return k, val, str
  180. end
  181.  
  182. function parseValue(str)
  183.     local fchar = str:sub(1, 1)
  184.     if fchar == "{" then
  185.         return parseObject(str)
  186.     elseif fchar == "[" then
  187.         return parseArray(str)
  188.     elseif tonumber(fchar) ~= nil or numChars[fchar] then
  189.         return parseNumber(str)
  190.     elseif str:sub(1, 4) == "true" or str:sub(1, 5) == "false" then
  191.         return parseBoolean(str)
  192.     elseif fchar == "\"" then
  193.         return parseString(str)
  194.     elseif str:sub(1, 4) == "null" then
  195.         return parseNull(str)
  196.     end
  197.     return nil
  198. end
  199.  
  200. function decode(str)
  201.     str = removeWhite(str)
  202.     t = parseValue(str)
  203.     return t
  204. end
  205.  
  206. function decodeFromFile(path)
  207.     local file = assert(fs.open(path, "r"))
  208.     local decoded = decode(file.readAll())
  209.     file.close()
  210.     return decoded
  211. end
  212. -- End of JSON parser
  213.  
  214. -- Ramuthra's plane installer
  215. local github = "https://api.github.com/repos/Saldor010/Ramuthras-Plane/contents"
  216. local cobaltGit = "https://api.github.com/repos/ebernerd/Cobalt/contents"
  217. local args = {...}
  218. if args[1] then
  219.     if not type(args[1]) == "string" then
  220.         args[1] = nil
  221.     elseif string.find(args[1],string.len(args[1]),string.len(args[1])) ~= "/" and string.find(args[1],string.len(args[1]),string.len(args[1])) ~= "\\" then
  222.         args[1] = args[1].."/"
  223.     end
  224. end
  225. local pathToInstall = args[1] or "RamuthrasPlane/"
  226. local token = args[2]
  227.  
  228. local function downloadFile(url,filePath)  
  229.     local fileContent = http.get(url)
  230.     if fileContent then fileContent = fileContent.readAll() else return false end
  231.    
  232.     if not fs.exists(filePath) then
  233.         local file = fs.open(filePath,"w")
  234.         file.write(fileContent)
  235.         file.close()
  236.         return true
  237.     else return false end
  238. end
  239.  
  240. local function readRepository(url,path2,verify)
  241.     if not path2 then path2 = "" end
  242.     --[[term.setTextColor(colors.red)
  243.     term.clear()]]--
  244.     term.setTextColor(colors.blue)
  245.    
  246.     --print(url)
  247.     local a = nil
  248.     if token then
  249.         a = {
  250.             ["Authorization"] = "token "..tostring(token)
  251.         }
  252.     end
  253.     local repository = http.get(url,a)
  254.     if repository == nil then
  255.         term.setTextColor(colors.red)
  256.         print("Failed to reach repository.")
  257.         term.setTextColor(colors.orange)
  258.         print("You may have exceeded GitHub's rate limit. Please try again later or pass an OAuth token as the 2nd argument.")
  259.         error()
  260.     end
  261.     if repository then repository = repository.readAll() else return false end
  262.     repository = decode(repository)
  263.     --print(repository)
  264.    
  265.     for k,v in pairs(repository) do
  266.         --print("a")
  267.         if v["download_url"] then
  268.             --[[term.setCursorPos(1,7)
  269.             term.setTextColor(colors.red)
  270.             term.write(string.rep(" ",51))
  271.             term.setTextColor(colors.orange)
  272.             term.setCursorPos(math.floor(51/2)-math.floor(string.len("Downloading file:")/2),7)
  273.             term.write("Downloading file:")
  274.            
  275.             term.setCursorPos(1,8)
  276.             term.setTextColor(colors.red)
  277.             term.write(string.rep(" ",51))
  278.             term.setTextColor(colors.orange)]]--
  279.            
  280.             sleep(0.2) -- Give time for the user to actually read the message
  281.            
  282.             local success = nil
  283.             --[[if string.sub(v["url"],1,70) == "https://api.github.com/repos/Saldor010/Ramuthras-Plane/contents/cobalt" or string.sub(v["url"],1,74) == "https://api.github.com/repos/Saldor010/Ramuthras-Plane/contents/cobalt-lib" or string.sub(v["url"],1,73) == "https://api.github.com/repos/Saldor010/Ramuthras-Plane/contents/cobalt-ui" then
  284.                 term.setCursorPos(math.floor(51/2)-math.floor(string.len(v["path"])/2),8)
  285.                 term.write(pathToInstall..v["path"])
  286.                 print(v["path"])
  287.                 success = downloadFile(v["download_url"],v["path"])
  288.             else]]--
  289.                 --term.setCursorPos(math.floor(51/2)-math.floor(string.len(pathToInstall..v["path"])/2),8)
  290.                 --term.write(pathToInstall..v["path"])
  291.                 if verify(v["download_url"]) then
  292.                     print(path2..v["path"])
  293.                     success = downloadFile(v["download_url"],path2..v["path"])
  294.                 end
  295.             --end
  296.         else
  297.             readRepository(v["url"],path2,verify)
  298.         end
  299.     end
  300. end
  301.  
  302. if token then
  303.     term.setTextColor(colors.orange)
  304.     print("Token "..token.." will be used during the installation.")
  305.     term.setTextColor(colors.blue)
  306. end
  307.  
  308. --readRepository("https://www.google.com","A")
  309. readRepository(github,pathToInstall,function(download_url) return true end)
  310. readRepository(cobaltGit,"",function(download_url)
  311.     --print(download_url)
  312.     if string.sub(download_url,58,66) == "cobalt-ui" or string.sub(download_url,58,67) == "cobalt-lib" or string.sub(download_url,58,63) == "cobalt" then
  313.         return true
  314.     else
  315.         return false
  316.     end
  317. end)
  318.  
  319. --[[term.clear()
  320. term.setCursorPos(1,1)
  321. term.setTextColor(colors.orange)]]--
  322. term.setTextColor(colors.green)
  323. print("Download complete! Run "..pathToInstall.."rmplane.lua to start the game!")
  324. term.setTextColor(colors.white)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement