eolib.data¶
Utilities to read and write EO data types.
- class EoReader[source]¶
Bases:
objectA class for reading EO data from a sequence of bytes.
EoReaderfeatures a chunked reading mode, which is important for accurate emulation of the official game client.See the chunked reading documentation for more information.
- __init__(data)[source]¶
Creates a new EoReader instance for the specified data.
- Parameters:
data (
bytes) – The byte array containing the input data.
- slice(index=None, length=None) EoReader[source]¶
Creates a new EoReader whose input data is a shared subsequence of this reader’s data.
The input data of the new reader will start at position index in this reader and contain up to length bytes. The two reader’s position and chunked reading mode will be independent.
The new reader’s position will be zero, and its chunked reading mode will be false.
- Parameters:
index (
int|None, default:None) – The position in this reader at which the data of the new reader will start; must be non-negative. Defaults to the current reader position.length (
int|None, default:None) – The length of the shared subsequence of data to supply to the new reader; must be non-negative. Defaults to the length of the remaining data starting from index.
- Returns:
The new reader.
- Raises:
ValueError – If index or length is negative.
- get_bytes(length) bytearray[source]¶
Reads an array of raw bytes from the input data.
- Parameters:
length (
int) – The number of bytes to read.- Returns:
An array of raw bytes.
- get_char() int[source]¶
Reads an encoded 1-byte integer from the input data.
- Returns:
A decoded 1-byte integer.
- get_short() int[source]¶
Reads an encoded 2-byte integer from the input data.
- Returns:
A decoded 2-byte integer.
- get_three() int[source]¶
Reads an encoded 3-byte integer from the input data.
- Returns:
A decoded 3-byte integer.
- get_int() int[source]¶
Reads an encoded 4-byte integer from the input data.
- Returns:
A decoded 4-byte integer.
- get_fixed_string(length, padded=False) str[source]¶
Reads a string with a fixed length from the input data.
- Parameters:
- Returns:
A decoded string.
- Raises:
ValueError – If the length is negative.
- get_encoded_string() str[source]¶
Reads an encoded string from the input data.
- Returns:
A decoded string.
- get_fixed_encoded_string(length, padded=False) str[source]¶
Reads an encoded string with a fixed length from the input data.
- Parameters:
- Returns:
A decoded string.
- Raises:
ValueError – If the length is negative.
- property chunked_reading_mode: bool¶
Gets or sets the chunked reading mode for the reader.
In chunked reading mode:
The reader will treat
0xFFbytes as the end of the current chunk.next_chunk()can be called to move to the next chunk.
- property remaining: int¶
If chunked reading mode is enabled, gets the number of bytes remaining in the current chunk. Otherwise, gets the total number of bytes remaining in the input data.
- next_chunk() None[source]¶
Moves the reader position to the start of the next chunk in the input data.
- Raises:
RuntimeError – If not in chunked reading mode.
- class EoWriter[source]¶
Bases:
objectA class for writing EO data to a sequence of bytes.
- add_byte(value) None[source]¶
Adds a raw byte to the writer data.
- Parameters:
value (
int) – The byte to add.- Raises:
ValueError – If the value is above 0xFF.
- add_bytes(bytes) None[source]¶
Adds raw bytes to the writer data.
- Parameters:
bytes (
bytes) – The bytes to add.
- add_char(number) None[source]¶
Adds an encoded 1-byte integer to the writer data.
- Parameters:
number (
int) – The number to encode and add.- Raises:
ValueError – If the value is not below CHAR_MAX.
- add_short(number) None[source]¶
Adds an encoded 2-byte integer to the writer data.
- Parameters:
number (
int) – The number to encode and add.- Raises:
ValueError – If the value is not below SHORT_MAX.
- add_three(number) None[source]¶
Adds an encoded 3-byte integer to the writer data.
- Parameters:
number (
int) – The number to encode and add.- Raises:
ValueError – If the value is not below THREE_MAX.
- add_int(number) None[source]¶
Adds an encoded 4-byte integer to the writer data.
- Parameters:
number (
int) – The number to encode and add.- Raises:
ValueError – If the value is not below INT_MAX.
- add_string(string) None[source]¶
Adds a string to the writer data.
- Parameters:
string (
str) – The string to be added.
- add_fixed_string(string, length, padded=False) None[source]¶
Adds a fixed-length string to the writer data.
- add_encoded_string(string) None[source]¶
Adds an encoded string to the writer data.
- Parameters:
string (
str) – The string to be encoded and added.
- encode_number(number) bytes[source]¶
Encodes a number to a sequence of bytes.
- Parameters:
number (
int) – The number to encode.- Returns:
The encoded sequence of bytes.
- decode_number(encoded_number) int[source]¶
Decodes a number from a sequence of bytes.
- Parameters:
encoded_number (
bytes) – The sequence of bytes to decode.- Returns:
The decoded number.