def testCopyChunks(self): level = self.anvilLevel.level temppath = mktemp("AnvilCreate") newLevel = MCInfdevOldLevel(filename=temppath, create=True) for cx, cz in level.allChunks: newLevel.copyChunkFrom(level, cx, cz) newLevel.close() shutil.rmtree(temppath)
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 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 extractZipSchematicFromIter(sourceLevel, box, zipfilename=None, entities=True): # converts classic blocks to alpha # probably should only apply to alpha levels if zipfilename is None: zipfilename = tempfile.mktemp("zipschematic") p = sourceLevel.adjustExtractionParameters(box) if p is None: return sourceBox, destPoint = p destPoint = (0, 0, 0) tempfolder = tempfile.mktemp("schematic") try: tempSchematic = MCInfdevOldLevel(tempfolder, create=True) tempSchematic.materials = sourceLevel.materials for i in tempSchematic.copyBlocksFromIter(sourceLevel, sourceBox, destPoint, entities=entities, create=True): yield i tempSchematic.saveInPlace() # lights not needed for this format - crashes minecraft though schematicDat = nbt.TAG_Compound() schematicDat.name = "Mega Schematic" schematicDat["Width"] = nbt.TAG_Int(sourceBox.size[0]) schematicDat["Height"] = nbt.TAG_Int(sourceBox.size[1]) schematicDat["Length"] = nbt.TAG_Int(sourceBox.size[2]) schematicDat["Materials"] = nbt.TAG_String(tempSchematic.materials.name) schematicDat.save(os.path.join(tempfolder, "schematic.dat")) zipdir(tempfolder, zipfilename) import mclevel yield mclevel.fromFile(zipfilename) finally: # We get here if the generator is GCed also if os.path.exists(tempfolder): shutil.rmtree(tempfolder, False)
def manmade_relight(): t = templevel.TempLevel("TimeRelight", createFunc=lambda f:MCInfdevOldLevel(f, create=True)) world = t.level station = mclevel.fromFile("testfiles/station.schematic") times = 2 for x in range(times): for z in range(times): world.copyBlocksFrom(station, station.bounds, (x * station.Width, 63, z * station.Length), create=True) t = timeit(lambda: world.generateLights(world.allChunks), number=1) print "Relight manmade building: %d chunks in %.02f seconds (%.02fms per chunk)" % (world.chunkCount, t, t / world.chunkCount * 1000)
def extractZipSchematicFromIter(sourceLevel, box, zipfilename=None, entities=True): # converts classic blocks to alpha # probably should only apply to alpha levels if zipfilename is None: zipfilename = tempfile.mktemp("zipschematic") p = sourceLevel.adjustExtractionParameters(box) if p is None: return sourceBox, destPoint = p destPoint = (0, 0, 0) tempfolder = tempfile.mktemp("schematic") try: tempSchematic = MCInfdevOldLevel(tempfolder, create=True) tempSchematic.materials = sourceLevel.materials for i in tempSchematic.copyBlocksFromIter(sourceLevel, sourceBox, destPoint, entities=entities, create=True): yield i tempSchematic.saveInPlace( ) # lights not needed for this format - crashes minecraft though schematicDat = nbt.TAG_Compound() schematicDat.name = "Mega Schematic" schematicDat["Width"] = nbt.TAG_Int(sourceBox.size[0]) schematicDat["Height"] = nbt.TAG_Int(sourceBox.size[1]) schematicDat["Length"] = nbt.TAG_Int(sourceBox.size[2]) schematicDat["Materials"] = nbt.TAG_String( tempSchematic.materials.name) schematicDat.save(os.path.join(tempfolder, "schematic.dat")) zipdir(tempfolder, zipfilename) import mclevel yield mclevel.fromFile(zipfilename) finally: # We get here if the generator is GCed also if os.path.exists(tempfolder): shutil.rmtree(tempfolder, False)
def testCreate(self): temppath = mktemp("AlphaCreate") self.alphaLevel = MCInfdevOldLevel(filename=temppath, create=True) self.alphaLevel.close() shutil.rmtree(temppath)
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.")
def testCreate(self): temppath = mktemp("AnvilCreate") self.anvilLevel = MCInfdevOldLevel(filename=temppath, create=True) self.anvilLevel.close() shutil.rmtree(temppath)
class TestAnvilLevelCreate(unittest.TestCase): def testCreate(self): temppath = mktemp("AnvilCreate") self.anvilLevel = MCInfdevOldLevel(filename=temppath, create=True) self.anvilLevel.close() shutil.rmtree(temppath)
class TestAlphaLevelCreate(unittest.TestCase): def testCreate(self): temppath = mktemp("AlphaCreate") self.alphaLevel = MCInfdevOldLevel(filename=temppath, create=True) self.alphaLevel.close() shutil.rmtree(temppath)