Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local event = require("event")
- local serialization = require("serialization")
- local keyboard = require("keyboard")
- local shell = require("shell")
- local outputFile = assert(..., "file expected")
- local function checkedInput(f, check, hint)
- while true do
- local text = io.read()
- if text == nil then
- return nil
- end
- local output = f(text)
- if output and check(output) then
- return output
- end
- if hint then
- io.write(hint)
- end
- end
- end
- io.write("enter number of component types: ")
- local ntypes = checkedInput(tonumber, function(i) return i>0 and i%1==0 end, " expected positive integer: ")
- if ntypes then
- local outputTable = {}
- local names = {}
- for i = 1, ntypes do
- io.write(("enter name #%u: "):format(i))
- local name = io.read()
- if not name then return end
- names[i] = name
- end
- local i, cycle = 0, 0
- local function continue()
- i = i % ntypes + 1
- if i == 1 then cycle = cycle + 1 end
- print(("waiting for %s (space to skip, enter to finish)"):format(names[i]))
- end
- continue()
- local function get(t, key)
- local v = t[key]
- if v == nil then
- v = {}
- t[key] = v
- end
- return v
- end
- local function set(t, key, index, value)
- if key == "" then
- t[index] = value
- return
- end
- local prefix, suffix = string.match(key, "^([^%.]+)%.?(.-)$")
- return set(get(t, prefix), suffix, index, value)
- end
- while true do
- local e, a, b, c = event.pull()
- if e == "key_down" then
- if c == keyboard.keys.enter then
- break
- elseif c == keyboard.keys.space then
- print("skipped")
- continue()
- end
- elseif e == "component_added" then
- print("added" .. a)
- set(outputTable, names[i], cycle, a)
- continue()
- end
- end
- local stream = io.open(outputFile, "w")
- stream:write("return ")
- stream:write(serialization.serialize(outputTable))
- stream:close()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement