-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheddft3Plugin.cpp
More file actions
357 lines (297 loc) · 11.6 KB
/
eddft3Plugin.cpp
File metadata and controls
357 lines (297 loc) · 11.6 KB
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
#include "pch.h"
#include "eddft3Plugin.h"
#include <regex>
#include <ranges>
#include <charconv>
#include <format>
eddft3::eddft3Plugin::eddft3Plugin() : EuroScopePlugIn::CPlugIn(EuroScopePlugIn::COMPATIBILITY_CODE,
eddft3::pluginName.c_str(),
eddft3::pluginVersion.c_str(),
eddft3::pluginAuthor.c_str(),
eddft3::pluginCopyright.c_str())
{
RegisterTagItemType("RWY Hint", TAG_ITEM_DFT3_HINT);
RegisterTagItemFunction("RWY Hint Acknowledge", TAG_FUNC_DFT3_HINT_ACK);
DisplayUserMessage("Message", "eddft3", std::string("Version: " + pluginVersion + " loaded.").c_str(), true, true, true, false, false);
eddfPosition = getEddfPosition();
colorOk = getColorFromSettings("ok");
colorWarn = getColorFromSettings("warn");
colorCaution = getColorFromSettings("caution");
}
eddft3::eddft3Plugin::~eddft3Plugin()
{
DisplayUserMessage("Message", "eddft3", "Unloaded successfully", true, true, true, false, false);
}
EuroScopePlugIn::CPosition eddft3::eddft3Plugin::getEddfPosition()
{
EuroScopePlugIn::CPosition pos;
for (EuroScopePlugIn::CSectorElement airport = this->SectorFileElementSelectFirst(EuroScopePlugIn::SECTOR_ELEMENT_AIRPORT);
airport.IsValid();
airport = this->SectorFileElementSelectNext(airport, EuroScopePlugIn::SECTOR_ELEMENT_AIRPORT))
{
eddft3::Logger::log("Checking airport: " + std::string(airport.GetName()));
if (airport.GetName() == std::string("EDDF"))
{
airport.GetPosition(&pos, 0);
eddft3::Logger::log("EDDF position found: " + std::to_string(pos.m_Latitude) + ", " + std::to_string(pos.m_Longitude));
break;
}
}
return pos;
}
eddft3::color eddft3::eddft3Plugin::getColorFromSettings(const std::string_view& colorType)
{
std::string setting;
try
{
if (colorType == "ok")
{
if (const char* tmpColor = this->GetDataFromSettings("okColor"); tmpColor != nullptr)
setting = tmpColor;
else setting = "000.255.000"; // default bright green
}
else if (colorType == "warn")
{
if (const char* tmpColor = this->GetDataFromSettings("warnColor"); tmpColor != nullptr)
setting = tmpColor;
else setting = "255.255.000"; // default yellow
}
else if (colorType == "caution")
{
if (const char* tmpColor = this->GetDataFromSettings("cautionColor"); tmpColor != nullptr)
setting = tmpColor;
else setting = "255.000.000"; // default red
}
else
{
eddft3::Logger::log("Invalid color type requested: " + std::string(colorType));
return { 0,0,0 };
}
}
catch (const std::exception& e)
{
eddft3::Logger::log("Error retrieving color setting for " + std::string(colorType) + ": " + e.what());
return { 0,0,0 };
}
color settingColor{ 0,0,0 };
static const std::regex pattern(R"(^\d{3}\.\d{3}\.\d{3}$)");
if (std::regex_match(setting, pattern))
{
auto parts = setting | std::ranges::views::split('.');
int idx = 0;
for (auto part : parts)
{
int value = 0;
auto first = &*part.begin();
auto last = first + std::ranges::distance(part);
auto [_, ec] = std::from_chars(first, last, value);
if (ec == std::errc{} && value >= 0 && value <= 255)
{
if (idx == 0) settingColor.r = (uint8_t)value;
if (idx == 1) settingColor.g = (uint8_t)value;
if (idx == 2) settingColor.b = (uint8_t)value;
}
idx++;
}
}
else eddft3::Logger::log("Invalid color setting format. Expected format: RRR.GGG.BBB (e.g. 255.000.000 for red). Current value: " + setting);
eddft3::Logger::log("Color for " + std::string(colorType) + " set to: " + std::to_string(settingColor.r) + ":" + std::to_string(settingColor.g) + ":" + std::to_string(settingColor.b));
return settingColor;
}
bool eddft3::eddft3Plugin::isSouthApp(const std::string_view& last)
{
std::string_view ownFac(ControllerMyself().GetCallsign());
// static const std::regex starPattern(R"([BC](?=/|$))");
// bool starMatch = std::regex_match(last.begin(), last.end(), starPattern);
if (ownFac.ends_with("APP") || ownFac.ends_with("TWR")) // #note: consider extracting TWR
{
eddft3::Logger::log("[APP/TWR] Checking last part (should contain rwy): " + std::string(last));
return last.ends_with("25L") || last.ends_with("07R");
}
if (ownFac.ends_with("CTR"))
{
size_t slashPos = last.find('/');
std::string_view star = last.substr(0, slashPos);
if (!this->activeAppAtc.empty())
{
eddft3::Logger::log("[CTR] Active APP controllers: " + std::to_string(this->activeAppAtc.size()) + ". Checking STAR: " + std::string(star));
return star.ends_with('B') || star.ends_with('C');
}
eddft3::Logger::log("[CTR] No active APP controllers found. Checking last part: " + std::string(last));
return (star.ends_with('B') || star.ends_with('C')) && (last.ends_with("25L") || last.ends_with("07R")); // top-down
}
return false; // default - fallback
}
void eddft3::eddft3Plugin::OnGetTagItem(EuroScopePlugIn::CFlightPlan FlightPlan, EuroScopePlugIn::CRadarTarget RadarTarget, int ItemCode, int TagData, char sItemString[16], int* pColorCode, COLORREF* pRGB, double* pFontSize)
{
if (!FlightPlan.IsValid() || !RadarTarget.IsValid())
return;
if (ItemCode == eddft3::TAG_ITEM_DFT3_HINT)
{
*pColorCode = EuroScopePlugIn::TAG_COLOR_RGB_DEFINED;
double distance = FlightPlan.GetCorrelatedRadarTarget().GetPosition().GetPosition().DistanceTo(eddfPosition);
if (FlightPlan.GetFlightPlanData().GetDestination() == std::string("EDDF") /*&& distance <= 80.0*/)
{
std::string callsign(FlightPlan.GetCallsign());
std::string acftType(FlightPlan.GetFlightPlanData().GetAircraftFPType());
// check only operators landing in the south
if (southOps.find(callsign.substr(0, 3)) != southOps.end() || acftAllRwys.find(acftType) == acftAllRwys.end())
{
std::string_view route = FlightPlan.GetFlightPlanData().GetRoute();
size_t lastCharPos = route.find_last_not_of(' ');
if (lastCharPos == std::string_view::npos)
{
eddft3::Logger::log("Route is empty or only contains spaces for flight " + callsign);
return;
}
route.remove_suffix(route.size() - lastCharPos - 1);
size_t lastSpacePos = route.find_last_of(' ');
std::string_view lastRoutePart = (lastSpacePos == std::string_view::npos) ? route : route.substr(lastSpacePos + 1);
if (!isSouthApp(std::string(lastRoutePart.begin(), lastRoutePart.end())))
{
ackFpln.contains(callsign) ? *pRGB = RGB(colorWarn.r, colorWarn.g, colorWarn.b) : *pRGB = RGB(colorCaution.r, colorCaution.g, colorCaution.b);
strcpy_s(sItemString, 16, "S");
}
else
{
*pRGB = RGB(colorOk.r, colorOk.g, colorOk.b);
strcpy_s(sItemString, 16, "S");
}
}
}
}
}
void eddft3::eddft3Plugin::OnFunctionCall(int FunctionId, const char* sItemString, POINT Pt, RECT Area)
{
if (FunctionId == eddft3::TAG_FUNC_DFT3_HINT_ACK)
{
EuroScopePlugIn::CFlightPlan aselFP = this->FlightPlanSelectASEL();
if (!aselFP.IsValid())
{
eddft3::Logger::log("No valid flight plan selected for acknowledgment.");
return;
}
eddft3::Logger::log("Acknowledging hint for flight: " + std::string(aselFP.GetCallsign()));
this->ackFpln.emplace(aselFP.GetCallsign());
}
}
bool eddft3::eddft3Plugin::OnCompileCommand(const char* sCommandLine)
{
std::string command(sCommandLine);
if (command.starts_with(".dft3"))
{
std::vector<std::string> parameters;
auto commandSplit = command | std::ranges::views::split(' ');
for (auto part : commandSplit)
{
if (std::string_view(part.begin(), part.end()) == std::string_view(".dft3")) continue;
parameters.emplace_back(part.begin(), part.end());
}
if (std::find(parameters.begin(), parameters.end(), "log") != parameters.end())
{
if (!eddft3::Logger::isRunning())
eddft3::Logger::initialize();
else
eddft3::Logger::shutdown();
return true;
}
if(std::find(parameters.begin(), parameters.end(), "rgb") != parameters.end())
{
if(parameters.size() < 3)
{
eddft3::Logger::log("Insufficient parameters for RGB command. Command was: " + command);
return true;
}
std::string rgbValue = parameters.back();
std::string rgbMode = parameters[parameters.size() - 2];
static const std::regex rgbPattern(R"(^\s*\d{3}:\d{3}:\d{3}\s*$)");
if (rgbMode != "ok" && rgbMode != "warn" && rgbMode != "caution")
{
eddft3::Logger::log("Invalid RGB mode. Expected 'ok', 'warn', or 'caution'. Current value: " + rgbMode);
return true;
}
if (!std::regex_match(rgbValue, rgbPattern))
{
eddft3::Logger::log("Invalid RGB command format. Expected format: .dft3 rgb R:G:B (e.g. .dft3 rgb 255:000:000 for red). Current value: " + rgbValue);
return true;
}
auto colorParts = rgbValue | std::ranges::views::split(':');
int idx = 0;
color tmpColor;
for (auto part : colorParts)
{
int value = 0;
auto first = &*part.begin();
auto last = first + std::ranges::distance(part);
auto [_, ec] = std::from_chars(first, last, value);
if (ec == std::errc{} && value >= 0 && value <= 255)
{
if (idx == 0) tmpColor.r = (uint8_t)value;
if (idx == 1) tmpColor.g = (uint8_t)value;
if (idx == 2) tmpColor.b = (uint8_t)value;
}
idx++;
}
std::string formattedColor = std::format("{:03}.{:03}.{:03}", tmpColor.r, tmpColor.g, tmpColor.b);
// premature saving of settings - will be prompted on ES closure
if (rgbMode == "ok")
{
colorOk = tmpColor;
eddft3::Logger::log("Setting OK color to: " + formattedColor);
this->SaveDataToSettings("okColor", "south RWY ok color", formattedColor.c_str());
}
else if (rgbMode == "warn")
{
colorWarn = tmpColor;
eddft3::Logger::log("Setting WARN color to: " + formattedColor);
this->SaveDataToSettings("warnColor", "south RWY warning color", formattedColor.c_str());
}
else if (rgbMode == "caution")
{
colorCaution = tmpColor;
eddft3::Logger::log("Setting CAUTION color to: " + formattedColor);
this->SaveDataToSettings("cautionColor", "south RWY caution color", formattedColor.c_str());
}
return true;
}
if (std::find(parameters.begin(), parameters.end(), "reset") != parameters.end())
{
this->activeAppAtc.clear();
DisplayUserMessage("eddft3", "eddft3", "Active app ATC list cleared via command.", true, true, true, true, false);
eddft3::Logger::log("Active app ATC list cleared via command.");
this->ackFpln.clear();
DisplayUserMessage("eddft3", "eddft3", "Acknowledged flight plans cleared via command.", true, true, true, true, false);
eddft3::Logger::log("Acknowledged flight plans cleared via command.");
return true;
}
DisplayUserMessage("eddft3", "eddft3", ("Unkown command: " + command).c_str(), true, true, true, true, false);
eddft3::Logger::log("Unknown command parameter. Command was: " + command);
return true;
}
return false;
}
void eddft3::eddft3Plugin::OnControllerPositionUpdate(EuroScopePlugIn::CController Controller)
{
if (!Controller.IsValid() || !Controller.IsController()) return;
if (Controller.GetPrimaryFrequency() == 199.998 || Controller.GetCallsign() == ControllerMyself().GetCallsign()) return;
std::string_view callsign(Controller.GetCallsign());
if (callsign.starts_with("EDDF") && callsign.ends_with("APP")) this->activeAppAtc.emplace(callsign);
}
eddft3::eddft3Plugin* pEddft3Plugin = nullptr;
// Diese Funktion wird von EuroScope aufgerufen, wenn das Plugin geladen wird
void __declspec(dllexport) EuroScopePlugInInit(EuroScopePlugIn::CPlugIn** ppPlugInInstance)
{
// eddft3::Logger::initialize(); // - disabled
*ppPlugInInstance = pEddft3Plugin = new eddft3::eddft3Plugin();
}
// Diese Funktion wird beim Entladen aufgerufen
void __declspec(dllexport) EuroScopePlugInExit(void)
{
eddft3::Logger::log("eddft3 shutdown...");
if (pEddft3Plugin != nullptr)
{
delete pEddft3Plugin;
pEddft3Plugin = nullptr;
}
eddft3::Logger::shutdown();
}