aboutsummaryrefslogtreecommitdiff
path: root/azalea-protocol/src/mc_buf/mod.rs
blob: a924431eb5e6350d1a42004ec437a351a05aa5ab (plain)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//! Utilities for reading and writing for the Minecraft protocol

mod read;
mod write;

pub use read::{McBufReadable, McBufVarintReadable, Readable};
pub use write::{McBufVarintWritable, McBufWritable, Writable};

// const DEFAULT_NBT_QUOTA: u32 = 2097152;
const MAX_STRING_LENGTH: u16 = 32767;
// const MAX_COMPONENT_STRING_LENGTH: u32 = 262144;

#[cfg(test)]
mod tests {
    use super::*;
    use azalea_core::resource_location::ResourceLocation;
    use std::{collections::HashMap, io::Cursor};
    use tokio::io::BufReader;

    #[test]
    fn test_write_varint() {
        let mut buf = Vec::new();
        buf.write_varint(123456).unwrap();
        assert_eq!(buf, vec![192, 196, 7]);

        let mut buf = Vec::new();
        buf.write_varint(0).unwrap();
        assert_eq!(buf, vec![0]);
    }

    #[tokio::test]
    async fn test_read_varint() {
        let mut buf = BufReader::new(Cursor::new(vec![192, 196, 7]));
        assert_eq!(buf.read_varint().await.unwrap(), 123456);
        assert_eq!(buf.get_varint_size(123456), 3);

        let mut buf = BufReader::new(Cursor::new(vec![0]));
        assert_eq!(buf.read_varint().await.unwrap(), 0);
        assert_eq!(buf.get_varint_size(0), 1);

        let mut buf = BufReader::new(Cursor::new(vec![1]));
        assert_eq!(buf.read_varint().await.unwrap(), 1);
        assert_eq!(buf.get_varint_size(1), 1);
    }

    #[tokio::test]
    async fn test_read_varint_longer() {
        let mut buf = BufReader::new(Cursor::new(vec![138, 56, 0, 135, 56, 123]));
        assert_eq!(buf.read_varint().await.unwrap(), 7178);
    }

    #[tokio::test]
    async fn test_list() {
        let mut buf = Vec::new();
        buf.write_list(&vec!["a", "bc", "def"], |buf, s| buf.write_utf(s))
            .unwrap();

        // there's no read_list because idk how to do it in rust
        let mut buf = BufReader::new(Cursor::new(buf));

        let mut result = Vec::new();
        let length = buf.read_varint().await.unwrap();
        for _ in 0..length {
            result.push(buf.read_utf().await.unwrap());
        }

        assert_eq!(result, vec!["a", "bc", "def"]);
    }

    #[tokio::test]
    async fn test_int_id_list() {
        let mut buf = Vec::new();
        buf.write_list(&vec![1, 2, 3], |buf, i| buf.write_varint(*i))
            .unwrap();

        let mut buf = BufReader::new(Cursor::new(buf));

        let result = buf.read_int_id_list().await.unwrap();
        assert_eq!(result, vec![1, 2, 3]);
    }

    #[tokio::test]
    async fn test_map() {
        let mut buf = Vec::new();
        buf.write_map(
            vec![("a", 1), ("bc", 23), ("def", 456)],
            Vec::write_utf,
            Vec::write_varint,
        )
        .unwrap();

        let mut buf = BufReader::new(Cursor::new(buf));

        let mut result = Vec::new();
        let length = buf.read_varint().await.unwrap();
        for _ in 0..length {
            result.push((
                buf.read_utf().await.unwrap(),
                buf.read_varint().await.unwrap(),
            ));
        }

        assert_eq!(
            result,
            vec![
                ("a".to_string(), 1),
                ("bc".to_string(), 23),
                ("def".to_string(), 456)
            ]
        );
    }

    #[tokio::test]
    async fn test_nbt() {
        let mut buf = Vec::new();
        buf.write_nbt(&azalea_nbt::Tag::Compound(HashMap::from_iter(vec![(
            "hello world".to_string(),
            azalea_nbt::Tag::Compound(HashMap::from_iter(vec![(
                "name".to_string(),
                azalea_nbt::Tag::String("Bananrama".to_string()),
            )])),
        )])))
        .unwrap();

        let mut buf = BufReader::new(Cursor::new(buf));

        let result = buf.read_nbt().await.unwrap();
        assert_eq!(
            result,
            azalea_nbt::Tag::Compound(HashMap::from_iter(vec![(
                "hello world".to_string(),
                azalea_nbt::Tag::Compound(HashMap::from_iter(vec![(
                    "name".to_string(),
                    azalea_nbt::Tag::String("Bananrama".to_string()),
                )])),
            )]))
        );
    }

    #[tokio::test]
    async fn test_long() {
        let mut buf = Vec::new();
        buf.write_long(123456).unwrap();

        let mut buf = BufReader::new(Cursor::new(buf));

        assert_eq!(buf.read_long().await.unwrap(), 123456);
    }

    #[tokio::test]
    async fn test_resource_location() {
        let mut buf = Vec::new();
        buf.write_resource_location(&ResourceLocation::new("minecraft:dirt").unwrap())
            .unwrap();

        let mut buf = BufReader::new(Cursor::new(buf));

        assert_eq!(
            buf.read_resource_location().await.unwrap(),
            ResourceLocation::new("minecraft:dirt").unwrap()
        );
    }
}