Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local argsSource = { ... }
- function splitText(source, letters)
- local parts = {}
- local akt = "";
- local qM1 = 0; local qM2 = 0; local b1 = 0; local b2 = 0; local b3 = 0;
- for i = 1, #source do
- local z = string.sub(source, i, i)
- if z == "'" and qM2 == 0 then qM1 = 1 - qM1;
- elseif z == "\"" and qM1 == 0 then qM2 = 1 - qM2;
- elseif qM1 == 0 and qM2 == 0 then
- if z == "(" then b1 = b1 + 1
- elseif z == ")" then b1 = b1 - 1
- elseif z == "[" then b2 = b2 + 1
- elseif z == "]" then b2 = b2 - 1
- elseif z == "{" then b3 = b3 + 1
- elseif z == "}" then b3 = b3 - 1
- end
- end
- local split = false
- if b1 == 0 and b2 == 0 and b3 == 0 and qM1 == 0 and qM2 == 0 then
- for j = 1, #letters do
- if z == string.sub(letters, j, j) then
- split = true
- break
- end
- end
- end
- if split then
- if akt ~= "" then
- table.insert(parts, akt)
- akt = ""
- end
- else akt = akt .. z
- end
- end
- if akt ~= "" then table.insert(parts, akt) end
- return parts
- end
- local args = {}
- local TYPE_DECLARATION = 0
- local TYPE_TABLE = 1
- local TYPE_TABLE_MAP = 1
- local TYPE_TABLE_ARR = 2
- local TYPE_VALUE = 2
- local TYPE_CONSTANT = 3
- local TYPE_PASTEBIN = 4
- if peripheral == nil then
- _G.os.reboot = function() print("fct: os.reboot()") end
- _G.os.setComputerLabel = function(label) print("fct: os.setComputerLabel(\""..label.."\")") end
- end
- function getTypeName(t)
- if t == TYPE_DECLARATION then return "declaration"
- elseif t == TYPE_TABLE then return "table"
- elseif t == TYPE_VALUE then return "value"
- elseif t == TYPE_CONSTANT then return "constant"
- elseif t == TYPE_PASTEBIN then return "pastebin"
- else error("unknown type: " .. t)
- end
- end
- function loadArgData(arg)
- local v = loadVar(arg)
- if v.type ~= TYPE_DECLARATION then
- error("every argument of this program needs to be of type 'declaration'")
- end
- return v
- end
- function readFileText(file)
- local f = fs.open(file, "r")
- local text = f.readAll()
- f.close()
- return text
- end
- function tryMatches(var, format, name)
- if not string.match(var, format) == var then error("invalid " .. name .. ": '" .. var .. "'") end
- end
- function loadVar(source)
- local pts = splitText(source, "=")
- if #pts > 1 then
- if #pts > 2 then error("only one '=' in declaration") end
- tryMatches(pts[1], "[A-Za-z_][A-Za-z0-9_]*", "dec-name")
- return { type = TYPE_DECLARATION, name = pts[1], value = loadVar(pts[2]) }
- end
- if string.sub(source, 1, 1) == "{" and string.sub(source, #source) == "}" then
- local pts = splitText(string.sub(source, 2, #source - 1), ",")
- local values = {}
- for i, pt in pairs(pts) do
- local v = loadVar(pt)
- if v.type == TYPE_DECLARATION then
- values[v.name] = v.value
- else table.insert(values, v)
- end
- end
- return { type = TYPE_TABLE, values = values }
- elseif string.sub(source, 1, 1) == "<" and string.sub(source, #source) == ">" then
- source = string.sub(source, 2, #source - 1)
- local ind = string.find(source, ":")
- local action = string.sub(source, 1, ind - 1)
- tryMatches(action, "[A-Za-z_][A-Za-z0-9_]*", "action-name")
- if action == "pb" then
- local id = string.sub(source, ind + 1)
- tryMatches(id, "[A-Za-z_][A-Za-z0-9_]*", "pastebinId-name")
- return { type = TYPE_PASTEBIN, id = id }
- elseif action == "input" then
- local v = loadVar(string.sub(source, ind + 1))
- if v.type ~= TYPE_VALUE or type(v.value) ~= "string" then error("string value expected for function <input:''>") end
- io.write("input<" .. v.value .. ">: ")
- local data = io.read()
- return { type = TYPE_VALUE, value = data }
- elseif action == "inputNum" then
- local v = loadVar(string.sub(source, ind + 1))
- if v.type ~= TYPE_VALUE or type(v.value) ~= "string" then error("string value expected for function <input:''>") end
- io.write("inputNum<" .. v.value .. ">: ")
- local data = tonumber(io.read())
- return { type = TYPE_VALUE, value = data }
- else error("unknown action: '" .. action .. "'")
- end
- elseif string.sub(source, 1, 1) == "'" and string.sub(source, #source) == "'" then
- return { type = TYPE_VALUE, value = string.sub(source, 2, #source - 1) }
- elseif string.sub(source, 1, 1) == "$" and string.sub(source, #source) == "$" then
- local name = string.sub(source, 2, #source - 1)
- return { type = TYPE_CONSTANT, name = name }
- elseif source == "true" or source == "false" then
- return { type = TYPE_VALUE, value = source == "true" }
- elseif tonumber(source) ~= nil then
- return { type = TYPE_VALUE, value = tonumber(source) }
- else error("invalid source: '" .. source .. "'")
- end
- end
- function checkType(name, value, t, t2)
- if value.type ~= t then
- error("value '" .. name .. "' needs to be of type '" .. getTypeName(t) .. "' (type '" .. getTypeName(value.type) .. "' given)")
- end
- if t2 ~= nil then
- if value.type == TYPE_TABLE then
- if t2 == TYPE_TABLE_MAP then
- for i, _ in pairs(value.values) do
- if type(i) == "number" then
- error("table '" .. name .. "' needs to be a map")
- end
- end
- elseif t2 == TYPE_TABLE_ARR then
- for i, _ in pairs(value.values) do
- if type(i) == "string" then
- error("table '" .. name .. "' needs to be an array")
- end
- end
- else error("unknown type2: '" .. t2 .. "'")
- end
- elseif value.type == TYPE_VALUE then
- if type(value.value) ~= t2 then error("value '" .. name .. "' needs to be of type '" .. t2 .. "' (type '" .. type(value.value .. "' given)")) end
- end
- end
- end
- function writeToFile(fileName, data)
- local f = fs.open(fileName, "w")
- f.write(data)
- f.close()
- end
- local startupBuildData = {
- files = {},
- cmds = {}
- }
- local reboot = false
- local functions = {
- startup = {
- delay = function(data)
- checkType("startup>delay", data, TYPE_VALUE, "number")
- startupBuildData.delay = data.value
- end,
- files = function(data)
- checkType("startup>files", data, TYPE_TABLE, TYPE_TABLE_MAP)
- for fileName, v in pairs(data.values) do
- checkType("startup>files>value", v, TYPE_PASTEBIN)
- table.insert(startupBuildData.files, { name = fileName, id = v.id })
- end
- end,
- cmds = function(data)
- checkType("startup>cmds", data, TYPE_TABLE, TYPE_TABLE_ARR)
- for i, cmd in pairs(data.values) do
- checkType("startup>cmds>cmd", cmd, TYPE_TABLE, TYPE_TABLE_ARR)
- table.insert(startupBuildData.cmds, cmd.values)
- end
- end
- },
- reboot = function(data)
- checkType("reboot", data, TYPE_VALUE, "boolean")
- reboot = data.value
- end,
- label = function(data)
- checkType("label", data, TYPE_VALUE, "string")
- os.setComputerLabel(data.value)
- end
- }
- for i, v in pairs(argsSource) do
- table.insert(args, loadArgData(v))
- end
- function printTable(tbl)
- printTableIndex(tbl, "", {})
- end
- function printTableIndex(tbl, before, alreadyUsed)
- for i, v in pairs(tbl) do
- if type(v) == "table" then
- if tableContainsValue(alreadyUsed, v) then
- print(before .. i .. ":", "alreadyUsed");
- else
- print(before .. i .. ": [");
- table.insert(alreadyUsed, v)
- printTableIndex(v, before .. "\t", alreadyUsed)
- print(before .. "]")
- end
- else
- print(before .. i .. ":", v);
- end
- end
- end
- function tableContainsValue(table, value)
- for i, v in pairs(table) do
- if v == value then return true end
- end
- return false
- end
- function executeFunction(tName, name, data, functions)
- for n, fct in pairs(functions) do
- if name == n then
- if type(fct) == "table" then
- tName = tName .. "[" .. name .. "]"
- checkType(tName .. ".value", data, TYPE_TABLE, TYPE_TABLE_MAP)
- for n1, v in pairs(data.values) do
- executeFunction(tName, n1, v, fct)
- end
- else
- fct(data)
- end
- return
- end
- end
- error("function-name '" .. name .. "' isn't allowed for '" .. tName .. "'")
- end
- function writeToFile(fileName, data)
- if fs == nil then
- local f = io.open(fileName, "w")
- io.output(f)
- io.write(data)
- io.close(f)
- else
- local f = fs.open(fileName, "w")
- f.write(data)
- f.close()
- end
- end
- for i, v in pairs(args) do
- local tName = "level0-argument"
- executeFunction(tName, v.name, v.value, functions)
- --[[local fct = functions
- while type(fct) == "table" do
- for i,v1 in pairs(v.values) do
- checkType(tName, v, TYPE_DECLARATION)
- local name = v.name
- local value = v.value
- local fct = functions[v.name]
- if fct == nil then error(tName .. " '" .. v.name .. "' isn't defined") end
- end
- end]]
- end
- function replaceConstants(v, constants)
- if v.type == TYPE_CONSTANT then
- if constants[v.name] ~= nil then
- return { type = TYPE_VALUE, value = constants[v.name] }
- end
- elseif v.type == TYPE_TABLE then
- for i, v1 in pairs(v.values) do
- v.values[i] = replaceConstants(v1, constants)
- end
- elseif v.type ~= TYPE_VALUE then
- error("unsupported type: '" .. getTypeName(v.type) .. "'")
- end
- return v
- end
- local txt = "local args={...}\n"
- if startupBuildData.delay ~= nil then
- txt = txt .. "sleep(" .. startupBuildData.delay .. ")\n"
- end
- for i, f in pairs(startupBuildData.files) do
- txt = txt .. "if fs.exists(\""..f.name.."\") then shell.run(\"delete\",\"" .. f.name .. "\") end\n"
- txt = txt .. "shell.run(\"pastebin\",\"get\",\"" .. f.id .. "\",\"" .. f.name .. "\")\n"
- end
- for i, cmd in pairs(startupBuildData.cmds) do
- cmd = replaceConstants({ type = TYPE_TABLE, values = cmd }, { arg1 = "raw:args[1]", arg2 = "raw:args[2]" }).values
- for i, v in pairs(cmd) do
- checkType("cmds>cmd", v, TYPE_VALUE, "string")
- end
- txt = txt .. "shell.run("
- for i, v in pairs(cmd) do
- if i ~= 1 then txt = txt .. "," end
- if string.sub(v.value, 1, 4) == "raw:" then
- txt = txt .. string.sub(v.value, 5)
- else txt = txt .. "\"" .. v.value .. "\""
- end
- end
- txt = txt .. ")\n"
- end
- writeToFile("startup", txt)
- if reboot then
- os.reboot()
- end
- --[[
- local args = {}
- for i, v in pairs(argsSource) do
- if string.find(v, "=") ~= nil then
- local ind = string.find(v, "=")
- args[string.sub(v, 1, ind - 1)] = string.sub(v, ind + 1)
- else args[i] = v
- end
- end
- local reboot = false
- local actions = {}
- for key, arg in pairs(args) do
- if type(key) == "number" then
- if arg == "reboot" then
- reboot = true
- else error("unknown argument: " .. arg)
- end
- else
- if key == "startup" then
- table.insert(actions, function()
- local f = fs.open("startup", "w")
- f.write("shell.run(\"delete\",\"unitHandler\")\n")
- f.write("shell.run(\"pastebin\",\"get\",\"" .. arg .. "\",\"unitHandler\")\n")
- f.write("shell.run(\"unitHandler\")\n")
- f.close()
- print("successfully set startup to '" .. arg .. "'")
- end)
- elseif key == "label" then
- table.insert(actions, function()
- os.setComputerLabel(arg)
- print("successfully set label to '" .. arg .. "'")
- end)
- else error("unknown argument-key: " .. key)
- end
- end
- end
- for i = 1, #actions do
- actions[i]()
- end
- if reboot then os.reboot() end
- ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement