t8rt0t

vl32-to-nbt.lua

Jul 28th, 2024 (edited)
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.91 KB | None | 0 0
  1. local zip = require("LibDeflate")
  2. local atlaslib = require("atlas")
  3. local expect = require("cc.expect").expect
  4. local schem = require("schem")
  5.  
  6. local args = {...}
  7. local spack,sunpack,sbyte = string.pack,string.unpack,string.byte
  8. local osepoch = os.epoch
  9. local ibe = ">i4"
  10.  
  11. local input_dir,output_dir,offset = args[1],args[2],tonumber(args[3])
  12. expect(1,input_dir,"string")
  13. expect(2,output_dir,"string")
  14. expect(3,offset,"number","nil")
  15. if not offset or offset < 0 then offset = 0 end
  16. offset = offset*16
  17.  
  18. if not fs.isDir(input_dir) then error("Bad argument #1 (directory expected, got "..type(input_dir)..")") end
  19. if not fs.isDir(output_dir) then error("Bad argument #1 (directory expected, got "..type(output_dir)..")") end
  20.  
  21. local VL_CHUNK_SIZE = 4096
  22. local SCHEMATIC_SIZE = 64
  23.  
  24. local vls = fs.find(fs.combine(input_dir,"*.vl32"))
  25. if #vls == 0 then error("Could not find any .vl32 files in input directory "..input_dir) end
  26.  
  27. local atlas
  28. local atlas_paths = fs.find(fs.combine(input_dir,"*.atlas"))
  29. if #atlas_paths == 0 or not fs.exists(atlas_paths[1]) then
  30.     print("Please provide an input directory with resource files inside to create the atlas")
  31.     atlas = atlaslib.create(read())
  32. else
  33.     print("loading atlas from path",atlas_paths[1])
  34.     atlas = atlaslib.load(atlas_paths[1])
  35. end
  36. --lower nosleep threshhold
  37. local nosleeptimer = osepoch("UTC") + 5000
  38. local function nosleep()
  39.     if os.epoch("UTC") > nosleeptimer then
  40.         os.queueEvent("nosleep")
  41.         os.pullEvent()
  42.         nosleeptimer = osepoch("UTC") + 5000
  43.     end
  44. end
  45.  
  46. local iter_count = offset/16
  47. local maxX = term.getSize()
  48.  
  49. local numVoxels = 0
  50. for k,v in pairs(vls) do
  51.     numVoxels = numVoxels + fs.getSize(v)/16
  52. end
  53. local barLength = maxX - 3
  54.  
  55. local function printProgressBar()
  56.     local progress = iter_count/numVoxels
  57.     term.write(string.format("%.2d%%",progress*100))
  58.     local bar = string.rep(" ",progress*barLength)..string.rep("\x7f",(1-progress)*barLength)
  59.     term.blit(bar,string.rep("f",#bar),string.rep("9",#bar))
  60.     --term.write(string.format("%.2d%%%s%s",progress,string.rep("\x7f",progress*barLength),string.rep("_",(1-progress)*barLength)))
  61.     print()
  62. end
  63.  
  64. local startTime = osepoch("UTC")
  65.  
  66. --wrap in pcall to avoid early stream closing
  67. local function do_vl_chunk(stream)
  68.     local loaded_schems = {}
  69.     local atlas_cache = {}
  70.     print(string.format("%d/%d voxels| Elapsed time:",iter_count,numVoxels),os.date("%T",(osepoch("UTC")-startTime)/1000))
  71.     pcall(printProgressBar)
  72.     local s,err = pcall(function()
  73.             for i=1,VL_CHUNK_SIZE do
  74.                 iter_count = iter_count + 1
  75.                 nosleep()
  76.                 local x,y,z = sunpack(ibe,stream(4)),sunpack(ibe,stream(4)),sunpack(ibe,stream(4))
  77.                 local argb = stream(4)
  78.                 --local a,r,g,b = stream(),sbyte(stream()),sbyte(stream()),sbyte(stream())
  79.                 --local block = atlaslib.getClosestBlock(atlas,r,g,b)
  80.                 local block
  81.                 if atlas_cache[argb] then
  82.                     block = atlas_cache[argb]
  83.                 else
  84.                     block = atlaslib.getClosestBlock(atlas,sbyte(argb,2),sbyte(argb,3),sbyte(argb,4))
  85.                     atlas_cache[argb] = block
  86.                 end
  87.                 --skip the a part
  88.                 local schem_path = string.format("ots_y%d_x%d_z%d.nbt",y/SCHEMATIC_SIZE,x/SCHEMATIC_SIZE,z/SCHEMATIC_SIZE)
  89.                 if not loaded_schems[schem_path] then
  90.                     if fs.exists(fs.combine(output_dir,schem_path)) then
  91.                         print("loading existing schem at",schem_path)
  92.                         loaded_schems[schem_path] = schem.open(fs.combine(output_dir,schem_path)):setBlock(x%SCHEMATIC_SIZE,y%SCHEMATIC_SIZE,z%SCHEMATIC_SIZE,block)
  93.                         nosleep()
  94.                     else
  95.                         print("creating new schem for path",schem_path)
  96.                         loaded_schems[schem_path] = schem:new():setSize(SCHEMATIC_SIZE,SCHEMATIC_SIZE,SCHEMATIC_SIZE):setBlock(x%SCHEMATIC_SIZE,y%SCHEMATIC_SIZE,z%SCHEMATIC_SIZE,block)
  97.                         nosleep()
  98.                     end
  99.                 else
  100.                     loaded_schems[schem_path]:setBlock(x%SCHEMATIC_SIZE,y%SCHEMATIC_SIZE,z%SCHEMATIC_SIZE,block)
  101.                 end
  102.             end
  103.     end)
  104.     print("saving schematics")
  105.     for path,schematic in pairs(loaded_schems) do
  106.         schematic:save(fs.combine(output_dir,path))
  107.         nosleep()
  108.     end
  109.     if not s then
  110.         printError(err)
  111.         if err:find("yielding") then
  112.             nosleep()
  113.             return true --Ignore yield bullshit
  114.         end
  115.     end
  116.     return s,err
  117. end
  118.  
  119. for k,path in pairs(vls) do
  120.     print("loading vl32 from",path)
  121.     local file = fs.open(path,"r")
  122.     if offset > 0 then
  123.         offset = offset - #file.read(offset)
  124.     end
  125.     while do_vl_chunk(file.read) do
  126.         nosleep()
  127.     end
  128.     file.close()
  129. end
Add Comment
Please, Sign In to add comment