-
Notifications
You must be signed in to change notification settings - Fork 2
/
command_normalize.go
160 lines (135 loc) · 3.45 KB
/
command_normalize.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
package jambon
import (
"fmt"
"io"
"runtime"
"sort"
"strings"
"github.com/b1naryth1ef/jambon/tacview"
"github.com/urfave/cli/v2"
)
const normalizeDescription = `Normalize an ACMI file by completely rewriting it. If the output file is zip encoded
the internal ACMI text file will be placed in the root of the zip, ignoring any
directory structure from the input file. Memory overhead can be massively reduced at
the cost of speed by setting the concurrency flag to 1.`
// CommandNormalize handles rewriting ACMI files
var CommandNormalize = cli.Command{
Name: "normalize",
Description: normalizeDescription,
Action: commandNormalize,
Flags: []cli.Flag{
&cli.PathFlag{
Name: "input",
Usage: "path to the input ACMI file",
Required: true,
},
&cli.PathFlag{
Name: "output",
Usage: "path to the output ACMI file",
Required: true,
},
&cli.StringSliceFlag{
Name: "exclude-property",
Usage: "provide a key=value property pair that will cause matching objects to be excluded from the output",
},
&cli.IntFlag{
Name: "concurrency",
Usage: "number of parallel processing routines to run",
Value: runtime.GOMAXPROCS(-1),
},
},
}
func commandNormalize(ctx *cli.Context) error {
excludeProperties := make(map[string]string)
for _, property := range ctx.StringSlice("exclude-property") {
parts := strings.SplitN(property, "=", 2)
if len(parts) != 2 {
return fmt.Errorf("Failed to process exclude property '%v'", property)
}
excludeProperties[parts[0]] = parts[1]
}
inputFile, err := openReadableTacView(ctx.Path("input"))
if err != nil {
return err
}
outputFile, err := openWritableTacView(ctx.Path("output"))
if err != nil {
return err
}
reader, err := tacview.NewReader(inputFile)
if err != nil {
return err
}
return normalize(ctx.Int("concurrency"), reader, outputFile, func(o *tacview.Object) bool {
if len(excludeProperties) == 0 {
return true
}
for k, v := range excludeProperties {
prop := o.Get(k)
if prop != nil && prop.Value == v {
return false
}
}
return true
})
}
func normalize(concurrency int, input *tacview.Reader, output io.WriteCloser, filter func(o *tacview.Object) bool) error {
done := make(chan error)
timeFrames := make(chan *tacview.TimeFrame)
writer, err := tacview.NewWriter(output, &input.Header)
if err != nil {
return err
}
defer writer.Close()
collected := make([]*tacview.TimeFrame, 0)
filteredObjects := make(map[uint64]struct{})
go func() {
defer close(done)
for {
tf, ok := <-timeFrames
if !ok {
return
}
for _, object := range tf.Objects {
_, isFiltered := filteredObjects[object.Id]
if object.Deleted && isFiltered {
delete(filteredObjects, object.Id)
} else if isFiltered {
tf.Delete(object.Id)
} else if !filter(object) {
filteredObjects[object.Id] = struct{}{}
tf.Delete(object.Id)
}
}
if concurrency == 1 {
err := writer.WriteTimeFrame(tf)
if err != nil {
done <- err
return
}
} else {
collected = append(collected, tf)
}
}
}()
err = input.ProcessTimeFrames(concurrency, timeFrames)
if err != nil {
return err
}
err = <-done
if err != nil {
return err
}
if concurrency != 1 {
sort.Slice(collected, func(i, j int) bool {
return collected[i].Offset < collected[j].Offset
})
for _, tf := range collected {
err = writer.WriteTimeFrame(tf)
if err != nil {
return err
}
}
}
return nil
}