Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat (indev): movement data #105

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 0 additions & 116 deletions dissect/dump.go

This file was deleted.

55 changes: 55 additions & 0 deletions dissect/movement.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package dissect

import "bytes"

type MovementUpdates struct {
Username string `json:"username"`
Data []MovementUpdate `json:"data"`
}

type MovementUpdate struct {
X float32
Y float32
Z float32
}

// TODO: there appears to be multiple types of movement updates (movement only, rotation only (?), movement + rotation (?))
// that can be identified by 2 bytes
// gotta implement the other types of movement updates
var movementType = []byte{0xC0, 0x3F}

func readMovement(r *Reader) error {
t, err := r.Bytes(2)
if err != nil {
return err
}
if !bytes.Equal(t, movementType) {
return nil
}
x, err := r.Float32()
if err != nil {
return err
}
y, err := r.Float32()
if err != nil {
return err
}
z, err := r.Float32()
if err != nil {
return err
}
if len(r.Movement) == 0 {
// TODO: how to ID the player and the timestamp??
// maybe there is a consistent update rate?
r.Movement = append(r.Movement, MovementUpdates{
Username: "N/A",
Data: []MovementUpdate{},
})
}
r.Movement[0].Data = append(r.Movement[0].Data, MovementUpdate{
X: x,
Y: y,
Z: z,
})
return nil
}
15 changes: 13 additions & 2 deletions dissect/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ type Reader struct {
planted bool
readPartial bool // reads up to the player info packets
playersRead int
Header Header `json:"header"`
MatchFeedback []MatchUpdate `json:"matchFeedback"`
Header Header `json:"header"`
MatchFeedback []MatchUpdate `json:"matchFeedback"`
Movement []MovementUpdates `json:"movement,omitempty"`
Scoreboard Scoreboard
}

Expand Down Expand Up @@ -68,6 +69,7 @@ func NewReader(in io.Reader) (r *Reader, err error) {
r.Listen([]byte{0x22, 0xA9, 0xC8, 0x58, 0xD9}, readDefuserTimer)
r.Listen([]byte{0xEC, 0xDA, 0x4F, 0x80}, readScoreboardScore)
r.Listen([]byte{0x4D, 0x73, 0x7F, 0x9E}, readScoreboardAssists)
r.Listen([]byte{0x60, 0x73, 0x85, 0xFE}, readMovement)
return r, err
}

Expand Down Expand Up @@ -342,6 +344,15 @@ func (r *Reader) Uint64() (uint64, error) {
return binary.LittleEndian.Uint64(b), nil
}

func (r *Reader) Float32() (float32, error) {
b, err := r.Bytes(4)
if err != nil {
return 0, err
}
u := binary.LittleEndian.Uint32(b)
return math.Float32frombits(u), nil
}

func (r *Reader) Write(w io.Writer) (n int, err error) {
return w.Write(r.b)
}
27 changes: 11 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package main

import (
"encoding/json"
"github.com/redraskal/r6-dissect/dissect"
"io"
"os"
"strings"

"github.com/redraskal/r6-dissect/dissect"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -50,12 +51,7 @@ func main() {
log.Fatal().Msg("dump requires a replay file input.")
}
if viper.GetBool("dump") {
outBin, err := os.OpenFile(out.Name()+".bin", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, os.ModePerm)
if err != nil {
log.Fatal().Err(err).Send()
}
defer outBin.Close()
if err := writeRoundDump(in, out, outBin); err != nil {
if err := writeRoundDump(in, out); err != nil {
log.Fatal().Err(err).Send()
}
return
Expand All @@ -78,8 +74,10 @@ func setup() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
pflag.StringP("format", "f", "", "specifies the output format (json, excel)")
pflag.StringP("output", "o", "", "specifies the output path")
// TODO: implement this flag
pflag.StringP("show", "s", "", "optionally provide additional data (movement)")
pflag.BoolP("debug", "d", false, "sets log level to debug")
pflag.BoolP("dump", "p", false, "dumps packets to the output")
pflag.BoolP("dump", "p", false, "dumps decompressed replay to the output")
pflag.Bool("info", false, "prints the replay header")
pflag.BoolP("version", "v", false, "prints the version")
pflag.Parse()
Expand Down Expand Up @@ -170,6 +168,7 @@ func writeRound(in io.Reader, out io.Writer) error {
dissect.Header
MatchFeedback []dissect.MatchUpdate `json:"matchFeedback"`
PlayerStats []dissect.PlayerRoundStats `json:"stats"`
Movement []dissect.MovementUpdates `json:"movement,omitempty"`
}
if err := r.Read(); !dissect.Ok(err) {
return err
Expand All @@ -179,21 +178,17 @@ func writeRound(in io.Reader, out io.Writer) error {
r.Header,
r.MatchFeedback,
r.PlayerStats(),
r.Movement,
})
}

func writeRoundDump(in io.Reader, out *os.File, outBin *os.File) error {
func writeRoundDump(in io.Reader, out *os.File) error {
r, err := dissect.NewReader(in)
if err != nil {
return err
}
if _, err := r.Write(outBin); err != nil {
return err
}
if err := r.Dump(out); !dissect.Ok(err) {
return err
}
return nil
_, err = r.Write(out)
return err
}

func piped(f *os.File) bool {
Expand Down
Loading