diff options
Diffstat (limited to 'utils')
| -rw-r--r-- | utils/bitreader.go | 94 | ||||
| -rw-r--r-- | utils/utils.go | 44 |
2 files changed, 2 insertions, 136 deletions
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 @@ | |||
| 1 | package utils | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "fmt" | ||
| 5 | "math" | ||
| 6 | "strconv" | ||
| 7 | ) | ||
| 8 | |||
| 9 | func ReadButtonsDataFromInt32(input int32) []string { | ||
| 10 | buttonList := [32]string{ | ||
| 11 | "Attack", | ||
| 12 | "Jump", | ||
| 13 | "Duck", | ||
| 14 | "Forward", | ||
| 15 | "Back", | ||
| 16 | "Use", | ||
| 17 | "Cancel", | ||
| 18 | "Left", | ||
| 19 | "Right", | ||
| 20 | "MoveLeft", | ||
| 21 | "MoveRight", | ||
| 22 | "Attack2", | ||
| 23 | "Run", | ||
| 24 | "Reload", | ||
| 25 | "Alt1", | ||
| 26 | "Alt2", | ||
| 27 | "Score", | ||
| 28 | "Speed", | ||
| 29 | "Walk", | ||
| 30 | "Zoom", | ||
| 31 | "Weapon1", | ||
| 32 | "Weapon2", | ||
| 33 | "BullRush", | ||
| 34 | "Grenade1", | ||
| 35 | "Grenade2", | ||
| 36 | "LookSpin", | ||
| 37 | "CurrentAbility", | ||
| 38 | "PreviousAbility", | ||
| 39 | "Ability1", | ||
| 40 | "Ability2", | ||
| 41 | "Ability3", | ||
| 42 | "Ability4", | ||
| 43 | } | ||
| 44 | var buttons []string | ||
| 45 | if input == 0 { | ||
| 46 | buttons = append(buttons, buttonList[0]) | ||
| 47 | return buttons | ||
| 48 | } | ||
| 49 | for i := 1; i < 33; i++ { | ||
| 50 | if ReadBitState(input, i) { | ||
| 51 | buttons = append(buttons, buttonList[i]) | ||
| 52 | } | ||
| 53 | } | ||
| 54 | return buttons | ||
| 55 | } | ||
| 56 | |||
| 57 | func ReadBitState(input int32, index int) bool { | ||
| 58 | value := input & (1 << index) | ||
| 59 | return value > 0 | ||
| 60 | } | ||
| 61 | |||
| 62 | func ReadBitStateLSB(input byte, index int) (bool, error) { | ||
| 63 | if index < 0 && index > 7 { | ||
| 64 | return false, fmt.Errorf("IndexOutOfBounds for type byte") | ||
| 65 | } | ||
| 66 | value := input & (1 << index) | ||
| 67 | return (value > 0), nil | ||
| 68 | } | ||
| 69 | |||
| 70 | func Read32BitsAfterFirstBitInt32(input []byte, index int, step int) int32 { | ||
| 71 | binary := "" | ||
| 72 | binary += fmt.Sprintf("%08b", input[step])[8-index : 8] | ||
| 73 | binary += fmt.Sprintf("%08b", input[step-1]) | ||
| 74 | binary += fmt.Sprintf("%08b", input[step-2]) | ||
| 75 | binary += fmt.Sprintf("%08b", input[step-3]) | ||
| 76 | binary += fmt.Sprintf("%08b", input[step-4])[:8-index] | ||
| 77 | output, err := strconv.ParseInt(binary, 2, 32) | ||
| 78 | CheckError(err) | ||
| 79 | return int32(output) | ||
| 80 | |||
| 81 | } | ||
| 82 | |||
| 83 | func Read32BitsAfterFirstBitFloat32(input []byte, index int, step int) float32 { | ||
| 84 | binary := "" | ||
| 85 | binary += fmt.Sprintf("%08b", input[step])[8-index : 8] | ||
| 86 | binary += fmt.Sprintf("%08b", input[step-1]) | ||
| 87 | binary += fmt.Sprintf("%08b", input[step-2]) | ||
| 88 | binary += fmt.Sprintf("%08b", input[step-3]) | ||
| 89 | binary += fmt.Sprintf("%08b", input[step-4])[:8-index] | ||
| 90 | output, err := strconv.ParseUint(binary, 2, 32) | ||
| 91 | CheckError(err) | ||
| 92 | return math.Float32frombits(uint32(output)) | ||
| 93 | |||
| 94 | } | ||
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 @@ | |||
| 1 | package utils | 1 | package utils |
| 2 | 2 | ||
| 3 | import ( | 3 | import "os" |
| 4 | "encoding/binary" | ||
| 5 | "log" | ||
| 6 | "math" | ||
| 7 | "math/bits" | ||
| 8 | "os" | ||
| 9 | "unsafe" | ||
| 10 | ) | ||
| 11 | 4 | ||
| 12 | func CheckError(e error) { | 5 | func CheckError(e error) { |
| 13 | if e != nil { | 6 | if e != nil { |
| 14 | log.Panic(e) | 7 | panic(e) |
| 15 | } | 8 | } |
| 16 | } | 9 | } |
| 17 | 10 | ||
| 18 | func ReverseByteArrayValues(byteArr []byte, size int) []byte { | ||
| 19 | arr := make([]byte, size) | ||
| 20 | for index, byteValue := range byteArr { | ||
| 21 | arr[index] = bits.Reverse8(byteValue) | ||
| 22 | } | ||
| 23 | return arr | ||
| 24 | } | ||
| 25 | |||
| 26 | func ReadByteFromFile(file *os.File, size int32) []byte { | 11 | func ReadByteFromFile(file *os.File, size int32) []byte { |
| 27 | tmp := make([]byte, size) | 12 | tmp := make([]byte, size) |
| 28 | file.Read(tmp) | 13 | file.Read(tmp) |
| 29 | return tmp | 14 | return tmp |
| 30 | } | 15 | } |
| 31 | |||
| 32 | func IntFromBytes(byteArr []byte) uint32 { | ||
| 33 | int := binary.LittleEndian.Uint32(byteArr) | ||
| 34 | return int | ||
| 35 | } | ||
| 36 | |||
| 37 | func FloatFromBytes(byteArr []byte) float32 { | ||
| 38 | bits := binary.LittleEndian.Uint32(byteArr) | ||
| 39 | float := math.Float32frombits(bits) | ||
| 40 | return float | ||
| 41 | } | ||
| 42 | |||
| 43 | func FloatArrFromBytes(byteArr []byte) []float32 { | ||
| 44 | if len(byteArr) == 0 { | ||
| 45 | return nil | ||
| 46 | } | ||
| 47 | |||
| 48 | l := len(byteArr) / 4 | ||
| 49 | ptr := unsafe.Pointer(&byteArr[0]) | ||
| 50 | // It is important to keep in mind that the Go garbage collector | ||
| 51 | // will not interact with this data, and that if src if freed, | ||
| 52 | // the behavior of any Go code using the slice is nondeterministic. | ||
| 53 | // Reference: https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices | ||
| 54 | return (*[1 << 26]float32)((*[1 << 26]float32)(ptr))[:l:l] | ||
| 55 | } | ||