def testImportSchematic(self): level = self.anvilLevel.level cx, cz = level.allChunks.next() schem = mclevel.fromFile("schematics/CreativeInABox.schematic") box = BoundingBox((cx * 16, 64, cz * 16), schem.bounds.size) level.copyBlocksFrom(schem, schem.bounds, (0, 64, 0)) schem = MCSchematic(shape=schem.bounds.size) schem.copyBlocksFrom(level, box, (0, 0, 0)) convertedSourceBlocks, convertedSourceData = block_copy.convertBlocks(schem, level, schem.Blocks, schem.Data) assert (level.getChunk(cx, cz).Blocks[0:1, 0:3, 64:65] == convertedSourceBlocks).all()
def testImportSchematic(self): level = self.alphalevel.level cx, cz = level.allChunks.next() schem = mclevel.fromFile("schematics/CreativeInABox.schematic") box = BoundingBox((cx * 16, 64, cz * 16), schem.bounds.size) level.copyBlocksFrom(schem, schem.bounds, (0, 64, 0)) schem = MCSchematic(shape=schem.bounds.size) schem.copyBlocksFrom(level, box, (0, 0, 0)) convertedSourceBlocks, convertedSourceData = schem.convertBlocksFromLevel( level, schem.Blocks, schem.Data) assert (level.getChunk( cx, cz).Blocks[0:1, 0:3, 64:65] == convertedSourceBlocks).all()
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, 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 testCreate(self): # info("Schematic from indev") size = (64, 64, 64) temp = mktemp("testcreate.schematic") schematic = MCSchematic(shape=size, filename=temp, mats='Classic') level = self.indevlevel.level self.failUnlessRaises(ValueError, lambda: ( schematic.copyBlocksFrom(level, BoundingBox((-32, -32, -32), (64, 64, 64,)), (0, 0, 0)) )) schematic.copyBlocksFrom(level, BoundingBox((0, 0, 0), (64, 64, 64,)), (0, 0, 0)) assert((schematic.Blocks[0:64, 0:64, 0:64] == level.Blocks[0:64, 0:64, 0:64]).all()) schematic.compress() schematic.copyBlocksFrom(level, BoundingBox((0, 0, 0), (64, 64, 64,)), (-32, -32, -32)) assert((schematic.Blocks[0:32, 0:32, 0:32] == level.Blocks[32:64, 32:64, 32:64]).all()) schematic.compress() schematic.saveInPlace() schem = mclevel.fromFile("schematics/CreativeInABox.schematic") tempSchematic = MCSchematic(shape=(1, 1, 3)) tempSchematic.copyBlocksFrom(schem, BoundingBox((0, 0, 0), (1, 1, 3)), (0, 0, 0)) level = self.alphalevel.level for cx, cz in itertools.product(xrange(0, 4), xrange(0, 4)): try: level.createChunk(cx, cz) except ValueError: pass schematic.copyBlocksFrom(level, BoundingBox((0, 0, 0), (64, 64, 64,)), (0, 0, 0)) schematic.close() os.remove(temp)
def testCreate(self): # info("Schematic from indev") size = (64, 64, 64) temp = mktemp("testcreate.schematic") schematic = MCSchematic(shape=size, filename=temp, mats='Classic') level = self.indevlevel.level self.failUnlessRaises( ValueError, lambda: (schematic.copyBlocksFrom( level, BoundingBox((-32, -32, -32), ( 64, 64, 64, )), (0, 0, 0)))) schematic.copyBlocksFrom(level, BoundingBox((0, 0, 0), ( 64, 64, 64, )), (0, 0, 0)) assert ((schematic.Blocks[0:64, 0:64, 0:64] == level.Blocks[0:64, 0:64, 0:64]).all()) schematic.compress() schematic.copyBlocksFrom(level, BoundingBox((0, 0, 0), ( 64, 64, 64, )), (-32, -32, -32)) assert ((schematic.Blocks[0:32, 0:32, 0:32] == level.Blocks[32:64, 32:64, 32:64]).all()) schematic.compress() schematic.saveInPlace() schem = mclevel.fromFile("schematics/CreativeInABox.schematic") tempSchematic = MCSchematic(shape=(1, 1, 3)) tempSchematic.copyBlocksFrom(schem, BoundingBox((0, 0, 0), (1, 1, 3)), (0, 0, 0)) info("Schematic from alpha") level = self.alphalevel.level for cx, cz in itertools.product(xrange(0, 4), xrange(0, 4)): try: level.createChunk(cx, cz) except ValueError: pass schematic.copyBlocksFrom(level, BoundingBox((0, 0, 0), ( 64, 64, 64, )), (0, 0, 0)) schematic.close() os.remove(temp)
except Exception, 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, 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 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 test_schematic_extended_ids(): s = MCSchematic(shape=(3, 2, 2)) s.Blocks[0,0,0] = 2048 assert s.Blocks[0,0,0] == 2048