Advertisement
JereTheJuggler

Untitled

Feb 25th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.98 KB | None | 0 0
  1. local tArgs = {...}
  2.  
  3. function printUsage()
  4. term.clear()
  5. term.setCursorPos(1,1)
  6. term.setTextColor(configColors.err)
  7. print("Usages:")
  8. print(" SmelteryIO run")
  9. print(" SmelteryIO config")
  10. print(" SmelteryIO help")
  11. end
  12.  
  13. local finishedInitialConfigurationSetup = false
  14. local termWidth,termHeight = term.getSize()
  15. local configPath = shell.resolve("").."/SmelteryIO.config"
  16. local usedConnections = {}
  17. local configColors = {
  18. title=colors.cyan,
  19. prompt=colors.yellow,
  20. err=colors.red,
  21. success=colors.green,
  22. text=colors.white
  23. }
  24.  
  25. local configData = {
  26. controller=nil,
  27. patternChest=nil,
  28. triggerInventory=nil,
  29. connections={}
  30. }
  31. local setup = {}
  32.  
  33. -- configurator function
  34.  
  35. function getInventoryPeripheral(prompt)
  36. while true do
  37. term.setTextColor(configColors.prompt)
  38. print(prompt)
  39. print("Press any key to cancel")
  40. local e,side
  41. local downed = false
  42. while true do
  43. e,side = os.pullEvent()
  44. if e == "key" then
  45. downed = true
  46. elseif e == "key_up" and downed then
  47. return nil
  48. elseif e == "peripheral" then
  49. break
  50. end
  51. end
  52. local perip = peripheral.wrap(side)
  53. if perip.pushItems ~= nil and type(perip.pushItems) == "function" then
  54. term.setTextColor(configColors.success)
  55. print("Successfully selected peripheral:")
  56. term.setTextColor(configColors.text)
  57. print("\""..side.."\"")
  58. return side
  59. end
  60. term.setTextColor(configColors.err)
  61. print("Invalid peripheral selected. Peripheral does not have a pushItems method:")
  62. term.setTextColor(configColors.text)
  63. print("\""..side.."\"")
  64. end
  65. end
  66.  
  67. function getPeripheral(prompt,peripheralType)
  68. while true do
  69. term.setTextColor(configColors.prompt)
  70. print(prompt)
  71. print("Press any key to cancel")
  72. local e,side
  73. local downed = false
  74. while true do
  75. e,side = os.pullEvent()
  76. if e == "key" then
  77. downed = true
  78. elseif e == "key_up" and downed then
  79. return nil
  80. elseif e == "peripheral" then
  81. break
  82. end
  83. end
  84. if string.find(side,peripheralType,1,true) ~= nil then
  85. term.setTextColor(configColors.success)
  86. print("Successfully selected peripheral:")
  87. term.setTextColor(configColors.text)
  88. print("\""..side.."\"")
  89. return side
  90. end
  91. term.setTextColor(configColors.err)
  92. print("Invalid peripheral selected:")
  93. term.setTextColor(configColors.text)
  94. print("\""..side.."\"")
  95. end
  96. end
  97.  
  98. function setController(initial)
  99. term.clear()
  100. term.setCursorPos(1,1)
  101. term.setTextColor(configColors.title)
  102. if initial then
  103. print("SmelteryIO Configurator - Initial Setup")
  104. else
  105. print("SmelteryIO Configurator - Set Smeltery Controller")
  106. end
  107. local perip = getPeripheral("Please connect a Smeltery Controller to the network","smeltery_controller")
  108. if perip == nil then
  109. return false
  110. end
  111. configData.controller = perip
  112. if not initial then
  113. term.setTextColor(configColors.prompt)
  114. print("Press any key to return to the main menu")
  115. os.pullEvent("key")
  116. os.pullEvent("key_up")
  117. end
  118. return true
  119. end
  120. function setPatternChest(initial)
  121. if not initial then
  122. term.clear()
  123. term.setTextColor(configColors.title)
  124. term.setCursorPos(1,1)
  125. print("SmelteryIO Configurator - Set Pattern Chest")
  126. end
  127. local perip = getInventoryPeripheral("Please connect an inventory to the network that will hold your casts for the smeltery")
  128. if perip == nil then
  129. return false
  130. end
  131. configData.patternChest = perip
  132. if not initial then
  133. term.setTextColor(configColors.prompt)
  134. print("Press any key to return to the main menu")
  135. os.pullEvent("key")
  136. os.pullEvent("key_up")
  137. end
  138. return true
  139. end
  140. function setTriggerChest(initial)
  141. if not initial then
  142. term.clear()
  143. term.setTextColor(configColors.title)
  144. term.setCursorPos(1,1)
  145. print("SmelteryIO Configurator - Set Trigger Chest")
  146. end
  147. local perip = getInventoryPeripheral("Please connect an inventory to the network that will contain an item to trigger the smeltery drains")
  148. if perip == nil then
  149. return false
  150. end
  151. configData.triggerInventory = perip
  152. term.setTextColor(configColors.prompt)
  153. if initial then
  154. print("Completed initial setup. Press any key to return to")
  155. print("the main menu")
  156. else
  157. print("Press any key to return to the main menu")
  158. end
  159. os.pullEvent("key")
  160. os.pullEvent("key_up")
  161. return true
  162. end
  163.  
  164. function initialConfigurationSetup()
  165. if setController(true) then
  166. if setPatternChest(true) then
  167. if setTriggerChest(true) then
  168. finishedInitialConfigurationSetup = true
  169. end
  170. end
  171. end
  172. end
  173.  
  174. function addConnection(connectionType)
  175. term.clear()
  176. term.setTextColor(configColors.title)
  177. term.setCursorPos(1,1)
  178. local connection = {}
  179. if connectionType == "basin" then
  180. print("SmelteryIO Configurator - Add Casting Basin")
  181. else
  182. print("SmelteryIO Configurator - Add Casting Table")
  183. end
  184. while true do
  185. term.setTextColor(configColors.prompt)
  186. if connectionType == "basin" then
  187. print("Please connect a casting basin to the network, or press any key to go back to the main menu")
  188. else
  189. print("Please connect a casting table to the network, or press any key to go back to the main menu")
  190. end
  191. local e,side
  192. local downed = false
  193. while true do
  194. e,side = os.pullEvent()
  195. if e == "key" then
  196. downed = true
  197. elseif e == "key_up" and downed then
  198. return nil
  199. elseif e == "peripheral" then
  200. break
  201. end
  202. end
  203. 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
  204. local used = false
  205. for s=1,#usedConnections,1 do
  206. if usedConnections[s] == side then
  207. used = true
  208. break
  209. end
  210. end
  211. if not used then
  212. term.setTextColor(configColors.success)
  213. print("Successfully selected peripheral:")
  214. term.setTextColor(configColors.text)
  215. print("\""..side.."\"")
  216. connection["container"] = side
  217. break
  218. else
  219. term.setTextColor(configColors.err)
  220. print("A connection has already been added for selected peripheral:")
  221. term.setTextColor(configColors.text)
  222. print("\""..side.."\"")
  223. end
  224. else
  225. term.setTextColor(configColors.err)
  226. print("Invalid peripheral selected:")
  227. term.setTextColor(configColors.text)
  228. print("\""..side.."\"")
  229. end
  230. end
  231. while true do
  232. term.setTextColor(configColors.prompt)
  233. if connectionType == "basin" then
  234. 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")
  235. else
  236. 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")
  237. end
  238. local e,side
  239. local downed = false
  240. while true do
  241. e,side = os.pullEvent()
  242. if e == "key" then
  243. downed = true
  244. elseif e == "key_up" and downed then
  245. return nil
  246. elseif e == "peripheral" then
  247. break
  248. end
  249. end
  250. local inv = peripheral.wrap(side)
  251. if inv.pushItems ~= nil and type(inv.pushItems) == "function" then
  252. term.setTextColor(configColors.success)
  253. print("Successfully selected peripheral:")
  254. term.setTextColor(configColors.text)
  255. print("\""..side.."\"")
  256. connection["inv"] = side
  257. break
  258. end
  259. term.setTextColor(configColors.err)
  260. print("Invalid peripheral selected. No \"pushItems\" method found for peripheral:")
  261. term.setTextColor(configColors.text)
  262. print("\""..side.."\"")
  263. end
  264. term.setTextColor(configColors.prompt)
  265. table.insert(usedConnections,connection.container)
  266. connection["type"] = connectionType
  267. table.insert(configData.connections,connection)
  268. if connectionType == "basin" then
  269. print("Successfully added basin. Press any key to return to the main menu")
  270. else
  271. print("Successfully added table. Press any key to return to the main menu")
  272. end
  273. os.pullEvent("key_up")
  274. end
  275.  
  276. function runConfigurator()
  277. configData = {
  278. controller=nil,
  279. patternChest=nil,
  280. triggerInventory=nil,
  281. connections={}
  282. }
  283. term.clear()
  284. term.setCursorPos(1,1)
  285. term.setTextColor(configColors.title)
  286. print("SmelteryIO Configurator")
  287. term.setTextColor(configColors.text)
  288. print("Before continuing, please make sure all peripherals")
  289. print("that will be used by SmelteryIO are disconnected")
  290. print("from the network.")
  291. term.setTextColor(configColors.prompt)
  292. print("Press any key to continue")
  293. os.pullEvent("key")
  294. os.pullEvent("key_up")
  295. while true do
  296. term.clear()
  297. term.setCursorPos(1,1)
  298. term.setTextColor(configColors.title)
  299. print("SmelteryIO Configurator - Main Menu")
  300. local options = {}
  301. if finishedInitialConfigurationSetup then
  302. table.insert(options,{operation="set_controller",display="Set Smeltery Controller"})
  303. table.insert(options,{operation="set_pattern_chest",display="Set Pattern Chest"})
  304. table.insert(options,{operation="set_trigger_chest",display="Set Trigger Chest"})
  305. table.insert(options,{operation="add_casting_basin",display="Add Casting Basin"})
  306. table.insert(options,{operation="add_casting_table",display="Add Casting Table"})
  307. else
  308. table.insert(options,{operation="initial_setup",display="Initial Setup"})
  309. end
  310. table.insert(options,{operation="help",display="Help"})
  311. if finishedInitialConfigurationSetup and #configData.connections > 0 then
  312. table.insert(options,{operation="save",display="Save & Quit"})
  313. end
  314. table.insert(options,{operation="quit",display="Quit Without Saving"})
  315. for oi = 1,#options,1 do
  316. term.setTextColor(configColors.prompt)
  317. write(oi..": ")
  318. term.setTextColor(configColors.text)
  319. print(options[oi].display)
  320. end
  321. term.setTextColor(configColors.prompt)
  322. term.setCursorPos(1,termHeight-2)
  323. print("Enter a number from 1 to "..#options..":")
  324. local operation = nil
  325. while operation == nil do
  326. term.setTextColor(configColors.text)
  327. term.clearLine(termHeight-1)
  328. term.setCursorPos(1,termHeight-1)
  329. local response = tonumber(read())
  330. os.pullEvent("key_up")
  331. term.setTextColor(configColors.err)
  332. term.setCursorPos(1,termHeight-4)
  333. if response == nil then
  334. print("Invalid input. Input must be a number.")
  335. elseif response < 1 then
  336. print("Invalid input. Value too low. ")
  337. elseif response > #options then
  338. print("Invalid input. Value too high. ")
  339. else
  340. operation = options[response].operation
  341. end
  342. end
  343. term.setTextColor(configColors.text)
  344. if operation == "add_casting_basin" then
  345. addConnection("basin")
  346. elseif operation == "add_casting_table" then
  347. addConnection("table")
  348. elseif operation == "initial_setup" then
  349. initialConfigurationSetup()
  350. elseif operation == "set_controller" then
  351. setController(false)
  352. elseif operation == "set_pattern_chest" then
  353. setPatternChest(false)
  354. elseif operation == "set_trigger_chest" then
  355. setTriggerChest(false)
  356. elseif operation == "quit" then
  357. term.clear()
  358. term.setCursorPos(1,1)
  359. break
  360. elseif operation == "help" then
  361. showHelp()
  362. elseif operation == "save" then
  363. if fs.exists(configPath) then
  364. fs.delete(configPath)
  365. end
  366. local file = fs.open(configPath,"w")
  367. file.writeLine(textutils.serialize(configData))
  368. file.close()
  369. term.clear()
  370. term.setCursorPos(1,1)
  371. break
  372. end
  373. end
  374. end
  375.  
  376. --run functions
  377.  
  378. local orderQueue = {}
  379.  
  380. function parseConfigData()
  381. term.setCursorPos(1,1)
  382. term.setTextColor(configColors.err)
  383. if not fs.exists(configPath) then
  384. return false,"Configuration file not set up"
  385. end
  386. local file = fs.open(configPath,"r")
  387. configData = textutils.unserialize(file.readAll())
  388. file.close()
  389. if configData == nil then
  390. return false,"Error parsing configuration file"
  391. end
  392. if configData.controller == nil then
  393. return false,"Smeltery Controller not defined in configuration file"
  394. end
  395. if configData.patternChest == nil then
  396. return false,"Pattern Chest not defined in configuration file"
  397. end
  398. if configData.triggerInventory == nil then
  399. return false,"Trigger Inventory not defined in configuration file"
  400. end
  401. if configData.connections == nil then
  402. return false,"Connections not defined in configuration file"
  403. end
  404. if type(configData.connections) ~= "table" then
  405. return false,"Connections defined in configuration file should be a table"
  406. end
  407. if #configData.connections == 0 then
  408. return false,"Connections defined in configuration file is empty"
  409. end
  410. for _,conn in pairs(configData.connections) do
  411. if conn.inv == nil then
  412. return false,"Improperly defined connection in configuration file. Missing inv property"
  413. end
  414. if conn.container == nil then
  415. return false,"Improperly defined connection in configuration file. Missing container property"
  416. end
  417. if conn.type == nil then
  418. return false,"Improperly defined connection in configuration file. Missing type property"
  419. end
  420. if conn.type ~= "basin" and conn.type ~= "table" then
  421. return false,"Improperly defined connection in configuration file. type property should be either \"basin\" or \"table\""
  422. end
  423. end
  424. return true
  425. end
  426.  
  427. function wrapPeripherals()
  428. setup = {
  429. controller=nil,
  430. patternChest=nil,
  431. triggerInventory=nil,
  432. basins={},
  433. tables={}
  434. }
  435. local setupValid = true
  436. term.clear()
  437. term.setCursorPos(1,1)
  438. term.setTextColor(configColors.err)
  439. if not peripheral.isPresent(configData.controller) then
  440. print("- Smeltery Controller specified in configuration file is not present")
  441. setupValid = false
  442. else
  443. setup["controller"] = peripheral.wrap(configData.controller)
  444. end
  445. if not peripheral.isPresent(configData.patternChest) then
  446. print("- Pattern Chest specified in configuration file is not present")
  447. setupValid = false
  448. else
  449. setup["- patternChest"] = peripheral.wrap(configData.patternChest)
  450. end
  451. if not peripheral.isPresent(configData.triggerInventory) then
  452. print("- Trigger Inventory specified in configuration file is not present")
  453. setupValid = false
  454. else
  455. setup["- triggerInventory"] = peripheral.wrap(configData.triggerInventory)
  456. end
  457. for _,conn in pairs(configData.connections) do
  458. if peripheral.isPresent(conn.container) and peripheral.isPresent(conn.inv) then
  459. if conn.type == "basin" then
  460. table.insert(setup.basins,{
  461. container=peripheral.wrap(conn.container),
  462. inv=peripheral.wrap(conn.inv)
  463. })
  464. else
  465. table.insert(setup.tables,{
  466. container=peripheral.wrap(conn.container),
  467. inv=peripheral.wrap(conn.inv)
  468. })
  469. end
  470. end
  471. end
  472. if #setup.basins == 0 and #setup.tables == 0 then
  473. print("- Could not form any complete connections. Peripherals specified in the configuration file for the connections are not present")
  474. setupValid = false
  475. end
  476. return setupValid
  477. end
  478.  
  479. function runSmelteryIO()
  480. local success,message = parseConfigData()
  481. if not success then
  482. term.setTextColor(configColors.err)
  483. term.setCursorPos(1,1)
  484. print(message)
  485. term.setTextColor(configColors.prompt)
  486. print("Press any key to continue to the SmelteryIO Configurator")
  487. os.pullEvent("key")
  488. os.pullEvent("key_up")
  489. runConfigurator()
  490. return nil
  491. end
  492. while true do
  493. if wrapPeripherals() then
  494. break
  495. end
  496. term.setTextColor(configColors.prompt)
  497. print("Press any key to continue")
  498. os.pullEvent("key")
  499. os.pullEvent("key_up")
  500. term.clear()
  501. term.setCursorPos(1,1)
  502. term.setTextColor(configColors.err)
  503. print("The configuration file is valid, but the peripherals specified in it are not present")
  504. term.setTextColor(configColors.prompt)
  505. write("1: ")
  506. term.setTextColor(configColors.text)
  507. print("Retry connecting peripherals")
  508. term.setTextColor(configColors.prompt)
  509. write("2: ")
  510. term.setTextColor(configColors.text)
  511. print("Run configurator")
  512. term.setTextColor(configColors.prompt)
  513. write("3: ")
  514. term.setTextColor(configColors.text)
  515. print("Quit")
  516.  
  517. term.setCursorPos(1,termHeight-2)
  518. term.setTextColor(configColors.prompt)
  519. print("Enter a number from 1-3:")
  520.  
  521. local operation = nil
  522. while operation == nil do
  523. term.setTextColor(configColors.text)
  524. term.clearLine(termHeight-1)
  525. term.setCursorPos(1,termHeight-1)
  526. local response = tonumber(read())
  527. os.pullEvent("key_up")
  528. term.setTextColor(configColors.err)
  529. term.setCursorPos(1,termHeight-4)
  530. if response == nil then
  531. print("Invalid input. Input must be a number.")
  532. elseif response < 1 then
  533. print("Invalid input. Value too low. ")
  534. elseif response > 3 then
  535. print("Invalid input. Value too high. ")
  536. elseif response == 1 then
  537. operation = "retry"
  538. elseif response == 2 then
  539. operation = "run_configurator"
  540. elseif response == 3 then
  541. operation = "quit"
  542. end
  543. end
  544.  
  545. if operation == "quit" then
  546. return nil
  547. elseif operation == "run_configurator" then
  548. runConfigurator()
  549. return nil
  550. end
  551. --just continue for retry
  552. end
  553. end
  554.  
  555. --main
  556.  
  557. if #tArgs == 0 then
  558. printUsage()
  559. elseif tArgs[1] == "config" then
  560. term.clear()
  561. runConfigurator()
  562. elseif tArgs[1] == "run" then
  563. term.clear()
  564. runSmelteryIO()
  565. else
  566. printUsage()
  567. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement