Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Args
- local args = { ... }
- local program = args[1] or ""
- local repoDir = args[2] or "/lua"
- local channel = tonumber(args[3]) or 99
- -- Peripherals
- local modem = peripheral.find("modem")
- -- Setup
- local blacklist = {"lua/state"}
- -- Main
- function start()
- term.clear()
- term.setCursorPos(1, 1)
- print("\n# Network Sync Started")
- print("\n# Sync Config:")
- print("- Program: " .. program)
- print("- Respository Directory: ".. repoDir)
- print("- Channel: " .. channel)
- print("\n# Sync Controls:")
- print("- Up (Held) - Pushes local respository to server")
- print("- Down - Pulls respository from server")
- modem.open(channel)
- if(not fs.isDir(repoDir)) then
- print("\n---")
- print("\n# Running First Time Setup")
- print("- Creating", repoDir, "and performing pull")
- if(not fs.exists("/startup.lua")) then
- print("- Creating default /startup.lua")
- local file = fs.open("/startup.lua", "w")
- file.write(
- "shell.run('/lua/sync.lua \"/lua/programs/[PROGRAM_NAME] [ARGS]\"')"
- )
- file.close()
- end
- fs.makeDir(repoDir)
- pull()
- print("\n---")
- print("\n# First Time Setup Complete")
- print("- Exiting...\n")
- return
- end
- while(true) do
- parallel.waitForAny(await,
- function()
- if(program == "") then
- print("\n# Sync Paused - Program Invalid")
- sleep(999999)
- end
- print("\n# Program Starting in...")
- for i = 3, 1, -1 do
- print("#", i)
- sleep(1)
- end
- shell.run(program)
- end
- )
- print("\n# Program Exited")
- sleep(1)
- end
- end
- function await()
- while(true) do
- local event, p1, p2, p3, p4, p5 = os.pullEvent()
- local isUpKeyHeld = (event == "key" and p1 == 200 and p2)
- local isDownKey = (event == "key" and p1 == 208)
- local isModemMessage = (event == "modem_message")
- if(isUpKeyHeld) then
- push()
- break
- elseif(isDownKey) then
- pull()
- break
- elseif(isModemMessage) then
- local body = p4
- if(body.type == "syncGlobalPush") then
- print("\n# Global Push Recieved")
- updateRepo(body.files)
- break
- elseif(body.type == "syncGlobalRestart") then
- print("\n---")
- print("\n# Global Restart Recieved")
- break
- elseif(body.type == "syncGlobalHardRestart") then
- print("\n---")
- print("\n# Global Hard Restart Recieved")
- sleep(1)
- os.reboot()
- elseif(body.type == "syncNetworkPoll") then
- modem.transmit(channel, channel,
- {
- type = "syncNetworkPollRes",
- deviceData = { program = program }
- }
- )
- end
- end
- end
- end
- function pull()
- print("\n---")
- print("\n# Performing Pull")
- modem.transmit(channel, channel,
- {
- type = "syncPull"
- }
- )
- while(true) do
- local event, p1, p2, p3, p4, p5 = os.pullEvent()
- local isModemMessage = (event == "modem_message")
- if(isModemMessage) then
- local body = p4
- if(body.type == "syncPullRes") then
- print("- Pull Response Recieved")
- return updateRepo(body.files)
- end
- end
- end
- end
- function push()
- print("\n---")
- print("\n# Performing Push")
- local files = serialiseFiles(repoDir)
- modem.transmit(channel, channel,
- {
- type = "syncPush",
- files = files
- }
- )
- end
- function updateRepo(files)
- clearRepo()
- print("- Updating Local Repository")
- for i, file in pairs(files) do
- local openedFile = fs.open(file.path, "w")
- openedFile.write(file.contents)
- openedFile.close()
- end
- end
- function clearRepo()
- print("- Clearing Repository")
- local files = listAllFiles(repoDir)
- for i, file in pairs(files) do
- fs.delete(file.path)
- end
- end
- function serialiseFiles()
- local files = listAllFiles(repoDir)
- print("- Ignoring", #blacklist, "file(s)")
- print("- Serialised", #files, "file(s)")
- return files
- end
- function listAllFiles(_path, _files)
- local path = _path or ""
- local files = _files or {}
- for i, file in ipairs(fs.list(path)) do
- local path = fs.combine(path, file)
- if(fs.isDir(path)) then
- listAllFiles(path, files)
- else
- local exclude = false
- for i, name in pairs(blacklist) do
- if(string.match(path, name)) then exclude = true end
- end
- if(not exclude) then
- local openedFile = fs.open(path, "r")
- local contents = openedFile.readAll()
- openedFile.close()
- table.insert(files, {
- path = path,
- contents = contents
- })
- end
- end
- end
- return files
- end
- start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement