lev = MCJavaLevel(filename, data) lev.compressed = compressed return lev except Exception, 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 loadWorld(name): filename = os.path.join(minecraftSaveFileDir, name) return fromFile(filename) def loadWorldNumber(i): # deprecated filename = u"{0}{1}{2}{3}{1}".format(minecraftSaveFileDir, os.sep, u"World", i)
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.")
try: lev = MCJavaLevel(filename, data) lev.compressed = compressed return lev except Exception, 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(root_tag=root_tag, 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 loadWorld(name): filename = os.path.join(minecraftSaveFileDir, name) return fromFile(filename) def loadWorldNumber(i): # deprecated filename = u"{0}{1}{2}{3}{1}".format(minecraftSaveFileDir, os.sep, u"World", i) return fromFile(filename)
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.")