gezepi

tools

Dec 19th, 2013
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.77 KB | None | 0 0
  1. --[[    Tools
  2.     This is an API I use for my other programs
  3.     It helps keep the other code a bit cleaner
  4.     I also have less files to update anytime I make a change ;)
  5.    
  6.     Credits:
  7.     PT is a table serialization API written by PixelToast from the ComputerCraft forums
  8.     http://www.computercraft.info/forums2/index.php?/topic/16166-improved-serialization-and-unserialization/
  9.    
  10.     --gezepi
  11. ]]
  12. --[[    http://www.computercraft.info/forums2/index.php?/topic/15605-how-to-make-turtle-programs-available-to-all-turtles/  ]]
  13.  
  14. local files = nil
  15.  
  16. local oldPrint = print
  17.  
  18. local function checkFiles()
  19.     if not files then error("No list of files") end
  20. end
  21.  
  22. local function loadAPI(name)
  23.     if not files then error("Need a table of files to load "..name) end
  24.     os.loadAPI(files[name])
  25. end
  26. --[[    Puts a prefix in front of anything printed from this API    ]]
  27. local function print(...)
  28.     local s = "tools:"
  29.     for i=1,#arg do
  30.         s = s..arg[i]
  31.     end
  32.     oldPrint(s)
  33. end
  34. --[[    Loads and unserializes a table  ]]
  35. function loadTable(filename)
  36.     if not filename then error("Trying to load a nil filename") end
  37.     print("Loading "..filename)
  38.     if not fs.exists(filename) then print(filename.." doesn't exist") ; return {} end
  39.     local file = fs.open(filename, "r")
  40.     local data = file.readAll()
  41.     file.close()
  42.     loadAPI("pt")
  43.     local s=pt.unserialize(data)
  44.     os.unloadAPI("pt")
  45.     return s
  46. end
  47.  --[[   Prints the string if debug is true  ]]
  48. function bug(bolDebug)
  49.     print("Returning 'bug'")
  50.     while pt==nil do loadAPI("pt") end
  51.     local serialize = pt.serialize
  52.     os.unloadAPI("pt")
  53.     function b(s)
  54.         if not bolDebug then return end
  55.         local w, h = term.getSize()
  56.         local x, y = term.getCursorPos()
  57.         term.setCursorPos(x,h)
  58.         if type(s)=="table" then
  59.             s=serialize(s)
  60.         end
  61.         oldPrint(s)
  62.     end
  63.     return b
  64. end
  65. --[[    Starts rednet for the given modem type  ]]
  66. function network(start, modemType)
  67.     if start==nil then start=true end
  68.     if modemType==nil then modemType = "wireless modem" end
  69.     if modemType~="modem" and modemType~="wireless modem" then print(modemType) ; error("Please use 'modem' or 'wireless modem' as types") end
  70.     --loadAPI("rom/shared/tools")
  71.     local modemSide = getPeripheral(modemType)
  72.     if modemSide==nil then error("Please attach a wireless modem to this computer") end
  73.     if start then
  74.         rednet.open(modemSide)
  75.         if not rednet.isOpen(modemSide) then error("Unable to start Rednet") end
  76.     else
  77.         rednet.close(modemSide)
  78.         if rednet.isOpen(modemSide) then error("Unable to stop Rednet") end
  79.     end
  80.     modemSide = nil
  81.     print("Rednet started on "..modemType)
  82.     return true
  83. end
  84. --[[    Saves a table to disk   ]]
  85. function saveTable(t, filename)
  86.     print("Saving "..filename)
  87.         local file = fs.open(filename, "w")
  88.         loadAPI("pt")
  89.         local s = pt.serialize(t)
  90.         os.unloadAPI("pt")
  91.         file.write(s)
  92.         file.close()
  93. end
  94. --[[    Writes a string to the terminal such that the next thing to print overwrites it ]]
  95. function wPrint(s)
  96.     local w, h = term.getSize()
  97.     local x,y = term.getCursorPos()
  98.     term.write(s..string.rep(" ", w - string.len(s)))
  99.     --term.write(s..string.rep(" ", w - string.len(s)))
  100.     term.setCursorPos(1, y)
  101. end
  102. --[[    Unloads this API    ]]
  103. unload = function()
  104.     print("Unloaded")
  105.     os.unloadAPI("tools")
  106. end
  107. --[[    Returns the side a peripheral is on ]]
  108. function getPeripheral(t)
  109.     for k, v in pairs(peripheral.getNames()) do
  110.         --print(peripheral.getType(v))
  111.         if peripheral.getType(v)=="modem" then
  112.             if t:lower()=="wireless modem" and peripheral.wrap(v).isWireless() then return v end
  113.             if t:lower()=="modem" and not peripheral.wrap(v).isWireless() then return v end
  114.         else
  115.             if peripheral.getType(v):lower()==t:lower() then return v end
  116.         end
  117.     end
  118.     print(t.." not found")
  119.     return nil
  120. end
  121. --[[    Returns table of computer names used for networking ]]
  122. function names(name)
  123.     --if not name then error("Can't lookup a nil computer name") end
  124.     local n = loadTable(files["names"])
  125.     if name==nil then return n else
  126.         if n[name]~=nil then return n[name] end
  127.         print("Available names:")
  128.         bug(true)(n)
  129.         error(name.." is not in table of names")
  130.     end
  131.     return n
  132. end
  133. --[[    Returns a table of tesseract frequencies for item transfers ]]
  134. function freqs(freq)
  135.     checkFiles()
  136.     local f = loadTable(files["freqs"])
  137.     if freq==nil then return f else
  138.         if f[freq]~=nil then return f[freq] end
  139.         print("Available frequencies:")
  140.         bug(true)(f)
  141.         error("'"..freq.."' not recognized as a frequency")
  142.         return false
  143.     end
  144. end
  145. --[[    Replaces rednet.send so that a table of to ids can be used  ]]
  146. function send(ids, msg)
  147.     if type(ids)=="table" then
  148.         for i=1, #ids do
  149.             rednet.send(ids[i], msg)
  150.         end
  151.     else
  152.         rednet.send(ids, msg)
  153.     end
  154. end
  155. --[[    This needs to be run for most of the functions to run   ]]
  156. function init(nfiles)
  157.     if nfiles then
  158.         files = nfiles
  159.         print("Loaded with files")
  160.     else
  161.         print("No files loaded...")
  162.     end
  163. end
Advertisement
Add Comment
Please, Sign In to add comment