t8rt0t

schem.lua

Jul 28th, 2024 (edited)
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.55 KB | None | 0 0
  1. local nbt = require"nbt"
  2. local zip = require"LibDeflate"
  3. local expect = require("cc.expect").expect
  4.  
  5. --true if palette blocks are the same
  6. local function isPaletteEqual(pb1,pb2)
  7.     --print("check palette equal")
  8.     if tostring(pb1.Name) ~= tostring(pb2.Name) then return false end
  9.     if pb1.Properties then
  10.         if not pb2.Properties then return false end
  11.         for k,v in pairs(pb1.Properties) do
  12.             if tostring(v) ~= tostring(pb2.Properties[k]) then return false end
  13.         end
  14.         for k,v in pairs(pb2.Properties) do
  15.             if tostring(v) ~= tostring(pb1.Properties[k]) then return false end
  16.         end
  17.     else
  18.         return pb1.Properties == pb2.Properties
  19.     end
  20. end
  21.  
  22. --Merge the other schematic into the main one
  23. local function mergeSchematic(main_schem_root,other_schem,other_schem_pos,main_schem_pos)
  24.     --print("merge schematic")
  25.     --_,other_schem = next(rawget(other_schem,"data")) --schematics are wrapped in a secondary compound tag for no good reason
  26.     local remap = mergePalettes(main_schem_root.palette,other_schem.palette)
  27.     --copy over the blocks with the remap
  28.     for _,block in pairs(other_schem.blocks) do
  29.         --print("merging blocks")
  30.         --write("blockstate: "..tostring(block.state))
  31.         block.state = remap[nbt.tonumber(block.state)]
  32.         --print(" remap: "..tostring(block.state))
  33.         for i=1,3 do
  34.             block.pos[i] = other_schem_pos[i]+block.pos[i]-main_schem_pos[i]
  35.             --main_schem.root.size[i] = math.max(main_schem.root.size[i],block.pos[i]+1) --update schem size
  36.         end
  37.         main_schem_root.blocks[#main_schem_root.blocks+1] = block
  38.     end
  39.     for i=1,3 do
  40.         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])
  41.         main_schem_pos[i] = math.min(nbt.tonumber(main_schem_pos[i]),nbt.tonumber(other_schem_pos[i]))
  42.     end
  43. end
  44.  
  45. local function mergePalettes(p1,p2)
  46.     --print("mergine palettes")
  47.     local remap = {}
  48.     for oldIdx,pb2 in pairs(p2) do
  49.         oldIdx = oldIdx - 1 --I hate 1s based indexing
  50.         for idx,pb1 in pairs(p1) do
  51.             if isPaletteEqual(pb1,pb2) then
  52.                 remap[oldIdx] = idx - 1
  53.                 break
  54.             end
  55.         end
  56.         if not remap[oldIdx] then
  57.             remap[oldIdx] = #p1 --do this first because it is intentionally 1 smaller than the lua index (nbt uses 0 based indexing)
  58.             p1[#p1+1] = pb2
  59.         end
  60.     end
  61.     return remap
  62. end
  63.  
  64. local osepoch = os.epoch
  65. local nosleeptimer = osepoch("UTC") + 5000
  66. local function nosleep()
  67.     if osepoch("UTC") > nosleeptimer then
  68.         os.queueEvent("nosleep")
  69.         os.pullEvent()
  70.         nosleeptimer = osepoch("UTC") + 5000
  71.     end
  72. end
  73.  
  74. local SCHEM = {}
  75. local schem_mt = {__index=SCHEM}
  76. function SCHEM.new()
  77.     local data = {}
  78.     local schem = nbt.COMPOUND()
  79.     schem.root = nbt.COMPOUND()
  80.     schem.root.blocks = nbt.LIST(10)
  81.     schem.root.size = nbt.LIST(3,{0,0,0})
  82.     schem.root.pos = nbt.LIST(3,{0,0,0})
  83.     -- for i=1,3 do
  84.     --     schem.root.size[i] = 0
  85.     --     schem.root.pos[i] = 0
  86.     -- end
  87.     schem.root.palette = nbt.LIST(10)
  88.     print(schem.root.palette)
  89.     schem.root.DataVersion = 2975
  90.     data.schem = schem
  91.     data.palette_cache = {}
  92.     data.pos_cache = {}
  93.     setmetatable(data,schem_mt)
  94.     return data
  95. end
  96. function SCHEM.merge(schem1,schem2)
  97.     if getmetatable(schem1) ~= schem_mt or getmetatable(schem2) ~= schem_mt then error("Arguments must both be schematics") end
  98.     return mergeSchematic(schem1.schem.root,schem2.schem.root,schem2.schem.root.pos,schem2.schem.root.pos)
  99. end
  100. function SCHEM.open(path)
  101.     assert(fs.exists(path),"Schematic must be part of a file")
  102.     local f = fs.open(path,"r")
  103.     local data = f.readAll()
  104.     f.close()
  105.     nosleep()
  106.     local unzipped_data = assert(zip:DecompressGzip(data))
  107.     local schem = assert(nbt.frombinary(unzipped_data))
  108.     local data = {schem=schem,palette_cache = {},pos_cache = {}}
  109.     nosleep()
  110.     for k,v in pairs(schem.root.blocks) do
  111.         nosleep()
  112.         data.pos_cache[tostring(v.pos)] = k
  113.     end
  114.     for k,v in pairs(schem.root.palette) do
  115.         if not v.Properties then
  116.             data.palette_cache[v.Name.data] = k - 1 --0 based indexing for the State property
  117.         end
  118.     end
  119.     nosleep()
  120.     setmetatable(data,schem_mt)
  121.     return data
  122. end
  123. function SCHEM.setBlock(schem,x,y,z,name,properties,tag)
  124.     if getmetatable(schem) ~= schem_mt then error("Bad argument #1 (schem expected, got "..type(schem)..")") end
  125.     expect(2,x,"number")
  126.     expect(3,y,"number")
  127.     expect(4,z,"number")
  128.     expect(5,name,"string")
  129.     expect(6,properties,"table","nil")
  130.     expect(7,nbt,"table","nil")
  131.     local state = #schem.schem.root.palette
  132.     if not properties then
  133.         if schem.palette_cache[name] then
  134.             state = schem.palette_cache[name]
  135.         else
  136.             schem.palette_cache[name] = state
  137.             print(schem.schem.root.palette)
  138.             schem.schem.root.palette[state+1] = nbt.COMPOUND({Name=name})
  139.         end
  140.     else
  141.         schem.schem.root.palette[state+1] = nbt.COMPOUND({Name=name,Properties=properties})
  142.     end
  143.     local pos = string.format("[%d,%d,%d]",x,y,z)
  144.     local blockIdx
  145.     if schem.pos_cache[pos] then
  146.         blockIdx = schem.pos_cache[pos]
  147.     else
  148.         blockIdx = #schem.schem.root.blocks+1
  149.         schem.pos_cache[pos] = blockIdx
  150.     end
  151.     local block = nbt.COMPOUND({state=state,pos=nbt.LIST(3,{x,y,z})})
  152.     if tag then
  153.         block.nbt = tag
  154.     end
  155.     schem.schem.root.blocks[blockIdx] = block
  156.     return schem
  157. end
  158. function SCHEM.save(schem,path)
  159.     if getmetatable(schem) ~= schem_mt then error("Bad argument #1 (schem expected, got "..type(schem)..")") end
  160.     expect(2,path,"string")
  161.     os.queueEvent("nosleep")
  162.     os.pullEvent()
  163.     local binarydata = assert(nbt.tobinary(schem.schem))
  164.     os.queueEvent("nosleep")
  165.     os.pullEvent()
  166.     local zippeddata = assert(zip:CompressGzip(binarydata))
  167.     os.queueEvent("nosleep")
  168.     os.pullEvent()
  169.     local f = fs.open(path,"w+")
  170.     f.write(zippeddata)
  171.     f.close()
  172.     return schem
  173. end
  174. function SCHEM.setSize(schem,x,y,z)
  175.     if getmetatable(schem) ~= schem_mt then error("Bad argument #1 (schem expected, got "..type(schem)..")") end
  176.     expect(2,x,"number")
  177.     expect(3,y,"number")
  178.     expect(4,z,"number")
  179.     schem.schem.root.size = nbt.LIST(3,{x,y,z})
  180.     return schem
  181. end
  182. local SCHEM_MT = {
  183.     __call = SCHEM.new
  184. }
  185. setmetatable(SCHEM,SCHEM_MT)
  186.  
  187. return SCHEM
Advertisement
Add Comment
Please, Sign In to add comment