A multiplayer VR framework w/voice chat
1use std::io::Read;
2
3use bevy::math::{Quat, Vec3};
4
5pub struct StreamedBuffer{
6 stream: Box<dyn Read>
7}
8
9impl StreamedBuffer{
10 pub fn new( stream: impl Read + 'static ) -> Self{
11 Self {
12 stream: Box::new(stream)
13 }
14 }
15
16 pub fn get_u8(&mut self) -> u8{
17 let mut b = [0u8; 1];
18 self.stream.read(&mut b).unwrap();
19
20 b[0]
21 }
22
23 pub fn get_u8s(&mut self, count: usize) -> Vec<u8>{
24 let mut b = vec![0u8; count];
25 self.stream.read(&mut b).unwrap();
26
27 b
28 }
29
30 pub fn get_i32(&mut self) -> i32{
31 let b = self.get_u8s(4);
32 i32::from_le_bytes([ b[0], b[1], b[2], b[3] ])
33 }
34
35 pub fn get_u32(&mut self) -> u32{
36 let b = self.get_u8s(4);
37 u32::from_le_bytes([ b[0], b[1], b[2], b[3] ])
38 }
39
40 pub fn get_str(&mut self) -> String{
41 let length = self.get_u32();
42 let b = self.get_u8s(length as usize);
43
44 let string = str::from_utf8(&b).unwrap();
45 string.to_owned()
46 }
47
48
49 pub fn get_vec3(&mut self) -> Vec3{
50 Vec3 { x: self.get_f32(), y: self.get_f32(), z: self.get_f32() }
51 }
52
53 pub fn get_quat(&mut self) -> Quat{
54 Quat::from_xyzw(self.get_f32(), self.get_f32(), self.get_f32(), self.get_f32())
55 }
56
57
58 pub fn get_f32(&mut self) -> f32{
59 let b = self.get_u8s(4);
60 f32::from_le_bytes([ b[0], b[1], b[2], b[3] ])
61 }
62}