Advertisement
LDDestroier

STD-GUI OnlineAPPS List

Nov 14th, 2016
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.80 KB | None | 0 0
  1. local tArg = {...}
  2.  
  3. ------------------------------------------------------------------ utils
  4. local controls = {["\n"]="\\n", ["\r"]="\\r", ["\t"]="\\t", ["\b"]="\\b", ["\f"]="\\f", ["\""]="\\\"", ["\\"]="\\\\"}
  5. local function isArray(t)
  6.     local max = 0
  7.     for k,v in pairs(t) do
  8.         if type(k) ~= "number" then
  9.             return false
  10.         elseif k > max then
  11.             max = k
  12.         end
  13.     end
  14.     return max == #t
  15. end
  16. local whites = {['\n']=true; ['\r']=true; ['\t']=true; [' ']=true; [',']=true; [':']=true}
  17. local function removeWhite(str)
  18.     while whites[str:sub(1, 1)] do
  19.         str = str:sub(2)
  20.     end
  21.     return str
  22. end
  23. local encodeCommon, arrEncoding, parseNumber, parseString, parseArray, parseMember, parseValue, parseObject, decode, decodeFromFile
  24.  
  25. ------------------------------------------------------------------ encoding
  26.  
  27. function encodeCommon(val, pretty, tabLevel, tTracking)
  28.     local str = ""
  29.     local function tab(s)
  30.         str = str .. ("\t"):rep(tabLevel) .. s
  31.     end
  32.     function arrEncoding(val, bracket, closeBracket, iterator, loopFunc)
  33.         str = str .. bracket
  34.         if pretty then
  35.             str = str .. "\n"
  36.             tabLevel = tabLevel + 1
  37.         end
  38.         for k,v in iterator(val) do
  39.             tab("")
  40.             loopFunc(k,v)
  41.             str = str .. ","
  42.             if pretty then str = str .. "\n" end
  43.         end
  44.         if pretty then
  45.             tabLevel = tabLevel - 1
  46.         end
  47.         if str:sub(-2) == ",\n" then
  48.             str = str:sub(1, -3) .. "\n"
  49.         elseif str:sub(-1) == "," then
  50.             str = str:sub(1, -2)
  51.         end
  52.         tab(closeBracket)
  53.     end
  54.     if type(val) == "table" then
  55.         assert(not tTracking[val], "Cannot encode a table holding itself recursively")
  56.         tTracking[val] = true
  57.         if isArray(val) then
  58.             arrEncoding(val, "[", "]", ipairs, function(k,v)
  59.                 str = str .. encodeCommon(v, pretty, tabLevel, tTracking)
  60.             end)
  61.         else
  62.             arrEncoding(val, "{", "}", pairs, function(k,v)
  63.                 assert(type(k) == "string", "JSON object keys must be strings", 2)
  64.                 str = str .. encodeCommon(k, pretty, tabLevel, tTracking)
  65.                 str = str .. (pretty and ": " or ":") .. encodeCommon(v, pretty, tabLevel, tTracking)
  66.             end)
  67.         end
  68.     elseif type(val) == "string" then
  69.         str = '"' .. val:gsub("[%c\"\\]", controls) .. '"'
  70.     elseif type(val) == "number" or type(val) == "boolean" then
  71.         str = tostring(val)
  72.     else
  73.         error("JSON only supports arrays, objects, numbers, booleans, and strings", 2)
  74.     end
  75.     return str
  76. end
  77. local function encode(val)
  78.     return encodeCommon(val, false, 0, {})
  79. end
  80. local function encodePretty(val)
  81.     return encodeCommon(val, true, 0, {})
  82. end
  83.  
  84. ------------------------------------------------------------------ decoding
  85.  
  86. local decodeControls = {}
  87. for k,v in pairs(controls) do
  88.     decodeControls[v] = k
  89. end
  90. local function parseBoolean(str)
  91.     if str:sub(1, 4) == "true" then
  92.         return true, removeWhite(str:sub(5))
  93.     else
  94.         return false, removeWhite(str:sub(6))
  95.     end
  96. end
  97. local function parseNull(str)
  98.     return nil, removeWhite(str:sub(5))
  99. end
  100. local numChars = {['e']=true; ['E']=true; ['+']=true; ['-']=true; ['.']=true}
  101. function parseNumber(str)
  102.     local i = 1
  103.     while numChars[str:sub(i, i)] or tonumber(str:sub(i, i)) do
  104.         i = i + 1
  105.     end
  106.     local val = tonumber(str:sub(1, i - 1))
  107.     str = removeWhite(str:sub(i))
  108.     return val, str
  109. end
  110. function parseString(str)
  111.     str = str:sub(2)
  112.     local s = ""
  113.     while str:sub(1,1) ~= "\"" do
  114.         local next = str:sub(1,1)
  115.         str = str:sub(2)
  116.         assert(next ~= "\n", "Unclosed string")
  117.  
  118.         if next == "\\" then
  119.             local escape = str:sub(1,1)
  120.             str = str:sub(2)
  121.  
  122.             next = assert(decodeControls[next..escape], "Invalid escape character")
  123.         end
  124.         s = s .. next
  125.     end
  126.     return s, removeWhite(str:sub(2))
  127. end
  128. function parseArray(str)
  129.     str = removeWhite(str:sub(2))
  130.     local val = {}
  131.     local i = 1
  132.     while str:sub(1, 1) ~= "]" do
  133.         local v = nil
  134.         v, str = parseValue(str)
  135.         val[i] = v
  136.         i = i + 1
  137.         str = removeWhite(str)
  138.     end
  139.     str = removeWhite(str:sub(2))
  140.     return val, str
  141. end
  142. function parseValue(str)
  143.     local fchar = str:sub(1, 1)
  144.     if fchar == "{" then
  145.         return parseObject(str)
  146.     elseif fchar == "[" then
  147.         return parseArray(str)
  148.     elseif tonumber(fchar) ~= nil or numChars[fchar] then
  149.         return parseNumber(str)
  150.     elseif str:sub(1, 4) == "true" or str:sub(1, 5) == "false" then
  151.         return parseBoolean(str)
  152.     elseif fchar == "\"" then
  153.         return parseString(str)
  154.     elseif str:sub(1, 4) == "null" then
  155.         return parseNull(str)
  156.     end
  157.     return nil
  158. end
  159. function parseMember(str)
  160.     local k = nil
  161.     k, str = parseValue(str)
  162.     local val = nil
  163.     val, str = parseValue(str)
  164.     return k, val, str
  165. end
  166. function parseObject(str)
  167.     str = removeWhite(str:sub(2))
  168.     local val = {}
  169.     while str:sub(1, 1) ~= "}" do
  170.         local k, v = nil, nil
  171.         k, v, str = parseMember(str)
  172.         val[k] = v
  173.         str = removeWhite(str)
  174.     end
  175.     str = removeWhite(str:sub(2))
  176.     return val, str
  177. end
  178. function decode(str)
  179.     str = removeWhite(str)
  180.     t = parseValue(str)
  181.     return t
  182. end
  183. function decodeFromFile(path)
  184.     local file = assert(fs.open(path, "r"))
  185.     local decoded = decode(file.readAll())
  186.     file.close()
  187.     return decoded
  188. end
  189.  
  190. local getTableSize = function(tbl)
  191.     local amnt = 0
  192.     for k,v in pairs(tbl) do
  193.         amnt = amnt + 1
  194.     end
  195.     return amnt
  196. end
  197.  
  198. if tArg[1] ~= "storelengthcheck" then
  199.     std.storeCatagoryNames = {"Programs"}
  200. end
  201.  
  202. local req = http.get("http://twijnweb.com/cc/api/v1.0/programs/getall.php")
  203. if not req then
  204.     return false
  205. end
  206. req = decode(req.readAll())
  207. if type(req) ~= "table" then
  208.     return false
  209. elseif req.programs then
  210.     req = req.programs
  211. else
  212.     return false
  213. end
  214. local buffer
  215. local laURLs = {}
  216. for k,v in pairs(req) do
  217.     buffer = {
  218.         title = v.name,
  219.         url = "http://get.twijnweb.com/"..v.code,
  220.         creator = v.owner,
  221.         description = v.description,
  222.         catagory = 1,
  223.         forumPost = "N/A ",
  224.         keywords = {},
  225.     }
  226.     laURLs[v.code] = buffer
  227. end
  228. if tArg[1] == "storelengthcheck" then
  229.     return getTableSize(laURLs)
  230. else
  231.     std.storeURLs = laURLs
  232. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement