Untitled
unknown
plain_text
a year ago
1.8 kB
7
Indexable
class AnvilFile:
def __init__(self, file_path):
self.file_path = file_path
self.locations = []
self.timestamps = []
self.chunks = []
def write(self,filepath):
with open(filepath,'wb') as f:
for location in self.locations:
f.write(struct.pack('>I', location))
for timestamp in self.timestamps:
f.write(struct.pack('>I', timestamp))
#f.seek(8192)
print(len(self.chunks))
for chunk in self.chunks:
if chunk is not None:
length, compression_type, compression_data = chunk
f.write(struct.pack('>I', length))
f.write(struct.pack('>B', compression_type))
f.write(compression_data)
# Pad to 4KiB alignment
padding = 4096 - (length + 4) % 4096
if padding < 4096:
f.write(b'\x00' * padding)
def load(self):
with open(self.file_path, 'rb') as f:
# Read the header (chunk location table)
self.locations = [struct.unpack('>I', f.read(4))[0] for _ in range(1024)]
self.timestamps = [struct.unpack('>I', f.read(4))[0] for _ in range(1024)]
print(len(self.locations))
#print(self.timestamps)
for index, offset in enumerate(self.locations):
if offset != 0:
sector_offset = (offset >> 8) * 4096
f.seek(sector_offset)
length = struct.unpack('>I', f.read(4))[0]
compression_type = struct.unpack('>B', f.read(1))[0]
chunk_data = f.read(length - 1)
self.chunks.append((length, compression_type, chunk_data))
Editor is loading...
Leave a Comment