Refactor Config

This commit is contained in:
Kilian Hofmann 2022-09-10 22:55:04 +02:00
parent d6d3c0b4d9
commit 4445f1f12a
6 changed files with 97 additions and 65 deletions

View File

@ -2,5 +2,4 @@
- Update OSXCross Docker image to SDK 11
- Implement ARM64 arch for Plugin
- Refactor SimDatabase to be a class
- Requires new Airport class
- Refactor Config to be a class
- Requires new Airport class

48
file/config.cpp Normal file
View File

@ -0,0 +1,48 @@
#include "config/config.h"
namespace germanairlinesva
{
namespace file
{
namespace config
{
void Config::writeFile() const
{
std::ofstream out(XPLANE_PLUGIN_DIRECTORY CONFIG);
out << "scenery=" << this->scenery << "\n";
out << "user=" << this->user << "\n";
out << "token=" << this->token << "\n";
out.close();
}
Config::Config()
{
std::ifstream in(XPLANE_PLUGIN_DIRECTORY CONFIG);
std::string line;
while (std::getline(in, line)) {
std::vector<std::string> fields =
germanairlinesva::util::split(line, '=');
if (fields.size() >= 2) {
germanairlinesva::util::trim(fields[0]);
germanairlinesva::util::trim(fields[1]);
if (fields[0] == "scenery") {
this->scenery = fields[1];
} else if (fields[0] == "user") {
this->user = fields[1];
} else if (fields[0] == "token") {
this->token = fields[1];
}
}
}
in.close();
}
void Config::updateScenery(std::string scenery)
{
this->scenery = scenery;
this->writeFile();
}
} // namespace config
} // namespace file
} // namespace germanairlinesva

View File

@ -0,0 +1,42 @@
#ifndef GERMANAIRLINESVA_FILE_CONFIG_H
#define GERMANAIRLINESVA_FILE_CONFIG_H
#include <map>
#include <fstream>
#include <map>
#include <string>
#include <vector>
#include "constants.h"
#include "util.hpp"
namespace germanairlinesva
{
namespace file
{
namespace config
{
class Config
{
private:
std::string user;
std::string scenery;
std::string token;
void writeFile() const;
public:
Config();
void updateScenery(std::string scenery);
inline const std::string getUser() const { return this->user; }
inline const std::string getScenery() const { return this->scenery; }
inline const std::string getToken() const { return this->token; }
};
} // namespace config
} // namespace file
} // namespace germanairlinesva
#endif

View File

@ -1,53 +0,0 @@
#ifndef GERMANAIRLINESVA_FILE_CONFIG_H
#define GERMANAIRLINESVA_FILE_CONFIG_H
#include <map>
#include <fstream>
#include <map>
#include <string>
#include <vector>
#include "util.hpp"
namespace germanairlinesva
{
namespace file
{
namespace config
{
static inline std::map<std::string, std::string>
readConfig(const std::string &file)
{
std::ifstream config(file);
std::map<std::string, std::string> settings;
std::string line;
while (std::getline(config, line)) {
std::vector<std::string> fields =
germanairlinesva::util::split(line, '=');
if (fields.size() >= 2) {
germanairlinesva::util::trim(fields[0]);
germanairlinesva::util::trim(fields[1]);
settings[fields[0]] = fields[1];
}
}
config.close();
return settings;
}
static inline void
writeConfig(const std::map<std::string, std::string> &config,
const std::string &file)
{
std::ofstream cfg(file);
for (const std::pair<const std::string, std::string> &entry : config) {
cfg << entry.first << '=' << entry.second << '\n';
}
cfg.close();
}
} // namespace config
} // namespace file
} // namespace germanairlinesva
#endif

View File

@ -1,7 +1,7 @@
#ifndef GERMANAIRLINESVA_GACONNECTOR_XPLUGIN_MAIN_H
#define GERMANAIRLINESVA_GACONNECTOR_XPLUGIN_MAIN_H
#include "config/config.hpp"
#include "config/config.h"
#include "constants.h"
#include "logbook/logbook.h"
#include "recording/recording.h"

View File

@ -10,7 +10,7 @@ std::thread serverThread;
std::thread recordingThread;
std::atomic<bool> wantsExit;
std::map<std::string, std::string> configuration;
germanairlinesva::file::config::Config configuration;
std::map<std::string,
std::pair<std::vector<germanairlinesva::file::simdata::Gate>,
std::vector<germanairlinesva::file::simdata::Runway>>>
@ -114,14 +114,13 @@ PLUGIN_API int XPluginStart(char *outName, char *outSig, char *outDesc)
roll = XPLMFindDataRef("sim/flightmodel/position/phi"); // FLOAT
quaternion = XPLMFindDataRef("sim/flightmodel/position/q"); // FLOAT[4]
configuration = germanairlinesva::file::config::readConfig(
XPLANE_PLUGIN_DIRECTORY CONFIG);
configuration = germanairlinesva::file::config::Config();
toLog("Config loaded");
try {
connector = new germanairlinesva_websocket::Websocket(
"wss://ws.hofmannnet.myhome-server.de:8000",
configuration["user"],
configuration.getUser(),
toLog);
} catch (const std::invalid_argument &e) {
toLog(e.what());
@ -133,7 +132,7 @@ PLUGIN_API int XPluginStart(char *outName, char *outSig, char *outDesc)
if (germanairlinesva::util::generateMD5(XPLANE_CUSTOM_SCENERY, hash, toLog) ==
0) {
if (strcmp(configuration["scenery"].c_str(), hash) != 0 ||
if (strcmp(configuration.getScenery().c_str(), hash) != 0 ||
!germanairlinesva::util::fileExists(
XPLANE_PLUGIN_DIRECTORY SIMDATABASE)) {
scan(xplaneVersion < 12000 ? XPLANE11_BASE_SCENERY
@ -145,10 +144,7 @@ PLUGIN_API int XPluginStart(char *outName, char *outSig, char *outDesc)
airports,
XPLANE_PLUGIN_DIRECTORY SIMDATABASE);
configuration["scenery"] = hash;
germanairlinesva::file::config::writeConfig(
configuration,
XPLANE_PLUGIN_DIRECTORY CONFIG);
configuration.updateScenery(hash);
toLog("Sim Database updated");
} else {
airports = germanairlinesva::file::simdata::fromFile(