Giter VIP home page Giter VIP logo

nbt-rust's Introduction

nbt_rust

A Rust library for serializing and deserializing the NBT (Named Binary Tag) file format used by Minecraft.

NBT format documentation

Serialization

Check out the examples or simply run

  • cargo run --example from_file
  • cargo run --example serialize
  • cargo run --example deserialize

This library allows you to write a single u8, or String, or any other supported type.

use nbt_rust::ser::Serializer;

let writer = /* impl std::io::Write */;
let mut ser = Serializer::new(writer);
ser.serialize("my_int", 1)?; // or ser.serialize_int("my_int", 1)
ser.serialize("my_byte", 12u8)?; // or ser.serialize_byte("my_byte", 12)
ser.serialize("my_short", 123i16)?; // or ser.serialize_short("my_short", 123)
ser.serialize("my_string", "Das ist cool")?; // or ser.serialize_string("my_string", "Das ist cool")

Note: In this example, different data types are serialized consecutively but not within a List or an NBT array, so the Deserializer will only read the first value.

Minecraft typically uses Compound tags in its files, so consider using start_compound immediately.

use nbt_rust::ser::Serializer;

let writer = /* impl std::io::Write */;
let mut compound_ser = Serializer::new(writer).start_compound("my_compound")?;

// Write fields to the Compound tag
compound_ser.write_field("item_1", 213u32)?;
compound_ser.write_field("item_2", &[1u8,2,3,4])?;

// Finish the compound tag and get your writer back (if needed)
compound_ser.end()?; // This will return the owned `Serializer` object

Deserialization

There was no real need to wrap deserialization methods into a struct.

There are two ways to deserialize: from_bytes and from_reader

  • from_bytes(&[u8])
use nbt_rust::de::from_bytes;
use nbt_rust::NbtTag;

let bytes = /* Some NBT formatted binary data */;
let (name: String, result: NbtTag) = from_bytes(bytes);
/* Do something with `name` and `result` */
  • from_reader(impl std::io::Read)
use nbt_rust::de::from_reader;
use nbt_rust::NbtTag;

let data = /* impl std::io::Read */;
let (name: String, result: NbtTag) = from_reader(bytes);
/* Do something with `name` and `result` */

nbt-rust's People

Contributors

hollowness-inside avatar

Stargazers

 avatar  avatar

Watchers

 avatar

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.