public AnvilSection LoadTree(TagNode tree) { TagNodeCompound ctree = tree as TagNodeCompound; if (ctree == null) { return(null); } _y = ctree["Y"] as TagNodeByte; _blocks = new YZXByteArray(XDIM, YDIM, ZDIM, ctree["Blocks"] as TagNodeByteArray); _data = new YZXNibbleArray(XDIM, YDIM, ZDIM, ctree["Data"] as TagNodeByteArray); _skyLight = new YZXNibbleArray(XDIM, YDIM, ZDIM, ctree["SkyLight"] as TagNodeByteArray); _blockLight = new YZXNibbleArray(XDIM, YDIM, ZDIM, ctree["BlockLight"] as TagNodeByteArray); if (!ctree.ContainsKey("AddBlocks")) { ctree["AddBlocks"] = new TagNodeByteArray(new byte[2048]); } _addBlocks = new YZXNibbleArray(XDIM, YDIM, ZDIM, ctree["AddBlocks"] as TagNodeByteArray); _tree = ctree; return(this); }
private void BuildNbtTree() { int elements3 = XDIM * YDIM * ZDIM; TagNodeByteArray blocks = new TagNodeByteArray(new byte[elements3]); TagNodeByteArray data = new TagNodeByteArray(new byte[elements3 >> 1]); TagNodeByteArray skyLight = new TagNodeByteArray(new byte[elements3 >> 1]); TagNodeByteArray blockLight = new TagNodeByteArray(new byte[elements3 >> 1]); TagNodeByteArray addBlocks = new TagNodeByteArray(new byte[elements3 >> 1]); _blocks = new YZXByteArray(XDIM, YDIM, ZDIM, blocks); _data = new YZXNibbleArray(XDIM, YDIM, ZDIM, data); _skyLight = new YZXNibbleArray(XDIM, YDIM, ZDIM, skyLight); _blockLight = new YZXNibbleArray(XDIM, YDIM, ZDIM, blockLight); _addBlocks = new YZXNibbleArray(XDIM, YDIM, ZDIM, addBlocks); TagNodeCompound tree = new TagNodeCompound(); tree.Add("Y", new TagNodeByte(_y)); tree.Add("Blocks", blocks); tree.Add("Data", data); tree.Add("SkyLight", skyLight); tree.Add("BlockLight", blockLight); tree.Add("AddBlocks", addBlocks); _tree = tree; }
public CompositeYZXByteArray(YZXByteArray[] sections) { for (int i = 0; i < sections.Length; i++) if (sections[i] == null) throw new ArgumentException("sections argument cannot have null entries."); for (int i = 0; i < sections.Length; i++) { if (sections[i].Length != sections[0].Length || sections[i].XDim != sections[0].XDim || sections[i].YDim != sections[0].YDim || sections[i].ZDim != sections[0].ZDim) throw new ArgumentException("All elements in sections argument must have same metrics."); } _sections = sections; }
/// <summary> /// Exports the <see cref="Schematic"/> object to a schematic file. /// </summary> /// <param name="path">The path to write out the schematic file to.</param> public void Export(string path) { int xdim = _blockset.XDim; int ydim = _blockset.YDim; int zdim = _blockset.ZDim; byte[] blockData = new byte[xdim * ydim * zdim]; byte[] dataData = new byte[xdim * ydim * zdim]; YZXByteArray schemaBlocks = new YZXByteArray(_blockset.XDim, _blockset.YDim, _blockset.ZDim, blockData); YZXByteArray schemaData = new YZXByteArray(_blockset.XDim, _blockset.YDim, _blockset.ZDim, dataData); TagNodeList entities = new TagNodeList(TagType.TAG_COMPOUND); TagNodeList tileEntities = new TagNodeList(TagType.TAG_COMPOUND); for (int x = 0; x < xdim; x++) { for (int z = 0; z < zdim; z++) { for (int y = 0; y < ydim; y++) { AlphaBlock block = _blockset.GetBlock(x, y, z); schemaBlocks[x, y, z] = (byte)block.ID; schemaData[x, y, z] = (byte)block.Data; TileEntity te = block.GetTileEntity(); if (te != null) { te.X = x; te.Y = y; te.Z = z; tileEntities.Add(te.BuildTree()); } } } } foreach (EntityTyped e in _entityset) { entities.Add(e.BuildTree()); } TagNodeCompound schematic = new TagNodeCompound(); schematic["Width"] = new TagNodeShort((short)xdim); schematic["Length"] = new TagNodeShort((short)zdim); schematic["Height"] = new TagNodeShort((short)ydim); schematic["Entities"] = entities; schematic["TileEntities"] = tileEntities; schematic["Materials"] = new TagNodeString("Alpha"); schematic["Blocks"] = new TagNodeByteArray(blockData); schematic["Data"] = new TagNodeByteArray(dataData); NBTFile schematicFile = new NBTFile(path); Stream nbtStream = schematicFile.GetDataOutputStream(); if (nbtStream == null) { return; } NbtTree tree = new NbtTree(schematic, "Schematic"); tree.WriteTo(nbtStream); nbtStream.Close(); }
/// <summary> /// Imports a schematic file at the given path and returns in as a <see cref="Schematic"/> object. /// </summary> /// <param name="path">The path to the schematic file.</param> /// <returns>A <see cref="Schematic"/> object containing the decoded schematic file data.</returns> public static Schematic Import(string path) { NBTFile schematicFile = new NBTFile(path); if (!schematicFile.Exists()) { return(null); } Stream nbtStream = schematicFile.GetDataInputStream(); if (nbtStream == null) { return(null); } NbtTree tree = new NbtTree(nbtStream); NbtVerifier v = new NbtVerifier(tree.Root, _schema); if (!v.Verify()) { return(null); } //TagNodeCompound schematic = tree.Root["Schematic"] as TagNodeCompound; TagNodeCompound schematic = tree.Root; int xdim = schematic["Width"].ToTagShort(); int zdim = schematic["Length"].ToTagShort(); int ydim = schematic["Height"].ToTagShort(); Schematic self = new Schematic(xdim, ydim, zdim); // Damnit, schematic is YZX ordering. YZXByteArray schemaBlocks = new YZXByteArray(xdim, ydim, zdim, schematic["Blocks"].ToTagByteArray()); YZXByteArray schemaData = new YZXByteArray(xdim, ydim, zdim, schematic["Data"].ToTagByteArray()); for (int x = 0; x < xdim; x++) { for (int y = 0; y < ydim; y++) { for (int z = 0; z < zdim; z++) { self._blocks[x, y, z] = schemaBlocks[x, y, z]; self._data[x, y, z] = schemaData[x, y, z]; } } } TagNodeList entities = schematic["Entities"] as TagNodeList; foreach (TagNode e in entities) { self._entities.Add(e); } TagNodeList tileEntities = schematic["TileEntities"] as TagNodeList; foreach (TagNode te in tileEntities) { self._tileEntities.Add(te); } self._blockset.Refresh(); return(self); }
/// <summary> /// Exports the <see cref="Schematic"/> object to a schematic file. /// </summary> /// <param name="path">The path to write out the schematic file to.</param> public void Export(string path) { int xdim = _blockset.XDim; int ydim = _blockset.YDim; int zdim = _blockset.ZDim; byte[] blockData = new byte[xdim * ydim * zdim]; byte[] dataData = new byte[xdim * ydim * zdim]; YZXByteArray schemaBlocks = new YZXByteArray(_blockset.XDim, _blockset.YDim, _blockset.ZDim, blockData); YZXByteArray schemaData = new YZXByteArray(_blockset.XDim, _blockset.YDim, _blockset.ZDim, dataData); TagNodeList entities = new TagNodeList(TagType.TAG_COMPOUND); TagNodeList tileEntities = new TagNodeList(TagType.TAG_COMPOUND); for (int x = 0; x < xdim; x++) { for (int z = 0; z < zdim; z++) { for (int y = 0; y < ydim; y++) { AlphaBlock block = _blockset.GetBlock(x, y, z); schemaBlocks[x, y, z] = (byte)block.ID; schemaData[x, y, z] = (byte)block.Data; TileEntity te = block.GetTileEntity(); if (te != null) { te.X = x; te.Y = y; te.Z = z; tileEntities.Add(te.BuildTree()); } } } } foreach (TypedEntity e in _entityset) { entities.Add(e.BuildTree()); } TagNodeCompound schematic = new TagNodeCompound(); schematic["Width"] = new TagNodeShort((short)xdim); schematic["Length"] = new TagNodeShort((short)zdim); schematic["Height"] = new TagNodeShort((short)ydim); schematic["Entities"] = entities; schematic["TileEntities"] = tileEntities; schematic["Materials"] = new TagNodeString("Alpha"); schematic["Blocks"] = new TagNodeByteArray(blockData); schematic["Data"] = new TagNodeByteArray(dataData); NBTFile schematicFile = new NBTFile(path); Stream nbtStream = schematicFile.GetDataOutputStream(); if (nbtStream == null) { return; } NbtTree tree = new NbtTree(schematic, "Schematic"); tree.WriteTo(nbtStream); nbtStream.Close(); }
/// <summary> /// Imports a schematic file at the given path and returns in as a <see cref="Schematic"/> object. /// </summary> /// <param name="path">The path to the schematic file.</param> /// <returns>A <see cref="Schematic"/> object containing the decoded schematic file data.</returns> public static Schematic Import(string path) { NBTFile schematicFile = new NBTFile(path); if (!schematicFile.Exists()) { return null; } Stream nbtStream = schematicFile.GetDataInputStream(); if (nbtStream == null) { return null; } NbtTree tree = new NbtTree(nbtStream); NbtVerifier v = new NbtVerifier(tree.Root, _schema); if (!v.Verify()) { return null; } //TagNodeCompound schematic = tree.Root["Schematic"] as TagNodeCompound; TagNodeCompound schematic = tree.Root; int xdim = schematic["Width"].ToTagShort(); int zdim = schematic["Length"].ToTagShort(); int ydim = schematic["Height"].ToTagShort(); Schematic self = new Schematic(xdim, ydim, zdim); // Damnit, schematic is YZX ordering. YZXByteArray schemaBlocks = new YZXByteArray(xdim, ydim, zdim, schematic["Blocks"].ToTagByteArray()); YZXByteArray schemaData = new YZXByteArray(xdim, ydim, zdim, schematic["Data"].ToTagByteArray()); for (int x = 0; x < xdim; x++) { for (int y = 0; y < ydim; y++) { for (int z = 0; z < zdim; z++) { self._blocks[x, y, z] = schemaBlocks[x, y, z]; self._data[x, y, z] = schemaData[x, y, z]; } } } TagNodeList entities = schematic["Entities"] as TagNodeList; foreach (TagNode e in entities) { self._entities.Add(e); } TagNodeList tileEntities = schematic["TileEntities"] as TagNodeList; foreach (TagNode te in tileEntities) { self._tileEntities.Add(te); } self._blockset.Refresh(); return self; }
private void BuildNBTTree() { int elements2 = XDIM * ZDIM; _sections = new AnvilSection[16]; TagNodeList sections = new TagNodeList(TagType.TAG_COMPOUND); for (int i = 0; i < _sections.Length; i++) { _sections[i] = new AnvilSection(i); sections.Add(_sections[i].BuildTree()); } YZXByteArray[] blocksBA = new YZXByteArray[_sections.Length]; YZXNibbleArray[] dataBA = new YZXNibbleArray[_sections.Length]; YZXNibbleArray[] skyLightBA = new YZXNibbleArray[_sections.Length]; YZXNibbleArray[] blockLightBA = new YZXNibbleArray[_sections.Length]; for (int i = 0; i < _sections.Length; i++) { blocksBA[i] = _sections[i].Blocks; dataBA[i] = _sections[i].Data; skyLightBA[i] = _sections[i].SkyLight; blockLightBA[i] = _sections[i].BlockLight; } _blocks = new CompositeYZXByteArray(blocksBA); _data = new CompositeYZXNibbleArray(dataBA); _skyLight = new CompositeYZXNibbleArray(skyLightBA); _blockLight = new CompositeYZXNibbleArray(blockLightBA); TagNodeIntArray heightMap = new TagNodeIntArray(new int[elements2]); _heightMap = new ZXIntArray(XDIM, ZDIM, heightMap); TagNodeByteArray biomes = new TagNodeByteArray(new byte[elements2]); _biomes = new ZXByteArray(XDIM, ZDIM, biomes); for (int x = 0; x < XDIM; x++) { for (int z = 0; z < ZDIM; z++) { _biomes[x, z] = BiomeType.Default; } } _entities = new TagNodeList(TagType.TAG_COMPOUND); _tileEntities = new TagNodeList(TagType.TAG_COMPOUND); _tileTicks = new TagNodeList(TagType.TAG_COMPOUND); TagNodeCompound level = new TagNodeCompound(); level.Add("Sections", sections); level.Add("HeightMap", heightMap); level.Add("Biomes", biomes); level.Add("Entities", _entities); level.Add("TileEntities", _tileEntities); level.Add("TileTicks", _tileTicks); level.Add("LastUpdate", new TagNodeLong(Timestamp())); level.Add("xPos", new TagNodeInt(_cx)); level.Add("zPos", new TagNodeInt(_cz)); level.Add("TerrainPopulated", new TagNodeByte()); _tree = new NbtTree(); _tree.Root.Add("Level", level); _blockManager = new AlphaBlockCollection(_blocks, _data, _blockLight, _skyLight, _heightMap, _tileEntities); _entityManager = new EntityCollection(_entities); }
public AnvilChunk LoadTree(TagNode tree) { TagNodeCompound ctree = tree as TagNodeCompound; if (ctree == null) { return(null); } _tree = new NbtTree(ctree); TagNodeCompound level = _tree.Root["Level"] as TagNodeCompound; TagNodeList sections = level["Sections"] as TagNodeList; foreach (TagNodeCompound section in sections) { AnvilSection anvilSection = new AnvilSection(section); if (anvilSection.Y < 0 || anvilSection.Y >= _sections.Length) { continue; } _sections[anvilSection.Y] = anvilSection; } YZXByteArray[] blocksBA = new YZXByteArray[_sections.Length]; YZXNibbleArray[] dataBA = new YZXNibbleArray[_sections.Length]; YZXNibbleArray[] skyLightBA = new YZXNibbleArray[_sections.Length]; YZXNibbleArray[] blockLightBA = new YZXNibbleArray[_sections.Length]; for (int i = 0; i < _sections.Length; i++) { if (_sections[i] == null) { _sections[i] = new AnvilSection(i); } blocksBA[i] = _sections[i].Blocks; dataBA[i] = _sections[i].Data; skyLightBA[i] = _sections[i].SkyLight; blockLightBA[i] = _sections[i].BlockLight; } _blocks = new CompositeYZXByteArray(blocksBA); _data = new CompositeYZXNibbleArray(dataBA); _skyLight = new CompositeYZXNibbleArray(skyLightBA); _blockLight = new CompositeYZXNibbleArray(blockLightBA); _heightMap = new ZXIntArray(XDIM, ZDIM, level["HeightMap"] as TagNodeIntArray); if (level.ContainsKey("Biomes")) { _biomes = new ZXByteArray(XDIM, ZDIM, level["Biomes"] as TagNodeByteArray); } else { level["Biomes"] = new TagNodeByteArray(new byte[256]); _biomes = new ZXByteArray(XDIM, ZDIM, level["Biomes"] as TagNodeByteArray); for (int x = 0; x < XDIM; x++) { for (int z = 0; z < ZDIM; z++) { _biomes[x, z] = BiomeType.Default; } } } _entities = level["Entities"] as TagNodeList; _tileEntities = level["TileEntities"] as TagNodeList; if (level.ContainsKey("TileTicks")) { _tileTicks = level["TileTicks"] as TagNodeList; } else { _tileTicks = new TagNodeList(TagType.TAG_COMPOUND); } // List-type patch up if (_entities.Count == 0) { level["Entities"] = new TagNodeList(TagType.TAG_COMPOUND); _entities = level["Entities"] as TagNodeList; } if (_tileEntities.Count == 0) { level["TileEntities"] = new TagNodeList(TagType.TAG_COMPOUND); _tileEntities = level["TileEntities"] as TagNodeList; } if (_tileTicks.Count == 0) { level["TileTicks"] = new TagNodeList(TagType.TAG_COMPOUND); _tileTicks = level["TileTicks"] as TagNodeList; } _cx = level["xPos"].ToTagInt(); _cz = level["zPos"].ToTagInt(); _blockManager = new AlphaBlockCollection(_blocks, _data, _blockLight, _skyLight, _heightMap, _tileEntities, _tileTicks); _entityManager = new EntityCollection(_entities); return(this); }