Advertisement
Guest User

factory_companion

a guest
Jul 7th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.00 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local serialization = require("serialization")
  4. local keyboard = require("keyboard")
  5. local shell = require("shell")
  6.  
  7. local outputFile = assert(..., "file expected")
  8.  
  9. local function checkedInput(f, check, hint)
  10.   while true do
  11.     local text = io.read()
  12.     if text == nil then
  13.       return nil
  14.     end
  15.     local output = f(text)
  16.     if output and check(output) then
  17.       return output
  18.     end
  19.     if hint then
  20.       io.write(hint)
  21.     end
  22.   end
  23. end
  24.  
  25. io.write("enter number of component types: ")
  26. local ntypes = checkedInput(tonumber, function(i) return i>0 and i%1==0 end, " expected positive integer: ")
  27. if ntypes then
  28.   local outputTable = {}
  29.   local names = {}
  30.   for i = 1, ntypes do
  31.     io.write(("enter name #%u: "):format(i))
  32.     local name = io.read()
  33.     if not name then return end
  34.     names[i] = name
  35.   end
  36.   local i, cycle = 0, 0
  37.   local function continue()
  38.     i = i % ntypes + 1
  39.     if i == 1 then cycle = cycle + 1 end
  40.     print(("waiting for %s (space to skip, enter to finish)"):format(names[i]))
  41.   end
  42.   continue()
  43.   local function get(t, key)
  44.     local v = t[key]
  45.     if v == nil then
  46.       v = {}
  47.       t[key] = v
  48.     end
  49.     return v
  50.   end
  51.   local function set(t, key, index, value)
  52.     if key == "" then
  53.       t[index] = value
  54.       return
  55.     end
  56.     local prefix, suffix = string.match(key, "^([^%.]+)%.?(.-)$")
  57.     return set(get(t, prefix), suffix, index, value)
  58.   end
  59.   while true do
  60.     local e, a, b, c = event.pull()
  61.     if e == "key_down" then
  62.       if c == keyboard.keys.enter then
  63.         break
  64.       elseif c == keyboard.keys.space then
  65.         print("skipped")
  66.         continue()
  67.       end
  68.     elseif e == "component_added" then
  69.       print("added" .. a)
  70.       set(outputTable, names[i], cycle, a)
  71.       continue()
  72.     end
  73.   end
  74.   local stream = io.open(outputFile, "w")
  75.   stream:write("return ")
  76.   stream:write(serialization.serialize(outputTable))
  77.   stream:close()
  78. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement