Advertisement
faubiguy

CBM-Convert

Oct 20th, 2012
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.10 KB | None | 0 0
  1. os.loadAPI("CBM")
  2.  
  3. local args = {...}
  4. if #args ~= 2 then
  5.  print("Usage: Convert-CBM <filepath-to-export> <new-filepath>")
  6.  return
  7. end
  8. local filepath = args[1]
  9. local newpath = args[2]
  10.  
  11. if not fs.exists(filepath) then
  12.  print("Error: File not found")
  13.  return
  14. end
  15.  
  16. if fs.isDir(filepath) then
  17.  print(filepath, " is a directory")
  18.  return
  19. end
  20.  
  21. if fs.exists(newpath) then
  22.  print(newpath, " already exists")
  23.  return
  24. end
  25.  
  26. print("Exporting to CBM...")
  27.  
  28. local lines = {}
  29.  
  30. local file = fs.open(filepath, "r")
  31. while true do
  32.  local line = file.readLine()
  33.  if not line then break end
  34.  table.insert(lines, line)
  35. end
  36. file.close()
  37.  
  38. pixels = {}
  39. for _=1,#lines do table.insert(pixels, {}) end
  40.  
  41. for i=1,#lines do
  42.  pixels[i] = {}
  43.  for hex in string.gmatch(lines[i], "[0-9a-f ]") do
  44.   table.insert(pixels[i], tonumber(hex, 16) or -1)
  45.  end
  46. end
  47.  
  48. local height = #pixels
  49. local width = #pixels[1]
  50.  
  51. if width == 0 or height == 0 then
  52.  print("Corrupt file")
  53.  return
  54. end
  55.  
  56. for i=2,#pixels do
  57.  if #pixels[i] ~= width then
  58.   print("Corrupt file")
  59.   return
  60.  end
  61. end
  62.  
  63. CBM.saveCBM(newpath, pixels, true)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement