pub struct Reader<'a> { /* private fields */ }Expand description
A cursor over data, read by the rules of one of UnityPy’s readers
(ReaderKind). Positions are absolute within data and signed, as a
Python Position (module docs): on the memory reader every read follows
the helper of its Python primitive at whatever position it is, negative
or past the end; the stream reader’s position is never negative.
Implementations§
Source§impl<'a> Reader<'a>
impl<'a> Reader<'a>
Sourcepub fn new(data: &'a [u8], endian: Endian) -> Self
pub fn new(data: &'a [u8], endian: Endian) -> Self
A memory reader at position 0 (what UnityPy reads bytes with).
Sourcepub fn with_kind(data: &'a [u8], endian: Endian, kind: ReaderKind) -> Self
pub fn with_kind(data: &'a [u8], endian: Endian, kind: ReaderKind) -> Self
A reader of kind at position 0.
Sourcepub fn kind(&self) -> ReaderKind
pub fn kind(&self) -> ReaderKind
Which reader’s rules it reads by.
Sourcepub fn set_position(&mut self, pos: i128) -> Result<(), Error>
pub fn set_position(&mut self, pos: i128) -> Result<(), Error>
An assignment to reader.Position: the memory reader takes any
position, negative or past the end; the stream reader refuses a
negative one or one at or above 2^63 (ReaderKind::seek), and
stays where it was.
Sourcepub fn set_endian(&mut self, endian: Endian)
pub fn set_endian(&mut self, endian: Endian)
Changes the byte order (a SerializedFile header switches it).
Sourcepub fn remaining(&self) -> usize
pub fn remaining(&self) -> usize
The bytes read(-1) would return from here: what a table’s growth may
count on (plan section 3.3). The same on both readers.
Sourcepub fn align(&mut self, n: u32) -> Result<(), Error>
pub fn align(&mut self, n: u32) -> Result<(), Error>
align_stream(n): py_align, without a bounds check, assigned
to the position: the memory reader takes any result; the stream
reader’s position is never negative, so the arithmetic is the same,
and it refuses a result at or above 2^63 as its seek does
(ReaderKind::align), staying where it was.
Sourcepub fn read_range(&mut self, n: i64) -> Result<Range<usize>, Error>
pub fn read_range(&mut self, n: i64) -> Result<Range<usize>, Error>
read(n), the range of data it returns: the memory reader’s
py_read slice, short or empty at the ends (read_bytes on a
truncated block, File.py:79), the position moved by n; or the
stream reader’s, which refuses an n below -1 and stops at the end
(ReaderKind::read).
Sourcepub fn read(&mut self, n: i64) -> Result<&'a [u8], Error>
pub fn read(&mut self, n: i64) -> Result<&'a [u8], Error>
read(n) as it is: Self::read_range’s bytes.
Sourcepub fn read_bytes(&mut self, n: usize) -> Result<&'a [u8], Error>
pub fn read_bytes(&mut self, n: usize) -> Result<&'a [u8], Error>
n bytes where UnityPy’s caller unpacks them whole (read(n), then
struct.unpack): what read(n) returns must hold n bytes, else
OutOfBounds; the position moves as read(n) moves it either way.
Sourcepub fn read_bytes_short(&mut self, n: usize) -> &'a [u8] ⓘ
pub fn read_bytes_short(&mut self, n: usize) -> &'a [u8] ⓘ
read(n) as it is for a size UnityPy’s own code fixes (3, 16 or 32
bytes), which neither reader refuses: short or empty at the ends.
Sourcepub fn read_string_to_null(&mut self) -> Result<&'a [u8], Error>
pub fn read_string_to_null(&mut self) -> Result<&'a [u8], Error>
read_string_to_null with the 32,767-byte window, by the reader’s
rule (py_string_to_null or the stream reader’s loop,
ReaderKind::string_to_null).
Sourcepub fn read_bool(&mut self) -> Result<bool, Error>
pub fn read_bool(&mut self) -> Result<bool, Error>
One byte as a bool (non-zero is true): unpack("?", read(1)).
Sourcepub fn read_u16(&mut self) -> Result<u16, Error>
pub fn read_u16(&mut self) -> Result<u16, Error>
u16 in the reader’s byte order: ReaderKind::unpack.
Sourcepub fn read_i16(&mut self) -> Result<i16, Error>
pub fn read_i16(&mut self) -> Result<i16, Error>
i16 in the reader’s byte order: ReaderKind::unpack.
Sourcepub fn read_u32(&mut self) -> Result<u32, Error>
pub fn read_u32(&mut self) -> Result<u32, Error>
u32 in the reader’s byte order: ReaderKind::unpack.
Sourcepub fn read_i32(&mut self) -> Result<i32, Error>
pub fn read_i32(&mut self) -> Result<i32, Error>
i32 in the reader’s byte order: ReaderKind::unpack.
Sourcepub fn read_u64(&mut self) -> Result<u64, Error>
pub fn read_u64(&mut self) -> Result<u64, Error>
u64 in the reader’s byte order: ReaderKind::unpack.
Sourcepub fn read_i64(&mut self) -> Result<i64, Error>
pub fn read_i64(&mut self) -> Result<i64, Error>
i64 in the reader’s byte order: ReaderKind::unpack.
Sourcepub fn read_short_array<const N: usize>(&mut self) -> ShortArray<N>
pub fn read_short_array<const N: usize>(&mut self) -> ShortArray<N>
read(N) kept as it is (ShortArray): a GUID or a hash, which
UnityPy stores without checking its length, so a short read (on the
memory reader at a position in -N..0; at the end on either) is no
error; the position moves as read(N) moves it.
Sourcepub fn read_array<const N: usize>(&mut self) -> Result<[u8; N], Error>
pub fn read_array<const N: usize>(&mut self) -> Result<[u8; N], Error>
A fixed-size byte array: read(N), which must hold N bytes where
UnityPy’s caller needs them all (it unpacks them); where it keeps
them as they are, Self::read_short_array.
Sourcepub fn read_i32_array(
&mut self,
meter: &Arc<Meter>,
) -> Result<MeteredVec<i32>, Error>
pub fn read_i32_array( &mut self, meter: &Arc<Meter>, ) -> Result<MeteredVec<i32>, Error>
read_int_array (EndianBinaryReader.py:182, read_array_struct at
:164-168 and :258-264): an i32 count read as the reader’s own
read_int (ReaderKind::unpack), then the array: on the memory
reader struct.unpack_from of that many i32 at the new position (a
negative one counts from the end), the position moving by
4 * count; on the stream reader struct.unpack(read(4 * count)),
which moves it by what it read, even when that is short. A negative
count raises (Struct("<-1i") is a bad format) with the position
after the count, as does an array the bytes do not hold (the stream
reader’s has moved). The array is a vector on meter, the input’s,
charged after the bytes are checked and before it is allocated, so a
count allocates nothing the bytes do not back and the array counts
against its input while it is kept (round 5: this returned a plain
Vec, the one input-sized allocation a parser made uncharged); a
refused charge is the meter’s or the budget’s error, with the
position after the count. Implemented by the scaffold; the cases are
recorded from UnityPy 1.25.2 (read_i32_array_reads_as_unitypy_and_charges_the_meter).