1use binrw::__private::Required;
2use binrw::{BinResult, BinWrite, BinWriterExt, Endian};
3use std::io::{Read, Seek, SeekFrom, Write};
4
5pub fn skip_bytes<R>(map: &mut R, s: u8)
6where
7 R: Seek,
8{
9 map.seek(SeekFrom::Current(s.into())).unwrap();
10}
11
12pub fn read_u8<R>(map: &mut R) -> u8
13where
14 R: Read,
15{
16 let mut buf: [u8; 1] = [0];
17 let _ = map.read(&mut buf);
18 buf[0]
19}
20
21pub fn read_u8_arr<R>(map: &mut R, size: u8) -> Vec<u8>
22where
23 R: Read,
24{
25 let mut buf: Vec<_> = Vec::with_capacity(size.into());
26 let _ = map.take(size.into()).read_to_end(&mut buf);
27 buf
28}
29
30pub fn read_i8<R>(map: &mut R) -> i8
31where
32 R: Read,
33{
34 read_u8(map) as i8
35}
36
37pub fn read_u16<R>(map: &mut R, endian: Endian) -> u16
38where
39 R: Read,
40{
41 let arr = arr2(map);
42 if endian == Endian::Little {
43 u16::from_le_bytes(arr)
44 } else {
45 u16::from_be_bytes(arr)
46 }
47}
48
49pub fn read_u16_arr<R>(map: &mut R, endian: Endian, size: u8) -> Vec<u16>
50where
51 R: Read,
52{
53 (0..size)
54 .filter_map(|_| match read_u16(map, endian) {
55 v => Some(v),
56 })
57 .collect()
58}
59
60pub fn read_i16<R>(map: &mut R, endian: Endian) -> i16
61where
62 R: Read,
63{
64 let arr = arr2(map);
65 if endian == Endian::Little {
66 i16::from_le_bytes(arr)
67 } else {
68 i16::from_be_bytes(arr)
69 }
70}
71
72pub fn write_bin<R, T>(map: &mut R, b: T, endian: Endian) -> BinResult<()>
73where
74 R: Write + Seek,
75 T: BinWrite,
76 for<'a> T::Args<'a>: Required,
77{
78 if endian == Endian::Little {
79 map.write_le(&b)
80 } else {
81 map.write_be(&b)
82 }
83}
84
85pub fn read_i32<R>(map: &mut R, endian: Endian) -> i32
86where
87 R: Read,
88{
89 let arr = arr4(map);
90 if endian == Endian::Little {
91 i32::from_le_bytes(arr)
92 } else {
93 i32::from_be_bytes(arr)
94 }
95}
96
97pub fn read_u32<R>(map: &mut R, endian: Endian) -> u32
98where
99 R: Read,
100{
101 let arr = arr4(map);
102 if endian == Endian::Little {
103 u32::from_le_bytes(arr)
104 } else {
105 u32::from_be_bytes(arr)
106 }
107}
108
109pub fn read_u32_arr<R>(map: &mut R, endian: Endian, size: u8) -> Vec<u32>
110where
111 R: Read,
112{
113 (0..size)
114 .filter_map(|_| match read_u32(map, endian) {
115 v => Some(v),
116 })
117 .collect()
118}
119
120pub fn read_u64<R>(map: &mut R, endian: Endian) -> u64
121where
122 R: Read,
123{
124 let arr = arr8(map);
125 if endian == Endian::Little {
126 u64::from_le_bytes(arr)
127 } else {
128 u64::from_be_bytes(arr)
129 }
130}
131
132pub fn read_i64<R>(map: &mut R, endian: Endian) -> i64
133where
134 R: Read,
135{
136 let arr = arr8(map);
137 if endian == Endian::Little {
138 i64::from_le_bytes(arr)
139 } else {
140 i64::from_be_bytes(arr)
141 }
142}
143
144fn arr2<R>(map: &mut R) -> [u8; 2]
145where
146 R: Read,
147{
148 let mut buf: [u8; 2] = [0; 2];
149 let _ = map.read(&mut buf);
150 buf
151}
152
153pub fn arr4<R>(map: &mut R) -> [u8; 4]
154where
155 R: Read,
156{
157 let mut buf: [u8; 4] = [0; 4];
158 let _ = map.read(&mut buf);
159 buf
160}
161
162fn arr8<R>(map: &mut R) -> [u8; 8]
163where
164 R: Read,
165{
166 let mut buf: [u8; 8] = [0; 8];
167 let _ = map.read(&mut buf);
168 buf
169}