def save_to_tag(chunk): tag = NBTFile() tag.name = "" level = TAG_Compound() tag["Level"] = level level["Blocks"] = TAG_Byte_Array() level["HeightMap"] = TAG_Byte_Array() level["BlockLight"] = TAG_Byte_Array() level["Data"] = TAG_Byte_Array() level["SkyLight"] = TAG_Byte_Array() level["Blocks"].value = chunk.blocks.tostring() level["HeightMap"].value = chunk.heightmap.tostring() level["BlockLight"].value = pack_nibbles(chunk.blocklight) level["Data"].value = pack_nibbles(chunk.metadata) level["SkyLight"].value = pack_nibbles(chunk.skylight) level["TerrainPopulated"] = TAG_Byte(chunk.populated) level["TileEntities"] = TAG_List(type=TAG_Compound) for tile in chunk.tiles.itervalues(): level["TileEntities"].tags.append(tile.save_to_tag()) return tag
def _save_level_to_tag(self, level): tag = NBTFile() tag.name = "" tag["Data"] = TAG_Compound() tag["Data"]["RandomSeed"] = TAG_Long(level.seed) tag["Data"]["SpawnX"] = TAG_Int(level.spawn[0]) tag["Data"]["SpawnY"] = TAG_Int(level.spawn[1]) tag["Data"]["SpawnZ"] = TAG_Int(level.spawn[2]) return tag
def _save_tile_to_tag(self, tile): tag = NBTFile() tag.name = "" tag["id"] = TAG_String(tile.name) tag["x"] = TAG_Int(tile.x) tag["y"] = TAG_Int(tile.y) tag["z"] = TAG_Int(tile.z) self._tile_savers[tile.name](tile, tag) return tag
class EmptyStringTest(unittest.TestCase): def setUp(self): self.golden_value = "\x0A\0\x04Test\x08\0\x0Cempty string\0\0\0" self.nbtfile = NBTFile(buffer=StringIO(self.golden_value)) def testReadEmptyString(self): self.assertEqual(self.nbtfile.name, "Test") self.assertEqual(self.nbtfile["empty string"].value, "") def testWriteEmptyString(self): buffer = StringIO() self.nbtfile.write_file(buffer=buffer) self.assertEqual(buffer.getvalue(), self.golden_value)
def _save_chunk_to_tag(self, chunk): tag = NBTFile() tag.name = "" level = TAG_Compound() tag["Level"] = level level["xPos"] = TAG_Int(chunk.x) level["zPos"] = TAG_Int(chunk.z) level["HeightMap"] = TAG_Byte_Array() level["BlockLight"] = TAG_Byte_Array() level["SkyLight"] = TAG_Byte_Array() level["Sections"] = TAG_List(type=TAG_Compound) for i, s in enumerate(chunk.sections): if s: section = TAG_Compound() section.name = "" section["Y"] = TAG_Byte(i) section["Blocks"] = TAG_Byte_Array() section["Blocks"].value = s.blocks.tostring() section["Data"] = TAG_Byte_Array() section["Data"].value = pack_nibbles(s.metadata) section["SkyLight"] = TAG_Byte_Array() section["SkyLight"].value = pack_nibbles(s.skylight) level["Sections"].tags.append(section) level["HeightMap"].value = chunk.heightmap.tostring() level["BlockLight"].value = pack_nibbles(chunk.blocklight) level["TerrainPopulated"] = TAG_Byte(chunk.populated) level["Entities"] = TAG_List(type=TAG_Compound) for entity in chunk.entities: try: entitytag = self._save_entity_to_tag(entity) level["Entities"].tags.append(entitytag) except KeyError: log.msg("Unknown entity %s" % entity.name) level["TileEntities"] = TAG_List(type=TAG_Compound) for tile in chunk.tiles.itervalues(): try: tiletag = self._save_tile_to_tag(tile) level["TileEntities"].tags.append(tiletag) except KeyError: log.msg("Unknown tile entity %s" % tile.name) return tag
def save_to_tag(chest): tag = NBTFile() tag.name = "" tag["id"] = TAG_String("Chest") tag["x"] = TAG_Int(chest.x) tag["y"] = TAG_Int(chest.y) tag["z"] = TAG_Int(chest.z) tag["Items"] = chest.inventory.save_to_tag() return tag
def save_player(self, player): tag = NBTFile() tag.name = "" tag["Pos"] = TAG_List(type=TAG_Double) tag["Pos"].tags = [TAG_Double(i) for i in player.location.pos] tag["Rotation"] = TAG_List(type=TAG_Double) tag["Rotation"].tags = [TAG_Double(i) for i in player.location.ori.to_degs()] tag["Inventory"] = self._save_inventory_to_tag(player.inventory) fp = self.folder.child("players").child("%s.dat" % player.username) self._write_tag(fp, tag)
def save_player(self, player): tag = NBTFile() tag.name = "" tag["Pos"] = TAG_List(type=TAG_Double) tag["Pos"].tags = [TAG_Double(i) for i in player.location.pos] tag["Rotation"] = TAG_List(type=TAG_Double) tag["Rotation"].tags = [ TAG_Double(i) for i in player.location.ori.to_degs() ] tag["Inventory"] = self._save_inventory_to_tag(player.inventory) fp = self.folder.child("players").child("%s.dat" % player.username) self._write_tag(fp, tag)
def save_to_tag(player): tag = NBTFile() tag.name = "" tag["Pos"] = TAG_List(type=TAG_Double) tag["Pos"].tags = [TAG_Double(i) for i in (player.location.x, player.location.y, player.location.z)] tag["Rotation"] = TAG_List(type=TAG_Double) tag["Rotation"].tags = [TAG_Double(i) for i in (player.location.yaw, 0)] tag["Inventory"] = player.inventory.save_to_tag() return tag
def _save_level_to_tag(self, level): tag = NBTFile() tag.name = "" tag["Data"] = TAG_Compound() tag["Data"]["RandomSeed"] = TAG_Long(level.seed) tag["Data"]["SpawnX"] = TAG_Int(level.spawn[0]) tag["Data"]["SpawnY"] = TAG_Int(level.spawn[1]) tag["Data"]["SpawnZ"] = TAG_Int(level.spawn[2]) tag["Data"]["Time"] = TAG_Long(level.time) # Beta version and accounting. # Needed for Notchian tools to be able to comprehend this world. tag["Data"]["version"] = TAG_Int(19132) tag["Data"]["LevelName"] = TAG_String("Generated by Bravo :3") return tag
def save_to_tag(sign): tag = NBTFile() tag.name = "" tag["id"] = TAG_String("Sign") tag["x"] = TAG_Int(sign.x) tag["y"] = TAG_Int(sign.y) tag["z"] = TAG_Int(sign.z) tag["Text1"] = TAG_String(sign.text1) tag["Text2"] = TAG_String(sign.text2) tag["Text3"] = TAG_String(sign.text3) tag["Text4"] = TAG_String(sign.text4) return tag
def save_player(self, player): tag = NBTFile() tag.name = "" tag["Pos"] = TAG_List(type=TAG_Double) tag["Pos"].tags = [TAG_Double(i) for i in (player.location.x, player.location.y, player.location.z)] tag["Rotation"] = TAG_List(type=TAG_Double) tag["Rotation"].tags = [TAG_Double(i) for i in (player.location.yaw, player.location.pitch)] tag["Inventory"] = self._save_inventory_to_tag(player.inventory) fp = self.folder.child("players") if not fp.exists(): fp.makedirs() fp = fp.child("%s.dat" % player.username) self._write_tag(fp, tag)
def _save_entity_to_tag(self, entity): tag = NBTFile() tag.name = "" tag["id"] = TAG_String(entity.name) position = entity.location.pos tag["Pos"] = TAG_List(type=TAG_Double) tag["Pos"].tags = [TAG_Double(i) for i in position] rotation = entity.location.ori.to_degs() tag["Rotation"] = TAG_List(type=TAG_Double) tag["Rotation"].tags = [TAG_Double(i) for i in rotation] tag["OnGround"] = TAG_Byte(int(entity.location.grounded)) self._entity_savers[entity.name](entity, tag) return tag
def _save_entity_to_tag(self, entity): tag = NBTFile() tag.name = "" tag["id"] = TAG_String(entity.name) position = [entity.location.x, entity.location.y, entity.location.z] tag["Pos"] = TAG_List(type=TAG_Double) tag["Pos"].tags = [TAG_Double(i) for i in position] rotation = [entity.location.yaw, entity.location.pitch] tag["Rotation"] = TAG_List(type=TAG_Double) tag["Rotation"].tags = [TAG_Double(i) for i in rotation] tag["OnGround"] = TAG_Byte(int(entity.location.grounded)) self._entity_savers[entity.name](entity, tag) return tag
def _save_chunk_to_tag(self, chunk): tag = NBTFile() tag.name = "" level = TAG_Compound() tag["Level"] = level level["Blocks"] = TAG_Byte_Array() level["HeightMap"] = TAG_Byte_Array() level["BlockLight"] = TAG_Byte_Array() level["Data"] = TAG_Byte_Array() level["SkyLight"] = TAG_Byte_Array() level["Blocks"].value = chunk.blocks.tostring() level["HeightMap"].value = chunk.heightmap.tostring() level["BlockLight"].value = pack_nibbles(chunk.blocklight) level["Data"].value = pack_nibbles(chunk.metadata) level["SkyLight"].value = pack_nibbles(chunk.skylight) level["TerrainPopulated"] = TAG_Byte(chunk.populated) level["Entities"] = TAG_List(type=TAG_Compound) for entity in chunk.entities: try: entitytag = self._save_entity_to_tag(entity) level["Entities"].tags.append(entitytag) except KeyError: print "Unknown entity %s" % entity.name level["TileEntities"] = TAG_List(type=TAG_Compound) for tile in chunk.tiles.itervalues(): try: tiletag = self._save_tile_to_tag(tile) level["TileEntities"].tags.append(tiletag) except KeyError: print "Unknown tile entity %s" % tile.name return tag
def load_chunk(self, x, z): name = name_for_anvil(x, z) fp = self.folder.child("region").child(name) region = Region(fp) chunk = Chunk(x, z) try: data = region.get_chunk(x, z) tag = NBTFile(buffer=StringIO(data)) self._load_chunk_from_tag(chunk, tag) except MissingChunk: raise SerializerReadException("No chunk %r in region" % chunk) except Exception, e: raise SerializerReadException("%r couldn't be loaded: %s" % (chunk, e))
def load_chunk(self, chunk): region = name_for_region(chunk.x, chunk.z) fp = self.folder.child("region").child(region) if not fp.exists(): return x, z = chunk.x % 32, chunk.z % 32 if region not in self.regions: self.cache_region_pages(region) positions = self.regions[region][0] if (x, z) not in positions: return position, pages = positions[x, z] if not position or not pages: return with fp.open("r") as handle: handle.seek(position * 4096) data = handle.read(pages * 4096) length = unpack(">L", data[:4])[0] - 1 version = ord(data[4]) data = data[5:length+5] if version == 1: data = data.decode("gzip") fileobj = GzipFile(fileobj=StringIO(data)) elif version == 2: fileobj = StringIO(data.decode("zlib")) tag = NBTFile(buffer=fileobj) return self._load_chunk_from_tag(chunk, tag)
def setUp(self): self.golden_value = "\x0A\0\x04Test\x08\0\x0Cempty string\0\0\0" self.nbtfile = NBTFile(buffer=StringIO(self.golden_value))
def testWriteBig(self): mynbt = NBTFile(self.f.name) output = StringIO() mynbt.write_file(buffer=output) self.assertTrue(GzipFile(self.f.name).read() == output.getvalue())
def setUp(self): self.nbtfile = NBTFile()
def testWriteback(self): mynbt = NBTFile(self.f.name) mynbt.write_file()
def testReadBig(self): mynbt = NBTFile(self.f.name) self.assertTrue(mynbt.filename != None) self.assertEqual(len(mynbt.tags), 11)
def _read_tag(self, fp): if fp.exists() and fp.getsize(): return NBTFile(fileobj=fp.open("r")) return None
#!/usr/bin/env python import sys from bravo.nbt import NBTFile if len(sys.argv) < 2: print "Usage: %s <file>" % __name__ f = NBTFile(sys.argv[1]) print f.pretty_tree()