-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathanalyzeFP.cpp
455 lines (397 loc) · 12.1 KB
/
analyzeFP.cpp
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#include "stdafx.h"
#include "analyzeFP.hpp"
extern "C" IMAGE_DOS_HEADER __ImageBase;
bool blink;
bool debugMode, initialSidLoad;
int disCount;
ifstream sidDatei;
char DllPathFile[_MAX_PATH];
string pfad;
vector<string> sidName;
vector<string> sidEven;
vector<int> sidMin;
vector<int> sidMax;
using namespace std;
using namespace EuroScopePlugIn;
// Run on Plugin Initialization
CVFPCPlugin::CVFPCPlugin(void) :CPlugIn(EuroScopePlugIn::COMPATIBILITY_CODE, MY_PLUGIN_NAME, MY_PLUGIN_VERSION, MY_PLUGIN_DEVELOPER, MY_PLUGIN_COPYRIGHT)
{
string loadingMessage = "Version: ";
loadingMessage += MY_PLUGIN_VERSION;
loadingMessage += " loaded.";
sendMessage(loadingMessage);
// Register Tag Item "VFPC"
RegisterTagItemType("VFPC", TAG_ITEM_FPCHECK);
RegisterTagItemFunction("Check FP", TAG_FUNC_CHECKFP_MENU);
// Get Path of the Sid.txt
GetModuleFileNameA(HINSTANCE(&__ImageBase), DllPathFile, sizeof(DllPathFile));
pfad = DllPathFile;
pfad.resize(pfad.size() - strlen("VFPC.dll"));
pfad += "Sid.json";
debugMode = false;
initialSidLoad = false;
}
// Run on Plugin destruction, Ie. Closing EuroScope or unloading plugin
CVFPCPlugin::~CVFPCPlugin()
{
}
/*
Custom Functions
*/
void CVFPCPlugin::debugMessage(string type, string message) {
// Display Debug Message if debugMode = true
if (debugMode) {
DisplayUserMessage("VFPC", type.c_str(), message.c_str(), true, true, true, false, false);
}
}
void CVFPCPlugin::sendMessage(string type, string message) {
// Show a message
DisplayUserMessage("VFPC", type.c_str(), message.c_str(), true, true, true, true, false);
}
void CVFPCPlugin::sendMessage(string message) {
DisplayUserMessage("Message", "VFPC", message.c_str(), true, true, true, false, false);
}
void CVFPCPlugin::getSids() {
stringstream ss;
ifstream ifs;
ifs.open(pfad.c_str(), ios::binary);
ss << ifs.rdbuf();
ifs.close();
if (config.Parse<0>(ss.str().c_str()).HasParseError()) {
string error{ pfad };
error += " couldn't be opened!";
sendMessage("Error", error.c_str());
return;
}
airports.clear();
for (SizeType i = 0; i < config.Size(); i++) {
const Value& airport = config[i];
string airport_icao = airport["icao"].GetString();
airports.insert(pair<string, SizeType>(airport_icao, i));
}
}
// Does the checking and magic stuff, so everything will be alright, when this is finished! Or not. Who knows?
vector<string> CVFPCPlugin::validizeSid(CFlightPlan flightPlan) {
vector<string> returnValid{}; // 0 = Callsign, 1 = valid/invalid SID, 2 = SID Name, 3 = Even/Odd, 4 = Minimum Flight Level, 5 = Maximum Flight Level, 6 = Passed
returnValid.push_back(flightPlan.GetCallsign());
bool valid{ false };
string origin = flightPlan.GetFlightPlanData().GetOrigin(); to_upper(origin);
string destination = flightPlan.GetFlightPlanData().GetDestination(); to_upper(destination);
SizeType origin_int;
int RFL = flightPlan.GetFlightPlanData().GetFinalAltitude();
vector<string> route = split(flightPlan.GetFlightPlanData().GetRoute(), ' ');
for (int i = 0; i < route.size(); i++) {
to_upper(route[i]);
}
string sid = flightPlan.GetFlightPlanData().GetSidName(); to_upper(sid);
string first_wp = sid.substr(0, sid.find_first_of("0123456789")); to_upper(first_wp);
string first_airway;
vector<string>::iterator it = find(route.begin(), route.end(), first_wp);
if (it != route.end() && (it - route.begin()) != route.size() - 1) {
first_airway = route[(it - route.begin()) + 1];
to_upper(first_airway);
}
// Airport defined
if (airports.find(origin) == airports.end()) {
returnValid.push_back("Invalid");
returnValid.push_back("No valid Airport found!");
for (int i = 0; i < 5; i++) {
returnValid.push_back("-");
}
returnValid.push_back("Failed");
return returnValid;
}
else
origin_int = airports[origin];
// Any SIDs defined
if (!config[origin_int].HasMember("sids") || config[origin_int]["sids"].IsArray()) {
returnValid.push_back("Invalid");
returnValid.push_back("No SIDs defined!");
for (int i = 0; i < 5; i++) {
returnValid.push_back("-");
}
returnValid.push_back("Failed");
return returnValid;
}
// Needed SID defined
if (!config[origin_int]["sids"].HasMember(first_wp.c_str()) || !config[origin_int]["sids"][first_wp.c_str()].IsArray()) {
returnValid.push_back("Invalid");
returnValid.push_back("No valid SID found!");
for (int i = 0; i < 5; i++) {
returnValid.push_back("-");
}
returnValid.push_back("Failed");
return returnValid;
}
const Value& conditions = config[origin_int]["sids"][first_wp.c_str()];
for (SizeType i = 0; i < conditions.Size(); i++) {
returnValid.clear();
returnValid.push_back(flightPlan.GetCallsign());
bool passed[5]{ false };
valid = false;
// Does Condition contain our destination if it's limited
if (conditions[i]["destinations"].IsArray() && conditions[i]["destinations"].Size()) {
if (arrayContains(conditions[i]["destinations"], destination.c_str())) {
returnValid.push_back("Passed Destination");
passed[0] = true;
}
else {
continue;
}
}
else {
returnValid.push_back("No Destination restriction");
passed[0] = true;
}
// Does Condition contain our first airway if it's limited
if (conditions[i]["airways"].IsArray() && conditions[i]["airways"].Size()) {
string rte = flightPlan.GetFlightPlanData().GetRoute();
if (routeContains(rte, conditions[i]["airways"])) {
returnValid.push_back("Passed Airways");
passed[1] = true;
}
else {
continue;
}
}
else {
returnValid.push_back("No Airway restriction");
passed[1] = true;
}
valid = true;
returnValid.insert(returnValid.begin() + 1, "Valid");
returnValid.insert(returnValid.begin() + 2, first_wp);
// Direction of condition
string direction = conditions[i]["direction"].GetString();
to_upper(direction);
if (direction == "EVEN") {
if ((RFL / 1000) % 2 == 0) {
returnValid.push_back("Passed Even");
passed[2] = true;
}
else {
returnValid.push_back("Failed Even");
}
}
else if (direction == "ODD") {
if ((RFL / 1000) % 2 != 0) {
returnValid.push_back("Passed Odd");
passed[2] = true;
}
else {
returnValid.push_back("Failed Odd");
}
}
else if (direction == "ANY") {
returnValid.push_back("No Direction restriction");
passed[2] = true;
}
else {
string errorText{ "Config Error for Even/Odd on SID: " };
errorText += first_wp;
sendMessage("Error", errorText);
returnValid.push_back("Config Error for Even/Odd on this SID!");
}
// Flight level
int min_fl, max_fl;
if (conditions[i].HasMember("min_fl") && (min_fl = conditions[i]["min_fl"].GetInt()) > 0) {
if ((RFL / 100) >= min_fl) {
returnValid.push_back("Passed Minimum Flight Level");
passed[3] = true;
}
else {
returnValid.push_back("Failed Minimum Flight Level. Min FL: " + to_string(min_fl));
}
}
else {
returnValid.push_back("No Minimum Flight Level");
passed[3] = true;
}
if (conditions[i].HasMember("max_fl") && (max_fl = conditions[i]["max_fl"].GetInt()) > 0) {
if ((RFL / 100) <= max_fl) {
returnValid.push_back("Passed Maximum Flight Level");
passed[4] = true;
}
else {
returnValid.push_back("Failed Maximum Flight Level. Max FL: " + to_string(max_fl));
}
}
else {
returnValid.push_back("No Maximum Flight Level");
passed[4] = true;
}
bool passedVeri{ false };
for (int i = 0; i < 5; i++) {
if (passed[i])
{
passedVeri = true;
}
else {
passedVeri = false;
break;
}
}
if (passedVeri) {
returnValid.push_back("Passed");
break;
}
else {
returnValid.push_back("Failed");
if (!passed[0] || !passed[1])
continue;
else
break;
}
}
if (!valid) {
returnValid.push_back("Invalid");
returnValid.push_back("No valid SID found!");
for (int i = 0; i < 5; i++) {
returnValid.push_back("-");
}
returnValid.push_back("Failed");
}
return returnValid;
}
//
void CVFPCPlugin::OnFunctionCall(int FunctionId, const char * ItemString, POINT Pt, RECT Area) {
if (FunctionId == TAG_FUNC_CHECKFP_MENU) {
OpenPopupList(Area, "Check FP", 1);
AddPopupListElement("Show Checks", "", TAG_FUNC_CHECKFP_CHECK, false, 2, false);
}
if (FunctionId == TAG_FUNC_CHECKFP_CHECK) {
checkFPDetail();
}
}
// Get FlightPlan, and therefore get the first waypoint of the flightplan (ie. SID). Check if the (RFL/1000) corresponds to the SID Min FL and report output "OK" or "FPL"
void CVFPCPlugin::OnGetTagItem(CFlightPlan FlightPlan, CRadarTarget RadarTarget, int ItemCode, int TagData, char sItemString[16], int* pColorCode, COLORREF* pRGB, double* pFontSize)
{
if (ItemCode == TAG_ITEM_FPCHECK)
{
string FlightPlanString = FlightPlan.GetFlightPlanData().GetRoute();
int RFL = FlightPlan.GetFlightPlanData().GetFinalAltitude();
*pColorCode = TAG_COLOR_RGB_DEFINED;
string fpType{ FlightPlan.GetFlightPlanData().GetPlanType() };
if (fpType == "V") {
*pRGB = TAG_GREEN;
strcpy_s(sItemString, 16, "VFR");
}
else {
vector<string> messageBuffer{ validizeSid(FlightPlan) }; // 0 = Callsign, 1 = valid/invalid SID, 2 = SID Name, 3 = Even/Odd, 4 = Minimum Flight Level, 5 = Maximum Flight Level, 6 = Passed
if (messageBuffer.at(8) == "Passed") {
*pRGB = TAG_GREEN;
strcpy_s(sItemString, 16, "OK!");
}
else {
*pRGB = TAG_RED;
string code = getFails(validizeSid(FlightPlan));
strcpy_s(sItemString, 16, code.c_str());
}
}
}
}
bool CVFPCPlugin::OnCompileCommand(const char * sCommandLine) {
if (startsWith(".vfpc reload", sCommandLine))
{
sendMessage("Unloading all loaded SIDs...");
sidName.clear();
sidEven.clear();
sidMin.clear();
sidMax.clear();
initialSidLoad = false;
return true;
}
if (startsWith(".vfpc debug", sCommandLine)) {
if (debugMode) {
debugMessage("DebugMode", "Deactivating Debug Mode!");
debugMode = false;
} else {
debugMode = true;
debugMessage("DebugMode", "Activating Debug Mode!");
}
return true;
}
if (startsWith(".vfpc load", sCommandLine)) {
locale loc;
string buffer{ sCommandLine };
buffer.erase(0, 11);
getSids();
return true;
}
if (startsWith(".vfpc check", sCommandLine))
{
checkFPDetail();
return true;
}
return false;
}
// Sends to you, which checks were failed and which were passed on the selected aircraft
void CVFPCPlugin::checkFPDetail() {
vector<string> messageBuffer{ validizeSid(FlightPlanSelectASEL()) }; // 0 = Callsign, 1 = valid/invalid SID, 2 = SID Name, 3 = Even/Odd, 4 = Minimum Flight Level, 5 = Maximum Flight Level, 6 = Passed
sendMessage(messageBuffer.at(0), "Checking...");
string buffer{ messageBuffer.at(1) };
if (messageBuffer.at(1) == "Valid") {
buffer += ", found SID: ";
for (int i = 2; i < 8; i++) {
buffer += messageBuffer.at(i);
buffer += ", ";
}
buffer += messageBuffer.at(8);
buffer += " FlightPlan Check. Check complete.";
} else {
buffer += " ";
buffer += messageBuffer.at(2);
buffer += " Check complete.";
}
sendMessage(messageBuffer.at(0), buffer);
}
string CVFPCPlugin::getFails(vector<string> messageBuffer) {
vector<string> fail;
fail.push_back("FPL");
if (messageBuffer.at(1) == "Invalid") {
fail.push_back("SID");
}
if (messageBuffer.at(3).find_first_of("Failed") == 0) {
fail.push_back("DST");
}
if (messageBuffer.at(4).find_first_of("Failed") == 0) {
fail.push_back("AWY");
}
if (messageBuffer.at(5).find_first_of("Failed") == 0) {
fail.push_back("E/O");
}
if (messageBuffer.at(6).find_first_of("Failed") == 0) {
fail.push_back("MIN");
}
if (messageBuffer.at(7).find_first_of("Failed") == 0) {
fail.push_back("MAX");
}
int couldnt = disCount;
while (couldnt >= fail.size())
couldnt -= fail.size();
return fail[couldnt];
}
void CVFPCPlugin::OnTimer(int Counter) {
blink = !blink;
if (blink) {
if (disCount < 3) {
disCount++;
}
else {
disCount = 0;
}
}
// Loading proper Sids, when logged in
if (GetConnectionType() != CONNECTION_TYPE_NO && !initialSidLoad) {
string callsign{ ControllerMyself().GetCallsign() };
getSids();
initialSidLoad = true;
} else if (GetConnectionType() == CONNECTION_TYPE_NO && initialSidLoad) {
sidName.clear();
sidEven.clear();
sidMin.clear();
sidMax.clear();
initialSidLoad = false;
sendMessage("Unloading", "All loaded SIDs");
}
}