Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ Tools
- This is an API I use for my other programs
- It helps keep the other code a bit cleaner
- I also have less files to update anytime I make a change ;)
- Credits:
- PT is a table serialization API written by PixelToast from the ComputerCraft forums
- http://www.computercraft.info/forums2/index.php?/topic/16166-improved-serialization-and-unserialization/
- --gezepi
- ]]
- --[[ http://www.computercraft.info/forums2/index.php?/topic/15605-how-to-make-turtle-programs-available-to-all-turtles/ ]]
- local files = nil
- local oldPrint = print
- local function checkFiles()
- if not files then error("No list of files") end
- end
- local function loadAPI(name)
- if not files then error("Need a table of files to load "..name) end
- os.loadAPI(files[name])
- end
- --[[ Puts a prefix in front of anything printed from this API ]]
- local function print(...)
- local s = "tools:"
- for i=1,#arg do
- s = s..arg[i]
- end
- oldPrint(s)
- end
- --[[ Loads and unserializes a table ]]
- function loadTable(filename)
- if not filename then error("Trying to load a nil filename") end
- print("Loading "..filename)
- if not fs.exists(filename) then print(filename.." doesn't exist") ; return {} end
- local file = fs.open(filename, "r")
- local data = file.readAll()
- file.close()
- loadAPI("pt")
- local s=pt.unserialize(data)
- os.unloadAPI("pt")
- return s
- end
- --[[ Prints the string if debug is true ]]
- function bug(bolDebug)
- print("Returning 'bug'")
- while pt==nil do loadAPI("pt") end
- local serialize = pt.serialize
- os.unloadAPI("pt")
- function b(s)
- if not bolDebug then return end
- local w, h = term.getSize()
- local x, y = term.getCursorPos()
- term.setCursorPos(x,h)
- if type(s)=="table" then
- s=serialize(s)
- end
- oldPrint(s)
- end
- return b
- end
- --[[ Starts rednet for the given modem type ]]
- function network(start, modemType)
- if start==nil then start=true end
- if modemType==nil then modemType = "wireless modem" end
- if modemType~="modem" and modemType~="wireless modem" then print(modemType) ; error("Please use 'modem' or 'wireless modem' as types") end
- --loadAPI("rom/shared/tools")
- local modemSide = getPeripheral(modemType)
- if modemSide==nil then error("Please attach a wireless modem to this computer") end
- if start then
- rednet.open(modemSide)
- if not rednet.isOpen(modemSide) then error("Unable to start Rednet") end
- else
- rednet.close(modemSide)
- if rednet.isOpen(modemSide) then error("Unable to stop Rednet") end
- end
- modemSide = nil
- print("Rednet started on "..modemType)
- return true
- end
- --[[ Saves a table to disk ]]
- function saveTable(t, filename)
- print("Saving "..filename)
- local file = fs.open(filename, "w")
- loadAPI("pt")
- local s = pt.serialize(t)
- os.unloadAPI("pt")
- file.write(s)
- file.close()
- end
- --[[ Writes a string to the terminal such that the next thing to print overwrites it ]]
- function wPrint(s)
- local w, h = term.getSize()
- local x,y = term.getCursorPos()
- term.write(s..string.rep(" ", w - string.len(s)))
- --term.write(s..string.rep(" ", w - string.len(s)))
- term.setCursorPos(1, y)
- end
- --[[ Unloads this API ]]
- unload = function()
- print("Unloaded")
- os.unloadAPI("tools")
- end
- --[[ Returns the side a peripheral is on ]]
- function getPeripheral(t)
- for k, v in pairs(peripheral.getNames()) do
- --print(peripheral.getType(v))
- if peripheral.getType(v)=="modem" then
- if t:lower()=="wireless modem" and peripheral.wrap(v).isWireless() then return v end
- if t:lower()=="modem" and not peripheral.wrap(v).isWireless() then return v end
- else
- if peripheral.getType(v):lower()==t:lower() then return v end
- end
- end
- print(t.." not found")
- return nil
- end
- --[[ Returns table of computer names used for networking ]]
- function names(name)
- --if not name then error("Can't lookup a nil computer name") end
- local n = loadTable(files["names"])
- if name==nil then return n else
- if n[name]~=nil then return n[name] end
- print("Available names:")
- bug(true)(n)
- error(name.." is not in table of names")
- end
- return n
- end
- --[[ Returns a table of tesseract frequencies for item transfers ]]
- function freqs(freq)
- checkFiles()
- local f = loadTable(files["freqs"])
- if freq==nil then return f else
- if f[freq]~=nil then return f[freq] end
- print("Available frequencies:")
- bug(true)(f)
- error("'"..freq.."' not recognized as a frequency")
- return false
- end
- end
- --[[ Replaces rednet.send so that a table of to ids can be used ]]
- function send(ids, msg)
- if type(ids)=="table" then
- for i=1, #ids do
- rednet.send(ids[i], msg)
- end
- else
- rednet.send(ids, msg)
- end
- end
- --[[ This needs to be run for most of the functions to run ]]
- function init(nfiles)
- if nfiles then
- files = nfiles
- print("Loaded with files")
- else
- print("No files loaded...")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment