From 4e71a481884c0c43aea3d0ee695ae68cdec56478 Mon Sep 17 00:00:00 2001 From: BiSaXa <1669855+BiSaXa@users.noreply.github.com> Date: Wed, 7 Sep 2022 19:40:16 +0300 Subject: first rewrite commit using bisaxa/bitreader --- utils/bitreader.go | 94 ------------------------------------------------------ utils/utils.go | 44 ++----------------------- 2 files changed, 2 insertions(+), 136 deletions(-) delete mode 100644 utils/bitreader.go (limited to 'utils') diff --git a/utils/bitreader.go b/utils/bitreader.go deleted file mode 100644 index ec7454e..0000000 --- a/utils/bitreader.go +++ /dev/null @@ -1,94 +0,0 @@ -package utils - -import ( - "fmt" - "math" - "strconv" -) - -func ReadButtonsDataFromInt32(input int32) []string { - buttonList := [32]string{ - "Attack", - "Jump", - "Duck", - "Forward", - "Back", - "Use", - "Cancel", - "Left", - "Right", - "MoveLeft", - "MoveRight", - "Attack2", - "Run", - "Reload", - "Alt1", - "Alt2", - "Score", - "Speed", - "Walk", - "Zoom", - "Weapon1", - "Weapon2", - "BullRush", - "Grenade1", - "Grenade2", - "LookSpin", - "CurrentAbility", - "PreviousAbility", - "Ability1", - "Ability2", - "Ability3", - "Ability4", - } - var buttons []string - if input == 0 { - buttons = append(buttons, buttonList[0]) - return buttons - } - for i := 1; i < 33; i++ { - if ReadBitState(input, i) { - buttons = append(buttons, buttonList[i]) - } - } - return buttons -} - -func ReadBitState(input int32, index int) bool { - value := input & (1 << index) - return value > 0 -} - -func ReadBitStateLSB(input byte, index int) (bool, error) { - if index < 0 && index > 7 { - return false, fmt.Errorf("IndexOutOfBounds for type byte") - } - value := input & (1 << index) - return (value > 0), nil -} - -func Read32BitsAfterFirstBitInt32(input []byte, index int, step int) int32 { - binary := "" - binary += fmt.Sprintf("%08b", input[step])[8-index : 8] - binary += fmt.Sprintf("%08b", input[step-1]) - binary += fmt.Sprintf("%08b", input[step-2]) - binary += fmt.Sprintf("%08b", input[step-3]) - binary += fmt.Sprintf("%08b", input[step-4])[:8-index] - output, err := strconv.ParseInt(binary, 2, 32) - CheckError(err) - return int32(output) - -} - -func Read32BitsAfterFirstBitFloat32(input []byte, index int, step int) float32 { - binary := "" - binary += fmt.Sprintf("%08b", input[step])[8-index : 8] - binary += fmt.Sprintf("%08b", input[step-1]) - binary += fmt.Sprintf("%08b", input[step-2]) - binary += fmt.Sprintf("%08b", input[step-3]) - binary += fmt.Sprintf("%08b", input[step-4])[:8-index] - output, err := strconv.ParseUint(binary, 2, 32) - CheckError(err) - return math.Float32frombits(uint32(output)) - -} diff --git a/utils/utils.go b/utils/utils.go index 5226e80..46b707c 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -1,55 +1,15 @@ package utils -import ( - "encoding/binary" - "log" - "math" - "math/bits" - "os" - "unsafe" -) +import "os" func CheckError(e error) { if e != nil { - log.Panic(e) + panic(e) } } -func ReverseByteArrayValues(byteArr []byte, size int) []byte { - arr := make([]byte, size) - for index, byteValue := range byteArr { - arr[index] = bits.Reverse8(byteValue) - } - return arr -} - func ReadByteFromFile(file *os.File, size int32) []byte { tmp := make([]byte, size) file.Read(tmp) return tmp } - -func IntFromBytes(byteArr []byte) uint32 { - int := binary.LittleEndian.Uint32(byteArr) - return int -} - -func FloatFromBytes(byteArr []byte) float32 { - bits := binary.LittleEndian.Uint32(byteArr) - float := math.Float32frombits(bits) - return float -} - -func FloatArrFromBytes(byteArr []byte) []float32 { - if len(byteArr) == 0 { - return nil - } - - l := len(byteArr) / 4 - ptr := unsafe.Pointer(&byteArr[0]) - // It is important to keep in mind that the Go garbage collector - // will not interact with this data, and that if src if freed, - // the behavior of any Go code using the slice is nondeterministic. - // Reference: https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices - return (*[1 << 26]float32)((*[1 << 26]float32)(ptr))[:l:l] -} -- cgit v1.2.3