def readChunk(self, cx, cz): cx &= 0x1f cz &= 0x1f offset = self.getOffset(cx, cz) if offset == 0: return None sectorStart = offset >> 8 numSectors = offset & 0xff if numSectors == 0: return None if sectorStart + numSectors > len(self.freeSectors): return None with self.file as f: f.seek(sectorStart * self.SECTOR_BYTES) data = f.read(numSectors * self.SECTOR_BYTES) assert(len(data) > 0) # log.debug("REGION LOAD {0},{1} sector {2}".format(cx, cz, sectorStart)) length = struct.unpack_from(">I", data)[0] format = struct.unpack_from("B", data, 4)[0] data = data[5:length + 5] if format == self.VERSION_GZIP: return nbt.gunzip(data) if format == self.VERSION_DEFLATE: return inflate(data) raise IOError("Unknown compress format: {0}".format(format))
def fromFile(filename, loadInfinite=True, readonly=False): ''' The preferred method for loading Minecraft levels of any type. pass False to loadInfinite if you'd rather not load infdev levels. ''' log.info(u"Identifying " + filename) if not filename: raise IOError("File not found: " + filename) if not os.path.exists(filename): raise IOError("File not found: " + filename) if ZipSchematic._isLevel(filename): log.info("Zipfile found, attempting zipped infinite level") lev = ZipSchematic(filename) log.info("Detected zipped Infdev level") return lev if PocketWorld._isLevel(filename): return PocketWorld(filename) if MCInfdevOldLevel._isLevel(filename): log.info(u"Detected Infdev level.dat") if loadInfinite: return MCInfdevOldLevel(filename=filename, readonly=readonly) else: raise ValueError("Asked to load {0} which is an infinite level, loadInfinite was False".format( os.path.basename(filename))) if NewPocketWorld._isLevel(filename): raise ValueError("Don't have Pocket Edition 0.9+ support yet!") if os.path.isdir(filename): raise ValueError("Folder {0} was not identified as a Minecraft level.".format(os.path.basename(filename))) f = file(filename, 'rb') rawdata = f.read() f.close() if len(rawdata) < 4: raise ValueError("{0} is too small! ({1}) ".format(filename, len(rawdata))) data = fromstring(rawdata, dtype='uint8') if not data.any(): raise ValueError("{0} contains only zeroes. This file is damaged beyond repair.") if MCJavaLevel._isDataLevel(data): log.info(u"Detected Java-style level") lev = MCJavaLevel(filename, data) lev.compressed = False return lev # ungzdata = None compressed = True unzippedData = None try: unzippedData = nbt.gunzip(rawdata) except Exception, e: log.info(u"Exception during Gzip operation, assuming {0} uncompressed: {1!r}".format(filename, e)) if unzippedData is None: compressed = False unzippedData = rawdata
def readChunk(self, cx, cz): data, format = self._readChunk(cx, cz) if format == self.VERSION_GZIP: return nbt.gunzip(data) if format == self.VERSION_DEFLATE: return inflate(data) raise IOError("Unknown compress format: {0}".format(format))
def fromFile(filename, loadInfinite=True, readonly=False): ''' The preferred method for loading Minecraft levels of any type. pass False to loadInfinite if you'd rather not load infdev levels. ''' log.info(u"Identifying " + filename) if not filename: raise IOError("File not found: " + filename) if not os.path.exists(filename): raise IOError("File not found: " + filename) if ZipSchematic._isLevel(filename): log.info("Zipfile found, attempting zipped infinite level") lev = ZipSchematic(filename) log.info("Detected zipped Infdev level") return lev if PocketWorld._isLevel(filename): return PocketWorld(filename) if MCInfdevOldLevel._isLevel(filename): log.info(u"Detected Infdev level.dat") if loadInfinite: return MCInfdevOldLevel(filename=filename, readonly=readonly) else: raise ValueError("Asked to load {0} which is an infinite level, loadInfinite was False".format( os.path.basename(filename))) if os.path.isdir(filename): raise ValueError("Folder {0} was not identified as a Minecraft level.".format(os.path.basename(filename))) f = file(filename, 'rb') rawdata = f.read() f.close() if len(rawdata) < 4: raise ValueError("{0} is too small! ({1}) ".format(filename, len(rawdata))) data = fromstring(rawdata, dtype='uint8') if not data.any(): raise ValueError("{0} contains only zeroes. This file is damaged beyond repair.") if MCJavaLevel._isDataLevel(data): log.info(u"Detected Java-style level") lev = MCJavaLevel(filename, data) lev.compressed = False return lev # ungzdata = None compressed = True unzippedData = None try: unzippedData = nbt.gunzip(rawdata) except Exception, e: log.info(u"Exception during Gzip operation, assuming {0} uncompressed: {1!r}".format(filename, e)) if unzippedData is None: compressed = False unzippedData = rawdata
def _getChunkFromFile(self, cx, cz): fp = open(self._path, 'rb+') cx &= 0x1f cz &= 0x1f chunk_offset = self.getOffset(cx, cz) if chunk_offset == 0: #print('Chunk does not exist') return sector_start = chunk_offset >> 8 sector_nums = chunk_offset & 0xff if sector_nums == 0: #print('Chunk does not exist') return if sector_start + sector_nums > len(self._free_sectors): #print('Chunk does not exist') return fp.seek(sector_start * SECTOR_BYTES) data = fp.read(sector_nums * SECTOR_BYTES) if len(data) < 5: print('Chunk/Sector is malformed') return length = struct.unpack_from('>I', data)[0] _format = struct.unpack_from('B', data, 4)[0] data = data[5:length + 5] readable_data = None if _format == VERSION_GZIP: readable_data = nbt.gunzip(data) # print 'Chunk is in GZIP format' if _format == VERSION_DEFLATE: # print 'Chunk is in DEFLATE format' readable_data = zlib.decompress(data) fp.close() return BlockstateChunk(self.world, self, nbt.load(buf=readable_data))
def fromFile(filename, loadInfinite=True, readonly=False): ''' The preferred method for loading Minecraft levels of any type. pass False to loadInfinite if you'd rather not load infdev levels. ''' log.info(u"Identifying " + filename) if not filename: raise IOError("File not found: " + filename) if not os.path.exists(filename): raise IOError("File not found: " + filename) if ZipSchematic._isLevel(filename): log.info("Zipfile found, attempting zipped infinite level") lev = ZipSchematic(filename) log.info("Detected zipped Infdev level") return lev if PocketWorld._isLevel(filename): return PocketWorld(filename) if MCInfdevOldLevel._isLevel(filename): log.info(u"Detected Infdev level.dat") if loadInfinite: return MCInfdevOldLevel(filename=filename, readonly=readonly) else: raise ValueError("Asked to load {0} which is an infinite level, loadInfinite was False".format( os.path.basename(filename))) if PocketLeveldbWorld._isLevel(filename): if leveldbpocket.leveldb_available: return PocketLeveldbWorld(filename) else: logging.exception("Pocket support has failed") if os.path.isdir(filename): logging.exception("World load failed, trying to open a directory instead of a file") f = file(filename, 'rb') rawdata = f.read() f.close() if len(rawdata) < 4: raise ValueError("{0} is too small! ({1}) ".format(filename, len(rawdata))) data = fromstring(rawdata, dtype='uint8') if not data.any(): raise ValueError("{0} contains only zeroes. This file is damaged beyond repair.") if MCJavaLevel._isDataLevel(data): log.info(u"Detected Java-style level") lev = MCJavaLevel(filename, data) lev.compressed = False return lev # ungzdata = None compressed = True unzippedData = None try: unzippedData = nbt.gunzip(rawdata) except Exception as e: log.info(u"Exception during Gzip operation, assuming {0} uncompressed: {1!r}".format(filename, e)) if unzippedData is None: compressed = False unzippedData = rawdata #data = data = unzippedData if MCJavaLevel._isDataLevel(data): log.info(u"Detected compressed Java-style level") lev = MCJavaLevel(filename, data) lev.compressed = compressed return lev try: root_tag = nbt.load(buf=data) except Exception as e: log.info(u"Error during NBT load: {0!r}".format(e)) log.info(traceback.format_exc()) log.info(u"Fallback: Detected compressed flat block array, yzx ordered ") try: lev = MCJavaLevel(filename, data) lev.compressed = compressed return lev except Exception as e2: raise LoadingError(("Multiple errors encountered", e, e2), sys.exc_info()[2]) else: if MCIndevLevel._isTagLevel(root_tag): log.info(u"Detected Indev .mclevel") return MCIndevLevel(root_tag, filename) if MCSchematic._isTagLevel(root_tag): log.info(u"Detected Schematic.") return MCSchematic(filename=filename) if INVEditChest._isTagLevel(root_tag): log.info(u"Detected INVEdit inventory file") return INVEditChest(root_tag=root_tag, filename=filename) raise IOError("Cannot detect file type.")
def fromFile(filename, loadInfinite=True, readonly=False): ''' The preferred method for loading Minecraft levels of any type. pass False to loadInfinite if you'd rather not load infdev levels. ''' log.info(u"Identifying " + filename) if not filename: raise IOError("File not found: " + filename) if not os.path.exists(filename): raise IOError("File not found: " + filename) if ZipSchematic._isLevel(filename): log.info("Zipfile found, attempting zipped infinite level") lev = ZipSchematic(filename) log.info("Detected zipped Infdev level") return lev if PocketWorld._isLevel(filename): return PocketWorld(filename) if MCInfdevOldLevel._isLevel(filename): log.info(u"Detected Infdev level.dat") if loadInfinite: return MCInfdevOldLevel(filename=filename, readonly=readonly) else: raise ValueError( "Asked to load {0} which is an infinite level, loadInfinite was False" .format(os.path.basename(filename))) if PocketLeveldbWorld._isLevel(filename): if leveldbpocket.leveldb_available: return PocketLeveldbWorld(filename) else: logging.exception("Pocket support has failed") if os.path.isdir(filename): logging.exception( "World load failed, trying to open a directory instead of a file") f = file(filename, 'rb') rawdata = f.read() f.close() if len(rawdata) < 4: raise ValueError("{0} is too small! ({1}) ".format( filename, len(rawdata))) data = fromstring(rawdata, dtype='uint8') if not data.any(): raise ValueError( "{0} contains only zeroes. This file is damaged beyond repair.") if MCJavaLevel._isDataLevel(data): log.info(u"Detected Java-style level") lev = MCJavaLevel(filename, data) lev.compressed = False return lev # ungzdata = None compressed = True unzippedData = None try: unzippedData = nbt.gunzip(rawdata) except Exception as e: log.info( u"Exception during Gzip operation, assuming {0} uncompressed: {1!r}" .format(filename, e)) if unzippedData is None: compressed = False unzippedData = rawdata #data = data = unzippedData if MCJavaLevel._isDataLevel(data): log.info(u"Detected compressed Java-style level") lev = MCJavaLevel(filename, data) lev.compressed = compressed return lev try: root_tag = nbt.load(buf=data) except Exception as e: log.info(u"Error during NBT load: {0!r}".format(e)) log.info(traceback.format_exc()) log.info( u"Fallback: Detected compressed flat block array, yzx ordered ") try: lev = MCJavaLevel(filename, data) lev.compressed = compressed return lev except Exception as e2: raise LoadingError(("Multiple errors encountered", e, e2), sys.exc_info()[2]) else: if MCIndevLevel._isTagLevel(root_tag): log.info(u"Detected Indev .mclevel") return MCIndevLevel(root_tag, filename) if MCSchematic._isTagLevel(root_tag): log.info(u"Detected Schematic.") return MCSchematic(filename=filename) if INVEditChest._isTagLevel(root_tag): log.info(u"Detected INVEdit inventory file") return INVEditChest(root_tag=root_tag, filename=filename) raise IOError("Cannot detect file type.")