Advertisement
nonogamer9

OC-8 Download Center : Easy Way To Download CHIP-8 ROMS Into OpenComputers

Jul 21st, 2024
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.80 KB | Software | 0 0
  1. local component = require("component")
  2. local internet = component.internet
  3. local filesystem = require("filesystem")
  4. local shell = require("shell")
  5.  
  6. local roms = {
  7.     {name = "Pong", url = "https://github.com/brianhang/chip8-lua/blob/master/ROM/PONG"},
  8.     {name = "Tetris", url = "https://github.com/brianhang/chip8-lua/blob/master/ROM/TETRIS"},
  9.     {name = "Space Invaders", url = "https://github.com/brianhang/chip8-lua/blob/master/ROM/INVADERS"}
  10. }
  11.  
  12. local function downloadRom(url, savePath)
  13.     local handle, reason = internet.request(url)
  14.     if not handle then
  15.         return false, reason
  16.     end
  17.  
  18.     local file, reason = io.open(savePath, "wb")
  19.     if not file then
  20.         return false, reason
  21.     end
  22.  
  23.     for chunk in handle do
  24.         file:write(chunk)
  25.     end
  26.  
  27.     file:close()
  28.     return true
  29. end
  30.  
  31. local function showMenu()
  32.     print("Select a ROM to download:")
  33.     for i, rom in ipairs(roms) do
  34.         print(string.format("%d. %s", i, rom.name))
  35.     end
  36.     print("Enter the number of the ROM you want to download:")
  37.  
  38.     local choice = tonumber(io.read())
  39.     if choice and choice >= 1 and choice <= #roms then
  40.         return roms[choice]
  41.     else
  42.         return nil, "Invalid choice"
  43.     end
  44. end
  45.  
  46. local function main()
  47.     if not filesystem.exists("/home/ROMS") then
  48.         filesystem.makeDirectory("/home/ROMS")
  49.     end
  50.  
  51.     local rom, err = showMenu()
  52.     if not rom then
  53.         print("Error: " .. err)
  54.         return
  55.     end
  56.  
  57.     local savePath = "/home/ROMS/" .. rom.name .. ".ch8"
  58.     print("Downloading " .. rom.name .. "...")
  59.     local success, reason = downloadRom(rom.url, savePath)
  60.     if success then
  61.         print("Downloaded successfully and saved to " .. savePath)
  62.     else
  63.         print("Failed to download: " .. reason)
  64.     end
  65. end
  66.  
  67. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement