Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = {...}
- function printUsage()
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(configColors.err)
- print("Usages:")
- print(" SmelteryIO run")
- print(" SmelteryIO config")
- print(" SmelteryIO help")
- end
- local finishedInitialConfigurationSetup = false
- local termWidth,termHeight = term.getSize()
- local configPath = shell.resolve("").."/SmelteryIO.config"
- local usedConnections = {}
- local configColors = {
- title=colors.cyan,
- prompt=colors.yellow,
- err=colors.red,
- success=colors.green,
- text=colors.white
- }
- local configData = {
- controller=nil,
- patternChest=nil,
- triggerInventory=nil,
- connections={}
- }
- local setup = {}
- -- configurator function
- function getInventoryPeripheral(prompt)
- while true do
- term.setTextColor(configColors.prompt)
- print(prompt)
- print("Press any key to cancel")
- local e,side
- local downed = false
- while true do
- e,side = os.pullEvent()
- if e == "key" then
- downed = true
- elseif e == "key_up" and downed then
- return nil
- elseif e == "peripheral" then
- break
- end
- end
- local perip = peripheral.wrap(side)
- if perip.pushItems ~= nil and type(perip.pushItems) == "function" then
- term.setTextColor(configColors.success)
- print("Successfully selected peripheral:")
- term.setTextColor(configColors.text)
- print("\""..side.."\"")
- return side
- end
- term.setTextColor(configColors.err)
- print("Invalid peripheral selected. Peripheral does not have a pushItems method:")
- term.setTextColor(configColors.text)
- print("\""..side.."\"")
- end
- end
- function getPeripheral(prompt,peripheralType)
- while true do
- term.setTextColor(configColors.prompt)
- print(prompt)
- print("Press any key to cancel")
- local e,side
- local downed = false
- while true do
- e,side = os.pullEvent()
- if e == "key" then
- downed = true
- elseif e == "key_up" and downed then
- return nil
- elseif e == "peripheral" then
- break
- end
- end
- if string.find(side,peripheralType,1,true) ~= nil then
- term.setTextColor(configColors.success)
- print("Successfully selected peripheral:")
- term.setTextColor(configColors.text)
- print("\""..side.."\"")
- return side
- end
- term.setTextColor(configColors.err)
- print("Invalid peripheral selected:")
- term.setTextColor(configColors.text)
- print("\""..side.."\"")
- end
- end
- function setController(initial)
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(configColors.title)
- if initial then
- print("SmelteryIO Configurator - Initial Setup")
- else
- print("SmelteryIO Configurator - Set Smeltery Controller")
- end
- local perip = getPeripheral("Please connect a Smeltery Controller to the network","smeltery_controller")
- if perip == nil then
- return false
- end
- configData.controller = perip
- if not initial then
- term.setTextColor(configColors.prompt)
- print("Press any key to return to the main menu")
- os.pullEvent("key")
- os.pullEvent("key_up")
- end
- return true
- end
- function setPatternChest(initial)
- if not initial then
- term.clear()
- term.setTextColor(configColors.title)
- term.setCursorPos(1,1)
- print("SmelteryIO Configurator - Set Pattern Chest")
- end
- local perip = getInventoryPeripheral("Please connect an inventory to the network that will hold your casts for the smeltery")
- if perip == nil then
- return false
- end
- configData.patternChest = perip
- if not initial then
- term.setTextColor(configColors.prompt)
- print("Press any key to return to the main menu")
- os.pullEvent("key")
- os.pullEvent("key_up")
- end
- return true
- end
- function setTriggerChest(initial)
- if not initial then
- term.clear()
- term.setTextColor(configColors.title)
- term.setCursorPos(1,1)
- print("SmelteryIO Configurator - Set Trigger Chest")
- end
- local perip = getInventoryPeripheral("Please connect an inventory to the network that will contain an item to trigger the smeltery drains")
- if perip == nil then
- return false
- end
- configData.triggerInventory = perip
- term.setTextColor(configColors.prompt)
- if initial then
- print("Completed initial setup. Press any key to return to")
- print("the main menu")
- else
- print("Press any key to return to the main menu")
- end
- os.pullEvent("key")
- os.pullEvent("key_up")
- return true
- end
- function initialConfigurationSetup()
- if setController(true) then
- if setPatternChest(true) then
- if setTriggerChest(true) then
- finishedInitialConfigurationSetup = true
- end
- end
- end
- end
- function addConnection(connectionType)
- term.clear()
- term.setTextColor(configColors.title)
- term.setCursorPos(1,1)
- local connection = {}
- if connectionType == "basin" then
- print("SmelteryIO Configurator - Add Casting Basin")
- else
- print("SmelteryIO Configurator - Add Casting Table")
- end
- while true do
- term.setTextColor(configColors.prompt)
- if connectionType == "basin" then
- print("Please connect a casting basin to the network, or press any key to go back to the main menu")
- else
- print("Please connect a casting table to the network, or press any key to go back to the main menu")
- end
- local e,side
- local downed = false
- while true do
- e,side = os.pullEvent()
- if e == "key" then
- downed = true
- elseif e == "key_up" and downed then
- return nil
- elseif e == "peripheral" then
- break
- end
- end
- if (connectionType == "basin" and string.find(side,"casting_basin",1,true) ~= nil) or (connectionType == "table" and string.find(side,"casting_table",1,true) ~= nil) then
- local used = false
- for s=1,#usedConnections,1 do
- if usedConnections[s] == side then
- used = true
- break
- end
- end
- if not used then
- term.setTextColor(configColors.success)
- print("Successfully selected peripheral:")
- term.setTextColor(configColors.text)
- print("\""..side.."\"")
- connection["container"] = side
- break
- else
- term.setTextColor(configColors.err)
- print("A connection has already been added for selected peripheral:")
- term.setTextColor(configColors.text)
- print("\""..side.."\"")
- end
- else
- term.setTextColor(configColors.err)
- print("Invalid peripheral selected:")
- term.setTextColor(configColors.text)
- print("\""..side.."\"")
- end
- end
- while true do
- term.setTextColor(configColors.prompt)
- if connectionType == "basin" then
- print("Please connect an inventory to the network that will trigger the faucet for this basin, or press any key to go back to the main menu")
- else
- print("Please connect an inventory to the network that will trigger the faucet for this table, or press any key to go back to the main menu")
- end
- local e,side
- local downed = false
- while true do
- e,side = os.pullEvent()
- if e == "key" then
- downed = true
- elseif e == "key_up" and downed then
- return nil
- elseif e == "peripheral" then
- break
- end
- end
- local inv = peripheral.wrap(side)
- if inv.pushItems ~= nil and type(inv.pushItems) == "function" then
- term.setTextColor(configColors.success)
- print("Successfully selected peripheral:")
- term.setTextColor(configColors.text)
- print("\""..side.."\"")
- connection["inv"] = side
- break
- end
- term.setTextColor(configColors.err)
- print("Invalid peripheral selected. No \"pushItems\" method found for peripheral:")
- term.setTextColor(configColors.text)
- print("\""..side.."\"")
- end
- term.setTextColor(configColors.prompt)
- table.insert(usedConnections,connection.container)
- connection["type"] = connectionType
- table.insert(configData.connections,connection)
- if connectionType == "basin" then
- print("Successfully added basin. Press any key to return to the main menu")
- else
- print("Successfully added table. Press any key to return to the main menu")
- end
- os.pullEvent("key_up")
- end
- function runConfigurator()
- configData = {
- controller=nil,
- patternChest=nil,
- triggerInventory=nil,
- connections={}
- }
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(configColors.title)
- print("SmelteryIO Configurator")
- term.setTextColor(configColors.text)
- print("Before continuing, please make sure all peripherals")
- print("that will be used by SmelteryIO are disconnected")
- print("from the network.")
- term.setTextColor(configColors.prompt)
- print("Press any key to continue")
- os.pullEvent("key")
- os.pullEvent("key_up")
- while true do
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(configColors.title)
- print("SmelteryIO Configurator - Main Menu")
- local options = {}
- if finishedInitialConfigurationSetup then
- table.insert(options,{operation="set_controller",display="Set Smeltery Controller"})
- table.insert(options,{operation="set_pattern_chest",display="Set Pattern Chest"})
- table.insert(options,{operation="set_trigger_chest",display="Set Trigger Chest"})
- table.insert(options,{operation="add_casting_basin",display="Add Casting Basin"})
- table.insert(options,{operation="add_casting_table",display="Add Casting Table"})
- else
- table.insert(options,{operation="initial_setup",display="Initial Setup"})
- end
- table.insert(options,{operation="help",display="Help"})
- if finishedInitialConfigurationSetup and #configData.connections > 0 then
- table.insert(options,{operation="save",display="Save & Quit"})
- end
- table.insert(options,{operation="quit",display="Quit Without Saving"})
- for oi = 1,#options,1 do
- term.setTextColor(configColors.prompt)
- write(oi..": ")
- term.setTextColor(configColors.text)
- print(options[oi].display)
- end
- term.setTextColor(configColors.prompt)
- term.setCursorPos(1,termHeight-2)
- print("Enter a number from 1 to "..#options..":")
- local operation = nil
- while operation == nil do
- term.setTextColor(configColors.text)
- term.clearLine(termHeight-1)
- term.setCursorPos(1,termHeight-1)
- local response = tonumber(read())
- os.pullEvent("key_up")
- term.setTextColor(configColors.err)
- term.setCursorPos(1,termHeight-4)
- if response == nil then
- print("Invalid input. Input must be a number.")
- elseif response < 1 then
- print("Invalid input. Value too low. ")
- elseif response > #options then
- print("Invalid input. Value too high. ")
- else
- operation = options[response].operation
- end
- end
- term.setTextColor(configColors.text)
- if operation == "add_casting_basin" then
- addConnection("basin")
- elseif operation == "add_casting_table" then
- addConnection("table")
- elseif operation == "initial_setup" then
- initialConfigurationSetup()
- elseif operation == "set_controller" then
- setController(false)
- elseif operation == "set_pattern_chest" then
- setPatternChest(false)
- elseif operation == "set_trigger_chest" then
- setTriggerChest(false)
- elseif operation == "quit" then
- term.clear()
- term.setCursorPos(1,1)
- break
- elseif operation == "help" then
- showHelp()
- elseif operation == "save" then
- if fs.exists(configPath) then
- fs.delete(configPath)
- end
- local file = fs.open(configPath,"w")
- file.writeLine(textutils.serialize(configData))
- file.close()
- term.clear()
- term.setCursorPos(1,1)
- break
- end
- end
- end
- --run functions
- local orderQueue = {}
- function parseConfigData()
- term.setCursorPos(1,1)
- term.setTextColor(configColors.err)
- if not fs.exists(configPath) then
- return false,"Configuration file not set up"
- end
- local file = fs.open(configPath,"r")
- configData = textutils.unserialize(file.readAll())
- file.close()
- if configData == nil then
- return false,"Error parsing configuration file"
- end
- if configData.controller == nil then
- return false,"Smeltery Controller not defined in configuration file"
- end
- if configData.patternChest == nil then
- return false,"Pattern Chest not defined in configuration file"
- end
- if configData.triggerInventory == nil then
- return false,"Trigger Inventory not defined in configuration file"
- end
- if configData.connections == nil then
- return false,"Connections not defined in configuration file"
- end
- if type(configData.connections) ~= "table" then
- return false,"Connections defined in configuration file should be a table"
- end
- if #configData.connections == 0 then
- return false,"Connections defined in configuration file is empty"
- end
- for _,conn in pairs(configData.connections) do
- if conn.inv == nil then
- return false,"Improperly defined connection in configuration file. Missing inv property"
- end
- if conn.container == nil then
- return false,"Improperly defined connection in configuration file. Missing container property"
- end
- if conn.type == nil then
- return false,"Improperly defined connection in configuration file. Missing type property"
- end
- if conn.type ~= "basin" and conn.type ~= "table" then
- return false,"Improperly defined connection in configuration file. type property should be either \"basin\" or \"table\""
- end
- end
- return true
- end
- function wrapPeripherals()
- setup = {
- controller=nil,
- patternChest=nil,
- triggerInventory=nil,
- basins={},
- tables={}
- }
- local setupValid = true
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(configColors.err)
- if not peripheral.isPresent(configData.controller) then
- print("- Smeltery Controller specified in configuration file is not present")
- setupValid = false
- else
- setup["controller"] = peripheral.wrap(configData.controller)
- end
- if not peripheral.isPresent(configData.patternChest) then
- print("- Pattern Chest specified in configuration file is not present")
- setupValid = false
- else
- setup["- patternChest"] = peripheral.wrap(configData.patternChest)
- end
- if not peripheral.isPresent(configData.triggerInventory) then
- print("- Trigger Inventory specified in configuration file is not present")
- setupValid = false
- else
- setup["- triggerInventory"] = peripheral.wrap(configData.triggerInventory)
- end
- for _,conn in pairs(configData.connections) do
- if peripheral.isPresent(conn.container) and peripheral.isPresent(conn.inv) then
- if conn.type == "basin" then
- table.insert(setup.basins,{
- container=peripheral.wrap(conn.container),
- inv=peripheral.wrap(conn.inv)
- })
- else
- table.insert(setup.tables,{
- container=peripheral.wrap(conn.container),
- inv=peripheral.wrap(conn.inv)
- })
- end
- end
- end
- if #setup.basins == 0 and #setup.tables == 0 then
- print("- Could not form any complete connections. Peripherals specified in the configuration file for the connections are not present")
- setupValid = false
- end
- return setupValid
- end
- function runSmelteryIO()
- local success,message = parseConfigData()
- if not success then
- term.setTextColor(configColors.err)
- term.setCursorPos(1,1)
- print(message)
- term.setTextColor(configColors.prompt)
- print("Press any key to continue to the SmelteryIO Configurator")
- os.pullEvent("key")
- os.pullEvent("key_up")
- runConfigurator()
- return nil
- end
- while true do
- if wrapPeripherals() then
- break
- end
- term.setTextColor(configColors.prompt)
- print("Press any key to continue")
- os.pullEvent("key")
- os.pullEvent("key_up")
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(configColors.err)
- print("The configuration file is valid, but the peripherals specified in it are not present")
- term.setTextColor(configColors.prompt)
- write("1: ")
- term.setTextColor(configColors.text)
- print("Retry connecting peripherals")
- term.setTextColor(configColors.prompt)
- write("2: ")
- term.setTextColor(configColors.text)
- print("Run configurator")
- term.setTextColor(configColors.prompt)
- write("3: ")
- term.setTextColor(configColors.text)
- print("Quit")
- term.setCursorPos(1,termHeight-2)
- term.setTextColor(configColors.prompt)
- print("Enter a number from 1-3:")
- local operation = nil
- while operation == nil do
- term.setTextColor(configColors.text)
- term.clearLine(termHeight-1)
- term.setCursorPos(1,termHeight-1)
- local response = tonumber(read())
- os.pullEvent("key_up")
- term.setTextColor(configColors.err)
- term.setCursorPos(1,termHeight-4)
- if response == nil then
- print("Invalid input. Input must be a number.")
- elseif response < 1 then
- print("Invalid input. Value too low. ")
- elseif response > 3 then
- print("Invalid input. Value too high. ")
- elseif response == 1 then
- operation = "retry"
- elseif response == 2 then
- operation = "run_configurator"
- elseif response == 3 then
- operation = "quit"
- end
- end
- if operation == "quit" then
- return nil
- elseif operation == "run_configurator" then
- runConfigurator()
- return nil
- end
- --just continue for retry
- end
- end
- --main
- if #tArgs == 0 then
- printUsage()
- elseif tArgs[1] == "config" then
- term.clear()
- runConfigurator()
- elseif tArgs[1] == "run" then
- term.clear()
- runSmelteryIO()
- else
- printUsage()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement