This repository contains byte buffer implementation in both methodmap and 1.10 enum struct.
The most common use of this is for formatting messages over a socket.
The methodmap implementation is derived from
https://forums.alliedmods.net/showthread.php?t=265789 and patched for 1.11
Methodmap
PHP Code:
/**
* Creates a new ByteBuffer and returns a handle to it
*
* @param bool littleEndian true if numbers should be treated as little-endian, false for big-endian (default true for little-endian)
* @param char data A string containing data to initialize this buffer with (for reading)
* @param int dataSize The size in bytes of the input data
* @return ByteBuffer An integer between 0 and MAX_BUFFERS - 1 which refers to this ByteBuffer
*/
ByteBuffer CreateByteBuffer(bool littleEndian = true, const char data[] = "", int dataSize = 0)
/**
* @property int The current read/write offset of this buffer (starting at 0)
*/
ByteBuffer.Cursor
/**
* @property bool true if this buffer is treated as little-endian
*/
ByteBuffer.LittleEndian
/**
* @property bool true if this buffer is treated as big-endian
*/
ByteBuffer.BigEndian
/**
* Writes an unsigned byte of data (0 - 255 inclusive) and increments the Cursor by 1
*
* @param int data The data to write
* @noreturn
*/
void ByteBuffer.WriteByte(int data)
/**
* Writes an unsigned short (two bytes of data, 0 - 65535 inclusive) and increments the Cursor by 2
*
* @param int data The data to write
* @noreturn
*/
void ByteBuffer.WriteShort(int data)
/**
* Writes a 4-byte integer and increments the Cursor by 4
*
* @param int data The data to write
* @noreturn
*/
void ByteBuffer.WriteInt(int data)
/**
* Writes a null-terminated string and increments the Cursor by strlen(input) + 1
*
* @param char[] input The string to write
* @noreturn
*/
void ByteBuffer.WriteString(const char[] input)
/**
* Reads an unsigned byte of data (0 - 255 inclusive) and increments the Cursor by 1
*
* @return int Data at current byte
*/
int ByteBuffer.ReadByte()
/**
* Reads an unsigned short (two bytes of data, 0 - 65535 inclusive) and increments the Cursor by 2
*
* @return int Data at current two bytes
*/
int ByteBuffer.ReadShort()
/**
* Reads a 4-byte integer and increments the Cursor by 4
*
* @return int Data at current four bytes
*/
int ByteBuffer.ReadInt()
/**
* Reads a null-terminated string and increments the Cursor by strlen(output) + 1
*
* @param char[] output Buffer to which we will write the string
* @param int maxlen Maximum length of output buffer
* @return int Number of bytes written to output buffer
*/
int ByteBuffer.ReadString(char[] output, int maxlen)
/**
* Dumps the contents of this buffer from first byte to Cursor
*
* @param char[] output Buffer to which we will write this buffer's contents
* @param int maxlen Maximum length of output buffer
* @return int Number of bytes written to output buffer
*/
int ByteBuffer.Dump(char[] output, int maxlen)
/**
* Appends the contents of another ByteBuffer to the current one. Data will be written to current buffer starting at Cursor.
*
* @param ByteBuffer buffer Buffer whose contents will be appended to this one. Data will be read from buffer from 0 to buffer's Cursor.
* @return int Number of bytes written to current buffer
*/
int ByteBuffer.Append(ByteBuffer buffer)
/**
* Extracts the contents of the current buffer to a new ByteBuffer, starting at the Cursor.
*
* @param int length Maximum number of bytes that will be extracted and written to the new buffer.
* @return ByteBuffer A new ByteBuffer containing the extracted contents (new buffer's Cursor will be 0).
*/
ByteBuffer ByteBuffer.Extract(int length)
/**
* Resets the buffer's Cursor to 0
*
* @noreturn
*/
void ByteBuffer.Reset()
/**
* Closes the current buffer. All buffers must be closed when no longer in use.
* This simply marks the buffer as no longer in use. Memory for all possible buffer (MAX_BUFFERS) will be
* allocated on plugin load, and will only be freed on plugin unload. Any data currently in the buffer
* will be retained, but will be overwritten when this buffer index is reused. Any attempts to read/write
* from a closed buffer will throw an error.
*
* @noreturn
*/
void ByteBuffer.Close()
Enum struct is similar but encapsulated with enum struct instead of a methodmap.
Source
__________________