Sectly_Playz

Untitled

Apr 29th, 2023 (edited)
939
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.43 KB | None | 0 0
  1. -- Sectly's Market System!
  2. -- By: Sectly/TEC_NO
  3. if not term.isColor() then
  4.   error("Sectly's Market System is incompatible with regular "..(turtle and "turtles" or "computers").."!",0)
  5. end
  6.  
  7. local tArgs = {...}
  8. local minification = tArgs[2] ~= "-t" --trims all indentation from the files to reduce the file size by roughly 25%
  9. local folder = tArgs[1] or ""
  10. local tContents = {}
  11. local getContents
  12. local tSha = {}
  13. local tIgnore = { --files to ignore
  14.   ["README.md"] = true
  15. }
  16.  
  17. local prevLines
  18. local termX,termY = term.getSize()
  19. local midY = math.floor(termY/2)
  20. local print = function(text)
  21.   local tLines = {}
  22.   while #text > 0 do  --splits text into a table containing each line
  23.     local line = text:sub(1,termX)
  24.     local newLine = string.find(line.."","\n") --check for new line character
  25.     if newLine then
  26.       line = line:sub(1,newLine-1)
  27.       text = text:sub(#line+2,#text)
  28.     elseif #line == termX then
  29.       local endSpace = line:find"%s$" or line:find"%s%S-$" or termX
  30.       line = line:sub(1,endSpace)
  31.       text = text:sub(#line+1)
  32.     else
  33.       text = ""
  34.     end
  35.     line = string.rep(" ",math.max(math.floor((termX-#line)/2),0))..line
  36.     line = line..string.rep(" ",math.max(termX-#line,0))
  37.     tLines[#tLines+1] = line
  38.   end
  39.   if prevLines and #prevLines > #tLines then
  40.     for i = 1,#prevLines do
  41.       if not tLines[i] then
  42.         term.setCursorPos(1,midY-math.floor(#prevLines/2)+i)
  43.         term.clearLine()
  44.       end
  45.     end
  46.   end
  47.   for i,line in ipairs(tLines) do
  48.     term.setCursorPos(1,midY-math.floor((#tLines/2))+i)
  49.     term.write(line)
  50.   end
  51.   prevLines = tLines
  52. end
  53.  
  54. getContents = function(path)
  55.   path = path or ""
  56.   os.setComputerLabel("Saying Hi To Github.com...")
  57.   print("Scanning Github repo\n"..path.."\nfor files...")
  58.   local web = http.get("https://api.github.com/repos/Sectly/SectlysMarketSystem/contents"..path, { ["Authorization"] = "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" })
  59.   assert(web,(path ~= "" and "Error: Failed to get contents of "..path) or "Error: Github download limit exceeded")
  60.   local sContents = web.readAll()
  61.   web.close()
  62.   local _s,remainder = sContents:find'"name":"'
  63.   local name = sContents:match'"name":"(.-)"'
  64.   while name do
  65.     if tIgnore[name] then
  66.       sContents = sContents:sub(remainder)..""
  67.     else
  68.       sContents = sContents:sub(remainder)..""
  69.       local url = sContents:match'html_url":"(.-)"'
  70.       assert(url,"Error: Failed to get the url of "..path.."/"..name)
  71.       url = url:gsub("https://","https://raw.")
  72.       url = url:gsub("blob/","")
  73.       local fileName = folder..path.."/"..name
  74.       if sContents:match'"type":"(.-)"' == "file" then
  75.         tContents[fileName] = url
  76.         tSha[fileName] = sContents:match'"sha":"(.-)"'
  77.       else
  78.         if not fs.exists(fileName) then
  79.           fs.makeDir(fileName)
  80.         end
  81.         getContents(path.."/"..name)
  82.       end
  83.     end
  84.     _s,remainder = sContents:find'"name":"'
  85.     name = sContents:match'"name":"(.-)"'
  86.   end
  87. end
  88. local createPath
  89. term.setTextColor(colors.white)
  90. term.setBackgroundColor(colors.black)
  91. term.clear()
  92. getContents()
  93. for file,url in pairs(tContents) do
  94.   if not file:match"README.md" then
  95.     local saveFile = fs.open(file:match"smsr.Lua" and file:sub(1,#file-4) or file,"w")
  96.     print("Downloading\n"..url)
  97.     local webFile
  98.     while not webFile do
  99.       webFile = http.get(url)
  100.       if not webFile then
  101.         print("Download failed. Retry? (Y/N)")
  102.         while true do
  103.           local _e,key = os.pullEvent"key"
  104.           if key == 21 then --Y
  105.             print"Retrying..."
  106.             break
  107.           elseif key == 49 then --N
  108.             print"Installation canceled"
  109.             saveFile.close()
  110.             return
  111.           end
  112.         end
  113.       end
  114.     end
  115.     if file:match"smsr.Lua" then --main file can't be minified
  116.       saveFile.write(webFile.readAll())
  117.     else
  118.       local line = webFile.readLine()
  119.       while line do
  120.         saveFile.write((minification and (line:match"(%S.*)$" or "") or line).."\n")
  121.         line = webFile.readLine()
  122.       end
  123.     end
  124.     saveFile.close()
  125.     webFile.close()
  126.   end
  127. end
  128. local file = fs.open(folder.."/".."smsr.Lua","w")
  129. file.write(textutils.serialize(tSha))
  130. file.close()
  131. print("Sucessfully Installed Sectly's Market System\nType "..(folder ~= "" and folder ~= "/" and folder.."/" or "").."smsr to run it.")
  132. os.setComputerLabel("Done!")
Advertisement
Add Comment
Please, Sign In to add comment