Skip to content
This repository has been archived by the owner on Jan 22, 2024. It is now read-only.

Commit

Permalink
hande ErrUnexpectedEndOfDemo and exit with code 3 (#46) (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
markus-wa authored Jul 4, 2020
1 parent 267f51b commit 61ff21b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ Usage of csminify:
Snapshot frequency - per second (default 0.5)
-out path
Output file path (default stdout)
May exit with code 3 if a demo ends unexpectedly, but the minified data may still be usable if this happens
Direct bug reports and feature requests to https://github.com/markus-wa/cs-demo-minifier
```


Expand Down
13 changes: 12 additions & 1 deletion cmd/csminify/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"os"

demoinfocs "github.com/markus-wa/demoinfocs-golang/v2/pkg/demoinfocs"
msgpack "gopkg.in/vmihailenco/msgpack.v2"

min "github.com/markus-wa/cs-demo-minifier"
Expand All @@ -20,6 +21,8 @@ func main() {
fmt.Fprintln(os.Stderr, "Usage of csminify:")
fl.PrintDefaults()
fmt.Fprintln(os.Stderr)
fmt.Fprintln(os.Stderr, "May exit with code 3 if a demo ends unexpectedly, but the minified data may still be usable if this happens")
fmt.Fprintln(os.Stderr)
fmt.Fprintln(os.Stderr, "Direct bug reports and feature requests to https://github.com/markus-wa/cs-demo-minifier")
}

Expand All @@ -40,13 +43,17 @@ func main() {
outPath := *outPathPtr

err = minify(demPath, freq, format, outPath)
if err != nil {
if err == demoinfocs.ErrUnexpectedEndOfDemo {
fmt.Fprintln(os.Stderr, "WARNING: encountered unexpected end of demo, but the minified data may still be usable")
os.Exit(3)
} else if err != nil {
panic(err)
}
}

func minify(demPath string, freq float64, format string, outPath string) error {
var marshaller min.ReplayMarshaller

switch format {
case "json":
marshaller = func(replay rep.Replay, w io.Writer) error {
Expand All @@ -73,26 +80,30 @@ func minify(demPath string, freq float64, format string, outPath string) error {
}

var in io.Reader

switch demPath {
case "":
in = os.Stdin
default:
f, err := os.Open(demPath)
defer f.Close()
in = f

if err != nil {
return err
}
}

var out io.Writer

switch outPath {
case "":
out = os.Stdout
default:
f, err := os.Create(outPath)
defer f.Close()
out = f

if err != nil {
return err
}
Expand Down
22 changes: 16 additions & 6 deletions csminify.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ func Minify(r io.Reader, snapFreq float64, marshal ReplayMarshaller) ([]byte, er
func MinifyWithConfig(r io.Reader, cfg ReplayConfig, marshal ReplayMarshaller) ([]byte, error) {
var buf bytes.Buffer
err := MinifyToWithConfig(r, cfg, marshal, bufio.NewWriter(&buf))

if err != nil {
return nil, err
}

return buf.Bytes(), nil
}

Expand All @@ -43,11 +45,21 @@ func MinifyTo(r io.Reader, snapFreq float64, marshal ReplayMarshaller, w io.Writ
// See also: ToReplayWithConfig
func MinifyToWithConfig(r io.Reader, cfg ReplayConfig, marshal ReplayMarshaller, w io.Writer) error {
replay, err := ToReplayWithConfig(r, cfg)
if err != nil {

if err == dem.ErrUnexpectedEndOfDemo {
err = marshal(replay, w)

if err != nil {
return err
}

return dem.ErrUnexpectedEndOfDemo
} else if err != nil {
return err
}

err = marshal(replay, w)

return err
}

Expand Down Expand Up @@ -79,6 +91,7 @@ func ToReplayWithConfig(r io.Reader, cfg ReplayConfig) (rep.Replay, error) {
// TODO: Provide a way to pass on warnings to the caller
p := dem.NewParser(r)
header, err := p.ParseHeader()

if err != nil {
return rep.Replay{}, err
}
Expand All @@ -105,11 +118,8 @@ func ToReplayWithConfig(r io.Reader, cfg ReplayConfig) (rep.Replay, error) {
m.parser.RegisterEventHandler(m.frameDone)

err = p.ParseToEnd()
if err != nil {
return rep.Replay{}, err
}

return m.replay, nil
return m.replay, err
}

type minifier struct {
Expand All @@ -130,7 +140,7 @@ func newMinifier(parser dem.Parser, eventCollector *EventCollector, snapshotFreq
}
}

func (m *minifier) frameDone(e events.FrameDone) {
func (m *minifier) frameDone(events.FrameDone) {
tick := m.parser.CurrentFrame()
// Is it snapshot o'clock?
if tick%m.replay.Header.SnapshotRate == 0 {
Expand Down

0 comments on commit 61ff21b

Please sign in to comment.