-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide user configuration via config.toml file (#31)
* Add toml11 as submodule * Add functions to read or create config file * Add config loading and parsing * Use config values: stealer.enabled and loader.path * Use defaults if keys/tables do not exist * Add debug logging * Move default config string * Bump version * Bump version
- Loading branch information
1 parent
caea6f2
commit f66f412
Showing
14 changed files
with
145 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* Copyright (C) 2019 Lewis Clark | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <https://www.gnu.org/licenses/>. */ | ||
|
||
|
||
#include "config.hpp" | ||
|
||
static glt::config::Config cfg; | ||
|
||
void glt::config::LoadConfig() { | ||
glt::file::CreateConfig(); | ||
|
||
const auto& logger = GetLogger(); | ||
|
||
try { | ||
const auto t = toml::parse(glt::file::GetConfigFilePath()); | ||
|
||
if (t.contains("stealer")) { | ||
const auto& table_stealer = toml::find(t, "stealer"); | ||
|
||
if (table_stealer.contains("enabled")) { | ||
const auto stealer_enabled = toml::find(table_stealer, "enabled"); | ||
|
||
if (stealer_enabled.is_boolean()) { | ||
cfg.stealer_enabled = toml::get<bool>(stealer_enabled); | ||
} | ||
} | ||
} | ||
|
||
if (t.contains("loader")) { | ||
const auto& table_loader = toml::find(t, "loader"); | ||
|
||
if (table_loader.contains("file")) { | ||
const auto& loader_file = toml::find(table_loader, "file"); | ||
|
||
if (loader_file.is_string()) { | ||
cfg.loader_file = toml::get<std::string>(loader_file); | ||
} | ||
} | ||
} | ||
|
||
logger->info("Config loaded"); | ||
|
||
logger->debug("stealer.enabled = {}, loader.file = {}", cfg.stealer_enabled, cfg.loader_file); | ||
} | ||
catch (const std::exception& ex) { | ||
logger->error("Failed to parse config!"); | ||
logger->error(ex.what()); | ||
} | ||
} | ||
|
||
const glt::config::Config glt::config::GetConfig() { | ||
return cfg; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* Copyright (C) 2019 Lewis Clark | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <https://www.gnu.org/licenses/>. */ | ||
|
||
|
||
#ifndef CONFIG_H | ||
#define CONFIG_H | ||
|
||
#include <toml.hpp> | ||
|
||
#include "logger.hpp" | ||
#include "file/file.hpp" | ||
|
||
namespace glt::config { | ||
const std::string DEFAULT_CONFIG = "[general]\n\n[stealer]\nenabled = true\n\n[loader]\nfile = \"gluasteal.lua\"\n\n"; | ||
|
||
struct Config { | ||
bool stealer_enabled = true; | ||
std::string loader_file = "gluasteal.lua"; | ||
}; | ||
|
||
void LoadConfig(); | ||
const Config GetConfig(); | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters