mirror of
https://github.com/BeamMP/BeamMP-Launcher.git
synced 2026-04-10 09:46:16 +00:00
move new protocol code into main repo
This commit is contained in:
41
src/Config.cpp
Executable file
41
src/Config.cpp
Executable file
@@ -0,0 +1,41 @@
|
||||
#include "Config.h"
|
||||
|
||||
#include <boost/iostreams/device/mapped_file.hpp>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
Config::Config() {
|
||||
if (std::filesystem::exists("Launcher.cfg")) {
|
||||
boost::iostreams::mapped_file cfg("Launcher.cfg", boost::iostreams::mapped_file::mapmode::readonly);
|
||||
nlohmann::json d = nlohmann::json::parse(cfg.const_data(), nullptr, false);
|
||||
if (d.is_discarded()) {
|
||||
is_valid = false;
|
||||
}
|
||||
// parse config
|
||||
if (d["Port"].is_number()) {
|
||||
port = d["Port"].get<int>();
|
||||
}
|
||||
if (d["Build"].is_string()) {
|
||||
branch = d["Build"].get<std::string>();
|
||||
for (char& c : branch) {
|
||||
c = char(tolower(c));
|
||||
}
|
||||
}
|
||||
if (d["GameDir"].is_string()) {
|
||||
game_dir = d["GameDir"].get<std::string>();
|
||||
}
|
||||
} else {
|
||||
write_to_file();
|
||||
}
|
||||
}
|
||||
|
||||
void Config::write_to_file() const {
|
||||
nlohmann::json d {
|
||||
{ "Port", port },
|
||||
{ "Branch", branch },
|
||||
{ "GameDir", game_dir },
|
||||
};
|
||||
std::ofstream of("Launcher.cfg", std::ios::trunc);
|
||||
of << d.dump(4);
|
||||
}
|
||||
Reference in New Issue
Block a user