Advertisement
LDDestroier

STD-GUI Default List (github)

Jan 25th, 2018
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.49 KB | None | 0 0
  1. std = std or {}
  2. local verbose = true
  3.  
  4. std.aliases = {
  5.     ldd = "~f&1LDD&8estroier",
  6.     eldidi = "~f&1Eldidi&8Stroyrr",
  7.     oeed = "~d&0oeed",
  8.     lur = "~b&fLur",
  9.     theoriginalbit = "&ftheoriginal&3bit",
  10.     cloudninja = "~b&0Cloud Ninja",
  11.     run = "&0~dRUN",
  12.     download = "&0~dDOWNLOAD"
  13. }
  14.  
  15. for k,v in pairs(std.aliases) do std.aliases[k] = v.."&r~r" end
  16.  
  17. std.storeCatagoryNames = {
  18.     [1] = "Utility",            -- A general utility that doesn't fit into other categories.
  19.     [2] = "Pocket",             -- Tailored specifically for pocket computers.
  20.     [3] = "Game",               -- An actual game, not an animation or whatever.
  21.     [4] = "Toy",                -- This is where animations and whatnot would go.
  22.     [5] = "Operating System",   -- Links to an operating system install.
  23.     [6] = "Networking",         -- More specific utilities that use modems.
  24.     [7] = "Malicious",          -- Malicious code. Bad stuff.
  25.     [8] = "API",                -- Code to help with code? What sorcery!
  26.     [9] = "Turtle",             -- For turtles, using the Turtle API.
  27.     [10] = "Command",           -- If it uses the command API in its core utility.
  28.     [11] = "HTTP",              -- If it is centered around HTTP, put it here.
  29.     [12] = "Error!"             -- if it is a broken STD-GUI entry...
  30. }
  31.  
  32. std.storeCatagoryColors = {
  33.     [1] = {
  34.         txt = colors.black,
  35.         bg = colors.pink
  36.     },
  37.     [2] = {
  38.         txt = colors.white,
  39.         bg = colors.red
  40.     },
  41.     [3] = {
  42.         txt = colors.lightBlue,
  43.         bg = colors.blue
  44.     },
  45.     [4] = {
  46.         txt = colors.white,
  47.         bg = colors.green
  48.     },
  49.     [5] = {
  50.         txt = colors.black,
  51.         bg = colors.white
  52.     },
  53.     [6] = {
  54.         txt = colors.white,
  55.         bg = colors.purple
  56.     },
  57.     [7] = {
  58.         txt = colors.red,
  59.         bg = colors.orange
  60.     },
  61.     [8] = {
  62.         txt = colors.black,
  63.         bg = colors.yellow
  64.     },
  65.     [9] = {
  66.         txt = colors.white,
  67.         bg = colors.brown
  68.     },
  69.     [10] = {
  70.         txt = colors.gray,
  71.         bg = colors.lime
  72.     },
  73.     [11] = {
  74.         txt = colors.white,
  75.         bg = colors.cyan
  76.     },
  77.     [12] = {
  78.         txt = colors.white,
  79.         bg = colors.red
  80.     },
  81. }
  82.  
  83. -- thank you ElvishJerricco for this JSON API
  84. local json = {}
  85. local controls = {["\n"]="\\n", ["\r"]="\\r", ["\t"]="\\t", ["\b"]="\\b", ["\f"]="\\f", ["\""]="\\\"", ["\\"]="\\\\"}
  86. local function isArray(t)
  87.     local max = 0
  88.     for k,v in pairs(t) do
  89.         if type(k) ~= "number" then
  90.             return false
  91.         elseif k > max then
  92.             max = k
  93.         end
  94.     end
  95.     return max == #t
  96. end
  97. local whites = {['\n']=true; ['\r']=true; ['\t']=true; [' ']=true; [',']=true; [':']=true}
  98. function json.removeWhite(str)
  99.     while whites[str:sub(1, 1)] do
  100.         str = str:sub(2)
  101.     end
  102.     return str
  103. end
  104. local function encodeCommon(val, pretty, tabLevel, tTracking)
  105.     local str = ""
  106.     local function tab(s)
  107.         str = str .. ("\t"):rep(tabLevel) .. s
  108.     end
  109.     local function arrEncoding(val, bracket, closeBracket, iterator, loopFunc)
  110.         str = str .. bracket
  111.         if pretty then
  112.             str = str .. "\n"
  113.             tabLevel = tabLevel + 1
  114.         end
  115.         for k,v in iterator(val) do
  116.             tab("")
  117.             loopFunc(k,v)
  118.             str = str .. ","
  119.             if pretty then str = str .. "\n" end
  120.         end
  121.         if pretty then
  122.             tabLevel = tabLevel - 1
  123.         end
  124.         if str:sub(-2) == ",\n" then
  125.             str = str:sub(1, -3) .. "\n"
  126.         elseif str:sub(-1) == "," then
  127.             str = str:sub(1, -2)
  128.         end
  129.         tab(closeBracket)
  130.     end
  131.     if type(val) == "table" then
  132.         assert(not tTracking[val], "Cannot encode a table holding itself recursively")
  133.         tTracking[val] = true
  134.         if isArray(val) then
  135.             arrEncoding(val, "[", "]", ipairs, function(k,v)
  136.                 str = str .. encodeCommon(v, pretty, tabLevel, tTracking)
  137.             end)
  138.         else
  139.             arrEncoding(val, "{", "}", pairs, function(k,v)
  140.                 assert(type(k) == "string", "JSON object keys must be strings", 2)
  141.                 str = str .. encodeCommon(k, pretty, tabLevel, tTracking)
  142.                 str = str .. (pretty and ": " or ":") .. encodeCommon(v, pretty, tabLevel, tTracking)
  143.             end)
  144.         end
  145.     elseif type(val) == "string" then
  146.         str = '"' .. val:gsub("[%c\"\\]", controls) .. '"'
  147.     elseif type(val) == "number" or type(val) == "boolean" then
  148.         str = tostring(val)
  149.     else
  150.         error("JSON only supports arrays, objects, numbers, booleans, and strings", 2)
  151.     end
  152.     return str
  153. end
  154. function json.encode(val)
  155.     return encodeCommon(val, false, 0, {})
  156. end
  157. function json.encodePretty(val)
  158.     return encodeCommon(val, true, 0, {})
  159. end
  160. local decodeControls = {}
  161. for k,v in pairs(controls) do
  162.     decodeControls[v] = k
  163. end
  164. function json.parseBoolean(str)
  165.     if str:sub(1, 4) == "true" then
  166.         return true, json.removeWhite(str:sub(5))
  167.     else
  168.         return false, json.removeWhite(str:sub(6))
  169.     end
  170. end
  171. function json.parseNull(str)
  172.     return nil, json.removeWhite(str:sub(5))
  173. end
  174. local numChars = {['e']=true; ['E']=true; ['+']=true; ['-']=true; ['.']=true}
  175. function json.parseNumber(str)
  176.     local i = 1
  177.     while numChars[str:sub(i, i)] or tonumber(str:sub(i, i)) do
  178.         i = i + 1
  179.     end
  180.     local val = tonumber(str:sub(1, i - 1))
  181.     str = json.removeWhite(str:sub(i))
  182.     return val, str
  183. end
  184. function json.parseString(str)
  185.     str = str:sub(2)
  186.     local s = ""
  187.     while str:sub(1,1) ~= "\"" do
  188.         local next = str:sub(1,1)
  189.         str = str:sub(2)
  190.         assert(next ~= "\n", "Unclosed string")
  191.  
  192.         if next == "\\" then
  193.             local escape = str:sub(1,1)
  194.             str = str:sub(2)
  195.  
  196.             next = assert(decodeControls[next..escape], "Invalid escape character")
  197.         end
  198.  
  199.         s = s .. next
  200.     end
  201.     return s, json.removeWhite(str:sub(2))
  202. end
  203. function json.parseArray(str)
  204.     str = json.removeWhite(str:sub(2))
  205.  
  206.     local val = {}
  207.     local i = 1
  208.     while str:sub(1, 1) ~= "]" do
  209.         local v = nil
  210.         v, str = json.parseValue(str)
  211.         val[i] = v
  212.         i = i + 1
  213.         str = json.removeWhite(str)
  214.     end
  215.     str = json.removeWhite(str:sub(2))
  216.     return val, str
  217. end
  218. function json.parseObject(str)
  219.     str = json.removeWhite(str:sub(2))
  220.  
  221.     local val = {}
  222.     while str:sub(1, 1) ~= "}" do
  223.         local k, v = nil, nil
  224.         k, v, str = json.parseMember(str)
  225.         val[k] = v
  226.         str = json.removeWhite(str)
  227.     end
  228.     str = json.removeWhite(str:sub(2))
  229.     return val, str
  230. end
  231. function json.parseMember(str)
  232.     local k = nil
  233.     k, str = json.parseValue(str)
  234.     local val = nil
  235.     val, str = json.parseValue(str)
  236.     return k, val, str
  237. end
  238. function json.parseValue(str)
  239.     local fchar = str:sub(1, 1)
  240.     if fchar == "{" then
  241.         return json.parseObject(str)
  242.     elseif fchar == "[" then
  243.         return json.parseArray(str)
  244.     elseif tonumber(fchar) ~= nil or numChars[fchar] then
  245.         return json.parseNumber(str)
  246.     elseif str:sub(1, 4) == "true" or str:sub(1, 5) == "false" then
  247.         return json.parseBoolean(str)
  248.     elseif fchar == "\"" then
  249.         return json.parseString(str)
  250.     elseif str:sub(1, 4) == "null" then
  251.         return json.parseNull(str)
  252.     end
  253.     return nil
  254. end
  255. function json.decode(str)
  256.     str = json.removeWhite(str)
  257.     t = json.parseValue(str)
  258.     return t
  259. end
  260. function json.decodeFromFile(path)
  261.     local file = assert(fs.open(path, "r"))
  262.     local decoded = json.decode(file.readAll())
  263.     file.close()
  264.     return decoded
  265. end
  266.  
  267. local getTableSize = function(tbl)
  268.     local output = 0
  269.     for k,v in pairs(tbl) do
  270.         output = output + 1
  271.     end
  272.     return output
  273. end
  274.  
  275. local function getSTDStoreList(files) --thanks squiddev
  276.     if not files then return end
  277.     local urls = {}
  278.     local remaining = getTableSize(files)
  279.     for name, url in pairs(files) do
  280.         http.request(url)
  281.         urls[url] = name
  282.     end
  283.     while true do
  284.         if remaining == 0 then
  285.             break
  286.         end
  287.         local event, url, handle = os.pullEvent()
  288.         local name = urls[url]
  289.         if (event == "http_success") and (name and handle) then
  290.             remaining = remaining - 1
  291.             if verbose then
  292.                 write(".")
  293.             end
  294.             local contents = handle.readAll()
  295.             if contents then
  296.                 std.storeURLs[name] = textutils.unserialize(contents)
  297.             else
  298.                 std.storeURLs[name] = {
  299.                     title = "ERROR ("..name..")",
  300.                     url = "",
  301.                     creator = "???",
  302.                     description = "This app is broken. Please contact LDDestroier on the forums and nag his ears off.",
  303.                     catagory = 12,
  304.                     forumPost = "n/a ",
  305.                     keywords = {"fucking","error"},
  306.                 }
  307.             end
  308.             handle.close()
  309.             urls[url] = nil
  310.         elseif event == "http_failure" and name then
  311.             remaining = remaining - 1
  312.             if verbose then
  313.                 write("x")
  314.             end
  315.             std.storeURLs[name] = {
  316.                 title = "ERROR ("..name..")",
  317.                 url = "",
  318.                 creator = "???",
  319.                 description = "This app cannot be downloaded. Please contact the developer and nag them.",
  320.                 catagory = 12,
  321.                 forumPost = "n/a ",
  322.                 keywords = {"fucking","error"},
  323.             }
  324.         end
  325.     end
  326.     return true
  327. end
  328.  
  329. local listings = http.get("https://api.github.com/repos/LDDestroier/STD-GUI/contents/default")
  330. local simulDownloads = {}
  331. local amnt = 0
  332. if listings then
  333.     listings = json.decode(listings.readAll())
  334.     std.storeURLs = {}
  335.     for k,v in pairs(listings) do
  336.         if v.name then
  337.             simulDownloads[v.name] = "https://raw.githubusercontent.com/LDDestroier/STD-GUI/master/default/"..v.name
  338.             amnt = amnt + 1
  339.         end
  340.     end
  341.     if verbose then
  342.         print("\nFound " .. amnt .. " valid STD entries.")
  343.         write("Attempting to download")
  344.     end
  345.     getSTDStoreList(simulDownloads)
  346. else
  347.     return false
  348. end
  349.  
  350. -- if not std.std_version then return end
  351.  
  352. if ((std.std_version or 0) < 101) or requireInjector then
  353.  
  354.     local colors_names = {
  355.         ["0"] = colors.white,
  356.         ["1"] = colors.orange,
  357.         ["2"] = colors.magenta,
  358.         ["3"] = colors.lightBlue,
  359.         ["4"] = colors.yellow,
  360.         ["5"] = colors.lime,
  361.         ["6"] = colors.pink,
  362.         ["7"] = colors.gray,
  363.         ["8"] = colors.lightGray,
  364.         ["9"] = colors.cyan,
  365.         ["a"] = colors.purple,
  366.         ["b"] = colors.blue,
  367.         ["c"] = colors.brown,
  368.         ["d"] = colors.green,
  369.         ["e"] = colors.red,
  370.         ["f"] = colors.black,
  371.     }
  372.     local blit_names = {}
  373.     for k,v in pairs(colors_names) do
  374.         blit_names[v] = k
  375.     end
  376.    
  377.     local codeNames = { --just for checking, not for any translation
  378.         ["r"] = "reset",
  379.     }
  380.    
  381.     local moveOn
  382.     local textToBlit = function(str)
  383.         local p = 1
  384.         local output = ""
  385.         local txcolorout = ""
  386.         local bgcolorout = ""
  387.         local txcode = "&"
  388.         local bgcode = "~"
  389.         local doFormatting = true
  390.         local usedformats = {}
  391.         local txcol,bgcol = blit_names[term.getTextColor()], blit_names[term.getBackgroundColor()]
  392.         local origTX,origBG = blit_names[term.getTextColor()], blit_names[term.getBackgroundColor()]
  393.         local cx,cy
  394.         moveOn = function(tx,bg)
  395.             output = output..str:sub(p,p)
  396.             txcolorout = txcolorout..tx --(doFormatting and tx or origTX)
  397.             bgcolorout = bgcolorout..bg --(doFormatting and bg or origBG)
  398.         end
  399.         while p <= #str do
  400.             if str:sub(p,p) == txcode then
  401.                 if colors_names[str:sub(p+1,p+1)] and doFormatting then
  402.                     txcol = str:sub(p+1,p+1)
  403.                     usedformats.txcol = true
  404.                     p = p + 1
  405.                 elseif codeNames[str:sub(p+1,p+1)] then
  406.                     if str:sub(p+1,p+1) == "r" and doFormatting then
  407.                         txcol = blit_names[term.getTextColor()]
  408.                         p = p + 1
  409.                     elseif str:sub(p+1,p+1) == "{" and doFormatting then
  410.                         doFormatting = false
  411.                         p = p + 1
  412.                     elseif str:sub(p+1,p+1) == "}" and (not doFormatting) then
  413.                         doFormatting = true
  414.                         p = p + 1
  415.                     else
  416.                         moveOn(txcol,bgcol)
  417.                     end
  418.                 else
  419.                     moveOn(txcol,bgcol)
  420.                 end
  421.                 p = p + 1
  422.             elseif str:sub(p,p) == bgcode then
  423.                 if colors_names[str:sub(p+1,p+1)] and doFormatting then
  424.                     bgcol = str:sub(p+1,p+1)
  425.                     usedformats.bgcol = true
  426.                     p = p + 1
  427.                 elseif codeNames[str:sub(p+1,p+1)] and (str:sub(p+1,p+1) == "r") and doFormatting then
  428.                     bgcol = blit_names[term.getBackgroundColor()]
  429.                     p = p + 1
  430.                 else
  431.                     moveOn(txcol,bgcol)
  432.                 end
  433.                 p = p + 1
  434.             else
  435.                 moveOn(txcol,bgcol)
  436.                 p = p + 1
  437.             end
  438.         end
  439.         return output, txcolorout, bgcolorout, usedformats
  440.     end
  441.    
  442.     for k,v in pairs(std.storeURLs) do
  443.         std.storeURLs[k].title = textToBlit(std.storeURLs[k].title)
  444.         std.storeURLs[k].creator = textToBlit(std.storeURLs[k].creator)
  445.         std.storeURLs[k].description = textToBlit(std.storeURLs[k].description)
  446.     end
  447.     _G.std = std
  448. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement