Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- WARNING: Use this script at your own risk.
- -- Ensure you have a backup before running it.
- -- Doggy OS Smart Switch - User Data Transfer Utility
- -- Function to clear the screen and set up UI
- local function setupUI(title, subtitle)
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.clear()
- -- Center the title
- local w, h = term.getSize()
- local x = math.floor((w - #title) / 2)
- term.setCursorPos(x, 2)
- term.setTextColor(colors.cyan)
- print(title)
- -- Subtitle below the title
- local x_subtitle = math.floor((w - #subtitle) / 2)
- term.setCursorPos(x_subtitle, 4)
- term.setTextColor(colors.lightGray)
- print(subtitle)
- -- Draw horizontal line for separation
- term.setCursorPos(1, 6)
- term.setTextColor(colors.gray)
- print(string.rep("-", w))
- end
- -- Function to recursively collect user data
- local function collectUserData(path, userData, basePath)
- for _, file in pairs(fs.list(path)) do
- local fullPath = fs.combine(path, file)
- local relativePath = fullPath:sub(#basePath + 1)
- if fs.isDir(fullPath) then
- userData[relativePath] = "DIR"
- collectUserData(fullPath, userData, basePath)
- else
- local f = fs.open(fullPath, "r")
- if f then
- userData[relativePath] = f.readAll()
- f.close()
- end
- end
- end
- end
- -- Function to restore user data from received table
- local function restoreUserData(userData)
- fs.delete("/disk/users/")
- fs.makeDir("/disk/users/")
- for path, content in pairs(userData) do
- local fullPath = fs.combine("/disk/users/", path)
- if content == "DIR" then
- fs.makeDir(fullPath)
- else
- local file = fs.open(fullPath, "w")
- if file then
- file.write(content)
- file.close()
- end
- end
- end
- end
- -- Function to receive user data via rednet
- local function receiveUserData()
- setupUI("Doggy OS Smart Switch", "Waiting for user data from sender...")
- rednet.open("top")
- while true do
- local senderID, message = rednet.receive("DoggyOS_SmartSwitch")
- if message == "CONFIRM" then
- rednet.send(senderID, "CONFIRMED", "DoggyOS_SmartSwitch")
- elseif type(message) == "table" then
- restoreUserData(message)
- setupUI("Doggy OS Smart Switch", "User data received and restored!")
- print("Transfer complete.")
- os.sleep(2)
- break
- end
- end
- rednet.close("top")
- end
- -- Function to send user data via rednet
- local function sendUserData()
- setupUI("Doggy OS Smart Switch", "Enter receiver's ID:")
- term.setCursorPos(3, 8)
- local receiverID = tonumber(read())
- if not receiverID then
- setupUI("Doggy OS Smart Switch", "Invalid receiver ID.")
- os.sleep(2)
- return
- end
- setupUI("Doggy OS Smart Switch", "Waiting for receiver confirmation...")
- rednet.open("top")
- rednet.send(receiverID, "CONFIRM", "DoggyOS_SmartSwitch")
- local senderID, response = rednet.receive("DoggyOS_SmartSwitch", 10)
- if response == "CONFIRMED" then
- setupUI("Doggy OS Smart Switch", "Sending user data...")
- local userData = {}
- collectUserData("/disk/users/", userData, "/disk/users/")
- rednet.send(receiverID, userData, "DoggyOS_SmartSwitch")
- setupUI("Doggy OS Smart Switch", "User data sent successfully!")
- os.sleep(2)
- else
- setupUI("Doggy OS Smart Switch", "Receiver did not confirm.")
- os.sleep(2)
- end
- rednet.close("top")
- end
- -- Function to start the data transfer process
- local function startSmartSwitch()
- setupUI("Doggy OS Smart Switch", "Transfer your data to a new Doggy OS device")
- term.setCursorPos(3, 8)
- print("1. Send user data to another Doggy OS device")
- term.setCursorPos(3, 9)
- print("2. Receive user data from another Doggy OS device")
- term.setCursorPos(3, 11)
- write("Choose an option: ")
- local choice = read()
- if choice == "2" then
- receiveUserData()
- elseif choice == "1" then
- sendUserData()
- else
- setupUI("Doggy OS Smart Switch", "Invalid selection. Restarting...")
- os.sleep(2)
- startSmartSwitch()
- end
- end
- -- Main program
- startSmartSwitch()
- setupUI("Running Doggy OS", "Please wait...")
- shell.run("/disk/boot/error")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement