t8rt0t

ots_merge.lua

Jul 25th, 2024 (edited)
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.39 KB | None | 0 0
  1. local nbt = require"nbt"
  2. local zip = require"LibDeflate"
  3.  
  4. local args = {...}
  5.  
  6. shell.setCompletionFunction(shell.getRunningProgram(),shell.getCompletionInfo()["rom/programs/list.lua"].fnComplete)
  7.  
  8. local folder,output_path = args[1],args[2]
  9.  
  10. local function printUsage()
  11.     print("items inside the input directory should follow the format 'ots_x#_y#_z#.nbt'")
  12.     printError("ots_merge <input_dir> [output_path]")
  13. end
  14.  
  15. if not fs.isDir(folder) or (type(output_path) ~= "string" and type(output_path) ~= "nil") then
  16.     printUsage()
  17.     return
  18. end
  19.  
  20. local files = fs.find(fs.combine(folder,"ots_x*_y*_z*.nbt"))
  21.  
  22. if #files == 0 then
  23.     print("No valid files found")
  24.     printUsage()
  25.     return
  26. end
  27.  
  28. local main_schem = nbt.COMPOUND()
  29. main_schem.root = nbt.COMPOUND()
  30. main_schem.root.blocks = nbt.LIST(10)
  31. main_schem.root.size = nbt.LIST(3)
  32. for i=1,3 do
  33.     main_schem.root.size[i] = 0
  34. end
  35. main_schem.root.palette = nbt.LIST(10)
  36. main_schem.root.DataVersion = 2975 --idk why this needs to be here
  37.  
  38. --true if palette blocks are the same
  39. local function isPaletteEqual(pb1,pb2)
  40.     --print("check palette equal")
  41.     if tostring(pb1.Name) ~= tostring(pb2.Name) then return false end
  42.     if pb1.Properties then
  43.         if not pb2.Properties then return false end
  44.         for k,v in pairs(pb1.Properties) do
  45.             if tostring(v) ~= tostring(pb2.Properties[k]) then return false end
  46.         end
  47.         for k,v in pairs(pb2.Properties) do
  48.             if tostring(v) ~= tostring(pb1.Properties[k]) then return false end
  49.         end
  50.     else
  51.         return pb1.Properties == pb2.Properties
  52.     end
  53. end
  54.  
  55. local function mergePalettes(p1,p2)
  56.     --print("mergine palettes")
  57.     local remap = {}
  58.     for oldIdx,pb2 in pairs(p2) do
  59.         oldIdx = oldIdx - 1 --I hate 1s based indexing
  60.         for idx,pb1 in pairs(p1) do
  61.             if isPaletteEqual(pb1,pb2) then
  62.                 remap[oldIdx] = idx - 1
  63.                 break
  64.             end
  65.         end
  66.         if not remap[oldIdx] then
  67.             remap[oldIdx] = #p1 --do this first because it is intentionally 1 smaller than the lua index (nbt uses 0 based indexing)
  68.             p1[#p1+1] = pb2
  69.         end
  70.     end
  71.     return remap
  72. end
  73.  
  74. --Merge the other schematic into the main one
  75. local function mergeSchematic(main_schem_root,other_schem,other_schem_pos,main_schem_pos)
  76.     --print("merge schematic")
  77.     _,other_schem = next(rawget(other_schem,"data")) --schematics are wrapped in a secondary compound tag for no good reason
  78.     local remap = mergePalettes(main_schem_root.palette,other_schem.palette)
  79.     --copy over the blocks with the remap
  80.     for _,block in pairs(other_schem.blocks) do
  81.         --print("merging blocks")
  82.         --write("blockstate: "..tostring(block.state))
  83.         block.state = remap[nbt.tonumber(block.state)]
  84.         --print(" remap: "..tostring(block.state))
  85.         for i=1,3 do
  86.             block.pos[i] = other_schem_pos[i]+block.pos[i]-main_schem_pos[i]
  87.             --main_schem.root.size[i] = math.max(main_schem.root.size[i],block.pos[i]+1) --update schem size
  88.         end
  89.         main_schem_root.blocks[#main_schem_root.blocks+1] = block
  90.     end
  91.     --update size
  92.     --print("updating size")
  93.     for i=1,3 do
  94.         --print(main_schem_root.size)
  95.         --print(textutils.serialise(main_schem_pos):gsub("%s",""))
  96.         main_schem_root.size[i] = math.max(nbt.tonumber(main_schem_root.size[i]),other_schem_pos[i]+other_schem.size[i]-main_schem_pos[i])
  97.         main_schem_pos[i] = math.min(main_schem_pos[i],other_schem_pos[i])
  98.     end
  99. end
  100.  
  101. local main_pos
  102.  
  103. for _,path in pairs(files) do
  104.     local x,y,z = assert(path:match("ots_x(%d+)_y(%d+)_z(%d+)%.nbt"))
  105.     local pos = {tonumber(x),tonumber(y),tonumber(z)}
  106.     if not main_pos then main_pos = pos end
  107.     local file = fs.open(path,"r")
  108.     local compressed_data = file.readAll()
  109.     file.close()
  110.     local binary_data = assert(zip:DecompressGzip(compressed_data))
  111.     local schem = assert(nbt.frombinary(binary_data))
  112.     mergeSchematic(main_schem.root,schem,pos,main_pos)
  113.     os.queueEvent("nosleep")
  114.     os.pullEvent()
  115. end
  116.  
  117. output_path = output_path or string.format("merged/ots_x%d_y%d_z%d.nbt",unpack(main_pos))
  118.  
  119. local binary_data = assert(nbt.tobinary(main_schem))
  120. local zipped_data = assert(zip:CompressGzip(binary_data))
  121. print("Outputting to",output_path)
  122. local file = fs.open(output_path,"w+")
  123. file.write(zipped_data)
  124. file.close()
Advertisement
Add Comment
Please, Sign In to add comment