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-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-02-10 15:59:49 +00:00
|
|
|
|
2020-08-15 13:14:55 +00:00
|
|
|
std::string program_name;
|
|
|
|
|
2020-03-16 16:32:48 +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);
|
|
|
|
if (!param.empty())
|
|
|
|
options[param] = value;
|
2020-02-10 08:37:19 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 21:59:54 +00:00
|
|
|
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-02-15 11:04:05 +00:00
|
|
|
std::string env_data = get_data_dir();
|
|
|
|
std::string env_config = get_config_dir();
|
|
|
|
|
|
|
|
if (!env_config.empty())
|
|
|
|
paths.push_back(env_config + mangohud_dir + "MangoHud.conf");
|
2020-02-12 21:00:33 +00:00
|
|
|
|
|
|
|
std::string exe_path = get_exe_path();
|
|
|
|
auto n = exe_path.find_last_of('/');
|
2020-02-13 11:28:06 +00:00
|
|
|
if (!exe_path.empty() && n != std::string::npos && n < exe_path.size() - 1) {
|
2020-02-12 21:00:33 +00:00
|
|
|
// as executable's name
|
2020-02-13 11:28:06 +00:00
|
|
|
std::string basename = exe_path.substr(n + 1);
|
2020-08-15 13:14:55 +00:00
|
|
|
program_name = basename;
|
2020-02-15 11:04:05 +00:00
|
|
|
if (!env_config.empty())
|
|
|
|
paths.push_back(env_config + mangohud_dir + basename + ".conf");
|
2020-02-12 21:00:33 +00:00
|
|
|
|
|
|
|
// in executable's folder though not much sense in /usr/bin/
|
|
|
|
paths.push_back(exe_path.substr(0, n) + "/MangoHud.conf");
|
2020-02-13 11:28:06 +00:00
|
|
|
|
|
|
|
// find executable's path when run in Wine
|
2020-02-15 11:04:05 +00:00
|
|
|
if (!env_config.empty() && (basename == "wine-preloader" || basename == "wine64-preloader")) {
|
2020-08-15 13:14:55 +00:00
|
|
|
|
2020-03-25 15:08:08 +00:00
|
|
|
std::string name;
|
|
|
|
if (get_wine_exe_name(name)) {
|
|
|
|
paths.push_back(env_config + mangohud_dir + "wine-" + name + ".conf");
|
2020-02-13 11:28:06 +00:00
|
|
|
}
|
2020-08-15 13:14:55 +00:00
|
|
|
program_name = name;
|
2020-02-13 11:28:06 +00:00
|
|
|
}
|
2020-02-12 21:00:33 +00:00
|
|
|
}
|
2020-08-15 13:14:55 +00:00
|
|
|
if (program_name.empty())
|
|
|
|
program_name = "Unknown";
|
2020-03-28 21:59:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void parseConfigFile(overlay_params& params) {
|
|
|
|
params.options.clear();
|
|
|
|
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);
|
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
|
2020-03-13 14:14:58 +00:00
|
|
|
std::cerr << "skipping config: " << *p << " [ not found ]" << std::endl;
|
2020-02-13 13:19:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-06-19 12:21:33 +00:00
|
|
|
stream.imbue(std::locale::classic());
|
2020-02-18 07:32:45 +00:00
|
|
|
std::cerr << "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-02-13 11:47:14 +00:00
|
|
|
std::cerr << " [ ok ]" << std::endl;
|
2020-03-16 16:32:48 +00:00
|
|
|
params.config_file_path = *p;
|
2020-02-18 07:32:45 +00:00
|
|
|
return;
|
2020-02-10 08:37:19 +00:00
|
|
|
}
|
2020-02-15 11:04:05 +00:00
|
|
|
}
|