Advertisement
Captain_Kyros

NBT API

May 27th, 2015
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.42 KB | None | 0 0
  1. -- NBT API
  2. -- by MysticT
  3.  
  4. local function readShort(file)
  5.     local r = file.read() * 256 + file.read()
  6.     if r >= 32768 then
  7.         r = r - 65536
  8.     end
  9.     return r
  10. end
  11.  
  12. local function readInt(file)
  13.     local r = 0
  14.     for i = 1, 4 do
  15.         r = r * 256 + file.read()
  16.     end
  17.     if r >= 2147483648 then
  18.         r = r - 4294967296
  19.     end
  20.     return r
  21. end
  22.  
  23. local function readLong(file)
  24.     local r = 0
  25.     for i = 1, 8 do
  26.         r = r * 256 + file.read()
  27.     end
  28.     if r >= 9223372036854775808 then
  29.         r = r - 18446744073709551616
  30.     end
  31.     return r
  32. end
  33.  
  34. local function readFloat(file)
  35.     for i = 1, 4 do
  36.         file.read()
  37.     end
  38.     return 0
  39. end
  40.  
  41. local function readDouble(file)
  42.     for i = 1, 4 do
  43.         file.read()
  44.     end
  45.     return 0
  46. end
  47.  
  48. local function readByteArray(file)
  49.     local size = readInt(file)
  50.     local array = {}
  51.     for i = 1, size do
  52.         table.insert(array, file.read())
  53.     end
  54.     return array
  55. end
  56.  
  57. local function readString(file)
  58.     local len = readShort(file)
  59.     local s = ""
  60.     for i = 1, len do
  61.         local c = file.read()
  62.         if not c then
  63.             break
  64.         end
  65.         s = s..string.char(c)
  66.     end
  67.     return s
  68. end
  69.  
  70. local function readIntArray(file)
  71.     local size = readInt(file)
  72.     local array = {}
  73.     for i = 1, size do
  74.         table.insert(array, readInt(file))
  75.     end
  76.     return array
  77. end
  78.  
  79. local function readTags(file)
  80.     local function readPayload(id)
  81.         if id == 1 then
  82.             -- TAG_Byte
  83.             return file.read()
  84.         elseif id == 2 then
  85.             -- TAG_Short
  86.             return readShort(file)
  87.         elseif id == 3 then
  88.             -- TAG_Int
  89.             return readInt(file)
  90.         elseif id == 4 then
  91.             -- TAG_Long
  92.             return readLong(file)
  93.         elseif id == 5 then
  94.             -- TAG_Float
  95.             return readFloat(file)
  96.         elseif id == 6 then
  97.             -- TAG_Double
  98.             return readDouble(file)
  99.         elseif id == 7 then
  100.             -- TAG_Byte_Array
  101.             return readByteArray(file)
  102.         elseif id == 8 then
  103.             -- TAG_String
  104.             return readString(file)
  105.         elseif id == 9 then
  106.             -- TAG_List
  107.             local list = {}
  108.             local id = file.read()
  109.             local size = readInt(file)
  110.             for i = 1, size do
  111.                 table.insert(list, readPayload(id))
  112.             end
  113.             return list
  114.         elseif id == 10 then
  115.             -- TAG_Compound
  116.             return readTags(file)
  117.         elseif id == 11 then
  118.             -- TAG_Int_Array
  119.             return readIntArray(file)
  120.         end
  121.     end
  122.     local tags = {}
  123.     while true do
  124.         local tag = {}
  125.         tag.id = file.read()
  126.         if not tag.id then
  127.             break
  128.         end
  129.         if tag.id == 0 then
  130.             -- TAG_End
  131.             tag.name = ""
  132.             break
  133.         elseif tag.id > 11 then
  134.             return nil, "Unknown tag id: "..tostring(tag.id)
  135.         end
  136.         tag.name = readString(file)
  137.         tag.payload = readPayload(tag.id)
  138.         table.insert(tags, tag)
  139.     end
  140.     return tags
  141. end
  142.  
  143. -- API functions
  144.  
  145. function readNBTFile(sPath)
  146.     local file = fs.open(sPath, "rb")
  147.     if file then
  148.         local tags, err = readTags(file)
  149.         file.close()
  150.         return tags, err
  151.     end
  152.     return nil, "Error opening file "..sPath
  153. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement