diff --git a/README.md b/README.md index 68c5042..e6f911c 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/cmd/csminify/main.go b/cmd/csminify/main.go index dc1146f..47aad01 100644 --- a/cmd/csminify/main.go +++ b/cmd/csminify/main.go @@ -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" @@ -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") } @@ -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 { @@ -73,6 +80,7 @@ func minify(demPath string, freq float64, format string, outPath string) error { } var in io.Reader + switch demPath { case "": in = os.Stdin @@ -80,12 +88,14 @@ func minify(demPath string, freq float64, format string, outPath string) error { 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 @@ -93,6 +103,7 @@ func minify(demPath string, freq float64, format string, outPath string) error { f, err := os.Create(outPath) defer f.Close() out = f + if err != nil { return err } diff --git a/csminify.go b/csminify.go index edad2f0..ea7ba87 100644 --- a/csminify.go +++ b/csminify.go @@ -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 } @@ -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 } @@ -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 } @@ -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 { @@ -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 {