-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinline.vb
395 lines (294 loc) · 11.9 KB
/
inline.vb
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Data
Imports System.Drawing
Imports System.Diagnostics
Imports System.Windows.Forms
Imports System.Linq
Imports System.Xml.Linq
Imports System.Threading.Tasks
Imports System.IO
Imports System.Text.RegularExpressions
Imports System.Web.Script.Serialization
Imports System.Xml
Imports System.Xml.Serialization
Public Class VAInline
Public Sub Main()
' Set the directory locations that we want to search in
Dim dir_locations = {
Environ$("USERPROFILE")+"\Saved Games\DCS.openbeta\Datatape",
Environ$("USERPROFILE")+"\Saved Games\DCS\Datatape",
"C:\Program Files (x86)\VoiceAttack\Datatape"
}
' directory and file related variables
Dim dir_found as Boolean = false
Dim datatape_dir as String
Dim dir_loc as integer = 0
Dim file_found as Boolean = false
Dim xml_file_found as Boolean = false
Dim datatape_file as string
Dim file_code as string
Dim pos as integer = 2
Dim current_pos as string
' Clear all variabls firs
while pos < 101
VA.SetText("waypoint_"+pos.toString(), nothing)
VA.SetText("latitude_"+pos.toString(), nothing)
VA.SetText("longitude_"+pos.toString(), nothing)
VA.SetText("altitude_"+pos.toString(), nothing)
pos = pos + 1
end while
pos = 2
' Look for the available directories and pick the one that works the best based on the order within the list
while dir_found = false and dir_loc < dir_locations.length
If Directory.Exists(dir_locations(dir_loc)) Then
dir_found = true
datatape_dir = dir_locations(dir_loc)
VA.WriteToLog("Reading data from directory: "+datatape_dir, "blue")
End If
dir_loc = dir_loc + 1
end while
' If no directory was found, then it needs to be created/fixed. Output some insturctions on how to do so.
If dir_found = false then
If file_code <> nothing then
VA.WriteToLog("Advanced File Format: datatape_"+file_code+"_<ANYTHING_YOU_WANT_HERE>.txt", "red")
Else
VA.WriteToLog("Advanced File Format: datatape_alpha_<ANYTHING_YOU_WANT_HERE>.txt", "red")
End If
VA.WriteToLog("Basic File Format: datatape.txt", "red")
VA.WriteToLog("File name must match one of the following formats:", "red")
VA.WriteToLog(dir_locations(2), "red")
VA.WriteToLog(dir_locations(1), "red")
VA.WriteToLog(dir_locations(0), "red")
VA.WriteToLog("In order for datatape to run, you need to create a datatape file in one of the following locations:", "red")
VA.WriteToLog("Could not locate data file in directory: "+datatape_dir, "red")
return
End If
' If there is a file code like alpha, bravo, etc then we need to load it in now and flush the variable after
file_code = VA.GetText("file_code")
VA.SetText("file_code", nothing)
' If there is no file code then just load the old style datatape.txt file. Otherwise load one of the new style
' Prefer XML files and fall back to TXT files
If file_code = "" then
If File.exists(datatape_dir + "\datatape.xml") then
datatape_file = datatape_dir + "\datatape.xml"
file_found = true
xml_file_found = true
Else
If File.exists(datatape_dir + "\datatape.txt") then
datatape_file = datatape_dir + "\datatape.txt"
file_found = true
End If
End If
Else
datatape_file = Dir(datatape_dir + "\datatape_" + file_code + "*.xml")
If datatape_file <> "" then
datatape_file = datatape_dir + "\" + datatape_file
file_found = true
xml_file_found = true
Else
datatape_file = Dir(datatape_dir + "\datatape_" + file_code + "*.txt")
If datatape_file <> "" then
datatape_file = datatape_dir + "\" + datatape_file
file_found = true
End If
End If
End If
' No files were found and this is bad. Show instructions on what to do.
If file_found = false then
If file_code <> nothing then
VA.WriteToLog("Advanced File Format: datatape_"+file_code+"_<ANYTHING_YOU_WANT_HERE>.txt", "red")
Else
VA.WriteToLog("Advanced File Format: datatape_alpha_<ANYTHING_YOU_WANT_HERE>.txt", "red")
End If
VA.WriteToLog("Basic File Format: datatape.txt", "red")
VA.WriteToLog("File name must match one of the following formats:", "red")
VA.WriteToLog("Could not locate compatible datatape file", "red")
return
End If
' Main loop. Read the file, trim some fat, and get it plugged into vars
VA.WriteToLog("Reading from datatape file: "+ datatape_file, "blue")
' Run a dIfferent loop for XML files.
If xml_file_found = true then
Dim doc As New XmlDocument()
doc.Load(datatape_file)
Dim xml As String = File.ReadAllText(datatape_file)
Dim stpt as integer = 1
Dim sSerialize As Serializer = New Serializer()
Dim objects As Objects = sSerialize.Deserialize(Of Objects)(xml)
For Each xml_waypoint As Waypoint In objects.Waypoints
If stpt > 1 then
Dim formatted_lat as decimal = Math.Round(xml_waypoint.position.latitude, 6, MidpointRounding.AwayFromZero)
Dim formatted_lng as decimal = Math.Round(xml_waypoint.position.longitude, 6, MidpointRounding.AwayFromZero)
Dim latitude as string = ConvertLatToDM(formatted_lat)
Dim longitude as string = ConvertLongToDM(formatted_lng)
Dim waypoint as string = stpt
Dim altitude as string = ConvertMetersToFeet(xml_waypoint.position.altitude)
' Write to logs and set the variables
VA.WriteToLog("loading waypoint from file: "+ waypoint + " " + latitude + " " + longitude + " " + altitude, "blue")
VA.SetText("waypoint_"+waypoint, waypoint)
VA.SetText("latitude_"+waypoint, latitude)
VA.SetText("longitude_"+waypoint, longitude)
VA.SetText("altitude_"+waypoint, altitude)
End if
stpt = stpt + 1
Next
'For Each item As Dictionary(Of String, Object) In data("drawings")
'
' If item("type") = "circle" then
' If stpt > 1 then
' VA.WriteToLog("Auto Loading Steerpoint "+stpt.toString()+"- " + item("name"), "green")
'
' Dim lat as decimal = item("center_y")
' Dim formatted_lat as decimal = Math.Round(lat, 6, MidpointRounding.AwayFromZero)
'
' Dim lng as decimal = item("center_x")
' Dim formatted_lng as decimal = Math.Round(lng, 6, MidpointRounding.AwayFromZero)
'
' Dim latitude as string = ConvertLatToDM(formatted_lat)
' Dim longitude as string = ConvertLongToDM(formatted_lng)
' Dim waypoint as string = stpt
' Dim altitude as string = "0"
' ' Write to logs and set the variables
' VA.SetText("waypoint_"+waypoint, waypoint)
' VA.SetText("latitude_"+waypoint, latitude)
' VA.SetText("longitude_"+waypoint, longitude)
' VA.SetText("altitude_"+waypoint, altitude)
' End If
' stpt = stpt + 1
'
' End If
'
'Next
Else
for each line as string in File.ReadLines(datatape_file)
' We can comment out lines by prepending a #
If line.contains("#") = false then
' Trim out any comments within [BRACKETS]
line = Regex.replace(line, "\s?\[(.*)\]", "")
Dim rowdata As String() = line.Split(New Char() {":"c})
If rowdata(0) <> "1" Then
' Just in case there are typos, trim them out
line = trim(line)
line = Regex.Replace(line, " {2,}", " ")
line = line.replace("'","").replace(":","").replace("""", "").replace("FT","").replace("/", " ").replace("\", " ")
Dim values As String() = line.Split(New Char() {" "c})
If values.length = 4 Then
' Lots of sanity checking going on here
Dim waypoint as string = values(0)
Dim latitude as string = values(1)
Dim longitude as string = values(2)
Dim altitude as string = values(3)
Dim latitude_parts As String() = latitude.Split(New Char() {"."c})
Dim longitude_parts As String() = longitude.Split(New Char() {"."c})
Dim latitude_hm as String = latitude_parts(0)
Dim latitude_sec as String = latitude_parts(1)
Dim longitude_hm as String = longitude_parts(0)
Dim longitude_sec as String = longitude_parts(1)
' We need to verIfy the right data is in the E/W coordinates
If longitude_parts.contains("E0") = False And longitude_parts.contains("W0") = False And longitude_hm.length <> 6 Then
longitude = longitude.replace("E", "E0").replace("W","W0")
End If
' Replace NSEW with numbers
latitude = latitude.replace("N","2").replace("S","8").replace(".", "")
longitude = longitude.replace("E","6").replace("W","4").replace(".", "")
altitude = altitude.replace(",","")
' Append zeroes to any values shorter than required
while latitude.length < 8
latitude = latitude + "0"
end while
while longitude.length < 9
longitude = longitude + "0"
end while
' Write to logs and set the variables
VA.WriteToLog("loading waypoint from file: "+ waypoint + " " + latitude + " " + longitude + " " + altitude, "blue")
VA.SetText("waypoint_"+waypoint, waypoint)
VA.SetText("latitude_"+waypoint, latitude)
VA.SetText("longitude_"+waypoint, longitude)
VA.SetText("altitude_"+waypoint, altitude)
End If
' increment position for later
pos = pos + 1
End If
End If
Next
End If
End Sub
Function ConvertMetersToFeet(ByVal mm As decimal) As string
Dim result as decimal = Math.Round(mm * 3.28084)
Dim feet as integer = Math.Truncate(result)
ConvertMetersToFeet = feet.toString
End Function
Function ConvertLatToDM(ByVal dd As decimal) As string
Dim result as string = ""
Dim deg as integer = Math.Truncate(dd)
Dim fraction as decimal = Math.Abs(dd) - Math.Abs(deg)
If deg > 0 then
result = "2"
Else
deg = Math.Abs(deg)
result = "8"
End If
result = result + deg.toString()
Dim minutes as decimal = fraction * 60
minutes = Math.Round(minutes, 3, MidpointRounding.AwayFromZero)
If minutes < 10 then
result = result + "0"
End If
minutes = Math.Round(minutes*1000, 0, MidpointRounding.AwayFromZero)
ConvertLatToDM = result + minutes.toString()
End Function
Function ConvertLongToDM(ByVal dd As decimal) As string
Dim result as string = ""
Dim deg as integer = Math.Truncate(dd)
Dim fraction as decimal = Math.Abs(dd) - Math.Abs(deg)
If deg > 0 then
result = "6"
Else
deg = Math.Abs(deg)
result = "4"
End If
If deg < 100 then
result = result + "0"
End If
result = result + deg.toString()
Dim minutes as decimal = fraction * 60
minutes = Math.Round(minutes, 3, MidpointRounding.AwayFromZero)
If minutes < 10 then
result = result + "0"
End If
minutes = Math.Round(minutes*1000, 0, MidpointRounding.AwayFromZero)
ConvertLongToDM = result + minutes.toString()
End Function
End Class
Public Class Objects
Public Property Waypoints As List(Of Waypoint)
End Class
Public Class Waypoint
Public Property Name As String
Public Property Position As Position
End Class
Public Class Position
Public Property Latitude As decimal
Public Property Longitude As decimal
Public Property Altitude As decimal
End Class
Public Class Serializer
Public Function Deserialize(Of T As Class) _
(ByVal input As String) As T
Dim ser As XmlSerializer = New XmlSerializer(GetType(T))
Using sr As StringReader = New StringReader(input)
Return CType(ser.Deserialize(sr), T)
End Using
End Function
Public Function Serialize(Of T)(ByVal ObjectToSerialize As T) _
As String
Dim xmlSerializer As XmlSerializer = New XmlSerializer(ObjectToSerialize.[GetType]())
Using textWriter As StringWriter = New StringWriter()
xmlSerializer.Serialize(textWriter, ObjectToSerialize)
Return textWriter.ToString()
End Using
End Function
End Class