Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- noGPS converter
- -- Version 1.0
- -- by ChaosNicro
- local arg = {...}
- -- help
- function help()
- print("usage:")
- print("converter <filename> <direction> <keep> ")
- print("")
- print("filename: Name of the file to convert.")
- print("")
- print("direction: Whether to convert the file to noGPS format(true)")
- print("or revert a converted file back to the native format.")
- print("")
- print("keep: Whether to keep the original file (true) or to overwrite it (false).")
- print("")
- print("When keep is true the output will be named filename-noGPS.")
- error() -- using empty prints instead of /n is a personal preference
- end
- -- variables
- if (arg[1] == nil) or (not fs.exists(arg[1])) then
- help()
- end
- local filename = arg[1] -- name of the original file
- local direction -- true = convert, false = revert
- if arg[2] == "false" then direction = false elseif arg[2] == "true" then direction = true else help() end
- local keep -- Whether to keep the original file
- if arg[3] == "false" then keep = false elseif arg[3] == "true" then keep = true else help() end
- local currline = "something"
- local convFilename = filename.."-converted"
- -- handles
- local orfile = assert(fs.open(filename, "r"), "could not open file")
- local newfile = assert(fs.open(convFilename, "w"), "could not create new file")
- -- logic
- if direction then
- newfile.writeLine("os.loadAPI(\"noGPS\")")
- end
- while true do
- currline = orfile.readLine()
- if (currline ~= nil) and (direction) then
- currline = string.gsub(currline, "turtle.forward", "noGPS.forward")
- currline = string.gsub(currline, "turtle.back", "noGPS.back")
- currline = string.gsub(currline, "turtle.up", "noGPS.up")
- currline = string.gsub(currline, "turtle.down", "noGPS.down")
- currline = string.gsub(currline, "turtle.turnLeft", "noGPS.turnLeft")
- currline = string.gsub(currline, "turtle.turnRight", "noGPS.turnRight")
- newfile.writeLine(currline)
- elseif (currline ~= nil) and (not direction) then
- if currline ~= "os.loadAPI(\"noGPS\")" then
- currline = string.gsub(currline, "noGPS.forward", "turtle.forward")
- currline = string.gsub(currline, "noGPS.back", "turtle.back")
- currline = string.gsub(currline, "noGPS.up", "turtle.up")
- currline = string.gsub(currline, "noGPS.down", "turtle.down")
- currline = string.gsub(currline, "noGPS.turnLeft", "turtle.turnLeft")
- currline = string.gsub(currline, "noGPS.turnRight", "turtle.turnRight")
- newfile.writeLine(currline)
- end
- elseif currline == nil then
- break
- end
- end
- orfile.close()
- newfile.close()
- if not keep then
- fs.delete(filename)
- fs.move(convFilename, filename)
- end
- print("finished!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement