From f108a577658c9aab8496da4ebd0fb4f0216093e8 Mon Sep 17 00:00:00 2001 From: BiSaXa <1669855+BiSaXa@users.noreply.github.com> Date: Sat, 27 Aug 2022 13:02:35 +0300 Subject: init --- utils/header.go | 43 +++++++++++++++++++++++++++++++++++++++++++ utils/utils.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 utils/header.go create mode 100644 utils/utils.go (limited to 'utils') 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 @@ +package utils + +import ( + "fmt" + "os" +) + +func HeaderOut(file *os.File) { + DemoFileStamp := make([]byte, 8) + DemoProtocol := make([]byte, 4) + NetworkProtocol := make([]byte, 4) + ServerName := make([]byte, 260) + ClientName := make([]byte, 260) + MapName := make([]byte, 260) + GameDirectory := make([]byte, 260) + PlaybackTime := make([]byte, 4) + PlaybackTicks := make([]byte, 4) + PlaybackFrames := make([]byte, 4) + SignOnLength := make([]byte, 4) + file.Read(DemoFileStamp) + file.Read(DemoProtocol) + file.Read(NetworkProtocol) + file.Read(ServerName) + file.Read(ClientName) + file.Read(MapName) + file.Read(GameDirectory) + file.Read(PlaybackTime) + file.Read(PlaybackTicks) + file.Read(PlaybackFrames) + file.Read(SignOnLength) + + fmt.Println(string(DemoFileStamp)) + fmt.Println(IntFromBytes(DemoProtocol)) + fmt.Println(IntFromBytes(NetworkProtocol)) + fmt.Println(string(ServerName)) + fmt.Println(string(ClientName)) + fmt.Println(string(MapName)) + fmt.Println(string(GameDirectory)) + fmt.Println(FloatFromBytes(PlaybackTime)) + fmt.Println(IntFromBytes(PlaybackTicks)) + fmt.Println(IntFromBytes(PlaybackFrames)) + fmt.Println(IntFromBytes(SignOnLength)) +} 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 @@ +package utils + +import ( + "encoding/binary" + "log" + "math" + "os" + "unsafe" + + "github.com/potterxu/bitreader" +) + +func ReadByteFromFile(file *os.File, size int32) []byte { + tmp := make([]byte, size) + file.Read(tmp) + return tmp +} + +func CheckError(e error) { + if e != nil { + log.Panic(e) + } +} + +func CheckFirstBit(byteArr []byte) bool { + reader := bitreader.BitReader(byteArr) + state, err := reader.ReadBit() + if err != nil { + state = false + } + return state +} + +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