Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local nbt = require"nbt"
- local zip = require"LibDeflate"
- local args = {...}
- shell.setCompletionFunction(shell.getRunningProgram(),shell.getCompletionInfo()["rom/programs/list.lua"].fnComplete)
- local folder,output_path = args[1],args[2]
- local function printUsage()
- print("items inside the input directory should follow the format 'ots_x#_y#_z#.nbt'")
- printError("ots_merge <input_dir> [output_path]")
- end
- if not fs.isDir(folder) or (type(output_path) ~= "string" and type(output_path) ~= "nil") then
- printUsage()
- return
- end
- local files = fs.find(fs.combine(folder,"ots_x*_y*_z*.nbt"))
- if #files == 0 then
- print("No valid files found")
- printUsage()
- return
- end
- local main_schem = nbt.COMPOUND()
- main_schem.root = nbt.COMPOUND()
- main_schem.root.blocks = nbt.LIST(10)
- main_schem.root.size = nbt.LIST(3)
- for i=1,3 do
- main_schem.root.size[i] = 0
- end
- main_schem.root.palette = nbt.LIST(10)
- main_schem.root.DataVersion = 2975 --idk why this needs to be here
- --true if palette blocks are the same
- local function isPaletteEqual(pb1,pb2)
- --print("check palette equal")
- if tostring(pb1.Name) ~= tostring(pb2.Name) then return false end
- if pb1.Properties then
- if not pb2.Properties then return false end
- for k,v in pairs(pb1.Properties) do
- if tostring(v) ~= tostring(pb2.Properties[k]) then return false end
- end
- for k,v in pairs(pb2.Properties) do
- if tostring(v) ~= tostring(pb1.Properties[k]) then return false end
- end
- else
- return pb1.Properties == pb2.Properties
- end
- end
- local function mergePalettes(p1,p2)
- --print("mergine palettes")
- local remap = {}
- for oldIdx,pb2 in pairs(p2) do
- oldIdx = oldIdx - 1 --I hate 1s based indexing
- for idx,pb1 in pairs(p1) do
- if isPaletteEqual(pb1,pb2) then
- remap[oldIdx] = idx - 1
- break
- end
- end
- if not remap[oldIdx] then
- remap[oldIdx] = #p1 --do this first because it is intentionally 1 smaller than the lua index (nbt uses 0 based indexing)
- p1[#p1+1] = pb2
- end
- end
- return remap
- end
- --Merge the other schematic into the main one
- local function mergeSchematic(main_schem_root,other_schem,other_schem_pos,main_schem_pos)
- --print("merge schematic")
- _,other_schem = next(rawget(other_schem,"data")) --schematics are wrapped in a secondary compound tag for no good reason
- local remap = mergePalettes(main_schem_root.palette,other_schem.palette)
- --copy over the blocks with the remap
- for _,block in pairs(other_schem.blocks) do
- --print("merging blocks")
- --write("blockstate: "..tostring(block.state))
- block.state = remap[nbt.tonumber(block.state)]
- --print(" remap: "..tostring(block.state))
- for i=1,3 do
- block.pos[i] = other_schem_pos[i]+block.pos[i]-main_schem_pos[i]
- --main_schem.root.size[i] = math.max(main_schem.root.size[i],block.pos[i]+1) --update schem size
- end
- main_schem_root.blocks[#main_schem_root.blocks+1] = block
- end
- --update size
- --print("updating size")
- for i=1,3 do
- --print(main_schem_root.size)
- --print(textutils.serialise(main_schem_pos):gsub("%s",""))
- 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])
- main_schem_pos[i] = math.min(main_schem_pos[i],other_schem_pos[i])
- end
- end
- local main_pos
- for _,path in pairs(files) do
- local x,y,z = assert(path:match("ots_x(%d+)_y(%d+)_z(%d+)%.nbt"))
- local pos = {tonumber(x),tonumber(y),tonumber(z)}
- if not main_pos then main_pos = pos end
- local file = fs.open(path,"r")
- local compressed_data = file.readAll()
- file.close()
- local binary_data = assert(zip:DecompressGzip(compressed_data))
- local schem = assert(nbt.frombinary(binary_data))
- mergeSchematic(main_schem.root,schem,pos,main_pos)
- os.queueEvent("nosleep")
- os.pullEvent()
- end
- output_path = output_path or string.format("merged/ots_x%d_y%d_z%d.nbt",unpack(main_pos))
- local binary_data = assert(nbt.tobinary(main_schem))
- local zipped_data = assert(zip:CompressGzip(binary_data))
- print("Outputting to",output_path)
- local file = fs.open(output_path,"w+")
- file.write(zipped_data)
- file.close()
Advertisement
Add Comment
Please, Sign In to add comment