def scan_data(scanned_dat_file): """ Try to parse the nbd data file, and fill the scanned object. If something is wrong it will return a tuple with useful info to debug the problem. NOTE: idcounts.dat (number of map files) is a nbt file and is not compressed, we handle the special case here. """ s = scanned_dat_file try: if s.filename == 'idcounts.dat': # TODO: This is ugly # Open the file and create a buffer, this way # NBT won't try to de-gzip the file f = open(s.path) _ = nbt.NBTFile(buffer=f) else: _ = nbt.NBTFile(filename=s.path) s.readable = True except MalformedFileError as e: s.readable = False s.status_text = str(e) except IOError as e: s.readable = False s.status_text = str(e) except: s.readable = False except_type, except_class, tb = sys.exc_info() s = (s, (except_type, except_class, extract_tb(tb))) return s
def analyse(self, data_file): try: if data_file.filename == "idcounts.dat": if util.is_gz_file(data_file.path): dataFile = nbt.NBTFile(filename=data_file.path) else: f = open(data_file.path) dataFile = nbt.NBTFile(buffer=f) else: dataFile = nbt.NBTFile(filename=data_file.path) assert dataFile except MalformedFileError: data_file.scan_results.append(models.DATA_MALFORMED_FILE_ERROR) except IOError: data_file.scan_results.append(models.DATA_IO_ERROR) except Exception as e: print(e) print(data_file.filename) data_file.scan_results.append(models.DATA_UNEXPECTED) data_file.scan_time = time.time() data_file.scanned = True return data_file
def generate_schematic(data, layout, blank_name, name, low_state, high_state) -> None: blankschem = nbt.NBTFile(blank_name, "rb") nbtfile = nbt.NBTFile() nbtfile.name = "Schematic" nbtfile, low_id, high_id = generate_palette(nbtfile, blankschem, low_state, high_state) Width, Lenght, Height = get_dimensions(blankschem) nbtfile.tags.extend([ nbt.TAG_Int(name="DataVersion", value=blankschem["DataVersion"].value), nbt.TAG_Int(name="Version", value=blankschem["Version"].value), nbt.TAG_Short(name="Length", value=Lenght), nbt.TAG_Short(name="Height", value=Height), nbt.TAG_Short(name="Width", value=Width), nbt.TAG_Int_Array(name="Offset"), ]) nbtfile = generate_meta(nbtfile, blankschem) nbtfile.tags.append(nbt.TAG_Byte_Array(name="BlockData")) nbtfile["BlockData"].value = generate_block_data(data, layout, blankschem, high_id, low_id) nbtfile.write_file(name)
def clean_level() -> None: level = nbt.NBTFile(str(GAME_FOLDER / "level.dat"), "rb") if "Player" in level["Data"]: del level["Data"]["Player"] level.write_file()
def update_server_address(self, ip, name='AWScraft'): ''' Updates the server named <name> with new IP address <ip> in servers.dat. Otherwise creates a new server entry named <name> with <ip> ''' server_file = os.path.join(self.data_directory, 'servers.dat') if not os.path.isfile(server_file): return False # Check Server File Exists with open(server_file, 'rb') as binary: server = nbt.NBTFile(buffer=binary) updated = False for entry in server['servers']: if entry['name'] == name: entry['ip'] = ip updated = True if not updated: new_server = nbt.TAG_Compound() new_server.tags.append(nbt.TAG_String(name='name', value=name)) new_server.tags.append(nbt.TAG_String(name='ip', value=ip)) server['servers'].append(new_server) os.remove(server_file) with open(server_file, 'wb') as new_binary: server.write_file(buffer=new_binary) return True
def read_level(levelfile): logger = logging.getLogger('ping') t = dt = raining = thundering = None try: n = nbt.NBTFile(levelfile) if n is not None: try: t = n[0]["Time"].value t /= 24000 except: t = None try: dt = n[0]["DayTime"].value dt %= 24000 except: dt = None try: raining = bool(n[0]["raining"].value) except: raining = None try: thundering = bool(n[0]["thundering"].value) except: thundering = None except Exception, e: logger.error(e)
def load(cls, directory: str, definitions: str) -> World: wrapper = cls(directory, definitions) fp = open(path.join(directory, "level.dat"), "rb") root_tag = nbt.NBTFile(fileobj=fp) fp.close() return World(directory, root_tag, wrapper)
def load_nbt(self, file=""): self.file = file nbt_file = nbt.NBTFile(file, "rb") first = self.widget_treeview.insert(parent="", index="end", iid=nbt_file.name, text=nbt_file.name, tags="Bold") self.widget_treeview.item(first, open=True) parent = nbt_file.name for tag in nbt_file.tags: # print(tag.name, tag.valuestr(), tag.__class__.__name__) self.widget_treeview.insert(parent=parent, index="end", iid=tag.name, text=tag.name, values=[tag.valuestr(), tag.__class__.__name__], tags="Bold" if tag.__class__.__name__ in ["TAG_Compound", "TAG_List"] else "") if tag.__class__.__name__ in ["TAG_Compound", "TAG_List"]: parent = tag.name for compound_tag in nbt_file[tag.name].tags: self.load_nested(parent, nbt_file, compound_tag, tag) parent = nbt_file.name self.title("Feather - {}".format(file))
def nbt2hex(item): file = nbt.NBTFile() file.name = 'data' file.tags = item.tags out = cStringIO.StringIO() file.write_file(fileobj=out) return binascii.b2a_hex(bytearray(out.getvalue()))
def __missing__(self, key): try: value = nbt.NBTFile(key, "rb") self[key] = value return value except FileNotFoundError: logging.error(f"File [{key}] not found")
def chunk_data(self, chunk_x, chunk_z): """ Returns the NBT data for a chunk Parameters ---------- chunk_x Chunk's X value chunk_z Chunk's Z value Raises ------ anvil.GZipChunkData If the chunk's compression is gzip """ off = self.chunk_location(chunk_x, chunk_z) # (0, 0) means it hasn't generated yet, aka it doesn't exist yet if off == (0, 0): return off = off[0] * 4096 length = int.from_bytes(self.data[off:off + 4], byteorder="big") compression = self.data[off + 4] # 2 most of the time if compression == 1: raise GZipChunkData("GZip is not supported") compressed_data = self.data[off + 5:off + 5 + length - 1] return nbt.NBTFile(buffer=BytesIO(zlib.decompress(compressed_data)))
def get_done_auctions(num): while not queues[num].empty(): auc_uuid = queues[num].get() r = requests.get( f'https://api.hypixel.net/skyblock/auction?key=7e8355c8-a50b-4473-ba41-b03d0473a0d8&uuid={auc_uuid}' ).json() if r['success'] and len(r['auctions']) > 0: auction = r['auctions'][0] if round(time.time_ns() // 1000000 ) > auction['end'] and auction['highest_bid_amount'] > 0: item_name = auction['item_name'] amount_sold_for = auction['highest_bid_amount'] # decode inventory nbt_data = nbt.NBTFile(fileobj=BytesIO( base64.b64decode(auction['item_bytes']['data']))) item_count = nbt_data['i'][0]['Count'].value amount_per_item = amount_sold_for / item_count if item_name in current_run_data: current_run_data[item_name].append(amount_per_item) print(f'Added to {item_name} : {amount_per_item}') else: current_run_data[item_name] = [amount_per_item] print(f'Added a new item: {item_name}') auctions.remove(auc_uuid)
def template_village_file(tick): """ Creates a template villages.dat file that i can modify later on """ cat = nbt.NBTFile() cat2 = cat['data'] = nbt.TAG_Compound() cat2["Villages"] = nbt.TAG_List(Banana) cat2['Tick'] = nbt.TAG_Int(tick) return cat
def scan_data(scanned_dat_file): """ Try to parse the nbt data file, and fill the scanned object. Inputs: - scanned_dat_file -- ScannedDataFile object from world.py. If something is wrong it will return a tuple with useful info to debug the problem. NOTE: idcounts.dat (number of map files) is a nbt file and is not compressed, we handle the special case here. """ s = scanned_dat_file try: if s.filename == 'idcounts.dat': # TODO: This is ugly # Open the file and create a buffer, this way # NBT won't try to de-gzip the file f = open(s.path) _ = nbt.NBTFile(buffer=f) else: _ = nbt.NBTFile(filename=s.path) s.status = c.DATAFILE_OK except MalformedFileError: s.status = c.DATAFILE_UNREADABLE except IOError: s.status = c.DATAFILE_UNREADABLE except UnicodeDecodeError: s.status = c.DATAFILE_UNREADABLE except TypeError: s.status = c.DATAFILE_UNREADABLE except EOFError: # There is a compressed stream in the file but ends abruptly s.status = c.DATAFILE_UNREADABLE except: s.status = c.DATAFILE_UNREADABLE except_type, except_class, tb = sys.exc_info() s = (s, (except_type, except_class, extract_tb(tb))) return s
def __init__(self, schematic_file_path, max_size): nbtfile = nbt.NBTFile(schematic_file_path) self.width = nbtfile[u"Width"].value self.height = nbtfile[u"Height"].value self.depth = nbtfile[u"Length"].value self.check_size(max_size) self.blocks = nbtfile[u"Blocks"] self.data = nbtfile[u"Data"]
def saveEntities(self): for dim in self.dimensions: # FIXME: path = self.saveFolderPath() + '/entities.dat' nbtfile = nbt.NBTFile() nbtfile.name = 'Entities' # FIXME: nbtfile.tags.append(entity.toNbt(dim.entities)) nbtfile.write_file(path)
def __init__(self, world_folder, player_cache, stats_list, member_role): self.world_folder = world_folder self.player_cache = player_cache self.stats_list = stats_list self.member_role = member_role self.data_folder = os.path.join(world_folder, "data") self.stats_folder = os.path.join(world_folder, "stats") nbt_file = nbt.NBTFile(os.path.join(self.data_folder, "scoreboard.dat"))["data"] self.objectives = [objective['Name'].value for objective in nbt_file["Objectives"]] self.storage_command = storageCommand.storageCommand(self.world_folder)
def create_temp(file): """Copies and opens NBT file in read mode. Deletes it afterwards.""" temp_dir = tempfile.gettempdir() basename = os.path.basename(file) temp_path = os.path.join(temp_dir, "{}.copy".format(basename)) shutil.copyfile(file, temp_path) try: yield nbt.NBTFile(temp_path, "rb") finally: os.remove(temp_path)
def decode_nbt(data, compress=None): # {{{ try: if compress == 'zlib': data = zlib.decompress(data) elif compress == 'gzip': data = gzip.decompress(data) except Exception as e: print(e) return None try: tmp_file = io.BytesIO(data) nbtfile = nbt.NBTFile(buffer=tmp_file) except Exception as e: nbtfile = None finally: tmp_file.close() if nbtfile == None and compress == None: try: uncompress_data = zlib.decompress(data) except Exception as e: uncompress_data = None if uncompress_data == None: try: uncompress_data = gzip.decompress(data) except Exception as e: uncompress_data = None if uncompress_data == None: return None try: tmp_file = io.BytesIO(uncompress_data) nbtfile = nbt.NBTFile(buffer=tmp_file) except Exception as e: nbtfile = None finally: tmp_file.close() return nbtfile
def scan_player(scanned_dat_file): """ At the moment only tries to read a .dat player file. It returns 0 if it's ok and 1 if has some problem """ s = scanned_dat_file try: player_dat = nbt.NBTFile(filename=s.path) s.readable = True except Exception as e: s.readable = False s.status_text = e
def load_chunk( self, cx: int, cz: int ) -> Tuple[nbt.TAG_List, nbt.TAG_List, nbt.TAG_List]: rx, rz = world_utils.chunk_coords_to_region_coords(cx, cz) key = (rx, rz) if not self.load_region(rx, rz): raise Exception() cx &= 0x1F cz &= 0x1F chunk_offset = self._loaded_regions[key]["offsets"][ (cx & 0x1F) + (cz & 0x1F) * 32 ] if chunk_offset == 0: raise Exception() sector_start = chunk_offset >> 8 number_of_sectors = chunk_offset & 0xFF if number_of_sectors == 0: raise Exception() if sector_start + number_of_sectors > len( self._loaded_regions[key]["free_sectors"] ): raise Exception() with open( path.join(self._directory, "region", "r.{}.{}.mca".format(rx, rz)), "rb" ) as fp: fp.seek(sector_start * world_utils.SECTOR_BYTES) data = fp.read(number_of_sectors * world_utils.SECTOR_BYTES) if len(data) < 5: raise Exception("Malformed sector/chunk") length = struct.unpack_from(">I", data)[0] _format = struct.unpack_from("B", data, 4)[0] data = data[5 : length + 5] if _format == world_utils.VERSION_GZIP: data = world_utils.gunzip(data) elif _format == world_utils.VERSION_DEFLATE: data = zlib.decompress(data) nbt_data = nbt.NBTFile(buffer=BytesIO(data)) return ( nbt_data["Level"]["Sections"], nbt_data["Level"]["TileEntities"], nbt_data["Level"]["Entities"], )
def check_level_file(level_file): """ At the moment only tries to read a level.dat file and print a warning if there are problems. """ try: level_dat = nbt.NBTFile(filename=level_file) print "\'level.dat\' is readable." del level_dat except Exception, e: print "[WARNING!] The level.dat may be corrupted. Exception: \'{0}\'".format( e)
def main(): file = input("level.dat [level.dat] : ") or "level.dat" nbt_file = nbt.NBTFile(file, "rb") items = list_items(nbt_file) axes = list_axes(items) for item in items: print(item) for axe in axes: print(axe)
def read(file_object): id=Short.read(file_object) if id==-1: return Slot() count=Byte.read(file_object.bytes) dmg=Short.read(file_object.bytes) try: NBT=nbt.NBTFile(buffer=file_object.bytes) except: NBT=None return Slot(id,count,dmg,NBT)
def existing_village_file(kovetz): """ Create an editable villages.nbt file from an already existing one, using the same tick value """ try: cat77 = nbt.NBTFile(kovetz) except IOError: raise Exception( "Hmm. Unfortunately, the file requested does not exist :(") tick4 = cat77['data']['Tick'].value return cat77, tick4
def savePlayers(self): os.makedirs(self.saveFolderPath() + '/playerdata', exist_ok=True) for player in self.players: # FIXME: path = self.saveFolderPath() + '/playerdata/player.dat' nbtfile = nbt.NBTFile() nbtfile.name = 'Player' nbtfile.tags = player.toNbt().tags nbtfile.write_file(path)
def check_player_file(player_file): """ At the moment only tries to read a .dat player file. it returns 0 if it's ok and 1 if has some problem """ try: player_dat = nbt.NBTFile(filename=player_file) del player_dat return 0 except Exception, e: print "[WARNING!] The player file {0} may be corrupted. Exception: \'{1}\'".format( player_file, e) return 1
def scan_level(world_obj): """ At the moment only tries to read a level.dat file and print a warning if there are problems. """ w = world_obj try: level_dat = nbt.NBTFile(filename = w.level_file) del level_dat except Exception, e: w.level_problems.append(e)
def __init__(self, world_path): self.path = world_path # list with RegionSets self.regionsets = [] self.regionsets.append(RegionSet(join(self.path, "region/"))) for directory in glob(join(self.path, "DIM*/region")): self.regionsets.append(RegionSet(join(self.path, directory))) # level.dat # let's scan level.dat here so we can extract the world name # right now level_dat_path = join(self.path, "level.dat") if exists(level_dat_path): try: self.level_data = nbt.NBTFile(level_dat_path)["Data"] self.name = self.level_data["LevelName"].value self.scanned_level = ScannedDatFile(level_dat_path, readable=True, status_text="OK") except Exception as e: self.name = None self.scanned_level = ScannedDatFile(level_dat_path, readable=False, status_text=e) else: self.level_file = None self.level_data = None self.name = None self.scanned_level = ScannedDatFile(None, False, "The file doesn't exist") # player files player_paths = glob(join(join(self.path, "players"), "*.dat")) self.players = {} for path in player_paths: name = split(path)[1].split(".")[0] self.players[name] = ScannedDatFile(path) # does it look like a world folder? region_files = False for region_directory in self.regionsets: if region_directory: region_files = True if region_files: self.isworld = True else: self.isworld = False # set in scan.py, used in interactive.py self.scanned = False
def read(file_object): item_id = Short.read(file_object) item_count = None item_damage = None nbt_data = None try: if item_id != -1: item_count = Byte.read(file_object) item_damage = Short.read(file_object) nbt_data = nbt.NBTFile(buffer=file_object) except nbt.MalformedFileError as e: pass return Slot(item_id, item_count, item_damage, nbt_data)