2024-06-20 00:26:04 +00:00
|
|
|
package chunk7
|
2024-06-07 07:38:35 +00:00
|
|
|
|
|
|
|
// data has to be the uncompressed payload of a teeworlds packet
|
|
|
|
// without the packet header
|
|
|
|
//
|
|
|
|
// It will return all the chunks (messages) in that packet
|
|
|
|
func UnpackChunks(data []byte) []Chunk {
|
|
|
|
chunks := []Chunk{}
|
|
|
|
payloadSize := len(data)
|
|
|
|
i := 0
|
|
|
|
|
|
|
|
for i < payloadSize {
|
|
|
|
chunk := Chunk{}
|
2024-06-25 04:19:51 +00:00
|
|
|
// TODO: can we use ChunkHeader.Unpack(u) here to simplify the code?
|
|
|
|
chunk.Header.UnpackRaw(data[i:])
|
2024-06-07 07:38:35 +00:00
|
|
|
i += 2 // header
|
|
|
|
if chunk.Header.Flags.Vital {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
end := i + chunk.Header.Size
|
2024-06-17 04:24:08 +00:00
|
|
|
chunk.Data = make([]byte, end-i)
|
2024-06-23 19:18:54 +00:00
|
|
|
copy(chunk.Data, data[i:end])
|
2024-06-07 07:38:35 +00:00
|
|
|
i += chunk.Header.Size
|
|
|
|
chunks = append(chunks, chunk)
|
|
|
|
}
|
|
|
|
|
|
|
|
return chunks
|
|
|
|
}
|