Skip to content

Instantly share code, notes, and snippets.

@skwerlman
Last active August 29, 2015 14:26
Show Gist options
  • Save skwerlman/dc2f9eaeaa27268e3319 to your computer and use it in GitHub Desktop.
Save skwerlman/dc2f9eaeaa27268e3319 to your computer and use it in GitHub Desktop.
SolderInfo
local function reset()
term.setTextColor(colors.white)
end
function printc(color, ...)
term.setTextColor(color)
print(...)
end
function writec(color, ...)
term.setTextColor(color)
write(...)
end
local controls = {["\n"]="\\n", ["\r"]="\\r", ["\t"]="\\t", ["\b"]="\\b", ["\f"]="\\f", ["\""]="\\\"", ["\\"]="\\\\"}
local function isArray(t)
local max = 0
for k,v in pairs(t) do
if type(k) ~= "number" then
return false
elseif k > max then
max = k
end
end
return max == #t
end
local whites = {['\n']=true; ['r']=true; ['\t']=true; [' ']=true; [',']=true; [':']=true}
function removeWhite(str)
while whites[str:sub(1, 1)] do
str = str:sub(2)
end
return str
end
local function encodeCommon(val, pretty, tabLevel, tTracking)
local str = ""
local function tab(s)
str = str .. ("\t"):rep(tabLevel) .. s
end
local function arrEncoding(val, bracket, closeBracket, iterator, loopFunc)
str = str .. bracket
if pretty then
str = str .. "\n"
tabLevel = tabLevel + 1
end
for k,v in iterator(val) do
tab("")
loopFunc(k,v)
str = str .. ","
if pretty then str = str .. "\n" end
end
if pretty then
tabLevel = tabLevel - 1
end
if str:sub(-2) == ",\n" then
str = str:sub(1, -3) .. "\n"
elseif str:sub(-1) == "," then
str = str:sub(1, -2)
end
tab(closeBracket)
end
if type(val) == "table" then
assert(not tTracking[val], "Cannot encode a table holding itself recursively")
tTracking[val] = true
if isArray(val) then
arrEncoding(val, "[", "]", ipairs, function(k,v)
str = str .. encodeCommon(v, pretty, tabLevel, tTracking)
end)
else
arrEncoding(val, "{", "}", pairs, function(k,v)
assert(type(k) == "string", "JSON object keys must be strings", 2)
str = str .. encodeCommon(k, pretty, tabLevel, tTracking)
str = str .. (pretty and ": " or ":") .. encodeCommon(v, pretty, tabLevel, tTracking)
end)
end
elseif type(val) == "string" then
str = '"' .. val:gsub("[%c\"\\]", controls) .. '"'
elseif type(val) == "number" or type(val) == "boolean" then
str = tostring(val)
else
error("JSON only supports arrays, objects, numbers, booleans, and strings", 2)
end
return str
end
function encode(val)
return encodeCommon(val, false, 0, {})
end
function encodePretty(val)
return encodeCommon(val, true, 0, {})
end
function parseBoolean(str)
if str:sub(1, 4) == "true" then
return true, removeWhite(str:sub(5))
else
return false, removeWhite(str:sub(6))
end
end
function parseNull(str)
return nil, removeWhite(str:sub(5))
end
local numChars = {['e']=true; ['E']=true; ['+']=true; ['-']=true; ['.']=true}
function parseNumber(str)
local i = 1
while numChars[str:sub(i, i)] or tonumber(str:sub(i, i)) do
i = i + 1
end
local val = tonumber(str:sub(1, i - 1))
str = removeWhite(str:sub(i))
return val, str
end
function parseString(str)
local i,j = str:find('[^\\]"')
local s = str:sub(2, j - 1)
for k,v in pairs(controls) do
s = s:gsub(v, k)
end
str = removeWhite(str:sub(j + 1))
return s, str
end
function parseArray(str)
str = removeWhite(str:sub(2))
local val = {}
local i = 1
while str:sub(1, 1) ~= "]" do
local v = nil
v, str = parseValue(str)
val[i] = v
i = i + 1
str = removeWhite(str)
end
str = removeWhite(str:sub(2))
return val, str
end
function parseObject(str)
str = removeWhite(str:sub(2))
local val = {}
while str:sub(1, 1) ~= "}" do
local k, v = nil, nil
k, v, str = parseMember(str)
val[k] = v
str = removeWhite(str)
end
str = removeWhite(str:sub(2))
return val, str
end
function parseMember(str)
local k = nil
k, str = parseValue(str)
local val = nil
val, str = parseValue(str)
return k, val, str
end
function parseValue(str)
local fchar = str:sub(1, 1)
if fchar == "{" then
return parseObject(str)
elseif fchar == "[" then
return parseArray(str)
elseif tonumber(fchar) ~= nil or numChars[fchar] then
return parseNumber(str)
elseif str:sub(1, 4) == "true" or str:sub(1, 5) == "false" then
return parseBoolean(str)
elseif fchar == "\"" then
return parseString(str)
elseif str:sub(1, 4) == "null" then
return parseNull(str)
end
return nil
end
function decode(str)
str = removeWhite(str)
t = parseValue(str)
return t
end
function decodeFromFile(path)
local file = assert(fs.open(path, "r"))
return decode(file.readAll())
end
if not json then os.loadAPI('json') end
if not color2 then os.loadAPI('color2') end
local printc = color2.printc
local writec = color2.writec
local get = function(path)
--print(path)
return http.get(path)
end
-- user args
local solderRepo = '' -- The base URL of the solder application
local clientID = '' -- enter yourtechnic client's ID
local dispAll = false -- set to true if you want to display all available builds
-- user input validation
assert(solderRepo ~= '', 'You must change solderRepo to a valid URL')
assert(type(solderRepo) == 'string', 'solderRepo must be a string')
assert(type(clientID) == 'string', 'clientID must be a string')
assert(type(dispAll) == 'boolean', 'dispAll must be true or false')
-- define paths
local urlBase = 'http://' .. solderRepo
if not get(urlBase .. '/api') then -- not 0.7 compat, try again for 0.6
urlBase = urlBase .. '/index.php'
assert(get(urlBase .. '/api')) -- fail if not 0.7 or 0.6 compat
end
local apiBase = urlBase .. '/api'
local cid = '?cid=' .. clientID
local modpackBase = apiBase .. '/modpack/'
-- gather info
local api = json.decode(get(apiBase .. cid).readAll()) -- get general info about the solder install
local modpack = json.decode(get(modpackBase .. cid).readAll()) -- get a list of all modpacks
local modpackPub = json.decode(get(modpackBase).readAll()) -- check again w/o creds to see which ones are hidden, assuming we can see any hidden to begin with
local modpackInfo = {}
for k,v in pairs(modpack.modpacks) do
modpackInfo[k] = json.decode(get(modpackBase .. k .. cid).readAll())
end
-- output info
writec(colors.yellow, 'Crawling ')
printc(colors.green, solderRepo)
writec(colors.yellow, api.api)
printc(colors.green, ' ' .. api.version .. '-' .. api.stream)
printc(colors.yellow, 'Modpacks:')
for k,v in pairs(modpack.modpacks) do
writec(colors.yellow, ' ' .. v)
writec(colors.yellow, ' (')
writec(colors.green, k)
writec(colors.yellow,')')
if not modpackPub.modpacks[k] then -- check whether the modpack appears in the auth'd request and not the pub request
writec(colors.red, ' HIDDEN')
end
print()
writec(colors.yellow, ' Recommended: ')
printc(colors.green, tostring(modpackInfo[k].recommended))
writec(colors.yellow, ' Latest: ')
printc(colors.green, tostring(modpackInfo[k].latest))
if dispAll == true then
printc(colors.yellow, ' All Builds:')
for j,u in ipairs(modpackInfo[k].builds) do
printc(colors.green, ' ' .. tostring(u))
end
else
writec(colors.yellow, ' # of builds: ')
printc(colors.green, #modpackInfo[k].builds)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment