2020-02-10 08:37:19 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <string.h>
|
2020-03-16 15:48:12 +00:00
|
|
|
#include <thread>
|
2020-12-13 11:30:42 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <string>
|
2021-07-16 00:28:46 +00:00
|
|
|
#include <spdlog/spdlog.h>
|
2020-02-10 08:37:19 +00:00
|
|
|
#include "config.h"
|
2020-02-12 21:00:33 +00:00
|
|
|
#include "file_utils.h"
|
2020-03-02 09:30:37 +00:00
|
|
|
#include "string_utils.h"
|
2020-11-03 14:34:18 +00:00
|
|
|
#include "hud_elements.h"
|
2022-07-26 19:28:10 +00:00
|
|
|
#include "blacklist.h"
|
2020-08-15 13:14:55 +00:00
|
|
|
|
2023-04-06 06:35:44 +00:00
|
|
|
void parseConfigLine(std::string line, std::unordered_map<std::string, std::string>& options) {
|
2020-03-02 09:30:37 +00:00
|
|
|
std::string param, value;
|
|
|
|
|
|
|
|
if (line.find("#") != std::string::npos)
|
|
|
|
line = line.erase(line.find("#"), std::string::npos);
|
|
|
|
|
2020-02-10 08:37:19 +00:00
|
|
|
size_t equal = line.find("=");
|
2020-03-02 09:30:37 +00:00
|
|
|
if (equal == std::string::npos)
|
|
|
|
value = "1";
|
|
|
|
else
|
|
|
|
value = line.substr(equal+1);
|
|
|
|
|
|
|
|
param = line.substr(0, equal);
|
|
|
|
trim(param);
|
|
|
|
trim(value);
|
2020-11-03 14:34:18 +00:00
|
|
|
if (!param.empty()){
|
2020-11-06 23:01:23 +00:00
|
|
|
HUDElements.options.push_back({param, value});
|
2020-03-02 09:30:37 +00:00
|
|
|
options[param] = value;
|
2020-11-03 14:34:18 +00:00
|
|
|
}
|
2020-02-10 08:37:19 +00:00
|
|
|
}
|
|
|
|
|
2020-12-13 11:30:42 +00:00
|
|
|
static std::string get_program_dir() {
|
|
|
|
const std::string exe_path = get_exe_path();
|
|
|
|
if (exe_path.empty()) {
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
const auto n = exe_path.find_last_of('/');
|
|
|
|
if (n != std::string::npos) {
|
|
|
|
return exe_path.substr(0, n);
|
|
|
|
}
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string get_program_name() {
|
|
|
|
const std::string exe_path = get_exe_path();
|
|
|
|
std::string basename = "unknown";
|
|
|
|
if (exe_path.empty()) {
|
|
|
|
return basename;
|
|
|
|
}
|
|
|
|
const auto n = exe_path.find_last_of('/');
|
|
|
|
if (n == std::string::npos) {
|
|
|
|
return basename;
|
|
|
|
}
|
|
|
|
if (n < exe_path.size() - 1) {
|
|
|
|
// An executable's name.
|
|
|
|
basename = exe_path.substr(n + 1);
|
|
|
|
}
|
|
|
|
return basename;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void enumerate_config_files(std::vector<std::string>& paths) {
|
2020-02-15 11:04:05 +00:00
|
|
|
static const char *mangohud_dir = "/MangoHud/";
|
2020-02-12 21:00:33 +00:00
|
|
|
|
2020-12-13 11:30:42 +00:00
|
|
|
const std::string data_dir = get_data_dir();
|
|
|
|
const std::string config_dir = get_config_dir();
|
|
|
|
|
|
|
|
const std::string program_name = get_program_name();
|
|
|
|
|
|
|
|
if (config_dir.empty()) {
|
|
|
|
// If we can't find 'HOME' just abandon hope.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
paths.push_back(config_dir + mangohud_dir + "MangoHud.conf");
|
2023-05-05 01:04:15 +00:00
|
|
|
paths.push_back("/etc/MangoHud.conf");
|
2020-02-15 11:04:05 +00:00
|
|
|
|
2022-07-26 19:28:10 +00:00
|
|
|
if (is_blacklisted()) {
|
|
|
|
// Don't bother looking for conf file
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-13 11:30:42 +00:00
|
|
|
if (!program_name.empty()) {
|
|
|
|
paths.push_back(config_dir + mangohud_dir + program_name + ".conf");
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string program_dir = get_program_dir();
|
|
|
|
if (!program_dir.empty()) {
|
|
|
|
paths.push_back(program_dir + "/MangoHud.conf");
|
2020-02-12 21:00:33 +00:00
|
|
|
}
|
2020-12-13 11:30:42 +00:00
|
|
|
|
|
|
|
const std::string wine_program_name = get_wine_exe_name();
|
|
|
|
if (!wine_program_name.empty()) {
|
|
|
|
paths.push_back(config_dir + mangohud_dir + "wine-" + wine_program_name + ".conf");
|
|
|
|
}
|
2020-03-28 21:59:54 +00:00
|
|
|
}
|
|
|
|
|
2022-03-06 19:02:39 +00:00
|
|
|
void parseConfigFile(overlay_params& params) {
|
2020-03-28 21:59:54 +00:00
|
|
|
std::vector<std::string> paths;
|
|
|
|
const char *cfg_file = getenv("MANGOHUD_CONFIGFILE");
|
|
|
|
|
|
|
|
if (cfg_file)
|
|
|
|
paths.push_back(cfg_file);
|
|
|
|
else
|
|
|
|
enumerate_config_files(paths);
|
2021-11-12 04:44:53 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
paths.push_back("C:\\mangohud\\MangoHud.conf");
|
|
|
|
#endif
|
2020-02-10 08:37:19 +00:00
|
|
|
std::string line;
|
2020-02-18 07:32:45 +00:00
|
|
|
for (auto p = paths.rbegin(); p != paths.rend(); p++) {
|
|
|
|
std::ifstream stream(*p);
|
2020-02-13 13:19:42 +00:00
|
|
|
if (!stream.good()) {
|
|
|
|
// printing just so user has an idea of possible configs
|
2023-05-20 06:26:03 +00:00
|
|
|
SPDLOG_DEBUG("skipping config: '{}' [ not found ]", *p);
|
2020-02-13 13:19:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-06-19 12:21:33 +00:00
|
|
|
stream.imbue(std::locale::classic());
|
2023-05-20 06:26:03 +00:00
|
|
|
SPDLOG_DEBUG("parsing config: '{}'", *p);
|
2020-02-12 21:00:33 +00:00
|
|
|
while (std::getline(stream, line))
|
|
|
|
{
|
2020-03-16 16:32:48 +00:00
|
|
|
parseConfigLine(line, params.options);
|
2020-02-12 21:00:33 +00:00
|
|
|
}
|
2020-03-16 16:32:48 +00:00
|
|
|
params.config_file_path = *p;
|
2022-03-06 19:02:39 +00:00
|
|
|
return;
|
2020-02-10 08:37:19 +00:00
|
|
|
}
|
2020-02-15 11:04:05 +00:00
|
|
|
}
|