Advertisement
samuelask

Kino Uploader

Mar 8th, 2025 (edited)
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.02 KB | None | 0 0
  1. -- Kino Script Uploader
  2. local component = require("component")
  3. local event = require("event")
  4. local modem = component.modem
  5. local filesystem = require("filesystem")
  6. local serialization = require("serialization")
  7. local shell = require("shell")
  8.  
  9. local TIMEOUT = 5  -- Time to wait for wake-up confirmation
  10. local ADDRESS_FILE = "/home/modem_addresses.json"  -- Where Kino's address will be stored
  11. local TABLET_PROGRAM = "/home/tablet.lua"  -- Path to the tablet program
  12. local KINO_PROGRAM = "/home/kino.lua"  -- Path to the kino program
  13. tablet_address = modem.address
  14. local tablet_code = "tablet.lua"
  15. local tablet_pastebin_id = "vK0V08GF"
  16. local kino_code = "kino.lua"
  17. local kino_pastebin_id = "5W6e2i10"
  18.  
  19. modem.open(123)  -- Open channel
  20.  
  21. -- **Function to save Kino & Tablet addresses**
  22. local function save_addresses(kino_address, base_adress)
  23.     local tablet_address = modem.address
  24.     local data = {
  25.         kino_address = kino_address,
  26.         tablet_address = tablet_address,
  27.         base_adress = base_adress
  28.     }
  29.  
  30.     local file = io.open(ADDRESS_FILE, "w")
  31.     if file then
  32.         file:write(serialization.serialize(data))
  33.         file:close()
  34.         print("[💾] Addresses saved! Kino: " .. kino_address .. " | Tablet: " .. tablet_address)
  35.     else
  36.         print("[❌] Error: Could not save addresses!")
  37.     end
  38. end
  39.  
  40. -- **Function to download a missing file from Pastebin**
  41. local function download_file(file_name, pastebin_id)
  42.     print("[⚠] " .. file_name .. " not found. Downloading...")
  43.     local result, reason = shell.execute("pastebin get -f " .. pastebin_id .. " " .. file_name)
  44.    
  45.     if result then
  46.         print("[✔] Download successful! File saved as " .. file_name)
  47.         return true
  48.     else
  49.         print("[❌] Download failed: " .. (reason or "Unknown error"))
  50.         return false
  51.     end
  52. end
  53.  
  54. -- **Ensure `kino.lua` exists before proceeding**
  55. if not filesystem.exists(KINO_PROGRAM) then
  56.     if not download_file(kino_code, kino_pastebin_id) then return end
  57. end
  58.  
  59. -- **Load Kino script contents**
  60. local file = io.open(kino_code, "r")
  61. local kino_script = file:read("*all")
  62. file:close()
  63.  
  64.  
  65. print("[🔄] Sending wake-up signal to Kino...")
  66. modem.broadcast(123, "kino_wake")
  67.  
  68. -- **Wait for Kino to acknowledge the wake-up signal**
  69. local function wait_for_confirmation(timeout)
  70.     local start_time = os.time()
  71.  
  72.     while os.time() - start_time < timeout do
  73.         local _, _, from, port, _, message = event.pull(2, "modem_message")
  74.        
  75.         if port == 123 and message == "wake_ack" then
  76.             print("[✔] Kino is awake! Waiting for base computer...")
  77.             kino = from
  78.             modem.broadcast(123, "base_sync")
  79.             local start_time = os.time()
  80.            
  81.             while os.time() - start_time < timeout do
  82.                 local _, _, from, port, _, message = event.pull(2, "modem_message")
  83.                
  84.                 if port == 123 and message == "base_ack" then
  85.                     base_adress = from
  86.                     save_addresses(kino, base_adress)
  87.                     return kino, base_adress
  88.                 end
  89.             end
  90.         end
  91.     end
  92.  
  93.     return false, false
  94. end
  95.  
  96. -- **Wake up Kino and store address**
  97. local kino_address, base_adress = wait_for_confirmation(TIMEOUT)
  98. if not kino_address then
  99.     print("[❌] ERROR: No response from Kino! Check if it's powered on.")
  100.     return
  101. end
  102. if not base_adress then
  103.     print("[❌] ERROR: No response from Base! Check if it's powered on.")
  104.     return
  105. end
  106. -- **Start Upload**
  107. modem.send(kino_address, 123, "upload")
  108.  
  109. -- **Send the Kino script in chunks**
  110. for i = 1, #kino_script, 256 do
  111.     modem.send(kino_address, 123, kino_script:sub(i, i + 255))
  112.     os.sleep(0.1)
  113. end
  114. modem.send(kino_address, 123, "end")  -- End transmission
  115. print("[📤] Kino script uploaded. Awaiting confirmation...")
  116.  
  117. -- **Listen for Kino update confirmation**
  118. local function receive(_, _, sender, _, _, message)
  119.     if sender == kino_address then
  120.         if message == "kino_update_success" then
  121.             print("[✔] Kino successfully updated and running!")
  122.             print("[🚀] Starting tablet program...")
  123.             os.sleep(1)
  124.  
  125.             -- **Ensure `tablet.lua` exists before executing**
  126.             if not filesystem.exists(TABLET_PROGRAM) then
  127.                 print("[⚠] Tablet program missing. Downloading...")
  128.                 if not download_file(tablet_code, tablet_pastebin_id) then
  129.                     print("[❌] Could not start tablet program due to download failure.")
  130.                     return
  131.                 end
  132.             end
  133.  
  134.             -- **Start the tablet program**
  135.             shell.execute(TABLET_PROGRAM)
  136.             return true
  137.  
  138.         elseif message == "kino_update_error" then
  139.             print("[❌] Error: Kino failed to execute script!")
  140.             return true
  141.         end
  142.     end
  143. end
  144.  
  145. local timeout = 10
  146. local start_time = os.time()
  147. while os.time() - start_time < timeout do
  148.     local _, _, sender, _, _, message = event.pull(2, "modem_message")
  149.     if message then
  150.         if receive(nil, nil, sender, nil, nil, message) then
  151.             break
  152.         end
  153.     end
  154. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement