-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmain.go
141 lines (120 loc) · 2.81 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
// Author: Niels A.D.
// Project: gowarcraft3 (https://github.com/nielsAD/gowarcraft3)
// License: Mozilla Public License, v2.0
// w3gdump is a tool that decodes and dumps w3g files.
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"reflect"
"strings"
"github.com/nielsAD/gowarcraft3/file/w3g"
"github.com/nielsAD/gowarcraft3/network"
)
var (
sanitize = flag.String("sanitize", "", "Dump cleaned up replay to this file (no chat, sane colors)")
header = flag.Bool("header", false, "Decode header only")
stream = flag.Bool("stream", false, "Stream game to LAN")
jsonout = flag.Bool("json", false, "Print machine readable format")
)
var logOut = log.New(os.Stdout, "", 0)
var logErr = log.New(os.Stderr, "", 0)
func print(v interface{}) {
var str = fmt.Sprintf("%+v", v)[1:]
if *jsonout {
if json, err := json.Marshal(v); err == nil {
str = string(json)
}
}
logOut.Printf("%-14v %v\n", reflect.TypeOf(v).String()[5:], str)
}
func main() {
flag.Parse()
var filename = strings.Join(flag.Args(), " ")
if *stream {
if err := cast(filename); err != nil && !network.IsCloseError(err) {
logErr.Fatal("Stream error: ", err)
}
return
}
f, err := os.Open(filename)
if err != nil {
logErr.Fatal("Open error: ", err)
}
defer f.Close()
// Find header, nwg files have their own header prepended
var b = bufio.NewReaderSize(f, 8192)
if _, err := w3g.FindHeader(b); err != nil {
logErr.Fatal("Cannot find header: ", err)
}
hdr, data, _, err := w3g.DecodeHeader(b, w3g.NewFactoryCache(w3g.DefaultFactory))
if err != nil {
logErr.Fatal("DecodeHeader error: ", err)
}
var enc *w3g.Encoder
if *sanitize != "" {
o, err := os.Create(*sanitize)
if err != nil {
logErr.Fatal("Open error: ", err)
}
defer o.Close()
enc, err = w3g.NewEncoder(o, hdr.Encoding())
if err != nil {
logErr.Fatal("NewEncoder error: ", err)
}
enc.Header = *hdr
}
var skip = false
var maxp uint8 = 24
if hdr.GameVersion.Version < 29 {
maxp = 12
}
print(hdr)
if err := data.ForEach(func(r w3g.Record) error {
if enc != nil {
var write = true
switch v := r.(type) {
case *w3g.ChatMessage:
write = false
case *w3g.SlotInfo:
var c = uint8(0)
for i := range v.Slots {
if v.Slots[i].Team >= maxp {
continue
}
v.Slots[i].Color = c
c++
}
}
if write {
if _, err := enc.WriteRecord(r); err != nil {
return err
}
}
}
if !skip && *header {
switch r.(type) {
case *w3g.CountDownStart, *w3g.CountDownEnd, *w3g.GameStart:
if enc == nil {
return errBreakEarly
}
skip = true
}
}
if !skip {
print(r)
}
return nil
}); err != nil && err != errBreakEarly {
logErr.Fatal("Data error: ", err)
}
if enc != nil {
if err := enc.Close(); err != nil {
logErr.Fatal("Save error: ", err)
}
}
}