diff options
| author | BiSaXa <1669855+BiSaXa@users.noreply.github.com> | 2022-08-27 13:02:35 +0300 |
|---|---|---|
| committer | BiSaXa <1669855+BiSaXa@users.noreply.github.com> | 2022-08-27 13:02:35 +0300 |
| commit | f108a577658c9aab8496da4ebd0fb4f0216093e8 (patch) | |
| tree | e484a8a8b54c92ca4a393f267ebc755ec6434c8d /utils | |
| download | sdp.go-f108a577658c9aab8496da4ebd0fb4f0216093e8.tar.gz sdp.go-f108a577658c9aab8496da4ebd0fb4f0216093e8.tar.bz2 sdp.go-f108a577658c9aab8496da4ebd0fb4f0216093e8.zip | |
init
Diffstat (limited to 'utils')
| -rw-r--r-- | utils/header.go | 43 | ||||
| -rw-r--r-- | utils/utils.go | 57 |
2 files changed, 100 insertions, 0 deletions
diff --git a/utils/header.go b/utils/header.go new file mode 100644 index 0000000..68def9e --- /dev/null +++ b/utils/header.go | |||
| @@ -0,0 +1,43 @@ | |||
| 1 | package utils | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "fmt" | ||
| 5 | "os" | ||
| 6 | ) | ||
| 7 | |||
| 8 | func HeaderOut(file *os.File) { | ||
| 9 | DemoFileStamp := make([]byte, 8) | ||
| 10 | DemoProtocol := make([]byte, 4) | ||
| 11 | NetworkProtocol := make([]byte, 4) | ||
| 12 | ServerName := make([]byte, 260) | ||
| 13 | ClientName := make([]byte, 260) | ||
| 14 | MapName := make([]byte, 260) | ||
| 15 | GameDirectory := make([]byte, 260) | ||
| 16 | PlaybackTime := make([]byte, 4) | ||
| 17 | PlaybackTicks := make([]byte, 4) | ||
| 18 | PlaybackFrames := make([]byte, 4) | ||
| 19 | SignOnLength := make([]byte, 4) | ||
| 20 | file.Read(DemoFileStamp) | ||
| 21 | file.Read(DemoProtocol) | ||
| 22 | file.Read(NetworkProtocol) | ||
| 23 | file.Read(ServerName) | ||
| 24 | file.Read(ClientName) | ||
| 25 | file.Read(MapName) | ||
| 26 | file.Read(GameDirectory) | ||
| 27 | file.Read(PlaybackTime) | ||
| 28 | file.Read(PlaybackTicks) | ||
| 29 | file.Read(PlaybackFrames) | ||
| 30 | file.Read(SignOnLength) | ||
| 31 | |||
| 32 | fmt.Println(string(DemoFileStamp)) | ||
| 33 | fmt.Println(IntFromBytes(DemoProtocol)) | ||
| 34 | fmt.Println(IntFromBytes(NetworkProtocol)) | ||
| 35 | fmt.Println(string(ServerName)) | ||
| 36 | fmt.Println(string(ClientName)) | ||
| 37 | fmt.Println(string(MapName)) | ||
| 38 | fmt.Println(string(GameDirectory)) | ||
| 39 | fmt.Println(FloatFromBytes(PlaybackTime)) | ||
| 40 | fmt.Println(IntFromBytes(PlaybackTicks)) | ||
| 41 | fmt.Println(IntFromBytes(PlaybackFrames)) | ||
| 42 | fmt.Println(IntFromBytes(SignOnLength)) | ||
| 43 | } | ||
diff --git a/utils/utils.go b/utils/utils.go new file mode 100644 index 0000000..62924c2 --- /dev/null +++ b/utils/utils.go | |||
| @@ -0,0 +1,57 @@ | |||
| 1 | package utils | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "encoding/binary" | ||
| 5 | "log" | ||
| 6 | "math" | ||
| 7 | "os" | ||
| 8 | "unsafe" | ||
| 9 | |||
| 10 | "github.com/potterxu/bitreader" | ||
| 11 | ) | ||
| 12 | |||
| 13 | func ReadByteFromFile(file *os.File, size int32) []byte { | ||
| 14 | tmp := make([]byte, size) | ||
| 15 | file.Read(tmp) | ||
| 16 | return tmp | ||
| 17 | } | ||
| 18 | |||
| 19 | func CheckError(e error) { | ||
| 20 | if e != nil { | ||
| 21 | log.Panic(e) | ||
| 22 | } | ||
| 23 | } | ||
| 24 | |||
| 25 | func CheckFirstBit(byteArr []byte) bool { | ||
| 26 | reader := bitreader.BitReader(byteArr) | ||
| 27 | state, err := reader.ReadBit() | ||
| 28 | if err != nil { | ||
| 29 | state = false | ||
| 30 | } | ||
| 31 | return state | ||
| 32 | } | ||
| 33 | |||
| 34 | func IntFromBytes(byteArr []byte) uint32 { | ||
| 35 | int := binary.LittleEndian.Uint32(byteArr) | ||
| 36 | return int | ||
| 37 | } | ||
| 38 | |||
| 39 | func FloatFromBytes(byteArr []byte) float32 { | ||
| 40 | bits := binary.LittleEndian.Uint32(byteArr) | ||
| 41 | float := math.Float32frombits(bits) | ||
| 42 | return float | ||
| 43 | } | ||
| 44 | |||
| 45 | func FloatArrFromBytes(byteArr []byte) []float32 { | ||
| 46 | if len(byteArr) == 0 { | ||
| 47 | return nil | ||
| 48 | } | ||
| 49 | |||
| 50 | l := len(byteArr) / 4 | ||
| 51 | ptr := unsafe.Pointer(&byteArr[0]) | ||
| 52 | // It is important to keep in mind that the Go garbage collector | ||
| 53 | // will not interact with this data, and that if src if freed, | ||
| 54 | // the behavior of any Go code using the slice is nondeterministic. | ||
| 55 | // Reference: https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices | ||
| 56 | return (*[1 << 26]float32)((*[1 << 26]float32)(ptr))[:l:l] | ||
| 57 | } | ||