-
Notifications
You must be signed in to change notification settings - Fork 230
/
Copy pathTRWrapper.cs
376 lines (351 loc) · 15 KB
/
TRWrapper.cs
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
using System;
using System.Collections.Generic;
using System.Reflection;
using kOS.Safe.Encapsulation;
using kOS.Safe.Utilities;
using UnityEngine;
namespace kOS.AddOns.TrajectoriesAddon
{
public static class TRWrapper
{
private static bool? wrapped = null;
private static Type trajectoriesAPIType = null;
private static MethodInfo trGetTimeTillImpact = null;
private static MethodInfo trGetImpactPosition = null;
private static MethodInfo trCorrectedDirection = null;
private static MethodInfo trPlannedDirection = null;
private static MethodInfo trHasTarget = null;
private static MethodInfo trSetTarget = null;
private static MethodInfo trGetTarget = null;
private static MethodInfo trClearTarget = null;
private static MethodInfo trResetDescentProfile = null;
private static PropertyInfo trAlwaysUpdate = null;
private static PropertyInfo trProgradeEntry = null;
private static PropertyInfo trRetrogradeEntry = null;
private static PropertyInfo trDescentProfileAngles = null;
private static PropertyInfo trDescentProfileModes = null;
private static PropertyInfo trDescentProfileGrades = null;
private static Type GetType(string name)
{
Type type = null;
AssemblyLoader.loadedAssemblies.TypeOperation(t =>
{
if (t.FullName == name)
type = t;
});
return type;
}
private static void Init()
{
SafeHouse.Logger.Log("Attempting to Grab Trajectories Assembly...");
trajectoriesAPIType = GetType("Trajectories.API");
if (trajectoriesAPIType == null)
{
SafeHouse.Logger.Log("Trajectories API Type is null. Trajectories is not installed or is wrong version.");
wrapped = false;
return;
}
// Trajectories v2.2.0+ API Has version properties. Checking versions here.
if (trajectoriesAPIType.GetProperty("GetVersion") == null)
{
if (trajectoriesAPIType.GetMethod("HasTarget") == null) // assume pre v2.0.0 (Old API)
{
GetVersion = "";
GetVersionMajor = 0;
GetVersionMinor = 0;
GetVersionPatch = 0;
IsVerTwo = false;
IsVerTwoTwo = false;
IsVerTwoFour = false;
SafeHouse.Logger.Log("Checking Trajectories version: API.HasTarget method is null. Assuming version is pre 2.0.0");
}
else // assume v2.0.0 and v2.1.0 (API is identical in these versions)
{
GetVersion = "2.0.0";
GetVersionMajor = 2;
GetVersionMinor = 0;
GetVersionPatch = 0;
IsVerTwo = true;
IsVerTwoTwo = false;
IsVerTwoFour = false;
SafeHouse.Logger.Log("Checking Trajectories version: API.GetVersion method is null. Assuming version is pre 2.2.0");
}
}
else // assume v2.2.0 and above (New API)
{
GetVersion = (string)trajectoriesAPIType.GetProperty("GetVersion").GetValue(null, null);
Version version = new Version(GetVersion);
GetVersionMajor = version.Major;
GetVersionMinor = version.Minor;
GetVersionPatch = version.Build;
IsVerTwo = true;
IsVerTwoTwo = true;
// check for major versions above v2
if (version.Major > 2)
IsVerTwoFour = true;
else
IsVerTwoFour = (version.Major == 2 && version.Minor >= 4);
SafeHouse.Logger.Log("Checking Trajectories version: API.GetVersion returned version: v" + GetVersion);
}
// Method and property checking.
// Trajectories 2.0.0 changed the capitalization of this method. Trying both spellings here to support older Trajectories versions:
trGetImpactPosition = trajectoriesAPIType.GetMethod("GetImpactPosition") ?? trajectoriesAPIType.GetMethod("getImpactPosition");
if (trGetImpactPosition == null)
{
SafeHouse.Logger.Log("Trajectories.API.GetImpactPosition method is null.");
wrapped = false;
return;
}
// Trajectories 2.0.0 changed the capitalization of this method. Trying both spellings here to support older Trajectories versions:
trCorrectedDirection = trajectoriesAPIType.GetMethod("CorrectedDirection") ?? trajectoriesAPIType.GetMethod("correctedDirection");
if (trCorrectedDirection == null)
{
SafeHouse.Logger.Log("Trajectories.API.CorrectedDirection method is null.");
wrapped = false;
return;
}
// Trajectories 2.0.0 changed the capitalization of this method. Trying both spellings here to support older Trajectories versions:
trPlannedDirection = trajectoriesAPIType.GetMethod("PlannedDirection") ?? trajectoriesAPIType.GetMethod("plannedDirection");
if (trPlannedDirection == null)
{
SafeHouse.Logger.Log("Trajectories.API.PlannedDirection method is null.");
wrapped = false;
return;
}
// Trajectories 2.0.0 changed the capitalization of this method. Trying both spellings here to support older Trajectories versions:
trSetTarget = trajectoriesAPIType.GetMethod("SetTarget") ?? trajectoriesAPIType.GetMethod("setTarget");
if (trSetTarget == null)
{
SafeHouse.Logger.Log("Trajectories.API.SetTarget method is null.");
wrapped = false;
return;
}
// Trajectories 2.0.0 changed the capitalization of this method. Trying both spellings here to support older Trajectories versions:
trAlwaysUpdate = trajectoriesAPIType.GetProperty("AlwaysUpdate") ?? trajectoriesAPIType.GetProperty("alwaysUpdate");
if (trAlwaysUpdate == null)
{
SafeHouse.Logger.Log("Trajectories.API.AlwaysUpdate property is null.");
wrapped = false;
return;
}
trAlwaysUpdate.SetValue(null, true, null);
if ((bool)trAlwaysUpdate.GetValue(null, null) == false)
{
SafeHouse.Logger.Log("Trajectories.API.AlwaysUpdate was not set.");
wrapped = false;
return;
}
// Trajectories v2.0.0 HasTarget method
if (IsVerTwo)
{
trHasTarget = trajectoriesAPIType.GetMethod("HasTarget");
if (trHasTarget == null)
{
SafeHouse.Logger.Log("Trajectories.API.HasTarget method is null");
wrapped = false;
return;
}
}
// Trajectories v2.2.0 and above methods and properties
if (IsVerTwoTwo)
{
trGetTimeTillImpact = trajectoriesAPIType.GetMethod("GetTimeTillImpact");
if (trGetTimeTillImpact == null)
{
SafeHouse.Logger.Log("Trajectories.API.GetTimeTillImpact method is null");
wrapped = false;
return;
}
trProgradeEntry = trajectoriesAPIType.GetProperty("ProgradeEntry");
if (trProgradeEntry == null)
{
SafeHouse.Logger.Log("Trajectories.API.ProgradeEntry property is null");
wrapped = false;
return;
}
trRetrogradeEntry = trajectoriesAPIType.GetProperty("RetrogradeEntry");
if (trRetrogradeEntry == null)
{
SafeHouse.Logger.Log("Trajectories.API.RetrogradeEntry property is null");
wrapped = false;
return;
}
}
// Trajectories v2.4.0 and above methods and properties
if (IsVerTwoFour)
{
trGetTarget = trajectoriesAPIType.GetMethod("GetTarget");
if (trGetTarget == null)
{
SafeHouse.Logger.Log("Trajectories.API.GetTarget method is null");
wrapped = false;
return;
}
trClearTarget = trajectoriesAPIType.GetMethod("ClearTarget");
if (trClearTarget == null)
{
SafeHouse.Logger.Log("Trajectories.API.ClearTarget method is null");
wrapped = false;
return;
}
trResetDescentProfile = trajectoriesAPIType.GetMethod("ResetDescentProfile");
if (trResetDescentProfile == null)
{
SafeHouse.Logger.Log("Trajectories.API.ResetDescentProfile method is null");
wrapped = false;
return;
}
trDescentProfileAngles = trajectoriesAPIType.GetProperty("DescentProfileAngles");
if (trDescentProfileAngles == null)
{
SafeHouse.Logger.Log("Trajectories.API.DescentProfileAngles property is null");
wrapped = false;
return;
}
trDescentProfileModes = trajectoriesAPIType.GetProperty("DescentProfileModes");
if (trDescentProfileModes == null)
{
SafeHouse.Logger.Log("Trajectories.API.DescentProfileModes property is null");
wrapped = false;
return;
}
trDescentProfileGrades = trajectoriesAPIType.GetProperty("DescentProfileGrades");
if (trDescentProfileGrades == null)
{
SafeHouse.Logger.Log("Trajectories.API.DescentProfileGrades property is null");
wrapped = false;
return;
}
}
wrapped = true;
}
// Version checking properties
public static string GetVersion { get; private set; }
public static int GetVersionMajor { get; private set; }
public static int GetVersionMinor { get; private set; }
public static int GetVersionPatch { get; private set; }
public static bool IsVerTwo { get; private set; }
public static bool IsVerTwoTwo { get; private set; }
public static bool IsVerTwoFour { get; private set; }
// Standard methods
public static Vector3? ImpactVector() => (Vector3?)trGetImpactPosition.Invoke(null, new object[] { });
public static Vector3? CorrectedDirection() => (Vector3?)trCorrectedDirection.Invoke(null, new object[] { });
public static Vector3? PlannedDirection() => (Vector3?)trPlannedDirection.Invoke(null, new object[] { });
public static void SetTarget(double lat, double lon, double alt) => trSetTarget.Invoke(null, new object[] { lat, lon, alt });
// Trajectories v2.0.0 HasTarget method
public static bool? HasTarget()
{
if (trHasTarget == null)
return null;
return (bool?)trHasTarget.Invoke(null, new object[] { });
}
// Trajectories v2.2.0 and above methods and properties
public static double? GetTimeTillImpact()
{
if (trGetTimeTillImpact == null)
return null;
return (double?)trGetTimeTillImpact.Invoke(null, new object[] { });
}
public static bool? ProgradeEntry
{
get
{
if (trProgradeEntry == null) // will be null if TR version too low.
return null;
return (bool?)trProgradeEntry.GetValue(null, null);
}
set
{
if (trProgradeEntry != null) // will be null if TR version too low.
trProgradeEntry.SetValue(null, value, null);
}
}
public static bool? RetrogradeEntry
{
get
{
if (trRetrogradeEntry == null) // will be null if TR version too low.
return null;
return (bool?)trRetrogradeEntry.GetValue(null, null);
}
set
{
if (trRetrogradeEntry != null) // will be null if TR version too low.
trRetrogradeEntry.SetValue(null, value, null);
}
}
// Trajectories v2.4.0 and above methods and properties
public static Vector3d? GetTarget()
{
if (trGetTarget == null)
return null;
return (Vector3d?)trGetTarget.Invoke(null, new object[] { });
}
public static void ClearTarget()
{
if (trClearTarget == null)
return;
trClearTarget.Invoke(null, new object[] { });
}
public static void ResetDescentProfile(double AoA)
{
if (trResetDescentProfile == null)
return;
trResetDescentProfile.Invoke(null, new object[] { AoA });
}
public static List<double> DescentProfileAngles
{
get
{
if (trDescentProfileAngles == null)
return null;
return (List<double>)trDescentProfileAngles.GetValue(null, null);
}
set
{
if (trDescentProfileAngles != null)
trDescentProfileAngles.SetValue(null, value, null);
}
}
public static List<bool> DescentProfileModes
{
get
{
if (trDescentProfileModes == null)
return null;
return (List<bool>)trDescentProfileModes.GetValue(null, null);
}
set
{
if (trDescentProfileModes != null)
trDescentProfileModes.SetValue(null, value, null);
}
}
public static List<bool> DescentProfileGrades
{
get
{
if (trDescentProfileGrades == null)
return null;
return (List<bool>)trDescentProfileGrades.GetValue(null, null);
}
set
{
if (trDescentProfileGrades != null)
trDescentProfileGrades.SetValue(null, value, null);
}
}
public static BooleanValue Wrapped()
{
if (wrapped != null)
{
return wrapped;
}
else //if wrapped == null
{
Init();
return wrapped;
}
}
}
}