wiithon.binary.writer module
Write numbers, strings and raw bytes into a binary stream
This module exposes BinaryWriter, the counterpart of BinaryReader
Everything is written in big-endian since it is what the Wii uses
- class wiithon.binary.writer.BinaryWriter(stream, encoding='utf-8')[source]
Bases:
objectA writer that appends structured values to a stream
The writer never owns the stream, it only writes in it, so closing it is up to the caller Contrary to the reader there is no
from_byteshelper, wrap aio.BytesIOyourself and read it back withgetvalue()Example
>>> buf = BytesIO() >>> writer = BinaryWriter(buf) >>> writer.u32(0x5D1C9EA3) >>> writer.string("GAME", 0x10) >>> data = buf.getvalue()
- stream
Stream to write for operations
- encoding
Encoding used by default for writing
- seek(offset)[source]
Change the stream cursor position to a given offset
Note
Seeking past the end of the stream does not grow it, the gap is filled only when you write there
- tell()[source]
Get the current stream cursor position
- Return type:
- Returns:
The current stream cursor position within the stream
- pad(count, byte=b'\\x00')[source]
Write a filler of
countbytes to the stream of the same byte, used to align structure on a boundary- Parameters:
- Return type:
Note
countis the number of repetitions, not a number of bytes.
- size()[source]
Get the total size of the stream
The cursor is moved to the end to measure the stream then put back where it was.
- Return type:
- Returns:
The size of the whole stream in bytes, not the number of bytes written
- u8(data)[source]
Write a single unsigned byte to the stream in big-endian at the current stream position The cursor is placed after the written byte
- u16(data)[source]
Write two unsigned bytes to the stream in big-endian at the current stream position The cursor is placed after the written bytes
- u32(data)[source]
Write four unsigned bytes to the stream in big-endian at the current stream position The cursor is placed after the written bytes
- u64(data)[source]
Write eight unsigned bytes to the stream in big-endian at the current stream position The cursor is placed after the written bytes
- s8(data)[source]
Write a single signed byte to the stream in big-endian at the current stream position The cursor is placed after the written byte
- s16(data)[source]
Write two signed bytes to the stream in big-endian at the current stream position The cursor is placed after the written bytes
- s32(data)[source]
Write four signed bytes to the stream in big-endian at the current stream position The cursor is placed after the written bytes
- s64(data)[source]
Write eight signed bytes to the stream in big-endian at the current stream position The cursor is placed after the written bytes
- float(data)[source]
Write a 32 bits float to the stream in big-endian at the current stream position The cursor is placed after the written bytes
- list_u32(numbers)[source]
Write a list of unsigned 32 bits numbers one after the other
Note
No count is written before the list. If the format needs one, write it yourself
- u32_shifted(data)[source]
Right shift a number 2 times then write it as an unsigned 32 bits number
This is the inverse of
u32_shifted(), for the formats that store offsets divided by 4Warning
The two low bits are lost.
- u32_le(data)[source]
Write four unsigned bytes to the stream in little-endian at the current stream position
Note
Almost everything on a Wii disc is big-endian. This is only for the few structures that are not
- string(value, size=None, padding=b'\\x00', encoding=None, *, add_null_byte=False)[source]
Write a string in a field of a fixed size, padded if it is too short
- Parameters:
value (
str) – The string to writesize (
int|None, default:None) – The size of the field in bytes. Defaults to the encoded length ofvalue, which writes it without any paddingpadding (
bytes, default:b'\\x00') – The byte used to fill the end of the fieldencoding (
str|None, default:None) – The encoding to use. Defaults to the encoding of the writeradd_null_byte (
bool, default:False) – Write an extra null byte after the field, for the formats that store strings one after the other in a string table
- Raises:
BinaryError – If the encoded string is longer than
size- Return type:
Note
sizecounts encoded bytes, not characters. A japanese string inshift_jistakes more room than its length suggestsWarning
With
add_null_bytethe field takessize + 1bytes, the null byte is not counted inside