Skip to content

Instantly share code, notes, and snippets.

@timia2109
Last active August 29, 2015 14:03
Show Gist options
  • Save timia2109/83cc83e054802d2ef8d7 to your computer and use it in GitHub Desktop.
Save timia2109/83cc83e054802d2ef8d7 to your computer and use it in GitHub Desktop.
CC API List
<os>
<version use="()" desc="Returns the version of the OS the computer is running, which (for CraftOS) also contains the version of ComputerCraft." rtn="string version" />
<getComputerID use="()" desc="Returns the unique ID of this computer. os.computerID() also behaves exactly the same as os.getComputerID()." rtn="number id" />
<getComputerLabel use="()" desc="Returns the label of this computer. os.computerLabel() also behaves exactly the same as os.getComputerLabel()." rtn="string/nil label" />
<setComputerLabel use="(string/nil label)" desc="Set the label of this computer." rtn="nil" />
<run use="(table environment, string programPath [, string arguments])" desc="An advanced way of starting programs. A started program will have a given environment table which determines what functions it has available, as well as any variables it will be able to access by default. You may prefer to use the Shell (API) unless you need to do something special." rtn="boolean success" />
<loadAPI use="(string path)" desc="Loads a Lua script as an API in its own namespace. It will be available to all programs that run on the terminal." rtn="boolean success" />
<unloadAPI use="(string name)" desc="Unloads a previously loaded API." rtn="nil" />
<pullEvent use="([string target-event])" desc="Blocks until the computer receives an event, or if target-event is specified, will block until an instance of target-event occurs. os.pullEvent(target-event) returns the event and any parameters the event may have. If a target-event is specified, the computer will not break for any other events (except termination)." rtn="string event, param1, param2, ..." />
<pullEventRaw use="([string target-event])" desc="Advanced version of pullEvent(). Blocks until the computer receives an event, or if target-event is specified, will block until an instance of target-event occurs. os.pullEventRaw(target-event) returns the event and any parameters the event may have. Unlike os.pullEvent(target-event), this function will not raise an error if a 'terminate' event is received." rtn="string event, param1, param2, ..." />
<queueEvent use="(string event, param1, param2, ...)" desc="Adds an event to the event queue with the name event and the given parameters." rtn="nil" />
<clock use="()" desc="Returns the amount of time since the in-game computer was started." rtn="number time" />
<startTimer use="(number timeout)" desc="Queues an event to be triggered after a number of seconds (timeout). The ID of the timer is returned from this function to differentiate multiple timers. Timers are one-shot; once they have fired an event you will need to start another one if you need a recurring timer." rtn="number timerID" />
<time use="()" desc="Returns the current in-game time." rtn="number time" />
<sleep use="(number time)" desc="Makes the system wait a number of seconds before continuing in the program. os.sleep(time) may also be used as simply [sleep(time)]." rtn="nil" />
<day use="()" desc="Return the current in-game day (the number of in-game days since the world was created)." rtn="number day" />
<setAlarm use="(number time)" desc="Queues an event to be triggered at the specified in-game time." rtn="number alarmID" />
<shutdown use="()" desc="Turns off the computer." rtn="nil" />
<reboot use="()" desc="reboot the computer." rtn="nil" />
</os>
<bit>
<blshift use="(number n, number bits)" desc="Shifts a number left by a specified number of bits." rtn="number value" />
</bit>
tArgs = { ... }
local url = {}
url['db'] = 'https://gist.githubusercontent.com/timia2109/83cc83e054802d2ef8d7/raw/5eb96e158a194be0b633b84b0f7be4f5e65dd2aa/APIs%20db'
--url['app'] = url.header..'87302eccbc15e6da3f02dff86726e452ed277d85/apis.lua'
local lmnet = {} --IMPORT APIS FROM LMNET
--[[
GET LMNET: https://github.com/MultHub/LMNet-OS/
LMNet-OS by MultHub and timia2109
]]--
function lmnet.cprint(text)
if type(text) ~= 'table' then
text = {text}
end
local w, h = term.getSize()
for i=1,#text do
local x, y = term.getCursorPos()
term.setCursorPos(math.floor(w/2)-math.floor(text[i]:len()/2), y)
print(text[i])
end
end
function lmnet.menu(items, title, start,allowNil,moreTitle)
local function clear()
term.clear()
term.setCursorPos(1, 1)
end
local termWidth, termHeight = term.getSize()
local drawSize = termHeight - 6
local function maxPages()
local itemCount = #items
local pageCount = 0
while itemCount > 0 do
itemCount = itemCount - drawSize
pageCount = pageCount + 1
end
return pageCount
end
local function iif(cond, trueval, falseval)
if cond then
return trueval
else
return falseval
end
end
local function pagedItems()
local ret = {}
for i = 1, maxPages() do
local tmp = {}
local nElements = 0
for j = drawSize*(i-1)+1, iif(drawSize*(i+1) > #items, #items, drawSize*(i+1)) do
if nElements < drawSize then
table.insert(tmp, items[j])
nElements = nElements + 1
end
end
table.insert(ret, tmp)
end
return ret
end
local selected = 1
if start then
selected = start
end
local page = 1
local function redraw()
term.setBackgroundColor(colors.black)
clear()
lmnet.cprint(title)
if moreTitle then
head = moreTitle
else
head = {"Select with arrow keys or with mouse.","Press enter to select.",}
if not allowNil or allowNil == true then
head[3] = 'Terminate to cancel.'
end
end
for i=1,3 do
print(head[i])
end
pages = "<- (page "..page.." of "..maxPages()..") ->"
print(pages)
for i = 1, #pagedItems()[page] do
if selected == drawSize*(page-1)+i then
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
else
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
end
term.clearLine()
print(iif(selected == drawSize*(page-1)+i, ">", " ").." "..pagedItems()[page][i])
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
end
end
local function changePage(pW)
if pW == 1 and page < maxPages() then
page = page + 1
if selected + drawSize > #items then
selected = #items
else
selected = selected + drawSize
end
elseif pW == -1 and page > 1 then
page = page - 1
if selected - drawSize < 1 then
selected = 1
else
selected = selected - drawSize
end
end
end
while true do
redraw()
local eventData = {os.pullEventRaw()}
if eventData[1] == "terminate" then
if not allowNil or allowNil == true then
clear()
return nil
end
elseif eventData[1] == "key" then
if eventData[2] == keys.up and selected > 1 then
selected = selected - 1
if selected-(page-1)*drawSize < 1 then
page = page - 1
end
elseif eventData[2] == keys.down and selected < #items then
selected = selected + 1
if selected-(page-1)*drawSize > drawSize then
page = page + 1
end
elseif eventData[2] == keys.enter then
clear()
return items[selected]
elseif eventData[2] == keys.left then
changePage(-1)
elseif eventData[2] == keys.right then
changePage(1)
end
elseif eventData[1] == 'mouse_click' then
if eventData[4] > 5 then
clear()
selected = (eventData[4]-6+((page-1)*drawSize))+1
return items[selected]
elseif eventData[4] == 5 then
if eventData[3] == 1 or eventData[3] == 2 then
changePage(-1)
elseif eventData[3] == pages:len()-1 or eventData[3] == pages:len()-2 then
changePage(1)
end
end
elseif eventData[1] == 'mouse_scroll' then
if eventData[2] == 1 then
changePage(1)
elseif eventData[2] == -1 then
changePage(-1)
end
end
sleep(0)
end
end
local ui = {}
function ui.cl()
term.clear()
term.setCursorPos(1,1)
end
function ui.col(pTxt,pBack)
if not pTxt then
pTxt = colors['white']
elseif not pBack then
pBack = colors['black']
end
if term.isColor() then
term.setTextColor(colors[pTxt])
term.setBackgroundColor(colors[pBack])
end
end
function ui.line(pLine,pCol)
term.setCursorPos(1,pLine)
local xSize,ySize = term.getSize()
paintutils.drawLine(1, pLine, xSize, pLine, colors[pCol])
term.setCursorPos(1,pLine)
end
function ui.apis()
ui.cl()
local x = lmnet.menu(getAllAPI(),'Choose an API',1,true)
if not x then
printError('TERMINATE')
return
end
ui.apiAll(x)
end
function ui.apiAll(pAPI)
ui.cl()
local x = lmnet.menu(getAPI(pAPI),'API: '..pAPI)
if not x then
return
end
ui.method(pAPI,x)
end
function ui.method(pAPI,pMethod)
ui.cl()
textutils.slowPrint('Get '..pAPI..'.'..pMethod..'() data now...')
local db = getdb()
local xSize,ySize = term.getSize()
if not db then
printError('Can\'t load Database. Is githubusercontent.com on the HTTP Whitelist?')
return
end
if not db[pAPI:lower()] then
if _G[pAPI]['apiInfoXML'] then
db[pAPI:lower()] = _G[pAPI]['apiInfoXML']()
else
ui.cl()
local err = {
'Can\'t find API Data. Is that an orgin CC API?',
'Or maybe I havn\'t insert them into the db',
'Want your API in that db? Check the CC Forum topic and write to me.',
}
for i=1,#err do
printError(err[i])
end
return
end
end
local allINeed = db[pAPI:lower()][pMethod]
ui.cl()
ui.line(1,'blue')
term.setCursorPos(1,1)
lmnet.cprint('API: '..pAPI..' | '..pMethod)
--ui.col()
term.setCursorPos(1,2)
print('Usage: ')
print(' '..pAPI..'.'..pMethod..allINeed['use'])
term.setCursorPos(1,5)
print('Return: ')
print(' '..allINeed['rtn'])
term.setCursorPos(1, 8)
print('Description: ')
print(' '..allINeed['desc'])
ui.line(ySize,'red')
lmnet.cprint("< Back to APIs (b)")
while true do
local ev = {os.pullEvent()}
if ev[1] == 'mouse_click' then
if ev[4] == ySize or ev[4] == ySize-1 then
ui.apis()
break
end
elseif ev[1] == 'char' then
if ev[2] == 'b' then
ui.apis()
break
end
end
end
end
function getAPI(pAPIName)
if not _G[pAPIName] then
return nil
end
local fkts = {}
for i,v in pairs(_G[pAPIName]) do
table.insert(fkts,i)
end
return fkts
end
function getdb()
local function useXML(pIn)
rtn = {}
local x = xml.collect(pIn)
for i,v in pairs(x) do
rtn[v['label']] = {}
for i=1,#v do
local lMain = v['label']
local lSub = v[i]['label']
rtn[lMain][lSub] = v[i]['xarg']
end
end
return rtn
end
local resp
if fs.exists('apidb.xml') then --JUST FOR MY TESTS.
local file = fs.open("apidb.xml",'r')
resp = file.readAll()
file.close()
else
resp = http.get(url['db']).readAll()
end
if resp then
return useXML(resp)
else
return false
end
end
function getAllAPI()
local rtn = {}
for i,v in pairs(_G) do
if type(v) == 'table' then
table.insert(rtn,i)
end
end
return rtn
end
if #tArgs < 1 then
ui.apis()
else
textutils.pagedTabulate(getAPI(tArgs[1]))
end
--module(..., package.seeall)
---------------------------------------------------------------------------------
---------------------------------------------------------------------------------
--
-- xml.lua - XML parser for use with the Corona SDK.
--
-- version: 1.2
--
-- CHANGELOG:
--
-- 1.2 - Created new structure for returned table
-- 1.1 - Fixed base directory issue with the loadFile() function.
--
-- NOTE: This is a modified version of Alexander Makeev's Lua-only XML parser
-- found here: http://lua-users.org/wiki/LuaXml
--
---------------------------------------------------------------------------------
---------------------------------------------------------------------------------
--[[This is a file, I found on the Internet to parse XML files to a table. Don't know of who is it, but I still hope we can use it.
Require for: betterPastebin (API)
Pastebin Link: http://pastebin.com/HB8Jt28Z
~~timia2109
]]--
function parseargs(s)
local arg = {}
string.gsub(s, "([%-%w]+)=([\"'])(.-)%2", function (w, _, a)
arg[w] = a
end)
return arg
end
function collect(s)
local stack = {}
local top = {}
table.insert(stack, top)
local ni,c,label,xarg, empty
local i, j = 1, 1
while true do
ni,j,c,label,xarg, empty = string.find(s, "<(%/?)([%w:]+)(.-)(%/?)>", i)
if not ni then break end
local text = string.sub(s, i, ni-1)
if not string.find(text, "^%s*$") then
table.insert(top, text)
end
if empty == "/" then -- empty element tag
table.insert(top, {label=label, xarg=parseargs(xarg), empty=1})
elseif c == "" then -- start tag
top = {label=label, xarg=parseargs(xarg)}
table.insert(stack, top) -- new level
else -- end tag
local toclose = table.remove(stack) -- remove top
top = stack[#stack]
if #stack < 1 then
error("nothing to close with "..label)
end
if toclose.label ~= label then
error("trying to close "..toclose.label.." with "..label)
end
table.insert(top, toclose)
end
i = j+1
end
local text = string.sub(s, i)
if not string.find(text, "^%s*$") then
table.insert(stack[#stack], text)
end
if #stack > 1 then
error("unclosed "..stack[#stack].label)
end
return stack[1]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment