summaryrefslogtreecommitdiff
path: root/shared/src/helper.rs
blob: d91313cddd01bc6ee63e7e4872de634470e07078 (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
/*
    wearechat - generic multiplayer game with voip
    Copyright (C) 2025 metamuffin

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, version 3 of the License only.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/
use crate::packets::{Data, Object, Resource};
use anyhow::Result;
use std::{
    io::{Read, Write},
    marker::PhantomData,
};

pub trait ReadWrite: Sized {
    fn write(&self, w: &mut dyn Write) -> Result<()>;
    fn read(r: &mut dyn Read) -> Result<Self>;

    fn write_alloc(&self) -> Vec<u8> {
        let mut buf = Vec::new();
        self.write(&mut buf).unwrap();
        buf
    }
}

impl ReadWrite for f32 {
    fn write(&self, w: &mut dyn Write) -> Result<()> {
        w.write_all(&self.to_be_bytes())?;
        Ok(())
    }
    fn read(r: &mut dyn Read) -> Result<Self> {
        let mut buf = [0; 4];
        r.read_exact(&mut buf)?;
        Ok(f32::from_be_bytes(buf))
    }
}
impl ReadWrite for u32 {
    fn write(&self, w: &mut dyn Write) -> Result<()> {
        w.write_all(&self.to_be_bytes())?;
        Ok(())
    }
    fn read(r: &mut dyn Read) -> Result<Self> {
        let mut buf = [0; 4];
        r.read_exact(&mut buf)?;
        Ok(u32::from_be_bytes(buf))
    }
}
impl ReadWrite for Vec<u8> {
    fn write(&self, w: &mut dyn Write) -> Result<()> {
        w.write_all(&self)?;
        Ok(())
    }
    fn read(r: &mut dyn Read) -> Result<Self> {
        let mut buf = Vec::new();
        r.read_to_end(&mut buf)?;
        Ok(buf)
    }
}
impl ReadWrite for String {
    fn write(&self, w: &mut dyn Write) -> Result<()> {
        w.write_all(self.as_bytes())?;
        Ok(())
    }
    fn read(r: &mut dyn Read) -> Result<Self> {
        let mut buf = Vec::new();
        r.read_to_end(&mut buf)?;
        Ok(String::from_utf8(buf)?)
    }
}

impl ReadWrite for Data {
    fn write(&self, w: &mut dyn Write) -> Result<()> {
        w.write_all(&(self.0.len() as u32).to_be_bytes())?;
        w.write_all(&self.0)?;
        Ok(())
    }
    fn read(r: &mut dyn Read) -> Result<Self> {
        let mut size = [0; 4];
        r.read_exact(&mut size)?;
        let size = u32::from_be_bytes(size);
        let mut buf = vec![0; size as usize];
        r.read_exact(&mut buf)?;
        Ok(Self(buf))
    }
}
impl<T> ReadWrite for Resource<T> {
    fn write(&self, w: &mut dyn Write) -> Result<()> {
        w.write_all(&self.0)?;
        Ok(())
    }
    fn read(r: &mut dyn Read) -> Result<Self> {
        let mut s = Self([0; 32], PhantomData);
        r.read_exact(&mut s.0)?;
        Ok(s)
    }
}
impl ReadWrite for Object {
    fn write(&self, w: &mut dyn Write) -> Result<()> {
        w.write_all(&self.0.to_be_bytes())?;
        Ok(())
    }
    fn read(r: &mut dyn Read) -> Result<Self> {
        let mut s = [0; 16];
        r.read_exact(&mut s)?;
        Ok(Object(u128::from_be_bytes(s)))
    }
}
impl<T: ReadWrite, const N: usize> ReadWrite for [T; N] {
    fn write(&self, w: &mut dyn Write) -> Result<()> {
        for e in self {
            e.write(w)?;
        }
        Ok(())
    }

    fn read(r: &mut dyn Read) -> Result<Self> {
        [(); N].try_map(|()| T::read(r))
    }
}