aboutsummaryrefslogtreecommitdiffstats
path: root/common/buffer/get.go
diff options
context:
space:
mode:
Diffstat (limited to 'common/buffer/get.go')
-rw-r--r--common/buffer/get.go189
1 files changed, 189 insertions, 0 deletions
diff --git a/common/buffer/get.go b/common/buffer/get.go
new file mode 100644
index 0000000..4f27b70
--- /dev/null
+++ b/common/buffer/get.go
@@ -0,0 +1,189 @@
+package buffer
+
+import (
+ "encoding/binary"
+ "math"
+ "strings"
+)
+
+func (b *Buffer) ReadByte() (byte, error) {
+ if b.off+1 > len(b.buf) {
+ return 0, ErrBadRead
+ }
+
+ r := b.buf[b.off]
+ b.off += 1
+ return r, nil
+}
+
+func (b *Buffer) GetBytes(n int) ([]byte, error) {
+ if b.off+n > len(b.buf) {
+ return nil, ErrBadRead
+ }
+
+ r := b.buf[b.off : b.off+n]
+ b.off += n
+ return r, nil
+}
+
+func (b *Buffer) GetUint8() (uint8, error) {
+ if b.off+1 > len(b.buf) {
+ return 0, ErrBadRead
+ }
+
+ r := b.buf[b.off]
+ b.off++
+ return r, nil
+}
+
+func (b *Buffer) GetInt8() (int8, error) {
+ r, err := b.GetUint8()
+ if err != nil {
+ return 0, err
+ }
+
+ return int8(r), nil
+}
+
+func (b *Buffer) GetInt16() (int16, error) {
+ r, err := b.GetUint16()
+ if err != nil {
+ return 0, err
+ }
+
+ return int16(r), nil
+}
+
+func (b *Buffer) GetUint16() (uint16, error) {
+ if b.off+2 > len(b.buf) {
+ return 0, ErrBadRead
+ }
+
+ r := binary.LittleEndian.Uint16(b.buf[b.off : b.off+2])
+ b.off += 2
+ return r, nil
+}
+
+func (b *Buffer) GetInt32() (int32, error) {
+ r, err := b.GetUint32()
+ if err != nil {
+ return 0, err
+ }
+
+ return int32(r), nil
+}
+
+func (b *Buffer) GetUint32() (uint32, error) {
+ if b.off+4 > len(b.buf) {
+ return 0, ErrBadRead
+ }
+
+ r := binary.LittleEndian.Uint32(b.buf[b.off : b.off+4])
+ b.off += 4
+ return r, nil
+}
+
+func (b *Buffer) GetFloat32() (float32, error) {
+ r, err := b.GetUint32()
+ if err != nil {
+ return 0, err
+ }
+
+ return math.Float32frombits(r), nil
+}
+
+func (b *Buffer) GetAngle8() (float32, error) {
+ r, err := b.ReadByte()
+ if err != nil {
+ return 0, err
+ }
+
+ return float32(int8(r)) * (360.0 / 256), nil
+}
+
+func (b *Buffer) GetAngle16() (float32, error) {
+ r, err := b.GetUint16()
+ if err != nil {
+ return 0, err
+ }
+
+ return float32(r) * (360.0 / 65536), nil
+}
+
+func (b *Buffer) GetCoord16() (float32, error) {
+ r, err := b.GetInt16()
+ if err != nil {
+ return 0, err
+ }
+
+ return float32(r) / 8.0, nil
+}
+
+func (b *Buffer) GetCoord32() (float32, error) {
+ return b.GetFloat32()
+}
+
+func (b *Buffer) GetString() (string, error) {
+ var str strings.Builder
+
+ for b.off < b.Len() {
+ r, err := b.ReadByte()
+ if err != nil {
+ return "", err
+ }
+
+ if r == 0xff {
+ continue
+ }
+
+ if r == 0x00 {
+ break
+ }
+
+ str.WriteByte(r)
+ }
+
+ return str.String(), nil
+}
+
+func (b *Buffer) PeekInt32() (int32, error) {
+ if b.off+4 > len(b.buf) {
+ return 0, ErrBadRead
+ }
+
+ return int32(binary.LittleEndian.Uint32(b.buf[b.off : b.off+4])), nil
+}
+
+func (b *Buffer) PeekBytes(n int) ([]byte, error) {
+ if b.off+n > len(b.buf) {
+ return nil, ErrBadRead
+ }
+
+ return b.buf[b.off : b.off+n], nil
+}
+
+func (b *Buffer) PeekBytesAt(off, size int) ([]byte, error) {
+ if off+size > len(b.buf) {
+ return nil, ErrBadRead
+ }
+
+ return b.buf[off : off+size], nil
+}
+
+func (b *Buffer) Skip(n int) error {
+ if b.off+n > len(b.buf) {
+ return ErrBadRead
+ }
+
+ b.off += n
+ return nil
+}
+
+func (b *Buffer) Seek(n int) error {
+ if n > len(b.buf) {
+ return ErrBadRead
+ }
+
+ b.off = n
+ return nil
+}
ght'> 2024-09-25added ; chaining operatorGravatar BanceDev 1-14/+14 2024-09-25added || chaining operatorGravatar BanceDev 1-2/+9 2024-09-25Update README.mdGravatar Lance Borden 1-0/+3 2024-09-23fixed lua api exit status issueGravatar BanceDev 1-1/+1 2024-09-23fixed splitting within quoted stringGravatar BanceDev 1-5/+10 2024-09-23made and operator actually conditionalGravatar BanceDev 2-17/+27 2024-09-23fixed operator chaining to handle commands terminating with an operatorGravatar BanceDev 1-14/+5 2024-09-23implemented background process operatorGravatar BanceDev 6-154/+238 2024-09-23implemented piping into new chaining methodGravatar BanceDev 1-10/+30 2024-09-18fixed && chaining for builtinsGravatar BanceDev 2-23/+31 2024-09-18added basic && chainingGravatar BanceDev 2-8/+73 2024-09-17changed tokenizer to handle all the chaining operatorsGravatar BanceDev 3-20/+78 2024-09-17Update README.mdGravatar Lance Borden 1-0/+1 2024-09-13added better clarification to help menuGravatar BanceDev 1-1/+2 2024-09-13improved installation instructionsGravatar BanceDev 1-3/+3 2024-09-12v0.1.1Gravatar BanceDev 1-1/+1 2024-09-12fixed bug in input buffer handling due to misplaced printGravatar BanceDev 2-5/+8 2024-09-12Update build.yml checkout v4Gravatar Lance Borden 1-1/+1 2024-09-12Update build.yml to artifact v4Gravatar Lance Borden 1-1/+1 2024-09-12fixed exit status issue with non interative modeGravatar BanceDev 1-1/+1 2024-09-12added non interative mode for running lua scriptsGravatar BanceDev 2-4/+28 2024-09-12temporary github action fix until non-interactive mode is implementedGravatar BanceDev 1-2/+2 2024-09-12attempt to update build script to accept input into lush shellGravatar BanceDev 2-4/+6 2024-09-12prevent lush workflow from getting stuck in testsGravatar Lance Borden 1-1/+4 2024-09-12removed chsh in workflowGravatar Lance Borden 1-4/+1