Advertisement
MoonlightOwl

Schematic2Holo by Totoro

Jul 18th, 2014
705
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.03 KB | None | 0 0
  1. -- Schematic2Holo
  2. -- Made by Totoro (25.06.2014)
  3.  
  4. io.write("Input file name: ")
  5. local filename = io.read()
  6.  
  7. local length = 0
  8. local height = 0
  9. local width = 0
  10. local blocks = {}
  11. local data = {}
  12.  
  13. function getBlockId(x,y,z)
  14.   return blocks[y + z*width + x*length*width+1]
  15. end
  16.  
  17. function getData(x,y,z)
  18.   return data[y + z*width + x*length*width+1]
  19. end
  20.  
  21. function readbytes(file, n)
  22.   for i=1,n do
  23.     string.byte(file:read(1))
  24.   end
  25. end
  26.  
  27. function readname(file)  
  28.   n1 = string.byte(file:read(1))
  29.   n2 = string.byte(file:read(1))
  30.  
  31.   if(n1 == nil or n2 == nil) then
  32.     return ""
  33.   end
  34.  
  35.   n = n1*256 + n2
  36.  
  37.   str = ""
  38.   for i=1,n do
  39.     c = file:read(1)
  40.     if c == nil then
  41.       return
  42.     end
  43.     str = str .. c
  44.   end
  45.   --
  46.   --print("Name: "..str)
  47.   --io.read()
  48.   --
  49.   return str
  50. end
  51.  
  52. function parse(a, file, containsName)
  53.   if containsName == nil then containsName = true end
  54.   --print(a)
  55.   if a==0 then
  56.     return
  57.   end
  58.   if containsName then
  59.     name = readname(file)
  60.   end
  61.    
  62.   if a==1 then
  63.     readbytes(file,1)  
  64.   elseif a==2 then
  65.     i1 = string.byte(file:read(1))
  66.     i2 = string.byte(file:read(1))
  67.     i = i1*256 + i2
  68.     if(name=="Height") then
  69.       height = i
  70.     elseif (name=="Length") then
  71.       length = i
  72.     elseif (name=="Width") then
  73.       width = i
  74.     end
  75.   elseif a==3 then
  76.     readbytes(file,4)
  77.   elseif a==4 then
  78.     readbytes(file,8)
  79.   elseif a==5 then
  80.     readbytes(file,4)
  81.   elseif a==6 then
  82.     readbytes(file,8)
  83.   elseif a==7 then
  84.     i1 = string.byte(file:read(1))
  85.     i2 = string.byte(file:read(1))
  86.     i3 = string.byte(file:read(1))
  87.     i4 = string.byte(file:read(1))
  88.     i = i1*256*256*256 + i2*256*256 + i3*256 + i4
  89.     if name == "Blocks" then
  90.       for i=1,i do
  91.         table.insert(blocks, string.byte(file:read(1)))
  92.       end
  93.     elseif name == "Data" then
  94.       for i=1,i do
  95.         table.insert(data, string.byte(file:read(1)))
  96.       end
  97.     else
  98.       readbytes(file,i)
  99.     end
  100.   elseif a==8 then
  101.     i1 = string.byte(file:read(1))
  102.     i2 = string.byte(file:read(1))
  103.     i = i1*256 + i2
  104.     readbytes(file,i)
  105.   elseif a==9 then
  106.     t = string.byte(file:read(1))
  107.     i1 = string.byte(file:read(1))
  108.     i2 = string.byte(file:read(1))
  109.     i3 = string.byte(file:read(1))
  110.     i4 = string.byte(file:read(1))
  111.     i = i1*256*256*256 + i2*256*256 + i3*256 + i4
  112.     --print("Num: "..i)
  113.     for j=1,i do
  114.       --print("Step: "..j.." of "..i)
  115.       if t ~= 10 then
  116.         parse(t, file, false)
  117.       else
  118.         break
  119.         --parse(string.byte(file:read(1)), file)
  120.       end
  121.     end
  122.   end
  123. end
  124.  
  125. -- import
  126. file = io.open(filename, "rb")
  127. a = 0
  128. while (a ~= nil) do
  129.   a = file:read(1)
  130.   if a == nil then break end
  131.   a = string.byte(a)
  132.   parse(a, file)
  133. end
  134.  
  135. io.write("Length: " .. length)
  136. io.write("   Width: " .. width)
  137. io.write("   Height: " .. height .. "\n")
  138.  
  139. file.close()
  140. file = io.open(filename..".txt", "w")
  141.  
  142. print(#blocks)
  143. local bx, ax, by, ay, bz, az
  144. if width%2 == 1 then bx = (48-width-1)/2; ax = bx + 1
  145. else bx = (48-width)/2; ax = bx; end
  146. if length%2 == 1 then by = (48-length-1)/2; ay = by + 1
  147. else by = (48-length)/2; ay = by; end
  148. if height%2 == 1 then bz = (32-height-1)/2; az = bz + 1
  149. else bz = (32-height)/2; az = bz; end
  150.  
  151. --for i=1, bz*length*width do file:write("0") end
  152. --for i=1, #blocks do
  153. --  if blocks[i] ~= 0 then
  154. --    file:write(blocks[i]%3+1)
  155. --  else
  156. --    file:write("0")
  157. --  end
  158. --end
  159. --for i=1, az*length*width do file:write("0") end
  160. --print()
  161.  
  162. -- export
  163. for i=1, bz*48*48 do file:write("0") end
  164. for y=0,height-1 do
  165.   for i=1, bx*48 do file:write("0") end
  166.   for x=0,width-1 do
  167.     for i=1, by do file:write("0") end
  168.     for z=0,length-1 do
  169.       blockID = getBlockId(y,x,z)
  170.       blockData = getData(y,x,z)
  171.       if blockID ~= 0 then
  172.         file:write(string.char(blockData+49))
  173.       else
  174.         file:write("0")
  175.       end
  176.     end
  177.     for i=1, ay do file:write("0") end
  178.   end
  179.   for i=1, ax*48 do file:write("0") end
  180. end
  181. for i=1, az*48*48 do file:write("0") end
  182. file.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement