
import struct
 
msg_reply = bytearray()
 
def ReadByte(_msg):
    bytes = _msg[:1] # Read first byte
    del _msg[:1]     # Seek to byte 1
    return struct.unpack("<B", bytes)[0]

def ReadInt16(_msg):
    bytes = _msg[:2] # Read first 2 bytes
    del _msg[:2]     # Seek to byte 2
    return struct.unpack("<h", bytes)[0]

def ReadInt32(_msg):
    bytes = _msg[:4] # Read first 4 bytes
    del _msg[:4]     # Seek to byte 4
    return struct.unpack("<i", bytes)[0]

def ReadInt64(_msg):
    bytes = _msg[:8] # Read first 4 bytes
    del _msg[:8]     # Seek to byte 4
    return struct.unpack("<q", bytes)[0]

def ReadFloat(_msg):
    bytes = _msg[:4] # Read first 4 bytes
    del _msg[:4]     # Seek to byte 4
    return struct.unpack("<f", bytes)[0]

def ReadString(_msg):       # This function finds the first null terminator in a string and returns everything before it
    i = _msg.index(b'\x00')
    bytes = _msg[:i]
    del _msg[:i + 1]
    return bytes.decode()

def WriteByte(_msg, _value):    # Force-converts to byte
    _msg += (struct.pack("<B", _value))

def WriteInt16(_msg, _value):   # Force-converts to int32 as 2 bytes
    _msg += (struct.pack("<h", _value))

def WriteInt32(_msg, _value):   # Force-converts to int32 as 4 bytes
    _msg += (struct.pack("<i", _value))
    
def WriteInt64(_msg, _value):   # Force-converts to int32 as 4 bytes
    _msg += (struct.pack("<q", _value))

def WriteFloat(_msg, _value):   # Force-converts to float32 as 4 bytes
    _msg += (struct.pack("<f", _value))

def WriteString(_msg, _value):          # This adds a null terminator to the end of a string
    _msg += _value.encode() + b'\x00'