Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Version 1.01
- -- ######################################################
- -- ## Table Utility Functions ##
- -- ######################################################
- function tableContainsValue(table, element)
- for _, value in pairs(table) do
- if value == element then
- return true
- end
- end
- return false
- end
- function tableContainsKey(table, element)
- for key, value in pairs(table) do
- if key == element then
- return true
- end
- end
- return false
- end
- function explode(div,str)
- if (div=='') then return false end
- local pos,arr = 0,{}
- for st,sp in function() return string.find(str,div,pos,true) end do
- table.insert(arr,string.sub(str,pos,st-1))
- pos = sp + 1
- end
- table.insert(arr,string.sub(str,pos))
- return arr
- end
- function dump(o)
- if type(o) == 'table' then
- local s = '{ '
- for k,v in pairs(o) do
- if type(k) ~= 'number' then k = '"'..k..'"' end
- s = s .. '['..k..'] = ' .. dump(v) .. ','
- end
- return s .. '} '
- else
- return tostring(o)
- end
- end
- function pairsByKeys (t, f)
- local a = {}
- for n in pairs(t) do
- table.insert(a, n)
- end
- table.sort(a, f)
- local i = 0 -- iterator variable
- local iter = function () -- iterator function
- i = i + 1
- if a[i] == nil then
- return nil
- else
- return a[i], t[a[i]]
- end
- end
- return iter
- end
- function ripairs(t)
- idx={}
- for k,v in pairs(t) do
- if type(k)=="number" then idx[#idx+1]=k end
- end
- table.sort(idx)
- local function ripairs_it(t,_)
- if #idx==0 then return nil end
- k=idx[#idx]
- idx[#idx]=nil
- return k,t[k]
- end
- return ripairs_it, t, nil
- end
- function table_invert(t)
- local s={}
- for k,v in pairs(t) do
- s[v]=k
- end
- return s
- end
- function table_count(T)
- local count = 0
- for _ in pairs(T) do count = count + 1 end
- return count
- end
- --// The Save Function
- function table_save( tbl,filename )
- local saveable = textutils.serialize(tbl)
- local handle = assert(fs.open(filename, "w"), "Error saving to file.")
- handle.write(saveable)
- handle.close()
- end
- --// The Load Function
- function table_load( sfile )
- local handle = assert(fs.open(sfile,"r"), "Error loading from file.")
- local input = handle.readAll()
- handle.close()
- local settings = textutils.unserialize(input)
- return settings
- end
- -- ######################################################
- -- ## String Functions ##
- -- ######################################################
- function padLeft(str, len, char)
- if char == nil then char = ' ' end
- if (str == nil) or (string.len(str) == 0) then
- return string.rep(char, len)
- else
- if string.len(str) > len then
- return string.sub(str,1,len)
- else
- return str .. string.rep(char, len - string.len(str))
- end
- end
- end
- function padRight(str, len, char)
- if char == nil then char = ' ' end
- if (str == nil) or (string.len(str) == 0) then
- return string.rep(char, len)
- else
- if string.len(str) > len then
- return string.sub(str,1,len)
- else
- return string.rep(char, len - string.len(str)) .. str
- end
- end
- end
- -- ######################################################
- -- ## "Encrpytion" Functions ##
- -- ######################################################
- -- From the ComputerCraft forums, mostly is obscuring strings
- -- rather than "true" encryption, but works for what we need
- -- which is not sending plaintext PW's over rednet.
- -- http://bit.ly/13zuMU9
- local function enc(str, key)
- if not key then return false end
- local f = ""
- local kb = {}
- for i = 1, #key do
- kb[i] = string.byte(key:sub(i,i)) - 31
- end
- local c = 1
- for i = 1, #str do
- local t1 = string.byte(str:sub(i,i))
- if t1 == 10 then
- f = f .. string.char(10)
- else
- local t2 = t1 + kb[c]
- if t2 > 126 then
- local t3 = t2 - 126
- t2 = 31 + t3
- end
- f = f .. string.char(t2)
- end
- c = c + 1
- if c > #key then
- c = 1
- end
- end
- return f
- end
- local function dec(str, key)
- if not key then return false end
- local f = ""
- local kb = {}
- for i = 1, #key do
- kb[i] = string.byte(key:sub(i,i)) - 31
- end
- local c = 1
- for i = 1, #str do
- local t2 = string.byte(str:sub(i,i)) - 31
- if t2 == -21 then
- f = f .. string.char(10)
- else
- local t3 = t2 - kb[c] + 31
- if t3 < 32 then
- t3 = t3 + 126 - 31
- end
- if t3 < 32 then return false, t3 end
- if t3 > 126 then return false, t3 end
- f = f .. string.char(t3)
- end
- c = c + 1
- if c > #key then
- c = 1
- end
- end
- return f
- end
- function encrypt(str, key)
- if not key then return false end
- if #key < 3 then return false end
- local f = str
- key = key .. key .. "#me1"
- for i = 0, #key - 1 do
- f = enc(f, key:sub(i,i + 1))
- end
- f = enc(f, key:sub(1,2) .. key:sub(#key - 1, #key) .. enc(key, key .. key))
- return f
- end
- function decrypt(str, key)
- if not key then return false end
- if #key < 3 then return false end
- local f = str
- key = key .. key .. "#me1"
- for i = 0, #key - 1 do
- f = dec(f, key:sub(i,i + 1))
- end
- f = dec(f, key:sub(1,2) .. key:sub(#key - 1, #key) .. enc(key, key .. key))
- return f
- end
- -- ######################################################
- -- ## Terminal Functions ##
- -- ######################################################
- function setupScreen()
- local newScreen = {}
- for i = 4, 16 do
- newScreen[i] = ""
- end
- return newScreen
- end
- function addLine(screen,newLine)
- local newScreen = {}
- for i = 4,15 do
- newScreen[i] = screen[i+1]
- end
- newScreen[16] = newLine
- return newScreen
- end
- function addPrintLine(screen,newLine)
- local newScreen = {}
- for i = 4,15 do
- newScreen[i] = screen[i+1]
- end
- newScreen[16] = newLine
- for i = 4,16 do
- term.setCursorPos(1,i)
- term.write("| "..padLeft(newScreen[i],47," ").." |")
- end
- return newScreen
- end
- function printScreen(screen)
- for i = 4,16 do
- term.setCursorPos(1,i)
- print("| "..padLeft(screen[i],47," ").." |")
- end
- end
- function justPrintLine(screen,newLine)
- screen = addPrintLine(screen,newLine)
- --printScreen(screen)
- return screen
- end
- function printUILine(newLine)
- term.setCursorPos(1,18)
- term.write("| "..padLeft(newLine,47," ").." |")
- end
- -- ######################################################
- -- ## User Input Functions ##
- -- ######################################################
- function getInput(prompt)
- printUILine(prompt)
- term.setCursorPos(3+string.len(prompt)+1,18)
- local input = read()
- term.setCursorPos(1,1)
- return input
- end
- function getConfigInput(prompt)
- write(prompt)
- local input = read()
- return input
- end
- function getNumericInput(screen,prompt)
- local input = getInput(prompt)
- while (tonumber(input) == nil) do
- screen = addPrintLine(screen,"Invalid entry, value must be numeric.")
- printScreen(screen)
- input = getInput(prompt)
- end
- return input
- end
- function getPasswordInput(prompt)
- printUILine(prompt)
- term.setCursorPos(3+string.len(prompt)+1,18)
- local input = read("*")
- term.setCursorPos(1,1)
- return input
- end
- -- ######################################################
- -- ## Config Functions ##
- -- ######################################################
- function caseCheck(config,questionable)
- if (config == nil) then return nil end
- for option, settings in pairs(config) do
- if (string.upper(questionable) == string.upper(option)) then
- return option
- end
- end
- return nil
- end
- function addConfigOption(config,named,default,prompt,optionType)
- local t = {}
- t["value"] = default
- t["default"] = default
- t["prompt"] = prompt
- if (optionType ~= nil) then
- t["type"] = optionType
- else
- t["type"] = 'normal'
- end
- config[named] = t
- return config
- end
- function setConfigOption(config,named,newValue)
- local t = config[caseCheck(config,named)]
- t["value"] = newValue
- config[named] = t
- return config
- end
- function getConfigOption(config,named)
- if (caseCheck(config,named) == nil) then return nil end
- local t = config[caseCheck(config,named)]
- local val = t["value"]
- if (tonumber(val) ~= nil) then
- -- it's a number, pass it back through tonumber just in case
- val = tonumber(val)
- return val
- else
- return val
- end
- end
- function getConfigDefault(config,named)
- local t = config[caseCheck(config,named)]
- local val = t["default"]
- if (tonumber(val) ~= nil) then
- -- it's a number, pass it back through tonumber just in case
- val = tonumber(val)
- return val
- else
- return val
- end
- end
- function getConfigPrompt(config,named)
- local t = config[caseCheck(config,named)]
- local val = t["prompt"]
- if (val == nil) or (string.len(val) == 0) then
- val = "X"
- end
- return val
- end
- function getValidateInput(screen,prompt,acceptable,default)
- local input = ""
- repeat
- if (input ~= "") then
- screen = addPrintLine(screen,"Invalid entry. Acceptable values are: ")
- for i,v in ipairs(acceptable) do
- if (string.lower(v)~= string.lower(default)) then
- screen = addPrintLine(screen," "..v)
- else
- screen = addPrintLine(screen," *"..v)
- end
- end
- printScreen(screen)
- end
- input = getInput(prompt)
- until (tableContainsValue(acceptable,input) or (input == ""))
- if (input == "") then
- input = default
- end
- return input
- end
- function getConfigFromUser(screen,config,named)
- local rightName = caseCheck(config,named)
- local t = config[rightName]
- local newValue = ''
- if (t["type"] == 'normal') then
- newValue = getInput(getConfigPrompt(config,rightName))
- elseif (t["type"] == 'side') then
- local sideValidation = redstone.getSides()
- newValue = getValidateInput(screen,getConfigPrompt(config,rightName),redstone.getSides(),getConfigDefault(config,rightName))
- elseif (t["type"] == 'pass') then
- newValue = getPasswordInput(getConfigPrompt(config,rightName))
- elseif (t["type"] == 'numeric') then
- newValue = getNumericInput(screen,getConfigPrompt(config,rightName))
- end
- if (newValue == "") then
- newValue = getConfigDefault(config,rightName)
- end
- config = setConfigOption(config,caseCheck(config,named),newValue)
- return screen, config
- end
- function getConfigSideFromUser(screen,config,named)
- local sideValidation = redstone.getSides()
- local newValue = getValidateInput(screen,getConfigPrompt(config,caseCheck(config,named)),redstone.getSides(),getConfigDefault(config,caseCheck(config,named)))
- config = setConfigOption(config,caseCheck(config,named),newValue)
- return screen, config
- end
- function checkPassword(screen,config,named)
- local t = config[caseCheck(config,named)]
- if (t == nil) then
- return true
- end
- if (t["type"] ~= 'pass') then -- is it a "password" type
- return true -- if not, just return true
- end
- local rightPW = t["value"]
- if (rightPW == nil) or (rightPW == "") then
- return true
- end
- return (rightPW == getPasswordInput(t["prompt"]))
- end
- function verifyConfig(default,config)
- -- verify all 'default' config options are in the new config table
- for option,settings in pairs(default) do
- local t = config[option]
- if (t == nil) then
- config[option] = settings
- end
- end
- -- verify there are no deprecated config options
- for option,settings in pairs(config) do
- local t = default[option]
- if (t == nil) then
- config[option] = nil
- end
- end
- return config
- end
- function printConfig(config,screen,which)
- if (which == nil) then
- for option,settings in pairsByKeys(config) do
- --print(dump(settings))
- --os.sleep(1.5)
- if (settings["type"] ~= 'pass') then
- screen = addLine(screen,padLeft(option,12," ")..": "..padLeft(settings["value"],36," "))
- else
- if (settings["value"] ~= nil) and (settings["value"] ~= "") then
- screen = addLine(screen,padLeft(option,12," ")..": "..padLeft("",10,"*"))
- else
- screen = addLine(screen,padLeft(option,12," ")..": ".."<<not set>>")
- end
- end
- end
- else
- which = caseCheck(config,which)
- local settings = config[which]
- if (settings["type"] ~= 'pass') then
- screen = addLine(screen,padLeft(which,12," ")..": "..padLeft(settings["value"],36," "))
- else
- if (settings["value"] ~= nil) and (settings["value"] ~= "") then
- screen = addLine(screen,padLeft(which,12," ")..": "..padLeft("",10,"*"))
- else
- screen = addLine(screen,padLeft(which,12," ")..": ".."<<not set>>")
- end
- end
- end
- printScreen(screen)
- return screen
- end
- -- ######################################################
- -- ## Old Print Functions ##
- -- ######################################################
- function printLine(output)
- -- just in case I need any other logic, using an API function
- --print(output)
- term.write(output)
- local x, y = term.getCursorPos()
- term.setCursorPos(1,y+1)
- end
- function changeUpdate(output)
- oldX, oldY = term.getCursorPos()
- term.setCursorPos(14,16)
- write(padLeft(output,38," "))
- term.setCursorPos(oldX,oldY)
- end
- function centerLine(output)
- x, _ = term.getSize()
- _, y = term.getCursorPos()
- term.setCursorPos((x-#output)/2,y)
- print(output)
- end
- function termClear(versionString)
- term.clear()
- x, _ = term.getSize()
- term.setCursorPos(x-#tostring(os.getComputerID()),1)
- write(tostring(os.getComputerID()))
- centerLine("--==[ "..versionString.." ]==--")
- end
- function drawHeader(header3,header4,header5,header6,networkName,modemSide,col3,col4,col5,col6)
- local divider = "---------------------------------------------------"
- printLine(divider)
- printLine(padLeft("Network",12," ").." "..padLeft("Modem",8," ")..padLeft(header3,8," ")..padLeft(header4,8," ")..padLeft(header5,8," ")..padLeft(header6,5," "))
- printLine(divider)
- printLine(padLeft(networkName,12," ").." "..padLeft(modemSide,8," ")..padLeft(col3,8," ")..padLeft(col4,8," ")..padLeft(col5,8," ")..padLeft(col6,5," "))
- printLine(divider)
- end
- function printUpdateLine(content)
- local oldX, oldY = term.getCursorPos()
- term.setCursorPos(3,16)
- print(padLeft(content,1,47))
- term.setCursorPos(oldX,oldY)
- end
- -- ######################################################
- -- ## Terminal Templates ##
- -- ######################################################
- function printFrame()
- term.clear()
- term.setCursorPos(1,1)
- printLine("+-------------------------------------------------+")
- printLine("| |")
- printLine("+-------------------------------------------------+")
- for i = 4,16 do
- printLine("| |")
- end
- printLine("+-------------------------------------------------+")
- printLine("| |")
- printLine("+-------------------------------------------------+")
- end
- function printMotorTemplate(config)
- printFrame()
- term.setCursorPos(3,2)
- if (config ~= nil) and (table_count(config) > 0) then
- write(padLeft("EC: "..getConfigOption(config,"Version").." | Network: "..getConfigOption(config,"Network"),47," "))
- end
- end
- function printDoorTemplate(config)
- printFrame()
- term.setCursorPos(3,2)
- if (config ~= nil) and (table_count(config) > 0) then
- write(padLeft("EC: "..getConfigOption(config,"Version").." | Network: "..getConfigOption(config,"Network"),47," "))
- end
- end
- function printCabinTemplate(config)
- printFrame()
- term.setCursorPos(3,2)
- if (config ~= nil) and (table_count(config) > 0) then
- write(padLeft(getConfigOption(config,"Intro"),47," "))
- term.setCursorPos(3,4)
- write(padLeft(getConfigOption(config,"Floor"),6," ").."| "..padLeft(getConfigOption(config,"Description"),36," "))
- term.setCursorPos(1,5)
- printLine("+-------------------------------------------------+")
- end
- end
- -- ######################################################
- -- ## Communcation Functions ##
- -- ######################################################
- function openModem(whichSide)
- whichSide = tostring(whichSide)
- --print("Opening side: ["..whichSide.."]")
- --BUGFIX 03/25/2013 - changed "nil" to "false"
- if (rednet.isOpen(whichSide) == false) then
- rednet.open(whichSide)
- end
- end
- function broadcast(networkName,message)
- message = networkName.."|"..message
- --openModem(modemSide)
- --print("Broadcasting: "..message)
- rednet.broadcast(message)
- end
- function send(networkName,id,message)
- message = networkName.."|"..message
- --printLine("Sending ["..id.."]: "..message)
- --openModem(modemSide)
- rednet.send(id,message)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement