-
Notifications
You must be signed in to change notification settings - Fork 2
/
jambon.go
68 lines (51 loc) · 1.1 KB
/
jambon.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
package jambon
import (
"io"
"runtime"
"sort"
"github.com/b1naryth1ef/jambon/tacview"
)
type JambonProcessor interface {
ProcessFile(*tacview.Reader) error
}
type JambonNoopProcessor struct {
dest io.WriteCloser
}
func NewJambonNoopProcessor(dest io.WriteCloser) *JambonNoopProcessor {
return &JambonNoopProcessor{dest: dest}
}
func (j *JambonNoopProcessor) ProcessFile(source *tacview.Reader) error {
done := make(chan struct{})
timeFrames := make(chan *tacview.TimeFrame)
collected := make([]*tacview.TimeFrame, 0)
go func() {
defer close(done)
for {
tf, ok := <-timeFrames
if !ok {
return
}
collected = append(collected, tf)
}
}()
err := source.ProcessTimeFrames(runtime.GOMAXPROCS(-1), timeFrames)
if err != nil {
return err
}
<-done
sort.Slice(collected, func(i, j int) bool {
return collected[i].Offset < collected[j].Offset
})
writer, err := tacview.NewWriter(j.dest, &source.Header)
if err != nil {
return err
}
defer writer.Close()
for _, tf := range collected {
err = writer.WriteTimeFrame(tf)
if err != nil {
return err
}
}
return nil
}