| 49 | |
| 50 | template <class T> |
| 51 | static void MaybeSaveJSON( |
| 52 | const T& parentValue, |
| 53 | const T& value, |
| 54 | const std::filesystem::path& path) { |
| 55 | const auto fullPath |
| 56 | = path.is_absolute() ? path : Filesystem::GetSettingsDirectory() / path; |
| 57 | |
| 58 | nlohmann::json j; |
| 59 | // If a profile already modified a setting, keep that setting even if it |
| 60 | // matches the parent now |
| 61 | if (std::filesystem::exists(fullPath)) { |
| 62 | std::ifstream f(fullPath); |
| 63 | try { |
| 64 | f >> j; |
| 65 | } catch (const nlohmann::json::exception& e) { |
| 66 | dprint( |
| 67 | "Error reading JSON from file '{}': {}", fullPath.string(), e.what()); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | const auto parentPath = fullPath.parent_path(); |
| 72 | if (!std::filesystem::exists(parentPath)) { |
| 73 | std::filesystem::create_directories(parentPath); |
| 74 | } |
| 75 | |
| 76 | to_json_with_default(j, parentValue, value); |
| 77 | if (j.is_object() && j.size() > 0) { |
| 78 | std::ofstream f(fullPath); |
| 79 | f << std::setw(2) << j << std::endl; |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | if (std::filesystem::exists(fullPath)) { |
| 84 | std::filesystem::remove(fullPath); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** Used for GamesList and TabsList, where we don't want to merge configs - |
| 89 | * either inherit, or overwrite */ |
nothing calls this directly
no test coverage detected