forked from pierr3/ModeS
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Helpers.cpp
161 lines (134 loc) · 5.09 KB
/
Helpers.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
#include "stdafx.h"
#include "Helpers.h"
string LoadUpdateString()
{
const string AGENT{ "EuroScope " + (string)ESversion() + " plug-in: " + MY_PLUGIN_NAME + "/" + MY_PLUGIN_VERSION};
HINTERNET connect = InternetOpen(AGENT.c_str(), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (!connect) {
throw error { string {"Connection failed to verify the plugin version. Error: " + to_string(GetLastError()) } };
}
HINTERNET OpenAddress = InternetOpenUrl(connect, MY_PLUGIN_UPDATE_URL, NULL, 0, INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RELOAD, 0);
if (!OpenAddress) {
InternetCloseHandle(connect);
throw error { string { "Failed to load plugin version verification file. Error: " + to_string(GetLastError()) } };
}
char DataReceived[256];
DWORD NumberOfBytesRead { 0 };
string answer;
while (InternetReadFile(OpenAddress, DataReceived, 256, &NumberOfBytesRead) && NumberOfBytesRead)
answer.append(DataReceived, NumberOfBytesRead);
InternetCloseHandle(OpenAddress);
InternetCloseHandle(connect);
return answer;
}
string LoadWebSquawk(EuroScopePlugIn::CFlightPlan FP, EuroScopePlugIn::CController ATCO, vector<const char*> usedCodes, bool vicinityADEP, int ConnectionType)
{
//PluginData p;
//const string AGENT{ "EuroScope " + string { MY_PLUGIN_NAME } + "/" + string { MY_PLUGIN_VERSION } };
const string AGENT{ "EuroScope " + (string)ESversion() + " plug-in: " + MY_PLUGIN_NAME + "/" + MY_PLUGIN_VERSION };
HINTERNET connect = InternetOpen(AGENT.c_str(), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (!connect) {
#ifdef _DEBUG
//throw error { string { "Failed reach the CCAMS server. Error: " + to_string(GetLastError()) } };
#endif
return string{ "E404" };
}
//int statusCode;
char responseText[MAX_PATH]; // change to wchar_t for unicode
DWORD responseTextSize = sizeof(responseText);
//Check existance of page (for 404 error)
if (HttpQueryInfo(connect, HTTP_QUERY_STATUS_CODE, &responseText, &responseTextSize, NULL))
{
//statusCode = ;
if (atoi(responseText) != 200)
return string{ "E" + string(responseText) };
}
//cpr::Response r = cpr::Get(cpr::Url{ "http://localhost/webtools/CCAMS/squawk" },
// cpr::Parameters{ {"callsign", ATCO.GetCallsign()} });
//if (r.status_code != 200)
// return "CURL ERROR";
string codes;
for (size_t i = 0; i < usedCodes.size(); i++)
{
if (i > 0)
codes += ",";
codes += usedCodes[i];
}
//string build_url = "http://localhost/webtools/CCAMS/squawk?callsign=" + string(ATCO.GetCallsign());
string build_url = "https://ccams.kilojuliett.ch/squawk?callsign=" + string(ATCO.GetCallsign());
if (FP.IsValid())
{
if (vicinityADEP)
{
build_url += "&orig=" + string(FP.GetFlightPlanData().GetOrigin());
}
build_url += "&dest=" + string(FP.GetFlightPlanData().GetDestination()) +
"&flightrule=" + string(FP.GetFlightPlanData().GetPlanType());
if (FP.GetCorrelatedRadarTarget().IsValid())
if (FP.GetCorrelatedRadarTarget().GetPosition().IsValid())
build_url += "&latitude=" + to_string(FP.GetCorrelatedRadarTarget().GetPosition().GetPosition().m_Latitude) +
"&longitude=" + to_string(FP.GetCorrelatedRadarTarget().GetPosition().GetPosition().m_Longitude);
build_url += "&connectiontype=" + to_string(ConnectionType);
#ifndef _DEBUG
if (ConnectionType > 2)
{
build_url += "&sim";
}
#endif
}
if (codes.size() > 0)
{
build_url += "&codes=" + codes;
}
HINTERNET OpenAddress = InternetOpenUrl(connect, build_url.c_str(), NULL, 0, INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RELOAD, 0);
if (!OpenAddress) {
InternetCloseHandle(connect);
#ifdef _DEBUG
//throw error{ string { "Failed reach the CCAMS server. Error: " + to_string(GetLastError()) } };
#endif
return string{ "E406" };
}
char DataReceived[256];
DWORD NumberOfBytesRead{ 0 };
string answer;
while (InternetReadFile(OpenAddress, DataReceived, 256, &NumberOfBytesRead) && NumberOfBytesRead)
answer.append(DataReceived, NumberOfBytesRead);
InternetCloseHandle(OpenAddress);
InternetCloseHandle(connect);
trim(answer);
if (answer.length() == 0)
return string{ "E411" };
return answer;
}
string ESversion()
{
int EuroScopeVersion[4] = { 0, 0, 0, 0 };
DWORD verHandle = 0;
char szVersionFile[MAX_PATH];
GetModuleFileName(NULL, szVersionFile, MAX_PATH);
const DWORD verSize = GetFileVersionInfoSize(szVersionFile, &verHandle);
if (verSize != NULL && verHandle == 0)
{
char* verData = new char[verSize];
if (GetFileVersionInfo(szVersionFile, verHandle, verSize, verData))
{
VS_FIXEDFILEINFO* verInfo = nullptr;
unsigned int size = 0;
if (VerQueryValue(verData, "\\", (LPVOID*)&verInfo, &size))
{
if (size)
{
if (verInfo->dwSignature == 0xfeef04bd)
{
EuroScopeVersion[0] = (verInfo->dwFileVersionMS >> 16) & 0xffff;
EuroScopeVersion[1] = (verInfo->dwFileVersionMS >> 0) & 0xffff;
EuroScopeVersion[2] = (verInfo->dwFileVersionLS >> 16) & 0xffff;
EuroScopeVersion[3] = (verInfo->dwFileVersionLS >> 0) & 0xffff;
}
}
}
}
delete[] verData;
}
return to_string(EuroScopeVersion[0]) + "." + to_string(EuroScopeVersion[1]) + "." + to_string(EuroScopeVersion[2]) + "." + to_string(EuroScopeVersion[3]);
}