Saldor010

ImpendingDoomInstaller

Nov 27th, 2017
1,004
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.22 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/Impending-Doom/contents"
  216. local args = {...}
  217. if args[1] then
  218.     if not type(args[1]) == "string" then
  219.         args[1] = nil
  220.     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
  221.         args[1] = args[1].."/"
  222.     end
  223. end
  224. local pathToInstall = args[1] or "ImpendingDoom/"
  225.  
  226. local function downloadFile(url,filePath)  
  227.     local fileContent = http.get(url)
  228.     if fileContent then fileContent = fileContent.readAll() else return false end
  229.    
  230.     if not fs.exists(filePath) then
  231.         local file = fs.open(filePath,"w")
  232.         file.write(fileContent)
  233.         file.close()
  234.         return true
  235.     else
  236.         if term.isColor() then term.setTextColor(colors.red) end
  237.         local wD,hT = term.getSize()
  238.         local x,y = term.getCursorPos()
  239.         term.setCursorPos(1,hT)
  240.         term.write("File already exists. Overwrite? (y/n)")
  241.         local overwrite = false
  242.         while true do
  243.             local ev,p1,p2,p3,p4,p5 = os.pullEvent()
  244.             if ev == "char" and string.lower(p1) == "y" then
  245.                 overwrite = true
  246.                 break
  247.             elseif ev == "char" and string.lower(p1) == "n" then
  248.                 overwrite = false
  249.                 break
  250.             end
  251.         end
  252.         term.setCursorPos(1,hT)
  253.         term.write(string.rep(" ",wD))
  254.         term.setCursorPos(x,y)
  255.        
  256.         if overwrite then
  257.             local file = fs.open(filePath,"w")
  258.             file.write(fileContent)
  259.             file.close()
  260.             return true
  261.         else return false end
  262.     return false end
  263. end
  264.  
  265. local function readRepository(url)
  266.     local repository = http.get(url)
  267.     if repository then repository = repository.readAll() else return false end
  268.     repository = decode(repository)
  269.    
  270.     for k,v in pairs(repository) do
  271.         if v["download_url"] then
  272.             local success = nil
  273.             --[[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
  274.                 term.setCursorPos(math.floor(51/2)-math.floor(string.len(v["path"])/2),8)
  275.                 term.write(pathToInstall..v["path"])
  276.                 success = downloadFile(v["download_url"],v["path"])
  277.             else]]--
  278.             if term.isColor() then term.setTextColor(128) end
  279.             write("Downloading "..v["path"]..".. ")
  280.             success = downloadFile(v["download_url"],pathToInstall..v["path"])
  281.             if success then
  282.                 if term.isColor() then term.setTextColor(colors.lime) end
  283.                 print("Success")
  284.             else
  285.                 if term.isColor() then term.setTextColor(colors.red) end
  286.                 print("Failed")
  287.             end
  288.             --end
  289.         else
  290.             readRepository(v["url"])
  291.         end
  292.     end
  293. end
  294. if not term.isColor() then
  295.     print("Warning! The program you're about to download cannot be run on this computer. Proceed anyways? (y/n)")
  296.     local proceed = false
  297.     while true do
  298.         local ev,p1,p2,p3,p4,p5 = os.pullEvent()
  299.         if ev == "char" and string.lower(p1) == "y" then
  300.             proceed = true
  301.             break
  302.         elseif ev == "char" and string.lower(p1) == "n" then
  303.             proceed = false
  304.             break
  305.         end
  306.     end
  307.     if proceed == false then print("Download cancelled.") error() end
  308. end
  309. readRepository(github)
  310.  
  311. if term.isColor() then term.setTextColor(colors.orange) end
  312. print("Download complete! Run "..pathToInstall.."coldwar.lua to start the game!")
Add Comment
Please, Sign In to add comment