rpfm_lib/binary/reader.rs
1//---------------------------------------------------------------------------//
2// Copyright (c) 2017-2026 Ismael Gutiérrez González. All rights reserved.
3//
4// This file is part of the Rusted PackFile Manager (RPFM) project,
5// which can be found here: https://github.com/Frodo45127/rpfm.
6//
7// This file is licensed under the MIT license, which can be found here:
8// https://github.com/Frodo45127/rpfm/blob/master/LICENSE.
9//---------------------------------------------------------------------------//
10
11//! Module with the [`ReadBytes`] trait, to read bytes to known types.
12
13use byteorder::{BigEndian, LittleEndian, ReadBytesExt};
14use encoding_rs::{ISO_8859_15, UTF_16LE};
15use itertools::Itertools;
16use nalgebra::{Vector2, Vector4};
17
18use std::io::{Read, Seek, SeekFrom};
19
20use crate::error::{Result, RLibError};
21
22//---------------------------------------------------------------------------//
23// Trait Definition
24//---------------------------------------------------------------------------//
25
26/// This trait allow us to easily read all kind of data from a source that implements [`Read`] + [`Seek`].
27pub trait ReadBytes: Read + Seek {
28
29 /// This function returns the length of the data we're reading.
30 ///
31 /// Extracted from the nightly std.
32 ///
33 /// ```rust
34 /// use std::io::Cursor;
35 ///
36 /// use rpfm_lib::binary::ReadBytes;
37 ///
38 /// let data = vec![1, 2, 3, 4];
39 /// let mut cursor = Cursor::new(data);
40 /// let len = cursor.len().unwrap();
41 /// assert_eq!(len, 4);
42 /// ```
43 fn len(&mut self) -> Result<u64> {
44 let old_pos = self.stream_position()?;
45 let len = self.seek(SeekFrom::End(0))?;
46 // Avoid seeking a third time when we were already at the end of the
47 // stream. The branch is usually way cheaper than a seek operation.
48 if old_pos != len {
49 self.seek(SeekFrom::Start(old_pos))?;
50 }
51 Ok(len)
52 }
53
54 /// This function returns if the data is empty.
55 ///
56 /// It's slightly faster than checking for len == 0.
57 ///
58 /// ```rust
59 /// use std::io::Cursor;
60 ///
61 /// use rpfm_lib::binary::ReadBytes;
62 ///
63 /// let data = vec![];
64 /// let mut cursor = Cursor::new(data);
65 /// assert!(ReadBytes::is_empty(&mut cursor).unwrap());
66 /// ```
67 fn is_empty(&mut self) -> Result<bool> {
68 self.len().map(|len| len == 0)
69 }
70
71 /// This function returns the amount of bytes specified in the `size` argument as a [`Vec<u8>`].
72 ///
73 /// If `rewind` is true, the cursor will be reset to its original position once the data is returned.
74 ///
75 /// ```rust
76 /// use std::io::Cursor;
77 ///
78 /// use rpfm_lib::binary::ReadBytes;
79 ///
80 /// let data = vec![1, 2, 3, 4];
81 /// let mut cursor = Cursor::new(data.to_vec());
82 /// let data_read = cursor.read_slice(4, false).unwrap();
83 /// assert_eq!(data, data_read);
84 ///
85 /// # assert_eq!(ReadBytes::read_slice(&mut Cursor::new([1, 2, 3, 4]), 4, false).unwrap(), vec![1, 2, 3, 4]);
86 /// # assert_eq!(ReadBytes::read_slice(&mut Cursor::new(vec![0u8; 0]), 0, false).unwrap(), vec![0u8; 0]);
87 /// # assert_eq!(ReadBytes::read_slice(&mut Cursor::new([]), 4, false).is_err(), true);
88 /// ```
89 fn read_slice(&mut self, size: usize, rewind: bool) -> Result<Vec<u8>> {
90 let mut data = vec![0; size];
91
92 // If len is 0, just return.
93 if size == 0 {
94 return Ok(data)
95 }
96
97 self.read_exact(&mut data)?;
98
99 if rewind {
100 self.seek(SeekFrom::Current(-(size as i64)))?;
101 }
102
103 Ok(data)
104 }
105
106 /// This function tries to read a bool value from `self`.
107 ///
108 /// This is simple: 0 is false, 1 is true. Anything else is an error.
109 /// It may fail if there are not enough bytes to read the value or `self` cannot be read.
110 ///
111 /// ```rust
112 /// use std::io::Cursor;
113 ///
114 /// use rpfm_lib::binary::ReadBytes;
115 ///
116 /// let data = vec![0, 1, 2];
117 /// let mut cursor = Cursor::new(data);
118 ///
119 /// let first = cursor.read_bool();
120 /// let second = cursor.read_bool();
121 /// let third = cursor.read_bool();
122 ///
123 /// assert_eq!(first.unwrap(), false);
124 /// assert_eq!(second.unwrap(), true);
125 /// assert!(third.is_err());
126 /// ```
127 fn read_bool(&mut self) -> Result<bool> {
128 let value = self.read_u8()?;
129 match value {
130 0 => Ok(false),
131 1 => Ok(true),
132 _ => Err(RLibError::DecodingBoolError(value)),
133 }
134 }
135
136 /// This function tries to read an unsigned byte value from `self`.
137 ///
138 /// It may fail if there are not enough bytes to read the value or `self` cannot be read.
139 ///
140 /// ```rust
141 /// use std::io::Cursor;
142 ///
143 /// use rpfm_lib::binary::ReadBytes;
144 ///
145 /// let data = vec![10];
146 /// let mut cursor = Cursor::new(data);
147 /// let data = cursor.read_u8().unwrap();
148 ///
149 /// assert_eq!(data, 10);
150 /// assert_eq!(cursor.read_u8().is_err(), true);
151 /// ```
152 fn read_u8(&mut self) -> Result<u8> {
153 ReadBytesExt::read_u8(self).map_err(From::from)
154 }
155
156 /// This function tries to read an u16 value from `self`.
157 ///
158 /// It may fail if there are not enough bytes to read the value or `self` cannot be read.
159 ///
160 /// ```rust
161 /// use std::io::Cursor;
162 ///
163 /// use rpfm_lib::binary::ReadBytes;
164 ///
165 /// let data = vec![10, 0, 10];
166 /// let mut cursor = Cursor::new(data);
167 /// let data = cursor.read_u16().unwrap();
168 ///
169 /// assert_eq!(data, 10);
170 /// assert_eq!(cursor.read_u16().is_err(), true);
171 /// ```
172 fn read_u16(&mut self) -> Result<u16> {
173 ReadBytesExt::read_u16::<LittleEndian>(self).map_err(From::from)
174 }
175
176 /// This function tries to read an u24 value from `self`.
177 ///
178 /// It may fail if there are not enough bytes to read the value or `self` cannot be read.
179 ///
180 /// ```rust
181 /// use std::io::Cursor;
182 ///
183 /// use rpfm_lib::binary::ReadBytes;
184 ///
185 /// let data = vec![152, 150, 129, 152, 150];
186 /// let mut cursor = Cursor::new(data);
187 /// let data = cursor.read_u24().unwrap();
188 ///
189 /// assert_eq!(data, 84_926_96);
190 /// assert_eq!(cursor.read_u24().is_err(), true);
191 /// ```
192 fn read_u24(&mut self) -> Result<u32> {
193 ReadBytesExt::read_u24::<LittleEndian>(self).map_err(From::from)
194 }
195
196 /// This function tries to read a big-endian u24 value from `self`.
197 ///
198 /// It may fail if there are not enough bytes to read the value or `self` cannot be read.
199 ///
200 /// ```rust
201 /// use std::io::Cursor;
202 ///
203 /// use rpfm_lib::binary::ReadBytes;
204 ///
205 /// let data = vec![1, 134, 160, 152, 150];
206 /// let mut cursor = Cursor::new(data);
207 /// let data = cursor.read_u24_be().unwrap();
208 ///
209 /// assert_eq!(data, 100_000);
210 /// assert_eq!(cursor.read_u24_be().is_err(), true);
211 /// ```
212 fn read_u24_be(&mut self) -> Result<u32> {
213 ReadBytesExt::read_u24::<BigEndian>(self).map_err(From::from)
214 }
215
216 /// This function tries to read an u32 value from `self`.
217 ///
218 /// It may fail if there are not enough bytes to read the value or `self` cannot be read.
219 ///
220 /// ```rust
221 /// use std::io::Cursor;
222 ///
223 /// use rpfm_lib::binary::ReadBytes;
224 ///
225 /// let data = vec![10, 0, 0, 0, 10, 0, 0];
226 /// let mut cursor = Cursor::new(data);
227 /// let data = cursor.read_u32().unwrap();
228 ///
229 /// assert_eq!(data, 10);
230 /// assert_eq!(cursor.read_u32().is_err(), true);
231 /// ```
232 fn read_u32(&mut self) -> Result<u32> {
233 ReadBytesExt::read_u32::<LittleEndian>(self).map_err(From::from)
234 }
235
236 /// This function tries to read an u64 value from `self`.
237 ///
238 /// It may fail if there are not enough bytes to read the value or `self` cannot be read.
239 ///
240 /// ```rust
241 /// use std::io::Cursor;
242 ///
243 /// use rpfm_lib::binary::ReadBytes;
244 ///
245 /// let data = vec![10, 0, 0, 0, 0, 0, 0, 0, 10, 0];
246 /// let mut cursor = Cursor::new(data);
247 /// let data = cursor.read_u64().unwrap();
248 ///
249 /// assert_eq!(data, 10);
250 /// assert_eq!(cursor.read_u64().is_err(), true);
251 /// ```
252 fn read_u64(&mut self) -> Result<u64> {
253 ReadBytesExt::read_u64::<LittleEndian>(self).map_err(From::from)
254 }
255
256 /// This function tries to read CA's own take (or I greatly misunderstood this) on ULEB_128 values from `self`.
257 ///
258 /// It may fail if there are not enough bytes to read the value or `self` cannot be read.
259 ///
260 /// ```rust
261 /// use std::io::Cursor;
262 ///
263 /// use rpfm_lib::binary::ReadBytes;
264 ///
265 /// let data = vec![0x80, 10];
266 /// let mut cursor = Cursor::new(data);
267 /// let data = cursor.read_cauleb128().unwrap();
268 ///
269 /// assert_eq!(data, 10);
270 /// assert_eq!(cursor.read_cauleb128().is_err(), true);
271 /// ```
272 fn read_cauleb128(&mut self) -> Result<u32> {
273 let mut value: u32 = 0;
274 let mut byte = self.read_u8()?;
275
276 while(byte & 0x80) != 0 {
277 value = (value << 7) | (byte & 0x7f) as u32;
278
279 // Check the new byte is even valid before continuing.
280 byte = self.read_u8()?;
281 }
282
283 value = (value << 7) | (byte & 0x7f) as u32;
284 Ok(value)
285 }
286
287 /// This function tries to read a signed byte value from `self`.
288 ///
289 /// It may fail if there are not enough bytes to read the value or `self` cannot be read.
290 ///
291 /// ```rust
292 /// use std::io::Cursor;
293 ///
294 /// use rpfm_lib::binary::ReadBytes;
295 ///
296 /// let data = vec![254];
297 /// let mut cursor = Cursor::new(data);
298 /// let data = cursor.read_i8().unwrap();
299 ///
300 /// assert_eq!(data, -2);
301 /// assert_eq!(cursor.read_i8().is_err(), true);
302 /// ```
303 fn read_i8(&mut self) -> Result<i8> {
304 ReadBytesExt::read_i8(self).map_err(From::from)
305 }
306
307 /// This function tries to read an i16 value from `self`.
308 ///
309 /// It may fail if there are not enough bytes to read the value or `self` cannot be read.
310 ///
311 /// ```rust
312 /// use std::io::Cursor;
313 ///
314 /// use rpfm_lib::binary::ReadBytes;
315 ///
316 /// let data = vec![254, 254, 10];
317 /// let mut cursor = Cursor::new(data);
318 /// let data = cursor.read_i16().unwrap();
319 ///
320 /// assert_eq!(data, -258);
321 /// assert_eq!(cursor.read_i16().is_err(), true);
322 /// ```
323 fn read_i16(&mut self) -> Result<i16> {
324 ReadBytesExt::read_i16::<LittleEndian>(self).map_err(From::from)
325 }
326
327 /// This function tries to read an i24 value from `self`.
328 ///
329 /// It may fail if there are not enough bytes to read the value or `self` cannot be read.
330 ///
331 /// ```rust
332 /// use std::io::Cursor;
333 ///
334 /// use rpfm_lib::binary::ReadBytes;
335 ///
336 /// let data = vec![152, 150, 129, 152, 150];
337 /// let mut cursor = Cursor::new(data);
338 /// let data = cursor.read_i24().unwrap();
339 ///
340 /// assert_eq!(data, -8_284_520);
341 /// assert_eq!(cursor.read_i24().is_err(), true);
342 /// ```
343 fn read_i24(&mut self) -> Result<i32> {
344 ReadBytesExt::read_i24::<LittleEndian>(self).map_err(From::from)
345 }
346
347 /// This function tries to read a big-endian i24 value from `self`.
348 ///
349 /// It may fail if there are not enough bytes to read the value or `self` cannot be read.
350 ///
351 /// ```rust
352 /// use std::io::Cursor;
353 ///
354 /// use rpfm_lib::binary::ReadBytes;
355 ///
356 /// let data = vec![255, 255, 254, 152, 150];
357 /// let mut cursor = Cursor::new(data);
358 /// let data = cursor.read_i24_be().unwrap();
359 ///
360 /// assert_eq!(data, -2);
361 /// assert_eq!(cursor.read_i24_be().is_err(), true);
362 /// ```
363 fn read_i24_be(&mut self) -> Result<i32> {
364 ReadBytesExt::read_i24::<BigEndian>(self).map_err(From::from)
365 }
366
367 /// This function tries to read an i32 value from `self`.
368 ///
369 /// It may fail if there are not enough bytes to read the value or `self` cannot be read.
370 ///
371 /// ```rust
372 /// use std::io::Cursor;
373 ///
374 /// use rpfm_lib::binary::ReadBytes;
375 ///
376 /// let data = vec![10, 0, 0, 0, 10, 0, 0];
377 /// let mut cursor = Cursor::new(data);
378 /// let data = cursor.read_i32().unwrap();
379 ///
380 /// assert_eq!(data, 10);
381 /// assert_eq!(cursor.read_i32().is_err(), true);
382 /// ```
383 fn read_i32(&mut self) -> Result<i32> {
384 ReadBytesExt::read_i32::<LittleEndian>(self).map_err(From::from)
385 }
386
387 /// This function tries to read an i64 value from `self`.
388 ///
389 /// It may fail if there are not enough bytes to read the value or `self` cannot be read.
390 ///
391 /// ```rust
392 /// use std::io::Cursor;
393 ///
394 /// use rpfm_lib::binary::ReadBytes;
395 ///
396 /// let data = vec![10, 0, 0, 0, 0, 0, 0, 0, 10, 0];
397 /// let mut cursor = Cursor::new(data);
398 /// let data = cursor.read_i64().unwrap();
399 ///
400 /// assert_eq!(data, 10);
401 /// assert_eq!(cursor.read_i64().is_err(), true);
402 /// ```
403 fn read_i64(&mut self) -> Result<i64> {
404 ReadBytesExt::read_i64::<LittleEndian>(self).map_err(From::from)
405 }
406
407 /// This function tries to read an optional i16 value from `self`.
408 ///
409 /// The value is preceeded by a bool. If the bool is true, we expect a value after it.
410 /// If its false, we expect a sentinel value (0) after it.
411 ///
412 /// It may fail if there are not enough bytes to read the value or `self` cannot be read.
413 ///
414 /// ```rust
415 /// use std::io::Cursor;
416 ///
417 /// use rpfm_lib::binary::ReadBytes;
418 ///
419 /// let data = vec![1, 254, 254, 2];
420 /// let mut cursor = Cursor::new(data);
421 /// let data = cursor.read_optional_i16().unwrap();
422 ///
423 /// assert_eq!(data, -258);
424 /// assert_eq!(cursor.read_optional_i16().is_err(), true);
425 ///
426 /// # assert_eq!(ReadBytes::read_optional_i16(&mut Cursor::new([1, 10])).is_err(), true);
427 /// ```
428 fn read_optional_i16(&mut self) -> Result<i16> {
429 let _ = self.read_bool()?;
430 self.read_i16()
431 }
432
433 /// This function tries to read an optional i32 value from `self`.
434 ///
435 /// The value is preceeded by a bool. If the bool is true, we expect a value after it.
436 /// If its false, we expect a sentinel value (0) after it.
437 ///
438 /// It may fail if there are not enough bytes to read the value or `self` cannot be read.
439 ///
440 /// ```rust
441 /// use std::io::Cursor;
442 ///
443 /// use rpfm_lib::binary::ReadBytes;
444 ///
445 /// let data = vec![1, 10, 0, 0, 0, 2];
446 /// let mut cursor = Cursor::new(data);
447 /// let data = cursor.read_optional_i32().unwrap();
448 ///
449 /// assert_eq!(data, 10);
450 /// assert_eq!(cursor.read_optional_i32().is_err(), true);
451 ///
452 /// # assert_eq!(ReadBytes::read_optional_i32(&mut Cursor::new([1, 10])).is_err(), true);
453 /// ```
454 fn read_optional_i32(&mut self) -> Result<i32> {
455 let _ = self.read_bool()?;
456 self.read_i32()
457 }
458
459 /// This function tries to read an optional i64 value from `self`.
460 ///
461 /// The value is preceeded by a bool. If the bool is true, we expect a value after it.
462 /// If its false, we expect a sentinel value (0) after it.
463 ///
464 /// It may fail if there are not enough bytes to read the value or `self` cannot be read.
465 ///
466 /// ```rust
467 /// use std::io::Cursor;
468 ///
469 /// use rpfm_lib::binary::ReadBytes;
470 ///
471 /// let data = vec![1, 10, 0, 0, 0, 0, 0, 0, 0, 2];
472 /// let mut cursor = Cursor::new(data);
473 /// let data = cursor.read_optional_i64().unwrap();
474 ///
475 /// assert_eq!(data, 10);
476 /// assert_eq!(cursor.read_optional_i64().is_err(), true);
477 ///
478 /// # assert_eq!(ReadBytes::read_optional_i64(&mut Cursor::new([1, 10])).is_err(), true);
479 /// ```
480 fn read_optional_i64(&mut self) -> Result<i64> {
481 let _ = self.read_bool()?;
482 self.read_i64()
483 }
484
485 /// This function tries to read an f16 value from `self`.
486 ///
487 /// It may fail if there are not enough bytes to read the value or `self` cannot be read.
488 ///
489 /// ```rust
490 /// use std::io::Cursor;
491 ///
492 /// use rpfm_lib::binary::ReadBytes;
493 ///
494 /// let data = vec![32, 65];
495 /// let mut cursor = Cursor::new(data);
496 /// let data = cursor.read_f16().unwrap();
497 ///
498 /// assert_eq!(data, half::f16::from_f32(2.5625));
499 /// assert_eq!(cursor.read_f16().is_err(), true);
500 /// ```
501 fn read_f16(&mut self) -> Result<half::f16> {
502 Ok(half::f16::from_bits(self.read_u16()?))
503 }
504
505 /// This function tries to read an f32 value from `self`.
506 ///
507 /// It may fail if there are not enough bytes to read the value or `self` cannot be read.
508 ///
509 /// ```rust
510 /// use std::io::Cursor;
511 ///
512 /// use rpfm_lib::binary::ReadBytes;
513 ///
514 /// let data = vec![0, 0, 32, 65];
515 /// let mut cursor = Cursor::new(data);
516 /// let data = cursor.read_f32().unwrap();
517 ///
518 /// assert_eq!(data, 10.0);
519 /// assert_eq!(cursor.read_f32().is_err(), true);
520 /// ```
521 fn read_f32(&mut self) -> Result<f32> {
522 ReadBytesExt::read_f32::<LittleEndian>(self).map_err(From::from)
523 }
524
525 /// This function tries to read an f32 value encoded in a single byte from `self`.
526 ///
527 /// It may fail if there are not enough bytes to read the value or `self` cannot be read.
528 ///
529 /// ```rust
530 /// use std::io::Cursor;
531 ///
532 /// use rpfm_lib::binary::ReadBytes;
533 ///
534 /// let data = vec![32];
535 /// let mut cursor = Cursor::new(data);
536 /// let data = cursor.read_f32_normal_from_u8().unwrap();
537 ///
538 /// assert_eq!(data, -0.7490196);
539 /// assert_eq!(cursor.read_f32_normal_from_u8().is_err(), true);
540 /// ```
541 fn read_f32_normal_from_u8(&mut self) -> Result<f32> {
542 Ok(self.read_u8()? as f32 / 255.0 * 2.0 - 1.0)
543 }
544
545 /// This function tries to read an f64 value from `self`.
546 ///
547 /// It may fail if there are not enough bytes to read the value or `self` cannot be read.
548 ///
549 /// ```rust
550 /// use std::io::Cursor;
551 ///
552 /// use rpfm_lib::binary::ReadBytes;
553 ///
554 /// let data = vec![0, 0, 0, 0, 0, 0, 36, 64];
555 /// let mut cursor = Cursor::new(data);
556 /// let data = cursor.read_f64().unwrap();
557 ///
558 /// assert_eq!(data, 10.0);
559 /// assert_eq!(cursor.read_f64().is_err(), true);
560 /// ```
561 fn read_f64(&mut self) -> Result<f64> {
562 ReadBytesExt::read_f64::<LittleEndian>(self).map_err(From::from)
563 }
564
565 /// This function tries to read an UTF-8 String value of the provided `size` from `self`.
566 ///
567 /// It may fail if there are not enough bytes to read the value, the value contains invalid
568 /// characters for an UTF-8 String, or `self` cannot be read.
569 ///
570 /// ```rust
571 /// use std::io::Cursor;
572 ///
573 /// use rpfm_lib::binary::ReadBytes;
574 ///
575 /// let data = vec![87, 97, 104, 97, 104, 97, 104, 97, 104, 97];
576 /// let mut cursor = Cursor::new(data);
577 /// let data = cursor.read_string_u8(10).unwrap();
578 ///
579 /// assert_eq!(data, "Wahahahaha");
580 /// assert_eq!(cursor.read_string_u8(10).is_err(), true);
581 /// ```
582 fn read_string_u8(&mut self, size: usize) -> Result<String> {
583 let mut data = vec![0; size];
584 self.read_exact(&mut data)?;
585 String::from_utf8(data).map_err(From::from)
586 }
587
588 /// This function tries to read an ISO-8859-15 String value of the provided `size` from `self`.
589 ///
590 /// It may fail if there are not enough bytes to read the value or `self` cannot be read.
591 ///
592 /// ```rust
593 /// use std::io::Cursor;
594 ///
595 /// use rpfm_lib::binary::ReadBytes;
596 ///
597 /// let data = vec![87, 97, 104, 97, 255, 104, 97, 104, 97, 104, 97];
598 /// let mut cursor = Cursor::new(data);
599 /// let data = cursor.read_string_u8_iso_8859_15(11).unwrap();
600 ///
601 /// assert_eq!(data, "Wahaÿhahaha");
602 /// assert_eq!(cursor.read_string_u8_iso_8859_15(10).is_err(), true);
603 /// ```
604 fn read_string_u8_iso_8859_15(&mut self, size: usize) -> Result<String> {
605 let mut data = vec![0; size];
606 self.read_exact(&mut data)?;
607
608 ISO_8859_15.decode_without_bom_handling_and_without_replacement(&data)
609 .ok_or(RLibError::DecodingString0TeminatedNo0Error)
610 .map(|s| s.to_string())
611 }
612
613 /// This function tries to read a 00-Padded UTF-8 String value of the provided `size` from `self`.
614 ///
615 /// Note that `size` here is the full length of the String, including the 00 bytes that act as padding.
616 ///
617 /// It may fail if there are not enough bytes to read the value, the value contains invalid
618 /// characters for an UTF-8 String, or `self` cannot be read.
619 ///
620 /// ```rust
621 /// use std::io::Cursor;
622 ///
623 /// use rpfm_lib::binary::ReadBytes;
624 ///
625 /// let data = vec![87, 97, 104, 97, 104, 97, 0, 0, 0, 0];
626 /// let mut cursor = Cursor::new(data);
627 /// let data = cursor.read_string_u8_0padded(10).unwrap();
628 ///
629 /// assert_eq!(data, "Wahaha");
630 /// assert_eq!(cursor.read_string_u8_0padded(10).is_err(), true);
631 /// ```
632 fn read_string_u8_0padded(&mut self, size: usize) -> Result<String> {
633 let mut data = vec![0; size];
634 self.read_exact(&mut data)?;
635
636 let size_no_zeros = data.iter().position(|x| *x == 0).unwrap_or(size);
637 String::from_utf8(data[..size_no_zeros].to_vec()).map_err(From::from)
638 }
639
640 /// This function tries to read a 00-Terminated (or NULL-Terminated) UTF-8 String value from `self`.
641 ///
642 /// It may fail if there are not enough bytes to read the value, the value contains invalid
643 /// characters for an UTF-8 String, the last value is not 00 or `self` cannot be read.
644 ///
645 /// ```rust
646 /// use std::io::Cursor;
647 ///
648 /// use rpfm_lib::binary::ReadBytes;
649 ///
650 /// let data = vec![87, 97, 104, 97, 104, 97, 104, 97, 0];
651 /// let mut cursor = Cursor::new(data);
652 /// let data = cursor.read_string_u8_0terminated().unwrap();
653 ///
654 /// assert_eq!(data, "Wahahaha");
655 /// assert_eq!(cursor.read_string_u8_0terminated().is_err(), true);
656 /// ```
657 fn read_string_u8_0terminated(&mut self) -> Result<String> {
658
659 // So, reads are expensive, so instead of reading byte by byte, we read a bunch of them
660 // and start searching with memchr. If we can't find anything, read another bunch and try again.
661 let mut buf = [0; 512];
662 let mut data = vec![];
663 let mut curr_pos = 0u64;
664 let mut end_pos = 0u64;
665 let mut found = false;
666
667 loop {
668 let read = self.read(&mut buf);
669 match read {
670 Ok(0) => break,
671 Ok(read_bytes) => {
672 if let Some(pos) = memchr::memchr(0, &buf[..read_bytes]) {
673
674 // If we found a 00, get the final "read" position, the final position of the 00 byte,
675 // and mark the byte as found.
676 end_pos = curr_pos + read_bytes as u64;
677 curr_pos += pos as u64;
678 data.extend_from_slice(&buf[..pos]);
679 found = true;
680 break;
681 } else {
682 curr_pos += read_bytes as u64;
683 data.extend_from_slice(&buf);
684 }
685 }
686
687 // If there is any error, just return it.
688 Err(error) => return Err(error)?,
689 }
690 }
691
692 // If we exited without finding the 00 byte, return an error.
693 if !found {
694 return Err(RLibError::DecodingString0TeminatedNo0Error);
695 }
696
697 // Move the cursor to the end of the value, so we can continue reading.
698 // -1 because we need to end after the 00 byte.
699 let new_pos = (end_pos - curr_pos - 1) as i64;
700 self.seek(SeekFrom::Current(-new_pos))?;
701
702 // Get a String from it. Lossy because older games have packs with broken symbols in their paths.
703 Ok(String::from_utf8_lossy(&data).to_string())
704 }
705
706 /// This function tries to read a Sized UTF-8 String value from `self`.
707 ///
708 /// In Sized Strings, the first two values of the data are the size in Characters of the string,
709 /// followed by the String itself.
710 ///
711 /// It may fail if there are not enough bytes to read the value, the value contains invalid
712 /// characters for an UTF-8 String, or `self` cannot be read.
713 ///
714 /// ```rust
715 /// use std::io::Cursor;
716 ///
717 /// use rpfm_lib::binary::ReadBytes;
718 ///
719 /// let data = vec![10, 0, 87, 97, 104, 97, 104, 97, 104, 97, 104, 97];
720 /// let mut cursor = Cursor::new(data);
721 /// let data = cursor.read_sized_string_u8().unwrap();
722 ///
723 /// assert_eq!(data, "Wahahahaha");
724 /// assert_eq!(cursor.read_sized_string_u8().is_err(), true);
725 /// ```
726 fn read_sized_string_u8(&mut self) -> Result<String> {
727 if let Ok(size) = self.read_u16() {
728 self.read_string_u8(size as usize)
729 }
730 else {
731 Err(RLibError::DecodingStringSizeError("UTF-8 String".to_owned()))
732 }
733 }
734
735 /// This function tries to read a Sized UTF-8 String value from `self`.
736 ///
737 /// In these particular Sized Strings, the first four values of the data are the size in Characters of the string,
738 /// followed by the String itself.
739 ///
740 /// It may fail if there are not enough bytes to read the value, the value contains invalid
741 /// characters for an UTF-8 String, or `self` cannot be read.
742 ///
743 /// ```rust
744 /// use std::io::Cursor;
745 ///
746 /// use rpfm_lib::binary::ReadBytes;
747 ///
748 /// let data = vec![10, 0, 0, 0, 87, 97, 104, 97, 104, 97, 104, 97, 104, 97];
749 /// let mut cursor = Cursor::new(data);
750 /// let data = cursor.read_sized_string_u8_u32().unwrap();
751 ///
752 /// assert_eq!(data, "Wahahahaha");
753 /// assert_eq!(cursor.read_sized_string_u8_u32().is_err(), true);
754 /// ```
755 fn read_sized_string_u8_u32(&mut self) -> Result<String> {
756 if let Ok(size) = self.read_u32() {
757 self.read_string_u8(size as usize)
758 }
759 else {
760 Err(RLibError::DecodingStringSizeError("UTF-8 String".to_owned()))
761 }
762 }
763
764 /// This function tries to read an Optional UTF-8 String value from `self`.
765 ///
766 /// In Optional Strings, the first byte is a boolean. If true, it's followed by a Sized String.
767 /// If false, then there is no more data after the boolean.
768 ///
769 /// It may fail if there are not enough bytes to read the value, the first value is not a boolean,
770 /// the value contains invalid, characters for an UTF-8 String, or `self` cannot be read.
771 ///
772 /// ```rust
773 /// use std::io::Cursor;
774 ///
775 /// use rpfm_lib::binary::ReadBytes;
776 ///
777 /// let data = vec![1, 10, 0, 87, 97, 104, 97, 104, 97, 104, 97, 104, 97];
778 /// let mut cursor = Cursor::new(data);
779 /// let data = cursor.read_optional_string_u8().unwrap();
780 ///
781 /// assert_eq!(data, "Wahahahaha");
782 /// assert_eq!(cursor.read_optional_string_u8().is_err(), true);
783 /// ```
784 fn read_optional_string_u8(&mut self) -> Result<String> {
785 let is = self.read_bool()
786 .map_err(|_| RLibError::DecodingOptionalStringBoolError("UTF-8 Optional String".to_owned()))?;
787
788 if is {
789 self.read_sized_string_u8()
790 } else {
791 Ok(String::new())
792 }
793 }
794
795 /// This function tries to read an UTF-16 String value of the provided `size` (in bytes) from `self`.
796 ///
797 /// It may fail if there are not enough bytes to read the value, the value contains invalid
798 /// characters for an UTF-16 String, or `self` cannot be read.
799 ///
800 /// ```rust
801 /// use std::io::Cursor;
802 ///
803 /// use rpfm_lib::binary::ReadBytes;
804 ///
805 /// let data = vec![87, 0, 97, 0, 104, 0, 97, 0, 104, 0, 97, 0];
806 /// let mut cursor = Cursor::new(data);
807 /// let data = cursor.read_string_u16(12).unwrap();
808 ///
809 /// assert_eq!(data, "Wahaha");
810 /// assert_eq!(cursor.read_string_u16(12).is_err(), true);
811 /// ```
812 fn read_string_u16(&mut self, size: usize) -> Result<String> {
813 if size % 2 == 1 {
814 return Err(RLibError::DecodeUTF16UnevenInputError(size));
815 }
816 let mut data = vec![0; size];
817 self.read_exact(&mut data)?;
818
819 Ok(UTF_16LE.decode(&data).0.to_string())
820 }
821
822 /// This function tries to read a 00-Padded UTF-16 String value of the provided `size` from `self`.
823 ///
824 /// Note that `size` here is the full length of the String in bytes, including the 00 bytes that act as padding.
825 ///
826 /// It may fail if there are not enough bytes to read the value, the value contains invalid
827 /// characters for an UTF-16 String, or `self` cannot be read.
828 ///
829 /// ```rust
830 /// use std::io::Cursor;
831 ///
832 /// use rpfm_lib::binary::ReadBytes;
833 ///
834 /// let data = vec![87, 0, 97, 0, 104, 0, 97, 0, 104, 0, 97, 0, 0, 0, 0, 0, 0, 0, 0, 0];
835 /// let mut cursor = Cursor::new(data);
836 /// let data = cursor.read_string_u16_0padded(20).unwrap();
837 ///
838 /// assert_eq!(data, "Wahaha");
839 /// assert_eq!(cursor.read_string_u16_0padded(20).is_err(), true);
840 /// ```
841 fn read_string_u16_0padded(&mut self, size: usize) -> Result<String> {
842 let mut data = vec![0; size];
843 self.read_exact(&mut data)?;
844
845 let size_no_zeros = (0..size.wrapping_div(2)).position(|x| data[x * 2] == 0).unwrap_or(size.wrapping_div(2));
846 Ok(UTF_16LE.decode(&data[..size_no_zeros * 2]).0.to_string())
847 }
848
849 /// This function tries to read a 00-Terminated (or NULL-Terminated) UTF-16 String value from `self`.
850 ///
851 /// It may fail if there are not enough bytes to read the value, the value contains invalid
852 /// characters for an UTF-16 String, the last value is not 00 or `self` cannot be read.
853 ///
854 /// ```rust
855 /// use std::io::Cursor;
856 ///
857 /// use rpfm_lib::binary::ReadBytes;
858 ///
859 /// let data = vec![87, 00, 97, 00, 104, 00, 97, 00, 104, 00, 97, 00, 104, 00, 97, 00, 00, 00];
860 /// let mut cursor = Cursor::new(data);
861 /// let data = cursor.read_string_u16_0terminated().unwrap();
862 ///
863 /// assert_eq!(data, "Wahahaha");
864 /// assert_eq!(cursor.read_string_u16_0terminated().is_err(), true);
865 /// ```
866 fn read_string_u16_0terminated(&mut self) -> Result<String> {
867
868 // So, reads are expensive, so instead of reading byte by byte, we read a bunch of them
869 // and start searching with a chunk iterator. If we can't find anything, read another bunch and try again.
870 let mut buf = [0; 512];
871 let mut data = vec![];
872 let mut curr_pos = 0u64;
873 let mut end_pos = 0u64;
874 let mut found = false;
875
876 loop {
877 let read = self.read(&mut buf);
878 match read {
879 Ok(0) => break,
880 Ok(read_bytes) => {
881
882 if let Some(pos) = buf[..read_bytes].iter().chunks(2).into_iter().position(|chunk| {
883 let chunk = chunk.collect::<Vec<_>>();
884 chunk.len() == 2 && *chunk[0] == 0 && *chunk[1] == 0
885 }) {
886
887 // If we found a 00 00, get the final "read" position, the final position of the 00 byte,
888 // and mark the byte as found.
889 end_pos = curr_pos + read_bytes as u64;
890 curr_pos += pos as u64 * 2;
891 data.extend_from_slice(&buf[..pos * 2]);
892 found = true;
893 break;
894 } else {
895 curr_pos += read_bytes as u64;
896 data.extend_from_slice(&buf);
897 }
898 }
899
900 // If there is any error, just return it.
901 Err(error) => return Err(error)?,
902 }
903 }
904
905 // If we exited without finding the 00 byte, return an error.
906 if !found {
907 return Err(RLibError::DecodingString0TeminatedNo0Error);
908 }
909
910 // Move the cursor to the end of the value, so we can continue reading.
911 // -2 because we need to end after the last 00 byte.
912 let new_pos = end_pos as i64 - curr_pos as i64 - 2;
913 self.seek(SeekFrom::Current(-new_pos))?;
914
915 // Get a String from it. Lossy because older games have packs with broken symbols in their paths.
916 Ok(UTF_16LE.decode(&data).0.to_string())
917 }
918
919 /// This function tries to read a Sized UTF-16 String value from `self`.
920 ///
921 /// In Sized Strings, the first two values of the data are the size in Characters of the string,
922 /// followed by the String itself.
923 ///
924 /// It may fail if there are not enough bytes to read the value, the value contains invalid
925 /// characters for an UTF-16 String, or `self` cannot be read.
926 ///
927 /// ```rust
928 /// use std::io::Cursor;
929 ///
930 /// use rpfm_lib::binary::ReadBytes;
931 ///
932 /// let data = vec![4, 0, 87, 0, 97, 0, 104, 0, 97, 0];
933 /// let mut cursor = Cursor::new(data);
934 /// let data = cursor.read_sized_string_u16().unwrap();
935 ///
936 /// assert_eq!(data, "Waha");
937 /// assert_eq!(cursor.read_sized_string_u16().is_err(), true);
938 /// ```
939 fn read_sized_string_u16(&mut self) -> Result<String> {
940 if let Ok(size) = self.read_u16() {
941 self.read_string_u16(size.wrapping_mul(2) as usize)
942 }
943 else {
944 Err(RLibError::DecodingStringSizeError("UTF-16 String".to_owned()))
945 }
946 }
947
948 /// This function tries to read a Sized UTF-16 String value from `self`.
949 ///
950 /// In these particular Sized Strings, the first four values of the data are the size in Characters of the string,
951 /// followed by the String itself.
952 ///
953 /// It may fail if there are not enough bytes to read the value, the value contains invalid
954 /// characters for an UTF-16 String, or `self` cannot be read.
955 ///
956 /// ```rust
957 /// use std::io::Cursor;
958 ///
959 /// use rpfm_lib::binary::ReadBytes;
960 ///
961 /// let data = vec![4, 0, 0, 0, 87, 0, 97, 0, 104, 0, 97, 0];
962 /// let mut cursor = Cursor::new(data);
963 /// let data = cursor.read_sized_string_u16_u32().unwrap();
964 ///
965 /// assert_eq!(data, "Waha");
966 /// assert_eq!(cursor.read_sized_string_u16_u32().is_err(), true);
967 /// ```
968 fn read_sized_string_u16_u32(&mut self) -> Result<String> {
969 if let Ok(size) = self.read_u32() {
970 self.read_string_u16(size.wrapping_mul(2) as usize)
971 }
972 else {
973 Err(RLibError::DecodingStringSizeError("UTF-16 String".to_owned()))
974 }
975 }
976
977 /// This function tries to read an Optional UTF-16 String value from `self`.
978 ///
979 /// In Optional Strings, the first byte is a boolean. If true, it's followed by a Sized String.
980 /// If false, then there is no more data after the boolean.
981 ///
982 /// It may fail if there are not enough bytes to read the value, the first value is not a boolean,
983 /// the value contains invalid, characters for an UTF-16 String, or `self` cannot be read.
984 ///
985 /// ```rust
986 /// use std::io::Cursor;
987 ///
988 /// use rpfm_lib::binary::ReadBytes;
989 ///
990 /// let data = vec![1, 4, 0, 87, 0, 97, 0, 104, 0, 97, 0];
991 /// let mut cursor = Cursor::new(data);
992 /// let data = cursor.read_optional_string_u16().unwrap();
993 ///
994 /// assert_eq!(data, "Waha");
995 /// assert_eq!(cursor.read_optional_string_u16().is_err(), true);
996 /// ```
997 fn read_optional_string_u16(&mut self) -> Result<String> {
998 let is = self.read_bool()
999 .map_err(|_| RLibError::DecodingOptionalStringBoolError("UTF-16 Optional String".to_owned()))?;
1000
1001 if is {
1002 self.read_sized_string_u16()
1003 } else {
1004 Ok(String::new())
1005 }
1006 }
1007
1008 /// This function tries to read a Hex-Encoded RGB Colour from `self`.
1009 ///
1010 /// It may fail if there are not enough bytes to read the value or `self` cannot be read.
1011 ///
1012 /// ```rust
1013 /// use std::io::Cursor;
1014 ///
1015 /// use rpfm_lib::binary::ReadBytes;
1016 ///
1017 /// let data = vec![0xFF, 0x04, 0x05, 0x00];
1018 /// let mut cursor = Cursor::new(data);
1019 /// let data = cursor.read_string_colour_rgb().unwrap();
1020 ///
1021 /// assert_eq!(data, "0504FF");
1022 /// assert_eq!(cursor.read_string_colour_rgb().is_err(), true);
1023 /// ```
1024 fn read_string_colour_rgb(&mut self) -> Result<String> {
1025 let value = self.read_u32()?;
1026
1027 // Padding to 8 zeros so we don't lose the first one, then remove the last two zeros (alpha?).
1028 // REMEMBER, FORMAT ENCODED IS BBGGRR00.
1029 Ok(format!("{value:06X?}"))
1030 }
1031
1032 /// This function tries to read a Vector of 2 u8 values from `self`.
1033 ///
1034 /// It may fail if there are not enough bytes to read the value or `self` cannot be read.
1035 ///
1036 /// ```rust
1037 /// use nalgebra::Vector2;
1038 /// use std::io::Cursor;
1039 ///
1040 /// use rpfm_lib::binary::ReadBytes;
1041 ///
1042 /// let data = vec![0x0A, 0x0A];
1043 /// let mut cursor = Cursor::new(data);
1044 /// let data = cursor.read_vector_2_u8().unwrap();
1045 ///
1046 /// assert_eq!(data, Vector2::new(10, 10));
1047 /// assert_eq!(cursor.read_vector_2_u8().is_err(), true);
1048 /// ```
1049 fn read_vector_2_u8(&mut self) -> Result<Vector2<u8>> {
1050 let x = self.read_u8()?;
1051 let y = self.read_u8()?;
1052
1053 Ok(Vector2::new(x, y))
1054 }
1055
1056 /// This function tries to read a Vector of 2 f32 percentage values from `self`.
1057 ///
1058 /// It may fail if there are not enough bytes to read the value or `self` cannot be read.
1059 ///
1060 /// ```rust
1061 /// use nalgebra::Vector2;
1062 /// use std::io::Cursor;
1063 ///
1064 /// use rpfm_lib::binary::ReadBytes;
1065 ///
1066 /// let data = vec![0x0A, 0x0A];
1067 /// let mut cursor = Cursor::new(data);
1068 /// let data = cursor.read_vector_2_f32_pct_from_vector_2_u8().unwrap();
1069 ///
1070 /// assert_eq!(data, Vector2::new(0.039215688, 0.039215688));
1071 /// assert_eq!(cursor.read_vector_2_f32_pct_from_vector_2_u8().is_err(), true);
1072 /// ```
1073 fn read_vector_2_f32_pct_from_vector_2_u8(&mut self) -> Result<Vector2<f32>> {
1074 let x = self.read_u8()? as f32;
1075 let y = self.read_u8()? as f32;
1076
1077 Ok(Vector2::new(x / 255.0, y / 255.0))
1078 }
1079
1080 /// This function tries to read a Vector of 2 f32 values from `self`.
1081 ///
1082 /// It may fail if there are not enough bytes to read the value or `self` cannot be read.
1083 ///
1084 /// ```rust
1085 /// use nalgebra::Vector2;
1086 /// use std::io::Cursor;
1087 ///
1088 /// use rpfm_lib::binary::ReadBytes;
1089 ///
1090 /// let data = vec![0x0A, 0x0A, 0x0A, 0x0A];
1091 /// let mut cursor = Cursor::new(data);
1092 /// let data = cursor.read_vector_2_f32_from_vector_2_f16().unwrap();
1093 ///
1094 /// assert_eq!(data, Vector2::new(0.00018429756, 0.00018429756));
1095 /// assert_eq!(cursor.read_vector_2_f32_from_vector_2_f16().is_err(), true);
1096 /// ```
1097 fn read_vector_2_f32_from_vector_2_f16(&mut self) -> Result<Vector2<f32>> {
1098 let x = self.read_f16()?.to_f32();
1099 let y = self.read_f16()?.to_f32();
1100
1101 Ok(Vector2::new(x, y))
1102 }
1103
1104 /// This function tries to read a Vector of 4 u8 values from `self`.
1105 ///
1106 /// It may fail if there are not enough bytes to read the value or `self` cannot be read.
1107 ///
1108 /// ```rust
1109 /// use nalgebra::Vector4;
1110 /// use std::io::Cursor;
1111 ///
1112 /// use rpfm_lib::binary::ReadBytes;
1113 ///
1114 /// let data = vec![0x0A, 0x0A, 0x0A, 0x0A];
1115 /// let mut cursor = Cursor::new(data);
1116 /// let data = cursor.read_vector_4_u8().unwrap();
1117 ///
1118 /// assert_eq!(data, Vector4::new(10, 10, 10, 10));
1119 /// assert_eq!(cursor.read_vector_4_u8().is_err(), true);
1120 /// ```
1121 fn read_vector_4_u8(&mut self) -> Result<Vector4<u8>> {
1122 let x = self.read_u8()?;
1123 let y = self.read_u8()?;
1124 let z = self.read_u8()?;
1125 let w = self.read_u8()?;
1126
1127 Ok(Vector4::new(x, y, z, w))
1128 }
1129
1130 /// This function tries to read a Vector of 4 f32 values from `self`.
1131 ///
1132 /// It may fail if there are not enough bytes to read the value or `self` cannot be read.
1133 ///
1134 /// ```rust
1135 /// use nalgebra::Vector4;
1136 /// use std::io::Cursor;
1137 ///
1138 /// use rpfm_lib::binary::ReadBytes;
1139 ///
1140 /// let data = vec![
1141 /// 0x00, 0x00, 0xFF, 0x3F,
1142 /// 0x00, 0x00, 0xFF, 0x3F,
1143 /// 0x00, 0x00, 0xFF, 0x3F,
1144 /// 0x00, 0x00, 0xFF, 0x3F
1145 /// ];
1146 /// let mut cursor = Cursor::new(data);
1147 /// let data = cursor.read_vector_4_f32().unwrap();
1148 ///
1149 /// assert_eq!(data, Vector4::new(1.9921875, 1.9921875, 1.9921875, 1.9921875));
1150 /// assert_eq!(cursor.read_vector_4_f32().is_err(), true);
1151 /// ```
1152 fn read_vector_4_f32(&mut self) -> Result<Vector4<f32>> {
1153 let x = self.read_f32()?;
1154 let y = self.read_f32()?;
1155 let z = self.read_f32()?;
1156 let w = self.read_f32()?;
1157
1158 Ok(Vector4::new(x, y, z, w))
1159 }
1160
1161 /// This function tries to read a Vector of 4 f32 values from a Vector of 3 f32 values from`self`.
1162 ///
1163 /// It may fail if there are not enough bytes to read the value or `self` cannot be read.
1164 ///
1165 /// ```rust
1166 /// use nalgebra::Vector4;
1167 /// use std::io::Cursor;
1168 ///
1169 /// use rpfm_lib::binary::ReadBytes;
1170 ///
1171 /// let data = vec![
1172 /// 0x00, 0x00, 0xFF, 0x3F,
1173 /// 0x00, 0x00, 0xFF, 0x3F,
1174 /// 0x00, 0x00, 0xFF, 0x3F
1175 /// ];
1176 /// let mut cursor = Cursor::new(data);
1177 /// let data = cursor.read_vector_4_f32_from_vec_3_f32().unwrap();
1178 ///
1179 /// assert_eq!(data, Vector4::new(1.9921875, 1.9921875, 1.9921875, 0.0));
1180 /// assert_eq!(cursor.read_vector_4_f32_from_vec_3_f32().is_err(), true);
1181 /// ```
1182 fn read_vector_4_f32_from_vec_3_f32(&mut self) -> Result<Vector4<f32>> {
1183 let x = self.read_f32()?;
1184 let y = self.read_f32()?;
1185 let z = self.read_f32()?;
1186
1187 Ok(Vector4::new(x, y, z, 0.0))
1188 }
1189
1190 /// This function tries to read a Vector of 4 f32 normalized values from `self`.
1191 ///
1192 /// It may fail if there are not enough bytes to read the value or `self` cannot be read.
1193 ///
1194 /// ```rust
1195 /// use nalgebra::Vector4;
1196 /// use std::io::Cursor;
1197 ///
1198 /// use rpfm_lib::binary::ReadBytes;
1199 ///
1200 /// let data = vec![0x0A, 0x0A, 0x0A, 0x0A];
1201 /// let mut cursor = Cursor::new(data);
1202 /// let data = cursor.read_vector_4_f32_normal_from_vector_4_u8().unwrap();
1203 ///
1204 /// assert_eq!(data, Vector4::new(-0.92156863, -0.92156863, -0.92156863, -0.92156863));
1205 /// assert_eq!(cursor.read_vector_4_f32_normal_from_vector_4_u8().is_err(), true);
1206 /// ```
1207 fn read_vector_4_f32_normal_from_vector_4_u8(&mut self) -> Result<Vector4<f32>> {
1208 let mut x = self.read_f32_normal_from_u8()?;
1209 let mut y = self.read_f32_normal_from_u8()?;
1210 let mut z = self.read_f32_normal_from_u8()?;
1211 let w = self.read_f32_normal_from_u8()?;
1212
1213 if w > 0.0 {
1214 x *= w;
1215 y *= w;
1216 z *= w;
1217 }
1218
1219 Ok(Vector4::new(x, y, z, w))
1220 }
1221
1222 /// This function tries to read a Vector of 4 f32 percentage values from `self`.
1223 ///
1224 /// It may fail if there are not enough bytes to read the value or `self` cannot be read.
1225 ///
1226 /// ```rust
1227 /// use nalgebra::Vector4;
1228 /// use std::io::Cursor;
1229 ///
1230 /// use rpfm_lib::binary::ReadBytes;
1231 ///
1232 /// let data = vec![0x0A, 0x0A, 0x0A, 0x0A];
1233 /// let mut cursor = Cursor::new(data);
1234 /// let data = cursor.read_vector_4_f32_pct_from_vector_4_u8().unwrap();
1235 ///
1236 /// assert_eq!(data, Vector4::new(0.039215688, 0.039215688, 0.039215688, 0.039215688));
1237 /// assert_eq!(cursor.read_vector_4_f32_pct_from_vector_4_u8().is_err(), true);
1238 /// ```
1239 fn read_vector_4_f32_pct_from_vector_4_u8(&mut self) -> Result<Vector4<f32>> {
1240 let x = self.read_u8()? as f32;
1241 let y = self.read_u8()? as f32;
1242 let z = self.read_u8()? as f32;
1243 let w = self.read_u8()? as f32;
1244
1245 Ok(Vector4::new(x / 255.0, y / 255.0, z / 255.0, w / 255.0))
1246 }
1247
1248 /// This function tries to read a Vector of 4 f32 normalized values from `self`.
1249 ///
1250 /// It may fail if there are not enough bytes to read the value or `self` cannot be read.
1251 ///
1252 /// ```rust
1253 /// use nalgebra::Vector4;
1254 /// use std::io::Cursor;
1255 ///
1256 /// use rpfm_lib::binary::ReadBytes;
1257 ///
1258 /// let data = vec![
1259 /// 0x0A, 0x3F,
1260 /// 0x0A, 0x3F,
1261 /// 0x0A, 0x3F,
1262 /// 0x0A, 0x3F
1263 /// ];
1264 /// let mut cursor = Cursor::new(data);
1265 /// let data = cursor.read_vector_4_f32_normal_from_vector_4_f16().unwrap();
1266 ///
1267 /// assert_eq!(data, Vector4::new(3.096775, 3.096775, 3.096775, 1.7597656));
1268 /// assert_eq!(cursor.read_vector_4_f32_normal_from_vector_4_f16().is_err(), true);
1269 /// ```
1270 fn read_vector_4_f32_normal_from_vector_4_f16(&mut self) -> Result<Vector4<f32>> {
1271 let mut x = self.read_f16()?.to_f32();
1272 let mut y = self.read_f16()?.to_f32();
1273 let mut z = self.read_f16()?.to_f32();
1274 let w = self.read_f16()?.to_f32();
1275
1276 if w != 0.0 {
1277 x *= w;
1278 y *= w;
1279 z *= w;
1280 }
1281
1282 Ok(Vector4::new(x, y, z, w))
1283 }
1284}
1285
1286// Automatic implementation for everything that implements `Read + Seek`.
1287impl<R: Read + Seek> ReadBytes for R {}