shadPS4/src/common/config.cpp

119 lines
3.2 KiB
C++
Raw Normal View History

2024-02-23 22:32:32 +01:00
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
2023-08-14 19:17:01 +02:00
#include <fstream>
#include <string>
#include <fmt/core.h>
2023-11-05 12:22:18 +01:00
#include <toml.hpp>
2024-02-23 22:32:32 +01:00
#include "config.h"
2023-08-14 19:17:01 +02:00
namespace Config {
bool isNeo = false;
2023-08-16 09:34:04 +02:00
u32 screenWidth = 1280;
u32 screenHeight = 720;
std::string logFilter;
std::string logType = "sync";
bool isDebugDump = false;
2023-08-14 19:17:01 +02:00
bool isNeoMode() {
return isNeo;
}
u32 getScreenWidth() {
return screenWidth;
}
u32 getScreenHeight() {
return screenHeight;
}
std::string getLogFilter() {
return logFilter;
}
2023-08-16 09:34:04 +02:00
std::string getLogType() {
return logType;
}
bool debugDump() {
return isDebugDump;
}
2023-08-14 19:17:01 +02:00
void load(const std::filesystem::path& path) {
// If the configuration file does not exist, create it and return
std::error_code error;
if (!std::filesystem::exists(path, error)) {
save(path);
return;
}
toml::value data;
try {
data = toml::parse(path);
} catch (std::exception& ex) {
fmt::print("Got exception trying to load config file. Exception: {}\n", ex.what());
2023-08-14 19:17:01 +02:00
return;
}
if (data.contains("General")) {
auto generalResult = toml::expect<toml::value>(data.at("General"));
if (generalResult.is_ok()) {
auto general = generalResult.unwrap();
isNeo = toml::find_or<toml::boolean>(general, "isPS4Pro", false);
logFilter = toml::find_or<toml::string>(general, "logFilter", "");
logType = toml::find_or<toml::string>(general, "logType", "sync");
2023-08-16 09:34:04 +02:00
}
}
if (data.contains("GPU")) {
auto generalResult = toml::expect<toml::value>(data.at("GPU"));
if (generalResult.is_ok()) {
auto general = generalResult.unwrap();
screenWidth = toml::find_or<toml::integer>(general, "screenWidth", false);
screenHeight = toml::find_or<toml::integer>(general, "screenHeight", false);
2023-08-14 19:17:01 +02:00
}
}
if (data.contains("Debug")) {
auto debugResult = toml::expect<toml::value>(data.at("Debug"));
if (debugResult.is_ok()) {
auto debug = debugResult.unwrap();
isDebugDump = toml::find_or<toml::boolean>(debug, "DebugDump", false);
}
}
2023-08-14 19:17:01 +02:00
}
void save(const std::filesystem::path& path) {
toml::basic_value<toml::preserve_comments> data;
std::error_code error;
if (std::filesystem::exists(path, error)) {
try {
data = toml::parse<toml::preserve_comments>(path);
} catch (const std::exception& ex) {
fmt::print("Exception trying to parse config file. Exception: {}\n", ex.what());
2023-08-14 19:17:01 +02:00
return;
}
} else {
if (error) {
fmt::print("Filesystem error accessing {} (error: {})\n", path.string(),
error.message().c_str());
2023-08-14 19:17:01 +02:00
}
fmt::print("Saving new configuration file {}\n", path.string());
2023-08-14 19:17:01 +02:00
}
data["General"]["isPS4Pro"] = isNeo;
data["General"]["logFilter"] = logFilter;
data["General"]["logType"] = logType;
2023-08-16 09:34:04 +02:00
data["GPU"]["screenWidth"] = screenWidth;
data["GPU"]["screenHeight"] = screenHeight;
data["Debug"]["DebugDump"] = isDebugDump;
2023-08-14 19:17:01 +02:00
std::ofstream file(path, std::ios::out);
file << data;
file.close();
}
} // namespace Config