Skip to content

Commit

Permalink
More work into ESE position loading.
Browse files Browse the repository at this point in the history
+ Changes to JSON config structure
  • Loading branch information
Kirollos committed Apr 14, 2024
1 parent 4a24cdf commit 28b6d59
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 29 deletions.
1 change: 1 addition & 0 deletions DiscordEuroscope/ConfigData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace DiscordEuroScope_Configuration
this->discord_presence_large_image_key = "";
this->discord_presence_small_image_key = "";
this->sweatbox_bypass = false;
this->loaded_from_ese = false;
this->RadioCallsigns.clear();
for (int i = 0; i < 7; i++)
{
Expand Down
12 changes: 10 additions & 2 deletions DiscordEuroscope/ConfigData.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@

namespace DiscordEuroScope_Configuration
{
typedef std::map<std::string, std::string> typeRadioCallsigns;
typedef struct
{
std::string callsign;
std::string icao;
std::string frequency;
std::string radio_callsign;
} RadioCallsignElement_t;
typedef std::vector<RadioCallsignElement_t> RadioCallsigns_t;
enum States_Enum
{
State_Idle,
Expand Down Expand Up @@ -56,7 +63,8 @@ namespace DiscordEuroScope_Configuration
std::string discord_presence_small_image_key;
bool sweatbox_bypass;
State states[7];
typeRadioCallsigns RadioCallsigns;
RadioCallsigns_t RadioCallsigns;
bool loaded_from_ese = false;

ConfigData();

Expand Down
82 changes: 62 additions & 20 deletions DiscordEuroscope/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@

const char default_file_content[] = DEFAULT_FILE_CONTENT;

// caching so we don't regex every time
static std::string last_fetch_callsign = "";
static std::string last_returned_rcallsign = "";
static std::string last_fetch_frequency = "";

namespace DiscordEuroScope_Configuration
{
ConfigManager::ConfigManager()
Expand Down Expand Up @@ -129,56 +134,93 @@ namespace DiscordEuroScope_Configuration
_ready = false;
json_document.GetAllocator().Clear();
data.Cleanup();
last_fetch_callsign = "";
last_returned_rcallsign = "";
last_fetch_frequency = "";
}

void ConfigManager::LoadRadioCallsigns()
{
if (json_document.HasMember("load_from_ese")) {
if (json_document["load_from_ese"].IsBool() && json_document["load_from_ese"].GetBool() && json_document["path_to_ese"].IsString())
if (!json_document["radio_callsigns"].IsObject()) return;
auto& RadioCallsignObj = json_document["radio_callsigns"];
auto& RadioCallsignConfigObj = RadioCallsignObj["config"];
if (RadioCallsignConfigObj.HasMember("load_from_ese")) {
if (RadioCallsignConfigObj["load_from_ese"].IsBool() && RadioCallsignConfigObj["load_from_ese"].GetBool())
{
std::string relative_to_full_path;
std::string relative_path = json_document["path_to_ese"].GetString();
std::string relative_to_absolute_path;
std::string relative_path =
RadioCallsignConfigObj.HasMember("path_to_ese") && RadioCallsignConfigObj["path_to_ese"].IsString()
? RadioCallsignConfigObj["path_to_ese"].GetString()
: ".\\";
if (relative_path[1] == ':')
relative_to_full_path = relative_path;
relative_to_absolute_path = relative_path;
else
relative_to_full_path = this->file_path.substr(0, this->file_path.find_last_of('\\') + 1) + relative_path;
if (ESEHandler::LocateESEFile(relative_to_full_path))
relative_to_absolute_path = this->file_path.substr(0, this->file_path.find_last_of('\\') + 1) + relative_path;
if (ESEHandler::LocateESEFile(relative_to_absolute_path))
{
int p = ESEHandler::ParsePositions();
ESEHandler::GetRadioCallsigns(data.RadioCallsigns);
return;
if (ESEHandler::ParsePositions() > 0) {
data.loaded_from_ese = true;
ESEHandler::GetRadioCallsigns(data.RadioCallsigns);
return;
}
}
}
}

assert(json_document["radio_callsigns"].IsObject());
assert(RadioCallsignObj.HasMember("custom") && RadioCallsignObj["custom"].IsArray());
data.RadioCallsigns.clear();
for (auto& it : json_document["radio_callsigns"].GetObject())
for (auto& it : RadioCallsignObj["custom"].GetArray())
{
if (it.name.GetType() == rapidjson::kStringType && it.value.GetType() == rapidjson::kStringType)
if (it.IsObject())
{
data.RadioCallsigns.insert(std::pair<std::string, std::string>(it.name.GetString(), it.value.GetString()));
RadioCallsignElement_t element;
if (it.HasMember("callsign") && it["callsign"].IsString())
element.callsign = std::string(it["callsign"].GetString());
if (it.HasMember("frequency") && it["frequency"].IsString())
element.frequency = std::string(it["frequency"].GetString());
if (it.HasMember("rcallsign") && it["rcallsign"].IsString())
element.radio_callsign = std::string(it["rcallsign"].GetString());

element.icao = element.callsign.substr(0, element.callsign.find_first_of('_'));
data.RadioCallsigns.push_back(element);
}
}
}

void ConfigManager::FindRadioCallsign(std::string callsign, std::string& radio_callsign)
void ConfigManager::FindRadioCallsign(std::string callsign, std::string frequency, std::string& radio_callsign)
{
// caching so we don't regex every time
static std::string last_fetch_callsign = "";
static std::string last_returned_rcallsign = "";
if (callsign == last_fetch_callsign)
if (callsign == last_fetch_callsign && frequency == last_fetch_frequency)
{
radio_callsign = last_returned_rcallsign; // return cached result
return;
}
last_fetch_callsign = callsign;
last_fetch_frequency = frequency;
std::string icao = callsign.substr(0, callsign.find_first_of('_'));

if (data.loaded_from_ese)
{
for (auto& it : data.RadioCallsigns)
{
if (it.icao == icao && it.frequency == frequency)
{
radio_callsign = it.radio_callsign;
last_returned_rcallsign = radio_callsign;
return;
}
}
last_returned_rcallsign = radio_callsign = callsign;
return;
}

for (auto& it : data.RadioCallsigns)
{
std::regex rgx(std::string(it.first));
std::regex rgx(std::string(it.callsign));
std::smatch rgx_match;
if(!std::regex_search(callsign, rgx_match, rgx)) continue;
radio_callsign = it.second;
if (!it.frequency.empty() && !(it.frequency == frequency && it.icao == icao)) continue;
radio_callsign = it.radio_callsign;

std::vector<std::string> dict;
for (int rgx_it = 1; rgx_it < rgx_match.size(); rgx_it++)
Expand Down
2 changes: 1 addition & 1 deletion DiscordEuroscope/ConfigManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace DiscordEuroScope_Configuration
void Cleanup(void);
void LoadRadioCallsigns(void);
void GenerateConfigFile(std::string filepath);
void FindRadioCallsign(std::string callsign, std::string& radio_callsign);
void FindRadioCallsign(std::string callsign, std::string frequency, std::string& radio_callsign);
};
}

Expand Down
16 changes: 14 additions & 2 deletions DiscordEuroscope/DefaultFileContent.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,20 @@
}\r\
},\r\
\"radio_callsigns\": {\r\
\"^HECC_CTR$\": \"Cairo Control Bandbox\",\r\
\"^HECC_(\\\\d+)(?:_)CTR$\" : \"Cairo Control ACC $0\"\r\
\"config\": {\r\
\"load_from_ese\": false,\r\
\"path_to_ese\" : \"..\\\\..\\\\\"\r\
},\r\
\"custom\" : [\r\
{\r\
\"callsign\": \"^HECC_CTR$\",\r\
\"rcallsign\" : \"Cairo Control Bandbox\"\r\
},\r\
{\r\
\"callsign\": \"^HECC_(\\\\d+)(?:_)CTR$\",\r\
\"rcallsign\" : \"Cairo Control ACC $0\"\r\
}\r\
]\r\
}\r\
}\r\
")
3 changes: 2 additions & 1 deletion DiscordEuroscope/DiscordEuroscopeExt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,9 @@ VOID CALLBACK DiscordTimer(_In_ HWND hwnd, _In_ UINT uMsg, _In_ UINT_PTR idEvent

std::string freq_str(8, 0);
std::snprintf((char*)freq_str.c_str(), 8, "%.3f", frequency);
freq_str = std::string(freq_str, 0, strlen(freq_str.c_str()));
std::string radio_callsign;
pMyPlugIn->config.FindRadioCallsign(callsign, radio_callsign);
pMyPlugIn->config.FindRadioCallsign(callsign, freq_str, radio_callsign);
const std::map<std::string, std::string> Dictionary =
{
{"callsign", callsign},
Expand Down
21 changes: 19 additions & 2 deletions DiscordEuroscope/ESEHandler.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "stdafx.h"
#include "ESEHandler.h"
#include "ConfigData.h"
#include <filesystem>
#include <fstream>

Expand Down Expand Up @@ -58,11 +59,27 @@ int ESEHandler::ParsePositions(void)
return positions.size();
}

void ESEHandler::GetRadioCallsigns(std::map<std::string, std::string>& rcs)
void ESEHandler::GetRadioCallsigns(RadioCallsigns_t& rcs)
{
using namespace DiscordEuroScope_Configuration;
rcs.clear();
for (auto& pos : positions)
{
rcs.insert(std::pair<std::string, std::string>(pos.callsign, pos.radio_callsign));
DiscordEuroScope_Configuration::RadioCallsignElement_t element;
element.callsign = pos.callsign;
element.frequency = pos.frequency;
element.icao = pos.callsign.substr(0, pos.callsign.find_first_of('_'));
element.radio_callsign = pos.radio_callsign;
bool flag = false;
for (auto& it : rcs)
{
if (it.callsign == element.callsign /* TEMP DISABLE || (it.icao == element.icao && it.frequency == element.frequency)*/)
{
flag = true;
break;
}
}
if(!flag)
rcs.push_back(element);
}
}
4 changes: 3 additions & 1 deletion DiscordEuroscope/ESEHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
#include <string>
#include <vector>
#include <map>
#include "ConfigData.h"

using DiscordEuroScope_Configuration::RadioCallsigns_t;

struct ESEPositionItem
{
Expand All @@ -36,5 +38,5 @@ class ESEHandler
public:
static bool LocateESEFile(std::string relative_path_to_ese);
static int ParsePositions(void);
static void GetRadioCallsigns(std::map<std::string, std::string>& rcs);
static void GetRadioCallsigns(RadioCallsigns_t& rcs);
};

0 comments on commit 28b6d59

Please sign in to comment.