1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use crate::{CompoundTag, Tag};
use byteorder::{BigEndian, WriteBytesExt};
use flate2::write::{GzEncoder, ZlibEncoder};
use std::io::{Error, Write};

pub fn write_gzip_compound_tag<W: Write>(
    writer: &mut W,
    compound_tag: CompoundTag,
) -> Result<(), Error> {
    write_compound_tag(
        &mut GzEncoder::new(writer, Default::default()),
        compound_tag,
    )
}

pub fn write_zlib_compound_tag<W: Write>(
    writer: &mut W,
    compound_tag: CompoundTag,
) -> Result<(), Error> {
    write_compound_tag(
        &mut ZlibEncoder::new(writer, Default::default()),
        compound_tag,
    )
}

pub fn write_compound_tag<W: Write>(
    writer: &mut W,
    compound_tag: CompoundTag,
) -> Result<(), Error> {
    let name = compound_tag.name.clone();
    let tag = Tag::Compound(compound_tag);
    writer.write_u8(tag.type_id())?;

    match name {
        Some(value) => write_string(writer, value.to_owned())?,
        None => write_string(writer, String::from(""))?,
    }

    write_tag(writer, tag)
}

fn write_tag<W: Write>(writer: &mut W, tag: Tag) -> Result<(), Error> {
    match tag {
        Tag::Byte(value) => writer.write_i8(value)?,
        Tag::Short(value) => writer.write_i16::<BigEndian>(value)?,
        Tag::Int(value) => writer.write_i32::<BigEndian>(value)?,
        Tag::Long(value) => writer.write_i64::<BigEndian>(value)?,
        Tag::Float(value) => writer.write_f32::<BigEndian>(value)?,
        Tag::Double(value) => writer.write_f64::<BigEndian>(value)?,
        Tag::ByteArray(value) => {
            writer.write_u32::<BigEndian>(value.len() as u32)?;

            for v in value {
                writer.write_i8(v)?;
            }
        }
        Tag::String(value) => write_string(writer, value)?,
        Tag::List(value) => {
            if value.len() > 0 {
                writer.write_u8(value[0].type_id())?;
            } else {
                // Empty list type.
                writer.write_u8(0)?;
            }

            writer.write_u32::<BigEndian>(value.len() as u32)?;

            for tag in value {
                write_tag(writer, tag)?;
            }
        }
        Tag::Compound(value) => {
            for (name, tag) in value.tags {
                writer.write_u8(tag.type_id())?;
                write_string(writer, name)?;
                write_tag(writer, tag)?;
            }

            // To mark compound tag end.
            writer.write_u8(0)?;
        }
        Tag::IntArray(value) => {
            writer.write_u32::<BigEndian>(value.len() as u32)?;

            for v in value {
                writer.write_i32::<BigEndian>(v)?;
            }
        }
        Tag::LongArray(value) => {
            writer.write_u32::<BigEndian>(value.len() as u32)?;

            for v in value {
                writer.write_i64::<BigEndian>(v)?;
            }
        }
    }

    Ok(())
}

fn write_string<W: Write>(writer: &mut W, value: String) -> Result<(), Error> {
    writer.write_u16::<BigEndian>(value.len() as u16)?;
    writer.write(value.as_bytes())?;

    Ok(())
}

#[test]
fn test_hello_world_write() {
    let mut hello_world = CompoundTag::named("hello world");
    hello_world.insert_str("name", "Bananrama");

    let mut vec = Vec::new();
    write_compound_tag(&mut vec, hello_world).unwrap();

    assert_eq!(
        vec,
        include_bytes!("../test/binary/hello_world.dat").to_vec()
    );
}

#[test]
fn test_servers_write() {
    let mut server = CompoundTag::new();

    server.insert_str("ip", "localhost:25565");
    server.insert_str("name", "Minecraft Server");
    server.insert_bool("hideAddress", true);

    let mut servers = Vec::new();
    servers.push(server);

    let mut root_tag = CompoundTag::new();
    root_tag.insert_compound_tag_vec("servers", servers);

    let mut vec = Vec::new();
    write_compound_tag(&mut vec, root_tag).unwrap();

    assert_eq!(vec, include_bytes!("../test/binary/servers.dat").to_vec());
}