forked from b1naryth1ef/lardoon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
import.go
260 lines (221 loc) · 5.27 KB
/
import.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package lardoon
import (
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"github.com/b1naryth1ef/jambon"
"github.com/b1naryth1ef/jambon/tacview"
)
var nonHumanSlotRe = regexp.MustCompile(`(.+)#\d\d\d-\d\d`)
type objectData struct {
Id uint64
Types string
Name string
Pilot string
CreatedOffset int
DeletedOffset int
}
func ImportPath(path string) error {
stat, err := os.Stat(path)
if err != nil {
return err
}
if !stat.Mode().IsRegular() {
entries, err := os.ReadDir(path)
if err != nil {
return err
}
for _, entry := range entries {
path := filepath.Join(path, entry.Name())
// Because Windows only updates file sizes on a CloseHandle() call, we need to open and then close the file
// to get the real size of a file that might still being written to, otherwise it might return 0kb.
// See: https://devblogs.microsoft.com/oldnewthing/20111226-00/?p=8813
file, err := os.Open(path)
if err != nil {
log.Printf("Cannot open file %v", path)
continue
}
file.Close()
info, err := os.Stat(path)
if err != nil {
log.Printf("Cannot Stat() file %v", path)
continue
}
if info.Size() == 0 {
log.Printf("Skipping empty file %v", path)
continue
}
if info.IsDir() {
err := ImportPath(path)
if err != nil {
return err
}
} else {
if strings.HasSuffix(path, ".acmi") {
err := ImportFile(path)
if err != nil {
log.Printf("Failed to process path %v: %v", path, err)
}
}
}
}
return nil
}
return ImportFile(path)
}
func ImportFile(target string) error {
var err error
target, err = filepath.Abs(target)
if err != nil {
return err
}
stat, err := os.Stat(target)
if err != nil {
return err
}
file, err := jambon.OpenReadableTacView(target)
if err != nil {
return err
}
reader, err := tacview.NewReader(file)
if err != nil {
return err
}
rootObject := reader.Header.InitialTimeFrame.Get(0)
recordingTime := rootObject.Get("RecordingTime")
if recordingTime == nil {
return fmt.Errorf("Malformed Tacview file: missing property: RecordingTime")
}
title := rootObject.Get("Title")
if title == nil {
log.Printf("Warning: missing property Title, using empty string as default")
title = &tacview.Property{Key: "Title", Value: ""}
}
dataSource := rootObject.Get("DataSource")
if dataSource == nil {
return fmt.Errorf("Malformed Tacview file: missing property: DataSource")
}
dataRecorder := rootObject.Get("DataRecorder")
if dataRecorder == nil {
return fmt.Errorf("Malformed Tacview file: missing property: DataRecorder")
}
tx, err := beginTransaction()
if err != nil {
return err
}
defer tx.Rollback()
replayId, err := createReplay(
tx,
target,
reader.Header.ReferenceTime.String(),
recordingTime.Value,
title.Value,
dataSource.Value,
dataRecorder.Value,
int(stat.Size()),
)
if err != nil {
return err
}
if replayId == -1 {
return nil
}
log.Printf("Importing replay %v (#%v)", target, replayId)
timeFrames := make(chan *tacview.TimeFrame)
objects := make(map[uint64]*objectData)
done := make(chan struct{})
var lastFrame int
var firstFrame int
go func() {
defer close(done)
for {
tf, ok := <-timeFrames
if !ok {
return
}
if int(tf.Offset) > 0 && int(tf.Offset) < firstFrame {
firstFrame = int(tf.Offset)
}
if int(tf.Offset) > lastFrame {
lastFrame = int(tf.Offset)
}
for _, object := range tf.Objects {
_, exists := objects[object.Id]
if object.Deleted && exists {
objects[object.Id].DeletedOffset = int(tf.Offset)
err := createReplayObject(
tx,
replayId,
int(object.Id),
objects[object.Id].Types,
objects[object.Id].Name,
objects[object.Id].Pilot,
objects[object.Id].CreatedOffset,
objects[object.Id].DeletedOffset,
)
if err != nil {
panic(err)
}
delete(objects, object.Id)
} else if !exists {
types := object.Get("Type")
if types != nil {
if strings.Contains(types.Value, "Air") && (strings.Contains(types.Value, "FixedWing") || strings.Contains(types.Value, "Rotorcraft")) {
name := object.Get("Name").Value
pilotProp := object.Get("Pilot")
if pilotProp == nil {
continue
}
pilot := pilotProp.Value
groupProp := object.Get("Group")
if groupProp == nil {
continue
}
group := pilotProp.Value
result := nonHumanSlotRe.FindAllStringSubmatch(pilot, -1)
if len(result) > 0 && strings.HasPrefix(group, result[0][1]) {
continue
}
objects[object.Id] = &objectData{
Id: object.Id,
Name: name,
Pilot: pilot,
Types: types.Value,
CreatedOffset: int(tf.Offset),
}
}
}
}
}
}
}()
err = reader.ProcessTimeFrames(runtime.GOMAXPROCS(-1), timeFrames)
<-done
for _, object := range objects {
err := createReplayObject(
tx,
replayId,
int(object.Id),
object.Types,
object.Name,
object.Pilot,
object.CreatedOffset,
lastFrame,
)
if err != nil {
return err
}
}
if err != nil {
return err
}
err = setReplayDuration(tx, replayId, lastFrame-firstFrame)
if err != nil {
return err
}
return tx.Commit()
}