summaryrefslogtreecommitdiff
path: root/byte_stream.gd
blob: fe150fc4b59e256a13f76105a74970bbe62d3a9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
class_name ByteStream


var bytes: PackedByteArray

var position: int = 0


@warning_ignore("shadowed_variable")
static func from_bytes(bytes: PackedByteArray) -> ByteStream:
	var byte_stream = ByteStream.new()
	
	byte_stream.bytes = bytes
	
	return byte_stream


@warning_ignore("shadowed_variable")
func seek(position: int):
	if position > 0:
		assert(position <= bytes.size())
	
	self.position = position


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() -> int:
	var result = bytes.decode_u16(position)
	seek(position + 2)
	
	return result

func decode_u32() -> int:
	var result = bytes.decode_u32(position)
	seek(position + 4)
	
	return result

func decode_u64() -> int:
	var result = bytes.decode_u64(position)
	seek(position + 8)
	
	return result

func decode_s8() -> int:
	var result = bytes.decode_s8(position)
	seek(position + 1)
	
	return result

func decode_s16() -> int:
	var result = bytes.decode_s16(position)
	seek(position + 2)
	
	return result

func decode_s32() -> int:
	var result = bytes.decode_s32(position)
	seek(position + 4)
	
	return result

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()