eolib.data

Utilities to read and write EO data types.

class EoReader[source]

Bases: object

A class for reading EO data from a sequence of bytes.

EoReader features 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_byte() int[source]

Reads a raw byte from the input data.

Returns:

A raw byte.

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_string() str[source]

Reads a string from the input data.

Returns:

A string.

get_fixed_string(length, padded=False) str[source]

Reads a string with a fixed length from the input data.

Parameters:
  • length (int) – The length of the string.

  • padded (bool, default: False) – True if the string is padded with trailing 0xFF bytes.

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:
  • length (int) – The length of the string.

  • padded (bool, default: False) – True if the string is padded with trailing 0xFF bytes.

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 0xFF bytes 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.

property position: int

Gets the current position in the input data.

class EoWriter[source]

Bases: object

A class for writing EO data to a sequence of bytes.

__init__()[source]

Creates a new EoWriter with no data.

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.

Parameters:
  • string (str) – The string to be added.

  • length (int) – The expected length of the string.

  • padded (bool, default: False) – True if the string should be padded to the length with trailing 0xFF bytes. Defaults to False.

add_encoded_string(string) None[source]

Adds an encoded string to the writer data.

Parameters:

string (str) – The string to be encoded and added.

add_fixed_encoded_string(string, length, padded=False) None[source]

Adds a fixed-length encoded string to the writer data.

Parameters:
  • string (str) – The string to be encoded and added.

  • length (int) – The expected length of the string.

  • padded (bool, default: False) – True if the string should be padded to the length with trailing 0xFF bytes. Defaults to False.

property string_sanitization_mode: bool

Whether string sanitization is enabled for the writer.

to_bytearray() bytearray[source]

Gets the writer data as a byte array.

Returns:

A copy of the writer data as a byte array.

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.

encode_string(bytes) None[source]

Encodes a string by inverting the bytes and then reversing them.

This is an in-place operation.

Parameters:

bytes (bytearray) – The byte array to encode.

decode_string(bytes) None[source]

Decodes a string by reversing the bytes and then inverting them.

This is an in-place operation.

Parameters:

bytes (bytearray) – The byte array to decode.