diff options
Diffstat (limited to 'byte_stream.gd')
-rw-r--r-- | byte_stream.gd | 42 |
1 files changed, 32 insertions, 10 deletions
diff --git a/byte_stream.gd b/byte_stream.gd index df55a7d..fe150fc 100644 --- a/byte_stream.gd +++ b/byte_stream.gd @@ -7,7 +7,7 @@ var position: int = 0 @warning_ignore("shadowed_variable") -static func from_bytes(bytes: PackedByteArray): +static func from_bytes(bytes: PackedByteArray) -> ByteStream: var byte_stream = ByteStream.new() byte_stream.bytes = bytes @@ -18,55 +18,77 @@ static func from_bytes(bytes: PackedByteArray): @warning_ignore("shadowed_variable") func seek(position: int): if position > 0: - assert(position < bytes.size()) + assert(position <= bytes.size()) self.position = position -func decode_u8(): +func advance(jumps: int): + position += jumps + + +func get_buffer(length: int) -> ByteStream: + var byte_stream = ByteStream.new() + + byte_stream.bytes = bytes.slice(position, position + length) + seek(position + length) + + return byte_stream + + +func decode_u8() -> int: var result = bytes.decode_u8(position) seek(position + 1) return result -func decode_u16(): +func decode_u16() -> int: var result = bytes.decode_u16(position) seek(position + 2) return result -func decode_u32(): +func decode_u32() -> int: var result = bytes.decode_u32(position) seek(position + 4) return result -func decode_u64(): +func decode_u64() -> int: var result = bytes.decode_u64(position) seek(position + 8) return result -func decode_s8(): +func decode_s8() -> int: var result = bytes.decode_s8(position) seek(position + 1) return result -func decode_s16(): +func decode_s16() -> int: var result = bytes.decode_s16(position) seek(position + 2) return result -func decode_s32(): +func decode_s32() -> int: var result = bytes.decode_s32(position) seek(position + 4) return result -func decode_s64(): +func decode_s64() -> int: var result = bytes.decode_s64(position) seek(position + 8) return result + +func decode_float() -> float: + var result = bytes.decode_float(position) + seek(position + 4) + + return result + +func get_string_from_utf8(length: int) -> String: + return get_buffer(length).bytes.get_string_from_utf8() |