diff --git a/TODO.md b/TODO.md index c30438e..508fd9b 100644 --- a/TODO.md +++ b/TODO.md @@ -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 \ No newline at end of file + - Requires new Airport class \ No newline at end of file diff --git a/file/config.cpp b/file/config.cpp new file mode 100644 index 0000000..686300f --- /dev/null +++ b/file/config.cpp @@ -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 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 \ No newline at end of file diff --git a/file/include/config/config.h b/file/include/config/config.h new file mode 100644 index 0000000..5d66b3f --- /dev/null +++ b/file/include/config/config.h @@ -0,0 +1,42 @@ +#ifndef GERMANAIRLINESVA_FILE_CONFIG_H +#define GERMANAIRLINESVA_FILE_CONFIG_H + +#include + +#include +#include +#include +#include + +#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 diff --git a/file/include/config/config.hpp b/file/include/config/config.hpp deleted file mode 100644 index 93cfbbb..0000000 --- a/file/include/config/config.hpp +++ /dev/null @@ -1,53 +0,0 @@ -#ifndef GERMANAIRLINESVA_FILE_CONFIG_H -#define GERMANAIRLINESVA_FILE_CONFIG_H - -#include - -#include -#include -#include -#include - -#include "util.hpp" - -namespace germanairlinesva -{ -namespace file -{ - namespace config - { - static inline std::map - readConfig(const std::string &file) - { - std::ifstream config(file); - std::map settings; - - std::string line; - while (std::getline(config, line)) { - std::vector 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 &config, - const std::string &file) - { - std::ofstream cfg(file); - for (const std::pair &entry : config) { - cfg << entry.first << '=' << entry.second << '\n'; - } - cfg.close(); - } - } // namespace config -} // namespace file -} // namespace germanairlinesva -#endif diff --git a/xplugin/include/main.h b/xplugin/include/main.h index a0c01e0..8cececa 100644 --- a/xplugin/include/main.h +++ b/xplugin/include/main.h @@ -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" diff --git a/xplugin/main.cpp b/xplugin/main.cpp index 072ee9d..d73862d 100644 --- a/xplugin/main.cpp +++ b/xplugin/main.cpp @@ -10,7 +10,7 @@ std::thread serverThread; std::thread recordingThread; std::atomic wantsExit; -std::map configuration; +germanairlinesva::file::config::Config configuration; std::map, std::vector>> @@ -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(