-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
204 lines (190 loc) · 4.59 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import (
"encoding/json"
"io"
"os"
"strings"
"github.com/redraskal/r6-dissect/dissect"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
var Version = "dev"
type OutputFormat = string
const (
JSON OutputFormat = "json"
Excel OutputFormat = "excel"
)
func main() {
setup()
format := viper.GetString("format")
in, err := viperFileOrDefault("input", os.Stdin, os.O_RDONLY)
if err != nil {
log.Fatal().Err(err).Send()
}
defer in.Close()
out, err := viperFileOrDefault("output", os.Stdout, os.O_CREATE|os.O_TRUNC|os.O_WRONLY)
if err != nil {
log.Fatal().Err(err).Send()
}
defer out.Close()
stat, err := in.Stat()
if err != nil {
log.Fatal().Err(err).Send()
}
if viper.GetBool("info") {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
if err := printHead(in); err != nil {
log.Fatal().Err(err).Send()
}
return
}
if viper.GetBool("dump") && stat.IsDir() {
log.Fatal().Msg("dump requires a replay file input.")
}
if viper.GetBool("dump") {
if err := writeRoundDump(in, out); err != nil {
log.Fatal().Err(err).Send()
}
return
}
if stat.IsDir() {
if err := writeMatch(in, format, out); err != nil {
log.Fatal().Err(err).Send()
}
return
}
if format == Excel {
log.Fatal().Msg("Dissect will only export a match folder to Excel.")
}
if err = writeRound(in, out); err != nil {
log.Fatal().Err(err).Send()
}
}
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")
pflag.BoolP("debug", "d", false, "sets log level to debug")
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()
if err := viper.BindPFlags(pflag.CommandLine); err != nil {
log.Fatal().Err(err)
}
if viper.GetBool("debug") {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
} else {
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
}
if viper.GetBool("version") {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
log.Info().Msgf("r6-dissect version: %s", Version)
log.Info().Msg("https://github.com/redraskal/r6-dissect")
os.Exit(0)
}
extra := len(pflag.Args())
if extra < 1 && !piped(os.Stdin) {
log.Fatal().Msg("Specify a valid match replay file/folder path (*.rec files)")
} else if extra > 0 {
viper.Set("input", pflag.Args()[0])
}
if !viper.IsSet("format") {
output := viper.GetString("output")
if strings.HasSuffix(output, ".xlsx") {
viper.Set("format", "excel")
} else if strings.HasSuffix(output, ".json") {
viper.Set("format", "json")
}
}
format := strings.ToLower(viper.GetString("format"))
if len(format) > 0 && !(format == "json" || format == "excel") {
log.Fatal().Msg("Specify a valid output format (json, excel)")
} else if len(format) == 0 {
viper.Set("format", "json")
}
}
func printHead(in *os.File) error {
stat, err := in.Stat()
if err != nil {
return err
}
if stat.IsDir() {
m, err := dissect.NewMatchReader(in)
if err != nil {
return err
}
r, err := m.FirstRound()
if err != nil {
return err
}
r.Head()
return nil
}
r, err := dissect.NewReader(in)
if err != nil {
return err
}
if err := r.ReadPartial(); !dissect.Ok(err) {
return err
}
r.Head()
return nil
}
func writeMatch(in *os.File, format OutputFormat, out io.Writer) error {
m, err := dissect.NewMatchReader(in)
if err != nil {
return err
}
if err := m.Read(); !dissect.Ok(err) {
return err
}
if format == Excel {
return m.WriteExcel(out)
}
return m.WriteJSON(out)
}
func writeRound(in io.Reader, out io.Writer) error {
r, err := dissect.NewReader(in)
if err != nil {
return err
}
type output struct {
dissect.Header
MatchFeedback []dissect.MatchUpdate `json:"matchFeedback"`
PlayerStats []dissect.PlayerRoundStats `json:"stats"`
}
if err := r.Read(); !dissect.Ok(err) {
return err
}
encoder := json.NewEncoder(out)
return encoder.Encode(output{
r.Header,
r.MatchFeedback,
r.PlayerStats(),
})
}
func writeRoundDump(in io.Reader, out *os.File) error {
r, err := dissect.NewReader(in)
if err != nil {
return err
}
_, err = r.Write(out)
return err
}
func piped(f *os.File) bool {
stat, err := f.Stat()
if err != nil {
return false
}
return stat.Mode()&os.ModeNamedPipe != 0
}
func viperFileOrDefault(key string, def *os.File, flag int) (*os.File, error) {
val := viper.GetString(key)
if len(val) > 0 {
return os.OpenFile(val, flag, os.ModePerm)
}
return def, nil
}