Giter VIP home page Giter VIP logo

Comments (1)

max-ishere avatar max-ishere commented on August 22, 2024

Here are 2 functions that allow you to read a region file header and then get the raw data from them

import zlib, gzip


def GetAnvilChunkData(region, x: int, z: int) -> dict:
    """Returns info stored in anvil header about a chunk at chunk coordinates
    Arguments:
    region -- a file object created with open('/path/')
    x, z -- chunk position (regardless if its region-local or global)
    """
    region.seek(4 * ((x % 32) + (z % 32) * 32), 0)

    offset = int.from_bytes(region.read(3), byteorder ='big')
    sectors = int.from_bytes(region.read(1), byteorder ='big')
    
    region.seek(4 * ((x % 32) + (z % 32) * 32) + 0x1000, 0)
    
    timestamp = int.from_bytes(region.read(4), byteorder ='big')
    
    return dict({'offset':    offset,
                'sectors':   sectors,
                 'timestamp': timestamp})

def GetChunkNbtData(region, x: int, z: int):
    """Finds chunk data sectors and returns uncompressed buffer with NBT data"""
    anvil_data = GetAnvilChunkData(region, x, z)

    if anvil_data['offset'] < 2:
        return bytes(0)
    
    region.seek(anvil_data['offset'] * 0x1000)

    length = int.from_bytes(region.read(4), byteorder ='big')
    compression_type = int.from_bytes(region.read(1), byteorder ='big')
    
    if compression_type == 3:
        return region.read(length)

    if compression_type == 2:
        return zlib.decompress(region.read(length))
    
    if compression_type == 1:
        return gzip.decompress(region.read(length))
    

Once you have those you can use code below to read raw nbt data and make a tree.

#!/bin/python
import nbtlib, io
from mvc.mca import GetChunkNbtData


def main():
    file = open('data/region.mca', 'rb')
    nbt_file = nbtlib.File.from_fileobj(io.BytesIO(GetChunkNbtData(file, 3, 3)))
    print(nbt_file.root.keys())

main()

Seems to work fine with my test data from hermitcraft.com S7:

$ python main.py
dict_keys(['Level', 'DataVersion'])

If this gets added then pls credit me, not that I am willing to do that rn myself... sorry have some stuff i want to finish

from nbtlib.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.