51 lines
777 B
Go
51 lines
777 B
Go
package models
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"math"
|
|
)
|
|
|
|
type Reader struct {
|
|
buf []byte
|
|
off int
|
|
}
|
|
|
|
func NewReader(b []byte) *Reader {
|
|
return &Reader{buf: b}
|
|
}
|
|
|
|
func (r *Reader) ReadU8() uint8 {
|
|
v := r.buf[r.off]
|
|
r.off++
|
|
return v
|
|
}
|
|
|
|
func (r *Reader) ReadU16() uint16 {
|
|
v := binary.LittleEndian.Uint16(r.buf[r.off:])
|
|
r.off += 2
|
|
return v
|
|
}
|
|
|
|
func (r *Reader) ReadU32() uint32 {
|
|
v := binary.LittleEndian.Uint32(r.buf[r.off:])
|
|
r.off += 4
|
|
return v
|
|
}
|
|
|
|
func (r *Reader) ReadF32() float32 {
|
|
v := math.Float32frombits(binary.LittleEndian.Uint32(r.buf[r.off:]))
|
|
r.off += 4
|
|
return v
|
|
}
|
|
|
|
func (r *Reader) ReadBool() bool {
|
|
return r.ReadU8() == 1
|
|
}
|
|
|
|
func (r *Reader) ReadString() string {
|
|
l := r.ReadU16()
|
|
s := string(r.buf[r.off : r.off+int(l)])
|
|
r.off += int(l)
|
|
return s
|
|
}
|