summaryrefslogtreecommitdiff
path: root/byte_stream.gd
diff options
context:
space:
mode:
authorDaniel Weipert <git@mail.dweipert.de>2024-12-04 14:23:42 +0100
committerDaniel Weipert <git@mail.dweipert.de>2024-12-04 14:23:42 +0100
commit688790b5dc0ea8f51a99e42a00c7510b9bd87aa6 (patch)
tree71c04c9f401c67caf5c1ea3f56c7360ac1d14b86 /byte_stream.gd
parent2bf6b303fc062a800afb784877d989686287a2a7 (diff)
next commit
Diffstat (limited to 'byte_stream.gd')
-rw-r--r--byte_stream.gd72
1 files changed, 72 insertions, 0 deletions
diff --git a/byte_stream.gd b/byte_stream.gd
new file mode 100644
index 0000000..df55a7d
--- /dev/null
+++ b/byte_stream.gd
@@ -0,0 +1,72 @@
+class_name ByteStream
+
+
+var bytes: PackedByteArray
+
+var position: int = 0
+
+
+@warning_ignore("shadowed_variable")
+static func from_bytes(bytes: PackedByteArray):
+ 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 decode_u8():
+ var result = bytes.decode_u8(position)
+ seek(position + 1)
+
+ return result
+
+func decode_u16():
+ var result = bytes.decode_u16(position)
+ seek(position + 2)
+
+ return result
+
+func decode_u32():
+ var result = bytes.decode_u32(position)
+ seek(position + 4)
+
+ return result
+
+func decode_u64():
+ var result = bytes.decode_u64(position)
+ seek(position + 8)
+
+ return result
+
+func decode_s8():
+ var result = bytes.decode_s8(position)
+ seek(position + 1)
+
+ return result
+
+func decode_s16():
+ var result = bytes.decode_s16(position)
+ seek(position + 2)
+
+ return result
+
+func decode_s32():
+ var result = bytes.decode_s32(position)
+ seek(position + 4)
+
+ return result
+
+func decode_s64():
+ var result = bytes.decode_s64(position)
+ seek(position + 8)
+
+ return result