Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Kino Script Uploader
- local component = require("component")
- local event = require("event")
- local modem = component.modem
- local filesystem = require("filesystem")
- local serialization = require("serialization")
- local shell = require("shell")
- local TIMEOUT = 5 -- Time to wait for wake-up confirmation
- local ADDRESS_FILE = "/home/modem_addresses.json" -- Where Kino's address will be stored
- local TABLET_PROGRAM = "/home/tablet.lua" -- Path to the tablet program
- local KINO_PROGRAM = "/home/kino.lua" -- Path to the kino program
- tablet_address = modem.address
- local tablet_code = "tablet.lua"
- local tablet_pastebin_id = "vK0V08GF"
- local kino_code = "kino.lua"
- local kino_pastebin_id = "5W6e2i10"
- modem.open(123) -- Open channel
- -- **Function to save Kino & Tablet addresses**
- local function save_addresses(kino_address, base_adress)
- local tablet_address = modem.address
- local data = {
- kino_address = kino_address,
- tablet_address = tablet_address,
- base_adress = base_adress
- }
- local file = io.open(ADDRESS_FILE, "w")
- if file then
- file:write(serialization.serialize(data))
- file:close()
- print("[💾] Addresses saved! Kino: " .. kino_address .. " | Tablet: " .. tablet_address)
- else
- print("[❌] Error: Could not save addresses!")
- end
- end
- -- **Function to download a missing file from Pastebin**
- local function download_file(file_name, pastebin_id)
- print("[⚠] " .. file_name .. " not found. Downloading...")
- local result, reason = shell.execute("pastebin get -f " .. pastebin_id .. " " .. file_name)
- if result then
- print("[✔] Download successful! File saved as " .. file_name)
- return true
- else
- print("[❌] Download failed: " .. (reason or "Unknown error"))
- return false
- end
- end
- -- **Ensure `kino.lua` exists before proceeding**
- if not filesystem.exists(KINO_PROGRAM) then
- if not download_file(kino_code, kino_pastebin_id) then return end
- end
- -- **Load Kino script contents**
- local file = io.open(kino_code, "r")
- local kino_script = file:read("*all")
- file:close()
- print("[🔄] Sending wake-up signal to Kino...")
- modem.broadcast(123, "kino_wake")
- -- **Wait for Kino to acknowledge the wake-up signal**
- local function wait_for_confirmation(timeout)
- local start_time = os.time()
- while os.time() - start_time < timeout do
- local _, _, from, port, _, message = event.pull(2, "modem_message")
- if port == 123 and message == "wake_ack" then
- print("[✔] Kino is awake! Waiting for base computer...")
- kino = from
- modem.broadcast(123, "base_sync")
- local start_time = os.time()
- while os.time() - start_time < timeout do
- local _, _, from, port, _, message = event.pull(2, "modem_message")
- if port == 123 and message == "base_ack" then
- base_adress = from
- save_addresses(kino, base_adress)
- return kino, base_adress
- end
- end
- end
- end
- return false, false
- end
- -- **Wake up Kino and store address**
- local kino_address, base_adress = wait_for_confirmation(TIMEOUT)
- if not kino_address then
- print("[❌] ERROR: No response from Kino! Check if it's powered on.")
- return
- end
- if not base_adress then
- print("[❌] ERROR: No response from Base! Check if it's powered on.")
- return
- end
- -- **Start Upload**
- modem.send(kino_address, 123, "upload")
- -- **Send the Kino script in chunks**
- for i = 1, #kino_script, 256 do
- modem.send(kino_address, 123, kino_script:sub(i, i + 255))
- os.sleep(0.1)
- end
- modem.send(kino_address, 123, "end") -- End transmission
- print("[📤] Kino script uploaded. Awaiting confirmation...")
- -- **Listen for Kino update confirmation**
- local function receive(_, _, sender, _, _, message)
- if sender == kino_address then
- if message == "kino_update_success" then
- print("[✔] Kino successfully updated and running!")
- print("[🚀] Starting tablet program...")
- os.sleep(1)
- -- **Ensure `tablet.lua` exists before executing**
- if not filesystem.exists(TABLET_PROGRAM) then
- print("[⚠] Tablet program missing. Downloading...")
- if not download_file(tablet_code, tablet_pastebin_id) then
- print("[❌] Could not start tablet program due to download failure.")
- return
- end
- end
- -- **Start the tablet program**
- shell.execute(TABLET_PROGRAM)
- return true
- elseif message == "kino_update_error" then
- print("[❌] Error: Kino failed to execute script!")
- return true
- end
- end
- end
- local timeout = 10
- local start_time = os.time()
- while os.time() - start_time < timeout do
- local _, _, sender, _, _, message = event.pull(2, "modem_message")
- if message then
- if receive(nil, nil, sender, nil, nil, message) then
- break
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement