file_processor¶
Submodule for low level Terraria save file operations, and in particular, the FileProcessor class.
- class lihzahrd.terraria.utils.file_processor.FileProcessor(stream: BinaryIO)¶
Helper class for reading and writing C# data structures to Terraria save files.
- read(structure: Struct) Any¶
Read the given structure from
stream.- Parameters:
structure – The structure to read.
- Returns:
The value of the read structure (will probably be a
tuple).
- write(structure: Struct, value: Iterable[Any]) None¶
Write the given structure from
stream.- Parameters:
structure – The structure to write.
value – The value of the structure.
- INT_TO_BITS: dict[int, tuple[bool, bool, bool, bool, bool, bool, bool, bool]]¶
Cache associating each
intbetween 0 and 255 to atupleof 8boolelements of its bits in little-endian order.Example
>>> FileProcessor.INT_TO_BITS[0] (False, False, False, False, False, False, False, False) >>> FileProcessor.INT_TO_BITS[1] (True, False, False, False, False, False, False, False) >>> FileProcessor.INT_TO_BITS[128] (False, False, False, False, False, False, False, True)
Note
This is necessary to speed up bits processing, which would otherwise take a long time.
- read_bits() tuple[bool, bool, bool, bool, bool, bool, bool, bool]¶
Read a
BYTEfromstream, then get itsINT_TO_BITS.
- write_bits(value: tuple[bool, bool, bool, bool, bool, bool, bool, bool]) None¶
Convert a series of
INT_TO_BITSto aBYTE, then write it tostream.
- LONG_MIN: Literal[-9223372036854775808] = -9223372036854775808¶
The minimum value that a
LONGcan have.
- LONG_MAX: Literal[9223372036854775807] = 9223372036854775807¶
The maximum value that a
LONGcan have.
- ULONG_MAX: Literal[18446744073709551615] = 18446744073709551615¶
The maximum value that a
ULONGcan have.
- read_double() float¶
-
Note
Since Python’s
floatis 64-bit, no data is lost in the conversion.- Returns:
The read value, as a
float.
- read_bytes_count(count: int) bytes¶
Read a fixed number of bytes from
stream.- Parameters:
count – The number of bytes to read.
- Returns:
The read
bytes.
- read_bytes_until(address: int | None) bytes¶
Read bytes from
streamuntil the given address.- Parameters:
address – The address to read bytes until.
- Returns:
The read
bytes.
- read_string_raw(count: int) str¶
Read a fixed number of bytes into a
latin1string.- Parameters:
count – The number of bytes to read.
- Returns:
The read bytes, as a
strwithlatin1encoding.
- write_string_raw(value: str) None¶
-
Warning
This does not write the size of the string!
- Parameters:
value – The
strto write.
- read_string_variable() str¶
Read a
strof variable length fromstream:First,
read_uleb128()is called to determine the size of the string in bytes.Then,
read_string_raw()is called to read the actual string.
- Returns:
The read
str.
- write_string_variable(value: str) None¶
Write a
strof variable length tostream:First, the string is converted to
bytesusing thelatin1encoding.Then, the size of the string in bytes is written with
write_uleb128().Finally, the
bytesare written withwrite_bytes().
- Parameters:
value – The
strto write.
- write_uuid(value: UUID) None¶
Write the 16 bytes corresponding to the given
UUIDtostream.- Parameters:
value – The
UUIDto write.