本文整理汇总了Python中nbt.load函数的典型用法代码示例。如果您正苦于以下问题:Python load函数的具体用法?Python load怎么用?Python load使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了load函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: findTrueSpawn
def findTrueSpawn(self):
"""Adds the true spawn location to self.POI. The spawn Y coordinate
is almost always the default of 64. Find the first air block above
that point for the true spawn location"""
## read spawn info from level.dat
data = nbt.load(os.path.join(self.worlddir, "level.dat"))[1]
spawnX = data["Data"]["SpawnX"]
spawnY = data["Data"]["SpawnY"]
spawnZ = data["Data"]["SpawnZ"]
## The chunk that holds the spawn location
chunkX = spawnX / 16
chunkY = spawnZ / 16
## The filename of this chunk
chunkFile = os.path.join(
base36encode(chunkX % 64),
base36encode(chunkY % 64),
"c.%s.%s.dat" % (base36encode(chunkX), base36encode(chunkY)),
)
data = nbt.load(os.path.join(self.worlddir, chunkFile))[1]
level = data["Level"]
blockArray = numpy.frombuffer(level["Blocks"], dtype=numpy.uint8).reshape((16, 16, 128))
## The block for spawn *within* the chunk
inChunkX = spawnX - (chunkX * 16)
inChunkZ = spawnZ - (chunkY * 16)
## find the first air block
while blockArray[inChunkX, inChunkZ, spawnY] != 0:
spawnY += 1
self.POI.append(dict(x=spawnX, y=spawnY, z=spawnZ, msg="Spawn"))
示例2: findTrueSpawn
def findTrueSpawn(self):
"""Adds the true spawn location to self.POI. The spawn Y coordinate
is almost always the default of 64. Find the first air block above
that point for the true spawn location"""
## read spawn info from level.dat
data = nbt.load(os.path.join(self.worlddir, "level.dat"))[1]
spawnX = data['Data']['SpawnX']
spawnY = data['Data']['SpawnY']
spawnZ = data['Data']['SpawnZ']
## The chunk that holds the spawn location
chunkX = spawnX/16
chunkY = spawnZ/16
## The filename of this chunk
chunkFile = self.get_chunk_path(chunkX, chunkY)
data=nbt.load(chunkFile)[1]
level = data['Level']
blockArray = numpy.frombuffer(level['Blocks'], dtype=numpy.uint8).reshape((16,16,128))
## The block for spawn *within* the chunk
inChunkX = spawnX - (chunkX*16)
inChunkZ = spawnZ - (chunkY*16)
## find the first air block
while (blockArray[inChunkX, inChunkZ, spawnY] != 0):
spawnY += 1
self.POI.append( dict(x=spawnX, y=spawnY, z=spawnZ,
msg="Spawn", type="spawn", chunk=(inChunkX,inChunkZ)))
示例3: __init__
def __init__(self, shape=None, root_tag=None, filename=None, mats="Alpha"):
""" shape is (x,y,z) for a new level's shape. if none, takes
root_tag as a TAG_Compound for an existing schematic file. if
none, tries to read the tag from filename. if none, results
are undefined. materials can be a MCMaterials instance, or one of
"Classic", "Alpha", "Pocket" to indicate allowable blocks. The default
is Alpha.
block coordinate order in the file is y,z,x to use the same code as classic/indev levels.
in hindsight, this was a completely arbitrary decision.
the Entities and TileEntities are nbt.TAG_List objects containing TAG_Compounds.
this makes it easy to copy entities without knowing about their insides.
rotateLeft swaps the axes of the different arrays. because of this, the Width, Height, and Length
reflect the current dimensions of the schematic rather than the ones specified in the NBT structure.
I'm not sure what happens when I try to re-save a rotated schematic.
"""
# if(shape != None):
# self.setShape(shape)
if filename:
self.filename = filename
if None is root_tag and os.path.exists(filename):
root_tag = nbt.load(filename)
else:
self.filename = None
if mats in namedMaterials:
self.materials = namedMaterials[mats]
else:
assert isinstance(mats, MCMaterials)
self.materials = mats
if root_tag:
self.root_tag = root_tag
if Materials in root_tag:
self.materials = namedMaterials[self.Materials]
else:
root_tag[Materials] = nbt.TAG_String(self.materials.name)
self.shapeChunkData()
else:
assert shape != None
root_tag = nbt.TAG_Compound(name="Schematic")
root_tag[Height] = nbt.TAG_Short(shape[1])
root_tag[Length] = nbt.TAG_Short(shape[2])
root_tag[Width] = nbt.TAG_Short(shape[0])
root_tag[Entities] = nbt.TAG_List()
root_tag[TileEntities] = nbt.TAG_List()
root_tag["Materials"] = nbt.TAG_String(self.materials.name)
root_tag[Blocks] = nbt.TAG_Byte_Array(zeros((shape[1], shape[2], shape[0]), uint8))
root_tag[Data] = nbt.TAG_Byte_Array(zeros((shape[1], shape[2], shape[0]), uint8))
self.root_tag = root_tag
self.dataIsPacked = True
示例4: get_worlds
def get_worlds():
"Returns {world # or name : level.dat information}"
ret = {}
save_dir = get_save_dir()
# No dirs found - most likely not running from inside minecraft-dir
if save_dir is None:
return None
for dir in os.listdir(save_dir):
world_dat = os.path.join(save_dir, dir, "level.dat")
if not os.path.exists(world_dat):
continue
info = nbt.load(world_dat)[1]
info["Data"]["path"] = os.path.join(save_dir, dir)
if dir.startswith("World") and len(dir) == 6:
try:
world_n = int(dir[-1])
ret[world_n] = info["Data"]
except ValueError:
pass
if "LevelName" in info["Data"].keys():
ret[info["Data"]["LevelName"]] = info["Data"]
return ret
示例5: findTrueSpawn
def findTrueSpawn(self):
"""Adds the true spawn location to self.POI. The spawn Y coordinate
is almost always the default of 64. Find the first air block above
that point for the true spawn location"""
## read spawn info from level.dat
data = nbt.load(os.path.join(self.worlddir, "level.dat"))[1]
spawnX = data["Data"]["SpawnX"]
spawnY = data["Data"]["SpawnY"]
spawnZ = data["Data"]["SpawnZ"]
## The chunk that holds the spawn location
chunkX = spawnX / 16
chunkY = spawnZ / 16
## The filename of this chunk
chunkFile = self.get_region_path(chunkX, chunkY)
data = nbt.load_from_region(chunkFile, chunkX, chunkY)[1]
level = data["Level"]
blockArray = numpy.frombuffer(level["Blocks"], dtype=numpy.uint8).reshape((16, 16, 128))
## The block for spawn *within* the chunk
inChunkX = spawnX - (chunkX * 16)
inChunkZ = spawnZ - (chunkY * 16)
## find the first air block
while blockArray[inChunkX, inChunkZ, spawnY] != 0:
spawnY += 1
if spawnY == 128:
break
self.POI.append(dict(x=spawnX, y=spawnY, z=spawnZ, msg="Spawn", type="spawn", chunk=(inChunkX, inChunkZ)))
self.spawn = (spawnX, spawnY, spawnZ)
示例6: testErrors
def testErrors(self):
"""
attempt to name elements of a TAG_List
named list elements are not allowed by the NBT spec,
so we must discard any names when writing a list.
"""
level = self.testCreate()
level["Map"]["Spawn"][0].name = "Torg Potter"
data = level.save()
newlevel = nbt.load(buf=data)
n = newlevel["Map"]["Spawn"][0].name
if n:
print "Named list element failed: %s" % n
# attempt to delete non-existent TAG_Compound elements
# this generates a KeyError like a python dict does.
level = self.testCreate()
try:
del level["DEADBEEF"]
except KeyError:
pass
else:
assert False
示例7: saveGeneratedChunk
def saveGeneratedChunk(self, cx, cz, tempChunkBytes):
"""
Chunks get generated using Anvil generation. This is a (slow) way of importing anvil chunk bytes
and converting them to MCPE chunk data. Could definitely use some improvements, but at least it works.
:param cx, cx: Coordinates of the chunk
:param tempChunkBytes: str. Raw MCRegion chunk data.
:return:
"""
loaded_data = nbt.load(buf=tempChunkBytes)
class fake:
def __init__(self):
self.Height = 128
tempChunk = AnvilChunkData(fake(), (0, 0), loaded_data)
if not self.containsChunk(cx, cz):
self.createChunk(cx, cz)
chunk = self.getChunk(cx, cz)
chunk.Blocks = numpy.array(tempChunk.Blocks, dtype='uint16')
chunk.Data = numpy.array(tempChunk.Data, dtype='uint8')
chunk.SkyLight = numpy.array(tempChunk.SkyLight, dtype='uint8')
chunk.BlockLight = numpy.array(tempChunk.BlockLight, dtype='uint8')
chunk.dirty = True
self.worldFile.saveChunk(chunk)
else:
logger.info("Tried to import generated chunk at %s, %s but the chunk already existed." % cx, cz)
示例8: __init__
def __init__(self, filename, create=False):
self.zipfilename = filename
tempdir = tempfile.mktemp("schematic")
if create is False:
zf = zipfile.ZipFile(filename)
zf.extractall(tempdir)
zf.close()
super(ZipSchematic, self).__init__(tempdir, create)
atexit.register(shutil.rmtree, self.worldFolder.filename, True)
try:
schematicDat = nbt.load(self.worldFolder.getFilePath("schematic.dat"))
self.Width = schematicDat['Width'].value
self.Height = schematicDat['Height'].value
self.Length = schematicDat['Length'].value
if "Materials" in schematicDat:
self.materials = namedMaterials[schematicDat["Materials"].value]
except Exception, e:
print "Exception reading schematic.dat, skipping: {0!r}".format(e)
self.Width = 0
self.Length = 0
示例9: testBigEndianIntHeightMap
def testBigEndianIntHeightMap(self):
""" Test modifying, saving, and loading the new TAG_Int_Array heightmap
added with the Anvil format.
"""
chunk = nbt.load("testfiles/AnvilChunk.dat")
hm = chunk["Level"]["HeightMap"]
hm.value[2] = 500
oldhm = numpy.array(hm.value)
filename = mktemp("ChangedChunk")
chunk.save(filename)
changedChunk = nbt.load(filename)
os.unlink(filename)
eq = (changedChunk["Level"]["HeightMap"].value == oldhm)
assert eq.all()
示例10: testSpeed
def testSpeed(self):
d = join("testfiles", "TileTicks_chunks")
files = [join(d, f) for f in os.listdir(d)]
startTime = time.time()
for i in range(20):
for f in files[:40]:
n = nbt.load(f)
print "Duration: ", time.time() - startTime
示例11: repair
def repair(self):
lostAndFound = {}
_freeSectors = [True] * len(self.freeSectors)
_freeSectors[0] = _freeSectors[1] = False
deleted = 0
recovered = 0
log.info("Beginning repairs on {file} ({chunks} chunks)".format(file=os.path.basename(self.path), chunks=sum(self.offsets > 0)))
rx, rz = self.regionCoords
for index, offset in enumerate(self.offsets):
if offset:
cx = index & 0x1f
cz = index >> 5
cx += rx << 5
cz += rz << 5
sectorStart = offset >> 8
sectorCount = offset & 0xff
try:
if sectorStart + sectorCount > len(self.freeSectors):
raise RegionMalformed("Offset {start}:{end} ({offset}) at index {index} pointed outside of the file".format(
start=sectorStart, end=sectorStart + sectorCount, index=index, offset=offset))
data = self.readChunk(cx, cz)
if data is None:
raise RegionMalformed("Failed to read chunk data for {0}".format((cx, cz)))
chunkTag = nbt.load(buf=data)
lev = chunkTag["Level"]
xPos = lev["xPos"].value
zPos = lev["zPos"].value
overlaps = False
for i in range(sectorStart, sectorStart + sectorCount):
if _freeSectors[i] is False:
overlaps = True
_freeSectors[i] = False
if xPos != cx or zPos != cz or overlaps:
lostAndFound[xPos, zPos] = data
if (xPos, zPos) != (cx, cz):
raise RegionMalformed("Chunk {found} was found in the slot reserved for {expected}".format(found=(xPos, zPos), expected=(cx, cz)))
else:
raise RegionMalformed("Chunk {found} (in slot {expected}) has overlapping sectors with another chunk!".format(found=(xPos, zPos), expected=(cx, cz)))
except Exception as e:
log.info("Unexpected chunk data at sector {sector} ({exc})".format(sector=sectorStart, exc=e))
self.setOffset(cx, cz, 0)
deleted += 1
for cPos, foundData in lostAndFound.items():
cx, cz = cPos
if self.getOffset(cx, cz) == 0:
log.info("Found chunk {found} and its slot is empty, recovering it".format(found=cPos))
self.saveChunk(cx, cz, foundData)
recovered += 1
log.info("Repair complete. Removed {0} chunks, recovered {1} chunks, net {2}".format(deleted, recovered, recovered - deleted))
示例12: testSpeed
def testSpeed(self):
d = join("testfiles", "TileTicks_chunks")
files = [join(d, f) for f in os.listdir(d)]
startTime = time.time()
for f in files[:40]:
n = nbt.load(f)
duration = time.time() - startTime
assert duration < 1.0 # Will fail when not using _nbt.pyx
示例13: _loadLevelDat
def _loadLevelDat(filename):
root_tag_buf = open(filename, 'rb').read()
magic, length, root_tag_buf = root_tag_buf[:4], root_tag_buf[4:8], root_tag_buf[8:]
if struct.Struct('<i').unpack(magic)[0] < 3:
logger.info("Found an old level.dat file. Aborting world load")
raise InvalidPocketLevelDBWorldException() # Maybe try convert/load old PE world?
if len(root_tag_buf) != struct.Struct('<i').unpack(length)[0]:
raise nbt.NBTFormatError()
self.root_tag = nbt.load(buf=root_tag_buf)
示例14: find_true_spawn
def find_true_spawn(self):
"""Adds the true spawn location to self.POI. The spawn Y coordinate
is almost always the default of 64. Find the first air block above
that point for the true spawn location"""
## read spawn info from level.dat
data = nbt.load(os.path.join(self.worlddir, "level.dat"))[1]
disp_spawnX = spawnX = data['Data']['SpawnX']
spawnY = data['Data']['SpawnY']
disp_spawnZ = spawnZ = data['Data']['SpawnZ']
if self.north_direction == 'upper-left':
temp = spawnX
spawnX = -spawnZ
spawnZ = temp
elif self.north_direction == 'upper-right':
spawnX = -spawnX
spawnZ = -spawnZ
elif self.north_direction == 'lower-right':
temp = spawnX
spawnX = spawnZ
spawnZ = -temp
## The chunk that holds the spawn location
chunkX = spawnX/16
chunkY = spawnZ/16
## clamp spawnY to a sane value, in-chunk value
if spawnY < 0:
spawnY = 0
if spawnY > 127:
spawnY = 127
try:
## The filename of this chunk
chunkFile = self.get_region_path(chunkX, chunkY)
if chunkFile is not None:
data = nbt.load_from_region(chunkFile, chunkX, chunkY, self.north_direction)
if data is not None:
level = data[1]['Level']
blockArray = numpy.frombuffer(level['Blocks'], dtype=numpy.uint8).reshape((16,16,128))
## The block for spawn *within* the chunk
inChunkX = spawnX - (chunkX*16)
inChunkZ = spawnZ - (chunkY*16)
## find the first air block
while (blockArray[inChunkX, inChunkZ, spawnY] != 0):
spawnY += 1
if spawnY == 128:
break
except chunk.ChunkCorrupt:
#ignore corrupt spawn, and continue
pass
self.POI.append( dict(x=disp_spawnX, y=spawnY, z=disp_spawnZ,
msg="Spawn", type="spawn", chunk=(chunkX, chunkY)))
self.spawn = (disp_spawnX, spawnY, disp_spawnZ)
示例15: addSpawn
def addSpawn(self):
"""Adds the true spawn location to self.POI."""
## read spawn info from level.dat
data = nbt.load(os.path.join(self.worlddir, "level.dat"))[1]
spawnX = data["Data"]["SpawnX"]
spawnY = data["Data"]["SpawnY"]
spawnZ = data["Data"]["SpawnZ"]
self.POI.append(dict(x=spawnX, y=spawnY, z=spawnZ, msg="Spawn", id=0))